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