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