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