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