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