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