]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf-m10300.c
PR ld/13991
[thirdparty/binutils-gdb.git] / bfd / elf-m10300.c
1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/mn10300.h"
27 #include "libiberty.h"
28
29 /* The mn10300 linker needs to keep track of the number of relocs that
30 it decides to copy in check_relocs for each symbol. This is so
31 that it can discard PC relative relocs if it doesn't need them when
32 linking with -Bsymbolic. We store the information in a field
33 extending the regular ELF linker hash table. */
34
35 struct elf32_mn10300_link_hash_entry
36 {
37 /* The basic elf link hash table entry. */
38 struct elf_link_hash_entry root;
39
40 /* For function symbols, the number of times this function is
41 called directly (ie by name). */
42 unsigned int direct_calls;
43
44 /* For function symbols, the size of this function's stack
45 (if <= 255 bytes). We stuff this into "call" instructions
46 to this target when it's valid and profitable to do so.
47
48 This does not include stack allocated by movm! */
49 unsigned char stack_size;
50
51 /* For function symbols, arguments (if any) for movm instruction
52 in the prologue. We stuff this value into "call" instructions
53 to the target when it's valid and profitable to do so. */
54 unsigned char movm_args;
55
56 /* For function symbols, the amount of stack space that would be allocated
57 by the movm instruction. This is redundant with movm_args, but we
58 add it to the hash table to avoid computing it over and over. */
59 unsigned char movm_stack_size;
60
61 /* When set, convert all "call" instructions to this target into "calls"
62 instructions. */
63 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
64
65 /* Used to mark functions which have had redundant parts of their
66 prologue deleted. */
67 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
68 unsigned char flags;
69
70 /* Calculated value. */
71 bfd_vma value;
72
73 #define GOT_UNKNOWN 0
74 #define GOT_NORMAL 1
75 #define GOT_TLS_GD 2
76 #define GOT_TLS_LD 3
77 #define GOT_TLS_IE 4
78 /* Used to distinguish GOT entries for TLS types from normal GOT entries. */
79 unsigned char tls_type;
80 };
81
82 /* We derive a hash table from the main elf linker hash table so
83 we can store state variables and a secondary hash table without
84 resorting to global variables. */
85 struct elf32_mn10300_link_hash_table
86 {
87 /* The main hash table. */
88 struct elf_link_hash_table root;
89
90 /* A hash table for static functions. We could derive a new hash table
91 instead of using the full elf32_mn10300_link_hash_table if we wanted
92 to save some memory. */
93 struct elf32_mn10300_link_hash_table *static_hash_table;
94
95 /* Random linker state flags. */
96 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
97 char flags;
98 struct
99 {
100 bfd_signed_vma refcount;
101 bfd_vma offset;
102 char got_allocated;
103 char rel_emitted;
104 } tls_ldm_got;
105 };
106
107 #define elf_mn10300_hash_entry(ent) ((struct elf32_mn10300_link_hash_entry *)(ent))
108
109 struct elf_mn10300_obj_tdata
110 {
111 struct elf_obj_tdata root;
112
113 /* tls_type for each local got entry. */
114 char * local_got_tls_type;
115 };
116
117 #define elf_mn10300_tdata(abfd) \
118 ((struct elf_mn10300_obj_tdata *) (abfd)->tdata.any)
119
120 #define elf_mn10300_local_got_tls_type(abfd) \
121 (elf_mn10300_tdata (abfd)->local_got_tls_type)
122
123 #ifndef streq
124 #define streq(a, b) (strcmp ((a),(b)) == 0)
125 #endif
126
127 /* For MN10300 linker hash table. */
128
129 /* Get the MN10300 ELF linker hash table from a link_info structure. */
130
131 #define elf32_mn10300_hash_table(p) \
132 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
133 == MN10300_ELF_DATA ? ((struct elf32_mn10300_link_hash_table *) ((p)->hash)) : NULL)
134
135 #define elf32_mn10300_link_hash_traverse(table, func, info) \
136 (elf_link_hash_traverse \
137 (&(table)->root, \
138 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
139 (info)))
140
141 static reloc_howto_type elf_mn10300_howto_table[] =
142 {
143 /* Dummy relocation. Does nothing. */
144 HOWTO (R_MN10300_NONE,
145 0,
146 2,
147 16,
148 FALSE,
149 0,
150 complain_overflow_bitfield,
151 bfd_elf_generic_reloc,
152 "R_MN10300_NONE",
153 FALSE,
154 0,
155 0,
156 FALSE),
157 /* Standard 32 bit reloc. */
158 HOWTO (R_MN10300_32,
159 0,
160 2,
161 32,
162 FALSE,
163 0,
164 complain_overflow_bitfield,
165 bfd_elf_generic_reloc,
166 "R_MN10300_32",
167 FALSE,
168 0xffffffff,
169 0xffffffff,
170 FALSE),
171 /* Standard 16 bit reloc. */
172 HOWTO (R_MN10300_16,
173 0,
174 1,
175 16,
176 FALSE,
177 0,
178 complain_overflow_bitfield,
179 bfd_elf_generic_reloc,
180 "R_MN10300_16",
181 FALSE,
182 0xffff,
183 0xffff,
184 FALSE),
185 /* Standard 8 bit reloc. */
186 HOWTO (R_MN10300_8,
187 0,
188 0,
189 8,
190 FALSE,
191 0,
192 complain_overflow_bitfield,
193 bfd_elf_generic_reloc,
194 "R_MN10300_8",
195 FALSE,
196 0xff,
197 0xff,
198 FALSE),
199 /* Standard 32bit pc-relative reloc. */
200 HOWTO (R_MN10300_PCREL32,
201 0,
202 2,
203 32,
204 TRUE,
205 0,
206 complain_overflow_bitfield,
207 bfd_elf_generic_reloc,
208 "R_MN10300_PCREL32",
209 FALSE,
210 0xffffffff,
211 0xffffffff,
212 TRUE),
213 /* Standard 16bit pc-relative reloc. */
214 HOWTO (R_MN10300_PCREL16,
215 0,
216 1,
217 16,
218 TRUE,
219 0,
220 complain_overflow_bitfield,
221 bfd_elf_generic_reloc,
222 "R_MN10300_PCREL16",
223 FALSE,
224 0xffff,
225 0xffff,
226 TRUE),
227 /* Standard 8 pc-relative reloc. */
228 HOWTO (R_MN10300_PCREL8,
229 0,
230 0,
231 8,
232 TRUE,
233 0,
234 complain_overflow_bitfield,
235 bfd_elf_generic_reloc,
236 "R_MN10300_PCREL8",
237 FALSE,
238 0xff,
239 0xff,
240 TRUE),
241
242 /* GNU extension to record C++ vtable hierarchy. */
243 HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
244 0, /* rightshift */
245 0, /* size (0 = byte, 1 = short, 2 = long) */
246 0, /* bitsize */
247 FALSE, /* pc_relative */
248 0, /* bitpos */
249 complain_overflow_dont, /* complain_on_overflow */
250 NULL, /* special_function */
251 "R_MN10300_GNU_VTINHERIT", /* name */
252 FALSE, /* partial_inplace */
253 0, /* src_mask */
254 0, /* dst_mask */
255 FALSE), /* pcrel_offset */
256
257 /* GNU extension to record C++ vtable member usage */
258 HOWTO (R_MN10300_GNU_VTENTRY, /* type */
259 0, /* rightshift */
260 0, /* size (0 = byte, 1 = short, 2 = long) */
261 0, /* bitsize */
262 FALSE, /* pc_relative */
263 0, /* bitpos */
264 complain_overflow_dont, /* complain_on_overflow */
265 NULL, /* special_function */
266 "R_MN10300_GNU_VTENTRY", /* name */
267 FALSE, /* partial_inplace */
268 0, /* src_mask */
269 0, /* dst_mask */
270 FALSE), /* pcrel_offset */
271
272 /* Standard 24 bit reloc. */
273 HOWTO (R_MN10300_24,
274 0,
275 2,
276 24,
277 FALSE,
278 0,
279 complain_overflow_bitfield,
280 bfd_elf_generic_reloc,
281 "R_MN10300_24",
282 FALSE,
283 0xffffff,
284 0xffffff,
285 FALSE),
286 HOWTO (R_MN10300_GOTPC32, /* type */
287 0, /* rightshift */
288 2, /* size (0 = byte, 1 = short, 2 = long) */
289 32, /* bitsize */
290 TRUE, /* pc_relative */
291 0, /* bitpos */
292 complain_overflow_bitfield, /* complain_on_overflow */
293 bfd_elf_generic_reloc, /* */
294 "R_MN10300_GOTPC32", /* name */
295 FALSE, /* partial_inplace */
296 0xffffffff, /* src_mask */
297 0xffffffff, /* dst_mask */
298 TRUE), /* pcrel_offset */
299
300 HOWTO (R_MN10300_GOTPC16, /* type */
301 0, /* rightshift */
302 1, /* size (0 = byte, 1 = short, 2 = long) */
303 16, /* bitsize */
304 TRUE, /* pc_relative */
305 0, /* bitpos */
306 complain_overflow_bitfield, /* complain_on_overflow */
307 bfd_elf_generic_reloc, /* */
308 "R_MN10300_GOTPC16", /* name */
309 FALSE, /* partial_inplace */
310 0xffff, /* src_mask */
311 0xffff, /* dst_mask */
312 TRUE), /* pcrel_offset */
313
314 HOWTO (R_MN10300_GOTOFF32, /* type */
315 0, /* rightshift */
316 2, /* size (0 = byte, 1 = short, 2 = long) */
317 32, /* bitsize */
318 FALSE, /* pc_relative */
319 0, /* bitpos */
320 complain_overflow_bitfield, /* complain_on_overflow */
321 bfd_elf_generic_reloc, /* */
322 "R_MN10300_GOTOFF32", /* name */
323 FALSE, /* partial_inplace */
324 0xffffffff, /* src_mask */
325 0xffffffff, /* dst_mask */
326 FALSE), /* pcrel_offset */
327
328 HOWTO (R_MN10300_GOTOFF24, /* type */
329 0, /* rightshift */
330 2, /* size (0 = byte, 1 = short, 2 = long) */
331 24, /* bitsize */
332 FALSE, /* pc_relative */
333 0, /* bitpos */
334 complain_overflow_bitfield, /* complain_on_overflow */
335 bfd_elf_generic_reloc, /* */
336 "R_MN10300_GOTOFF24", /* name */
337 FALSE, /* partial_inplace */
338 0xffffff, /* src_mask */
339 0xffffff, /* dst_mask */
340 FALSE), /* pcrel_offset */
341
342 HOWTO (R_MN10300_GOTOFF16, /* type */
343 0, /* rightshift */
344 1, /* size (0 = byte, 1 = short, 2 = long) */
345 16, /* bitsize */
346 FALSE, /* pc_relative */
347 0, /* bitpos */
348 complain_overflow_bitfield, /* complain_on_overflow */
349 bfd_elf_generic_reloc, /* */
350 "R_MN10300_GOTOFF16", /* name */
351 FALSE, /* partial_inplace */
352 0xffff, /* src_mask */
353 0xffff, /* dst_mask */
354 FALSE), /* pcrel_offset */
355
356 HOWTO (R_MN10300_PLT32, /* type */
357 0, /* rightshift */
358 2, /* size (0 = byte, 1 = short, 2 = long) */
359 32, /* bitsize */
360 TRUE, /* pc_relative */
361 0, /* bitpos */
362 complain_overflow_bitfield, /* complain_on_overflow */
363 bfd_elf_generic_reloc, /* */
364 "R_MN10300_PLT32", /* name */
365 FALSE, /* partial_inplace */
366 0xffffffff, /* src_mask */
367 0xffffffff, /* dst_mask */
368 TRUE), /* pcrel_offset */
369
370 HOWTO (R_MN10300_PLT16, /* type */
371 0, /* rightshift */
372 1, /* size (0 = byte, 1 = short, 2 = long) */
373 16, /* bitsize */
374 TRUE, /* pc_relative */
375 0, /* bitpos */
376 complain_overflow_bitfield, /* complain_on_overflow */
377 bfd_elf_generic_reloc, /* */
378 "R_MN10300_PLT16", /* name */
379 FALSE, /* partial_inplace */
380 0xffff, /* src_mask */
381 0xffff, /* dst_mask */
382 TRUE), /* pcrel_offset */
383
384 HOWTO (R_MN10300_GOT32, /* type */
385 0, /* rightshift */
386 2, /* size (0 = byte, 1 = short, 2 = long) */
387 32, /* bitsize */
388 FALSE, /* pc_relative */
389 0, /* bitpos */
390 complain_overflow_bitfield, /* complain_on_overflow */
391 bfd_elf_generic_reloc, /* */
392 "R_MN10300_GOT32", /* name */
393 FALSE, /* partial_inplace */
394 0xffffffff, /* src_mask */
395 0xffffffff, /* dst_mask */
396 FALSE), /* pcrel_offset */
397
398 HOWTO (R_MN10300_GOT24, /* type */
399 0, /* rightshift */
400 2, /* size (0 = byte, 1 = short, 2 = long) */
401 24, /* bitsize */
402 FALSE, /* pc_relative */
403 0, /* bitpos */
404 complain_overflow_bitfield, /* complain_on_overflow */
405 bfd_elf_generic_reloc, /* */
406 "R_MN10300_GOT24", /* name */
407 FALSE, /* partial_inplace */
408 0xffffffff, /* src_mask */
409 0xffffffff, /* dst_mask */
410 FALSE), /* pcrel_offset */
411
412 HOWTO (R_MN10300_GOT16, /* type */
413 0, /* rightshift */
414 1, /* size (0 = byte, 1 = short, 2 = long) */
415 16, /* bitsize */
416 FALSE, /* pc_relative */
417 0, /* bitpos */
418 complain_overflow_bitfield, /* complain_on_overflow */
419 bfd_elf_generic_reloc, /* */
420 "R_MN10300_GOT16", /* name */
421 FALSE, /* partial_inplace */
422 0xffffffff, /* src_mask */
423 0xffffffff, /* dst_mask */
424 FALSE), /* pcrel_offset */
425
426 HOWTO (R_MN10300_COPY, /* type */
427 0, /* rightshift */
428 2, /* size (0 = byte, 1 = short, 2 = long) */
429 32, /* bitsize */
430 FALSE, /* pc_relative */
431 0, /* bitpos */
432 complain_overflow_bitfield, /* complain_on_overflow */
433 bfd_elf_generic_reloc, /* */
434 "R_MN10300_COPY", /* name */
435 FALSE, /* partial_inplace */
436 0xffffffff, /* src_mask */
437 0xffffffff, /* dst_mask */
438 FALSE), /* pcrel_offset */
439
440 HOWTO (R_MN10300_GLOB_DAT, /* type */
441 0, /* rightshift */
442 2, /* size (0 = byte, 1 = short, 2 = long) */
443 32, /* bitsize */
444 FALSE, /* pc_relative */
445 0, /* bitpos */
446 complain_overflow_bitfield, /* complain_on_overflow */
447 bfd_elf_generic_reloc, /* */
448 "R_MN10300_GLOB_DAT", /* name */
449 FALSE, /* partial_inplace */
450 0xffffffff, /* src_mask */
451 0xffffffff, /* dst_mask */
452 FALSE), /* pcrel_offset */
453
454 HOWTO (R_MN10300_JMP_SLOT, /* type */
455 0, /* rightshift */
456 2, /* size (0 = byte, 1 = short, 2 = long) */
457 32, /* bitsize */
458 FALSE, /* pc_relative */
459 0, /* bitpos */
460 complain_overflow_bitfield, /* complain_on_overflow */
461 bfd_elf_generic_reloc, /* */
462 "R_MN10300_JMP_SLOT", /* name */
463 FALSE, /* partial_inplace */
464 0xffffffff, /* src_mask */
465 0xffffffff, /* dst_mask */
466 FALSE), /* pcrel_offset */
467
468 HOWTO (R_MN10300_RELATIVE, /* type */
469 0, /* rightshift */
470 2, /* size (0 = byte, 1 = short, 2 = long) */
471 32, /* bitsize */
472 FALSE, /* pc_relative */
473 0, /* bitpos */
474 complain_overflow_bitfield, /* complain_on_overflow */
475 bfd_elf_generic_reloc, /* */
476 "R_MN10300_RELATIVE", /* name */
477 FALSE, /* partial_inplace */
478 0xffffffff, /* src_mask */
479 0xffffffff, /* dst_mask */
480 FALSE), /* pcrel_offset */
481
482 HOWTO (R_MN10300_TLS_GD, /* type */
483 0, /* rightshift */
484 2, /* size (0 = byte, 1 = short, 2 = long) */
485 32, /* bitsize */
486 FALSE, /* pc_relative */
487 0, /* bitpos */
488 complain_overflow_bitfield, /* complain_on_overflow */
489 bfd_elf_generic_reloc, /* */
490 "R_MN10300_TLS_GD", /* name */
491 FALSE, /* partial_inplace */
492 0xffffffff, /* src_mask */
493 0xffffffff, /* dst_mask */
494 FALSE), /* pcrel_offset */
495
496 HOWTO (R_MN10300_TLS_LD, /* type */
497 0, /* rightshift */
498 2, /* size (0 = byte, 1 = short, 2 = long) */
499 32, /* bitsize */
500 FALSE, /* pc_relative */
501 0, /* bitpos */
502 complain_overflow_bitfield, /* complain_on_overflow */
503 bfd_elf_generic_reloc, /* */
504 "R_MN10300_TLS_LD", /* name */
505 FALSE, /* partial_inplace */
506 0xffffffff, /* src_mask */
507 0xffffffff, /* dst_mask */
508 FALSE), /* pcrel_offset */
509
510 HOWTO (R_MN10300_TLS_LDO, /* type */
511 0, /* rightshift */
512 2, /* size (0 = byte, 1 = short, 2 = long) */
513 32, /* bitsize */
514 FALSE, /* pc_relative */
515 0, /* bitpos */
516 complain_overflow_bitfield, /* complain_on_overflow */
517 bfd_elf_generic_reloc, /* */
518 "R_MN10300_TLS_LDO", /* name */
519 FALSE, /* partial_inplace */
520 0xffffffff, /* src_mask */
521 0xffffffff, /* dst_mask */
522 FALSE), /* pcrel_offset */
523
524 HOWTO (R_MN10300_TLS_GOTIE, /* type */
525 0, /* rightshift */
526 2, /* size (0 = byte, 1 = short, 2 = long) */
527 32, /* bitsize */
528 FALSE, /* pc_relative */
529 0, /* bitpos */
530 complain_overflow_bitfield, /* complain_on_overflow */
531 bfd_elf_generic_reloc, /* */
532 "R_MN10300_TLS_GOTIE", /* name */
533 FALSE, /* partial_inplace */
534 0xffffffff, /* src_mask */
535 0xffffffff, /* dst_mask */
536 FALSE), /* pcrel_offset */
537
538 HOWTO (R_MN10300_TLS_IE, /* type */
539 0, /* rightshift */
540 2, /* size (0 = byte, 1 = short, 2 = long) */
541 32, /* bitsize */
542 FALSE, /* pc_relative */
543 0, /* bitpos */
544 complain_overflow_bitfield, /* complain_on_overflow */
545 bfd_elf_generic_reloc, /* */
546 "R_MN10300_TLS_IE", /* name */
547 FALSE, /* partial_inplace */
548 0xffffffff, /* src_mask */
549 0xffffffff, /* dst_mask */
550 FALSE), /* pcrel_offset */
551
552 HOWTO (R_MN10300_TLS_LE, /* type */
553 0, /* rightshift */
554 2, /* size (0 = byte, 1 = short, 2 = long) */
555 32, /* bitsize */
556 FALSE, /* pc_relative */
557 0, /* bitpos */
558 complain_overflow_bitfield, /* complain_on_overflow */
559 bfd_elf_generic_reloc, /* */
560 "R_MN10300_TLS_LE", /* name */
561 FALSE, /* partial_inplace */
562 0xffffffff, /* src_mask */
563 0xffffffff, /* dst_mask */
564 FALSE), /* pcrel_offset */
565
566 HOWTO (R_MN10300_TLS_DTPMOD, /* type */
567 0, /* rightshift */
568 2, /* size (0 = byte, 1 = short, 2 = long) */
569 32, /* bitsize */
570 FALSE, /* pc_relative */
571 0, /* bitpos */
572 complain_overflow_bitfield, /* complain_on_overflow */
573 bfd_elf_generic_reloc, /* */
574 "R_MN10300_TLS_DTPMOD", /* name */
575 FALSE, /* partial_inplace */
576 0xffffffff, /* src_mask */
577 0xffffffff, /* dst_mask */
578 FALSE), /* pcrel_offset */
579
580 HOWTO (R_MN10300_TLS_DTPOFF, /* type */
581 0, /* rightshift */
582 2, /* size (0 = byte, 1 = short, 2 = long) */
583 32, /* bitsize */
584 FALSE, /* pc_relative */
585 0, /* bitpos */
586 complain_overflow_bitfield, /* complain_on_overflow */
587 bfd_elf_generic_reloc, /* */
588 "R_MN10300_TLS_DTPOFF", /* name */
589 FALSE, /* partial_inplace */
590 0xffffffff, /* src_mask */
591 0xffffffff, /* dst_mask */
592 FALSE), /* pcrel_offset */
593
594 HOWTO (R_MN10300_TLS_TPOFF, /* type */
595 0, /* rightshift */
596 2, /* size (0 = byte, 1 = short, 2 = long) */
597 32, /* bitsize */
598 FALSE, /* pc_relative */
599 0, /* bitpos */
600 complain_overflow_bitfield, /* complain_on_overflow */
601 bfd_elf_generic_reloc, /* */
602 "R_MN10300_TLS_TPOFF", /* name */
603 FALSE, /* partial_inplace */
604 0xffffffff, /* src_mask */
605 0xffffffff, /* dst_mask */
606 FALSE), /* pcrel_offset */
607
608 HOWTO (R_MN10300_SYM_DIFF, /* type */
609 0, /* rightshift */
610 2, /* size (0 = byte, 1 = short, 2 = long) */
611 32, /* bitsize */
612 FALSE, /* pc_relative */
613 0, /* bitpos */
614 complain_overflow_dont,/* complain_on_overflow */
615 NULL, /* special handler. */
616 "R_MN10300_SYM_DIFF", /* name */
617 FALSE, /* partial_inplace */
618 0xffffffff, /* src_mask */
619 0xffffffff, /* dst_mask */
620 FALSE), /* pcrel_offset */
621
622 HOWTO (R_MN10300_ALIGN, /* type */
623 0, /* rightshift */
624 0, /* size (0 = byte, 1 = short, 2 = long) */
625 32, /* bitsize */
626 FALSE, /* pc_relative */
627 0, /* bitpos */
628 complain_overflow_dont,/* complain_on_overflow */
629 NULL, /* special handler. */
630 "R_MN10300_ALIGN", /* name */
631 FALSE, /* partial_inplace */
632 0, /* src_mask */
633 0, /* dst_mask */
634 FALSE) /* pcrel_offset */
635 };
636
637 struct mn10300_reloc_map
638 {
639 bfd_reloc_code_real_type bfd_reloc_val;
640 unsigned char elf_reloc_val;
641 };
642
643 static const struct mn10300_reloc_map mn10300_reloc_map[] =
644 {
645 { BFD_RELOC_NONE, R_MN10300_NONE, },
646 { BFD_RELOC_32, R_MN10300_32, },
647 { BFD_RELOC_16, R_MN10300_16, },
648 { BFD_RELOC_8, R_MN10300_8, },
649 { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
650 { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
651 { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
652 { BFD_RELOC_24, R_MN10300_24, },
653 { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
654 { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
655 { BFD_RELOC_32_GOT_PCREL, R_MN10300_GOTPC32 },
656 { BFD_RELOC_16_GOT_PCREL, R_MN10300_GOTPC16 },
657 { BFD_RELOC_32_GOTOFF, R_MN10300_GOTOFF32 },
658 { BFD_RELOC_MN10300_GOTOFF24, R_MN10300_GOTOFF24 },
659 { BFD_RELOC_16_GOTOFF, R_MN10300_GOTOFF16 },
660 { BFD_RELOC_32_PLT_PCREL, R_MN10300_PLT32 },
661 { BFD_RELOC_16_PLT_PCREL, R_MN10300_PLT16 },
662 { BFD_RELOC_MN10300_GOT32, R_MN10300_GOT32 },
663 { BFD_RELOC_MN10300_GOT24, R_MN10300_GOT24 },
664 { BFD_RELOC_MN10300_GOT16, R_MN10300_GOT16 },
665 { BFD_RELOC_MN10300_COPY, R_MN10300_COPY },
666 { BFD_RELOC_MN10300_GLOB_DAT, R_MN10300_GLOB_DAT },
667 { BFD_RELOC_MN10300_JMP_SLOT, R_MN10300_JMP_SLOT },
668 { BFD_RELOC_MN10300_RELATIVE, R_MN10300_RELATIVE },
669 { BFD_RELOC_MN10300_TLS_GD, R_MN10300_TLS_GD },
670 { BFD_RELOC_MN10300_TLS_LD, R_MN10300_TLS_LD },
671 { BFD_RELOC_MN10300_TLS_LDO, R_MN10300_TLS_LDO },
672 { BFD_RELOC_MN10300_TLS_GOTIE, R_MN10300_TLS_GOTIE },
673 { BFD_RELOC_MN10300_TLS_IE, R_MN10300_TLS_IE },
674 { BFD_RELOC_MN10300_TLS_LE, R_MN10300_TLS_LE },
675 { BFD_RELOC_MN10300_TLS_DTPMOD, R_MN10300_TLS_DTPMOD },
676 { BFD_RELOC_MN10300_TLS_DTPOFF, R_MN10300_TLS_DTPOFF },
677 { BFD_RELOC_MN10300_TLS_TPOFF, R_MN10300_TLS_TPOFF },
678 { BFD_RELOC_MN10300_SYM_DIFF, R_MN10300_SYM_DIFF },
679 { BFD_RELOC_MN10300_ALIGN, R_MN10300_ALIGN }
680 };
681
682 /* Create the GOT section. */
683
684 static bfd_boolean
685 _bfd_mn10300_elf_create_got_section (bfd * abfd,
686 struct bfd_link_info * info)
687 {
688 flagword flags;
689 flagword pltflags;
690 asection * s;
691 struct elf_link_hash_entry * h;
692 const struct elf_backend_data * bed = get_elf_backend_data (abfd);
693 int ptralign;
694
695 /* This function may be called more than once. */
696 if (bfd_get_section_by_name (abfd, ".got") != NULL)
697 return TRUE;
698
699 switch (bed->s->arch_size)
700 {
701 case 32:
702 ptralign = 2;
703 break;
704
705 case 64:
706 ptralign = 3;
707 break;
708
709 default:
710 bfd_set_error (bfd_error_bad_value);
711 return FALSE;
712 }
713
714 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
715 | SEC_LINKER_CREATED);
716
717 pltflags = flags;
718 pltflags |= SEC_CODE;
719 if (bed->plt_not_loaded)
720 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
721 if (bed->plt_readonly)
722 pltflags |= SEC_READONLY;
723
724 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
725 if (s == NULL
726 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
727 return FALSE;
728
729 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
730 .plt section. */
731 if (bed->want_plt_sym)
732 {
733 h = _bfd_elf_define_linkage_sym (abfd, info, s,
734 "_PROCEDURE_LINKAGE_TABLE_");
735 elf_hash_table (info)->hplt = h;
736 if (h == NULL)
737 return FALSE;
738 }
739
740 s = bfd_make_section_with_flags (abfd, ".got", flags);
741 if (s == NULL
742 || ! bfd_set_section_alignment (abfd, s, ptralign))
743 return FALSE;
744
745 if (bed->want_got_plt)
746 {
747 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
748 if (s == NULL
749 || ! bfd_set_section_alignment (abfd, s, ptralign))
750 return FALSE;
751 }
752
753 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
754 (or .got.plt) section. We don't do this in the linker script
755 because we don't want to define the symbol if we are not creating
756 a global offset table. */
757 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
758 elf_hash_table (info)->hgot = h;
759 if (h == NULL)
760 return FALSE;
761
762 /* The first bit of the global offset table is the header. */
763 s->size += bed->got_header_size;
764
765 return TRUE;
766 }
767
768 static reloc_howto_type *
769 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
770 bfd_reloc_code_real_type code)
771 {
772 unsigned int i;
773
774 for (i = ARRAY_SIZE (mn10300_reloc_map); i--;)
775 if (mn10300_reloc_map[i].bfd_reloc_val == code)
776 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
777
778 return NULL;
779 }
780
781 static reloc_howto_type *
782 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
783 const char *r_name)
784 {
785 unsigned int i;
786
787 for (i = ARRAY_SIZE (elf_mn10300_howto_table); i--;)
788 if (elf_mn10300_howto_table[i].name != NULL
789 && strcasecmp (elf_mn10300_howto_table[i].name, r_name) == 0)
790 return elf_mn10300_howto_table + i;
791
792 return NULL;
793 }
794
795 /* Set the howto pointer for an MN10300 ELF reloc. */
796
797 static void
798 mn10300_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
799 arelent *cache_ptr,
800 Elf_Internal_Rela *dst)
801 {
802 unsigned int r_type;
803
804 r_type = ELF32_R_TYPE (dst->r_info);
805 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
806 cache_ptr->howto = elf_mn10300_howto_table + r_type;
807 }
808
809 static int
810 elf_mn10300_tls_transition (struct bfd_link_info * info,
811 int r_type,
812 struct elf_link_hash_entry * h,
813 asection * sec,
814 bfd_boolean counting)
815 {
816 bfd_boolean is_local;
817
818 if (r_type == R_MN10300_TLS_GD
819 && h != NULL
820 && elf_mn10300_hash_entry (h)->tls_type == GOT_TLS_IE)
821 return R_MN10300_TLS_GOTIE;
822
823 if (info->shared)
824 return r_type;
825
826 if (! (sec->flags & SEC_CODE))
827 return r_type;
828
829 if (! counting && h != NULL && ! elf_hash_table (info)->dynamic_sections_created)
830 is_local = TRUE;
831 else
832 is_local = SYMBOL_CALLS_LOCAL (info, h);
833
834 /* For the main program, these are the transitions we do. */
835 switch (r_type)
836 {
837 case R_MN10300_TLS_GD: return is_local ? R_MN10300_TLS_LE : R_MN10300_TLS_GOTIE;
838 case R_MN10300_TLS_LD: return R_MN10300_NONE;
839 case R_MN10300_TLS_LDO: return R_MN10300_TLS_LE;
840 case R_MN10300_TLS_IE:
841 case R_MN10300_TLS_GOTIE: return is_local ? R_MN10300_TLS_LE : r_type;
842 }
843
844 return r_type;
845 }
846
847 /* Return the relocation value for @tpoff relocation
848 if STT_TLS virtual address is ADDRESS. */
849
850 static bfd_vma
851 dtpoff (struct bfd_link_info * info, bfd_vma address)
852 {
853 struct elf_link_hash_table *htab = elf_hash_table (info);
854
855 /* If tls_sec is NULL, we should have signalled an error already. */
856 if (htab->tls_sec == NULL)
857 return 0;
858 return address - htab->tls_sec->vma;
859 }
860
861 /* Return the relocation value for @tpoff relocation
862 if STT_TLS virtual address is ADDRESS. */
863
864 static bfd_vma
865 tpoff (struct bfd_link_info * info, bfd_vma address)
866 {
867 struct elf_link_hash_table *htab = elf_hash_table (info);
868
869 /* If tls_sec is NULL, we should have signalled an error already. */
870 if (htab->tls_sec == NULL)
871 return 0;
872 return address - (htab->tls_size + htab->tls_sec->vma);
873 }
874
875 /* Returns nonzero if there's a R_MN10300_PLT32 reloc that we now need
876 to skip, after this one. The actual value is the offset between
877 this reloc and the PLT reloc. */
878
879 static int
880 mn10300_do_tls_transition (bfd * input_bfd,
881 unsigned int r_type,
882 unsigned int tls_r_type,
883 bfd_byte * contents,
884 bfd_vma offset)
885 {
886 bfd_byte *op = contents + offset;
887 int gotreg = 0;
888
889 #define TLS_PAIR(r1,r2) ((r1) * R_MN10300_MAX + (r2))
890
891 /* This is common to all GD/LD transitions, so break it out. */
892 if (r_type == R_MN10300_TLS_GD
893 || r_type == R_MN10300_TLS_LD)
894 {
895 op -= 2;
896 /* mov imm,d0. */
897 BFD_ASSERT (bfd_get_8 (input_bfd, op) == 0xFC);
898 BFD_ASSERT (bfd_get_8 (input_bfd, op + 1) == 0xCC);
899 /* add aN,d0. */
900 BFD_ASSERT (bfd_get_8 (input_bfd, op + 6) == 0xF1);
901 gotreg = (bfd_get_8 (input_bfd, op + 7) & 0x0c) >> 2;
902 /* Call. */
903 BFD_ASSERT (bfd_get_8 (input_bfd, op + 8) == 0xDD);
904 }
905
906 switch (TLS_PAIR (r_type, tls_r_type))
907 {
908 case TLS_PAIR (R_MN10300_TLS_GD, R_MN10300_TLS_GOTIE):
909 {
910 /* Keep track of which register we put GOTptr in. */
911 /* mov (_x@indntpoff,a2),a0. */
912 memcpy (op, "\xFC\x20\x00\x00\x00\x00", 6);
913 op[1] |= gotreg;
914 /* add e2,a0. */
915 memcpy (op+6, "\xF9\x78\x28", 3);
916 /* or 0x00000000, d0 - six byte nop. */
917 memcpy (op+9, "\xFC\xE4\x00\x00\x00\x00", 6);
918 }
919 return 7;
920
921 case TLS_PAIR (R_MN10300_TLS_GD, R_MN10300_TLS_LE):
922 {
923 /* Register is *always* a0. */
924 /* mov _x@tpoff,a0. */
925 memcpy (op, "\xFC\xDC\x00\x00\x00\x00", 6);
926 /* add e2,a0. */
927 memcpy (op+6, "\xF9\x78\x28", 3);
928 /* or 0x00000000, d0 - six byte nop. */
929 memcpy (op+9, "\xFC\xE4\x00\x00\x00\x00", 6);
930 }
931 return 7;
932 case TLS_PAIR (R_MN10300_TLS_LD, R_MN10300_NONE):
933 {
934 /* Register is *always* a0. */
935 /* mov e2,a0. */
936 memcpy (op, "\xF5\x88", 2);
937 /* or 0x00000000, d0 - six byte nop. */
938 memcpy (op+2, "\xFC\xE4\x00\x00\x00\x00", 6);
939 /* or 0x00000000, e2 - seven byte nop. */
940 memcpy (op+8, "\xFE\x19\x22\x00\x00\x00\x00", 7);
941 }
942 return 7;
943
944 case TLS_PAIR (R_MN10300_TLS_LDO, R_MN10300_TLS_LE):
945 /* No changes needed, just the reloc change. */
946 return 0;
947
948 /* These are a little tricky, because we have to detect which
949 opcode is being used (they're different sizes, with the reloc
950 at different offsets within the opcode) and convert each
951 accordingly, copying the operands as needed. The conversions
952 we do are as follows (IE,GOTIE,LE):
953
954 1111 1100 1010 01Dn [-- abs32 --] MOV (x@indntpoff),Dn
955 1111 1100 0000 DnAm [-- abs32 --] MOV (x@gotntpoff,Am),Dn
956 1111 1100 1100 11Dn [-- abs32 --] MOV x@tpoff,Dn
957
958 1111 1100 1010 00An [-- abs32 --] MOV (x@indntpoff),An
959 1111 1100 0010 AnAm [-- abs32 --] MOV (x@gotntpoff,Am),An
960 1111 1100 1101 11An [-- abs32 --] MOV x@tpoff,An
961
962 1111 1110 0000 1110 Rnnn Xxxx [-- abs32 --] MOV (x@indntpoff),Rn
963 1111 1110 0000 1010 Rnnn Rmmm [-- abs32 --] MOV (x@indntpoff,Rm),Rn
964 1111 1110 0000 1000 Rnnn Xxxx [-- abs32 --] MOV x@tpoff,Rn
965
966 Since the GOT pointer is always $a2, we assume the last
967 normally won't happen, but let's be paranoid and plan for the
968 day that GCC optimizes it somewhow. */
969
970 case TLS_PAIR (R_MN10300_TLS_IE, R_MN10300_TLS_LE):
971 if (op[-2] == 0xFC)
972 {
973 op -= 2;
974 if ((op[1] & 0xFC) == 0xA4) /* Dn */
975 {
976 op[1] &= 0x03; /* Leaves Dn. */
977 op[1] |= 0xCC;
978 }
979 else /* An */
980 {
981 op[1] &= 0x03; /* Leaves An. */
982 op[1] |= 0xDC;
983 }
984 }
985 else if (op[-3] == 0xFE)
986 op[-2] = 0x08;
987 else
988 abort ();
989 break;
990
991 case TLS_PAIR (R_MN10300_TLS_GOTIE, R_MN10300_TLS_LE):
992 if (op[-2] == 0xFC)
993 {
994 op -= 2;
995 if ((op[1] & 0xF0) == 0x00) /* Dn */
996 {
997 op[1] &= 0x0C; /* Leaves Dn. */
998 op[1] >>= 2;
999 op[1] |= 0xCC;
1000 }
1001 else /* An */
1002 {
1003 op[1] &= 0x0C; /* Leaves An. */
1004 op[1] >>= 2;
1005 op[1] |= 0xDC;
1006 }
1007 }
1008 else if (op[-3] == 0xFE)
1009 op[-2] = 0x08;
1010 else
1011 abort ();
1012 break;
1013
1014 default:
1015 (*_bfd_error_handler)
1016 (_("%s: Unsupported transition from %s to %s"),
1017 bfd_get_filename (input_bfd),
1018 elf_mn10300_howto_table[r_type].name,
1019 elf_mn10300_howto_table[tls_r_type].name);
1020 break;
1021 }
1022 #undef TLS_PAIR
1023 return 0;
1024 }
1025
1026 /* Look through the relocs for a section during the first phase.
1027 Since we don't do .gots or .plts, we just need to consider the
1028 virtual table relocs for gc. */
1029
1030 static bfd_boolean
1031 mn10300_elf_check_relocs (bfd *abfd,
1032 struct bfd_link_info *info,
1033 asection *sec,
1034 const Elf_Internal_Rela *relocs)
1035 {
1036 struct elf32_mn10300_link_hash_table * htab = elf32_mn10300_hash_table (info);
1037 bfd_boolean sym_diff_reloc_seen;
1038 Elf_Internal_Shdr *symtab_hdr;
1039 Elf_Internal_Sym * isymbuf = NULL;
1040 struct elf_link_hash_entry **sym_hashes;
1041 const Elf_Internal_Rela *rel;
1042 const Elf_Internal_Rela *rel_end;
1043 bfd * dynobj;
1044 bfd_vma * local_got_offsets;
1045 asection * sgot;
1046 asection * srelgot;
1047 asection * sreloc;
1048 bfd_boolean result = FALSE;
1049
1050 sgot = NULL;
1051 srelgot = NULL;
1052 sreloc = NULL;
1053
1054 if (info->relocatable)
1055 return TRUE;
1056
1057 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1058 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1059 sym_hashes = elf_sym_hashes (abfd);
1060
1061 dynobj = elf_hash_table (info)->dynobj;
1062 local_got_offsets = elf_local_got_offsets (abfd);
1063 rel_end = relocs + sec->reloc_count;
1064 sym_diff_reloc_seen = FALSE;
1065
1066 for (rel = relocs; rel < rel_end; rel++)
1067 {
1068 struct elf_link_hash_entry *h;
1069 unsigned long r_symndx;
1070 unsigned int r_type;
1071 int tls_type = GOT_NORMAL;
1072
1073 r_symndx = ELF32_R_SYM (rel->r_info);
1074 if (r_symndx < symtab_hdr->sh_info)
1075 h = NULL;
1076 else
1077 {
1078 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1079 while (h->root.type == bfd_link_hash_indirect
1080 || h->root.type == bfd_link_hash_warning)
1081 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1082 }
1083
1084 r_type = ELF32_R_TYPE (rel->r_info);
1085 r_type = elf_mn10300_tls_transition (info, r_type, h, sec, TRUE);
1086
1087 /* Some relocs require a global offset table. */
1088 if (dynobj == NULL)
1089 {
1090 switch (r_type)
1091 {
1092 case R_MN10300_GOT32:
1093 case R_MN10300_GOT24:
1094 case R_MN10300_GOT16:
1095 case R_MN10300_GOTOFF32:
1096 case R_MN10300_GOTOFF24:
1097 case R_MN10300_GOTOFF16:
1098 case R_MN10300_GOTPC32:
1099 case R_MN10300_GOTPC16:
1100 case R_MN10300_TLS_GD:
1101 case R_MN10300_TLS_LD:
1102 case R_MN10300_TLS_GOTIE:
1103 case R_MN10300_TLS_IE:
1104 elf_hash_table (info)->dynobj = dynobj = abfd;
1105 if (! _bfd_mn10300_elf_create_got_section (dynobj, info))
1106 goto fail;
1107 break;
1108
1109 default:
1110 break;
1111 }
1112 }
1113
1114 switch (r_type)
1115 {
1116 /* This relocation describes the C++ object vtable hierarchy.
1117 Reconstruct it for later use during GC. */
1118 case R_MN10300_GNU_VTINHERIT:
1119 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1120 goto fail;
1121 break;
1122
1123 /* This relocation describes which C++ vtable entries are actually
1124 used. Record for later use during GC. */
1125 case R_MN10300_GNU_VTENTRY:
1126 BFD_ASSERT (h != NULL);
1127 if (h != NULL
1128 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1129 goto fail;
1130 break;
1131
1132 case R_MN10300_TLS_LD:
1133 htab->tls_ldm_got.refcount ++;
1134 tls_type = GOT_TLS_LD;
1135
1136 if (htab->tls_ldm_got.got_allocated)
1137 break;
1138 goto create_got;
1139
1140 case R_MN10300_TLS_IE:
1141 case R_MN10300_TLS_GOTIE:
1142 if (info->shared)
1143 info->flags |= DF_STATIC_TLS;
1144 /* Fall through */
1145
1146 case R_MN10300_TLS_GD:
1147 case R_MN10300_GOT32:
1148 case R_MN10300_GOT24:
1149 case R_MN10300_GOT16:
1150 create_got:
1151 /* This symbol requires a global offset table entry. */
1152
1153 switch (r_type)
1154 {
1155 case R_MN10300_TLS_IE:
1156 case R_MN10300_TLS_GOTIE: tls_type = GOT_TLS_IE; break;
1157 case R_MN10300_TLS_GD: tls_type = GOT_TLS_GD; break;
1158 default: tls_type = GOT_NORMAL; break;
1159 }
1160
1161 if (sgot == NULL)
1162 {
1163 sgot = bfd_get_section_by_name (dynobj, ".got");
1164 BFD_ASSERT (sgot != NULL);
1165 }
1166
1167 if (srelgot == NULL
1168 && (h != NULL || info->shared))
1169 {
1170 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1171 if (srelgot == NULL)
1172 {
1173 srelgot = bfd_make_section_with_flags (dynobj,
1174 ".rela.got",
1175 (SEC_ALLOC
1176 | SEC_LOAD
1177 | SEC_HAS_CONTENTS
1178 | SEC_IN_MEMORY
1179 | SEC_LINKER_CREATED
1180 | SEC_READONLY));
1181 if (srelgot == NULL
1182 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
1183 goto fail;
1184 }
1185 }
1186
1187 if (r_type == R_MN10300_TLS_LD)
1188 {
1189 htab->tls_ldm_got.offset = sgot->size;
1190 htab->tls_ldm_got.got_allocated ++;
1191 }
1192 else if (h != NULL)
1193 {
1194 if (elf_mn10300_hash_entry (h)->tls_type != tls_type
1195 && elf_mn10300_hash_entry (h)->tls_type != GOT_UNKNOWN)
1196 {
1197 if (tls_type == GOT_TLS_IE
1198 && elf_mn10300_hash_entry (h)->tls_type == GOT_TLS_GD)
1199 /* No change - this is ok. */;
1200 else if (tls_type == GOT_TLS_GD
1201 && elf_mn10300_hash_entry (h)->tls_type == GOT_TLS_IE)
1202 /* Transition GD->IE. */
1203 tls_type = GOT_TLS_IE;
1204 else
1205 (*_bfd_error_handler)
1206 (_("%B: %s' accessed both as normal and thread local symbol"),
1207 abfd, h ? h->root.root.string : "<local>");
1208 }
1209
1210 elf_mn10300_hash_entry (h)->tls_type = tls_type;
1211
1212 if (h->got.offset != (bfd_vma) -1)
1213 /* We have already allocated space in the .got. */
1214 break;
1215
1216 h->got.offset = sgot->size;
1217
1218 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1219 /* Make sure this symbol is output as a dynamic symbol. */
1220 && h->dynindx == -1)
1221 {
1222 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1223 goto fail;
1224 }
1225
1226 srelgot->size += sizeof (Elf32_External_Rela);
1227 if (r_type == R_MN10300_TLS_GD)
1228 srelgot->size += sizeof (Elf32_External_Rela);
1229 }
1230 else
1231 {
1232 /* This is a global offset table entry for a local
1233 symbol. */
1234 if (local_got_offsets == NULL)
1235 {
1236 size_t size;
1237 unsigned int i;
1238
1239 size = symtab_hdr->sh_info * (sizeof (bfd_vma) + sizeof (char));
1240 local_got_offsets = bfd_alloc (abfd, size);
1241
1242 if (local_got_offsets == NULL)
1243 goto fail;
1244
1245 elf_local_got_offsets (abfd) = local_got_offsets;
1246 elf_mn10300_local_got_tls_type (abfd)
1247 = (char *) (local_got_offsets + symtab_hdr->sh_info);
1248
1249 for (i = 0; i < symtab_hdr->sh_info; i++)
1250 local_got_offsets[i] = (bfd_vma) -1;
1251 }
1252
1253 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
1254 /* We have already allocated space in the .got. */
1255 break;
1256
1257 local_got_offsets[r_symndx] = sgot->size;
1258
1259 if (info->shared)
1260 {
1261 /* If we are generating a shared object, we need to
1262 output a R_MN10300_RELATIVE reloc so that the dynamic
1263 linker can adjust this GOT entry. */
1264 srelgot->size += sizeof (Elf32_External_Rela);
1265
1266 if (r_type == R_MN10300_TLS_GD)
1267 /* And a R_MN10300_TLS_DTPOFF reloc as well. */
1268 srelgot->size += sizeof (Elf32_External_Rela);
1269 }
1270
1271 elf_mn10300_local_got_tls_type (abfd) [r_symndx] = tls_type;
1272 }
1273
1274 sgot->size += 4;
1275 if (r_type == R_MN10300_TLS_GD
1276 || r_type == R_MN10300_TLS_LD)
1277 sgot->size += 4;
1278
1279 goto need_shared_relocs;
1280
1281 case R_MN10300_PLT32:
1282 case R_MN10300_PLT16:
1283 /* This symbol requires a procedure linkage table entry. We
1284 actually build the entry in adjust_dynamic_symbol,
1285 because this might be a case of linking PIC code which is
1286 never referenced by a dynamic object, in which case we
1287 don't need to generate a procedure linkage table entry
1288 after all. */
1289
1290 /* If this is a local symbol, we resolve it directly without
1291 creating a procedure linkage table entry. */
1292 if (h == NULL)
1293 continue;
1294
1295 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
1296 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
1297 break;
1298
1299 h->needs_plt = 1;
1300 break;
1301
1302 case R_MN10300_24:
1303 case R_MN10300_16:
1304 case R_MN10300_8:
1305 case R_MN10300_PCREL32:
1306 case R_MN10300_PCREL16:
1307 case R_MN10300_PCREL8:
1308 if (h != NULL)
1309 h->non_got_ref = 1;
1310 break;
1311
1312 case R_MN10300_SYM_DIFF:
1313 sym_diff_reloc_seen = TRUE;
1314 break;
1315
1316 case R_MN10300_32:
1317 if (h != NULL)
1318 h->non_got_ref = 1;
1319
1320 need_shared_relocs:
1321 /* If we are creating a shared library, then we
1322 need to copy the reloc into the shared library. */
1323 if (info->shared
1324 && (sec->flags & SEC_ALLOC) != 0
1325 /* Do not generate a dynamic reloc for a
1326 reloc associated with a SYM_DIFF operation. */
1327 && ! sym_diff_reloc_seen)
1328 {
1329 asection * sym_section = NULL;
1330
1331 /* Find the section containing the
1332 symbol involved in the relocation. */
1333 if (h == NULL)
1334 {
1335 Elf_Internal_Sym * isym;
1336
1337 if (isymbuf == NULL)
1338 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1339 symtab_hdr->sh_info, 0,
1340 NULL, NULL, NULL);
1341 if (isymbuf)
1342 {
1343 isym = isymbuf + r_symndx;
1344 /* All we care about is whether this local symbol is absolute. */
1345 if (isym->st_shndx == SHN_ABS)
1346 sym_section = bfd_abs_section_ptr;
1347 }
1348 }
1349 else
1350 {
1351 if (h->root.type == bfd_link_hash_defined
1352 || h->root.type == bfd_link_hash_defweak)
1353 sym_section = h->root.u.def.section;
1354 }
1355
1356 /* If the symbol is absolute then the relocation can
1357 be resolved during linking and there is no need for
1358 a dynamic reloc. */
1359 if (sym_section != bfd_abs_section_ptr)
1360 {
1361 /* When creating a shared object, we must copy these
1362 reloc types into the output file. We create a reloc
1363 section in dynobj and make room for this reloc. */
1364 if (sreloc == NULL)
1365 {
1366 sreloc = _bfd_elf_make_dynamic_reloc_section
1367 (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
1368 if (sreloc == NULL)
1369 goto fail;
1370 }
1371
1372 sreloc->size += sizeof (Elf32_External_Rela);
1373 }
1374 }
1375
1376 break;
1377 }
1378
1379 if (ELF32_R_TYPE (rel->r_info) != R_MN10300_SYM_DIFF)
1380 sym_diff_reloc_seen = FALSE;
1381 }
1382
1383 result = TRUE;
1384 fail:
1385 if (isymbuf != NULL)
1386 free (isymbuf);
1387
1388 return result;
1389 }
1390
1391 /* Return the section that should be marked against GC for a given
1392 relocation. */
1393
1394 static asection *
1395 mn10300_elf_gc_mark_hook (asection *sec,
1396 struct bfd_link_info *info,
1397 Elf_Internal_Rela *rel,
1398 struct elf_link_hash_entry *h,
1399 Elf_Internal_Sym *sym)
1400 {
1401 if (h != NULL)
1402 switch (ELF32_R_TYPE (rel->r_info))
1403 {
1404 case R_MN10300_GNU_VTINHERIT:
1405 case R_MN10300_GNU_VTENTRY:
1406 return NULL;
1407 }
1408
1409 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1410 }
1411
1412 /* Perform a relocation as part of a final link. */
1413
1414 static bfd_reloc_status_type
1415 mn10300_elf_final_link_relocate (reloc_howto_type *howto,
1416 bfd *input_bfd,
1417 bfd *output_bfd ATTRIBUTE_UNUSED,
1418 asection *input_section,
1419 bfd_byte *contents,
1420 bfd_vma offset,
1421 bfd_vma value,
1422 bfd_vma addend,
1423 struct elf_link_hash_entry * h,
1424 unsigned long symndx,
1425 struct bfd_link_info *info,
1426 asection *sym_sec ATTRIBUTE_UNUSED,
1427 int is_local ATTRIBUTE_UNUSED)
1428 {
1429 struct elf32_mn10300_link_hash_table * htab = elf32_mn10300_hash_table (info);
1430 static asection * sym_diff_section;
1431 static bfd_vma sym_diff_value;
1432 bfd_boolean is_sym_diff_reloc;
1433 unsigned long r_type = howto->type;
1434 bfd_byte * hit_data = contents + offset;
1435 bfd * dynobj;
1436 asection * sgot;
1437 asection * splt;
1438 asection * sreloc;
1439
1440 dynobj = elf_hash_table (info)->dynobj;
1441 sgot = NULL;
1442 splt = NULL;
1443 sreloc = NULL;
1444
1445 switch (r_type)
1446 {
1447 case R_MN10300_24:
1448 case R_MN10300_16:
1449 case R_MN10300_8:
1450 case R_MN10300_PCREL8:
1451 case R_MN10300_PCREL16:
1452 case R_MN10300_PCREL32:
1453 case R_MN10300_GOTOFF32:
1454 case R_MN10300_GOTOFF24:
1455 case R_MN10300_GOTOFF16:
1456 if (info->shared
1457 && (input_section->flags & SEC_ALLOC) != 0
1458 && h != NULL
1459 && ! SYMBOL_REFERENCES_LOCAL (info, h))
1460 return bfd_reloc_dangerous;
1461 case R_MN10300_GOT32:
1462 /* Issue 2052223:
1463 Taking the address of a protected function in a shared library
1464 is illegal. Issue an error message here. */
1465 if (info->shared
1466 && (input_section->flags & SEC_ALLOC) != 0
1467 && h != NULL
1468 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED
1469 && (h->type == STT_FUNC || h->type == STT_GNU_IFUNC)
1470 && ! SYMBOL_REFERENCES_LOCAL (info, h))
1471 return bfd_reloc_dangerous;
1472 }
1473
1474 is_sym_diff_reloc = FALSE;
1475 if (sym_diff_section != NULL)
1476 {
1477 BFD_ASSERT (sym_diff_section == input_section);
1478
1479 switch (r_type)
1480 {
1481 case R_MN10300_32:
1482 case R_MN10300_24:
1483 case R_MN10300_16:
1484 case R_MN10300_8:
1485 value -= sym_diff_value;
1486 /* If we are computing a 32-bit value for the location lists
1487 and the result is 0 then we add one to the value. A zero
1488 value can result because of linker relaxation deleteing
1489 prologue instructions and using a value of 1 (for the begin
1490 and end offsets in the location list entry) results in a
1491 nul entry which does not prevent the following entries from
1492 being parsed. */
1493 if (r_type == R_MN10300_32
1494 && value == 0
1495 && strcmp (input_section->name, ".debug_loc") == 0)
1496 value = 1;
1497 sym_diff_section = NULL;
1498 is_sym_diff_reloc = TRUE;
1499 break;
1500
1501 default:
1502 sym_diff_section = NULL;
1503 break;
1504 }
1505 }
1506
1507 switch (r_type)
1508 {
1509 case R_MN10300_SYM_DIFF:
1510 BFD_ASSERT (addend == 0);
1511 /* Cache the input section and value.
1512 The offset is unreliable, since relaxation may
1513 have reduced the following reloc's offset. */
1514 sym_diff_section = input_section;
1515 sym_diff_value = value;
1516 return bfd_reloc_ok;
1517
1518 case R_MN10300_ALIGN:
1519 case R_MN10300_NONE:
1520 return bfd_reloc_ok;
1521
1522 case R_MN10300_32:
1523 if (info->shared
1524 /* Do not generate relocs when an R_MN10300_32 has been used
1525 with an R_MN10300_SYM_DIFF to compute a difference of two
1526 symbols. */
1527 && is_sym_diff_reloc == FALSE
1528 /* Also, do not generate a reloc when the symbol associated
1529 with the R_MN10300_32 reloc is absolute - there is no
1530 need for a run time computation in this case. */
1531 && sym_sec != bfd_abs_section_ptr
1532 /* If the section is not going to be allocated at load time
1533 then there is no need to generate relocs for it. */
1534 && (input_section->flags & SEC_ALLOC) != 0)
1535 {
1536 Elf_Internal_Rela outrel;
1537 bfd_boolean skip, relocate;
1538
1539 /* When generating a shared object, these relocations are
1540 copied into the output file to be resolved at run
1541 time. */
1542 if (sreloc == NULL)
1543 {
1544 sreloc = _bfd_elf_get_dynamic_reloc_section
1545 (input_bfd, input_section, /*rela?*/ TRUE);
1546 if (sreloc == NULL)
1547 return FALSE;
1548 }
1549
1550 skip = FALSE;
1551
1552 outrel.r_offset = _bfd_elf_section_offset (input_bfd, info,
1553 input_section, offset);
1554 if (outrel.r_offset == (bfd_vma) -1)
1555 skip = TRUE;
1556
1557 outrel.r_offset += (input_section->output_section->vma
1558 + input_section->output_offset);
1559
1560 if (skip)
1561 {
1562 memset (&outrel, 0, sizeof outrel);
1563 relocate = FALSE;
1564 }
1565 else
1566 {
1567 /* h->dynindx may be -1 if this symbol was marked to
1568 become local. */
1569 if (h == NULL
1570 || SYMBOL_REFERENCES_LOCAL (info, h))
1571 {
1572 relocate = TRUE;
1573 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1574 outrel.r_addend = value + addend;
1575 }
1576 else
1577 {
1578 BFD_ASSERT (h->dynindx != -1);
1579 relocate = FALSE;
1580 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_32);
1581 outrel.r_addend = value + addend;
1582 }
1583 }
1584
1585 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1586 (bfd_byte *) (((Elf32_External_Rela *) sreloc->contents)
1587 + sreloc->reloc_count));
1588 ++sreloc->reloc_count;
1589
1590 /* If this reloc is against an external symbol, we do
1591 not want to fiddle with the addend. Otherwise, we
1592 need to include the symbol value so that it becomes
1593 an addend for the dynamic reloc. */
1594 if (! relocate)
1595 return bfd_reloc_ok;
1596 }
1597 value += addend;
1598 bfd_put_32 (input_bfd, value, hit_data);
1599 return bfd_reloc_ok;
1600
1601 case R_MN10300_24:
1602 value += addend;
1603
1604 if ((long) value > 0x7fffff || (long) value < -0x800000)
1605 return bfd_reloc_overflow;
1606
1607 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1608 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1609 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1610 return bfd_reloc_ok;
1611
1612 case R_MN10300_16:
1613 value += addend;
1614
1615 if ((long) value > 0x7fff || (long) value < -0x8000)
1616 return bfd_reloc_overflow;
1617
1618 bfd_put_16 (input_bfd, value, hit_data);
1619 return bfd_reloc_ok;
1620
1621 case R_MN10300_8:
1622 value += addend;
1623
1624 if ((long) value > 0x7f || (long) value < -0x80)
1625 return bfd_reloc_overflow;
1626
1627 bfd_put_8 (input_bfd, value, hit_data);
1628 return bfd_reloc_ok;
1629
1630 case R_MN10300_PCREL8:
1631 value -= (input_section->output_section->vma
1632 + input_section->output_offset);
1633 value -= offset;
1634 value += addend;
1635
1636 if ((long) value > 0x7f || (long) value < -0x80)
1637 return bfd_reloc_overflow;
1638
1639 bfd_put_8 (input_bfd, value, hit_data);
1640 return bfd_reloc_ok;
1641
1642 case R_MN10300_PCREL16:
1643 value -= (input_section->output_section->vma
1644 + input_section->output_offset);
1645 value -= offset;
1646 value += addend;
1647
1648 if ((long) value > 0x7fff || (long) value < -0x8000)
1649 return bfd_reloc_overflow;
1650
1651 bfd_put_16 (input_bfd, value, hit_data);
1652 return bfd_reloc_ok;
1653
1654 case R_MN10300_PCREL32:
1655 value -= (input_section->output_section->vma
1656 + input_section->output_offset);
1657 value -= offset;
1658 value += addend;
1659
1660 bfd_put_32 (input_bfd, value, hit_data);
1661 return bfd_reloc_ok;
1662
1663 case R_MN10300_GNU_VTINHERIT:
1664 case R_MN10300_GNU_VTENTRY:
1665 return bfd_reloc_ok;
1666
1667 case R_MN10300_GOTPC32:
1668 if (dynobj == NULL)
1669 return bfd_reloc_dangerous;
1670
1671 /* Use global offset table as symbol value. */
1672 value = bfd_get_section_by_name (dynobj,
1673 ".got")->output_section->vma;
1674 value -= (input_section->output_section->vma
1675 + input_section->output_offset);
1676 value -= offset;
1677 value += addend;
1678
1679 bfd_put_32 (input_bfd, value, hit_data);
1680 return bfd_reloc_ok;
1681
1682 case R_MN10300_GOTPC16:
1683 if (dynobj == NULL)
1684 return bfd_reloc_dangerous;
1685
1686 /* Use global offset table as symbol value. */
1687 value = bfd_get_section_by_name (dynobj,
1688 ".got")->output_section->vma;
1689 value -= (input_section->output_section->vma
1690 + input_section->output_offset);
1691 value -= offset;
1692 value += addend;
1693
1694 if ((long) value > 0x7fff || (long) value < -0x8000)
1695 return bfd_reloc_overflow;
1696
1697 bfd_put_16 (input_bfd, value, hit_data);
1698 return bfd_reloc_ok;
1699
1700 case R_MN10300_GOTOFF32:
1701 if (dynobj == NULL)
1702 return bfd_reloc_dangerous;
1703
1704 value -= bfd_get_section_by_name (dynobj,
1705 ".got")->output_section->vma;
1706 value += addend;
1707
1708 bfd_put_32 (input_bfd, value, hit_data);
1709 return bfd_reloc_ok;
1710
1711 case R_MN10300_GOTOFF24:
1712 if (dynobj == NULL)
1713 return bfd_reloc_dangerous;
1714
1715 value -= bfd_get_section_by_name (dynobj,
1716 ".got")->output_section->vma;
1717 value += addend;
1718
1719 if ((long) value > 0x7fffff || (long) value < -0x800000)
1720 return bfd_reloc_overflow;
1721
1722 bfd_put_8 (input_bfd, value, hit_data);
1723 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1724 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1725 return bfd_reloc_ok;
1726
1727 case R_MN10300_GOTOFF16:
1728 if (dynobj == NULL)
1729 return bfd_reloc_dangerous;
1730
1731 value -= bfd_get_section_by_name (dynobj,
1732 ".got")->output_section->vma;
1733 value += addend;
1734
1735 if ((long) value > 0x7fff || (long) value < -0x8000)
1736 return bfd_reloc_overflow;
1737
1738 bfd_put_16 (input_bfd, value, hit_data);
1739 return bfd_reloc_ok;
1740
1741 case R_MN10300_PLT32:
1742 if (h != NULL
1743 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1744 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1745 && h->plt.offset != (bfd_vma) -1)
1746 {
1747 if (dynobj == NULL)
1748 return bfd_reloc_dangerous;
1749
1750 splt = bfd_get_section_by_name (dynobj, ".plt");
1751
1752 value = (splt->output_section->vma
1753 + splt->output_offset
1754 + h->plt.offset) - value;
1755 }
1756
1757 value -= (input_section->output_section->vma
1758 + input_section->output_offset);
1759 value -= offset;
1760 value += addend;
1761
1762 bfd_put_32 (input_bfd, value, hit_data);
1763 return bfd_reloc_ok;
1764
1765 case R_MN10300_PLT16:
1766 if (h != NULL
1767 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1768 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1769 && h->plt.offset != (bfd_vma) -1)
1770 {
1771 if (dynobj == NULL)
1772 return bfd_reloc_dangerous;
1773
1774 splt = bfd_get_section_by_name (dynobj, ".plt");
1775
1776 value = (splt->output_section->vma
1777 + splt->output_offset
1778 + h->plt.offset) - value;
1779 }
1780
1781 value -= (input_section->output_section->vma
1782 + input_section->output_offset);
1783 value -= offset;
1784 value += addend;
1785
1786 if ((long) value > 0x7fff || (long) value < -0x8000)
1787 return bfd_reloc_overflow;
1788
1789 bfd_put_16 (input_bfd, value, hit_data);
1790 return bfd_reloc_ok;
1791
1792 case R_MN10300_TLS_LDO:
1793 value = dtpoff (info, value);
1794 bfd_put_32 (input_bfd, value + addend, hit_data);
1795 return bfd_reloc_ok;
1796
1797 case R_MN10300_TLS_LE:
1798 value = tpoff (info, value);
1799 bfd_put_32 (input_bfd, value + addend, hit_data);
1800 return bfd_reloc_ok;
1801
1802 case R_MN10300_TLS_LD:
1803 if (dynobj == NULL)
1804 return bfd_reloc_dangerous;
1805
1806 sgot = bfd_get_section_by_name (dynobj, ".got");
1807
1808 BFD_ASSERT (sgot != NULL);
1809 value = htab->tls_ldm_got.offset + sgot->output_offset;
1810 bfd_put_32 (input_bfd, value, hit_data);
1811
1812 if (!htab->tls_ldm_got.rel_emitted)
1813 {
1814 asection * srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1815 Elf_Internal_Rela rel;
1816
1817 BFD_ASSERT (srelgot != NULL);
1818 htab->tls_ldm_got.rel_emitted ++;
1819 rel.r_offset = (sgot->output_section->vma
1820 + sgot->output_offset
1821 + htab->tls_ldm_got.offset);
1822 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + htab->tls_ldm_got.offset);
1823 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + htab->tls_ldm_got.offset+4);
1824 rel.r_info = ELF32_R_INFO (0, R_MN10300_TLS_DTPMOD);
1825 rel.r_addend = 0;
1826 bfd_elf32_swap_reloca_out (output_bfd, & rel,
1827 (bfd_byte *) ((Elf32_External_Rela *) srelgot->contents
1828 + srelgot->reloc_count));
1829 ++ srelgot->reloc_count;
1830 }
1831
1832 return bfd_reloc_ok;
1833
1834 case R_MN10300_TLS_GOTIE:
1835 value = tpoff (info, value);
1836 /* Fall Through. */
1837
1838 case R_MN10300_TLS_GD:
1839 case R_MN10300_TLS_IE:
1840 case R_MN10300_GOT32:
1841 case R_MN10300_GOT24:
1842 case R_MN10300_GOT16:
1843 if (dynobj == NULL)
1844 return bfd_reloc_dangerous;
1845
1846 sgot = bfd_get_section_by_name (dynobj, ".got");
1847
1848 if (r_type == R_MN10300_TLS_GD)
1849 value = dtpoff (info, value);
1850
1851 if (h != NULL)
1852 {
1853 bfd_vma off;
1854
1855 off = h->got.offset;
1856 /* Offsets in the GOT are allocated in check_relocs
1857 which is not called for shared libraries... */
1858 if (off == (bfd_vma) -1)
1859 off = 0;
1860
1861 if (sgot->contents != NULL
1862 && (! elf_hash_table (info)->dynamic_sections_created
1863 || SYMBOL_REFERENCES_LOCAL (info, h)))
1864 /* This is actually a static link, or it is a
1865 -Bsymbolic link and the symbol is defined
1866 locally, or the symbol was forced to be local
1867 because of a version file. We must initialize
1868 this entry in the global offset table.
1869
1870 When doing a dynamic link, we create a .rela.got
1871 relocation entry to initialize the value. This
1872 is done in the finish_dynamic_symbol routine. */
1873 bfd_put_32 (output_bfd, value,
1874 sgot->contents + off);
1875
1876 value = sgot->output_offset + off;
1877 }
1878 else
1879 {
1880 bfd_vma off;
1881
1882 off = elf_local_got_offsets (input_bfd)[symndx];
1883
1884 if (off & 1)
1885 bfd_put_32 (output_bfd, value, sgot->contents + (off & ~ 1));
1886 else
1887 {
1888 bfd_put_32 (output_bfd, value, sgot->contents + off);
1889
1890 if (info->shared)
1891 {
1892 asection * srelgot;
1893 Elf_Internal_Rela outrel;
1894
1895 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1896 BFD_ASSERT (srelgot != NULL);
1897
1898 outrel.r_offset = (sgot->output_section->vma
1899 + sgot->output_offset
1900 + off);
1901 switch (r_type)
1902 {
1903 case R_MN10300_TLS_GD:
1904 outrel.r_info = ELF32_R_INFO (0, R_MN10300_TLS_DTPOFF);
1905 outrel.r_offset = (sgot->output_section->vma
1906 + sgot->output_offset
1907 + off + 4);
1908 bfd_elf32_swap_reloca_out (output_bfd, & outrel,
1909 (bfd_byte *) (((Elf32_External_Rela *)
1910 srelgot->contents)
1911 + srelgot->reloc_count));
1912 ++ srelgot->reloc_count;
1913 outrel.r_info = ELF32_R_INFO (0, R_MN10300_TLS_DTPMOD);
1914 break;
1915 case R_MN10300_TLS_GOTIE:
1916 case R_MN10300_TLS_IE:
1917 outrel.r_info = ELF32_R_INFO (0, R_MN10300_TLS_TPOFF);
1918 break;
1919 default:
1920 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1921 break;
1922 }
1923
1924 outrel.r_addend = value;
1925 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1926 (bfd_byte *) (((Elf32_External_Rela *)
1927 srelgot->contents)
1928 + srelgot->reloc_count));
1929 ++ srelgot->reloc_count;
1930 elf_local_got_offsets (input_bfd)[symndx] |= 1;
1931 }
1932
1933 value = sgot->output_offset + (off & ~(bfd_vma) 1);
1934 }
1935 }
1936
1937 value += addend;
1938
1939 if (r_type == R_MN10300_TLS_IE)
1940 {
1941 value += sgot->output_section->vma;
1942 bfd_put_32 (input_bfd, value, hit_data);
1943 return bfd_reloc_ok;
1944 }
1945 else if (r_type == R_MN10300_TLS_GOTIE
1946 || r_type == R_MN10300_TLS_GD
1947 || r_type == R_MN10300_TLS_LD)
1948 {
1949 bfd_put_32 (input_bfd, value, hit_data);
1950 return bfd_reloc_ok;
1951 }
1952 else if (r_type == R_MN10300_GOT32)
1953 {
1954 bfd_put_32 (input_bfd, value, hit_data);
1955 return bfd_reloc_ok;
1956 }
1957 else if (r_type == R_MN10300_GOT24)
1958 {
1959 if ((long) value > 0x7fffff || (long) value < -0x800000)
1960 return bfd_reloc_overflow;
1961
1962 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1963 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1964 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1965 return bfd_reloc_ok;
1966 }
1967 else if (r_type == R_MN10300_GOT16)
1968 {
1969 if ((long) value > 0x7fff || (long) value < -0x8000)
1970 return bfd_reloc_overflow;
1971
1972 bfd_put_16 (input_bfd, value, hit_data);
1973 return bfd_reloc_ok;
1974 }
1975 /* Fall through. */
1976
1977 default:
1978 return bfd_reloc_notsupported;
1979 }
1980 }
1981 \f
1982 /* Relocate an MN10300 ELF section. */
1983
1984 static bfd_boolean
1985 mn10300_elf_relocate_section (bfd *output_bfd,
1986 struct bfd_link_info *info,
1987 bfd *input_bfd,
1988 asection *input_section,
1989 bfd_byte *contents,
1990 Elf_Internal_Rela *relocs,
1991 Elf_Internal_Sym *local_syms,
1992 asection **local_sections)
1993 {
1994 Elf_Internal_Shdr *symtab_hdr;
1995 struct elf_link_hash_entry **sym_hashes;
1996 Elf_Internal_Rela *rel, *relend;
1997 Elf_Internal_Rela * trel;
1998
1999 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2000 sym_hashes = elf_sym_hashes (input_bfd);
2001
2002 rel = relocs;
2003 relend = relocs + input_section->reloc_count;
2004 for (; rel < relend; rel++)
2005 {
2006 int r_type;
2007 reloc_howto_type *howto;
2008 unsigned long r_symndx;
2009 Elf_Internal_Sym *sym;
2010 asection *sec;
2011 struct elf32_mn10300_link_hash_entry *h;
2012 bfd_vma relocation;
2013 bfd_reloc_status_type r;
2014 int tls_r_type;
2015 bfd_boolean unresolved_reloc = FALSE;
2016 bfd_boolean warned;
2017 struct elf_link_hash_entry * hh;
2018
2019 relocation = 0;
2020 r_symndx = ELF32_R_SYM (rel->r_info);
2021 r_type = ELF32_R_TYPE (rel->r_info);
2022 howto = elf_mn10300_howto_table + r_type;
2023
2024 /* Just skip the vtable gc relocs. */
2025 if (r_type == R_MN10300_GNU_VTINHERIT
2026 || r_type == R_MN10300_GNU_VTENTRY)
2027 continue;
2028
2029 h = NULL;
2030 sym = NULL;
2031 sec = NULL;
2032 if (r_symndx < symtab_hdr->sh_info)
2033 hh = NULL;
2034 else
2035 {
2036 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2037 r_symndx, symtab_hdr, sym_hashes,
2038 hh, sec, relocation,
2039 unresolved_reloc, warned);
2040 }
2041 h = elf_mn10300_hash_entry (hh);
2042
2043 tls_r_type = elf_mn10300_tls_transition (info, r_type, hh, input_section, 0);
2044 if (tls_r_type != r_type)
2045 {
2046 bfd_boolean had_plt;
2047
2048 had_plt = mn10300_do_tls_transition (input_bfd, r_type, tls_r_type,
2049 contents, rel->r_offset);
2050 r_type = tls_r_type;
2051 howto = elf_mn10300_howto_table + r_type;
2052
2053 if (had_plt)
2054 for (trel = rel+1; trel < relend; trel++)
2055 if ((ELF32_R_TYPE (trel->r_info) == R_MN10300_PLT32
2056 || ELF32_R_TYPE (trel->r_info) == R_MN10300_PCREL32)
2057 && rel->r_offset + had_plt == trel->r_offset)
2058 trel->r_info = ELF32_R_INFO (0, R_MN10300_NONE);
2059 }
2060
2061 if (r_symndx < symtab_hdr->sh_info)
2062 {
2063 sym = local_syms + r_symndx;
2064 sec = local_sections[r_symndx];
2065 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2066 }
2067 else
2068 {
2069 if ((h->root.root.type == bfd_link_hash_defined
2070 || h->root.root.type == bfd_link_hash_defweak)
2071 && ( r_type == R_MN10300_GOTPC32
2072 || r_type == R_MN10300_GOTPC16
2073 || (( r_type == R_MN10300_PLT32
2074 || r_type == R_MN10300_PLT16)
2075 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
2076 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
2077 && h->root.plt.offset != (bfd_vma) -1)
2078 || (( r_type == R_MN10300_GOT32
2079 || r_type == R_MN10300_GOT24
2080 || r_type == R_MN10300_TLS_GD
2081 || r_type == R_MN10300_TLS_LD
2082 || r_type == R_MN10300_TLS_GOTIE
2083 || r_type == R_MN10300_TLS_IE
2084 || r_type == R_MN10300_GOT16)
2085 && elf_hash_table (info)->dynamic_sections_created
2086 && !SYMBOL_REFERENCES_LOCAL (info, hh))
2087 || (r_type == R_MN10300_32
2088 /* _32 relocs in executables force _COPY relocs,
2089 such that the address of the symbol ends up
2090 being local. */
2091 && !info->executable
2092 && !SYMBOL_REFERENCES_LOCAL (info, hh)
2093 && ((input_section->flags & SEC_ALLOC) != 0
2094 /* DWARF will emit R_MN10300_32 relocations
2095 in its sections against symbols defined
2096 externally in shared libraries. We can't
2097 do anything with them here. */
2098 || ((input_section->flags & SEC_DEBUGGING) != 0
2099 && h->root.def_dynamic)))))
2100 /* In these cases, we don't need the relocation
2101 value. We check specially because in some
2102 obscure cases sec->output_section will be NULL. */
2103 relocation = 0;
2104
2105 else if (!info->relocatable && unresolved_reloc
2106 && _bfd_elf_section_offset (output_bfd, info, input_section,
2107 rel->r_offset) != (bfd_vma) -1)
2108
2109 (*_bfd_error_handler)
2110 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
2111 input_bfd,
2112 input_section,
2113 (long) rel->r_offset,
2114 howto->name,
2115 h->root.root.root.string);
2116 }
2117
2118 if (sec != NULL && discarded_section (sec))
2119 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2120 rel, relend, howto, contents);
2121
2122 if (info->relocatable)
2123 continue;
2124
2125 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
2126 input_section,
2127 contents, rel->r_offset,
2128 relocation, rel->r_addend,
2129 (struct elf_link_hash_entry *) h,
2130 r_symndx,
2131 info, sec, h == NULL);
2132
2133 if (r != bfd_reloc_ok)
2134 {
2135 const char *name;
2136 const char *msg = NULL;
2137
2138 if (h != NULL)
2139 name = h->root.root.root.string;
2140 else
2141 {
2142 name = (bfd_elf_string_from_elf_section
2143 (input_bfd, symtab_hdr->sh_link, sym->st_name));
2144 if (name == NULL || *name == '\0')
2145 name = bfd_section_name (input_bfd, sec);
2146 }
2147
2148 switch (r)
2149 {
2150 case bfd_reloc_overflow:
2151 if (! ((*info->callbacks->reloc_overflow)
2152 (info, (h ? &h->root.root : NULL), name,
2153 howto->name, (bfd_vma) 0, input_bfd,
2154 input_section, rel->r_offset)))
2155 return FALSE;
2156 break;
2157
2158 case bfd_reloc_undefined:
2159 if (! ((*info->callbacks->undefined_symbol)
2160 (info, name, input_bfd, input_section,
2161 rel->r_offset, TRUE)))
2162 return FALSE;
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 (link_info->relocatable)
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 (link_info->relocatable
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 = bfd_get_section_by_name (elf_hash_table (link_info)
3490 ->dynobj, ".plt");
3491
3492 value = ((splt->output_section->vma
3493 + splt->output_offset
3494 + h->root.plt.offset)
3495 - (sec->output_section->vma
3496 + sec->output_offset
3497 + irel->r_offset));
3498 }
3499
3500 /* If we've got a "call" instruction that needs to be turned
3501 into a "calls" instruction, do so now. It saves a byte. */
3502 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
3503 {
3504 unsigned char code;
3505
3506 /* Get the opcode. */
3507 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3508
3509 /* Make sure we're working with a "call" instruction! */
3510 if (code == 0xdd)
3511 {
3512 /* Note that we've changed the relocs, section contents,
3513 etc. */
3514 elf_section_data (sec)->relocs = internal_relocs;
3515 elf_section_data (sec)->this_hdr.contents = contents;
3516 symtab_hdr->contents = (unsigned char *) isymbuf;
3517
3518 /* Fix the opcode. */
3519 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
3520 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
3521
3522 /* Fix irel->r_offset and irel->r_addend. */
3523 irel->r_offset += 1;
3524 irel->r_addend += 1;
3525
3526 /* Delete one byte of data. */
3527 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3528 irel->r_offset + 3, 1))
3529 goto error_return;
3530
3531 /* That will change things, so, we should relax again.
3532 Note that this is not required, and it may be slow. */
3533 *again = TRUE;
3534 }
3535 }
3536 else if (h)
3537 {
3538 /* We've got a "call" instruction which needs some data
3539 from target function filled in. */
3540 unsigned char code;
3541
3542 /* Get the opcode. */
3543 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3544
3545 /* Insert data from the target function into the "call"
3546 instruction if needed. */
3547 if (code == 0xdd)
3548 {
3549 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
3550 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
3551 contents + irel->r_offset + 5);
3552 }
3553 }
3554
3555 /* Deal with pc-relative gunk. */
3556 value -= (sec->output_section->vma + sec->output_offset);
3557 value -= irel->r_offset;
3558 value += irel->r_addend;
3559
3560 /* See if the value will fit in 16 bits, note the high value is
3561 0x7fff + 2 as the target will be two bytes closer if we are
3562 able to relax, if it's in the same section. */
3563 if (sec->output_section == sym_sec->output_section)
3564 jump_offset = 0x8001;
3565 else
3566 jump_offset = 0x7fff;
3567
3568 /* Account for jumps across alignment boundaries using
3569 align_gap_adjustment. */
3570 if ((bfd_signed_vma) value < jump_offset - (bfd_signed_vma) align_gap_adjustment
3571 && ((bfd_signed_vma) value > -0x8000 + (bfd_signed_vma) align_gap_adjustment))
3572 {
3573 unsigned char code;
3574
3575 /* Get the opcode. */
3576 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3577
3578 if (code != 0xdc && code != 0xdd && code != 0xff)
3579 continue;
3580
3581 /* Note that we've changed the relocs, section contents, etc. */
3582 elf_section_data (sec)->relocs = internal_relocs;
3583 elf_section_data (sec)->this_hdr.contents = contents;
3584 symtab_hdr->contents = (unsigned char *) isymbuf;
3585
3586 /* Fix the opcode. */
3587 if (code == 0xdc)
3588 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
3589 else if (code == 0xdd)
3590 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
3591 else if (code == 0xff)
3592 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3593
3594 /* Fix the relocation's type. */
3595 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3596 (ELF32_R_TYPE (irel->r_info)
3597 == (int) R_MN10300_PLT32)
3598 ? R_MN10300_PLT16 :
3599 R_MN10300_PCREL16);
3600
3601 /* Delete two bytes of data. */
3602 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3603 irel->r_offset + 1, 2))
3604 goto error_return;
3605
3606 /* That will change things, so, we should relax again.
3607 Note that this is not required, and it may be slow. */
3608 *again = TRUE;
3609 }
3610 }
3611
3612 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
3613 branch. */
3614 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
3615 {
3616 bfd_vma value = symval;
3617
3618 /* If we've got a "call" instruction that needs to be turned
3619 into a "calls" instruction, do so now. It saves a byte. */
3620 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
3621 {
3622 unsigned char code;
3623
3624 /* Get the opcode. */
3625 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3626
3627 /* Make sure we're working with a "call" instruction! */
3628 if (code == 0xcd)
3629 {
3630 /* Note that we've changed the relocs, section contents,
3631 etc. */
3632 elf_section_data (sec)->relocs = internal_relocs;
3633 elf_section_data (sec)->this_hdr.contents = contents;
3634 symtab_hdr->contents = (unsigned char *) isymbuf;
3635
3636 /* Fix the opcode. */
3637 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
3638 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
3639
3640 /* Fix irel->r_offset and irel->r_addend. */
3641 irel->r_offset += 1;
3642 irel->r_addend += 1;
3643
3644 /* Delete one byte of data. */
3645 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3646 irel->r_offset + 1, 1))
3647 goto error_return;
3648
3649 /* That will change things, so, we should relax again.
3650 Note that this is not required, and it may be slow. */
3651 *again = TRUE;
3652 }
3653 }
3654 else if (h)
3655 {
3656 unsigned char code;
3657
3658 /* Get the opcode. */
3659 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3660
3661 /* Insert data from the target function into the "call"
3662 instruction if needed. */
3663 if (code == 0xcd)
3664 {
3665 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
3666 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
3667 contents + irel->r_offset + 3);
3668 }
3669 }
3670
3671 /* Deal with pc-relative gunk. */
3672 value -= (sec->output_section->vma + sec->output_offset);
3673 value -= irel->r_offset;
3674 value += irel->r_addend;
3675
3676 /* See if the value will fit in 8 bits, note the high value is
3677 0x7f + 1 as the target will be one bytes closer if we are
3678 able to relax. */
3679 if ((long) value < 0x80 && (long) value > -0x80)
3680 {
3681 unsigned char code;
3682
3683 /* Get the opcode. */
3684 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3685
3686 if (code != 0xcc)
3687 continue;
3688
3689 /* Note that we've changed the relocs, section contents, etc. */
3690 elf_section_data (sec)->relocs = internal_relocs;
3691 elf_section_data (sec)->this_hdr.contents = contents;
3692 symtab_hdr->contents = (unsigned char *) isymbuf;
3693
3694 /* Fix the opcode. */
3695 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
3696
3697 /* Fix the relocation's type. */
3698 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3699 R_MN10300_PCREL8);
3700
3701 /* Delete one byte of data. */
3702 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3703 irel->r_offset + 1, 1))
3704 goto error_return;
3705
3706 /* That will change things, so, we should relax again.
3707 Note that this is not required, and it may be slow. */
3708 *again = TRUE;
3709 }
3710 }
3711
3712 /* Try to eliminate an unconditional 8 bit pc-relative branch
3713 which immediately follows a conditional 8 bit pc-relative
3714 branch around the unconditional branch.
3715
3716 original: new:
3717 bCC lab1 bCC' lab2
3718 bra lab2
3719 lab1: lab1:
3720
3721 This happens when the bCC can't reach lab2 at assembly time,
3722 but due to other relaxations it can reach at link time. */
3723 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
3724 {
3725 Elf_Internal_Rela *nrel;
3726 bfd_vma value = symval;
3727 unsigned char code;
3728
3729 /* Deal with pc-relative gunk. */
3730 value -= (sec->output_section->vma + sec->output_offset);
3731 value -= irel->r_offset;
3732 value += irel->r_addend;
3733
3734 /* Do nothing if this reloc is the last byte in the section. */
3735 if (irel->r_offset == sec->size)
3736 continue;
3737
3738 /* See if the next instruction is an unconditional pc-relative
3739 branch, more often than not this test will fail, so we
3740 test it first to speed things up. */
3741 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
3742 if (code != 0xca)
3743 continue;
3744
3745 /* Also make sure the next relocation applies to the next
3746 instruction and that it's a pc-relative 8 bit branch. */
3747 nrel = irel + 1;
3748 if (nrel == irelend
3749 || irel->r_offset + 2 != nrel->r_offset
3750 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
3751 continue;
3752
3753 /* Make sure our destination immediately follows the
3754 unconditional branch. */
3755 if (symval != (sec->output_section->vma + sec->output_offset
3756 + irel->r_offset + 3))
3757 continue;
3758
3759 /* Now make sure we are a conditional branch. This may not
3760 be necessary, but why take the chance.
3761
3762 Note these checks assume that R_MN10300_PCREL8 relocs
3763 only occur on bCC and bCCx insns. If they occured
3764 elsewhere, we'd need to know the start of this insn
3765 for this check to be accurate. */
3766 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3767 if (code != 0xc0 && code != 0xc1 && code != 0xc2
3768 && code != 0xc3 && code != 0xc4 && code != 0xc5
3769 && code != 0xc6 && code != 0xc7 && code != 0xc8
3770 && code != 0xc9 && code != 0xe8 && code != 0xe9
3771 && code != 0xea && code != 0xeb)
3772 continue;
3773
3774 /* We also have to be sure there is no symbol/label
3775 at the unconditional branch. */
3776 if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
3777 irel->r_offset + 1))
3778 continue;
3779
3780 /* Note that we've changed the relocs, section contents, etc. */
3781 elf_section_data (sec)->relocs = internal_relocs;
3782 elf_section_data (sec)->this_hdr.contents = contents;
3783 symtab_hdr->contents = (unsigned char *) isymbuf;
3784
3785 /* Reverse the condition of the first branch. */
3786 switch (code)
3787 {
3788 case 0xc8:
3789 code = 0xc9;
3790 break;
3791 case 0xc9:
3792 code = 0xc8;
3793 break;
3794 case 0xc0:
3795 code = 0xc2;
3796 break;
3797 case 0xc2:
3798 code = 0xc0;
3799 break;
3800 case 0xc3:
3801 code = 0xc1;
3802 break;
3803 case 0xc1:
3804 code = 0xc3;
3805 break;
3806 case 0xc4:
3807 code = 0xc6;
3808 break;
3809 case 0xc6:
3810 code = 0xc4;
3811 break;
3812 case 0xc7:
3813 code = 0xc5;
3814 break;
3815 case 0xc5:
3816 code = 0xc7;
3817 break;
3818 case 0xe8:
3819 code = 0xe9;
3820 break;
3821 case 0x9d:
3822 code = 0xe8;
3823 break;
3824 case 0xea:
3825 code = 0xeb;
3826 break;
3827 case 0xeb:
3828 code = 0xea;
3829 break;
3830 }
3831 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3832
3833 /* Set the reloc type and symbol for the first branch
3834 from the second branch. */
3835 irel->r_info = nrel->r_info;
3836
3837 /* Make the reloc for the second branch a null reloc. */
3838 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3839 R_MN10300_NONE);
3840
3841 /* Delete two bytes of data. */
3842 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3843 irel->r_offset + 1, 2))
3844 goto error_return;
3845
3846 /* That will change things, so, we should relax again.
3847 Note that this is not required, and it may be slow. */
3848 *again = TRUE;
3849 }
3850
3851 /* Try to turn a 24 immediate, displacement or absolute address
3852 into a 8 immediate, displacement or absolute address. */
3853 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
3854 {
3855 bfd_vma value = symval;
3856 value += irel->r_addend;
3857
3858 /* See if the value will fit in 8 bits. */
3859 if ((long) value < 0x7f && (long) value > -0x80)
3860 {
3861 unsigned char code;
3862
3863 /* AM33 insns which have 24 operands are 6 bytes long and
3864 will have 0xfd as the first byte. */
3865
3866 /* Get the first opcode. */
3867 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3868
3869 if (code == 0xfd)
3870 {
3871 /* Get the second opcode. */
3872 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3873
3874 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3875 equivalent instructions exists. */
3876 if (code != 0x6b && code != 0x7b
3877 && code != 0x8b && code != 0x9b
3878 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3879 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3880 || (code & 0x0f) == 0x0e))
3881 {
3882 /* Not safe if the high bit is on as relaxing may
3883 move the value out of high mem and thus not fit
3884 in a signed 8bit value. This is currently over
3885 conservative. */
3886 if ((value & 0x80) == 0)
3887 {
3888 /* Note that we've changed the relocation contents,
3889 etc. */
3890 elf_section_data (sec)->relocs = internal_relocs;
3891 elf_section_data (sec)->this_hdr.contents = contents;
3892 symtab_hdr->contents = (unsigned char *) isymbuf;
3893
3894 /* Fix the opcode. */
3895 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
3896 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3897
3898 /* Fix the relocation's type. */
3899 irel->r_info =
3900 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3901 R_MN10300_8);
3902
3903 /* Delete two bytes of data. */
3904 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3905 irel->r_offset + 1, 2))
3906 goto error_return;
3907
3908 /* That will change things, so, we should relax
3909 again. Note that this is not required, and it
3910 may be slow. */
3911 *again = TRUE;
3912 break;
3913 }
3914 }
3915 }
3916 }
3917 }
3918
3919 /* Try to turn a 32bit immediate, displacement or absolute address
3920 into a 16bit immediate, displacement or absolute address. */
3921 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
3922 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
3923 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3924 {
3925 bfd_vma value = symval;
3926
3927 if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
3928 {
3929 asection * sgot;
3930
3931 sgot = bfd_get_section_by_name (elf_hash_table (link_info)
3932 ->dynobj, ".got");
3933
3934 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
3935 {
3936 value = sgot->output_offset;
3937
3938 if (h)
3939 value += h->root.got.offset;
3940 else
3941 value += (elf_local_got_offsets
3942 (abfd)[ELF32_R_SYM (irel->r_info)]);
3943 }
3944 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3945 value -= sgot->output_section->vma;
3946 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
3947 value = (sgot->output_section->vma
3948 - (sec->output_section->vma
3949 + sec->output_offset
3950 + irel->r_offset));
3951 else
3952 abort ();
3953 }
3954
3955 value += irel->r_addend;
3956
3957 /* See if the value will fit in 24 bits.
3958 We allow any 16bit match here. We prune those we can't
3959 handle below. */
3960 if ((long) value < 0x7fffff && (long) value > -0x800000)
3961 {
3962 unsigned char code;
3963
3964 /* AM33 insns which have 32bit operands are 7 bytes long and
3965 will have 0xfe as the first byte. */
3966
3967 /* Get the first opcode. */
3968 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3969
3970 if (code == 0xfe)
3971 {
3972 /* Get the second opcode. */
3973 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3974
3975 /* All the am33 32 -> 24 relaxing possibilities. */
3976 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3977 equivalent instructions exists. */
3978 if (code != 0x6b && code != 0x7b
3979 && code != 0x8b && code != 0x9b
3980 && (ELF32_R_TYPE (irel->r_info)
3981 != (int) R_MN10300_GOTPC32)
3982 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3983 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3984 || (code & 0x0f) == 0x0e))
3985 {
3986 /* Not safe if the high bit is on as relaxing may
3987 move the value out of high mem and thus not fit
3988 in a signed 16bit value. This is currently over
3989 conservative. */
3990 if ((value & 0x8000) == 0)
3991 {
3992 /* Note that we've changed the relocation contents,
3993 etc. */
3994 elf_section_data (sec)->relocs = internal_relocs;
3995 elf_section_data (sec)->this_hdr.contents = contents;
3996 symtab_hdr->contents = (unsigned char *) isymbuf;
3997
3998 /* Fix the opcode. */
3999 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
4000 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
4001
4002 /* Fix the relocation's type. */
4003 irel->r_info =
4004 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4005 (ELF32_R_TYPE (irel->r_info)
4006 == (int) R_MN10300_GOTOFF32)
4007 ? R_MN10300_GOTOFF24
4008 : (ELF32_R_TYPE (irel->r_info)
4009 == (int) R_MN10300_GOT32)
4010 ? R_MN10300_GOT24 :
4011 R_MN10300_24);
4012
4013 /* Delete one byte of data. */
4014 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4015 irel->r_offset + 3, 1))
4016 goto error_return;
4017
4018 /* That will change things, so, we should relax
4019 again. Note that this is not required, and it
4020 may be slow. */
4021 *again = TRUE;
4022 break;
4023 }
4024 }
4025 }
4026 }
4027
4028 /* See if the value will fit in 16 bits.
4029 We allow any 16bit match here. We prune those we can't
4030 handle below. */
4031 if ((long) value < 0x7fff && (long) value > -0x8000)
4032 {
4033 unsigned char code;
4034
4035 /* Most insns which have 32bit operands are 6 bytes long;
4036 exceptions are pcrel insns and bit insns.
4037
4038 We handle pcrel insns above. We don't bother trying
4039 to handle the bit insns here.
4040
4041 The first byte of the remaining insns will be 0xfc. */
4042
4043 /* Get the first opcode. */
4044 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
4045
4046 if (code != 0xfc)
4047 continue;
4048
4049 /* Get the second opcode. */
4050 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
4051
4052 if ((code & 0xf0) < 0x80)
4053 switch (code & 0xf0)
4054 {
4055 /* mov (d32,am),dn -> mov (d32,am),dn
4056 mov dm,(d32,am) -> mov dn,(d32,am)
4057 mov (d32,am),an -> mov (d32,am),an
4058 mov dm,(d32,am) -> mov dn,(d32,am)
4059 movbu (d32,am),dn -> movbu (d32,am),dn
4060 movbu dm,(d32,am) -> movbu dn,(d32,am)
4061 movhu (d32,am),dn -> movhu (d32,am),dn
4062 movhu dm,(d32,am) -> movhu dn,(d32,am) */
4063 case 0x00:
4064 case 0x10:
4065 case 0x20:
4066 case 0x30:
4067 case 0x40:
4068 case 0x50:
4069 case 0x60:
4070 case 0x70:
4071 /* Not safe if the high bit is on as relaxing may
4072 move the value out of high mem and thus not fit
4073 in a signed 16bit value. */
4074 if (code == 0xcc
4075 && (value & 0x8000))
4076 continue;
4077
4078 /* Note that we've changed the relocation contents, etc. */
4079 elf_section_data (sec)->relocs = internal_relocs;
4080 elf_section_data (sec)->this_hdr.contents = contents;
4081 symtab_hdr->contents = (unsigned char *) isymbuf;
4082
4083 /* Fix the opcode. */
4084 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
4085 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
4086
4087 /* Fix the relocation's type. */
4088 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4089 (ELF32_R_TYPE (irel->r_info)
4090 == (int) R_MN10300_GOTOFF32)
4091 ? R_MN10300_GOTOFF16
4092 : (ELF32_R_TYPE (irel->r_info)
4093 == (int) R_MN10300_GOT32)
4094 ? R_MN10300_GOT16
4095 : (ELF32_R_TYPE (irel->r_info)
4096 == (int) R_MN10300_GOTPC32)
4097 ? R_MN10300_GOTPC16 :
4098 R_MN10300_16);
4099
4100 /* Delete two bytes of data. */
4101 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4102 irel->r_offset + 2, 2))
4103 goto error_return;
4104
4105 /* That will change things, so, we should relax again.
4106 Note that this is not required, and it may be slow. */
4107 *again = TRUE;
4108 break;
4109 }
4110 else if ((code & 0xf0) == 0x80
4111 || (code & 0xf0) == 0x90)
4112 switch (code & 0xf3)
4113 {
4114 /* mov dn,(abs32) -> mov dn,(abs16)
4115 movbu dn,(abs32) -> movbu dn,(abs16)
4116 movhu dn,(abs32) -> movhu dn,(abs16) */
4117 case 0x81:
4118 case 0x82:
4119 case 0x83:
4120 /* Note that we've changed the relocation contents, etc. */
4121 elf_section_data (sec)->relocs = internal_relocs;
4122 elf_section_data (sec)->this_hdr.contents = contents;
4123 symtab_hdr->contents = (unsigned char *) isymbuf;
4124
4125 if ((code & 0xf3) == 0x81)
4126 code = 0x01 + (code & 0x0c);
4127 else if ((code & 0xf3) == 0x82)
4128 code = 0x02 + (code & 0x0c);
4129 else if ((code & 0xf3) == 0x83)
4130 code = 0x03 + (code & 0x0c);
4131 else
4132 abort ();
4133
4134 /* Fix the opcode. */
4135 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
4136
4137 /* Fix the relocation's type. */
4138 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4139 (ELF32_R_TYPE (irel->r_info)
4140 == (int) R_MN10300_GOTOFF32)
4141 ? R_MN10300_GOTOFF16
4142 : (ELF32_R_TYPE (irel->r_info)
4143 == (int) R_MN10300_GOT32)
4144 ? R_MN10300_GOT16
4145 : (ELF32_R_TYPE (irel->r_info)
4146 == (int) R_MN10300_GOTPC32)
4147 ? R_MN10300_GOTPC16 :
4148 R_MN10300_16);
4149
4150 /* The opcode got shorter too, so we have to fix the
4151 addend and offset too! */
4152 irel->r_offset -= 1;
4153
4154 /* Delete three bytes of data. */
4155 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4156 irel->r_offset + 1, 3))
4157 goto error_return;
4158
4159 /* That will change things, so, we should relax again.
4160 Note that this is not required, and it may be slow. */
4161 *again = TRUE;
4162 break;
4163
4164 /* mov am,(abs32) -> mov am,(abs16)
4165 mov am,(d32,sp) -> mov am,(d16,sp)
4166 mov dm,(d32,sp) -> mov dm,(d32,sp)
4167 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
4168 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
4169 case 0x80:
4170 case 0x90:
4171 case 0x91:
4172 case 0x92:
4173 case 0x93:
4174 /* sp-based offsets are zero-extended. */
4175 if (code >= 0x90 && code <= 0x93
4176 && (long) value < 0)
4177 continue;
4178
4179 /* Note that we've changed the relocation contents, etc. */
4180 elf_section_data (sec)->relocs = internal_relocs;
4181 elf_section_data (sec)->this_hdr.contents = contents;
4182 symtab_hdr->contents = (unsigned char *) isymbuf;
4183
4184 /* Fix the opcode. */
4185 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
4186 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
4187
4188 /* Fix the relocation's type. */
4189 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4190 (ELF32_R_TYPE (irel->r_info)
4191 == (int) R_MN10300_GOTOFF32)
4192 ? R_MN10300_GOTOFF16
4193 : (ELF32_R_TYPE (irel->r_info)
4194 == (int) R_MN10300_GOT32)
4195 ? R_MN10300_GOT16
4196 : (ELF32_R_TYPE (irel->r_info)
4197 == (int) R_MN10300_GOTPC32)
4198 ? R_MN10300_GOTPC16 :
4199 R_MN10300_16);
4200
4201 /* Delete two bytes of data. */
4202 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4203 irel->r_offset + 2, 2))
4204 goto error_return;
4205
4206 /* That will change things, so, we should relax again.
4207 Note that this is not required, and it may be slow. */
4208 *again = TRUE;
4209 break;
4210 }
4211 else if ((code & 0xf0) < 0xf0)
4212 switch (code & 0xfc)
4213 {
4214 /* mov imm32,dn -> mov imm16,dn
4215 mov imm32,an -> mov imm16,an
4216 mov (abs32),dn -> mov (abs16),dn
4217 movbu (abs32),dn -> movbu (abs16),dn
4218 movhu (abs32),dn -> movhu (abs16),dn */
4219 case 0xcc:
4220 case 0xdc:
4221 case 0xa4:
4222 case 0xa8:
4223 case 0xac:
4224 /* Not safe if the high bit is on as relaxing may
4225 move the value out of high mem and thus not fit
4226 in a signed 16bit value. */
4227 if (code == 0xcc
4228 && (value & 0x8000))
4229 continue;
4230
4231 /* "mov imm16, an" zero-extends the immediate. */
4232 if ((code & 0xfc) == 0xdc
4233 && (long) value < 0)
4234 continue;
4235
4236 /* Note that we've changed the relocation contents, etc. */
4237 elf_section_data (sec)->relocs = internal_relocs;
4238 elf_section_data (sec)->this_hdr.contents = contents;
4239 symtab_hdr->contents = (unsigned char *) isymbuf;
4240
4241 if ((code & 0xfc) == 0xcc)
4242 code = 0x2c + (code & 0x03);
4243 else if ((code & 0xfc) == 0xdc)
4244 code = 0x24 + (code & 0x03);
4245 else if ((code & 0xfc) == 0xa4)
4246 code = 0x30 + (code & 0x03);
4247 else if ((code & 0xfc) == 0xa8)
4248 code = 0x34 + (code & 0x03);
4249 else if ((code & 0xfc) == 0xac)
4250 code = 0x38 + (code & 0x03);
4251 else
4252 abort ();
4253
4254 /* Fix the opcode. */
4255 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
4256
4257 /* Fix the relocation's type. */
4258 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4259 (ELF32_R_TYPE (irel->r_info)
4260 == (int) R_MN10300_GOTOFF32)
4261 ? R_MN10300_GOTOFF16
4262 : (ELF32_R_TYPE (irel->r_info)
4263 == (int) R_MN10300_GOT32)
4264 ? R_MN10300_GOT16
4265 : (ELF32_R_TYPE (irel->r_info)
4266 == (int) R_MN10300_GOTPC32)
4267 ? R_MN10300_GOTPC16 :
4268 R_MN10300_16);
4269
4270 /* The opcode got shorter too, so we have to fix the
4271 addend and offset too! */
4272 irel->r_offset -= 1;
4273
4274 /* Delete three bytes of data. */
4275 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4276 irel->r_offset + 1, 3))
4277 goto error_return;
4278
4279 /* That will change things, so, we should relax again.
4280 Note that this is not required, and it may be slow. */
4281 *again = TRUE;
4282 break;
4283
4284 /* mov (abs32),an -> mov (abs16),an
4285 mov (d32,sp),an -> mov (d16,sp),an
4286 mov (d32,sp),dn -> mov (d16,sp),dn
4287 movbu (d32,sp),dn -> movbu (d16,sp),dn
4288 movhu (d32,sp),dn -> movhu (d16,sp),dn
4289 add imm32,dn -> add imm16,dn
4290 cmp imm32,dn -> cmp imm16,dn
4291 add imm32,an -> add imm16,an
4292 cmp imm32,an -> cmp imm16,an
4293 and imm32,dn -> and imm16,dn
4294 or imm32,dn -> or imm16,dn
4295 xor imm32,dn -> xor imm16,dn
4296 btst imm32,dn -> btst imm16,dn */
4297
4298 case 0xa0:
4299 case 0xb0:
4300 case 0xb1:
4301 case 0xb2:
4302 case 0xb3:
4303 case 0xc0:
4304 case 0xc8:
4305
4306 case 0xd0:
4307 case 0xd8:
4308 case 0xe0:
4309 case 0xe1:
4310 case 0xe2:
4311 case 0xe3:
4312 /* cmp imm16, an zero-extends the immediate. */
4313 if (code == 0xdc
4314 && (long) value < 0)
4315 continue;
4316
4317 /* So do sp-based offsets. */
4318 if (code >= 0xb0 && code <= 0xb3
4319 && (long) value < 0)
4320 continue;
4321
4322 /* Note that we've changed the relocation contents, etc. */
4323 elf_section_data (sec)->relocs = internal_relocs;
4324 elf_section_data (sec)->this_hdr.contents = contents;
4325 symtab_hdr->contents = (unsigned char *) isymbuf;
4326
4327 /* Fix the opcode. */
4328 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
4329 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
4330
4331 /* Fix the relocation's type. */
4332 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4333 (ELF32_R_TYPE (irel->r_info)
4334 == (int) R_MN10300_GOTOFF32)
4335 ? R_MN10300_GOTOFF16
4336 : (ELF32_R_TYPE (irel->r_info)
4337 == (int) R_MN10300_GOT32)
4338 ? R_MN10300_GOT16
4339 : (ELF32_R_TYPE (irel->r_info)
4340 == (int) R_MN10300_GOTPC32)
4341 ? R_MN10300_GOTPC16 :
4342 R_MN10300_16);
4343
4344 /* Delete two bytes of data. */
4345 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4346 irel->r_offset + 2, 2))
4347 goto error_return;
4348
4349 /* That will change things, so, we should relax again.
4350 Note that this is not required, and it may be slow. */
4351 *again = TRUE;
4352 break;
4353 }
4354 else if (code == 0xfe)
4355 {
4356 /* add imm32,sp -> add imm16,sp */
4357
4358 /* Note that we've changed the relocation contents, etc. */
4359 elf_section_data (sec)->relocs = internal_relocs;
4360 elf_section_data (sec)->this_hdr.contents = contents;
4361 symtab_hdr->contents = (unsigned char *) isymbuf;
4362
4363 /* Fix the opcode. */
4364 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
4365 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
4366
4367 /* Fix the relocation's type. */
4368 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4369 (ELF32_R_TYPE (irel->r_info)
4370 == (int) R_MN10300_GOT32)
4371 ? R_MN10300_GOT16
4372 : (ELF32_R_TYPE (irel->r_info)
4373 == (int) R_MN10300_GOTOFF32)
4374 ? R_MN10300_GOTOFF16
4375 : (ELF32_R_TYPE (irel->r_info)
4376 == (int) R_MN10300_GOTPC32)
4377 ? R_MN10300_GOTPC16 :
4378 R_MN10300_16);
4379
4380 /* Delete two bytes of data. */
4381 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4382 irel->r_offset + 2, 2))
4383 goto error_return;
4384
4385 /* That will change things, so, we should relax again.
4386 Note that this is not required, and it may be slow. */
4387 *again = TRUE;
4388 break;
4389 }
4390 }
4391 }
4392 }
4393
4394 if (isymbuf != NULL
4395 && symtab_hdr->contents != (unsigned char *) isymbuf)
4396 {
4397 if (! link_info->keep_memory)
4398 free (isymbuf);
4399 else
4400 {
4401 /* Cache the symbols for elf_link_input_bfd. */
4402 symtab_hdr->contents = (unsigned char *) isymbuf;
4403 }
4404 }
4405
4406 if (contents != NULL
4407 && elf_section_data (sec)->this_hdr.contents != contents)
4408 {
4409 if (! link_info->keep_memory)
4410 free (contents);
4411 else
4412 {
4413 /* Cache the section contents for elf_link_input_bfd. */
4414 elf_section_data (sec)->this_hdr.contents = contents;
4415 }
4416 }
4417
4418 if (internal_relocs != NULL
4419 && elf_section_data (sec)->relocs != internal_relocs)
4420 free (internal_relocs);
4421
4422 return TRUE;
4423
4424 error_return:
4425 if (isymbuf != NULL
4426 && symtab_hdr->contents != (unsigned char *) isymbuf)
4427 free (isymbuf);
4428 if (contents != NULL
4429 && elf_section_data (section)->this_hdr.contents != contents)
4430 free (contents);
4431 if (internal_relocs != NULL
4432 && elf_section_data (section)->relocs != internal_relocs)
4433 free (internal_relocs);
4434
4435 return FALSE;
4436 }
4437
4438 /* This is a version of bfd_generic_get_relocated_section_contents
4439 which uses mn10300_elf_relocate_section. */
4440
4441 static bfd_byte *
4442 mn10300_elf_get_relocated_section_contents (bfd *output_bfd,
4443 struct bfd_link_info *link_info,
4444 struct bfd_link_order *link_order,
4445 bfd_byte *data,
4446 bfd_boolean relocatable,
4447 asymbol **symbols)
4448 {
4449 Elf_Internal_Shdr *symtab_hdr;
4450 asection *input_section = link_order->u.indirect.section;
4451 bfd *input_bfd = input_section->owner;
4452 asection **sections = NULL;
4453 Elf_Internal_Rela *internal_relocs = NULL;
4454 Elf_Internal_Sym *isymbuf = NULL;
4455
4456 /* We only need to handle the case of relaxing, or of having a
4457 particular set of section contents, specially. */
4458 if (relocatable
4459 || elf_section_data (input_section)->this_hdr.contents == NULL)
4460 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
4461 link_order, data,
4462 relocatable,
4463 symbols);
4464
4465 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4466
4467 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
4468 (size_t) input_section->size);
4469
4470 if ((input_section->flags & SEC_RELOC) != 0
4471 && input_section->reloc_count > 0)
4472 {
4473 asection **secpp;
4474 Elf_Internal_Sym *isym, *isymend;
4475 bfd_size_type amt;
4476
4477 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, input_section,
4478 NULL, NULL, FALSE);
4479 if (internal_relocs == NULL)
4480 goto error_return;
4481
4482 if (symtab_hdr->sh_info != 0)
4483 {
4484 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4485 if (isymbuf == NULL)
4486 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
4487 symtab_hdr->sh_info, 0,
4488 NULL, NULL, NULL);
4489 if (isymbuf == NULL)
4490 goto error_return;
4491 }
4492
4493 amt = symtab_hdr->sh_info;
4494 amt *= sizeof (asection *);
4495 sections = bfd_malloc (amt);
4496 if (sections == NULL && amt != 0)
4497 goto error_return;
4498
4499 isymend = isymbuf + symtab_hdr->sh_info;
4500 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
4501 {
4502 asection *isec;
4503
4504 if (isym->st_shndx == SHN_UNDEF)
4505 isec = bfd_und_section_ptr;
4506 else if (isym->st_shndx == SHN_ABS)
4507 isec = bfd_abs_section_ptr;
4508 else if (isym->st_shndx == SHN_COMMON)
4509 isec = bfd_com_section_ptr;
4510 else
4511 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
4512
4513 *secpp = isec;
4514 }
4515
4516 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
4517 input_section, data, internal_relocs,
4518 isymbuf, sections))
4519 goto error_return;
4520
4521 if (sections != NULL)
4522 free (sections);
4523 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
4524 free (isymbuf);
4525 if (internal_relocs != elf_section_data (input_section)->relocs)
4526 free (internal_relocs);
4527 }
4528
4529 return data;
4530
4531 error_return:
4532 if (sections != NULL)
4533 free (sections);
4534 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
4535 free (isymbuf);
4536 if (internal_relocs != NULL
4537 && internal_relocs != elf_section_data (input_section)->relocs)
4538 free (internal_relocs);
4539 return NULL;
4540 }
4541
4542 /* Assorted hash table functions. */
4543
4544 /* Initialize an entry in the link hash table. */
4545
4546 /* Create an entry in an MN10300 ELF linker hash table. */
4547
4548 static struct bfd_hash_entry *
4549 elf32_mn10300_link_hash_newfunc (struct bfd_hash_entry *entry,
4550 struct bfd_hash_table *table,
4551 const char *string)
4552 {
4553 struct elf32_mn10300_link_hash_entry *ret =
4554 (struct elf32_mn10300_link_hash_entry *) entry;
4555
4556 /* Allocate the structure if it has not already been allocated by a
4557 subclass. */
4558 if (ret == NULL)
4559 ret = (struct elf32_mn10300_link_hash_entry *)
4560 bfd_hash_allocate (table, sizeof (* ret));
4561 if (ret == NULL)
4562 return (struct bfd_hash_entry *) ret;
4563
4564 /* Call the allocation method of the superclass. */
4565 ret = (struct elf32_mn10300_link_hash_entry *)
4566 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
4567 table, string);
4568 if (ret != NULL)
4569 {
4570 ret->direct_calls = 0;
4571 ret->stack_size = 0;
4572 ret->movm_args = 0;
4573 ret->movm_stack_size = 0;
4574 ret->flags = 0;
4575 ret->value = 0;
4576 ret->tls_type = GOT_UNKNOWN;
4577 }
4578
4579 return (struct bfd_hash_entry *) ret;
4580 }
4581
4582 static void
4583 _bfd_mn10300_copy_indirect_symbol (struct bfd_link_info * info,
4584 struct elf_link_hash_entry * dir,
4585 struct elf_link_hash_entry * ind)
4586 {
4587 struct elf32_mn10300_link_hash_entry * edir;
4588 struct elf32_mn10300_link_hash_entry * eind;
4589
4590 edir = elf_mn10300_hash_entry (dir);
4591 eind = elf_mn10300_hash_entry (ind);
4592
4593 if (ind->root.type == bfd_link_hash_indirect
4594 && dir->got.refcount <= 0)
4595 {
4596 edir->tls_type = eind->tls_type;
4597 eind->tls_type = GOT_UNKNOWN;
4598 }
4599 edir->direct_calls = eind->direct_calls;
4600 edir->stack_size = eind->stack_size;
4601 edir->movm_args = eind->movm_args;
4602 edir->movm_stack_size = eind->movm_stack_size;
4603 edir->flags = eind->flags;
4604
4605 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
4606 }
4607
4608 /* Create an mn10300 ELF linker hash table. */
4609
4610 static struct bfd_link_hash_table *
4611 elf32_mn10300_link_hash_table_create (bfd *abfd)
4612 {
4613 struct elf32_mn10300_link_hash_table *ret;
4614 bfd_size_type amt = sizeof (* ret);
4615
4616 ret = bfd_malloc (amt);
4617 if (ret == NULL)
4618 return NULL;
4619
4620 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
4621 elf32_mn10300_link_hash_newfunc,
4622 sizeof (struct elf32_mn10300_link_hash_entry),
4623 MN10300_ELF_DATA))
4624 {
4625 free (ret);
4626 return NULL;
4627 }
4628
4629 ret->flags = 0;
4630 ret->tls_ldm_got.refcount = 0;
4631 ret->tls_ldm_got.offset = -1;
4632 ret->tls_ldm_got.got_allocated = 0;
4633 ret->tls_ldm_got.rel_emitted = 0;
4634
4635 amt = sizeof (struct elf_link_hash_table);
4636 ret->static_hash_table = bfd_malloc (amt);
4637 if (ret->static_hash_table == NULL)
4638 {
4639 free (ret);
4640 return NULL;
4641 }
4642
4643 if (!_bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
4644 elf32_mn10300_link_hash_newfunc,
4645 sizeof (struct elf32_mn10300_link_hash_entry),
4646 MN10300_ELF_DATA))
4647 {
4648 free (ret->static_hash_table);
4649 free (ret);
4650 return NULL;
4651 }
4652 return & ret->root.root;
4653 }
4654
4655 /* Free an mn10300 ELF linker hash table. */
4656
4657 static void
4658 elf32_mn10300_link_hash_table_free (struct bfd_link_hash_table *hash)
4659 {
4660 struct elf32_mn10300_link_hash_table *ret
4661 = (struct elf32_mn10300_link_hash_table *) hash;
4662
4663 _bfd_generic_link_hash_table_free
4664 ((struct bfd_link_hash_table *) ret->static_hash_table);
4665 _bfd_generic_link_hash_table_free
4666 ((struct bfd_link_hash_table *) ret);
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 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
4777
4778 /* Return size of a PLT entry. */
4779 #define elf_mn10300_sizeof_plt(info) \
4780 (info->shared ? 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 int ptralign = 0;
4814
4815 switch (bed->s->arch_size)
4816 {
4817 case 32:
4818 ptralign = 2;
4819 break;
4820
4821 case 64:
4822 ptralign = 3;
4823 break;
4824
4825 default:
4826 bfd_set_error (bfd_error_bad_value);
4827 return FALSE;
4828 }
4829
4830 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4831 .rel[a].bss sections. */
4832 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4833 | SEC_LINKER_CREATED);
4834
4835 s = bfd_make_section_with_flags (abfd,
4836 (bed->default_use_rela_p
4837 ? ".rela.plt" : ".rel.plt"),
4838 flags | SEC_READONLY);
4839 if (s == NULL
4840 || ! bfd_set_section_alignment (abfd, s, ptralign))
4841 return FALSE;
4842
4843 if (! _bfd_mn10300_elf_create_got_section (abfd, info))
4844 return FALSE;
4845
4846 {
4847 const char * secname;
4848 char * relname;
4849 flagword secflags;
4850 asection * sec;
4851
4852 for (sec = abfd->sections; sec; sec = sec->next)
4853 {
4854 secflags = bfd_get_section_flags (abfd, sec);
4855 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
4856 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
4857 continue;
4858
4859 secname = bfd_get_section_name (abfd, sec);
4860 relname = bfd_malloc (strlen (secname) + 6);
4861 strcpy (relname, ".rela");
4862 strcat (relname, secname);
4863
4864 s = bfd_make_section_with_flags (abfd, relname,
4865 flags | SEC_READONLY);
4866 if (s == NULL
4867 || ! bfd_set_section_alignment (abfd, s, ptralign))
4868 return FALSE;
4869 }
4870 }
4871
4872 if (bed->want_dynbss)
4873 {
4874 /* The .dynbss section is a place to put symbols which are defined
4875 by dynamic objects, are referenced by regular objects, and are
4876 not functions. We must allocate space for them in the process
4877 image and use a R_*_COPY reloc to tell the dynamic linker to
4878 initialize them at run time. The linker script puts the .dynbss
4879 section into the .bss section of the final image. */
4880 s = bfd_make_section_with_flags (abfd, ".dynbss",
4881 SEC_ALLOC | SEC_LINKER_CREATED);
4882 if (s == NULL)
4883 return FALSE;
4884
4885 /* The .rel[a].bss section holds copy relocs. This section is not
4886 normally needed. We need to create it here, though, so that the
4887 linker will map it to an output section. We can't just create it
4888 only if we need it, because we will not know whether we need it
4889 until we have seen all the input files, and the first time the
4890 main linker code calls BFD after examining all the input files
4891 (size_dynamic_sections) the input sections have already been
4892 mapped to the output sections. If the section turns out not to
4893 be needed, we can discard it later. We will never need this
4894 section when generating a shared object, since they do not use
4895 copy relocs. */
4896 if (! info->shared)
4897 {
4898 s = bfd_make_section_with_flags (abfd,
4899 (bed->default_use_rela_p
4900 ? ".rela.bss" : ".rel.bss"),
4901 flags | SEC_READONLY);
4902 if (s == NULL
4903 || ! bfd_set_section_alignment (abfd, s, ptralign))
4904 return FALSE;
4905 }
4906 }
4907
4908 return TRUE;
4909 }
4910 \f
4911 /* Adjust a symbol defined by a dynamic object and referenced by a
4912 regular object. The current definition is in some section of the
4913 dynamic object, but we're not including those sections. We have to
4914 change the definition to something the rest of the link can
4915 understand. */
4916
4917 static bfd_boolean
4918 _bfd_mn10300_elf_adjust_dynamic_symbol (struct bfd_link_info * info,
4919 struct elf_link_hash_entry * h)
4920 {
4921 bfd * dynobj;
4922 asection * s;
4923
4924 dynobj = elf_hash_table (info)->dynobj;
4925
4926 /* Make sure we know what is going on here. */
4927 BFD_ASSERT (dynobj != NULL
4928 && (h->needs_plt
4929 || h->u.weakdef != NULL
4930 || (h->def_dynamic
4931 && h->ref_regular
4932 && !h->def_regular)));
4933
4934 /* If this is a function, put it in the procedure linkage table. We
4935 will fill in the contents of the procedure linkage table later,
4936 when we know the address of the .got section. */
4937 if (h->type == STT_FUNC
4938 || h->needs_plt)
4939 {
4940 if (! info->shared
4941 && !h->def_dynamic
4942 && !h->ref_dynamic)
4943 {
4944 /* This case can occur if we saw a PLT reloc in an input
4945 file, but the symbol was never referred to by a dynamic
4946 object. In such a case, we don't actually need to build
4947 a procedure linkage table, and we can just do a REL32
4948 reloc instead. */
4949 BFD_ASSERT (h->needs_plt);
4950 return TRUE;
4951 }
4952
4953 /* Make sure this symbol is output as a dynamic symbol. */
4954 if (h->dynindx == -1)
4955 {
4956 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4957 return FALSE;
4958 }
4959
4960 s = bfd_get_section_by_name (dynobj, ".plt");
4961 BFD_ASSERT (s != NULL);
4962
4963 /* If this is the first .plt entry, make room for the special
4964 first entry. */
4965 if (s->size == 0)
4966 s->size += elf_mn10300_sizeof_plt0 (info);
4967
4968 /* If this symbol is not defined in a regular file, and we are
4969 not generating a shared library, then set the symbol to this
4970 location in the .plt. This is required to make function
4971 pointers compare as equal between the normal executable and
4972 the shared library. */
4973 if (! info->shared
4974 && !h->def_regular)
4975 {
4976 h->root.u.def.section = s;
4977 h->root.u.def.value = s->size;
4978 }
4979
4980 h->plt.offset = s->size;
4981
4982 /* Make room for this entry. */
4983 s->size += elf_mn10300_sizeof_plt (info);
4984
4985 /* We also need to make an entry in the .got.plt section, which
4986 will be placed in the .got section by the linker script. */
4987 s = bfd_get_section_by_name (dynobj, ".got.plt");
4988 BFD_ASSERT (s != NULL);
4989 s->size += 4;
4990
4991 /* We also need to make an entry in the .rela.plt section. */
4992 s = bfd_get_section_by_name (dynobj, ".rela.plt");
4993 BFD_ASSERT (s != NULL);
4994 s->size += sizeof (Elf32_External_Rela);
4995
4996 return TRUE;
4997 }
4998
4999 /* If this is a weak symbol, and there is a real definition, the
5000 processor independent code will have arranged for us to see the
5001 real definition first, and we can just use the same value. */
5002 if (h->u.weakdef != NULL)
5003 {
5004 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
5005 || h->u.weakdef->root.type == bfd_link_hash_defweak);
5006 h->root.u.def.section = h->u.weakdef->root.u.def.section;
5007 h->root.u.def.value = h->u.weakdef->root.u.def.value;
5008 return TRUE;
5009 }
5010
5011 /* This is a reference to a symbol defined by a dynamic object which
5012 is not a function. */
5013
5014 /* If we are creating a shared library, we must presume that the
5015 only references to the symbol are via the global offset table.
5016 For such cases we need not do anything here; the relocations will
5017 be handled correctly by relocate_section. */
5018 if (info->shared)
5019 return TRUE;
5020
5021 /* If there are no references to this symbol that do not use the
5022 GOT, we don't need to generate a copy reloc. */
5023 if (!h->non_got_ref)
5024 return TRUE;
5025
5026 /* We must allocate the symbol in our .dynbss section, which will
5027 become part of the .bss section of the executable. There will be
5028 an entry for this symbol in the .dynsym section. The dynamic
5029 object will contain position independent code, so all references
5030 from the dynamic object to this symbol will go through the global
5031 offset table. The dynamic linker will use the .dynsym entry to
5032 determine the address it must put in the global offset table, so
5033 both the dynamic object and the regular object will refer to the
5034 same memory location for the variable. */
5035
5036 s = bfd_get_section_by_name (dynobj, ".dynbss");
5037 BFD_ASSERT (s != NULL);
5038
5039 /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
5040 copy the initial value out of the dynamic object and into the
5041 runtime process image. We need to remember the offset into the
5042 .rela.bss section we are going to use. */
5043 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5044 {
5045 asection * srel;
5046
5047 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
5048 BFD_ASSERT (srel != NULL);
5049 srel->size += sizeof (Elf32_External_Rela);
5050 h->needs_copy = 1;
5051 }
5052
5053 return _bfd_elf_adjust_dynamic_copy (h, s);
5054 }
5055
5056 /* Set the sizes of the dynamic sections. */
5057
5058 static bfd_boolean
5059 _bfd_mn10300_elf_size_dynamic_sections (bfd * output_bfd,
5060 struct bfd_link_info * info)
5061 {
5062 struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info);
5063 bfd * dynobj;
5064 asection * s;
5065 bfd_boolean plt;
5066 bfd_boolean relocs;
5067 bfd_boolean reltext;
5068
5069 dynobj = elf_hash_table (info)->dynobj;
5070 BFD_ASSERT (dynobj != NULL);
5071
5072 if (elf_hash_table (info)->dynamic_sections_created)
5073 {
5074 /* Set the contents of the .interp section to the interpreter. */
5075 if (info->executable)
5076 {
5077 s = bfd_get_section_by_name (dynobj, ".interp");
5078 BFD_ASSERT (s != NULL);
5079 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5080 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5081 }
5082 }
5083 else
5084 {
5085 /* We may have created entries in the .rela.got section.
5086 However, if we are not creating the dynamic sections, we will
5087 not actually use these entries. Reset the size of .rela.got,
5088 which will cause it to get stripped from the output file
5089 below. */
5090 s = bfd_get_section_by_name (dynobj, ".rela.got");
5091 if (s != NULL)
5092 s->size = 0;
5093 }
5094
5095 if (htab->tls_ldm_got.refcount > 0)
5096 {
5097 s = bfd_get_section_by_name (dynobj, ".rela.got");
5098 BFD_ASSERT (s != NULL);
5099 s->size += sizeof (Elf32_External_Rela);
5100 }
5101
5102 /* The check_relocs and adjust_dynamic_symbol entry points have
5103 determined the sizes of the various dynamic sections. Allocate
5104 memory for them. */
5105 plt = FALSE;
5106 relocs = FALSE;
5107 reltext = FALSE;
5108 for (s = dynobj->sections; s != NULL; s = s->next)
5109 {
5110 const char * name;
5111
5112 if ((s->flags & SEC_LINKER_CREATED) == 0)
5113 continue;
5114
5115 /* It's OK to base decisions on the section name, because none
5116 of the dynobj section names depend upon the input files. */
5117 name = bfd_get_section_name (dynobj, s);
5118
5119 if (streq (name, ".plt"))
5120 {
5121 /* Remember whether there is a PLT. */
5122 plt = s->size != 0;
5123 }
5124 else if (CONST_STRNEQ (name, ".rela"))
5125 {
5126 if (s->size != 0)
5127 {
5128 asection * target;
5129
5130 /* Remember whether there are any reloc sections other
5131 than .rela.plt. */
5132 if (! streq (name, ".rela.plt"))
5133 {
5134 const char * outname;
5135
5136 relocs = TRUE;
5137
5138 /* If this relocation section applies to a read only
5139 section, then we probably need a DT_TEXTREL
5140 entry. The entries in the .rela.plt section
5141 really apply to the .got section, which we
5142 created ourselves and so know is not readonly. */
5143 outname = bfd_get_section_name (output_bfd,
5144 s->output_section);
5145 target = bfd_get_section_by_name (output_bfd, outname + 5);
5146 if (target != NULL
5147 && (target->flags & SEC_READONLY) != 0
5148 && (target->flags & SEC_ALLOC) != 0)
5149 reltext = TRUE;
5150 }
5151
5152 /* We use the reloc_count field as a counter if we need
5153 to copy relocs into the output file. */
5154 s->reloc_count = 0;
5155 }
5156 }
5157 else if (! CONST_STRNEQ (name, ".got")
5158 && ! streq (name, ".dynbss"))
5159 /* It's not one of our sections, so don't allocate space. */
5160 continue;
5161
5162 if (s->size == 0)
5163 {
5164 /* If we don't need this section, strip it from the
5165 output file. This is mostly to handle .rela.bss and
5166 .rela.plt. We must create both sections in
5167 create_dynamic_sections, because they must be created
5168 before the linker maps input sections to output
5169 sections. The linker does that before
5170 adjust_dynamic_symbol is called, and it is that
5171 function which decides whether anything needs to go
5172 into these sections. */
5173 s->flags |= SEC_EXCLUDE;
5174 continue;
5175 }
5176
5177 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5178 continue;
5179
5180 /* Allocate memory for the section contents. We use bfd_zalloc
5181 here in case unused entries are not reclaimed before the
5182 section's contents are written out. This should not happen,
5183 but this way if it does, we get a R_MN10300_NONE reloc
5184 instead of garbage. */
5185 s->contents = bfd_zalloc (dynobj, s->size);
5186 if (s->contents == NULL)
5187 return FALSE;
5188 }
5189
5190 if (elf_hash_table (info)->dynamic_sections_created)
5191 {
5192 /* Add some entries to the .dynamic section. We fill in the
5193 values later, in _bfd_mn10300_elf_finish_dynamic_sections,
5194 but we must add the entries now so that we get the correct
5195 size for the .dynamic section. The DT_DEBUG entry is filled
5196 in by the dynamic linker and used by the debugger. */
5197 if (! info->shared)
5198 {
5199 if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
5200 return FALSE;
5201 }
5202
5203 if (plt)
5204 {
5205 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
5206 || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
5207 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
5208 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
5209 return FALSE;
5210 }
5211
5212 if (relocs)
5213 {
5214 if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
5215 || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
5216 || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
5217 sizeof (Elf32_External_Rela)))
5218 return FALSE;
5219 }
5220
5221 if (reltext)
5222 {
5223 if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
5224 return FALSE;
5225 }
5226 }
5227
5228 return TRUE;
5229 }
5230
5231 /* Finish up dynamic symbol handling. We set the contents of various
5232 dynamic sections here. */
5233
5234 static bfd_boolean
5235 _bfd_mn10300_elf_finish_dynamic_symbol (bfd * output_bfd,
5236 struct bfd_link_info * info,
5237 struct elf_link_hash_entry * h,
5238 Elf_Internal_Sym * sym)
5239 {
5240 bfd * dynobj;
5241
5242 dynobj = elf_hash_table (info)->dynobj;
5243
5244 if (h->plt.offset != (bfd_vma) -1)
5245 {
5246 asection * splt;
5247 asection * sgot;
5248 asection * srel;
5249 bfd_vma plt_index;
5250 bfd_vma got_offset;
5251 Elf_Internal_Rela rel;
5252
5253 /* This symbol has an entry in the procedure linkage table. Set
5254 it up. */
5255
5256 BFD_ASSERT (h->dynindx != -1);
5257
5258 splt = bfd_get_section_by_name (dynobj, ".plt");
5259 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
5260 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
5261 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
5262
5263 /* Get the index in the procedure linkage table which
5264 corresponds to this symbol. This is the index of this symbol
5265 in all the symbols for which we are making plt entries. The
5266 first entry in the procedure linkage table is reserved. */
5267 plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
5268 / elf_mn10300_sizeof_plt (info));
5269
5270 /* Get the offset into the .got table of the entry that
5271 corresponds to this function. Each .got entry is 4 bytes.
5272 The first three are reserved. */
5273 got_offset = (plt_index + 3) * 4;
5274
5275 /* Fill in the entry in the procedure linkage table. */
5276 if (! info->shared)
5277 {
5278 memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
5279 elf_mn10300_sizeof_plt (info));
5280 bfd_put_32 (output_bfd,
5281 (sgot->output_section->vma
5282 + sgot->output_offset
5283 + got_offset),
5284 (splt->contents + h->plt.offset
5285 + elf_mn10300_plt_symbol_offset (info)));
5286
5287 bfd_put_32 (output_bfd,
5288 (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
5289 (splt->contents + h->plt.offset
5290 + elf_mn10300_plt_plt0_offset (info)));
5291 }
5292 else
5293 {
5294 memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
5295 elf_mn10300_sizeof_plt (info));
5296
5297 bfd_put_32 (output_bfd, got_offset,
5298 (splt->contents + h->plt.offset
5299 + elf_mn10300_plt_symbol_offset (info)));
5300 }
5301
5302 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
5303 (splt->contents + h->plt.offset
5304 + elf_mn10300_plt_reloc_offset (info)));
5305
5306 /* Fill in the entry in the global offset table. */
5307 bfd_put_32 (output_bfd,
5308 (splt->output_section->vma
5309 + splt->output_offset
5310 + h->plt.offset
5311 + elf_mn10300_plt_temp_offset (info)),
5312 sgot->contents + got_offset);
5313
5314 /* Fill in the entry in the .rela.plt section. */
5315 rel.r_offset = (sgot->output_section->vma
5316 + sgot->output_offset
5317 + got_offset);
5318 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
5319 rel.r_addend = 0;
5320 bfd_elf32_swap_reloca_out (output_bfd, &rel,
5321 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
5322 + plt_index));
5323
5324 if (!h->def_regular)
5325 /* Mark the symbol as undefined, rather than as defined in
5326 the .plt section. Leave the value alone. */
5327 sym->st_shndx = SHN_UNDEF;
5328 }
5329
5330 if (h->got.offset != (bfd_vma) -1)
5331 {
5332 asection * sgot;
5333 asection * srel;
5334 Elf_Internal_Rela rel;
5335
5336 /* This symbol has an entry in the global offset table. Set it up. */
5337 sgot = bfd_get_section_by_name (dynobj, ".got");
5338 srel = bfd_get_section_by_name (dynobj, ".rela.got");
5339 BFD_ASSERT (sgot != NULL && srel != NULL);
5340
5341 rel.r_offset = (sgot->output_section->vma
5342 + sgot->output_offset
5343 + (h->got.offset & ~1));
5344
5345 switch (elf_mn10300_hash_entry (h)->tls_type)
5346 {
5347 case GOT_TLS_GD:
5348 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
5349 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset + 4);
5350 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_TLS_DTPMOD);
5351 rel.r_addend = 0;
5352 bfd_elf32_swap_reloca_out (output_bfd, & rel,
5353 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
5354 + srel->reloc_count));
5355 ++ srel->reloc_count;
5356 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_TLS_DTPOFF);
5357 rel.r_offset += 4;
5358 rel.r_addend = 0;
5359 break;
5360
5361 case GOT_TLS_IE:
5362 /* We originally stored the addend in the GOT, but at this
5363 point, we want to move it to the reloc instead as that's
5364 where the dynamic linker wants it. */
5365 rel.r_addend = bfd_get_32 (output_bfd, sgot->contents + h->got.offset);
5366 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
5367 if (h->dynindx == -1)
5368 rel.r_info = ELF32_R_INFO (0, R_MN10300_TLS_TPOFF);
5369 else
5370 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_TLS_TPOFF);
5371 break;
5372
5373 default:
5374 /* If this is a -Bsymbolic link, and the symbol is defined
5375 locally, we just want to emit a RELATIVE reloc. Likewise if
5376 the symbol was forced to be local because of a version file.
5377 The entry in the global offset table will already have been
5378 initialized in the relocate_section function. */
5379 if (info->shared
5380 && (info->symbolic || h->dynindx == -1)
5381 && h->def_regular)
5382 {
5383 rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
5384 rel.r_addend = (h->root.u.def.value
5385 + h->root.u.def.section->output_section->vma
5386 + h->root.u.def.section->output_offset);
5387 }
5388 else
5389 {
5390 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
5391 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
5392 rel.r_addend = 0;
5393 }
5394 }
5395
5396 if (ELF32_R_TYPE (rel.r_info) != R_MN10300_NONE)
5397 {
5398 bfd_elf32_swap_reloca_out (output_bfd, &rel,
5399 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
5400 + srel->reloc_count));
5401 ++ srel->reloc_count;
5402 }
5403 }
5404
5405 if (h->needs_copy)
5406 {
5407 asection * s;
5408 Elf_Internal_Rela rel;
5409
5410 /* This symbol needs a copy reloc. Set it up. */
5411 BFD_ASSERT (h->dynindx != -1
5412 && (h->root.type == bfd_link_hash_defined
5413 || h->root.type == bfd_link_hash_defweak));
5414
5415 s = bfd_get_section_by_name (h->root.u.def.section->owner,
5416 ".rela.bss");
5417 BFD_ASSERT (s != NULL);
5418
5419 rel.r_offset = (h->root.u.def.value
5420 + h->root.u.def.section->output_section->vma
5421 + h->root.u.def.section->output_offset);
5422 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
5423 rel.r_addend = 0;
5424 bfd_elf32_swap_reloca_out (output_bfd, & rel,
5425 (bfd_byte *) ((Elf32_External_Rela *) s->contents
5426 + s->reloc_count));
5427 ++ s->reloc_count;
5428 }
5429
5430 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
5431 if (streq (h->root.root.string, "_DYNAMIC")
5432 || h == elf_hash_table (info)->hgot)
5433 sym->st_shndx = SHN_ABS;
5434
5435 return TRUE;
5436 }
5437
5438 /* Finish up the dynamic sections. */
5439
5440 static bfd_boolean
5441 _bfd_mn10300_elf_finish_dynamic_sections (bfd * output_bfd,
5442 struct bfd_link_info * info)
5443 {
5444 bfd * dynobj;
5445 asection * sgot;
5446 asection * sdyn;
5447
5448 dynobj = elf_hash_table (info)->dynobj;
5449
5450 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
5451 BFD_ASSERT (sgot != NULL);
5452 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5453
5454 if (elf_hash_table (info)->dynamic_sections_created)
5455 {
5456 asection * splt;
5457 Elf32_External_Dyn * dyncon;
5458 Elf32_External_Dyn * dynconend;
5459
5460 BFD_ASSERT (sdyn != NULL);
5461
5462 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5463 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5464
5465 for (; dyncon < dynconend; dyncon++)
5466 {
5467 Elf_Internal_Dyn dyn;
5468 const char * name;
5469 asection * s;
5470
5471 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5472
5473 switch (dyn.d_tag)
5474 {
5475 default:
5476 break;
5477
5478 case DT_PLTGOT:
5479 name = ".got";
5480 goto get_vma;
5481
5482 case DT_JMPREL:
5483 name = ".rela.plt";
5484 get_vma:
5485 s = bfd_get_section_by_name (output_bfd, name);
5486 BFD_ASSERT (s != NULL);
5487 dyn.d_un.d_ptr = s->vma;
5488 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5489 break;
5490
5491 case DT_PLTRELSZ:
5492 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
5493 BFD_ASSERT (s != NULL);
5494 dyn.d_un.d_val = s->size;
5495 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5496 break;
5497
5498 case DT_RELASZ:
5499 /* My reading of the SVR4 ABI indicates that the
5500 procedure linkage table relocs (DT_JMPREL) should be
5501 included in the overall relocs (DT_RELA). This is
5502 what Solaris does. However, UnixWare can not handle
5503 that case. Therefore, we override the DT_RELASZ entry
5504 here to make it not include the JMPREL relocs. Since
5505 the linker script arranges for .rela.plt to follow all
5506 other relocation sections, we don't have to worry
5507 about changing the DT_RELA entry. */
5508 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
5509 if (s != NULL)
5510 dyn.d_un.d_val -= s->size;
5511 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5512 break;
5513 }
5514 }
5515
5516 /* Fill in the first entry in the procedure linkage table. */
5517 splt = bfd_get_section_by_name (dynobj, ".plt");
5518 if (splt && splt->size > 0)
5519 {
5520 if (info->shared)
5521 {
5522 memcpy (splt->contents, elf_mn10300_pic_plt_entry,
5523 elf_mn10300_sizeof_plt (info));
5524 }
5525 else
5526 {
5527 memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
5528 bfd_put_32 (output_bfd,
5529 sgot->output_section->vma + sgot->output_offset + 4,
5530 splt->contents + elf_mn10300_plt0_gotid_offset (info));
5531 bfd_put_32 (output_bfd,
5532 sgot->output_section->vma + sgot->output_offset + 8,
5533 splt->contents + elf_mn10300_plt0_linker_offset (info));
5534 }
5535
5536 /* UnixWare sets the entsize of .plt to 4, although that doesn't
5537 really seem like the right value. */
5538 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
5539
5540 /* UnixWare sets the entsize of .plt to 4, but this is incorrect
5541 as it means that the size of the PLT0 section (15 bytes) is not
5542 a multiple of the sh_entsize. Some ELF tools flag this as an
5543 error. We could pad PLT0 to 16 bytes, but that would introduce
5544 compatibilty issues with previous toolchains, so instead we
5545 just set the entry size to 1. */
5546 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 1;
5547 }
5548 }
5549
5550 /* Fill in the first three entries in the global offset table. */
5551 if (sgot->size > 0)
5552 {
5553 if (sdyn == NULL)
5554 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
5555 else
5556 bfd_put_32 (output_bfd,
5557 sdyn->output_section->vma + sdyn->output_offset,
5558 sgot->contents);
5559 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
5560 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
5561 }
5562
5563 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
5564
5565 return TRUE;
5566 }
5567
5568 /* Classify relocation types, such that combreloc can sort them
5569 properly. */
5570
5571 static enum elf_reloc_type_class
5572 _bfd_mn10300_elf_reloc_type_class (const Elf_Internal_Rela *rela)
5573 {
5574 switch ((int) ELF32_R_TYPE (rela->r_info))
5575 {
5576 case R_MN10300_RELATIVE: return reloc_class_relative;
5577 case R_MN10300_JMP_SLOT: return reloc_class_plt;
5578 case R_MN10300_COPY: return reloc_class_copy;
5579 default: return reloc_class_normal;
5580 }
5581 }
5582
5583 /* Allocate space for an MN10300 extension to the bfd elf data structure. */
5584
5585 static bfd_boolean
5586 mn10300_elf_mkobject (bfd *abfd)
5587 {
5588 return bfd_elf_allocate_object (abfd, sizeof (struct elf_mn10300_obj_tdata),
5589 MN10300_ELF_DATA);
5590 }
5591
5592 #define bfd_elf32_mkobject mn10300_elf_mkobject
5593
5594 #ifndef ELF_ARCH
5595 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
5596 #define TARGET_LITTLE_NAME "elf32-mn10300"
5597 #define ELF_ARCH bfd_arch_mn10300
5598 #define ELF_TARGET_ID MN10300_ELF_DATA
5599 #define ELF_MACHINE_CODE EM_MN10300
5600 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
5601 #define ELF_MAXPAGESIZE 0x1000
5602 #endif
5603
5604 #define elf_info_to_howto mn10300_info_to_howto
5605 #define elf_info_to_howto_rel 0
5606 #define elf_backend_can_gc_sections 1
5607 #define elf_backend_rela_normal 1
5608 #define elf_backend_check_relocs mn10300_elf_check_relocs
5609 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
5610 #define elf_backend_relocate_section mn10300_elf_relocate_section
5611 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
5612 #define bfd_elf32_bfd_get_relocated_section_contents \
5613 mn10300_elf_get_relocated_section_contents
5614 #define bfd_elf32_bfd_link_hash_table_create \
5615 elf32_mn10300_link_hash_table_create
5616 #define bfd_elf32_bfd_link_hash_table_free \
5617 elf32_mn10300_link_hash_table_free
5618
5619 #ifndef elf_symbol_leading_char
5620 #define elf_symbol_leading_char '_'
5621 #endif
5622
5623 /* So we can set bits in e_flags. */
5624 #define elf_backend_final_write_processing \
5625 _bfd_mn10300_elf_final_write_processing
5626 #define elf_backend_object_p _bfd_mn10300_elf_object_p
5627
5628 #define bfd_elf32_bfd_merge_private_bfd_data \
5629 _bfd_mn10300_elf_merge_private_bfd_data
5630
5631 #define elf_backend_can_gc_sections 1
5632 #define elf_backend_create_dynamic_sections \
5633 _bfd_mn10300_elf_create_dynamic_sections
5634 #define elf_backend_adjust_dynamic_symbol \
5635 _bfd_mn10300_elf_adjust_dynamic_symbol
5636 #define elf_backend_size_dynamic_sections \
5637 _bfd_mn10300_elf_size_dynamic_sections
5638 #define elf_backend_omit_section_dynsym \
5639 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
5640 #define elf_backend_finish_dynamic_symbol \
5641 _bfd_mn10300_elf_finish_dynamic_symbol
5642 #define elf_backend_finish_dynamic_sections \
5643 _bfd_mn10300_elf_finish_dynamic_sections
5644 #define elf_backend_copy_indirect_symbol \
5645 _bfd_mn10300_copy_indirect_symbol
5646 #define elf_backend_reloc_type_class \
5647 _bfd_mn10300_elf_reloc_type_class
5648
5649 #define elf_backend_want_got_plt 1
5650 #define elf_backend_plt_readonly 1
5651 #define elf_backend_want_plt_sym 0
5652 #define elf_backend_got_header_size 12
5653
5654 #include "elf32-target.h"