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