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