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