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