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