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