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