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