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