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