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