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