elf-m10300.c revision 1.1.1.6 1 1.1 christos /* Matsushita 10300 specific support for 32-bit ELF
2 1.1.1.6 christos Copyright (C) 1996-2016 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.6 christos if (bfd_link_pic (info))
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.1.6 christos if (bfd_link_relocatable (info))
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.6 christos if (bfd_link_pic (info))
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.1.6 christos && (h != NULL || bfd_link_pic (info)))
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.1.6 christos if (bfd_link_pic (info))
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.1.6 christos if (bfd_link_pic (info)
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.1.6 christos if (bfd_link_pic (info)
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.6 christos if (bfd_link_pic (info)
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.1.6 christos if (bfd_link_pic (info)
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.1.6 christos if (bfd_link_pic (info))
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.1.6 christos being local. */
2095 1.1 christos && !bfd_link_executable (info)
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.6 christos
2109 1.1.1.2 christos else if (!bfd_link_relocatable (info) && 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.1.6 christos
2126 1.1 christos if (bfd_link_relocatable (info))
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.1.6 christos case bfd_reloc_overflow:
2155 1.1.1.6 christos (*info->callbacks->reloc_overflow)
2156 1.1.1.6 christos (info, (h ? &h->root.root : NULL), name, howto->name,
2157 1.1 christos (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
2158 1.1 christos break;
2159 1.1 christos
2160 1.1.1.6 christos case bfd_reloc_undefined:
2161 1.1.1.6 christos (*info->callbacks->undefined_symbol)
2162 1.1 christos (info, name, input_bfd, input_section, rel->r_offset, TRUE);
2163 1.1 christos break;
2164 1.1 christos
2165 1.1 christos case bfd_reloc_outofrange:
2166 1.1 christos msg = _("internal error: out of range error");
2167 1.1 christos goto common_error;
2168 1.1 christos
2169 1.1 christos case bfd_reloc_notsupported:
2170 1.1 christos msg = _("internal error: unsupported relocation error");
2171 1.1 christos goto common_error;
2172 1.1 christos
2173 1.1 christos case bfd_reloc_dangerous:
2174 1.1 christos if (r_type == R_MN10300_PCREL32)
2175 1.1 christos msg = _("error: inappropriate relocation type for shared"
2176 1.1.1.2 christos " library (did you forget -fpic?)");
2177 1.1.1.2 christos else if (r_type == R_MN10300_GOT32)
2178 1.1.1.2 christos msg = _("%B: taking the address of protected function"
2179 1.1 christos " '%s' cannot be done when making a shared library");
2180 1.1 christos else
2181 1.1 christos msg = _("internal error: suspicious relocation type used"
2182 1.1 christos " in shared library");
2183 1.1 christos goto common_error;
2184 1.1 christos
2185 1.1 christos default:
2186 1.1 christos msg = _("internal error: unknown error");
2187 1.1 christos /* Fall through. */
2188 1.1 christos
2189 1.1.1.2 christos common_error:
2190 1.1.1.2 christos _bfd_error_handler (msg, input_bfd, name);
2191 1.1.1.2 christos bfd_set_error (bfd_error_bad_value);
2192 1.1 christos return FALSE;
2193 1.1 christos }
2194 1.1 christos }
2195 1.1 christos }
2196 1.1 christos
2197 1.1 christos return TRUE;
2198 1.1 christos }
2199 1.1 christos
2200 1.1 christos /* Finish initializing one hash table entry. */
2201 1.1 christos
2202 1.1 christos static bfd_boolean
2203 1.1 christos elf32_mn10300_finish_hash_table_entry (struct bfd_hash_entry *gen_entry,
2204 1.1 christos void * in_args)
2205 1.1 christos {
2206 1.1 christos struct elf32_mn10300_link_hash_entry *entry;
2207 1.1 christos struct bfd_link_info *link_info = (struct bfd_link_info *) in_args;
2208 1.1 christos unsigned int byte_count = 0;
2209 1.1 christos
2210 1.1 christos entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
2211 1.1 christos
2212 1.1 christos /* If we already know we want to convert "call" to "calls" for calls
2213 1.1 christos to this symbol, then return now. */
2214 1.1 christos if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
2215 1.1 christos return TRUE;
2216 1.1 christos
2217 1.1 christos /* If there are no named calls to this symbol, or there's nothing we
2218 1.1 christos can move from the function itself into the "call" instruction,
2219 1.1 christos then note that all "call" instructions should be converted into
2220 1.1 christos "calls" instructions and return. If a symbol is available for
2221 1.1 christos dynamic symbol resolution (overridable or overriding), avoid
2222 1.1 christos custom calling conventions. */
2223 1.1 christos if (entry->direct_calls == 0
2224 1.1 christos || (entry->stack_size == 0 && entry->movm_args == 0)
2225 1.1 christos || (elf_hash_table (link_info)->dynamic_sections_created
2226 1.1 christos && ELF_ST_VISIBILITY (entry->root.other) != STV_INTERNAL
2227 1.1 christos && ELF_ST_VISIBILITY (entry->root.other) != STV_HIDDEN))
2228 1.1 christos {
2229 1.1 christos /* Make a note that we should convert "call" instructions to "calls"
2230 1.1 christos instructions for calls to this symbol. */
2231 1.1 christos entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
2232 1.1 christos return TRUE;
2233 1.1 christos }
2234 1.1 christos
2235 1.1 christos /* We may be able to move some instructions from the function itself into
2236 1.1 christos the "call" instruction. Count how many bytes we might be able to
2237 1.1 christos eliminate in the function itself. */
2238 1.1 christos
2239 1.1 christos /* A movm instruction is two bytes. */
2240 1.1 christos if (entry->movm_args)
2241 1.1 christos byte_count += 2;
2242 1.1 christos
2243 1.1 christos /* Count the insn to allocate stack space too. */
2244 1.1 christos if (entry->stack_size > 0)
2245 1.1 christos {
2246 1.1 christos if (entry->stack_size <= 128)
2247 1.1 christos byte_count += 3;
2248 1.1 christos else
2249 1.1 christos byte_count += 4;
2250 1.1 christos }
2251 1.1 christos
2252 1.1 christos /* If using "call" will result in larger code, then turn all
2253 1.1 christos the associated "call" instructions into "calls" instructions. */
2254 1.1 christos if (byte_count < entry->direct_calls)
2255 1.1 christos entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
2256 1.1 christos
2257 1.1 christos /* This routine never fails. */
2258 1.1 christos return TRUE;
2259 1.1 christos }
2260 1.1 christos
2261 1.1 christos /* Used to count hash table entries. */
2262 1.1 christos
2263 1.1 christos static bfd_boolean
2264 1.1 christos elf32_mn10300_count_hash_table_entries (struct bfd_hash_entry *gen_entry ATTRIBUTE_UNUSED,
2265 1.1 christos void * in_args)
2266 1.1 christos {
2267 1.1 christos int *count = (int *) in_args;
2268 1.1 christos
2269 1.1 christos (*count) ++;
2270 1.1 christos return TRUE;
2271 1.1 christos }
2272 1.1 christos
2273 1.1 christos /* Used to enumerate hash table entries into a linear array. */
2274 1.1 christos
2275 1.1 christos static bfd_boolean
2276 1.1 christos elf32_mn10300_list_hash_table_entries (struct bfd_hash_entry *gen_entry,
2277 1.1 christos void * in_args)
2278 1.1 christos {
2279 1.1 christos struct bfd_hash_entry ***ptr = (struct bfd_hash_entry ***) in_args;
2280 1.1 christos
2281 1.1 christos **ptr = gen_entry;
2282 1.1 christos (*ptr) ++;
2283 1.1 christos return TRUE;
2284 1.1 christos }
2285 1.1 christos
2286 1.1 christos /* Used to sort the array created by the above. */
2287 1.1 christos
2288 1.1 christos static int
2289 1.1 christos sort_by_value (const void *va, const void *vb)
2290 1.1 christos {
2291 1.1 christos struct elf32_mn10300_link_hash_entry *a
2292 1.1 christos = *(struct elf32_mn10300_link_hash_entry **) va;
2293 1.1 christos struct elf32_mn10300_link_hash_entry *b
2294 1.1 christos = *(struct elf32_mn10300_link_hash_entry **) vb;
2295 1.1 christos
2296 1.1 christos return a->value - b->value;
2297 1.1 christos }
2298 1.1 christos
2299 1.1 christos /* Compute the stack size and movm arguments for the function
2300 1.1 christos referred to by HASH at address ADDR in section with
2301 1.1 christos contents CONTENTS, store the information in the hash table. */
2302 1.1 christos
2303 1.1 christos static void
2304 1.1 christos compute_function_info (bfd *abfd,
2305 1.1 christos struct elf32_mn10300_link_hash_entry *hash,
2306 1.1 christos bfd_vma addr,
2307 1.1 christos unsigned char *contents)
2308 1.1 christos {
2309 1.1 christos unsigned char byte1, byte2;
2310 1.1 christos /* We only care about a very small subset of the possible prologue
2311 1.1 christos sequences here. Basically we look for:
2312 1.1 christos
2313 1.1 christos movm [d2,d3,a2,a3],sp (optional)
2314 1.1 christos add <size>,sp (optional, and only for sizes which fit in an unsigned
2315 1.1 christos 8 bit number)
2316 1.1 christos
2317 1.1 christos If we find anything else, we quit. */
2318 1.1 christos
2319 1.1 christos /* Look for movm [regs],sp. */
2320 1.1 christos byte1 = bfd_get_8 (abfd, contents + addr);
2321 1.1 christos byte2 = bfd_get_8 (abfd, contents + addr + 1);
2322 1.1 christos
2323 1.1 christos if (byte1 == 0xcf)
2324 1.1 christos {
2325 1.1 christos hash->movm_args = byte2;
2326 1.1 christos addr += 2;
2327 1.1 christos byte1 = bfd_get_8 (abfd, contents + addr);
2328 1.1 christos byte2 = bfd_get_8 (abfd, contents + addr + 1);
2329 1.1 christos }
2330 1.1 christos
2331 1.1 christos /* Now figure out how much stack space will be allocated by the movm
2332 1.1 christos instruction. We need this kept separate from the function's normal
2333 1.1 christos stack space. */
2334 1.1 christos if (hash->movm_args)
2335 1.1 christos {
2336 1.1 christos /* Space for d2. */
2337 1.1 christos if (hash->movm_args & 0x80)
2338 1.1 christos hash->movm_stack_size += 4;
2339 1.1 christos
2340 1.1 christos /* Space for d3. */
2341 1.1 christos if (hash->movm_args & 0x40)
2342 1.1 christos hash->movm_stack_size += 4;
2343 1.1 christos
2344 1.1 christos /* Space for a2. */
2345 1.1 christos if (hash->movm_args & 0x20)
2346 1.1 christos hash->movm_stack_size += 4;
2347 1.1 christos
2348 1.1 christos /* Space for a3. */
2349 1.1 christos if (hash->movm_args & 0x10)
2350 1.1 christos hash->movm_stack_size += 4;
2351 1.1 christos
2352 1.1 christos /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
2353 1.1 christos if (hash->movm_args & 0x08)
2354 1.1 christos hash->movm_stack_size += 8 * 4;
2355 1.1 christos
2356 1.1 christos if (bfd_get_mach (abfd) == bfd_mach_am33
2357 1.1 christos || bfd_get_mach (abfd) == bfd_mach_am33_2)
2358 1.1 christos {
2359 1.1 christos /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
2360 1.1 christos if (hash->movm_args & 0x1)
2361 1.1 christos hash->movm_stack_size += 6 * 4;
2362 1.1 christos
2363 1.1 christos /* exreg1 space. e4, e5, e6, e7 */
2364 1.1 christos if (hash->movm_args & 0x2)
2365 1.1 christos hash->movm_stack_size += 4 * 4;
2366 1.1 christos
2367 1.1 christos /* exreg0 space. e2, e3 */
2368 1.1 christos if (hash->movm_args & 0x4)
2369 1.1 christos hash->movm_stack_size += 2 * 4;
2370 1.1 christos }
2371 1.1 christos }
2372 1.1 christos
2373 1.1 christos /* Now look for the two stack adjustment variants. */
2374 1.1 christos if (byte1 == 0xf8 && byte2 == 0xfe)
2375 1.1 christos {
2376 1.1 christos int temp = bfd_get_8 (abfd, contents + addr + 2);
2377 1.1 christos temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
2378 1.1 christos
2379 1.1 christos hash->stack_size = -temp;
2380 1.1 christos }
2381 1.1 christos else if (byte1 == 0xfa && byte2 == 0xfe)
2382 1.1 christos {
2383 1.1 christos int temp = bfd_get_16 (abfd, contents + addr + 2);
2384 1.1 christos temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
2385 1.1 christos temp = -temp;
2386 1.1 christos
2387 1.1 christos if (temp < 255)
2388 1.1 christos hash->stack_size = temp;
2389 1.1 christos }
2390 1.1 christos
2391 1.1 christos /* If the total stack to be allocated by the call instruction is more
2392 1.1 christos than 255 bytes, then we can't remove the stack adjustment by using
2393 1.1 christos "call" (we might still be able to remove the "movm" instruction. */
2394 1.1 christos if (hash->stack_size + hash->movm_stack_size > 255)
2395 1.1 christos hash->stack_size = 0;
2396 1.1 christos }
2397 1.1 christos
2398 1.1 christos /* Delete some bytes from a section while relaxing. */
2399 1.1 christos
2400 1.1 christos static bfd_boolean
2401 1.1 christos mn10300_elf_relax_delete_bytes (bfd *abfd,
2402 1.1 christos asection *sec,
2403 1.1 christos bfd_vma addr,
2404 1.1 christos int count)
2405 1.1 christos {
2406 1.1 christos Elf_Internal_Shdr *symtab_hdr;
2407 1.1 christos unsigned int sec_shndx;
2408 1.1 christos bfd_byte *contents;
2409 1.1 christos Elf_Internal_Rela *irel, *irelend;
2410 1.1 christos Elf_Internal_Rela *irelalign;
2411 1.1 christos bfd_vma toaddr;
2412 1.1 christos Elf_Internal_Sym *isym, *isymend;
2413 1.1 christos struct elf_link_hash_entry **sym_hashes;
2414 1.1 christos struct elf_link_hash_entry **end_hashes;
2415 1.1 christos unsigned int symcount;
2416 1.1 christos
2417 1.1 christos sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2418 1.1 christos
2419 1.1 christos contents = elf_section_data (sec)->this_hdr.contents;
2420 1.1 christos
2421 1.1 christos irelalign = NULL;
2422 1.1 christos toaddr = sec->size;
2423 1.1 christos
2424 1.1 christos irel = elf_section_data (sec)->relocs;
2425 1.1 christos irelend = irel + sec->reloc_count;
2426 1.1 christos
2427 1.1 christos if (sec->reloc_count > 0)
2428 1.1 christos {
2429 1.1 christos /* If there is an align reloc at the end of the section ignore it.
2430 1.1 christos GAS creates these relocs for reasons of its own, and they just
2431 1.1 christos serve to keep the section artifically inflated. */
2432 1.1 christos if (ELF32_R_TYPE ((irelend - 1)->r_info) == (int) R_MN10300_ALIGN)
2433 1.1.1.2 christos --irelend;
2434 1.1 christos
2435 1.1 christos /* The deletion must stop at the next ALIGN reloc for an aligment
2436 1.1 christos power larger than, or not a multiple of, the number of bytes we
2437 1.1 christos are deleting. */
2438 1.1 christos for (; irel < irelend; irel++)
2439 1.1 christos {
2440 1.1 christos int alignment = 1 << irel->r_addend;
2441 1.1 christos
2442 1.1 christos if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
2443 1.1 christos && irel->r_offset > addr
2444 1.1 christos && irel->r_offset < toaddr
2445 1.1 christos && (count < alignment
2446 1.1 christos || alignment % count != 0))
2447 1.1 christos {
2448 1.1 christos irelalign = irel;
2449 1.1 christos toaddr = irel->r_offset;
2450 1.1 christos break;
2451 1.1 christos }
2452 1.1 christos }
2453 1.1 christos }
2454 1.1 christos
2455 1.1 christos /* Actually delete the bytes. */
2456 1.1 christos memmove (contents + addr, contents + addr + count,
2457 1.1 christos (size_t) (toaddr - addr - count));
2458 1.1 christos
2459 1.1 christos /* Adjust the section's size if we are shrinking it, or else
2460 1.1 christos pad the bytes between the end of the shrunken region and
2461 1.1 christos the start of the next region with NOP codes. */
2462 1.1 christos if (irelalign == NULL)
2463 1.1 christos {
2464 1.1 christos sec->size -= count;
2465 1.1 christos /* Include symbols at the end of the section, but
2466 1.1 christos not at the end of a sub-region of the section. */
2467 1.1 christos toaddr ++;
2468 1.1 christos }
2469 1.1 christos else
2470 1.1 christos {
2471 1.1 christos int i;
2472 1.1 christos
2473 1.1 christos #define NOP_OPCODE 0xcb
2474 1.1 christos
2475 1.1 christos for (i = 0; i < count; i ++)
2476 1.1 christos bfd_put_8 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
2477 1.1 christos }
2478 1.1 christos
2479 1.1 christos /* Adjust all the relocs. */
2480 1.1 christos for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2481 1.1 christos {
2482 1.1 christos /* Get the new reloc address. */
2483 1.1 christos if ((irel->r_offset > addr
2484 1.1 christos && irel->r_offset < toaddr)
2485 1.1 christos || (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
2486 1.1 christos && irel->r_offset == toaddr))
2487 1.1 christos irel->r_offset -= count;
2488 1.1 christos }
2489 1.1 christos
2490 1.1 christos /* Adjust the local symbols in the section, reducing their value
2491 1.1 christos by the number of bytes deleted. Note - symbols within the deleted
2492 1.1 christos region are moved to the address of the start of the region, which
2493 1.1 christos actually means that they will address the byte beyond the end of
2494 1.1 christos the region once the deletion has been completed. */
2495 1.1 christos symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2496 1.1 christos isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2497 1.1 christos for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
2498 1.1 christos {
2499 1.1 christos if (isym->st_shndx == sec_shndx
2500 1.1 christos && isym->st_value > addr
2501 1.1 christos && isym->st_value < toaddr)
2502 1.1 christos {
2503 1.1 christos if (isym->st_value < addr + count)
2504 1.1 christos isym->st_value = addr;
2505 1.1 christos else
2506 1.1 christos isym->st_value -= count;
2507 1.1 christos }
2508 1.1 christos /* Adjust the function symbol's size as well. */
2509 1.1 christos else if (isym->st_shndx == sec_shndx
2510 1.1 christos && ELF_ST_TYPE (isym->st_info) == STT_FUNC
2511 1.1 christos && isym->st_value + isym->st_size > addr
2512 1.1 christos && isym->st_value + isym->st_size < toaddr)
2513 1.1 christos isym->st_size -= count;
2514 1.1 christos }
2515 1.1 christos
2516 1.1 christos /* Now adjust the global symbols defined in this section. */
2517 1.1 christos symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2518 1.1 christos - symtab_hdr->sh_info);
2519 1.1 christos sym_hashes = elf_sym_hashes (abfd);
2520 1.1 christos end_hashes = sym_hashes + symcount;
2521 1.1 christos for (; sym_hashes < end_hashes; sym_hashes++)
2522 1.1 christos {
2523 1.1 christos struct elf_link_hash_entry *sym_hash = *sym_hashes;
2524 1.1 christos
2525 1.1 christos if ((sym_hash->root.type == bfd_link_hash_defined
2526 1.1 christos || sym_hash->root.type == bfd_link_hash_defweak)
2527 1.1 christos && sym_hash->root.u.def.section == sec
2528 1.1 christos && sym_hash->root.u.def.value > addr
2529 1.1 christos && sym_hash->root.u.def.value < toaddr)
2530 1.1 christos {
2531 1.1 christos if (sym_hash->root.u.def.value < addr + count)
2532 1.1 christos sym_hash->root.u.def.value = addr;
2533 1.1 christos else
2534 1.1 christos sym_hash->root.u.def.value -= count;
2535 1.1 christos }
2536 1.1 christos /* Adjust the function symbol's size as well. */
2537 1.1 christos else if (sym_hash->root.type == bfd_link_hash_defined
2538 1.1 christos && sym_hash->root.u.def.section == sec
2539 1.1 christos && sym_hash->type == STT_FUNC
2540 1.1 christos && sym_hash->root.u.def.value + sym_hash->size > addr
2541 1.1 christos && sym_hash->root.u.def.value + sym_hash->size < toaddr)
2542 1.1 christos sym_hash->size -= count;
2543 1.1 christos }
2544 1.1 christos
2545 1.1 christos /* See if we can move the ALIGN reloc forward.
2546 1.1 christos We have adjusted r_offset for it already. */
2547 1.1 christos if (irelalign != NULL)
2548 1.1 christos {
2549 1.1 christos bfd_vma alignto, alignaddr;
2550 1.1 christos
2551 1.1 christos if ((int) irelalign->r_addend > 0)
2552 1.1 christos {
2553 1.1 christos /* This is the old address. */
2554 1.1 christos alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
2555 1.1 christos /* This is where the align points to now. */
2556 1.1 christos alignaddr = BFD_ALIGN (irelalign->r_offset,
2557 1.1 christos 1 << irelalign->r_addend);
2558 1.1 christos if (alignaddr < alignto)
2559 1.1 christos /* Tail recursion. */
2560 1.1 christos return mn10300_elf_relax_delete_bytes (abfd, sec, alignaddr,
2561 1.1 christos (int) (alignto - alignaddr));
2562 1.1 christos }
2563 1.1 christos }
2564 1.1 christos
2565 1.1 christos return TRUE;
2566 1.1 christos }
2567 1.1 christos
2568 1.1 christos /* Return TRUE if a symbol exists at the given address, else return
2569 1.1 christos FALSE. */
2570 1.1 christos
2571 1.1 christos static bfd_boolean
2572 1.1 christos mn10300_elf_symbol_address_p (bfd *abfd,
2573 1.1 christos asection *sec,
2574 1.1 christos Elf_Internal_Sym *isym,
2575 1.1 christos bfd_vma addr)
2576 1.1 christos {
2577 1.1 christos Elf_Internal_Shdr *symtab_hdr;
2578 1.1 christos unsigned int sec_shndx;
2579 1.1 christos Elf_Internal_Sym *isymend;
2580 1.1 christos struct elf_link_hash_entry **sym_hashes;
2581 1.1 christos struct elf_link_hash_entry **end_hashes;
2582 1.1 christos unsigned int symcount;
2583 1.1 christos
2584 1.1 christos sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2585 1.1 christos
2586 1.1 christos /* Examine all the symbols. */
2587 1.1 christos symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2588 1.1 christos for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
2589 1.1 christos if (isym->st_shndx == sec_shndx
2590 1.1 christos && isym->st_value == addr)
2591 1.1 christos return TRUE;
2592 1.1 christos
2593 1.1 christos symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2594 1.1 christos - symtab_hdr->sh_info);
2595 1.1 christos sym_hashes = elf_sym_hashes (abfd);
2596 1.1 christos end_hashes = sym_hashes + symcount;
2597 1.1 christos for (; sym_hashes < end_hashes; sym_hashes++)
2598 1.1 christos {
2599 1.1 christos struct elf_link_hash_entry *sym_hash = *sym_hashes;
2600 1.1 christos
2601 1.1 christos if ((sym_hash->root.type == bfd_link_hash_defined
2602 1.1 christos || sym_hash->root.type == bfd_link_hash_defweak)
2603 1.1 christos && sym_hash->root.u.def.section == sec
2604 1.1 christos && sym_hash->root.u.def.value == addr)
2605 1.1 christos return TRUE;
2606 1.1 christos }
2607 1.1 christos
2608 1.1 christos return FALSE;
2609 1.1 christos }
2610 1.1 christos
2611 1.1 christos /* This function handles relaxing for the mn10300.
2612 1.1 christos
2613 1.1 christos There are quite a few relaxing opportunities available on the mn10300:
2614 1.1 christos
2615 1.1 christos * calls:32 -> calls:16 2 bytes
2616 1.1 christos * call:32 -> call:16 2 bytes
2617 1.1 christos
2618 1.1 christos * call:32 -> calls:32 1 byte
2619 1.1 christos * call:16 -> calls:16 1 byte
2620 1.1 christos * These are done anytime using "calls" would result
2621 1.1 christos in smaller code, or when necessary to preserve the
2622 1.1 christos meaning of the program.
2623 1.1 christos
2624 1.1 christos * call:32 varies
2625 1.1 christos * call:16
2626 1.1 christos * In some circumstances we can move instructions
2627 1.1 christos from a function prologue into a "call" instruction.
2628 1.1 christos This is only done if the resulting code is no larger
2629 1.1 christos than the original code.
2630 1.1 christos
2631 1.1 christos * jmp:32 -> jmp:16 2 bytes
2632 1.1 christos * jmp:16 -> bra:8 1 byte
2633 1.1 christos
2634 1.1 christos * If the previous instruction is a conditional branch
2635 1.1 christos around the jump/bra, we may be able to reverse its condition
2636 1.1 christos and change its target to the jump's target. The jump/bra
2637 1.1 christos can then be deleted. 2 bytes
2638 1.1 christos
2639 1.1 christos * mov abs32 -> mov abs16 1 or 2 bytes
2640 1.1 christos
2641 1.1 christos * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
2642 1.1 christos - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
2643 1.1 christos
2644 1.1 christos * Most instructions which accept d32 can relax to d16 1 or 2 bytes
2645 1.1 christos - Most instructions which accept d16 can relax to d8 1 or 2 bytes
2646 1.1 christos
2647 1.1 christos We don't handle imm16->imm8 or d16->d8 as they're very rare
2648 1.1 christos and somewhat more difficult to support. */
2649 1.1 christos
2650 1.1 christos static bfd_boolean
2651 1.1 christos mn10300_elf_relax_section (bfd *abfd,
2652 1.1 christos asection *sec,
2653 1.1 christos struct bfd_link_info *link_info,
2654 1.1 christos bfd_boolean *again)
2655 1.1 christos {
2656 1.1 christos Elf_Internal_Shdr *symtab_hdr;
2657 1.1 christos Elf_Internal_Rela *internal_relocs = NULL;
2658 1.1 christos Elf_Internal_Rela *irel, *irelend;
2659 1.1 christos bfd_byte *contents = NULL;
2660 1.1 christos Elf_Internal_Sym *isymbuf = NULL;
2661 1.1 christos struct elf32_mn10300_link_hash_table *hash_table;
2662 1.1 christos asection *section = sec;
2663 1.1 christos bfd_vma align_gap_adjustment;
2664 1.1.1.6 christos
2665 1.1 christos if (bfd_link_relocatable (link_info))
2666 1.1 christos (*link_info->callbacks->einfo)
2667 1.1 christos (_("%P%F: --relax and -r may not be used together\n"));
2668 1.1 christos
2669 1.1 christos /* Assume nothing changes. */
2670 1.1 christos *again = FALSE;
2671 1.1 christos
2672 1.1 christos /* We need a pointer to the mn10300 specific hash table. */
2673 1.1 christos hash_table = elf32_mn10300_hash_table (link_info);
2674 1.1 christos if (hash_table == NULL)
2675 1.1 christos return FALSE;
2676 1.1 christos
2677 1.1 christos /* Initialize fields in each hash table entry the first time through. */
2678 1.1 christos if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
2679 1.1 christos {
2680 1.1 christos bfd *input_bfd;
2681 1.1 christos
2682 1.1 christos /* Iterate over all the input bfds. */
2683 1.1 christos for (input_bfd = link_info->input_bfds;
2684 1.1.1.4 christos input_bfd != NULL;
2685 1.1 christos input_bfd = input_bfd->link.next)
2686 1.1 christos {
2687 1.1 christos /* We're going to need all the symbols for each bfd. */
2688 1.1 christos symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2689 1.1 christos if (symtab_hdr->sh_info != 0)
2690 1.1 christos {
2691 1.1 christos isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2692 1.1 christos if (isymbuf == NULL)
2693 1.1 christos isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2694 1.1 christos symtab_hdr->sh_info, 0,
2695 1.1 christos NULL, NULL, NULL);
2696 1.1 christos if (isymbuf == NULL)
2697 1.1 christos goto error_return;
2698 1.1 christos }
2699 1.1 christos
2700 1.1 christos /* Iterate over each section in this bfd. */
2701 1.1 christos for (section = input_bfd->sections;
2702 1.1 christos section != NULL;
2703 1.1 christos section = section->next)
2704 1.1 christos {
2705 1.1 christos struct elf32_mn10300_link_hash_entry *hash;
2706 1.1 christos asection *sym_sec = NULL;
2707 1.1 christos const char *sym_name;
2708 1.1 christos char *new_name;
2709 1.1 christos
2710 1.1 christos /* If there's nothing to do in this section, skip it. */
2711 1.1 christos if (! ((section->flags & SEC_RELOC) != 0
2712 1.1 christos && section->reloc_count != 0))
2713 1.1 christos continue;
2714 1.1 christos if ((section->flags & SEC_ALLOC) == 0)
2715 1.1 christos continue;
2716 1.1 christos
2717 1.1 christos /* Get cached copy of section contents if it exists. */
2718 1.1 christos if (elf_section_data (section)->this_hdr.contents != NULL)
2719 1.1 christos contents = elf_section_data (section)->this_hdr.contents;
2720 1.1 christos else if (section->size != 0)
2721 1.1 christos {
2722 1.1 christos /* Go get them off disk. */
2723 1.1 christos if (!bfd_malloc_and_get_section (input_bfd, section,
2724 1.1 christos &contents))
2725 1.1 christos goto error_return;
2726 1.1 christos }
2727 1.1 christos else
2728 1.1 christos contents = NULL;
2729 1.1 christos
2730 1.1 christos /* If there aren't any relocs, then there's nothing to do. */
2731 1.1 christos if ((section->flags & SEC_RELOC) != 0
2732 1.1 christos && section->reloc_count != 0)
2733 1.1 christos {
2734 1.1 christos /* Get a copy of the native relocations. */
2735 1.1 christos internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
2736 1.1 christos NULL, NULL,
2737 1.1 christos link_info->keep_memory);
2738 1.1 christos if (internal_relocs == NULL)
2739 1.1 christos goto error_return;
2740 1.1 christos
2741 1.1 christos /* Now examine each relocation. */
2742 1.1 christos irel = internal_relocs;
2743 1.1 christos irelend = irel + section->reloc_count;
2744 1.1 christos for (; irel < irelend; irel++)
2745 1.1 christos {
2746 1.1 christos long r_type;
2747 1.1 christos unsigned long r_index;
2748 1.1 christos unsigned char code;
2749 1.1 christos
2750 1.1 christos r_type = ELF32_R_TYPE (irel->r_info);
2751 1.1 christos r_index = ELF32_R_SYM (irel->r_info);
2752 1.1 christos
2753 1.1 christos if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
2754 1.1 christos goto error_return;
2755 1.1 christos
2756 1.1 christos /* We need the name and hash table entry of the target
2757 1.1 christos symbol! */
2758 1.1 christos hash = NULL;
2759 1.1 christos sym_sec = NULL;
2760 1.1 christos
2761 1.1 christos if (r_index < symtab_hdr->sh_info)
2762 1.1 christos {
2763 1.1 christos /* A local symbol. */
2764 1.1 christos Elf_Internal_Sym *isym;
2765 1.1 christos struct elf_link_hash_table *elftab;
2766 1.1 christos bfd_size_type amt;
2767 1.1 christos
2768 1.1 christos isym = isymbuf + r_index;
2769 1.1 christos if (isym->st_shndx == SHN_UNDEF)
2770 1.1 christos sym_sec = bfd_und_section_ptr;
2771 1.1 christos else if (isym->st_shndx == SHN_ABS)
2772 1.1 christos sym_sec = bfd_abs_section_ptr;
2773 1.1 christos else if (isym->st_shndx == SHN_COMMON)
2774 1.1 christos sym_sec = bfd_com_section_ptr;
2775 1.1 christos else
2776 1.1 christos sym_sec
2777 1.1 christos = bfd_section_from_elf_index (input_bfd,
2778 1.1 christos isym->st_shndx);
2779 1.1 christos
2780 1.1 christos sym_name
2781 1.1 christos = bfd_elf_string_from_elf_section (input_bfd,
2782 1.1 christos (symtab_hdr
2783 1.1 christos ->sh_link),
2784 1.1 christos isym->st_name);
2785 1.1 christos
2786 1.1 christos /* If it isn't a function, then we don't care
2787 1.1 christos about it. */
2788 1.1 christos if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
2789 1.1 christos continue;
2790 1.1 christos
2791 1.1 christos /* Tack on an ID so we can uniquely identify this
2792 1.1 christos local symbol in the global hash table. */
2793 1.1 christos amt = strlen (sym_name) + 10;
2794 1.1 christos new_name = bfd_malloc (amt);
2795 1.1 christos if (new_name == NULL)
2796 1.1 christos goto error_return;
2797 1.1 christos
2798 1.1 christos sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2799 1.1 christos sym_name = new_name;
2800 1.1 christos
2801 1.1 christos elftab = &hash_table->static_hash_table->root;
2802 1.1 christos hash = ((struct elf32_mn10300_link_hash_entry *)
2803 1.1 christos elf_link_hash_lookup (elftab, sym_name,
2804 1.1 christos TRUE, TRUE, FALSE));
2805 1.1 christos free (new_name);
2806 1.1 christos }
2807 1.1 christos else
2808 1.1 christos {
2809 1.1 christos r_index -= symtab_hdr->sh_info;
2810 1.1 christos hash = (struct elf32_mn10300_link_hash_entry *)
2811 1.1 christos elf_sym_hashes (input_bfd)[r_index];
2812 1.1 christos }
2813 1.1 christos
2814 1.1 christos sym_name = hash->root.root.root.string;
2815 1.1 christos if ((section->flags & SEC_CODE) != 0)
2816 1.1 christos {
2817 1.1 christos /* If this is not a "call" instruction, then we
2818 1.1 christos should convert "call" instructions to "calls"
2819 1.1 christos instructions. */
2820 1.1 christos code = bfd_get_8 (input_bfd,
2821 1.1 christos contents + irel->r_offset - 1);
2822 1.1 christos if (code != 0xdd && code != 0xcd)
2823 1.1 christos hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
2824 1.1 christos }
2825 1.1 christos
2826 1.1 christos /* If this is a jump/call, then bump the
2827 1.1 christos direct_calls counter. Else force "call" to
2828 1.1 christos "calls" conversions. */
2829 1.1 christos if (r_type == R_MN10300_PCREL32
2830 1.1 christos || r_type == R_MN10300_PLT32
2831 1.1 christos || r_type == R_MN10300_PLT16
2832 1.1 christos || r_type == R_MN10300_PCREL16)
2833 1.1 christos hash->direct_calls++;
2834 1.1 christos else
2835 1.1 christos hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
2836 1.1 christos }
2837 1.1 christos }
2838 1.1 christos
2839 1.1 christos /* Now look at the actual contents to get the stack size,
2840 1.1 christos and a list of what registers were saved in the prologue
2841 1.1 christos (ie movm_args). */
2842 1.1 christos if ((section->flags & SEC_CODE) != 0)
2843 1.1 christos {
2844 1.1 christos Elf_Internal_Sym *isym, *isymend;
2845 1.1 christos unsigned int sec_shndx;
2846 1.1 christos struct elf_link_hash_entry **hashes;
2847 1.1 christos struct elf_link_hash_entry **end_hashes;
2848 1.1 christos unsigned int symcount;
2849 1.1 christos
2850 1.1 christos sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2851 1.1 christos section);
2852 1.1 christos
2853 1.1 christos symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2854 1.1 christos - symtab_hdr->sh_info);
2855 1.1 christos hashes = elf_sym_hashes (input_bfd);
2856 1.1 christos end_hashes = hashes + symcount;
2857 1.1 christos
2858 1.1 christos /* Look at each function defined in this section and
2859 1.1 christos update info for that function. */
2860 1.1 christos isymend = isymbuf + symtab_hdr->sh_info;
2861 1.1 christos for (isym = isymbuf; isym < isymend; isym++)
2862 1.1 christos {
2863 1.1 christos if (isym->st_shndx == sec_shndx
2864 1.1 christos && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
2865 1.1 christos {
2866 1.1 christos struct elf_link_hash_table *elftab;
2867 1.1 christos bfd_size_type amt;
2868 1.1 christos struct elf_link_hash_entry **lhashes = hashes;
2869 1.1 christos
2870 1.1 christos /* Skip a local symbol if it aliases a
2871 1.1 christos global one. */
2872 1.1 christos for (; lhashes < end_hashes; lhashes++)
2873 1.1 christos {
2874 1.1 christos hash = (struct elf32_mn10300_link_hash_entry *) *lhashes;
2875 1.1 christos if ((hash->root.root.type == bfd_link_hash_defined
2876 1.1 christos || hash->root.root.type == bfd_link_hash_defweak)
2877 1.1 christos && hash->root.root.u.def.section == section
2878 1.1 christos && hash->root.type == STT_FUNC
2879 1.1 christos && hash->root.root.u.def.value == isym->st_value)
2880 1.1 christos break;
2881 1.1 christos }
2882 1.1 christos if (lhashes != end_hashes)
2883 1.1 christos continue;
2884 1.1 christos
2885 1.1 christos if (isym->st_shndx == SHN_UNDEF)
2886 1.1 christos sym_sec = bfd_und_section_ptr;
2887 1.1 christos else if (isym->st_shndx == SHN_ABS)
2888 1.1 christos sym_sec = bfd_abs_section_ptr;
2889 1.1 christos else if (isym->st_shndx == SHN_COMMON)
2890 1.1 christos sym_sec = bfd_com_section_ptr;
2891 1.1 christos else
2892 1.1 christos sym_sec
2893 1.1 christos = bfd_section_from_elf_index (input_bfd,
2894 1.1 christos isym->st_shndx);
2895 1.1 christos
2896 1.1 christos sym_name = (bfd_elf_string_from_elf_section
2897 1.1 christos (input_bfd, symtab_hdr->sh_link,
2898 1.1 christos isym->st_name));
2899 1.1 christos
2900 1.1 christos /* Tack on an ID so we can uniquely identify this
2901 1.1 christos local symbol in the global hash table. */
2902 1.1 christos amt = strlen (sym_name) + 10;
2903 1.1 christos new_name = bfd_malloc (amt);
2904 1.1 christos if (new_name == NULL)
2905 1.1 christos goto error_return;
2906 1.1 christos
2907 1.1 christos sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2908 1.1 christos sym_name = new_name;
2909 1.1 christos
2910 1.1 christos elftab = &hash_table->static_hash_table->root;
2911 1.1 christos hash = ((struct elf32_mn10300_link_hash_entry *)
2912 1.1 christos elf_link_hash_lookup (elftab, sym_name,
2913 1.1 christos TRUE, TRUE, FALSE));
2914 1.1 christos free (new_name);
2915 1.1 christos compute_function_info (input_bfd, hash,
2916 1.1 christos isym->st_value, contents);
2917 1.1 christos hash->value = isym->st_value;
2918 1.1 christos }
2919 1.1 christos }
2920 1.1 christos
2921 1.1 christos for (; hashes < end_hashes; hashes++)
2922 1.1 christos {
2923 1.1 christos hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2924 1.1 christos if ((hash->root.root.type == bfd_link_hash_defined
2925 1.1 christos || hash->root.root.type == bfd_link_hash_defweak)
2926 1.1 christos && hash->root.root.u.def.section == section
2927 1.1 christos && hash->root.type == STT_FUNC)
2928 1.1 christos compute_function_info (input_bfd, hash,
2929 1.1 christos (hash)->root.root.u.def.value,
2930 1.1 christos contents);
2931 1.1 christos }
2932 1.1 christos }
2933 1.1 christos
2934 1.1 christos /* Cache or free any memory we allocated for the relocs. */
2935 1.1 christos if (internal_relocs != NULL
2936 1.1 christos && elf_section_data (section)->relocs != internal_relocs)
2937 1.1 christos free (internal_relocs);
2938 1.1 christos internal_relocs = NULL;
2939 1.1 christos
2940 1.1 christos /* Cache or free any memory we allocated for the contents. */
2941 1.1 christos if (contents != NULL
2942 1.1 christos && elf_section_data (section)->this_hdr.contents != contents)
2943 1.1 christos {
2944 1.1 christos if (! link_info->keep_memory)
2945 1.1 christos free (contents);
2946 1.1 christos else
2947 1.1 christos {
2948 1.1 christos /* Cache the section contents for elf_link_input_bfd. */
2949 1.1 christos elf_section_data (section)->this_hdr.contents = contents;
2950 1.1 christos }
2951 1.1 christos }
2952 1.1 christos contents = NULL;
2953 1.1 christos }
2954 1.1 christos
2955 1.1 christos /* Cache or free any memory we allocated for the symbols. */
2956 1.1 christos if (isymbuf != NULL
2957 1.1 christos && symtab_hdr->contents != (unsigned char *) isymbuf)
2958 1.1 christos {
2959 1.1 christos if (! link_info->keep_memory)
2960 1.1 christos free (isymbuf);
2961 1.1 christos else
2962 1.1 christos {
2963 1.1 christos /* Cache the symbols for elf_link_input_bfd. */
2964 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
2965 1.1 christos }
2966 1.1 christos }
2967 1.1 christos isymbuf = NULL;
2968 1.1 christos }
2969 1.1 christos
2970 1.1 christos /* Now iterate on each symbol in the hash table and perform
2971 1.1 christos the final initialization steps on each. */
2972 1.1 christos elf32_mn10300_link_hash_traverse (hash_table,
2973 1.1 christos elf32_mn10300_finish_hash_table_entry,
2974 1.1 christos link_info);
2975 1.1 christos elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2976 1.1 christos elf32_mn10300_finish_hash_table_entry,
2977 1.1 christos link_info);
2978 1.1 christos
2979 1.1 christos {
2980 1.1 christos /* This section of code collects all our local symbols, sorts
2981 1.1 christos them by value, and looks for multiple symbols referring to
2982 1.1 christos the same address. For those symbols, the flags are merged.
2983 1.1 christos At this point, the only flag that can be set is
2984 1.1 christos MN10300_CONVERT_CALL_TO_CALLS, so we simply OR the flags
2985 1.1 christos together. */
2986 1.1 christos int static_count = 0, i;
2987 1.1 christos struct elf32_mn10300_link_hash_entry **entries;
2988 1.1 christos struct elf32_mn10300_link_hash_entry **ptr;
2989 1.1 christos
2990 1.1 christos elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2991 1.1 christos elf32_mn10300_count_hash_table_entries,
2992 1.1 christos &static_count);
2993 1.1 christos
2994 1.1 christos entries = bfd_malloc (static_count * sizeof (* ptr));
2995 1.1 christos
2996 1.1 christos ptr = entries;
2997 1.1 christos elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2998 1.1 christos elf32_mn10300_list_hash_table_entries,
2999 1.1 christos & ptr);
3000 1.1 christos
3001 1.1 christos qsort (entries, static_count, sizeof (entries[0]), sort_by_value);
3002 1.1 christos
3003 1.1 christos for (i = 0; i < static_count - 1; i++)
3004 1.1 christos if (entries[i]->value && entries[i]->value == entries[i+1]->value)
3005 1.1 christos {
3006 1.1 christos int v = entries[i]->flags;
3007 1.1 christos int j;
3008 1.1 christos
3009 1.1 christos for (j = i + 1; j < static_count && entries[j]->value == entries[i]->value; j++)
3010 1.1 christos v |= entries[j]->flags;
3011 1.1 christos
3012 1.1 christos for (j = i; j < static_count && entries[j]->value == entries[i]->value; j++)
3013 1.1 christos entries[j]->flags = v;
3014 1.1 christos
3015 1.1 christos i = j - 1;
3016 1.1 christos }
3017 1.1 christos }
3018 1.1 christos
3019 1.1 christos /* All entries in the hash table are fully initialized. */
3020 1.1 christos hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
3021 1.1 christos
3022 1.1 christos /* Now that everything has been initialized, go through each
3023 1.1 christos code section and delete any prologue insns which will be
3024 1.1 christos redundant because their operations will be performed by
3025 1.1 christos a "call" instruction. */
3026 1.1 christos for (input_bfd = link_info->input_bfds;
3027 1.1.1.4 christos input_bfd != NULL;
3028 1.1 christos input_bfd = input_bfd->link.next)
3029 1.1 christos {
3030 1.1 christos /* We're going to need all the local symbols for each bfd. */
3031 1.1 christos symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3032 1.1 christos if (symtab_hdr->sh_info != 0)
3033 1.1 christos {
3034 1.1 christos isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3035 1.1 christos if (isymbuf == NULL)
3036 1.1 christos isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3037 1.1 christos symtab_hdr->sh_info, 0,
3038 1.1 christos NULL, NULL, NULL);
3039 1.1 christos if (isymbuf == NULL)
3040 1.1 christos goto error_return;
3041 1.1 christos }
3042 1.1 christos
3043 1.1 christos /* Walk over each section in this bfd. */
3044 1.1 christos for (section = input_bfd->sections;
3045 1.1 christos section != NULL;
3046 1.1 christos section = section->next)
3047 1.1 christos {
3048 1.1 christos unsigned int sec_shndx;
3049 1.1 christos Elf_Internal_Sym *isym, *isymend;
3050 1.1 christos struct elf_link_hash_entry **hashes;
3051 1.1 christos struct elf_link_hash_entry **end_hashes;
3052 1.1 christos unsigned int symcount;
3053 1.1 christos
3054 1.1 christos /* Skip non-code sections and empty sections. */
3055 1.1 christos if ((section->flags & SEC_CODE) == 0 || section->size == 0)
3056 1.1 christos continue;
3057 1.1 christos
3058 1.1 christos if (section->reloc_count != 0)
3059 1.1 christos {
3060 1.1 christos /* Get a copy of the native relocations. */
3061 1.1 christos internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
3062 1.1 christos NULL, NULL,
3063 1.1 christos link_info->keep_memory);
3064 1.1 christos if (internal_relocs == NULL)
3065 1.1 christos goto error_return;
3066 1.1 christos }
3067 1.1 christos
3068 1.1 christos /* Get cached copy of section contents if it exists. */
3069 1.1 christos if (elf_section_data (section)->this_hdr.contents != NULL)
3070 1.1 christos contents = elf_section_data (section)->this_hdr.contents;
3071 1.1 christos else
3072 1.1 christos {
3073 1.1 christos /* Go get them off disk. */
3074 1.1 christos if (!bfd_malloc_and_get_section (input_bfd, section,
3075 1.1 christos &contents))
3076 1.1 christos goto error_return;
3077 1.1 christos }
3078 1.1 christos
3079 1.1 christos sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
3080 1.1 christos section);
3081 1.1 christos
3082 1.1 christos /* Now look for any function in this section which needs
3083 1.1 christos insns deleted from its prologue. */
3084 1.1 christos isymend = isymbuf + symtab_hdr->sh_info;
3085 1.1 christos for (isym = isymbuf; isym < isymend; isym++)
3086 1.1 christos {
3087 1.1 christos struct elf32_mn10300_link_hash_entry *sym_hash;
3088 1.1 christos asection *sym_sec = NULL;
3089 1.1 christos const char *sym_name;
3090 1.1 christos char *new_name;
3091 1.1 christos struct elf_link_hash_table *elftab;
3092 1.1 christos bfd_size_type amt;
3093 1.1 christos
3094 1.1 christos if (isym->st_shndx != sec_shndx)
3095 1.1 christos continue;
3096 1.1 christos
3097 1.1 christos if (isym->st_shndx == SHN_UNDEF)
3098 1.1 christos sym_sec = bfd_und_section_ptr;
3099 1.1 christos else if (isym->st_shndx == SHN_ABS)
3100 1.1 christos sym_sec = bfd_abs_section_ptr;
3101 1.1 christos else if (isym->st_shndx == SHN_COMMON)
3102 1.1 christos sym_sec = bfd_com_section_ptr;
3103 1.1 christos else
3104 1.1 christos sym_sec
3105 1.1 christos = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3106 1.1 christos
3107 1.1 christos sym_name
3108 1.1 christos = bfd_elf_string_from_elf_section (input_bfd,
3109 1.1 christos symtab_hdr->sh_link,
3110 1.1 christos isym->st_name);
3111 1.1 christos
3112 1.1 christos /* Tack on an ID so we can uniquely identify this
3113 1.1 christos local symbol in the global hash table. */
3114 1.1 christos amt = strlen (sym_name) + 10;
3115 1.1 christos new_name = bfd_malloc (amt);
3116 1.1 christos if (new_name == NULL)
3117 1.1 christos goto error_return;
3118 1.1 christos sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
3119 1.1 christos sym_name = new_name;
3120 1.1 christos
3121 1.1 christos elftab = & hash_table->static_hash_table->root;
3122 1.1 christos sym_hash = (struct elf32_mn10300_link_hash_entry *)
3123 1.1 christos elf_link_hash_lookup (elftab, sym_name,
3124 1.1 christos FALSE, FALSE, FALSE);
3125 1.1 christos
3126 1.1 christos free (new_name);
3127 1.1 christos if (sym_hash == NULL)
3128 1.1 christos continue;
3129 1.1 christos
3130 1.1 christos if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
3131 1.1 christos && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
3132 1.1 christos {
3133 1.1 christos int bytes = 0;
3134 1.1 christos
3135 1.1 christos /* Note that we've changed things. */
3136 1.1 christos elf_section_data (section)->relocs = internal_relocs;
3137 1.1 christos elf_section_data (section)->this_hdr.contents = contents;
3138 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
3139 1.1 christos
3140 1.1 christos /* Count how many bytes we're going to delete. */
3141 1.1 christos if (sym_hash->movm_args)
3142 1.1 christos bytes += 2;
3143 1.1 christos
3144 1.1 christos if (sym_hash->stack_size > 0)
3145 1.1 christos {
3146 1.1 christos if (sym_hash->stack_size <= 128)
3147 1.1 christos bytes += 3;
3148 1.1 christos else
3149 1.1 christos bytes += 4;
3150 1.1 christos }
3151 1.1 christos
3152 1.1 christos /* Note that we've deleted prologue bytes for this
3153 1.1 christos function. */
3154 1.1 christos sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
3155 1.1 christos
3156 1.1 christos /* Actually delete the bytes. */
3157 1.1 christos if (!mn10300_elf_relax_delete_bytes (input_bfd,
3158 1.1 christos section,
3159 1.1 christos isym->st_value,
3160 1.1 christos bytes))
3161 1.1 christos goto error_return;
3162 1.1 christos
3163 1.1 christos /* Something changed. Not strictly necessary, but
3164 1.1 christos may lead to more relaxing opportunities. */
3165 1.1 christos *again = TRUE;
3166 1.1 christos }
3167 1.1 christos }
3168 1.1 christos
3169 1.1 christos /* Look for any global functions in this section which
3170 1.1 christos need insns deleted from their prologues. */
3171 1.1 christos symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
3172 1.1 christos - symtab_hdr->sh_info);
3173 1.1 christos hashes = elf_sym_hashes (input_bfd);
3174 1.1 christos end_hashes = hashes + symcount;
3175 1.1 christos for (; hashes < end_hashes; hashes++)
3176 1.1 christos {
3177 1.1 christos struct elf32_mn10300_link_hash_entry *sym_hash;
3178 1.1 christos
3179 1.1 christos sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
3180 1.1 christos if ((sym_hash->root.root.type == bfd_link_hash_defined
3181 1.1 christos || sym_hash->root.root.type == bfd_link_hash_defweak)
3182 1.1 christos && sym_hash->root.root.u.def.section == section
3183 1.1 christos && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
3184 1.1 christos && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
3185 1.1 christos {
3186 1.1 christos int bytes = 0;
3187 1.1.1.2 christos bfd_vma symval;
3188 1.1 christos struct elf_link_hash_entry **hh;
3189 1.1 christos
3190 1.1 christos /* Note that we've changed things. */
3191 1.1 christos elf_section_data (section)->relocs = internal_relocs;
3192 1.1 christos elf_section_data (section)->this_hdr.contents = contents;
3193 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
3194 1.1 christos
3195 1.1 christos /* Count how many bytes we're going to delete. */
3196 1.1 christos if (sym_hash->movm_args)
3197 1.1 christos bytes += 2;
3198 1.1 christos
3199 1.1 christos if (sym_hash->stack_size > 0)
3200 1.1 christos {
3201 1.1 christos if (sym_hash->stack_size <= 128)
3202 1.1 christos bytes += 3;
3203 1.1 christos else
3204 1.1 christos bytes += 4;
3205 1.1 christos }
3206 1.1 christos
3207 1.1 christos /* Note that we've deleted prologue bytes for this
3208 1.1 christos function. */
3209 1.1 christos sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
3210 1.1 christos
3211 1.1 christos /* Actually delete the bytes. */
3212 1.1 christos symval = sym_hash->root.root.u.def.value;
3213 1.1 christos if (!mn10300_elf_relax_delete_bytes (input_bfd,
3214 1.1 christos section,
3215 1.1 christos symval,
3216 1.1 christos bytes))
3217 1.1 christos goto error_return;
3218 1.1.1.2 christos
3219 1.1.1.2 christos /* There may be other C++ functions symbols with the same
3220 1.1.1.2 christos address. If so then mark these as having had their
3221 1.1.1.2 christos prologue bytes deleted as well. */
3222 1.1.1.2 christos for (hh = elf_sym_hashes (input_bfd); hh < end_hashes; hh++)
3223 1.1.1.2 christos {
3224 1.1.1.2 christos struct elf32_mn10300_link_hash_entry *h;
3225 1.1.1.2 christos
3226 1.1.1.2 christos h = (struct elf32_mn10300_link_hash_entry *) * hh;
3227 1.1.1.2 christos
3228 1.1.1.2 christos if (h != sym_hash
3229 1.1.1.2 christos && (h->root.root.type == bfd_link_hash_defined
3230 1.1.1.2 christos || h->root.root.type == bfd_link_hash_defweak)
3231 1.1.1.2 christos && h->root.root.u.def.section == section
3232 1.1.1.2 christos && ! (h->flags & MN10300_CONVERT_CALL_TO_CALLS)
3233 1.1.1.2 christos && h->root.root.u.def.value == symval
3234 1.1.1.2 christos && h->root.type == STT_FUNC)
3235 1.1.1.2 christos h->flags |= MN10300_DELETED_PROLOGUE_BYTES;
3236 1.1.1.2 christos }
3237 1.1 christos
3238 1.1 christos /* Something changed. Not strictly necessary, but
3239 1.1 christos may lead to more relaxing opportunities. */
3240 1.1 christos *again = TRUE;
3241 1.1 christos }
3242 1.1 christos }
3243 1.1 christos
3244 1.1 christos /* Cache or free any memory we allocated for the relocs. */
3245 1.1 christos if (internal_relocs != NULL
3246 1.1 christos && elf_section_data (section)->relocs != internal_relocs)
3247 1.1 christos free (internal_relocs);
3248 1.1 christos internal_relocs = NULL;
3249 1.1 christos
3250 1.1 christos /* Cache or free any memory we allocated for the contents. */
3251 1.1 christos if (contents != NULL
3252 1.1 christos && elf_section_data (section)->this_hdr.contents != contents)
3253 1.1 christos {
3254 1.1 christos if (! link_info->keep_memory)
3255 1.1 christos free (contents);
3256 1.1 christos else
3257 1.1 christos /* Cache the section contents for elf_link_input_bfd. */
3258 1.1 christos elf_section_data (section)->this_hdr.contents = contents;
3259 1.1 christos }
3260 1.1 christos contents = NULL;
3261 1.1 christos }
3262 1.1 christos
3263 1.1 christos /* Cache or free any memory we allocated for the symbols. */
3264 1.1 christos if (isymbuf != NULL
3265 1.1 christos && symtab_hdr->contents != (unsigned char *) isymbuf)
3266 1.1 christos {
3267 1.1 christos if (! link_info->keep_memory)
3268 1.1 christos free (isymbuf);
3269 1.1 christos else
3270 1.1 christos /* Cache the symbols for elf_link_input_bfd. */
3271 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
3272 1.1 christos }
3273 1.1 christos isymbuf = NULL;
3274 1.1 christos }
3275 1.1 christos }
3276 1.1 christos
3277 1.1 christos /* (Re)initialize for the basic instruction shortening/relaxing pass. */
3278 1.1 christos contents = NULL;
3279 1.1 christos internal_relocs = NULL;
3280 1.1 christos isymbuf = NULL;
3281 1.1 christos /* For error_return. */
3282 1.1 christos section = sec;
3283 1.1 christos
3284 1.1 christos /* We don't have to do anything for a relocatable link, if
3285 1.1 christos this section does not have relocs, or if this is not a
3286 1.1.1.6 christos code section. */
3287 1.1 christos if (bfd_link_relocatable (link_info)
3288 1.1 christos || (sec->flags & SEC_RELOC) == 0
3289 1.1 christos || sec->reloc_count == 0
3290 1.1 christos || (sec->flags & SEC_CODE) == 0)
3291 1.1 christos return TRUE;
3292 1.1 christos
3293 1.1 christos symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3294 1.1 christos
3295 1.1 christos /* Get a copy of the native relocations. */
3296 1.1 christos internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
3297 1.1 christos link_info->keep_memory);
3298 1.1 christos if (internal_relocs == NULL)
3299 1.1 christos goto error_return;
3300 1.1 christos
3301 1.1 christos /* Scan for worst case alignment gap changes. Note that this logic
3302 1.1 christos is not ideal; what we should do is run this scan for every
3303 1.1 christos opcode/address range and adjust accordingly, but that's
3304 1.1 christos expensive. Worst case is that for an alignment of N bytes, we
3305 1.1 christos move by 2*N-N-1 bytes, assuming we have aligns of 1, 2, 4, 8, etc
3306 1.1 christos all before it. Plus, this still doesn't cover cross-section
3307 1.1 christos jumps with section alignment. */
3308 1.1 christos irelend = internal_relocs + sec->reloc_count;
3309 1.1 christos align_gap_adjustment = 0;
3310 1.1 christos for (irel = internal_relocs; irel < irelend; irel++)
3311 1.1 christos {
3312 1.1 christos if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN)
3313 1.1 christos {
3314 1.1 christos bfd_vma adj = 1 << irel->r_addend;
3315 1.1 christos bfd_vma aend = irel->r_offset;
3316 1.1 christos
3317 1.1 christos aend = BFD_ALIGN (aend, 1 << irel->r_addend);
3318 1.1 christos adj = 2 * adj - adj - 1;
3319 1.1 christos
3320 1.1 christos /* Record the biggest adjustmnet. Skip any alignment at the
3321 1.1 christos end of our section. */
3322 1.1 christos if (align_gap_adjustment < adj
3323 1.1 christos && aend < sec->output_section->vma + sec->output_offset + sec->size)
3324 1.1 christos align_gap_adjustment = adj;
3325 1.1 christos }
3326 1.1 christos }
3327 1.1 christos
3328 1.1 christos /* Walk through them looking for relaxing opportunities. */
3329 1.1 christos irelend = internal_relocs + sec->reloc_count;
3330 1.1 christos for (irel = internal_relocs; irel < irelend; irel++)
3331 1.1 christos {
3332 1.1 christos bfd_vma symval;
3333 1.1 christos bfd_signed_vma jump_offset;
3334 1.1 christos asection *sym_sec = NULL;
3335 1.1 christos struct elf32_mn10300_link_hash_entry *h = NULL;
3336 1.1 christos
3337 1.1 christos /* If this isn't something that can be relaxed, then ignore
3338 1.1 christos this reloc. */
3339 1.1 christos if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
3340 1.1 christos || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
3341 1.1 christos || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
3342 1.1 christos continue;
3343 1.1 christos
3344 1.1 christos /* Get the section contents if we haven't done so already. */
3345 1.1 christos if (contents == NULL)
3346 1.1 christos {
3347 1.1 christos /* Get cached copy if it exists. */
3348 1.1 christos if (elf_section_data (sec)->this_hdr.contents != NULL)
3349 1.1 christos contents = elf_section_data (sec)->this_hdr.contents;
3350 1.1 christos else
3351 1.1 christos {
3352 1.1 christos /* Go get them off disk. */
3353 1.1 christos if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3354 1.1 christos goto error_return;
3355 1.1 christos }
3356 1.1 christos }
3357 1.1 christos
3358 1.1 christos /* Read this BFD's symbols if we haven't done so already. */
3359 1.1 christos if (isymbuf == NULL && symtab_hdr->sh_info != 0)
3360 1.1 christos {
3361 1.1 christos isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3362 1.1 christos if (isymbuf == NULL)
3363 1.1 christos isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3364 1.1 christos symtab_hdr->sh_info, 0,
3365 1.1 christos NULL, NULL, NULL);
3366 1.1 christos if (isymbuf == NULL)
3367 1.1 christos goto error_return;
3368 1.1 christos }
3369 1.1 christos
3370 1.1 christos /* Get the value of the symbol referred to by the reloc. */
3371 1.1 christos if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
3372 1.1 christos {
3373 1.1 christos Elf_Internal_Sym *isym;
3374 1.1 christos const char *sym_name;
3375 1.1 christos char *new_name;
3376 1.1 christos
3377 1.1 christos /* A local symbol. */
3378 1.1 christos isym = isymbuf + ELF32_R_SYM (irel->r_info);
3379 1.1 christos if (isym->st_shndx == SHN_UNDEF)
3380 1.1 christos sym_sec = bfd_und_section_ptr;
3381 1.1 christos else if (isym->st_shndx == SHN_ABS)
3382 1.1 christos sym_sec = bfd_abs_section_ptr;
3383 1.1 christos else if (isym->st_shndx == SHN_COMMON)
3384 1.1 christos sym_sec = bfd_com_section_ptr;
3385 1.1 christos else
3386 1.1 christos sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3387 1.1 christos
3388 1.1 christos sym_name = bfd_elf_string_from_elf_section (abfd,
3389 1.1 christos symtab_hdr->sh_link,
3390 1.1 christos isym->st_name);
3391 1.1 christos
3392 1.1.1.2 christos if ((sym_sec->flags & SEC_MERGE)
3393 1.1 christos && sym_sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3394 1.1 christos {
3395 1.1 christos symval = isym->st_value;
3396 1.1 christos
3397 1.1 christos /* GAS may reduce relocations against symbols in SEC_MERGE
3398 1.1 christos sections to a relocation against the section symbol when
3399 1.1 christos the original addend was zero. When the reloc is against
3400 1.1 christos a section symbol we should include the addend in the
3401 1.1 christos offset passed to _bfd_merged_section_offset, since the
3402 1.1 christos location of interest is the original symbol. On the
3403 1.1 christos other hand, an access to "sym+addend" where "sym" is not
3404 1.1 christos a section symbol should not include the addend; Such an
3405 1.1 christos access is presumed to be an offset from "sym"; The
3406 1.1 christos location of interest is just "sym". */
3407 1.1 christos if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
3408 1.1 christos symval += irel->r_addend;
3409 1.1 christos
3410 1.1 christos symval = _bfd_merged_section_offset (abfd, & sym_sec,
3411 1.1 christos elf_section_data (sym_sec)->sec_info,
3412 1.1 christos symval);
3413 1.1 christos
3414 1.1 christos if (ELF_ST_TYPE (isym->st_info) != STT_SECTION)
3415 1.1 christos symval += irel->r_addend;
3416 1.1 christos
3417 1.1 christos symval += sym_sec->output_section->vma
3418 1.1 christos + sym_sec->output_offset - irel->r_addend;
3419 1.1 christos }
3420 1.1 christos else
3421 1.1 christos symval = (isym->st_value
3422 1.1 christos + sym_sec->output_section->vma
3423 1.1 christos + sym_sec->output_offset);
3424 1.1 christos
3425 1.1 christos /* Tack on an ID so we can uniquely identify this
3426 1.1 christos local symbol in the global hash table. */
3427 1.1 christos new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
3428 1.1 christos if (new_name == NULL)
3429 1.1 christos goto error_return;
3430 1.1 christos sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
3431 1.1 christos sym_name = new_name;
3432 1.1 christos
3433 1.1 christos h = (struct elf32_mn10300_link_hash_entry *)
3434 1.1 christos elf_link_hash_lookup (&hash_table->static_hash_table->root,
3435 1.1 christos sym_name, FALSE, FALSE, FALSE);
3436 1.1 christos free (new_name);
3437 1.1 christos }
3438 1.1 christos else
3439 1.1 christos {
3440 1.1 christos unsigned long indx;
3441 1.1 christos
3442 1.1 christos /* An external symbol. */
3443 1.1 christos indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
3444 1.1 christos h = (struct elf32_mn10300_link_hash_entry *)
3445 1.1 christos (elf_sym_hashes (abfd)[indx]);
3446 1.1 christos BFD_ASSERT (h != NULL);
3447 1.1 christos if (h->root.root.type != bfd_link_hash_defined
3448 1.1 christos && h->root.root.type != bfd_link_hash_defweak)
3449 1.1 christos /* This appears to be a reference to an undefined
3450 1.1 christos symbol. Just ignore it--it will be caught by the
3451 1.1 christos regular reloc processing. */
3452 1.1 christos continue;
3453 1.1 christos
3454 1.1 christos /* Check for a reference to a discarded symbol and ignore it. */
3455 1.1 christos if (h->root.root.u.def.section->output_section == NULL)
3456 1.1 christos continue;
3457 1.1 christos
3458 1.1 christos sym_sec = h->root.root.u.def.section->output_section;
3459 1.1 christos
3460 1.1 christos symval = (h->root.root.u.def.value
3461 1.1 christos + h->root.root.u.def.section->output_section->vma
3462 1.1 christos + h->root.root.u.def.section->output_offset);
3463 1.1 christos }
3464 1.1 christos
3465 1.1 christos /* For simplicity of coding, we are going to modify the section
3466 1.1 christos contents, the section relocs, and the BFD symbol table. We
3467 1.1 christos must tell the rest of the code not to free up this
3468 1.1 christos information. It would be possible to instead create a table
3469 1.1 christos of changes which have to be made, as is done in coff-mips.c;
3470 1.1 christos that would be more work, but would require less memory when
3471 1.1 christos the linker is run. */
3472 1.1 christos
3473 1.1 christos /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
3474 1.1 christos branch/call, also deal with "call" -> "calls" conversions and
3475 1.1 christos insertion of prologue data into "call" instructions. */
3476 1.1 christos if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32
3477 1.1 christos || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32)
3478 1.1 christos {
3479 1.1 christos bfd_vma value = symval;
3480 1.1 christos
3481 1.1 christos if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32
3482 1.1 christos && h != NULL
3483 1.1 christos && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
3484 1.1 christos && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
3485 1.1 christos && h->root.plt.offset != (bfd_vma) -1)
3486 1.1 christos {
3487 1.1 christos asection * splt;
3488 1.1.1.2 christos
3489 1.1 christos splt = hash_table->root.splt;
3490 1.1 christos value = ((splt->output_section->vma
3491 1.1 christos + splt->output_offset
3492 1.1 christos + h->root.plt.offset)
3493 1.1 christos - (sec->output_section->vma
3494 1.1 christos + sec->output_offset
3495 1.1 christos + irel->r_offset));
3496 1.1 christos }
3497 1.1 christos
3498 1.1 christos /* If we've got a "call" instruction that needs to be turned
3499 1.1 christos into a "calls" instruction, do so now. It saves a byte. */
3500 1.1 christos if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
3501 1.1 christos {
3502 1.1 christos unsigned char code;
3503 1.1 christos
3504 1.1 christos /* Get the opcode. */
3505 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3506 1.1 christos
3507 1.1 christos /* Make sure we're working with a "call" instruction! */
3508 1.1 christos if (code == 0xdd)
3509 1.1 christos {
3510 1.1 christos /* Note that we've changed the relocs, section contents,
3511 1.1 christos etc. */
3512 1.1 christos elf_section_data (sec)->relocs = internal_relocs;
3513 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
3514 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
3515 1.1 christos
3516 1.1 christos /* Fix the opcode. */
3517 1.1 christos bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
3518 1.1 christos bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
3519 1.1 christos
3520 1.1 christos /* Fix irel->r_offset and irel->r_addend. */
3521 1.1 christos irel->r_offset += 1;
3522 1.1 christos irel->r_addend += 1;
3523 1.1 christos
3524 1.1 christos /* Delete one byte of data. */
3525 1.1 christos if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3526 1.1 christos irel->r_offset + 3, 1))
3527 1.1 christos goto error_return;
3528 1.1 christos
3529 1.1 christos /* That will change things, so, we should relax again.
3530 1.1 christos Note that this is not required, and it may be slow. */
3531 1.1 christos *again = TRUE;
3532 1.1 christos }
3533 1.1 christos }
3534 1.1 christos else if (h)
3535 1.1 christos {
3536 1.1 christos /* We've got a "call" instruction which needs some data
3537 1.1 christos from target function filled in. */
3538 1.1 christos unsigned char code;
3539 1.1 christos
3540 1.1 christos /* Get the opcode. */
3541 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3542 1.1 christos
3543 1.1 christos /* Insert data from the target function into the "call"
3544 1.1 christos instruction if needed. */
3545 1.1 christos if (code == 0xdd)
3546 1.1 christos {
3547 1.1 christos bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
3548 1.1 christos bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
3549 1.1 christos contents + irel->r_offset + 5);
3550 1.1 christos }
3551 1.1 christos }
3552 1.1 christos
3553 1.1 christos /* Deal with pc-relative gunk. */
3554 1.1 christos value -= (sec->output_section->vma + sec->output_offset);
3555 1.1 christos value -= irel->r_offset;
3556 1.1 christos value += irel->r_addend;
3557 1.1 christos
3558 1.1 christos /* See if the value will fit in 16 bits, note the high value is
3559 1.1 christos 0x7fff + 2 as the target will be two bytes closer if we are
3560 1.1 christos able to relax, if it's in the same section. */
3561 1.1 christos if (sec->output_section == sym_sec->output_section)
3562 1.1 christos jump_offset = 0x8001;
3563 1.1 christos else
3564 1.1 christos jump_offset = 0x7fff;
3565 1.1 christos
3566 1.1 christos /* Account for jumps across alignment boundaries using
3567 1.1 christos align_gap_adjustment. */
3568 1.1 christos if ((bfd_signed_vma) value < jump_offset - (bfd_signed_vma) align_gap_adjustment
3569 1.1 christos && ((bfd_signed_vma) value > -0x8000 + (bfd_signed_vma) align_gap_adjustment))
3570 1.1 christos {
3571 1.1 christos unsigned char code;
3572 1.1 christos
3573 1.1 christos /* Get the opcode. */
3574 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3575 1.1 christos
3576 1.1 christos if (code != 0xdc && code != 0xdd && code != 0xff)
3577 1.1 christos continue;
3578 1.1 christos
3579 1.1 christos /* Note that we've changed the relocs, section contents, etc. */
3580 1.1 christos elf_section_data (sec)->relocs = internal_relocs;
3581 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
3582 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
3583 1.1 christos
3584 1.1 christos /* Fix the opcode. */
3585 1.1 christos if (code == 0xdc)
3586 1.1 christos bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
3587 1.1 christos else if (code == 0xdd)
3588 1.1 christos bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
3589 1.1 christos else if (code == 0xff)
3590 1.1 christos bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3591 1.1 christos
3592 1.1 christos /* Fix the relocation's type. */
3593 1.1 christos irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3594 1.1 christos (ELF32_R_TYPE (irel->r_info)
3595 1.1 christos == (int) R_MN10300_PLT32)
3596 1.1 christos ? R_MN10300_PLT16 :
3597 1.1 christos R_MN10300_PCREL16);
3598 1.1 christos
3599 1.1 christos /* Delete two bytes of data. */
3600 1.1 christos if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3601 1.1 christos irel->r_offset + 1, 2))
3602 1.1 christos goto error_return;
3603 1.1 christos
3604 1.1 christos /* That will change things, so, we should relax again.
3605 1.1 christos Note that this is not required, and it may be slow. */
3606 1.1 christos *again = TRUE;
3607 1.1 christos }
3608 1.1 christos }
3609 1.1 christos
3610 1.1 christos /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
3611 1.1 christos branch. */
3612 1.1 christos if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
3613 1.1 christos {
3614 1.1 christos bfd_vma value = symval;
3615 1.1 christos
3616 1.1 christos /* If we've got a "call" instruction that needs to be turned
3617 1.1 christos into a "calls" instruction, do so now. It saves a byte. */
3618 1.1 christos if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
3619 1.1 christos {
3620 1.1 christos unsigned char code;
3621 1.1 christos
3622 1.1 christos /* Get the opcode. */
3623 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3624 1.1 christos
3625 1.1 christos /* Make sure we're working with a "call" instruction! */
3626 1.1 christos if (code == 0xcd)
3627 1.1 christos {
3628 1.1 christos /* Note that we've changed the relocs, section contents,
3629 1.1 christos etc. */
3630 1.1 christos elf_section_data (sec)->relocs = internal_relocs;
3631 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
3632 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
3633 1.1 christos
3634 1.1 christos /* Fix the opcode. */
3635 1.1 christos bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
3636 1.1 christos bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
3637 1.1 christos
3638 1.1 christos /* Fix irel->r_offset and irel->r_addend. */
3639 1.1 christos irel->r_offset += 1;
3640 1.1 christos irel->r_addend += 1;
3641 1.1 christos
3642 1.1 christos /* Delete one byte of data. */
3643 1.1 christos if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3644 1.1 christos irel->r_offset + 1, 1))
3645 1.1 christos goto error_return;
3646 1.1 christos
3647 1.1 christos /* That will change things, so, we should relax again.
3648 1.1 christos Note that this is not required, and it may be slow. */
3649 1.1 christos *again = TRUE;
3650 1.1 christos }
3651 1.1 christos }
3652 1.1 christos else if (h)
3653 1.1 christos {
3654 1.1 christos unsigned char code;
3655 1.1 christos
3656 1.1 christos /* Get the opcode. */
3657 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3658 1.1 christos
3659 1.1 christos /* Insert data from the target function into the "call"
3660 1.1 christos instruction if needed. */
3661 1.1 christos if (code == 0xcd)
3662 1.1 christos {
3663 1.1 christos bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
3664 1.1 christos bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
3665 1.1 christos contents + irel->r_offset + 3);
3666 1.1 christos }
3667 1.1 christos }
3668 1.1 christos
3669 1.1 christos /* Deal with pc-relative gunk. */
3670 1.1 christos value -= (sec->output_section->vma + sec->output_offset);
3671 1.1 christos value -= irel->r_offset;
3672 1.1 christos value += irel->r_addend;
3673 1.1 christos
3674 1.1 christos /* See if the value will fit in 8 bits, note the high value is
3675 1.1 christos 0x7f + 1 as the target will be one bytes closer if we are
3676 1.1 christos able to relax. */
3677 1.1 christos if ((long) value < 0x80 && (long) value > -0x80)
3678 1.1 christos {
3679 1.1 christos unsigned char code;
3680 1.1 christos
3681 1.1 christos /* Get the opcode. */
3682 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3683 1.1 christos
3684 1.1 christos if (code != 0xcc)
3685 1.1 christos continue;
3686 1.1 christos
3687 1.1 christos /* Note that we've changed the relocs, section contents, etc. */
3688 1.1 christos elf_section_data (sec)->relocs = internal_relocs;
3689 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
3690 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
3691 1.1 christos
3692 1.1 christos /* Fix the opcode. */
3693 1.1 christos bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
3694 1.1 christos
3695 1.1 christos /* Fix the relocation's type. */
3696 1.1 christos irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3697 1.1 christos R_MN10300_PCREL8);
3698 1.1 christos
3699 1.1 christos /* Delete one byte of data. */
3700 1.1 christos if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3701 1.1 christos irel->r_offset + 1, 1))
3702 1.1 christos goto error_return;
3703 1.1 christos
3704 1.1 christos /* That will change things, so, we should relax again.
3705 1.1 christos Note that this is not required, and it may be slow. */
3706 1.1 christos *again = TRUE;
3707 1.1 christos }
3708 1.1 christos }
3709 1.1 christos
3710 1.1 christos /* Try to eliminate an unconditional 8 bit pc-relative branch
3711 1.1 christos which immediately follows a conditional 8 bit pc-relative
3712 1.1 christos branch around the unconditional branch.
3713 1.1 christos
3714 1.1 christos original: new:
3715 1.1 christos bCC lab1 bCC' lab2
3716 1.1 christos bra lab2
3717 1.1 christos lab1: lab1:
3718 1.1 christos
3719 1.1 christos This happens when the bCC can't reach lab2 at assembly time,
3720 1.1 christos but due to other relaxations it can reach at link time. */
3721 1.1 christos if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
3722 1.1 christos {
3723 1.1 christos Elf_Internal_Rela *nrel;
3724 1.1 christos bfd_vma value = symval;
3725 1.1 christos unsigned char code;
3726 1.1 christos
3727 1.1 christos /* Deal with pc-relative gunk. */
3728 1.1 christos value -= (sec->output_section->vma + sec->output_offset);
3729 1.1 christos value -= irel->r_offset;
3730 1.1 christos value += irel->r_addend;
3731 1.1 christos
3732 1.1 christos /* Do nothing if this reloc is the last byte in the section. */
3733 1.1 christos if (irel->r_offset == sec->size)
3734 1.1 christos continue;
3735 1.1 christos
3736 1.1 christos /* See if the next instruction is an unconditional pc-relative
3737 1.1 christos branch, more often than not this test will fail, so we
3738 1.1 christos test it first to speed things up. */
3739 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
3740 1.1 christos if (code != 0xca)
3741 1.1 christos continue;
3742 1.1 christos
3743 1.1 christos /* Also make sure the next relocation applies to the next
3744 1.1 christos instruction and that it's a pc-relative 8 bit branch. */
3745 1.1 christos nrel = irel + 1;
3746 1.1 christos if (nrel == irelend
3747 1.1 christos || irel->r_offset + 2 != nrel->r_offset
3748 1.1 christos || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
3749 1.1 christos continue;
3750 1.1 christos
3751 1.1 christos /* Make sure our destination immediately follows the
3752 1.1 christos unconditional branch. */
3753 1.1 christos if (symval != (sec->output_section->vma + sec->output_offset
3754 1.1 christos + irel->r_offset + 3))
3755 1.1 christos continue;
3756 1.1 christos
3757 1.1 christos /* Now make sure we are a conditional branch. This may not
3758 1.1 christos be necessary, but why take the chance.
3759 1.1 christos
3760 1.1 christos Note these checks assume that R_MN10300_PCREL8 relocs
3761 1.1 christos only occur on bCC and bCCx insns. If they occured
3762 1.1 christos elsewhere, we'd need to know the start of this insn
3763 1.1 christos for this check to be accurate. */
3764 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3765 1.1 christos if (code != 0xc0 && code != 0xc1 && code != 0xc2
3766 1.1 christos && code != 0xc3 && code != 0xc4 && code != 0xc5
3767 1.1 christos && code != 0xc6 && code != 0xc7 && code != 0xc8
3768 1.1 christos && code != 0xc9 && code != 0xe8 && code != 0xe9
3769 1.1 christos && code != 0xea && code != 0xeb)
3770 1.1 christos continue;
3771 1.1 christos
3772 1.1 christos /* We also have to be sure there is no symbol/label
3773 1.1 christos at the unconditional branch. */
3774 1.1 christos if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
3775 1.1 christos irel->r_offset + 1))
3776 1.1 christos continue;
3777 1.1 christos
3778 1.1 christos /* Note that we've changed the relocs, section contents, etc. */
3779 1.1 christos elf_section_data (sec)->relocs = internal_relocs;
3780 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
3781 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
3782 1.1 christos
3783 1.1 christos /* Reverse the condition of the first branch. */
3784 1.1 christos switch (code)
3785 1.1 christos {
3786 1.1 christos case 0xc8:
3787 1.1 christos code = 0xc9;
3788 1.1 christos break;
3789 1.1 christos case 0xc9:
3790 1.1 christos code = 0xc8;
3791 1.1 christos break;
3792 1.1 christos case 0xc0:
3793 1.1 christos code = 0xc2;
3794 1.1 christos break;
3795 1.1 christos case 0xc2:
3796 1.1 christos code = 0xc0;
3797 1.1 christos break;
3798 1.1 christos case 0xc3:
3799 1.1 christos code = 0xc1;
3800 1.1 christos break;
3801 1.1 christos case 0xc1:
3802 1.1 christos code = 0xc3;
3803 1.1 christos break;
3804 1.1 christos case 0xc4:
3805 1.1 christos code = 0xc6;
3806 1.1 christos break;
3807 1.1 christos case 0xc6:
3808 1.1 christos code = 0xc4;
3809 1.1 christos break;
3810 1.1 christos case 0xc7:
3811 1.1 christos code = 0xc5;
3812 1.1 christos break;
3813 1.1 christos case 0xc5:
3814 1.1 christos code = 0xc7;
3815 1.1 christos break;
3816 1.1 christos case 0xe8:
3817 1.1 christos code = 0xe9;
3818 1.1 christos break;
3819 1.1 christos case 0x9d:
3820 1.1 christos code = 0xe8;
3821 1.1 christos break;
3822 1.1 christos case 0xea:
3823 1.1 christos code = 0xeb;
3824 1.1 christos break;
3825 1.1 christos case 0xeb:
3826 1.1 christos code = 0xea;
3827 1.1 christos break;
3828 1.1 christos }
3829 1.1 christos bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3830 1.1 christos
3831 1.1 christos /* Set the reloc type and symbol for the first branch
3832 1.1 christos from the second branch. */
3833 1.1 christos irel->r_info = nrel->r_info;
3834 1.1 christos
3835 1.1 christos /* Make the reloc for the second branch a null reloc. */
3836 1.1 christos nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3837 1.1 christos R_MN10300_NONE);
3838 1.1 christos
3839 1.1 christos /* Delete two bytes of data. */
3840 1.1 christos if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3841 1.1 christos irel->r_offset + 1, 2))
3842 1.1 christos goto error_return;
3843 1.1 christos
3844 1.1 christos /* That will change things, so, we should relax again.
3845 1.1 christos Note that this is not required, and it may be slow. */
3846 1.1 christos *again = TRUE;
3847 1.1 christos }
3848 1.1 christos
3849 1.1 christos /* Try to turn a 24 immediate, displacement or absolute address
3850 1.1 christos into a 8 immediate, displacement or absolute address. */
3851 1.1 christos if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
3852 1.1 christos {
3853 1.1 christos bfd_vma value = symval;
3854 1.1 christos value += irel->r_addend;
3855 1.1 christos
3856 1.1 christos /* See if the value will fit in 8 bits. */
3857 1.1 christos if ((long) value < 0x7f && (long) value > -0x80)
3858 1.1 christos {
3859 1.1 christos unsigned char code;
3860 1.1 christos
3861 1.1 christos /* AM33 insns which have 24 operands are 6 bytes long and
3862 1.1 christos will have 0xfd as the first byte. */
3863 1.1 christos
3864 1.1 christos /* Get the first opcode. */
3865 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3866 1.1 christos
3867 1.1 christos if (code == 0xfd)
3868 1.1 christos {
3869 1.1 christos /* Get the second opcode. */
3870 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3871 1.1 christos
3872 1.1 christos /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3873 1.1 christos equivalent instructions exists. */
3874 1.1 christos if (code != 0x6b && code != 0x7b
3875 1.1 christos && code != 0x8b && code != 0x9b
3876 1.1 christos && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3877 1.1 christos || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3878 1.1 christos || (code & 0x0f) == 0x0e))
3879 1.1 christos {
3880 1.1 christos /* Not safe if the high bit is on as relaxing may
3881 1.1 christos move the value out of high mem and thus not fit
3882 1.1 christos in a signed 8bit value. This is currently over
3883 1.1 christos conservative. */
3884 1.1 christos if ((value & 0x80) == 0)
3885 1.1 christos {
3886 1.1 christos /* Note that we've changed the relocation contents,
3887 1.1 christos etc. */
3888 1.1 christos elf_section_data (sec)->relocs = internal_relocs;
3889 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
3890 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
3891 1.1 christos
3892 1.1 christos /* Fix the opcode. */
3893 1.1 christos bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
3894 1.1 christos bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3895 1.1 christos
3896 1.1 christos /* Fix the relocation's type. */
3897 1.1 christos irel->r_info =
3898 1.1 christos ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3899 1.1 christos R_MN10300_8);
3900 1.1 christos
3901 1.1 christos /* Delete two bytes of data. */
3902 1.1 christos if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3903 1.1 christos irel->r_offset + 1, 2))
3904 1.1 christos goto error_return;
3905 1.1 christos
3906 1.1 christos /* That will change things, so, we should relax
3907 1.1 christos again. Note that this is not required, and it
3908 1.1 christos may be slow. */
3909 1.1 christos *again = TRUE;
3910 1.1 christos break;
3911 1.1 christos }
3912 1.1 christos }
3913 1.1 christos }
3914 1.1 christos }
3915 1.1 christos }
3916 1.1 christos
3917 1.1 christos /* Try to turn a 32bit immediate, displacement or absolute address
3918 1.1 christos into a 16bit immediate, displacement or absolute address. */
3919 1.1 christos if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
3920 1.1 christos || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
3921 1.1 christos || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3922 1.1 christos {
3923 1.1 christos bfd_vma value = symval;
3924 1.1 christos
3925 1.1 christos if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
3926 1.1 christos {
3927 1.1 christos asection * sgot;
3928 1.1.1.2 christos
3929 1.1 christos sgot = hash_table->root.sgot;
3930 1.1 christos if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
3931 1.1 christos {
3932 1.1 christos value = sgot->output_offset;
3933 1.1 christos
3934 1.1 christos if (h)
3935 1.1 christos value += h->root.got.offset;
3936 1.1 christos else
3937 1.1 christos value += (elf_local_got_offsets
3938 1.1 christos (abfd)[ELF32_R_SYM (irel->r_info)]);
3939 1.1 christos }
3940 1.1 christos else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3941 1.1 christos value -= sgot->output_section->vma;
3942 1.1 christos else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
3943 1.1 christos value = (sgot->output_section->vma
3944 1.1 christos - (sec->output_section->vma
3945 1.1 christos + sec->output_offset
3946 1.1 christos + irel->r_offset));
3947 1.1 christos else
3948 1.1 christos abort ();
3949 1.1 christos }
3950 1.1 christos
3951 1.1 christos value += irel->r_addend;
3952 1.1 christos
3953 1.1 christos /* See if the value will fit in 24 bits.
3954 1.1 christos We allow any 16bit match here. We prune those we can't
3955 1.1 christos handle below. */
3956 1.1 christos if ((long) value < 0x7fffff && (long) value > -0x800000)
3957 1.1 christos {
3958 1.1 christos unsigned char code;
3959 1.1 christos
3960 1.1 christos /* AM33 insns which have 32bit operands are 7 bytes long and
3961 1.1 christos will have 0xfe as the first byte. */
3962 1.1 christos
3963 1.1 christos /* Get the first opcode. */
3964 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3965 1.1 christos
3966 1.1 christos if (code == 0xfe)
3967 1.1 christos {
3968 1.1 christos /* Get the second opcode. */
3969 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3970 1.1 christos
3971 1.1 christos /* All the am33 32 -> 24 relaxing possibilities. */
3972 1.1 christos /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3973 1.1 christos equivalent instructions exists. */
3974 1.1 christos if (code != 0x6b && code != 0x7b
3975 1.1 christos && code != 0x8b && code != 0x9b
3976 1.1 christos && (ELF32_R_TYPE (irel->r_info)
3977 1.1 christos != (int) R_MN10300_GOTPC32)
3978 1.1 christos && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3979 1.1 christos || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3980 1.1 christos || (code & 0x0f) == 0x0e))
3981 1.1 christos {
3982 1.1 christos /* Not safe if the high bit is on as relaxing may
3983 1.1 christos move the value out of high mem and thus not fit
3984 1.1 christos in a signed 16bit value. This is currently over
3985 1.1 christos conservative. */
3986 1.1 christos if ((value & 0x8000) == 0)
3987 1.1 christos {
3988 1.1 christos /* Note that we've changed the relocation contents,
3989 1.1 christos etc. */
3990 1.1 christos elf_section_data (sec)->relocs = internal_relocs;
3991 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
3992 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
3993 1.1 christos
3994 1.1 christos /* Fix the opcode. */
3995 1.1 christos bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
3996 1.1 christos bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3997 1.1 christos
3998 1.1 christos /* Fix the relocation's type. */
3999 1.1 christos irel->r_info =
4000 1.1 christos ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4001 1.1 christos (ELF32_R_TYPE (irel->r_info)
4002 1.1 christos == (int) R_MN10300_GOTOFF32)
4003 1.1 christos ? R_MN10300_GOTOFF24
4004 1.1 christos : (ELF32_R_TYPE (irel->r_info)
4005 1.1 christos == (int) R_MN10300_GOT32)
4006 1.1 christos ? R_MN10300_GOT24 :
4007 1.1 christos R_MN10300_24);
4008 1.1 christos
4009 1.1 christos /* Delete one byte of data. */
4010 1.1 christos if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4011 1.1 christos irel->r_offset + 3, 1))
4012 1.1 christos goto error_return;
4013 1.1 christos
4014 1.1 christos /* That will change things, so, we should relax
4015 1.1 christos again. Note that this is not required, and it
4016 1.1 christos may be slow. */
4017 1.1 christos *again = TRUE;
4018 1.1 christos break;
4019 1.1 christos }
4020 1.1 christos }
4021 1.1 christos }
4022 1.1 christos }
4023 1.1 christos
4024 1.1 christos /* See if the value will fit in 16 bits.
4025 1.1 christos We allow any 16bit match here. We prune those we can't
4026 1.1 christos handle below. */
4027 1.1 christos if ((long) value < 0x7fff && (long) value > -0x8000)
4028 1.1 christos {
4029 1.1 christos unsigned char code;
4030 1.1 christos
4031 1.1 christos /* Most insns which have 32bit operands are 6 bytes long;
4032 1.1 christos exceptions are pcrel insns and bit insns.
4033 1.1 christos
4034 1.1 christos We handle pcrel insns above. We don't bother trying
4035 1.1 christos to handle the bit insns here.
4036 1.1 christos
4037 1.1 christos The first byte of the remaining insns will be 0xfc. */
4038 1.1 christos
4039 1.1 christos /* Get the first opcode. */
4040 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
4041 1.1 christos
4042 1.1 christos if (code != 0xfc)
4043 1.1 christos continue;
4044 1.1 christos
4045 1.1 christos /* Get the second opcode. */
4046 1.1 christos code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
4047 1.1 christos
4048 1.1 christos if ((code & 0xf0) < 0x80)
4049 1.1 christos switch (code & 0xf0)
4050 1.1 christos {
4051 1.1 christos /* mov (d32,am),dn -> mov (d32,am),dn
4052 1.1 christos mov dm,(d32,am) -> mov dn,(d32,am)
4053 1.1 christos mov (d32,am),an -> mov (d32,am),an
4054 1.1 christos mov dm,(d32,am) -> mov dn,(d32,am)
4055 1.1 christos movbu (d32,am),dn -> movbu (d32,am),dn
4056 1.1 christos movbu dm,(d32,am) -> movbu dn,(d32,am)
4057 1.1 christos movhu (d32,am),dn -> movhu (d32,am),dn
4058 1.1 christos movhu dm,(d32,am) -> movhu dn,(d32,am) */
4059 1.1 christos case 0x00:
4060 1.1 christos case 0x10:
4061 1.1 christos case 0x20:
4062 1.1 christos case 0x30:
4063 1.1 christos case 0x40:
4064 1.1 christos case 0x50:
4065 1.1 christos case 0x60:
4066 1.1 christos case 0x70:
4067 1.1 christos /* Not safe if the high bit is on as relaxing may
4068 1.1 christos move the value out of high mem and thus not fit
4069 1.1 christos in a signed 16bit value. */
4070 1.1 christos if (code == 0xcc
4071 1.1 christos && (value & 0x8000))
4072 1.1 christos continue;
4073 1.1 christos
4074 1.1 christos /* Note that we've changed the relocation contents, etc. */
4075 1.1 christos elf_section_data (sec)->relocs = internal_relocs;
4076 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
4077 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
4078 1.1 christos
4079 1.1 christos /* Fix the opcode. */
4080 1.1 christos bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
4081 1.1 christos bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
4082 1.1 christos
4083 1.1 christos /* Fix the relocation's type. */
4084 1.1 christos irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4085 1.1 christos (ELF32_R_TYPE (irel->r_info)
4086 1.1 christos == (int) R_MN10300_GOTOFF32)
4087 1.1 christos ? R_MN10300_GOTOFF16
4088 1.1 christos : (ELF32_R_TYPE (irel->r_info)
4089 1.1 christos == (int) R_MN10300_GOT32)
4090 1.1 christos ? R_MN10300_GOT16
4091 1.1 christos : (ELF32_R_TYPE (irel->r_info)
4092 1.1 christos == (int) R_MN10300_GOTPC32)
4093 1.1 christos ? R_MN10300_GOTPC16 :
4094 1.1 christos R_MN10300_16);
4095 1.1 christos
4096 1.1 christos /* Delete two bytes of data. */
4097 1.1 christos if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4098 1.1 christos irel->r_offset + 2, 2))
4099 1.1 christos goto error_return;
4100 1.1 christos
4101 1.1 christos /* That will change things, so, we should relax again.
4102 1.1 christos Note that this is not required, and it may be slow. */
4103 1.1 christos *again = TRUE;
4104 1.1 christos break;
4105 1.1 christos }
4106 1.1 christos else if ((code & 0xf0) == 0x80
4107 1.1 christos || (code & 0xf0) == 0x90)
4108 1.1 christos switch (code & 0xf3)
4109 1.1 christos {
4110 1.1 christos /* mov dn,(abs32) -> mov dn,(abs16)
4111 1.1 christos movbu dn,(abs32) -> movbu dn,(abs16)
4112 1.1 christos movhu dn,(abs32) -> movhu dn,(abs16) */
4113 1.1 christos case 0x81:
4114 1.1 christos case 0x82:
4115 1.1 christos case 0x83:
4116 1.1 christos /* Note that we've changed the relocation contents, etc. */
4117 1.1 christos elf_section_data (sec)->relocs = internal_relocs;
4118 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
4119 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
4120 1.1 christos
4121 1.1 christos if ((code & 0xf3) == 0x81)
4122 1.1 christos code = 0x01 + (code & 0x0c);
4123 1.1 christos else if ((code & 0xf3) == 0x82)
4124 1.1 christos code = 0x02 + (code & 0x0c);
4125 1.1 christos else if ((code & 0xf3) == 0x83)
4126 1.1 christos code = 0x03 + (code & 0x0c);
4127 1.1 christos else
4128 1.1 christos abort ();
4129 1.1 christos
4130 1.1 christos /* Fix the opcode. */
4131 1.1 christos bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
4132 1.1 christos
4133 1.1 christos /* Fix the relocation's type. */
4134 1.1 christos irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4135 1.1 christos (ELF32_R_TYPE (irel->r_info)
4136 1.1 christos == (int) R_MN10300_GOTOFF32)
4137 1.1 christos ? R_MN10300_GOTOFF16
4138 1.1 christos : (ELF32_R_TYPE (irel->r_info)
4139 1.1 christos == (int) R_MN10300_GOT32)
4140 1.1 christos ? R_MN10300_GOT16
4141 1.1 christos : (ELF32_R_TYPE (irel->r_info)
4142 1.1 christos == (int) R_MN10300_GOTPC32)
4143 1.1 christos ? R_MN10300_GOTPC16 :
4144 1.1 christos R_MN10300_16);
4145 1.1 christos
4146 1.1 christos /* The opcode got shorter too, so we have to fix the
4147 1.1 christos addend and offset too! */
4148 1.1 christos irel->r_offset -= 1;
4149 1.1 christos
4150 1.1 christos /* Delete three bytes of data. */
4151 1.1 christos if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4152 1.1 christos irel->r_offset + 1, 3))
4153 1.1 christos goto error_return;
4154 1.1 christos
4155 1.1 christos /* That will change things, so, we should relax again.
4156 1.1 christos Note that this is not required, and it may be slow. */
4157 1.1 christos *again = TRUE;
4158 1.1 christos break;
4159 1.1 christos
4160 1.1 christos /* mov am,(abs32) -> mov am,(abs16)
4161 1.1 christos mov am,(d32,sp) -> mov am,(d16,sp)
4162 1.1 christos mov dm,(d32,sp) -> mov dm,(d32,sp)
4163 1.1 christos movbu dm,(d32,sp) -> movbu dm,(d32,sp)
4164 1.1 christos movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
4165 1.1 christos case 0x80:
4166 1.1 christos case 0x90:
4167 1.1 christos case 0x91:
4168 1.1 christos case 0x92:
4169 1.1 christos case 0x93:
4170 1.1 christos /* sp-based offsets are zero-extended. */
4171 1.1 christos if (code >= 0x90 && code <= 0x93
4172 1.1 christos && (long) value < 0)
4173 1.1 christos continue;
4174 1.1 christos
4175 1.1 christos /* Note that we've changed the relocation contents, etc. */
4176 1.1 christos elf_section_data (sec)->relocs = internal_relocs;
4177 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
4178 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
4179 1.1 christos
4180 1.1 christos /* Fix the opcode. */
4181 1.1 christos bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
4182 1.1 christos bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
4183 1.1 christos
4184 1.1 christos /* Fix the relocation's type. */
4185 1.1 christos irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4186 1.1 christos (ELF32_R_TYPE (irel->r_info)
4187 1.1 christos == (int) R_MN10300_GOTOFF32)
4188 1.1 christos ? R_MN10300_GOTOFF16
4189 1.1 christos : (ELF32_R_TYPE (irel->r_info)
4190 1.1 christos == (int) R_MN10300_GOT32)
4191 1.1 christos ? R_MN10300_GOT16
4192 1.1 christos : (ELF32_R_TYPE (irel->r_info)
4193 1.1 christos == (int) R_MN10300_GOTPC32)
4194 1.1 christos ? R_MN10300_GOTPC16 :
4195 1.1 christos R_MN10300_16);
4196 1.1 christos
4197 1.1 christos /* Delete two bytes of data. */
4198 1.1 christos if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4199 1.1 christos irel->r_offset + 2, 2))
4200 1.1 christos goto error_return;
4201 1.1 christos
4202 1.1 christos /* That will change things, so, we should relax again.
4203 1.1 christos Note that this is not required, and it may be slow. */
4204 1.1 christos *again = TRUE;
4205 1.1 christos break;
4206 1.1 christos }
4207 1.1 christos else if ((code & 0xf0) < 0xf0)
4208 1.1 christos switch (code & 0xfc)
4209 1.1 christos {
4210 1.1 christos /* mov imm32,dn -> mov imm16,dn
4211 1.1 christos mov imm32,an -> mov imm16,an
4212 1.1 christos mov (abs32),dn -> mov (abs16),dn
4213 1.1 christos movbu (abs32),dn -> movbu (abs16),dn
4214 1.1 christos movhu (abs32),dn -> movhu (abs16),dn */
4215 1.1 christos case 0xcc:
4216 1.1 christos case 0xdc:
4217 1.1 christos case 0xa4:
4218 1.1 christos case 0xa8:
4219 1.1 christos case 0xac:
4220 1.1 christos /* Not safe if the high bit is on as relaxing may
4221 1.1 christos move the value out of high mem and thus not fit
4222 1.1 christos in a signed 16bit value. */
4223 1.1 christos if (code == 0xcc
4224 1.1 christos && (value & 0x8000))
4225 1.1 christos continue;
4226 1.1.1.2 christos
4227 1.1.1.2 christos /* "mov imm16, an" zero-extends the immediate. */
4228 1.1 christos if ((code & 0xfc) == 0xdc
4229 1.1 christos && (long) value < 0)
4230 1.1 christos continue;
4231 1.1 christos
4232 1.1 christos /* Note that we've changed the relocation contents, etc. */
4233 1.1 christos elf_section_data (sec)->relocs = internal_relocs;
4234 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
4235 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
4236 1.1 christos
4237 1.1 christos if ((code & 0xfc) == 0xcc)
4238 1.1 christos code = 0x2c + (code & 0x03);
4239 1.1 christos else if ((code & 0xfc) == 0xdc)
4240 1.1 christos code = 0x24 + (code & 0x03);
4241 1.1 christos else if ((code & 0xfc) == 0xa4)
4242 1.1 christos code = 0x30 + (code & 0x03);
4243 1.1 christos else if ((code & 0xfc) == 0xa8)
4244 1.1 christos code = 0x34 + (code & 0x03);
4245 1.1 christos else if ((code & 0xfc) == 0xac)
4246 1.1 christos code = 0x38 + (code & 0x03);
4247 1.1 christos else
4248 1.1 christos abort ();
4249 1.1 christos
4250 1.1 christos /* Fix the opcode. */
4251 1.1 christos bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
4252 1.1 christos
4253 1.1 christos /* Fix the relocation's type. */
4254 1.1 christos irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4255 1.1 christos (ELF32_R_TYPE (irel->r_info)
4256 1.1 christos == (int) R_MN10300_GOTOFF32)
4257 1.1 christos ? R_MN10300_GOTOFF16
4258 1.1 christos : (ELF32_R_TYPE (irel->r_info)
4259 1.1 christos == (int) R_MN10300_GOT32)
4260 1.1 christos ? R_MN10300_GOT16
4261 1.1 christos : (ELF32_R_TYPE (irel->r_info)
4262 1.1 christos == (int) R_MN10300_GOTPC32)
4263 1.1 christos ? R_MN10300_GOTPC16 :
4264 1.1 christos R_MN10300_16);
4265 1.1 christos
4266 1.1 christos /* The opcode got shorter too, so we have to fix the
4267 1.1 christos addend and offset too! */
4268 1.1 christos irel->r_offset -= 1;
4269 1.1 christos
4270 1.1 christos /* Delete three bytes of data. */
4271 1.1 christos if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4272 1.1 christos irel->r_offset + 1, 3))
4273 1.1 christos goto error_return;
4274 1.1 christos
4275 1.1 christos /* That will change things, so, we should relax again.
4276 1.1 christos Note that this is not required, and it may be slow. */
4277 1.1 christos *again = TRUE;
4278 1.1 christos break;
4279 1.1 christos
4280 1.1 christos /* mov (abs32),an -> mov (abs16),an
4281 1.1 christos mov (d32,sp),an -> mov (d16,sp),an
4282 1.1 christos mov (d32,sp),dn -> mov (d16,sp),dn
4283 1.1 christos movbu (d32,sp),dn -> movbu (d16,sp),dn
4284 1.1 christos movhu (d32,sp),dn -> movhu (d16,sp),dn
4285 1.1 christos add imm32,dn -> add imm16,dn
4286 1.1 christos cmp imm32,dn -> cmp imm16,dn
4287 1.1 christos add imm32,an -> add imm16,an
4288 1.1 christos cmp imm32,an -> cmp imm16,an
4289 1.1 christos and imm32,dn -> and imm16,dn
4290 1.1 christos or imm32,dn -> or imm16,dn
4291 1.1 christos xor imm32,dn -> xor imm16,dn
4292 1.1 christos btst imm32,dn -> btst imm16,dn */
4293 1.1 christos
4294 1.1 christos case 0xa0:
4295 1.1 christos case 0xb0:
4296 1.1 christos case 0xb1:
4297 1.1 christos case 0xb2:
4298 1.1 christos case 0xb3:
4299 1.1 christos case 0xc0:
4300 1.1 christos case 0xc8:
4301 1.1 christos
4302 1.1 christos case 0xd0:
4303 1.1 christos case 0xd8:
4304 1.1 christos case 0xe0:
4305 1.1 christos case 0xe1:
4306 1.1 christos case 0xe2:
4307 1.1 christos case 0xe3:
4308 1.1 christos /* cmp imm16, an zero-extends the immediate. */
4309 1.1 christos if (code == 0xdc
4310 1.1 christos && (long) value < 0)
4311 1.1 christos continue;
4312 1.1 christos
4313 1.1 christos /* So do sp-based offsets. */
4314 1.1 christos if (code >= 0xb0 && code <= 0xb3
4315 1.1 christos && (long) value < 0)
4316 1.1 christos continue;
4317 1.1 christos
4318 1.1 christos /* Note that we've changed the relocation contents, etc. */
4319 1.1 christos elf_section_data (sec)->relocs = internal_relocs;
4320 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
4321 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
4322 1.1 christos
4323 1.1 christos /* Fix the opcode. */
4324 1.1 christos bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
4325 1.1 christos bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
4326 1.1 christos
4327 1.1 christos /* Fix the relocation's type. */
4328 1.1 christos irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4329 1.1 christos (ELF32_R_TYPE (irel->r_info)
4330 1.1 christos == (int) R_MN10300_GOTOFF32)
4331 1.1 christos ? R_MN10300_GOTOFF16
4332 1.1 christos : (ELF32_R_TYPE (irel->r_info)
4333 1.1 christos == (int) R_MN10300_GOT32)
4334 1.1 christos ? R_MN10300_GOT16
4335 1.1 christos : (ELF32_R_TYPE (irel->r_info)
4336 1.1 christos == (int) R_MN10300_GOTPC32)
4337 1.1 christos ? R_MN10300_GOTPC16 :
4338 1.1 christos R_MN10300_16);
4339 1.1 christos
4340 1.1 christos /* Delete two bytes of data. */
4341 1.1 christos if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4342 1.1 christos irel->r_offset + 2, 2))
4343 1.1 christos goto error_return;
4344 1.1 christos
4345 1.1 christos /* That will change things, so, we should relax again.
4346 1.1 christos Note that this is not required, and it may be slow. */
4347 1.1 christos *again = TRUE;
4348 1.1 christos break;
4349 1.1 christos }
4350 1.1 christos else if (code == 0xfe)
4351 1.1 christos {
4352 1.1 christos /* add imm32,sp -> add imm16,sp */
4353 1.1 christos
4354 1.1 christos /* Note that we've changed the relocation contents, etc. */
4355 1.1 christos elf_section_data (sec)->relocs = internal_relocs;
4356 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
4357 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
4358 1.1 christos
4359 1.1 christos /* Fix the opcode. */
4360 1.1 christos bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
4361 1.1 christos bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
4362 1.1 christos
4363 1.1 christos /* Fix the relocation's type. */
4364 1.1 christos irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4365 1.1 christos (ELF32_R_TYPE (irel->r_info)
4366 1.1 christos == (int) R_MN10300_GOT32)
4367 1.1 christos ? R_MN10300_GOT16
4368 1.1 christos : (ELF32_R_TYPE (irel->r_info)
4369 1.1 christos == (int) R_MN10300_GOTOFF32)
4370 1.1 christos ? R_MN10300_GOTOFF16
4371 1.1 christos : (ELF32_R_TYPE (irel->r_info)
4372 1.1 christos == (int) R_MN10300_GOTPC32)
4373 1.1 christos ? R_MN10300_GOTPC16 :
4374 1.1 christos R_MN10300_16);
4375 1.1 christos
4376 1.1 christos /* Delete two bytes of data. */
4377 1.1 christos if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4378 1.1 christos irel->r_offset + 2, 2))
4379 1.1 christos goto error_return;
4380 1.1 christos
4381 1.1 christos /* That will change things, so, we should relax again.
4382 1.1 christos Note that this is not required, and it may be slow. */
4383 1.1 christos *again = TRUE;
4384 1.1 christos break;
4385 1.1 christos }
4386 1.1 christos }
4387 1.1 christos }
4388 1.1 christos }
4389 1.1 christos
4390 1.1 christos if (isymbuf != NULL
4391 1.1 christos && symtab_hdr->contents != (unsigned char *) isymbuf)
4392 1.1 christos {
4393 1.1 christos if (! link_info->keep_memory)
4394 1.1 christos free (isymbuf);
4395 1.1 christos else
4396 1.1 christos {
4397 1.1 christos /* Cache the symbols for elf_link_input_bfd. */
4398 1.1 christos symtab_hdr->contents = (unsigned char *) isymbuf;
4399 1.1 christos }
4400 1.1 christos }
4401 1.1 christos
4402 1.1 christos if (contents != NULL
4403 1.1 christos && elf_section_data (sec)->this_hdr.contents != contents)
4404 1.1 christos {
4405 1.1 christos if (! link_info->keep_memory)
4406 1.1 christos free (contents);
4407 1.1 christos else
4408 1.1 christos {
4409 1.1 christos /* Cache the section contents for elf_link_input_bfd. */
4410 1.1 christos elf_section_data (sec)->this_hdr.contents = contents;
4411 1.1 christos }
4412 1.1 christos }
4413 1.1 christos
4414 1.1 christos if (internal_relocs != NULL
4415 1.1 christos && elf_section_data (sec)->relocs != internal_relocs)
4416 1.1 christos free (internal_relocs);
4417 1.1 christos
4418 1.1 christos return TRUE;
4419 1.1 christos
4420 1.1 christos error_return:
4421 1.1 christos if (isymbuf != NULL
4422 1.1 christos && symtab_hdr->contents != (unsigned char *) isymbuf)
4423 1.1 christos free (isymbuf);
4424 1.1 christos if (contents != NULL
4425 1.1 christos && elf_section_data (section)->this_hdr.contents != contents)
4426 1.1 christos free (contents);
4427 1.1 christos if (internal_relocs != NULL
4428 1.1 christos && elf_section_data (section)->relocs != internal_relocs)
4429 1.1 christos free (internal_relocs);
4430 1.1 christos
4431 1.1 christos return FALSE;
4432 1.1 christos }
4433 1.1 christos
4434 1.1 christos /* This is a version of bfd_generic_get_relocated_section_contents
4435 1.1 christos which uses mn10300_elf_relocate_section. */
4436 1.1 christos
4437 1.1 christos static bfd_byte *
4438 1.1 christos mn10300_elf_get_relocated_section_contents (bfd *output_bfd,
4439 1.1 christos struct bfd_link_info *link_info,
4440 1.1 christos struct bfd_link_order *link_order,
4441 1.1 christos bfd_byte *data,
4442 1.1 christos bfd_boolean relocatable,
4443 1.1 christos asymbol **symbols)
4444 1.1 christos {
4445 1.1 christos Elf_Internal_Shdr *symtab_hdr;
4446 1.1 christos asection *input_section = link_order->u.indirect.section;
4447 1.1 christos bfd *input_bfd = input_section->owner;
4448 1.1 christos asection **sections = NULL;
4449 1.1 christos Elf_Internal_Rela *internal_relocs = NULL;
4450 1.1 christos Elf_Internal_Sym *isymbuf = NULL;
4451 1.1 christos
4452 1.1 christos /* We only need to handle the case of relaxing, or of having a
4453 1.1 christos particular set of section contents, specially. */
4454 1.1 christos if (relocatable
4455 1.1 christos || elf_section_data (input_section)->this_hdr.contents == NULL)
4456 1.1 christos return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
4457 1.1 christos link_order, data,
4458 1.1 christos relocatable,
4459 1.1 christos symbols);
4460 1.1 christos
4461 1.1 christos symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4462 1.1 christos
4463 1.1 christos memcpy (data, elf_section_data (input_section)->this_hdr.contents,
4464 1.1 christos (size_t) input_section->size);
4465 1.1 christos
4466 1.1 christos if ((input_section->flags & SEC_RELOC) != 0
4467 1.1 christos && input_section->reloc_count > 0)
4468 1.1 christos {
4469 1.1 christos asection **secpp;
4470 1.1 christos Elf_Internal_Sym *isym, *isymend;
4471 1.1 christos bfd_size_type amt;
4472 1.1 christos
4473 1.1 christos internal_relocs = _bfd_elf_link_read_relocs (input_bfd, input_section,
4474 1.1 christos NULL, NULL, FALSE);
4475 1.1 christos if (internal_relocs == NULL)
4476 1.1 christos goto error_return;
4477 1.1 christos
4478 1.1 christos if (symtab_hdr->sh_info != 0)
4479 1.1 christos {
4480 1.1 christos isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4481 1.1 christos if (isymbuf == NULL)
4482 1.1 christos isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
4483 1.1 christos symtab_hdr->sh_info, 0,
4484 1.1 christos NULL, NULL, NULL);
4485 1.1 christos if (isymbuf == NULL)
4486 1.1 christos goto error_return;
4487 1.1 christos }
4488 1.1 christos
4489 1.1 christos amt = symtab_hdr->sh_info;
4490 1.1 christos amt *= sizeof (asection *);
4491 1.1 christos sections = bfd_malloc (amt);
4492 1.1 christos if (sections == NULL && amt != 0)
4493 1.1 christos goto error_return;
4494 1.1 christos
4495 1.1 christos isymend = isymbuf + symtab_hdr->sh_info;
4496 1.1 christos for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
4497 1.1 christos {
4498 1.1 christos asection *isec;
4499 1.1 christos
4500 1.1 christos if (isym->st_shndx == SHN_UNDEF)
4501 1.1 christos isec = bfd_und_section_ptr;
4502 1.1 christos else if (isym->st_shndx == SHN_ABS)
4503 1.1 christos isec = bfd_abs_section_ptr;
4504 1.1 christos else if (isym->st_shndx == SHN_COMMON)
4505 1.1 christos isec = bfd_com_section_ptr;
4506 1.1 christos else
4507 1.1 christos isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
4508 1.1 christos
4509 1.1 christos *secpp = isec;
4510 1.1 christos }
4511 1.1 christos
4512 1.1 christos if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
4513 1.1 christos input_section, data, internal_relocs,
4514 1.1 christos isymbuf, sections))
4515 1.1 christos goto error_return;
4516 1.1 christos
4517 1.1 christos if (sections != NULL)
4518 1.1 christos free (sections);
4519 1.1 christos if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
4520 1.1 christos free (isymbuf);
4521 1.1 christos if (internal_relocs != elf_section_data (input_section)->relocs)
4522 1.1 christos free (internal_relocs);
4523 1.1 christos }
4524 1.1 christos
4525 1.1 christos return data;
4526 1.1 christos
4527 1.1 christos error_return:
4528 1.1 christos if (sections != NULL)
4529 1.1 christos free (sections);
4530 1.1 christos if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
4531 1.1 christos free (isymbuf);
4532 1.1 christos if (internal_relocs != NULL
4533 1.1 christos && internal_relocs != elf_section_data (input_section)->relocs)
4534 1.1 christos free (internal_relocs);
4535 1.1 christos return NULL;
4536 1.1 christos }
4537 1.1 christos
4538 1.1 christos /* Assorted hash table functions. */
4539 1.1 christos
4540 1.1 christos /* Initialize an entry in the link hash table. */
4541 1.1 christos
4542 1.1 christos /* Create an entry in an MN10300 ELF linker hash table. */
4543 1.1 christos
4544 1.1 christos static struct bfd_hash_entry *
4545 1.1 christos elf32_mn10300_link_hash_newfunc (struct bfd_hash_entry *entry,
4546 1.1 christos struct bfd_hash_table *table,
4547 1.1 christos const char *string)
4548 1.1 christos {
4549 1.1 christos struct elf32_mn10300_link_hash_entry *ret =
4550 1.1 christos (struct elf32_mn10300_link_hash_entry *) entry;
4551 1.1 christos
4552 1.1 christos /* Allocate the structure if it has not already been allocated by a
4553 1.1 christos subclass. */
4554 1.1 christos if (ret == NULL)
4555 1.1 christos ret = (struct elf32_mn10300_link_hash_entry *)
4556 1.1 christos bfd_hash_allocate (table, sizeof (* ret));
4557 1.1 christos if (ret == NULL)
4558 1.1 christos return (struct bfd_hash_entry *) ret;
4559 1.1 christos
4560 1.1 christos /* Call the allocation method of the superclass. */
4561 1.1 christos ret = (struct elf32_mn10300_link_hash_entry *)
4562 1.1 christos _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
4563 1.1 christos table, string);
4564 1.1 christos if (ret != NULL)
4565 1.1 christos {
4566 1.1 christos ret->direct_calls = 0;
4567 1.1 christos ret->stack_size = 0;
4568 1.1 christos ret->movm_args = 0;
4569 1.1 christos ret->movm_stack_size = 0;
4570 1.1 christos ret->flags = 0;
4571 1.1.1.2 christos ret->value = 0;
4572 1.1 christos ret->tls_type = GOT_UNKNOWN;
4573 1.1 christos }
4574 1.1 christos
4575 1.1 christos return (struct bfd_hash_entry *) ret;
4576 1.1 christos }
4577 1.1.1.2 christos
4578 1.1.1.2 christos static void
4579 1.1.1.2 christos _bfd_mn10300_copy_indirect_symbol (struct bfd_link_info * info,
4580 1.1.1.2 christos struct elf_link_hash_entry * dir,
4581 1.1.1.2 christos struct elf_link_hash_entry * ind)
4582 1.1.1.2 christos {
4583 1.1.1.2 christos struct elf32_mn10300_link_hash_entry * edir;
4584 1.1.1.2 christos struct elf32_mn10300_link_hash_entry * eind;
4585 1.1.1.2 christos
4586 1.1.1.2 christos edir = elf_mn10300_hash_entry (dir);
4587 1.1.1.2 christos eind = elf_mn10300_hash_entry (ind);
4588 1.1.1.2 christos
4589 1.1.1.2 christos if (ind->root.type == bfd_link_hash_indirect
4590 1.1.1.2 christos && dir->got.refcount <= 0)
4591 1.1.1.2 christos {
4592 1.1.1.2 christos edir->tls_type = eind->tls_type;
4593 1.1.1.2 christos eind->tls_type = GOT_UNKNOWN;
4594 1.1.1.2 christos }
4595 1.1.1.2 christos edir->direct_calls = eind->direct_calls;
4596 1.1.1.2 christos edir->stack_size = eind->stack_size;
4597 1.1.1.2 christos edir->movm_args = eind->movm_args;
4598 1.1.1.2 christos edir->movm_stack_size = eind->movm_stack_size;
4599 1.1.1.2 christos edir->flags = eind->flags;
4600 1.1.1.2 christos
4601 1.1.1.2 christos _bfd_elf_link_hash_copy_indirect (info, dir, ind);
4602 1.1.1.2 christos }
4603 1.1.1.4 christos
4604 1.1.1.4 christos /* Destroy an mn10300 ELF linker hash table. */
4605 1.1.1.4 christos
4606 1.1.1.4 christos static void
4607 1.1.1.4 christos elf32_mn10300_link_hash_table_free (bfd *obfd)
4608 1.1.1.4 christos {
4609 1.1.1.4 christos struct elf32_mn10300_link_hash_table *ret
4610 1.1.1.4 christos = (struct elf32_mn10300_link_hash_table *) obfd->link.hash;
4611 1.1.1.4 christos
4612 1.1.1.4 christos obfd->link.hash = &ret->static_hash_table->root.root;
4613 1.1.1.4 christos _bfd_elf_link_hash_table_free (obfd);
4614 1.1.1.4 christos obfd->is_linker_output = TRUE;
4615 1.1.1.4 christos obfd->link.hash = &ret->root.root;
4616 1.1.1.4 christos _bfd_elf_link_hash_table_free (obfd);
4617 1.1.1.4 christos }
4618 1.1 christos
4619 1.1 christos /* Create an mn10300 ELF linker hash table. */
4620 1.1 christos
4621 1.1 christos static struct bfd_link_hash_table *
4622 1.1 christos elf32_mn10300_link_hash_table_create (bfd *abfd)
4623 1.1 christos {
4624 1.1 christos struct elf32_mn10300_link_hash_table *ret;
4625 1.1 christos bfd_size_type amt = sizeof (* ret);
4626 1.1.1.2 christos
4627 1.1 christos ret = bfd_zmalloc (amt);
4628 1.1 christos if (ret == NULL)
4629 1.1 christos return NULL;
4630 1.1 christos
4631 1.1.1.2 christos amt = sizeof (struct elf_link_hash_table);
4632 1.1 christos ret->static_hash_table = bfd_zmalloc (amt);
4633 1.1 christos if (ret->static_hash_table == NULL)
4634 1.1 christos {
4635 1.1 christos free (ret);
4636 1.1 christos return NULL;
4637 1.1 christos }
4638 1.1 christos
4639 1.1 christos if (!_bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
4640 1.1 christos elf32_mn10300_link_hash_newfunc,
4641 1.1 christos sizeof (struct elf32_mn10300_link_hash_entry),
4642 1.1 christos MN10300_ELF_DATA))
4643 1.1 christos {
4644 1.1 christos free (ret->static_hash_table);
4645 1.1 christos free (ret);
4646 1.1 christos return NULL;
4647 1.1 christos }
4648 1.1.1.4 christos
4649 1.1.1.4 christos abfd->is_linker_output = FALSE;
4650 1.1.1.4 christos abfd->link.hash = NULL;
4651 1.1.1.4 christos if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
4652 1.1.1.4 christos elf32_mn10300_link_hash_newfunc,
4653 1.1.1.4 christos sizeof (struct elf32_mn10300_link_hash_entry),
4654 1.1.1.4 christos MN10300_ELF_DATA))
4655 1.1.1.4 christos {
4656 1.1.1.4 christos abfd->is_linker_output = TRUE;
4657 1.1.1.4 christos abfd->link.hash = &ret->static_hash_table->root.root;
4658 1.1.1.4 christos _bfd_elf_link_hash_table_free (abfd);
4659 1.1.1.4 christos free (ret);
4660 1.1.1.4 christos return NULL;
4661 1.1.1.4 christos }
4662 1.1 christos ret->root.root.hash_table_free = elf32_mn10300_link_hash_table_free;
4663 1.1.1.4 christos
4664 1.1 christos ret->tls_ldm_got.offset = -1;
4665 1.1.1.4 christos
4666 1.1 christos return & ret->root.root;
4667 1.1 christos }
4668 1.1 christos
4669 1.1 christos static unsigned long
4670 1.1 christos elf_mn10300_mach (flagword flags)
4671 1.1 christos {
4672 1.1 christos switch (flags & EF_MN10300_MACH)
4673 1.1 christos {
4674 1.1 christos case E_MN10300_MACH_MN10300:
4675 1.1 christos default:
4676 1.1 christos return bfd_mach_mn10300;
4677 1.1 christos
4678 1.1 christos case E_MN10300_MACH_AM33:
4679 1.1 christos return bfd_mach_am33;
4680 1.1 christos
4681 1.1 christos case E_MN10300_MACH_AM33_2:
4682 1.1 christos return bfd_mach_am33_2;
4683 1.1 christos }
4684 1.1 christos }
4685 1.1 christos
4686 1.1 christos /* The final processing done just before writing out a MN10300 ELF object
4687 1.1 christos file. This gets the MN10300 architecture right based on the machine
4688 1.1 christos number. */
4689 1.1 christos
4690 1.1 christos static void
4691 1.1 christos _bfd_mn10300_elf_final_write_processing (bfd *abfd,
4692 1.1 christos bfd_boolean linker ATTRIBUTE_UNUSED)
4693 1.1 christos {
4694 1.1 christos unsigned long val;
4695 1.1 christos
4696 1.1 christos switch (bfd_get_mach (abfd))
4697 1.1 christos {
4698 1.1 christos default:
4699 1.1 christos case bfd_mach_mn10300:
4700 1.1 christos val = E_MN10300_MACH_MN10300;
4701 1.1 christos break;
4702 1.1 christos
4703 1.1 christos case bfd_mach_am33:
4704 1.1 christos val = E_MN10300_MACH_AM33;
4705 1.1 christos break;
4706 1.1 christos
4707 1.1 christos case bfd_mach_am33_2:
4708 1.1 christos val = E_MN10300_MACH_AM33_2;
4709 1.1 christos break;
4710 1.1 christos }
4711 1.1 christos
4712 1.1 christos elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
4713 1.1 christos elf_elfheader (abfd)->e_flags |= val;
4714 1.1 christos }
4715 1.1 christos
4716 1.1 christos static bfd_boolean
4717 1.1 christos _bfd_mn10300_elf_object_p (bfd *abfd)
4718 1.1 christos {
4719 1.1 christos bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
4720 1.1 christos elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
4721 1.1 christos return TRUE;
4722 1.1 christos }
4723 1.1 christos
4724 1.1 christos /* Merge backend specific data from an object file to the output
4725 1.1 christos object file when linking. */
4726 1.1 christos
4727 1.1 christos static bfd_boolean
4728 1.1 christos _bfd_mn10300_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
4729 1.1 christos {
4730 1.1 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4731 1.1 christos || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4732 1.1 christos return TRUE;
4733 1.1 christos
4734 1.1 christos if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4735 1.1 christos && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
4736 1.1 christos {
4737 1.1 christos if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4738 1.1 christos bfd_get_mach (ibfd)))
4739 1.1 christos return FALSE;
4740 1.1 christos }
4741 1.1 christos
4742 1.1 christos return TRUE;
4743 1.1 christos }
4744 1.1 christos
4745 1.1 christos #define PLT0_ENTRY_SIZE 15
4746 1.1 christos #define PLT_ENTRY_SIZE 20
4747 1.1 christos #define PIC_PLT_ENTRY_SIZE 24
4748 1.1 christos
4749 1.1 christos static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] =
4750 1.1 christos {
4751 1.1 christos 0xfc, 0xa0, 0, 0, 0, 0, /* mov (.got+8),a0 */
4752 1.1 christos 0xfe, 0xe, 0x10, 0, 0, 0, 0, /* mov (.got+4),r1 */
4753 1.1 christos 0xf0, 0xf4, /* jmp (a0) */
4754 1.1 christos };
4755 1.1 christos
4756 1.1 christos static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] =
4757 1.1 christos {
4758 1.1 christos 0xfc, 0xa0, 0, 0, 0, 0, /* mov (nameN@GOT + .got),a0 */
4759 1.1 christos 0xf0, 0xf4, /* jmp (a0) */
4760 1.1 christos 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
4761 1.1 christos 0xdc, 0, 0, 0, 0, /* jmp .plt0 */
4762 1.1 christos };
4763 1.1 christos
4764 1.1 christos static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] =
4765 1.1 christos {
4766 1.1 christos 0xfc, 0x22, 0, 0, 0, 0, /* mov (nameN@GOT,a2),a0 */
4767 1.1 christos 0xf0, 0xf4, /* jmp (a0) */
4768 1.1 christos 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
4769 1.1 christos 0xf8, 0x22, 8, /* mov (8,a2),a0 */
4770 1.1 christos 0xfb, 0xa, 0x1a, 4, /* mov (4,a2),r1 */
4771 1.1 christos 0xf0, 0xf4, /* jmp (a0) */
4772 1.1 christos };
4773 1.1 christos
4774 1.1 christos /* Return size of the first PLT entry. */
4775 1.1.1.6 christos #define elf_mn10300_sizeof_plt0(info) \
4776 1.1 christos (bfd_link_pic (info) ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
4777 1.1 christos
4778 1.1 christos /* Return size of a PLT entry. */
4779 1.1.1.6 christos #define elf_mn10300_sizeof_plt(info) \
4780 1.1 christos (bfd_link_pic (info) ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
4781 1.1 christos
4782 1.1 christos /* Return offset of the PLT0 address in an absolute PLT entry. */
4783 1.1 christos #define elf_mn10300_plt_plt0_offset(info) 16
4784 1.1 christos
4785 1.1 christos /* Return offset of the linker in PLT0 entry. */
4786 1.1 christos #define elf_mn10300_plt0_linker_offset(info) 2
4787 1.1 christos
4788 1.1 christos /* Return offset of the GOT id in PLT0 entry. */
4789 1.1 christos #define elf_mn10300_plt0_gotid_offset(info) 9
4790 1.1 christos
4791 1.1 christos /* Return offset of the temporary in PLT entry. */
4792 1.1 christos #define elf_mn10300_plt_temp_offset(info) 8
4793 1.1 christos
4794 1.1 christos /* Return offset of the symbol in PLT entry. */
4795 1.1 christos #define elf_mn10300_plt_symbol_offset(info) 2
4796 1.1 christos
4797 1.1 christos /* Return offset of the relocation in PLT entry. */
4798 1.1 christos #define elf_mn10300_plt_reloc_offset(info) 11
4799 1.1 christos
4800 1.1 christos /* The name of the dynamic interpreter. This is put in the .interp
4801 1.1 christos section. */
4802 1.1 christos
4803 1.1 christos #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
4804 1.1 christos
4805 1.1 christos /* Create dynamic sections when linking against a dynamic object. */
4806 1.1 christos
4807 1.1 christos static bfd_boolean
4808 1.1 christos _bfd_mn10300_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
4809 1.1 christos {
4810 1.1 christos flagword flags;
4811 1.1 christos asection * s;
4812 1.1.1.2 christos const struct elf_backend_data * bed = get_elf_backend_data (abfd);
4813 1.1 christos struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info);
4814 1.1 christos int ptralign = 0;
4815 1.1 christos
4816 1.1 christos switch (bed->s->arch_size)
4817 1.1 christos {
4818 1.1 christos case 32:
4819 1.1 christos ptralign = 2;
4820 1.1 christos break;
4821 1.1 christos
4822 1.1 christos case 64:
4823 1.1 christos ptralign = 3;
4824 1.1 christos break;
4825 1.1 christos
4826 1.1 christos default:
4827 1.1 christos bfd_set_error (bfd_error_bad_value);
4828 1.1 christos return FALSE;
4829 1.1 christos }
4830 1.1 christos
4831 1.1 christos /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4832 1.1 christos .rel[a].bss sections. */
4833 1.1 christos flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4834 1.1 christos | SEC_LINKER_CREATED);
4835 1.1.1.2 christos
4836 1.1.1.2 christos s = bfd_make_section_anyway_with_flags (abfd,
4837 1.1.1.2 christos (bed->default_use_rela_p
4838 1.1.1.2 christos ? ".rela.plt" : ".rel.plt"),
4839 1.1.1.2 christos flags | SEC_READONLY);
4840 1.1 christos htab->root.srelplt = s;
4841 1.1 christos if (s == NULL
4842 1.1 christos || ! bfd_set_section_alignment (abfd, s, ptralign))
4843 1.1 christos return FALSE;
4844 1.1 christos
4845 1.1 christos if (! _bfd_mn10300_elf_create_got_section (abfd, info))
4846 1.1 christos return FALSE;
4847 1.1 christos
4848 1.1 christos if (bed->want_dynbss)
4849 1.1 christos {
4850 1.1 christos /* The .dynbss section is a place to put symbols which are defined
4851 1.1 christos by dynamic objects, are referenced by regular objects, and are
4852 1.1 christos not functions. We must allocate space for them in the process
4853 1.1 christos image and use a R_*_COPY reloc to tell the dynamic linker to
4854 1.1 christos initialize them at run time. The linker script puts the .dynbss
4855 1.1.1.2 christos section into the .bss section of the final image. */
4856 1.1.1.2 christos s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
4857 1.1 christos SEC_ALLOC | SEC_LINKER_CREATED);
4858 1.1 christos if (s == NULL)
4859 1.1 christos return FALSE;
4860 1.1 christos
4861 1.1 christos /* The .rel[a].bss section holds copy relocs. This section is not
4862 1.1 christos normally needed. We need to create it here, though, so that the
4863 1.1 christos linker will map it to an output section. We can't just create it
4864 1.1 christos only if we need it, because we will not know whether we need it
4865 1.1 christos until we have seen all the input files, and the first time the
4866 1.1 christos main linker code calls BFD after examining all the input files
4867 1.1 christos (size_dynamic_sections) the input sections have already been
4868 1.1 christos mapped to the output sections. If the section turns out not to
4869 1.1 christos be needed, we can discard it later. We will never need this
4870 1.1 christos section when generating a shared object, since they do not use
4871 1.1.1.6 christos copy relocs. */
4872 1.1 christos if (! bfd_link_pic (info))
4873 1.1.1.2 christos {
4874 1.1.1.2 christos s = bfd_make_section_anyway_with_flags (abfd,
4875 1.1.1.2 christos (bed->default_use_rela_p
4876 1.1.1.2 christos ? ".rela.bss" : ".rel.bss"),
4877 1.1 christos flags | SEC_READONLY);
4878 1.1 christos if (s == NULL
4879 1.1 christos || ! bfd_set_section_alignment (abfd, s, ptralign))
4880 1.1 christos return FALSE;
4881 1.1 christos }
4882 1.1 christos }
4883 1.1 christos
4884 1.1 christos return TRUE;
4885 1.1 christos }
4886 1.1 christos
4887 1.1 christos /* Adjust a symbol defined by a dynamic object and referenced by a
4889 1.1 christos regular object. The current definition is in some section of the
4890 1.1 christos dynamic object, but we're not including those sections. We have to
4891 1.1 christos change the definition to something the rest of the link can
4892 1.1 christos understand. */
4893 1.1 christos
4894 1.1 christos static bfd_boolean
4895 1.1 christos _bfd_mn10300_elf_adjust_dynamic_symbol (struct bfd_link_info * info,
4896 1.1.1.2 christos struct elf_link_hash_entry * h)
4897 1.1 christos {
4898 1.1 christos struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info);
4899 1.1 christos bfd * dynobj;
4900 1.1.1.2 christos asection * s;
4901 1.1 christos
4902 1.1 christos dynobj = htab->root.dynobj;
4903 1.1 christos
4904 1.1 christos /* Make sure we know what is going on here. */
4905 1.1 christos BFD_ASSERT (dynobj != NULL
4906 1.1 christos && (h->needs_plt
4907 1.1 christos || h->u.weakdef != NULL
4908 1.1 christos || (h->def_dynamic
4909 1.1 christos && h->ref_regular
4910 1.1 christos && !h->def_regular)));
4911 1.1 christos
4912 1.1 christos /* If this is a function, put it in the procedure linkage table. We
4913 1.1 christos will fill in the contents of the procedure linkage table later,
4914 1.1 christos when we know the address of the .got section. */
4915 1.1 christos if (h->type == STT_FUNC
4916 1.1.1.6 christos || h->needs_plt)
4917 1.1 christos {
4918 1.1 christos if (! bfd_link_pic (info)
4919 1.1 christos && !h->def_dynamic
4920 1.1 christos && !h->ref_dynamic)
4921 1.1 christos {
4922 1.1 christos /* This case can occur if we saw a PLT reloc in an input
4923 1.1 christos file, but the symbol was never referred to by a dynamic
4924 1.1 christos object. In such a case, we don't actually need to build
4925 1.1 christos a procedure linkage table, and we can just do a REL32
4926 1.1 christos reloc instead. */
4927 1.1 christos BFD_ASSERT (h->needs_plt);
4928 1.1 christos return TRUE;
4929 1.1 christos }
4930 1.1 christos
4931 1.1 christos /* Make sure this symbol is output as a dynamic symbol. */
4932 1.1 christos if (h->dynindx == -1)
4933 1.1 christos {
4934 1.1 christos if (! bfd_elf_link_record_dynamic_symbol (info, h))
4935 1.1 christos return FALSE;
4936 1.1.1.2 christos }
4937 1.1 christos
4938 1.1 christos s = htab->root.splt;
4939 1.1 christos BFD_ASSERT (s != NULL);
4940 1.1 christos
4941 1.1 christos /* If this is the first .plt entry, make room for the special
4942 1.1 christos first entry. */
4943 1.1 christos if (s->size == 0)
4944 1.1 christos s->size += elf_mn10300_sizeof_plt0 (info);
4945 1.1 christos
4946 1.1 christos /* If this symbol is not defined in a regular file, and we are
4947 1.1 christos not generating a shared library, then set the symbol to this
4948 1.1 christos location in the .plt. This is required to make function
4949 1.1.1.6 christos pointers compare as equal between the normal executable and
4950 1.1 christos the shared library. */
4951 1.1 christos if (! bfd_link_pic (info)
4952 1.1 christos && !h->def_regular)
4953 1.1 christos {
4954 1.1 christos h->root.u.def.section = s;
4955 1.1 christos h->root.u.def.value = s->size;
4956 1.1 christos }
4957 1.1 christos
4958 1.1 christos h->plt.offset = s->size;
4959 1.1 christos
4960 1.1 christos /* Make room for this entry. */
4961 1.1 christos s->size += elf_mn10300_sizeof_plt (info);
4962 1.1 christos
4963 1.1.1.2 christos /* We also need to make an entry in the .got.plt section, which
4964 1.1 christos will be placed in the .got section by the linker script. */
4965 1.1 christos s = htab->root.sgotplt;
4966 1.1 christos BFD_ASSERT (s != NULL);
4967 1.1 christos s->size += 4;
4968 1.1.1.2 christos
4969 1.1 christos /* We also need to make an entry in the .rela.plt section. */
4970 1.1 christos s = bfd_get_linker_section (dynobj, ".rela.plt");
4971 1.1 christos BFD_ASSERT (s != NULL);
4972 1.1 christos s->size += sizeof (Elf32_External_Rela);
4973 1.1 christos
4974 1.1 christos return TRUE;
4975 1.1 christos }
4976 1.1 christos
4977 1.1 christos /* If this is a weak symbol, and there is a real definition, the
4978 1.1 christos processor independent code will have arranged for us to see the
4979 1.1 christos real definition first, and we can just use the same value. */
4980 1.1 christos if (h->u.weakdef != NULL)
4981 1.1 christos {
4982 1.1 christos BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4983 1.1 christos || h->u.weakdef->root.type == bfd_link_hash_defweak);
4984 1.1 christos h->root.u.def.section = h->u.weakdef->root.u.def.section;
4985 1.1 christos h->root.u.def.value = h->u.weakdef->root.u.def.value;
4986 1.1 christos return TRUE;
4987 1.1 christos }
4988 1.1 christos
4989 1.1 christos /* This is a reference to a symbol defined by a dynamic object which
4990 1.1 christos is not a function. */
4991 1.1 christos
4992 1.1 christos /* If we are creating a shared library, we must presume that the
4993 1.1 christos only references to the symbol are via the global offset table.
4994 1.1.1.6 christos For such cases we need not do anything here; the relocations will
4995 1.1 christos be handled correctly by relocate_section. */
4996 1.1 christos if (bfd_link_pic (info))
4997 1.1 christos return TRUE;
4998 1.1 christos
4999 1.1 christos /* If there are no references to this symbol that do not use the
5000 1.1 christos GOT, we don't need to generate a copy reloc. */
5001 1.1 christos if (!h->non_got_ref)
5002 1.1 christos return TRUE;
5003 1.1 christos
5004 1.1 christos /* We must allocate the symbol in our .dynbss section, which will
5005 1.1 christos become part of the .bss section of the executable. There will be
5006 1.1 christos an entry for this symbol in the .dynsym section. The dynamic
5007 1.1 christos object will contain position independent code, so all references
5008 1.1 christos from the dynamic object to this symbol will go through the global
5009 1.1 christos offset table. The dynamic linker will use the .dynsym entry to
5010 1.1 christos determine the address it must put in the global offset table, so
5011 1.1 christos both the dynamic object and the regular object will refer to the
5012 1.1.1.2 christos same memory location for the variable. */
5013 1.1 christos
5014 1.1 christos s = bfd_get_linker_section (dynobj, ".dynbss");
5015 1.1 christos BFD_ASSERT (s != NULL);
5016 1.1 christos
5017 1.1 christos /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
5018 1.1 christos copy the initial value out of the dynamic object and into the
5019 1.1.1.2 christos runtime process image. We need to remember the offset into the
5020 1.1 christos .rela.bss section we are going to use. */
5021 1.1 christos if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5022 1.1 christos {
5023 1.1.1.2 christos asection * srel;
5024 1.1 christos
5025 1.1 christos srel = bfd_get_linker_section (dynobj, ".rela.bss");
5026 1.1 christos BFD_ASSERT (srel != NULL);
5027 1.1 christos srel->size += sizeof (Elf32_External_Rela);
5028 1.1 christos h->needs_copy = 1;
5029 1.1.1.4 christos }
5030 1.1 christos
5031 1.1 christos return _bfd_elf_adjust_dynamic_copy (info, h, s);
5032 1.1 christos }
5033 1.1 christos
5034 1.1 christos /* Set the sizes of the dynamic sections. */
5035 1.1 christos
5036 1.1 christos static bfd_boolean
5037 1.1 christos _bfd_mn10300_elf_size_dynamic_sections (bfd * output_bfd,
5038 1.1.1.2 christos struct bfd_link_info * info)
5039 1.1 christos {
5040 1.1 christos struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info);
5041 1.1 christos bfd * dynobj;
5042 1.1 christos asection * s;
5043 1.1 christos bfd_boolean plt;
5044 1.1 christos bfd_boolean relocs;
5045 1.1.1.2 christos bfd_boolean reltext;
5046 1.1 christos
5047 1.1 christos dynobj = htab->root.dynobj;
5048 1.1 christos BFD_ASSERT (dynobj != NULL);
5049 1.1 christos
5050 1.1 christos if (elf_hash_table (info)->dynamic_sections_created)
5051 1.1.1.6 christos {
5052 1.1 christos /* Set the contents of the .interp section to the interpreter. */
5053 1.1.1.2 christos if (bfd_link_executable (info))
5054 1.1 christos {
5055 1.1 christos s = bfd_get_linker_section (dynobj, ".interp");
5056 1.1 christos BFD_ASSERT (s != NULL);
5057 1.1 christos s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5058 1.1 christos s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5059 1.1 christos }
5060 1.1 christos }
5061 1.1 christos else
5062 1.1 christos {
5063 1.1 christos /* We may have created entries in the .rela.got section.
5064 1.1 christos However, if we are not creating the dynamic sections, we will
5065 1.1 christos not actually use these entries. Reset the size of .rela.got,
5066 1.1.1.2 christos which will cause it to get stripped from the output file
5067 1.1 christos below. */
5068 1.1 christos s = htab->root.sgot;
5069 1.1 christos if (s != NULL)
5070 1.1 christos s->size = 0;
5071 1.1.1.2 christos }
5072 1.1.1.2 christos
5073 1.1.1.2 christos if (htab->tls_ldm_got.refcount > 0)
5074 1.1.1.2 christos {
5075 1.1.1.2 christos s = bfd_get_linker_section (dynobj, ".rela.got");
5076 1.1.1.2 christos BFD_ASSERT (s != NULL);
5077 1.1.1.2 christos s->size += sizeof (Elf32_External_Rela);
5078 1.1 christos }
5079 1.1 christos
5080 1.1 christos /* The check_relocs and adjust_dynamic_symbol entry points have
5081 1.1 christos determined the sizes of the various dynamic sections. Allocate
5082 1.1 christos memory for them. */
5083 1.1 christos plt = FALSE;
5084 1.1 christos relocs = FALSE;
5085 1.1 christos reltext = FALSE;
5086 1.1 christos for (s = dynobj->sections; s != NULL; s = s->next)
5087 1.1 christos {
5088 1.1 christos const char * name;
5089 1.1 christos
5090 1.1 christos if ((s->flags & SEC_LINKER_CREATED) == 0)
5091 1.1 christos continue;
5092 1.1 christos
5093 1.1 christos /* It's OK to base decisions on the section name, because none
5094 1.1 christos of the dynobj section names depend upon the input files. */
5095 1.1 christos name = bfd_get_section_name (dynobj, s);
5096 1.1 christos
5097 1.1 christos if (streq (name, ".plt"))
5098 1.1 christos {
5099 1.1 christos /* Remember whether there is a PLT. */
5100 1.1 christos plt = s->size != 0;
5101 1.1 christos }
5102 1.1 christos else if (CONST_STRNEQ (name, ".rela"))
5103 1.1 christos {
5104 1.1 christos if (s->size != 0)
5105 1.1 christos {
5106 1.1 christos asection * target;
5107 1.1 christos
5108 1.1 christos /* Remember whether there are any reloc sections other
5109 1.1 christos than .rela.plt. */
5110 1.1 christos if (! streq (name, ".rela.plt"))
5111 1.1 christos {
5112 1.1 christos const char * outname;
5113 1.1 christos
5114 1.1 christos relocs = TRUE;
5115 1.1 christos
5116 1.1 christos /* If this relocation section applies to a read only
5117 1.1 christos section, then we probably need a DT_TEXTREL
5118 1.1 christos entry. The entries in the .rela.plt section
5119 1.1 christos really apply to the .got section, which we
5120 1.1 christos created ourselves and so know is not readonly. */
5121 1.1 christos outname = bfd_get_section_name (output_bfd,
5122 1.1 christos s->output_section);
5123 1.1 christos target = bfd_get_section_by_name (output_bfd, outname + 5);
5124 1.1 christos if (target != NULL
5125 1.1 christos && (target->flags & SEC_READONLY) != 0
5126 1.1 christos && (target->flags & SEC_ALLOC) != 0)
5127 1.1 christos reltext = TRUE;
5128 1.1 christos }
5129 1.1 christos
5130 1.1 christos /* We use the reloc_count field as a counter if we need
5131 1.1 christos to copy relocs into the output file. */
5132 1.1 christos s->reloc_count = 0;
5133 1.1 christos }
5134 1.1 christos }
5135 1.1 christos else if (! CONST_STRNEQ (name, ".got")
5136 1.1 christos && ! streq (name, ".dynbss"))
5137 1.1 christos /* It's not one of our sections, so don't allocate space. */
5138 1.1 christos continue;
5139 1.1 christos
5140 1.1 christos if (s->size == 0)
5141 1.1 christos {
5142 1.1 christos /* If we don't need this section, strip it from the
5143 1.1 christos output file. This is mostly to handle .rela.bss and
5144 1.1 christos .rela.plt. We must create both sections in
5145 1.1 christos create_dynamic_sections, because they must be created
5146 1.1 christos before the linker maps input sections to output
5147 1.1 christos sections. The linker does that before
5148 1.1 christos adjust_dynamic_symbol is called, and it is that
5149 1.1 christos function which decides whether anything needs to go
5150 1.1 christos into these sections. */
5151 1.1 christos s->flags |= SEC_EXCLUDE;
5152 1.1 christos continue;
5153 1.1 christos }
5154 1.1 christos
5155 1.1 christos if ((s->flags & SEC_HAS_CONTENTS) == 0)
5156 1.1 christos continue;
5157 1.1 christos
5158 1.1 christos /* Allocate memory for the section contents. We use bfd_zalloc
5159 1.1 christos here in case unused entries are not reclaimed before the
5160 1.1 christos section's contents are written out. This should not happen,
5161 1.1 christos but this way if it does, we get a R_MN10300_NONE reloc
5162 1.1 christos instead of garbage. */
5163 1.1 christos s->contents = bfd_zalloc (dynobj, s->size);
5164 1.1 christos if (s->contents == NULL)
5165 1.1 christos return FALSE;
5166 1.1 christos }
5167 1.1 christos
5168 1.1 christos if (elf_hash_table (info)->dynamic_sections_created)
5169 1.1 christos {
5170 1.1 christos /* Add some entries to the .dynamic section. We fill in the
5171 1.1 christos values later, in _bfd_mn10300_elf_finish_dynamic_sections,
5172 1.1 christos but we must add the entries now so that we get the correct
5173 1.1.1.6 christos size for the .dynamic section. The DT_DEBUG entry is filled
5174 1.1 christos in by the dynamic linker and used by the debugger. */
5175 1.1 christos if (! bfd_link_pic (info))
5176 1.1 christos {
5177 1.1 christos if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
5178 1.1 christos return FALSE;
5179 1.1 christos }
5180 1.1 christos
5181 1.1 christos if (plt)
5182 1.1 christos {
5183 1.1 christos if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
5184 1.1 christos || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
5185 1.1 christos || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
5186 1.1 christos || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
5187 1.1 christos return FALSE;
5188 1.1 christos }
5189 1.1 christos
5190 1.1 christos if (relocs)
5191 1.1 christos {
5192 1.1 christos if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
5193 1.1 christos || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
5194 1.1 christos || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
5195 1.1 christos sizeof (Elf32_External_Rela)))
5196 1.1 christos return FALSE;
5197 1.1 christos }
5198 1.1 christos
5199 1.1 christos if (reltext)
5200 1.1 christos {
5201 1.1 christos if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
5202 1.1 christos return FALSE;
5203 1.1 christos }
5204 1.1 christos }
5205 1.1 christos
5206 1.1 christos return TRUE;
5207 1.1 christos }
5208 1.1 christos
5209 1.1 christos /* Finish up dynamic symbol handling. We set the contents of various
5210 1.1 christos dynamic sections here. */
5211 1.1 christos
5212 1.1 christos static bfd_boolean
5213 1.1 christos _bfd_mn10300_elf_finish_dynamic_symbol (bfd * output_bfd,
5214 1.1 christos struct bfd_link_info * info,
5215 1.1 christos struct elf_link_hash_entry * h,
5216 1.1.1.2 christos Elf_Internal_Sym * sym)
5217 1.1 christos {
5218 1.1 christos struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info);
5219 1.1.1.2 christos bfd * dynobj;
5220 1.1 christos
5221 1.1 christos dynobj = htab->root.dynobj;
5222 1.1 christos
5223 1.1 christos if (h->plt.offset != (bfd_vma) -1)
5224 1.1 christos {
5225 1.1 christos asection * splt;
5226 1.1 christos asection * sgot;
5227 1.1 christos asection * srel;
5228 1.1 christos bfd_vma plt_index;
5229 1.1 christos bfd_vma got_offset;
5230 1.1 christos Elf_Internal_Rela rel;
5231 1.1 christos
5232 1.1 christos /* This symbol has an entry in the procedure linkage table. Set
5233 1.1 christos it up. */
5234 1.1 christos
5235 1.1.1.2 christos BFD_ASSERT (h->dynindx != -1);
5236 1.1.1.2 christos
5237 1.1.1.2 christos splt = htab->root.splt;
5238 1.1 christos sgot = htab->root.sgotplt;
5239 1.1 christos srel = bfd_get_linker_section (dynobj, ".rela.plt");
5240 1.1 christos BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
5241 1.1 christos
5242 1.1 christos /* Get the index in the procedure linkage table which
5243 1.1 christos corresponds to this symbol. This is the index of this symbol
5244 1.1 christos in all the symbols for which we are making plt entries. The
5245 1.1 christos first entry in the procedure linkage table is reserved. */
5246 1.1 christos plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
5247 1.1 christos / elf_mn10300_sizeof_plt (info));
5248 1.1 christos
5249 1.1 christos /* Get the offset into the .got table of the entry that
5250 1.1 christos corresponds to this function. Each .got entry is 4 bytes.
5251 1.1 christos The first three are reserved. */
5252 1.1 christos got_offset = (plt_index + 3) * 4;
5253 1.1.1.6 christos
5254 1.1 christos /* Fill in the entry in the procedure linkage table. */
5255 1.1 christos if (! bfd_link_pic (info))
5256 1.1 christos {
5257 1.1 christos memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
5258 1.1 christos elf_mn10300_sizeof_plt (info));
5259 1.1 christos bfd_put_32 (output_bfd,
5260 1.1 christos (sgot->output_section->vma
5261 1.1 christos + sgot->output_offset
5262 1.1 christos + got_offset),
5263 1.1 christos (splt->contents + h->plt.offset
5264 1.1 christos + elf_mn10300_plt_symbol_offset (info)));
5265 1.1 christos
5266 1.1 christos bfd_put_32 (output_bfd,
5267 1.1 christos (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
5268 1.1 christos (splt->contents + h->plt.offset
5269 1.1 christos + elf_mn10300_plt_plt0_offset (info)));
5270 1.1 christos }
5271 1.1 christos else
5272 1.1 christos {
5273 1.1 christos memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
5274 1.1 christos elf_mn10300_sizeof_plt (info));
5275 1.1 christos
5276 1.1 christos bfd_put_32 (output_bfd, got_offset,
5277 1.1 christos (splt->contents + h->plt.offset
5278 1.1 christos + elf_mn10300_plt_symbol_offset (info)));
5279 1.1 christos }
5280 1.1 christos
5281 1.1 christos bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
5282 1.1 christos (splt->contents + h->plt.offset
5283 1.1 christos + elf_mn10300_plt_reloc_offset (info)));
5284 1.1 christos
5285 1.1 christos /* Fill in the entry in the global offset table. */
5286 1.1 christos bfd_put_32 (output_bfd,
5287 1.1 christos (splt->output_section->vma
5288 1.1 christos + splt->output_offset
5289 1.1 christos + h->plt.offset
5290 1.1 christos + elf_mn10300_plt_temp_offset (info)),
5291 1.1 christos sgot->contents + got_offset);
5292 1.1 christos
5293 1.1 christos /* Fill in the entry in the .rela.plt section. */
5294 1.1 christos rel.r_offset = (sgot->output_section->vma
5295 1.1 christos + sgot->output_offset
5296 1.1 christos + got_offset);
5297 1.1 christos rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
5298 1.1 christos rel.r_addend = 0;
5299 1.1 christos bfd_elf32_swap_reloca_out (output_bfd, &rel,
5300 1.1 christos (bfd_byte *) ((Elf32_External_Rela *) srel->contents
5301 1.1 christos + plt_index));
5302 1.1 christos
5303 1.1 christos if (!h->def_regular)
5304 1.1 christos /* Mark the symbol as undefined, rather than as defined in
5305 1.1 christos the .plt section. Leave the value alone. */
5306 1.1 christos sym->st_shndx = SHN_UNDEF;
5307 1.1 christos }
5308 1.1 christos
5309 1.1 christos if (h->got.offset != (bfd_vma) -1)
5310 1.1 christos {
5311 1.1 christos asection * sgot;
5312 1.1 christos asection * srel;
5313 1.1 christos Elf_Internal_Rela rel;
5314 1.1.1.2 christos
5315 1.1.1.2 christos /* This symbol has an entry in the global offset table. Set it up. */
5316 1.1 christos sgot = htab->root.sgot;
5317 1.1 christos srel = bfd_get_linker_section (dynobj, ".rela.got");
5318 1.1 christos BFD_ASSERT (sgot != NULL && srel != NULL);
5319 1.1 christos
5320 1.1 christos rel.r_offset = (sgot->output_section->vma
5321 1.1 christos + sgot->output_offset
5322 1.1.1.2 christos + (h->got.offset & ~1));
5323 1.1 christos
5324 1.1.1.2 christos switch (elf_mn10300_hash_entry (h)->tls_type)
5325 1.1 christos {
5326 1.1.1.2 christos case GOT_TLS_GD:
5327 1.1.1.2 christos bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
5328 1.1.1.2 christos bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset + 4);
5329 1.1.1.2 christos rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_TLS_DTPMOD);
5330 1.1.1.2 christos rel.r_addend = 0;
5331 1.1.1.2 christos bfd_elf32_swap_reloca_out (output_bfd, & rel,
5332 1.1.1.2 christos (bfd_byte *) ((Elf32_External_Rela *) srel->contents
5333 1.1.1.2 christos + srel->reloc_count));
5334 1.1.1.2 christos ++ srel->reloc_count;
5335 1.1 christos rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_TLS_DTPOFF);
5336 1.1.1.2 christos rel.r_offset += 4;
5337 1.1.1.2 christos rel.r_addend = 0;
5338 1.1.1.2 christos break;
5339 1.1.1.2 christos
5340 1.1.1.2 christos case GOT_TLS_IE:
5341 1.1.1.2 christos /* We originally stored the addend in the GOT, but at this
5342 1.1.1.2 christos point, we want to move it to the reloc instead as that's
5343 1.1.1.2 christos where the dynamic linker wants it. */
5344 1.1.1.2 christos rel.r_addend = bfd_get_32 (output_bfd, sgot->contents + h->got.offset);
5345 1.1.1.2 christos bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
5346 1.1.1.2 christos if (h->dynindx == -1)
5347 1.1.1.2 christos rel.r_info = ELF32_R_INFO (0, R_MN10300_TLS_TPOFF);
5348 1.1.1.2 christos else
5349 1.1.1.2 christos rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_TLS_TPOFF);
5350 1.1.1.2 christos break;
5351 1.1.1.2 christos
5352 1.1.1.2 christos default:
5353 1.1.1.2 christos /* If this is a -Bsymbolic link, and the symbol is defined
5354 1.1.1.2 christos locally, we just want to emit a RELATIVE reloc. Likewise if
5355 1.1.1.2 christos the symbol was forced to be local because of a version file.
5356 1.1.1.6 christos The entry in the global offset table will already have been
5357 1.1.1.2 christos initialized in the relocate_section function. */
5358 1.1.1.2 christos if (bfd_link_pic (info)
5359 1.1.1.2 christos && (info->symbolic || h->dynindx == -1)
5360 1.1.1.2 christos && h->def_regular)
5361 1.1.1.2 christos {
5362 1.1.1.2 christos rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
5363 1.1.1.2 christos rel.r_addend = (h->root.u.def.value
5364 1.1.1.2 christos + h->root.u.def.section->output_section->vma
5365 1.1.1.2 christos + h->root.u.def.section->output_offset);
5366 1.1.1.2 christos }
5367 1.1.1.2 christos else
5368 1.1.1.2 christos {
5369 1.1.1.2 christos bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
5370 1.1.1.2 christos rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
5371 1.1 christos rel.r_addend = 0;
5372 1.1 christos }
5373 1.1.1.2 christos }
5374 1.1.1.2 christos
5375 1.1.1.2 christos if (ELF32_R_TYPE (rel.r_info) != R_MN10300_NONE)
5376 1.1.1.2 christos {
5377 1.1.1.2 christos bfd_elf32_swap_reloca_out (output_bfd, &rel,
5378 1.1.1.2 christos (bfd_byte *) ((Elf32_External_Rela *) srel->contents
5379 1.1.1.2 christos + srel->reloc_count));
5380 1.1 christos ++ srel->reloc_count;
5381 1.1 christos }
5382 1.1 christos }
5383 1.1 christos
5384 1.1 christos if (h->needs_copy)
5385 1.1 christos {
5386 1.1 christos asection * s;
5387 1.1 christos Elf_Internal_Rela rel;
5388 1.1 christos
5389 1.1 christos /* This symbol needs a copy reloc. Set it up. */
5390 1.1 christos BFD_ASSERT (h->dynindx != -1
5391 1.1 christos && (h->root.type == bfd_link_hash_defined
5392 1.1.1.2 christos || h->root.type == bfd_link_hash_defweak));
5393 1.1 christos
5394 1.1 christos s = bfd_get_linker_section (dynobj, ".rela.bss");
5395 1.1 christos BFD_ASSERT (s != NULL);
5396 1.1 christos
5397 1.1 christos rel.r_offset = (h->root.u.def.value
5398 1.1 christos + h->root.u.def.section->output_section->vma
5399 1.1 christos + h->root.u.def.section->output_offset);
5400 1.1 christos rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
5401 1.1 christos rel.r_addend = 0;
5402 1.1 christos bfd_elf32_swap_reloca_out (output_bfd, & rel,
5403 1.1 christos (bfd_byte *) ((Elf32_External_Rela *) s->contents
5404 1.1 christos + s->reloc_count));
5405 1.1 christos ++ s->reloc_count;
5406 1.1 christos }
5407 1.1.1.2 christos
5408 1.1 christos /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
5409 1.1 christos if (h == elf_hash_table (info)->hdynamic
5410 1.1 christos || h == elf_hash_table (info)->hgot)
5411 1.1 christos sym->st_shndx = SHN_ABS;
5412 1.1 christos
5413 1.1 christos return TRUE;
5414 1.1 christos }
5415 1.1 christos
5416 1.1 christos /* Finish up the dynamic sections. */
5417 1.1 christos
5418 1.1 christos static bfd_boolean
5419 1.1 christos _bfd_mn10300_elf_finish_dynamic_sections (bfd * output_bfd,
5420 1.1 christos struct bfd_link_info * info)
5421 1.1 christos {
5422 1.1 christos bfd * dynobj;
5423 1.1.1.2 christos asection * sgot;
5424 1.1 christos asection * sdyn;
5425 1.1.1.2 christos struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info);
5426 1.1.1.2 christos
5427 1.1 christos dynobj = htab->root.dynobj;
5428 1.1.1.2 christos sgot = htab->root.sgotplt;
5429 1.1 christos BFD_ASSERT (sgot != NULL);
5430 1.1 christos sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5431 1.1 christos
5432 1.1 christos if (elf_hash_table (info)->dynamic_sections_created)
5433 1.1 christos {
5434 1.1 christos asection * splt;
5435 1.1 christos Elf32_External_Dyn * dyncon;
5436 1.1 christos Elf32_External_Dyn * dynconend;
5437 1.1 christos
5438 1.1 christos BFD_ASSERT (sdyn != NULL);
5439 1.1 christos
5440 1.1 christos dyncon = (Elf32_External_Dyn *) sdyn->contents;
5441 1.1 christos dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5442 1.1 christos
5443 1.1 christos for (; dyncon < dynconend; dyncon++)
5444 1.1 christos {
5445 1.1 christos Elf_Internal_Dyn dyn;
5446 1.1 christos const char * name;
5447 1.1 christos asection * s;
5448 1.1 christos
5449 1.1 christos bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5450 1.1 christos
5451 1.1 christos switch (dyn.d_tag)
5452 1.1 christos {
5453 1.1 christos default:
5454 1.1 christos break;
5455 1.1 christos
5456 1.1 christos case DT_PLTGOT:
5457 1.1 christos name = ".got";
5458 1.1 christos goto get_vma;
5459 1.1 christos
5460 1.1 christos case DT_JMPREL:
5461 1.1.1.6 christos name = ".rela.plt";
5462 1.1.1.6 christos get_vma:
5463 1.1 christos s = bfd_get_linker_section (dynobj, name);
5464 1.1 christos dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5465 1.1 christos bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5466 1.1 christos break;
5467 1.1.1.6 christos
5468 1.1 christos case DT_PLTRELSZ:
5469 1.1 christos s = bfd_get_linker_section (dynobj, ".rela.plt");
5470 1.1 christos dyn.d_un.d_val = s->size;
5471 1.1 christos bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5472 1.1 christos break;
5473 1.1 christos
5474 1.1 christos case DT_RELASZ:
5475 1.1 christos /* My reading of the SVR4 ABI indicates that the
5476 1.1 christos procedure linkage table relocs (DT_JMPREL) should be
5477 1.1 christos included in the overall relocs (DT_RELA). This is
5478 1.1 christos what Solaris does. However, UnixWare can not handle
5479 1.1 christos that case. Therefore, we override the DT_RELASZ entry
5480 1.1 christos here to make it not include the JMPREL relocs. Since
5481 1.1 christos the linker script arranges for .rela.plt to follow all
5482 1.1.1.6 christos other relocation sections, we don't have to worry
5483 1.1 christos about changing the DT_RELA entry. */
5484 1.1 christos s = bfd_get_linker_section (dynobj, ".rela.plt");
5485 1.1 christos if (s != NULL)
5486 1.1 christos dyn.d_un.d_val -= s->size;
5487 1.1 christos bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5488 1.1 christos break;
5489 1.1 christos }
5490 1.1 christos }
5491 1.1.1.2 christos
5492 1.1 christos /* Fill in the first entry in the procedure linkage table. */
5493 1.1 christos splt = htab->root.splt;
5494 1.1.1.6 christos if (splt && splt->size > 0)
5495 1.1 christos {
5496 1.1 christos if (bfd_link_pic (info))
5497 1.1 christos {
5498 1.1 christos memcpy (splt->contents, elf_mn10300_pic_plt_entry,
5499 1.1 christos elf_mn10300_sizeof_plt (info));
5500 1.1 christos }
5501 1.1 christos else
5502 1.1 christos {
5503 1.1 christos memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
5504 1.1 christos bfd_put_32 (output_bfd,
5505 1.1 christos sgot->output_section->vma + sgot->output_offset + 4,
5506 1.1 christos splt->contents + elf_mn10300_plt0_gotid_offset (info));
5507 1.1 christos bfd_put_32 (output_bfd,
5508 1.1 christos sgot->output_section->vma + sgot->output_offset + 8,
5509 1.1 christos splt->contents + elf_mn10300_plt0_linker_offset (info));
5510 1.1 christos }
5511 1.1 christos
5512 1.1 christos /* UnixWare sets the entsize of .plt to 4, although that doesn't
5513 1.1.1.2 christos really seem like the right value. */
5514 1.1.1.2 christos elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
5515 1.1.1.2 christos
5516 1.1.1.2 christos /* UnixWare sets the entsize of .plt to 4, but this is incorrect
5517 1.1.1.2 christos as it means that the size of the PLT0 section (15 bytes) is not
5518 1.1.1.2 christos a multiple of the sh_entsize. Some ELF tools flag this as an
5519 1.1.1.2 christos error. We could pad PLT0 to 16 bytes, but that would introduce
5520 1.1.1.2 christos compatibilty issues with previous toolchains, so instead we
5521 1.1 christos just set the entry size to 1. */
5522 1.1 christos elf_section_data (splt->output_section)->this_hdr.sh_entsize = 1;
5523 1.1 christos }
5524 1.1 christos }
5525 1.1 christos
5526 1.1 christos /* Fill in the first three entries in the global offset table. */
5527 1.1 christos if (sgot->size > 0)
5528 1.1 christos {
5529 1.1 christos if (sdyn == NULL)
5530 1.1 christos bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
5531 1.1 christos else
5532 1.1 christos bfd_put_32 (output_bfd,
5533 1.1 christos sdyn->output_section->vma + sdyn->output_offset,
5534 1.1 christos sgot->contents);
5535 1.1 christos bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
5536 1.1 christos bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
5537 1.1 christos }
5538 1.1 christos
5539 1.1 christos elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
5540 1.1 christos
5541 1.1 christos return TRUE;
5542 1.1 christos }
5543 1.1 christos
5544 1.1 christos /* Classify relocation types, such that combreloc can sort them
5545 1.1 christos properly. */
5546 1.1.1.3 christos
5547 1.1.1.3 christos static enum elf_reloc_type_class
5548 1.1.1.3 christos _bfd_mn10300_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
5549 1.1 christos const asection *rel_sec ATTRIBUTE_UNUSED,
5550 1.1 christos const Elf_Internal_Rela *rela)
5551 1.1 christos {
5552 1.1 christos switch ((int) ELF32_R_TYPE (rela->r_info))
5553 1.1 christos {
5554 1.1 christos case R_MN10300_RELATIVE: return reloc_class_relative;
5555 1.1 christos case R_MN10300_JMP_SLOT: return reloc_class_plt;
5556 1.1 christos case R_MN10300_COPY: return reloc_class_copy;
5557 1.1 christos default: return reloc_class_normal;
5558 1.1 christos }
5559 1.1.1.2 christos }
5560 1.1.1.2 christos
5561 1.1.1.2 christos /* Allocate space for an MN10300 extension to the bfd elf data structure. */
5562 1.1.1.2 christos
5563 1.1.1.2 christos static bfd_boolean
5564 1.1.1.2 christos mn10300_elf_mkobject (bfd *abfd)
5565 1.1.1.2 christos {
5566 1.1.1.2 christos return bfd_elf_allocate_object (abfd, sizeof (struct elf_mn10300_obj_tdata),
5567 1.1.1.2 christos MN10300_ELF_DATA);
5568 1.1.1.2 christos }
5569 1.1.1.2 christos
5570 1.1 christos #define bfd_elf32_mkobject mn10300_elf_mkobject
5571 1.1.1.4 christos
5572 1.1 christos #ifndef ELF_ARCH
5573 1.1 christos #define TARGET_LITTLE_SYM mn10300_elf32_vec
5574 1.1 christos #define TARGET_LITTLE_NAME "elf32-mn10300"
5575 1.1 christos #define ELF_ARCH bfd_arch_mn10300
5576 1.1 christos #define ELF_TARGET_ID MN10300_ELF_DATA
5577 1.1 christos #define ELF_MACHINE_CODE EM_MN10300
5578 1.1 christos #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
5579 1.1 christos #define ELF_MAXPAGESIZE 0x1000
5580 1.1 christos #endif
5581 1.1 christos
5582 1.1 christos #define elf_info_to_howto mn10300_info_to_howto
5583 1.1 christos #define elf_info_to_howto_rel 0
5584 1.1 christos #define elf_backend_can_gc_sections 1
5585 1.1 christos #define elf_backend_rela_normal 1
5586 1.1 christos #define elf_backend_check_relocs mn10300_elf_check_relocs
5587 1.1 christos #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
5588 1.1 christos #define elf_backend_relocate_section mn10300_elf_relocate_section
5589 1.1 christos #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
5590 1.1 christos #define bfd_elf32_bfd_get_relocated_section_contents \
5591 1.1 christos mn10300_elf_get_relocated_section_contents
5592 1.1 christos #define bfd_elf32_bfd_link_hash_table_create \
5593 1.1 christos elf32_mn10300_link_hash_table_create
5594 1.1 christos
5595 1.1 christos #ifndef elf_symbol_leading_char
5596 1.1 christos #define elf_symbol_leading_char '_'
5597 1.1 christos #endif
5598 1.1 christos
5599 1.1 christos /* So we can set bits in e_flags. */
5600 1.1 christos #define elf_backend_final_write_processing \
5601 1.1 christos _bfd_mn10300_elf_final_write_processing
5602 1.1 christos #define elf_backend_object_p _bfd_mn10300_elf_object_p
5603 1.1 christos
5604 1.1 christos #define bfd_elf32_bfd_merge_private_bfd_data \
5605 1.1 christos _bfd_mn10300_elf_merge_private_bfd_data
5606 1.1 christos
5607 1.1 christos #define elf_backend_can_gc_sections 1
5608 1.1 christos #define elf_backend_create_dynamic_sections \
5609 1.1 christos _bfd_mn10300_elf_create_dynamic_sections
5610 1.1 christos #define elf_backend_adjust_dynamic_symbol \
5611 1.1 christos _bfd_mn10300_elf_adjust_dynamic_symbol
5612 1.1 christos #define elf_backend_size_dynamic_sections \
5613 1.1 christos _bfd_mn10300_elf_size_dynamic_sections
5614 1.1 christos #define elf_backend_omit_section_dynsym \
5615 1.1 christos ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
5616 1.1 christos #define elf_backend_finish_dynamic_symbol \
5617 1.1 christos _bfd_mn10300_elf_finish_dynamic_symbol
5618 1.1.1.2 christos #define elf_backend_finish_dynamic_sections \
5619 1.1.1.2 christos _bfd_mn10300_elf_finish_dynamic_sections
5620 1.1 christos #define elf_backend_copy_indirect_symbol \
5621 1.1 christos _bfd_mn10300_copy_indirect_symbol
5622 1.1 christos #define elf_backend_reloc_type_class \
5623 1.1 christos _bfd_mn10300_elf_reloc_type_class
5624 1.1 christos
5625 1.1 christos #define elf_backend_want_got_plt 1
5626 1.1 christos #define elf_backend_plt_readonly 1
5627 1.1 christos #define elf_backend_want_plt_sym 0
5628 1.1 christos #define elf_backend_got_header_size 12
5629
5630 #include "elf32-target.h"
5631