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