elf64-mmix.c revision 1.1.1.11 1 1.1 skrll /* MMIX-specific support for 64-bit ELF.
2 1.1.1.11 christos Copyright (C) 2001-2025 Free Software Foundation, Inc.
3 1.1 skrll Contributed by Hans-Peter Nilsson <hp (at) bitrange.com>
4 1.1 skrll
5 1.1 skrll This file is part of BFD, the Binary File Descriptor library.
6 1.1 skrll
7 1.1 skrll This program is free software; you can redistribute it and/or modify
8 1.1 skrll it under the terms of the GNU General Public License as published by
9 1.1 skrll the Free Software Foundation; either version 3 of the License, or
10 1.1 skrll (at your option) any later version.
11 1.1 skrll
12 1.1 skrll This program is distributed in the hope that it will be useful,
13 1.1 skrll but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 skrll MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 skrll GNU General Public License for more details.
16 1.1 skrll
17 1.1 skrll You should have received a copy of the GNU General Public License
18 1.1 skrll along with this program; if not, write to the Free Software
19 1.1 skrll Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 1.1 skrll MA 02110-1301, USA. */
21 1.1 skrll
22 1.1 skrll
23 1.1 skrll /* No specific ABI or "processor-specific supplement" defined. */
24 1.1 skrll
25 1.1 skrll /* TODO:
26 1.1 skrll - "Traditional" linker relaxation (shrinking whole sections).
27 1.1 skrll - Merge reloc stubs jumping to same location.
28 1.1 skrll - GETA stub relaxation (call a stub for out of range new
29 1.1 skrll R_MMIX_GETA_STUBBABLE). */
30 1.1 skrll
31 1.1 skrll #include "sysdep.h"
32 1.1 skrll #include "bfd.h"
33 1.1 skrll #include "libbfd.h"
34 1.1 skrll #include "elf-bfd.h"
35 1.1 skrll #include "elf/mmix.h"
36 1.1 skrll #include "opcode/mmix.h"
37 1.1 skrll
38 1.1 skrll #define MINUS_ONE (((bfd_vma) 0) - 1)
39 1.1 skrll
40 1.1 skrll #define MAX_PUSHJ_STUB_SIZE (5 * 4)
41 1.1 skrll
42 1.1 skrll /* Put these everywhere in new code. */
43 1.1 skrll #define FATAL_DEBUG \
44 1.1 skrll _bfd_abort (__FILE__, __LINE__, \
45 1.1 skrll "Internal: Non-debugged code (test-case missing)")
46 1.1 skrll
47 1.1 skrll #define BAD_CASE(x) \
48 1.1 skrll _bfd_abort (__FILE__, __LINE__, \
49 1.1 skrll "bad case for " #x)
50 1.1 skrll
51 1.1 skrll struct _mmix_elf_section_data
52 1.1 skrll {
53 1.1 skrll struct bfd_elf_section_data elf;
54 1.1 skrll union
55 1.1 skrll {
56 1.1 skrll struct bpo_reloc_section_info *reloc;
57 1.1 skrll struct bpo_greg_section_info *greg;
58 1.1 skrll } bpo;
59 1.1 skrll
60 1.1 skrll struct pushj_stub_info
61 1.1 skrll {
62 1.1 skrll /* Maximum number of stubs needed for this section. */
63 1.1 skrll bfd_size_type n_pushj_relocs;
64 1.1 skrll
65 1.1 skrll /* Size of stubs after a mmix_elf_relax_section round. */
66 1.1 skrll bfd_size_type stubs_size_sum;
67 1.1 skrll
68 1.1 skrll /* Per-reloc stubs_size_sum information. The stubs_size_sum member is the sum
69 1.1 skrll of these. Allocated in mmix_elf_check_common_relocs. */
70 1.1 skrll bfd_size_type *stub_size;
71 1.1 skrll
72 1.1 skrll /* Offset of next stub during relocation. Somewhat redundant with the
73 1.1 skrll above: error coverage is easier and we don't have to reset the
74 1.1 skrll stubs_size_sum for relocation. */
75 1.1 skrll bfd_size_type stub_offset;
76 1.1 skrll } pjs;
77 1.1.1.3 christos
78 1.1.1.3 christos /* Whether there has been a warning that this section could not be
79 1.1.1.3 christos linked due to a specific cause. FIXME: a way to access the
80 1.1.1.3 christos linker info or output section, then stuff the limiter guard
81 1.1.1.3 christos there. */
82 1.1.1.9 christos bool has_warned_bpo;
83 1.1.1.9 christos bool has_warned_pushj;
84 1.1 skrll };
85 1.1 skrll
86 1.1 skrll #define mmix_elf_section_data(sec) \
87 1.1 skrll ((struct _mmix_elf_section_data *) elf_section_data (sec))
88 1.1 skrll
89 1.1 skrll /* For each section containing a base-plus-offset (BPO) reloc, we attach
90 1.1 skrll this struct as mmix_elf_section_data (section)->bpo, which is otherwise
91 1.1 skrll NULL. */
92 1.1 skrll struct bpo_reloc_section_info
93 1.1 skrll {
94 1.1 skrll /* The base is 1; this is the first number in this section. */
95 1.1 skrll size_t first_base_plus_offset_reloc;
96 1.1 skrll
97 1.1 skrll /* Number of BPO-relocs in this section. */
98 1.1 skrll size_t n_bpo_relocs_this_section;
99 1.1 skrll
100 1.1 skrll /* Running index, used at relocation time. */
101 1.1 skrll size_t bpo_index;
102 1.1 skrll
103 1.1 skrll /* We don't have access to the bfd_link_info struct in
104 1.1 skrll mmix_final_link_relocate. What we really want to get at is the
105 1.1 skrll global single struct greg_relocation, so we stash it here. */
106 1.1 skrll asection *bpo_greg_section;
107 1.1 skrll };
108 1.1 skrll
109 1.1 skrll /* Helper struct (in global context) for the one below.
110 1.1 skrll There's one of these created for every BPO reloc. */
111 1.1 skrll struct bpo_reloc_request
112 1.1 skrll {
113 1.1 skrll bfd_vma value;
114 1.1 skrll
115 1.1 skrll /* Valid after relaxation. The base is 0; the first register number
116 1.1 skrll must be added. The offset is in range 0..255. */
117 1.1 skrll size_t regindex;
118 1.1 skrll size_t offset;
119 1.1 skrll
120 1.1 skrll /* The order number for this BPO reloc, corresponding to the order in
121 1.1 skrll which BPO relocs were found. Used to create an index after reloc
122 1.1 skrll requests are sorted. */
123 1.1 skrll size_t bpo_reloc_no;
124 1.1 skrll
125 1.1 skrll /* Set when the value is computed. Better than coding "guard values"
126 1.1 skrll into the other members. Is FALSE only for BPO relocs in a GC:ed
127 1.1 skrll section. */
128 1.1.1.9 christos bool valid;
129 1.1 skrll };
130 1.1 skrll
131 1.1 skrll /* We attach this as mmix_elf_section_data (sec)->bpo in the linker-allocated
132 1.1 skrll greg contents section (MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME),
133 1.1 skrll which is linked into the register contents section
134 1.1 skrll (MMIX_REG_CONTENTS_SECTION_NAME). This section is created by the
135 1.1 skrll linker; using the same hook as for usual with BPO relocs does not
136 1.1 skrll collide. */
137 1.1 skrll struct bpo_greg_section_info
138 1.1 skrll {
139 1.1 skrll /* After GC, this reflects the number of remaining, non-excluded
140 1.1 skrll BPO-relocs. */
141 1.1 skrll size_t n_bpo_relocs;
142 1.1 skrll
143 1.1 skrll /* This is the number of allocated bpo_reloc_requests; the size of
144 1.1 skrll sorted_indexes. Valid after the check.*relocs functions are called
145 1.1 skrll for all incoming sections. It includes the number of BPO relocs in
146 1.1 skrll sections that were GC:ed. */
147 1.1 skrll size_t n_max_bpo_relocs;
148 1.1 skrll
149 1.1 skrll /* A counter used to find out when to fold the BPO gregs, since we
150 1.1 skrll don't have a single "after-relaxation" hook. */
151 1.1 skrll size_t n_remaining_bpo_relocs_this_relaxation_round;
152 1.1 skrll
153 1.1 skrll /* The number of linker-allocated GREGs resulting from BPO relocs.
154 1.1 skrll This is an approximation after _bfd_mmix_before_linker_allocation
155 1.1 skrll and supposedly accurate after mmix_elf_relax_section is called for
156 1.1 skrll all incoming non-collected sections. */
157 1.1 skrll size_t n_allocated_bpo_gregs;
158 1.1 skrll
159 1.1 skrll /* Index into reloc_request[], sorted on increasing "value", secondary
160 1.1 skrll by increasing index for strict sorting order. */
161 1.1 skrll size_t *bpo_reloc_indexes;
162 1.1 skrll
163 1.1 skrll /* An array of all relocations, with the "value" member filled in by
164 1.1 skrll the relaxation function. */
165 1.1 skrll struct bpo_reloc_request *reloc_request;
166 1.1 skrll };
167 1.1 skrll
168 1.1 skrll
169 1.1.1.9 christos extern bool mmix_elf_final_link (bfd *, struct bfd_link_info *);
170 1.1 skrll
171 1.1.1.3 christos extern void mmix_elf_symbol_processing (bfd *, asymbol *);
172 1.1 skrll
173 1.1 skrll /* Only intended to be called from a debugger. */
174 1.1 skrll extern void mmix_dump_bpo_gregs
175 1.1.1.6 christos (struct bfd_link_info *, void (*) (const char *, ...));
176 1.1 skrll
177 1.1 skrll static void
178 1.1.1.3 christos mmix_set_relaxable_size (bfd *, asection *, void *);
179 1.1.1.3 christos static bfd_reloc_status_type
180 1.1.1.3 christos mmix_elf_reloc (bfd *, arelent *, asymbol *, void *,
181 1.1.1.3 christos asection *, bfd *, char **);
182 1.1.1.3 christos static bfd_reloc_status_type
183 1.1.1.3 christos mmix_final_link_relocate (reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
184 1.1.1.3 christos bfd_signed_vma, bfd_vma, const char *, asection *,
185 1.1.1.3 christos char **);
186 1.1 skrll
187 1.1 skrll
188 1.1 skrll /* Watch out: this currently needs to have elements with the same index as
189 1.1 skrll their R_MMIX_ number. */
190 1.1 skrll static reloc_howto_type elf_mmix_howto_table[] =
191 1.1 skrll {
192 1.1 skrll /* This reloc does nothing. */
193 1.1 skrll HOWTO (R_MMIX_NONE, /* type */
194 1.1 skrll 0, /* rightshift */
195 1.1.1.9 christos 0, /* size */
196 1.1.1.4 christos 0, /* bitsize */
197 1.1.1.9 christos false, /* pc_relative */
198 1.1 skrll 0, /* bitpos */
199 1.1.1.4 christos complain_overflow_dont, /* complain_on_overflow */
200 1.1 skrll bfd_elf_generic_reloc, /* special_function */
201 1.1 skrll "R_MMIX_NONE", /* name */
202 1.1.1.9 christos false, /* partial_inplace */
203 1.1 skrll 0, /* src_mask */
204 1.1 skrll 0, /* dst_mask */
205 1.1.1.9 christos false), /* pcrel_offset */
206 1.1 skrll
207 1.1 skrll /* An 8 bit absolute relocation. */
208 1.1 skrll HOWTO (R_MMIX_8, /* type */
209 1.1 skrll 0, /* rightshift */
210 1.1.1.9 christos 1, /* size */
211 1.1 skrll 8, /* bitsize */
212 1.1.1.9 christos false, /* pc_relative */
213 1.1 skrll 0, /* bitpos */
214 1.1 skrll complain_overflow_bitfield, /* complain_on_overflow */
215 1.1 skrll bfd_elf_generic_reloc, /* special_function */
216 1.1 skrll "R_MMIX_8", /* name */
217 1.1.1.9 christos false, /* partial_inplace */
218 1.1 skrll 0, /* src_mask */
219 1.1 skrll 0xff, /* dst_mask */
220 1.1.1.9 christos false), /* pcrel_offset */
221 1.1 skrll
222 1.1 skrll /* An 16 bit absolute relocation. */
223 1.1 skrll HOWTO (R_MMIX_16, /* type */
224 1.1 skrll 0, /* rightshift */
225 1.1.1.9 christos 2, /* size */
226 1.1 skrll 16, /* bitsize */
227 1.1.1.9 christos false, /* pc_relative */
228 1.1 skrll 0, /* bitpos */
229 1.1 skrll complain_overflow_bitfield, /* complain_on_overflow */
230 1.1 skrll bfd_elf_generic_reloc, /* special_function */
231 1.1 skrll "R_MMIX_16", /* name */
232 1.1.1.9 christos false, /* partial_inplace */
233 1.1 skrll 0, /* src_mask */
234 1.1 skrll 0xffff, /* dst_mask */
235 1.1.1.9 christos false), /* pcrel_offset */
236 1.1 skrll
237 1.1 skrll /* An 24 bit absolute relocation. */
238 1.1 skrll HOWTO (R_MMIX_24, /* type */
239 1.1 skrll 0, /* rightshift */
240 1.1.1.9 christos 4, /* size */
241 1.1 skrll 24, /* bitsize */
242 1.1.1.9 christos false, /* pc_relative */
243 1.1 skrll 0, /* bitpos */
244 1.1 skrll complain_overflow_bitfield, /* complain_on_overflow */
245 1.1 skrll bfd_elf_generic_reloc, /* special_function */
246 1.1 skrll "R_MMIX_24", /* name */
247 1.1.1.9 christos false, /* partial_inplace */
248 1.1 skrll ~0xffffff, /* src_mask */
249 1.1 skrll 0xffffff, /* dst_mask */
250 1.1.1.9 christos false), /* pcrel_offset */
251 1.1 skrll
252 1.1 skrll /* A 32 bit absolute relocation. */
253 1.1 skrll HOWTO (R_MMIX_32, /* type */
254 1.1 skrll 0, /* rightshift */
255 1.1.1.9 christos 4, /* size */
256 1.1 skrll 32, /* bitsize */
257 1.1.1.9 christos false, /* pc_relative */
258 1.1 skrll 0, /* bitpos */
259 1.1 skrll complain_overflow_bitfield, /* complain_on_overflow */
260 1.1 skrll bfd_elf_generic_reloc, /* special_function */
261 1.1 skrll "R_MMIX_32", /* name */
262 1.1.1.9 christos false, /* partial_inplace */
263 1.1 skrll 0, /* src_mask */
264 1.1 skrll 0xffffffff, /* dst_mask */
265 1.1.1.9 christos false), /* pcrel_offset */
266 1.1 skrll
267 1.1 skrll /* 64 bit relocation. */
268 1.1 skrll HOWTO (R_MMIX_64, /* type */
269 1.1 skrll 0, /* rightshift */
270 1.1.1.9 christos 8, /* size */
271 1.1 skrll 64, /* bitsize */
272 1.1.1.9 christos false, /* pc_relative */
273 1.1 skrll 0, /* bitpos */
274 1.1 skrll complain_overflow_bitfield, /* complain_on_overflow */
275 1.1 skrll bfd_elf_generic_reloc, /* special_function */
276 1.1 skrll "R_MMIX_64", /* name */
277 1.1.1.9 christos false, /* partial_inplace */
278 1.1 skrll 0, /* src_mask */
279 1.1 skrll MINUS_ONE, /* dst_mask */
280 1.1.1.9 christos false), /* pcrel_offset */
281 1.1 skrll
282 1.1 skrll /* An 8 bit PC-relative relocation. */
283 1.1 skrll HOWTO (R_MMIX_PC_8, /* type */
284 1.1 skrll 0, /* rightshift */
285 1.1.1.9 christos 1, /* size */
286 1.1 skrll 8, /* bitsize */
287 1.1.1.9 christos true, /* pc_relative */
288 1.1 skrll 0, /* bitpos */
289 1.1 skrll complain_overflow_bitfield, /* complain_on_overflow */
290 1.1 skrll bfd_elf_generic_reloc, /* special_function */
291 1.1 skrll "R_MMIX_PC_8", /* name */
292 1.1.1.9 christos false, /* partial_inplace */
293 1.1 skrll 0, /* src_mask */
294 1.1 skrll 0xff, /* dst_mask */
295 1.1.1.9 christos true), /* pcrel_offset */
296 1.1 skrll
297 1.1 skrll /* An 16 bit PC-relative relocation. */
298 1.1 skrll HOWTO (R_MMIX_PC_16, /* type */
299 1.1 skrll 0, /* rightshift */
300 1.1.1.9 christos 2, /* size */
301 1.1 skrll 16, /* bitsize */
302 1.1.1.9 christos true, /* pc_relative */
303 1.1 skrll 0, /* bitpos */
304 1.1 skrll complain_overflow_bitfield, /* complain_on_overflow */
305 1.1 skrll bfd_elf_generic_reloc, /* special_function */
306 1.1 skrll "R_MMIX_PC_16", /* name */
307 1.1.1.9 christos false, /* partial_inplace */
308 1.1 skrll 0, /* src_mask */
309 1.1 skrll 0xffff, /* dst_mask */
310 1.1.1.9 christos true), /* pcrel_offset */
311 1.1 skrll
312 1.1 skrll /* An 24 bit PC-relative relocation. */
313 1.1 skrll HOWTO (R_MMIX_PC_24, /* type */
314 1.1 skrll 0, /* rightshift */
315 1.1.1.9 christos 4, /* size */
316 1.1 skrll 24, /* bitsize */
317 1.1.1.9 christos true, /* pc_relative */
318 1.1 skrll 0, /* bitpos */
319 1.1 skrll complain_overflow_bitfield, /* complain_on_overflow */
320 1.1 skrll bfd_elf_generic_reloc, /* special_function */
321 1.1 skrll "R_MMIX_PC_24", /* name */
322 1.1.1.9 christos false, /* partial_inplace */
323 1.1 skrll ~0xffffff, /* src_mask */
324 1.1 skrll 0xffffff, /* dst_mask */
325 1.1.1.9 christos true), /* pcrel_offset */
326 1.1 skrll
327 1.1 skrll /* A 32 bit absolute PC-relative relocation. */
328 1.1 skrll HOWTO (R_MMIX_PC_32, /* type */
329 1.1 skrll 0, /* rightshift */
330 1.1.1.9 christos 4, /* size */
331 1.1 skrll 32, /* bitsize */
332 1.1.1.9 christos true, /* pc_relative */
333 1.1 skrll 0, /* bitpos */
334 1.1 skrll complain_overflow_bitfield, /* complain_on_overflow */
335 1.1 skrll bfd_elf_generic_reloc, /* special_function */
336 1.1 skrll "R_MMIX_PC_32", /* name */
337 1.1.1.9 christos false, /* partial_inplace */
338 1.1 skrll 0, /* src_mask */
339 1.1 skrll 0xffffffff, /* dst_mask */
340 1.1.1.9 christos true), /* pcrel_offset */
341 1.1 skrll
342 1.1 skrll /* 64 bit PC-relative relocation. */
343 1.1 skrll HOWTO (R_MMIX_PC_64, /* type */
344 1.1 skrll 0, /* rightshift */
345 1.1.1.9 christos 8, /* size */
346 1.1 skrll 64, /* bitsize */
347 1.1.1.9 christos true, /* pc_relative */
348 1.1 skrll 0, /* bitpos */
349 1.1 skrll complain_overflow_bitfield, /* complain_on_overflow */
350 1.1 skrll bfd_elf_generic_reloc, /* special_function */
351 1.1 skrll "R_MMIX_PC_64", /* name */
352 1.1.1.9 christos false, /* partial_inplace */
353 1.1 skrll 0, /* src_mask */
354 1.1 skrll MINUS_ONE, /* dst_mask */
355 1.1.1.9 christos true), /* pcrel_offset */
356 1.1 skrll
357 1.1 skrll /* GNU extension to record C++ vtable hierarchy. */
358 1.1 skrll HOWTO (R_MMIX_GNU_VTINHERIT, /* type */
359 1.1 skrll 0, /* rightshift */
360 1.1.1.9 christos 0, /* size */
361 1.1 skrll 0, /* bitsize */
362 1.1.1.9 christos false, /* pc_relative */
363 1.1 skrll 0, /* bitpos */
364 1.1 skrll complain_overflow_dont, /* complain_on_overflow */
365 1.1 skrll NULL, /* special_function */
366 1.1 skrll "R_MMIX_GNU_VTINHERIT", /* name */
367 1.1.1.9 christos false, /* partial_inplace */
368 1.1 skrll 0, /* src_mask */
369 1.1 skrll 0, /* dst_mask */
370 1.1.1.9 christos true), /* pcrel_offset */
371 1.1 skrll
372 1.1 skrll /* GNU extension to record C++ vtable member usage. */
373 1.1 skrll HOWTO (R_MMIX_GNU_VTENTRY, /* type */
374 1.1 skrll 0, /* rightshift */
375 1.1.1.9 christos 0, /* size */
376 1.1 skrll 0, /* bitsize */
377 1.1.1.9 christos false, /* pc_relative */
378 1.1 skrll 0, /* bitpos */
379 1.1 skrll complain_overflow_dont, /* complain_on_overflow */
380 1.1 skrll _bfd_elf_rel_vtable_reloc_fn, /* special_function */
381 1.1 skrll "R_MMIX_GNU_VTENTRY", /* name */
382 1.1.1.9 christos false, /* partial_inplace */
383 1.1 skrll 0, /* src_mask */
384 1.1 skrll 0, /* dst_mask */
385 1.1.1.9 christos false), /* pcrel_offset */
386 1.1 skrll
387 1.1 skrll /* The GETA relocation is supposed to get any address that could
388 1.1 skrll possibly be reached by the GETA instruction. It can silently expand
389 1.1 skrll to get a 64-bit operand, but will complain if any of the two least
390 1.1 skrll significant bits are set. The howto members reflect a simple GETA. */
391 1.1 skrll HOWTO (R_MMIX_GETA, /* type */
392 1.1 skrll 2, /* rightshift */
393 1.1.1.9 christos 4, /* size */
394 1.1 skrll 19, /* bitsize */
395 1.1.1.9 christos true, /* pc_relative */
396 1.1 skrll 0, /* bitpos */
397 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
398 1.1 skrll mmix_elf_reloc, /* special_function */
399 1.1 skrll "R_MMIX_GETA", /* name */
400 1.1.1.9 christos false, /* partial_inplace */
401 1.1 skrll ~0x0100ffff, /* src_mask */
402 1.1 skrll 0x0100ffff, /* dst_mask */
403 1.1.1.9 christos true), /* pcrel_offset */
404 1.1 skrll
405 1.1 skrll HOWTO (R_MMIX_GETA_1, /* type */
406 1.1 skrll 2, /* rightshift */
407 1.1.1.9 christos 4, /* size */
408 1.1 skrll 19, /* bitsize */
409 1.1.1.9 christos true, /* pc_relative */
410 1.1 skrll 0, /* bitpos */
411 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
412 1.1 skrll mmix_elf_reloc, /* special_function */
413 1.1 skrll "R_MMIX_GETA_1", /* name */
414 1.1.1.9 christos false, /* partial_inplace */
415 1.1 skrll ~0x0100ffff, /* src_mask */
416 1.1 skrll 0x0100ffff, /* dst_mask */
417 1.1.1.9 christos true), /* pcrel_offset */
418 1.1 skrll
419 1.1 skrll HOWTO (R_MMIX_GETA_2, /* type */
420 1.1 skrll 2, /* rightshift */
421 1.1.1.9 christos 4, /* size */
422 1.1 skrll 19, /* bitsize */
423 1.1.1.9 christos true, /* pc_relative */
424 1.1 skrll 0, /* bitpos */
425 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
426 1.1 skrll mmix_elf_reloc, /* special_function */
427 1.1 skrll "R_MMIX_GETA_2", /* name */
428 1.1.1.9 christos false, /* partial_inplace */
429 1.1 skrll ~0x0100ffff, /* src_mask */
430 1.1 skrll 0x0100ffff, /* dst_mask */
431 1.1.1.9 christos true), /* pcrel_offset */
432 1.1 skrll
433 1.1 skrll HOWTO (R_MMIX_GETA_3, /* type */
434 1.1 skrll 2, /* rightshift */
435 1.1.1.9 christos 4, /* size */
436 1.1 skrll 19, /* bitsize */
437 1.1.1.9 christos true, /* pc_relative */
438 1.1 skrll 0, /* bitpos */
439 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
440 1.1 skrll mmix_elf_reloc, /* special_function */
441 1.1 skrll "R_MMIX_GETA_3", /* name */
442 1.1.1.9 christos false, /* partial_inplace */
443 1.1 skrll ~0x0100ffff, /* src_mask */
444 1.1 skrll 0x0100ffff, /* dst_mask */
445 1.1.1.9 christos true), /* pcrel_offset */
446 1.1 skrll
447 1.1 skrll /* The conditional branches are supposed to reach any (code) address.
448 1.1 skrll It can silently expand to a 64-bit operand, but will emit an error if
449 1.1 skrll any of the two least significant bits are set. The howto members
450 1.1 skrll reflect a simple branch. */
451 1.1 skrll HOWTO (R_MMIX_CBRANCH, /* type */
452 1.1 skrll 2, /* rightshift */
453 1.1.1.9 christos 4, /* size */
454 1.1 skrll 19, /* bitsize */
455 1.1.1.9 christos true, /* pc_relative */
456 1.1 skrll 0, /* bitpos */
457 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
458 1.1 skrll mmix_elf_reloc, /* special_function */
459 1.1 skrll "R_MMIX_CBRANCH", /* name */
460 1.1.1.9 christos false, /* partial_inplace */
461 1.1 skrll ~0x0100ffff, /* src_mask */
462 1.1 skrll 0x0100ffff, /* dst_mask */
463 1.1.1.9 christos true), /* pcrel_offset */
464 1.1 skrll
465 1.1 skrll HOWTO (R_MMIX_CBRANCH_J, /* type */
466 1.1 skrll 2, /* rightshift */
467 1.1.1.9 christos 4, /* size */
468 1.1 skrll 19, /* bitsize */
469 1.1.1.9 christos true, /* pc_relative */
470 1.1 skrll 0, /* bitpos */
471 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
472 1.1 skrll mmix_elf_reloc, /* special_function */
473 1.1 skrll "R_MMIX_CBRANCH_J", /* name */
474 1.1.1.9 christos false, /* partial_inplace */
475 1.1 skrll ~0x0100ffff, /* src_mask */
476 1.1 skrll 0x0100ffff, /* dst_mask */
477 1.1.1.9 christos true), /* pcrel_offset */
478 1.1 skrll
479 1.1 skrll HOWTO (R_MMIX_CBRANCH_1, /* type */
480 1.1 skrll 2, /* rightshift */
481 1.1.1.9 christos 4, /* size */
482 1.1 skrll 19, /* bitsize */
483 1.1.1.9 christos true, /* pc_relative */
484 1.1 skrll 0, /* bitpos */
485 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
486 1.1 skrll mmix_elf_reloc, /* special_function */
487 1.1 skrll "R_MMIX_CBRANCH_1", /* name */
488 1.1.1.9 christos false, /* partial_inplace */
489 1.1 skrll ~0x0100ffff, /* src_mask */
490 1.1 skrll 0x0100ffff, /* dst_mask */
491 1.1.1.9 christos true), /* pcrel_offset */
492 1.1 skrll
493 1.1 skrll HOWTO (R_MMIX_CBRANCH_2, /* type */
494 1.1 skrll 2, /* rightshift */
495 1.1.1.9 christos 4, /* size */
496 1.1 skrll 19, /* bitsize */
497 1.1.1.9 christos true, /* pc_relative */
498 1.1 skrll 0, /* bitpos */
499 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
500 1.1 skrll mmix_elf_reloc, /* special_function */
501 1.1 skrll "R_MMIX_CBRANCH_2", /* name */
502 1.1.1.9 christos false, /* partial_inplace */
503 1.1 skrll ~0x0100ffff, /* src_mask */
504 1.1 skrll 0x0100ffff, /* dst_mask */
505 1.1.1.9 christos true), /* pcrel_offset */
506 1.1 skrll
507 1.1 skrll HOWTO (R_MMIX_CBRANCH_3, /* type */
508 1.1 skrll 2, /* rightshift */
509 1.1.1.9 christos 4, /* size */
510 1.1 skrll 19, /* bitsize */
511 1.1.1.9 christos true, /* pc_relative */
512 1.1 skrll 0, /* bitpos */
513 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
514 1.1 skrll mmix_elf_reloc, /* special_function */
515 1.1 skrll "R_MMIX_CBRANCH_3", /* name */
516 1.1.1.9 christos false, /* partial_inplace */
517 1.1 skrll ~0x0100ffff, /* src_mask */
518 1.1 skrll 0x0100ffff, /* dst_mask */
519 1.1.1.9 christos true), /* pcrel_offset */
520 1.1 skrll
521 1.1 skrll /* The PUSHJ instruction can reach any (code) address, as long as it's
522 1.1 skrll the beginning of a function (no usable restriction). It can silently
523 1.1 skrll expand to a 64-bit operand, but will emit an error if any of the two
524 1.1 skrll least significant bits are set. It can also expand into a call to a
525 1.1 skrll stub; see R_MMIX_PUSHJ_STUBBABLE. The howto members reflect a simple
526 1.1 skrll PUSHJ. */
527 1.1 skrll HOWTO (R_MMIX_PUSHJ, /* type */
528 1.1 skrll 2, /* rightshift */
529 1.1.1.9 christos 4, /* size */
530 1.1 skrll 19, /* bitsize */
531 1.1.1.9 christos true, /* pc_relative */
532 1.1 skrll 0, /* bitpos */
533 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
534 1.1 skrll mmix_elf_reloc, /* special_function */
535 1.1 skrll "R_MMIX_PUSHJ", /* name */
536 1.1.1.9 christos false, /* partial_inplace */
537 1.1 skrll ~0x0100ffff, /* src_mask */
538 1.1 skrll 0x0100ffff, /* dst_mask */
539 1.1.1.9 christos true), /* pcrel_offset */
540 1.1 skrll
541 1.1 skrll HOWTO (R_MMIX_PUSHJ_1, /* type */
542 1.1 skrll 2, /* rightshift */
543 1.1.1.9 christos 4, /* size */
544 1.1 skrll 19, /* bitsize */
545 1.1.1.9 christos true, /* pc_relative */
546 1.1 skrll 0, /* bitpos */
547 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
548 1.1 skrll mmix_elf_reloc, /* special_function */
549 1.1 skrll "R_MMIX_PUSHJ_1", /* name */
550 1.1.1.9 christos false, /* partial_inplace */
551 1.1 skrll ~0x0100ffff, /* src_mask */
552 1.1 skrll 0x0100ffff, /* dst_mask */
553 1.1.1.9 christos true), /* pcrel_offset */
554 1.1 skrll
555 1.1 skrll HOWTO (R_MMIX_PUSHJ_2, /* type */
556 1.1 skrll 2, /* rightshift */
557 1.1.1.9 christos 4, /* size */
558 1.1 skrll 19, /* bitsize */
559 1.1.1.9 christos true, /* pc_relative */
560 1.1 skrll 0, /* bitpos */
561 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
562 1.1 skrll mmix_elf_reloc, /* special_function */
563 1.1 skrll "R_MMIX_PUSHJ_2", /* name */
564 1.1.1.9 christos false, /* partial_inplace */
565 1.1 skrll ~0x0100ffff, /* src_mask */
566 1.1 skrll 0x0100ffff, /* dst_mask */
567 1.1.1.9 christos true), /* pcrel_offset */
568 1.1 skrll
569 1.1 skrll HOWTO (R_MMIX_PUSHJ_3, /* type */
570 1.1 skrll 2, /* rightshift */
571 1.1.1.9 christos 4, /* size */
572 1.1 skrll 19, /* bitsize */
573 1.1.1.9 christos true, /* pc_relative */
574 1.1 skrll 0, /* bitpos */
575 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
576 1.1 skrll mmix_elf_reloc, /* special_function */
577 1.1 skrll "R_MMIX_PUSHJ_3", /* name */
578 1.1.1.9 christos false, /* partial_inplace */
579 1.1 skrll ~0x0100ffff, /* src_mask */
580 1.1 skrll 0x0100ffff, /* dst_mask */
581 1.1.1.9 christos true), /* pcrel_offset */
582 1.1 skrll
583 1.1 skrll /* A JMP is supposed to reach any (code) address. By itself, it can
584 1.1 skrll reach +-64M; the expansion can reach all 64 bits. Note that the 64M
585 1.1 skrll limit is soon reached if you link the program in wildly different
586 1.1 skrll memory segments. The howto members reflect a trivial JMP. */
587 1.1 skrll HOWTO (R_MMIX_JMP, /* type */
588 1.1 skrll 2, /* rightshift */
589 1.1.1.9 christos 4, /* size */
590 1.1 skrll 27, /* bitsize */
591 1.1.1.9 christos true, /* pc_relative */
592 1.1 skrll 0, /* bitpos */
593 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
594 1.1 skrll mmix_elf_reloc, /* special_function */
595 1.1 skrll "R_MMIX_JMP", /* name */
596 1.1.1.9 christos false, /* partial_inplace */
597 1.1 skrll ~0x1ffffff, /* src_mask */
598 1.1 skrll 0x1ffffff, /* dst_mask */
599 1.1.1.9 christos true), /* pcrel_offset */
600 1.1 skrll
601 1.1 skrll HOWTO (R_MMIX_JMP_1, /* type */
602 1.1 skrll 2, /* rightshift */
603 1.1.1.9 christos 4, /* size */
604 1.1 skrll 27, /* bitsize */
605 1.1.1.9 christos true, /* pc_relative */
606 1.1 skrll 0, /* bitpos */
607 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
608 1.1 skrll mmix_elf_reloc, /* special_function */
609 1.1 skrll "R_MMIX_JMP_1", /* name */
610 1.1.1.9 christos false, /* partial_inplace */
611 1.1 skrll ~0x1ffffff, /* src_mask */
612 1.1 skrll 0x1ffffff, /* dst_mask */
613 1.1.1.9 christos true), /* pcrel_offset */
614 1.1 skrll
615 1.1 skrll HOWTO (R_MMIX_JMP_2, /* type */
616 1.1 skrll 2, /* rightshift */
617 1.1.1.9 christos 4, /* size */
618 1.1 skrll 27, /* bitsize */
619 1.1.1.9 christos true, /* pc_relative */
620 1.1 skrll 0, /* bitpos */
621 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
622 1.1 skrll mmix_elf_reloc, /* special_function */
623 1.1 skrll "R_MMIX_JMP_2", /* name */
624 1.1.1.9 christos false, /* partial_inplace */
625 1.1 skrll ~0x1ffffff, /* src_mask */
626 1.1 skrll 0x1ffffff, /* dst_mask */
627 1.1.1.9 christos true), /* pcrel_offset */
628 1.1 skrll
629 1.1 skrll HOWTO (R_MMIX_JMP_3, /* type */
630 1.1 skrll 2, /* rightshift */
631 1.1.1.9 christos 4, /* size */
632 1.1 skrll 27, /* bitsize */
633 1.1.1.9 christos true, /* pc_relative */
634 1.1 skrll 0, /* bitpos */
635 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
636 1.1 skrll mmix_elf_reloc, /* special_function */
637 1.1 skrll "R_MMIX_JMP_3", /* name */
638 1.1.1.9 christos false, /* partial_inplace */
639 1.1 skrll ~0x1ffffff, /* src_mask */
640 1.1 skrll 0x1ffffff, /* dst_mask */
641 1.1.1.9 christos true), /* pcrel_offset */
642 1.1 skrll
643 1.1 skrll /* When we don't emit link-time-relaxable code from the assembler, or
644 1.1 skrll when relaxation has done all it can do, these relocs are used. For
645 1.1 skrll GETA/PUSHJ/branches. */
646 1.1 skrll HOWTO (R_MMIX_ADDR19, /* type */
647 1.1 skrll 2, /* rightshift */
648 1.1.1.9 christos 4, /* size */
649 1.1 skrll 19, /* bitsize */
650 1.1.1.9 christos true, /* pc_relative */
651 1.1 skrll 0, /* bitpos */
652 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
653 1.1 skrll mmix_elf_reloc, /* special_function */
654 1.1 skrll "R_MMIX_ADDR19", /* name */
655 1.1.1.9 christos false, /* partial_inplace */
656 1.1 skrll ~0x0100ffff, /* src_mask */
657 1.1 skrll 0x0100ffff, /* dst_mask */
658 1.1.1.9 christos true), /* pcrel_offset */
659 1.1 skrll
660 1.1 skrll /* For JMP. */
661 1.1 skrll HOWTO (R_MMIX_ADDR27, /* type */
662 1.1 skrll 2, /* rightshift */
663 1.1.1.9 christos 4, /* size */
664 1.1 skrll 27, /* bitsize */
665 1.1.1.9 christos true, /* pc_relative */
666 1.1 skrll 0, /* bitpos */
667 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
668 1.1 skrll mmix_elf_reloc, /* special_function */
669 1.1 skrll "R_MMIX_ADDR27", /* name */
670 1.1.1.9 christos false, /* partial_inplace */
671 1.1 skrll ~0x1ffffff, /* src_mask */
672 1.1 skrll 0x1ffffff, /* dst_mask */
673 1.1.1.9 christos true), /* pcrel_offset */
674 1.1 skrll
675 1.1 skrll /* A general register or the value 0..255. If a value, then the
676 1.1 skrll instruction (offset -3) needs adjusting. */
677 1.1 skrll HOWTO (R_MMIX_REG_OR_BYTE, /* type */
678 1.1 skrll 0, /* rightshift */
679 1.1.1.9 christos 2, /* size */
680 1.1 skrll 8, /* bitsize */
681 1.1.1.9 christos false, /* pc_relative */
682 1.1 skrll 0, /* bitpos */
683 1.1 skrll complain_overflow_bitfield, /* complain_on_overflow */
684 1.1 skrll mmix_elf_reloc, /* special_function */
685 1.1 skrll "R_MMIX_REG_OR_BYTE", /* name */
686 1.1.1.9 christos false, /* partial_inplace */
687 1.1 skrll 0, /* src_mask */
688 1.1 skrll 0xff, /* dst_mask */
689 1.1.1.9 christos false), /* pcrel_offset */
690 1.1 skrll
691 1.1 skrll /* A general register. */
692 1.1 skrll HOWTO (R_MMIX_REG, /* type */
693 1.1 skrll 0, /* rightshift */
694 1.1.1.9 christos 2, /* size */
695 1.1 skrll 8, /* bitsize */
696 1.1.1.9 christos false, /* pc_relative */
697 1.1 skrll 0, /* bitpos */
698 1.1 skrll complain_overflow_bitfield, /* complain_on_overflow */
699 1.1 skrll mmix_elf_reloc, /* special_function */
700 1.1 skrll "R_MMIX_REG", /* name */
701 1.1.1.9 christos false, /* partial_inplace */
702 1.1 skrll 0, /* src_mask */
703 1.1 skrll 0xff, /* dst_mask */
704 1.1.1.9 christos false), /* pcrel_offset */
705 1.1 skrll
706 1.1 skrll /* A register plus an index, corresponding to the relocation expression.
707 1.1 skrll The sizes must correspond to the valid range of the expression, while
708 1.1 skrll the bitmasks correspond to what we store in the image. */
709 1.1 skrll HOWTO (R_MMIX_BASE_PLUS_OFFSET, /* type */
710 1.1 skrll 0, /* rightshift */
711 1.1.1.9 christos 8, /* size */
712 1.1 skrll 64, /* bitsize */
713 1.1.1.9 christos false, /* pc_relative */
714 1.1 skrll 0, /* bitpos */
715 1.1 skrll complain_overflow_bitfield, /* complain_on_overflow */
716 1.1 skrll mmix_elf_reloc, /* special_function */
717 1.1 skrll "R_MMIX_BASE_PLUS_OFFSET", /* name */
718 1.1.1.9 christos false, /* partial_inplace */
719 1.1 skrll 0, /* src_mask */
720 1.1 skrll 0xffff, /* dst_mask */
721 1.1.1.9 christos false), /* pcrel_offset */
722 1.1 skrll
723 1.1 skrll /* A "magic" relocation for a LOCAL expression, asserting that the
724 1.1 skrll expression is less than the number of global registers. No actual
725 1.1 skrll modification of the contents is done. Implementing this as a
726 1.1 skrll relocation was less intrusive than e.g. putting such expressions in a
727 1.1 skrll section to discard *after* relocation. */
728 1.1 skrll HOWTO (R_MMIX_LOCAL, /* type */
729 1.1 skrll 0, /* rightshift */
730 1.1.1.9 christos 0, /* size */
731 1.1 skrll 0, /* bitsize */
732 1.1.1.9 christos false, /* pc_relative */
733 1.1 skrll 0, /* bitpos */
734 1.1 skrll complain_overflow_dont, /* complain_on_overflow */
735 1.1 skrll mmix_elf_reloc, /* special_function */
736 1.1 skrll "R_MMIX_LOCAL", /* name */
737 1.1.1.9 christos false, /* partial_inplace */
738 1.1 skrll 0, /* src_mask */
739 1.1 skrll 0, /* dst_mask */
740 1.1.1.9 christos false), /* pcrel_offset */
741 1.1 skrll
742 1.1 skrll HOWTO (R_MMIX_PUSHJ_STUBBABLE, /* type */
743 1.1 skrll 2, /* rightshift */
744 1.1.1.9 christos 4, /* size */
745 1.1 skrll 19, /* bitsize */
746 1.1.1.9 christos true, /* pc_relative */
747 1.1 skrll 0, /* bitpos */
748 1.1 skrll complain_overflow_signed, /* complain_on_overflow */
749 1.1 skrll mmix_elf_reloc, /* special_function */
750 1.1 skrll "R_MMIX_PUSHJ_STUBBABLE", /* name */
751 1.1.1.9 christos false, /* partial_inplace */
752 1.1 skrll ~0x0100ffff, /* src_mask */
753 1.1 skrll 0x0100ffff, /* dst_mask */
754 1.1.1.9 christos true) /* pcrel_offset */
755 1.1 skrll };
756 1.1 skrll
757 1.1 skrll
758 1.1 skrll /* Map BFD reloc types to MMIX ELF reloc types. */
759 1.1 skrll
760 1.1 skrll struct mmix_reloc_map
761 1.1 skrll {
762 1.1 skrll bfd_reloc_code_real_type bfd_reloc_val;
763 1.1 skrll enum elf_mmix_reloc_type elf_reloc_val;
764 1.1 skrll };
765 1.1 skrll
766 1.1 skrll
767 1.1 skrll static const struct mmix_reloc_map mmix_reloc_map[] =
768 1.1 skrll {
769 1.1 skrll {BFD_RELOC_NONE, R_MMIX_NONE},
770 1.1 skrll {BFD_RELOC_8, R_MMIX_8},
771 1.1 skrll {BFD_RELOC_16, R_MMIX_16},
772 1.1 skrll {BFD_RELOC_24, R_MMIX_24},
773 1.1 skrll {BFD_RELOC_32, R_MMIX_32},
774 1.1 skrll {BFD_RELOC_64, R_MMIX_64},
775 1.1 skrll {BFD_RELOC_8_PCREL, R_MMIX_PC_8},
776 1.1 skrll {BFD_RELOC_16_PCREL, R_MMIX_PC_16},
777 1.1 skrll {BFD_RELOC_24_PCREL, R_MMIX_PC_24},
778 1.1 skrll {BFD_RELOC_32_PCREL, R_MMIX_PC_32},
779 1.1 skrll {BFD_RELOC_64_PCREL, R_MMIX_PC_64},
780 1.1 skrll {BFD_RELOC_VTABLE_INHERIT, R_MMIX_GNU_VTINHERIT},
781 1.1 skrll {BFD_RELOC_VTABLE_ENTRY, R_MMIX_GNU_VTENTRY},
782 1.1 skrll {BFD_RELOC_MMIX_GETA, R_MMIX_GETA},
783 1.1 skrll {BFD_RELOC_MMIX_CBRANCH, R_MMIX_CBRANCH},
784 1.1 skrll {BFD_RELOC_MMIX_PUSHJ, R_MMIX_PUSHJ},
785 1.1 skrll {BFD_RELOC_MMIX_JMP, R_MMIX_JMP},
786 1.1 skrll {BFD_RELOC_MMIX_ADDR19, R_MMIX_ADDR19},
787 1.1 skrll {BFD_RELOC_MMIX_ADDR27, R_MMIX_ADDR27},
788 1.1 skrll {BFD_RELOC_MMIX_REG_OR_BYTE, R_MMIX_REG_OR_BYTE},
789 1.1 skrll {BFD_RELOC_MMIX_REG, R_MMIX_REG},
790 1.1 skrll {BFD_RELOC_MMIX_BASE_PLUS_OFFSET, R_MMIX_BASE_PLUS_OFFSET},
791 1.1 skrll {BFD_RELOC_MMIX_LOCAL, R_MMIX_LOCAL},
792 1.1 skrll {BFD_RELOC_MMIX_PUSHJ_STUBBABLE, R_MMIX_PUSHJ_STUBBABLE}
793 1.1 skrll };
794 1.1 skrll
795 1.1 skrll static reloc_howto_type *
796 1.1.1.3 christos bfd_elf64_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
797 1.1.1.3 christos bfd_reloc_code_real_type code)
798 1.1 skrll {
799 1.1 skrll unsigned int i;
800 1.1 skrll
801 1.1 skrll for (i = 0;
802 1.1 skrll i < sizeof (mmix_reloc_map) / sizeof (mmix_reloc_map[0]);
803 1.1 skrll i++)
804 1.1 skrll {
805 1.1 skrll if (mmix_reloc_map[i].bfd_reloc_val == code)
806 1.1 skrll return &elf_mmix_howto_table[mmix_reloc_map[i].elf_reloc_val];
807 1.1 skrll }
808 1.1 skrll
809 1.1 skrll return NULL;
810 1.1 skrll }
811 1.1 skrll
812 1.1 skrll static reloc_howto_type *
813 1.1 skrll bfd_elf64_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
814 1.1 skrll const char *r_name)
815 1.1 skrll {
816 1.1 skrll unsigned int i;
817 1.1 skrll
818 1.1 skrll for (i = 0;
819 1.1 skrll i < sizeof (elf_mmix_howto_table) / sizeof (elf_mmix_howto_table[0]);
820 1.1 skrll i++)
821 1.1 skrll if (elf_mmix_howto_table[i].name != NULL
822 1.1 skrll && strcasecmp (elf_mmix_howto_table[i].name, r_name) == 0)
823 1.1 skrll return &elf_mmix_howto_table[i];
824 1.1 skrll
825 1.1 skrll return NULL;
826 1.1 skrll }
827 1.1 skrll
828 1.1.1.9 christos static bool
829 1.1.1.3 christos mmix_elf_new_section_hook (bfd *abfd, asection *sec)
830 1.1 skrll {
831 1.1.1.11 christos struct _mmix_elf_section_data *sdata;
832 1.1 skrll
833 1.1.1.11 christos sdata = bfd_zalloc (abfd, sizeof (*sdata));
834 1.1.1.11 christos if (sdata == NULL)
835 1.1.1.11 christos return false;
836 1.1.1.11 christos sec->used_by_bfd = sdata;
837 1.1 skrll
838 1.1 skrll return _bfd_elf_new_section_hook (abfd, sec);
839 1.1 skrll }
840 1.1 skrll
841 1.1 skrll
842 1.1 skrll /* This function performs the actual bitfiddling and sanity check for a
843 1.1 skrll final relocation. Each relocation gets its *worst*-case expansion
844 1.1 skrll in size when it arrives here; any reduction in size should have been
845 1.1 skrll caught in linker relaxation earlier. When we get here, the relocation
846 1.1 skrll looks like the smallest instruction with SWYM:s (nop:s) appended to the
847 1.1 skrll max size. We fill in those nop:s.
848 1.1 skrll
849 1.1 skrll R_MMIX_GETA: (FIXME: Relaxation should break this up in 1, 2, 3 tetra)
850 1.1 skrll GETA $N,foo
851 1.1 skrll ->
852 1.1 skrll SETL $N,foo & 0xffff
853 1.1 skrll INCML $N,(foo >> 16) & 0xffff
854 1.1 skrll INCMH $N,(foo >> 32) & 0xffff
855 1.1 skrll INCH $N,(foo >> 48) & 0xffff
856 1.1 skrll
857 1.1 skrll R_MMIX_CBRANCH: (FIXME: Relaxation should break this up, but
858 1.1 skrll condbranches needing relaxation might be rare enough to not be
859 1.1 skrll worthwhile.)
860 1.1 skrll [P]Bcc $N,foo
861 1.1 skrll ->
862 1.1 skrll [~P]B~cc $N,.+20
863 1.1 skrll SETL $255,foo & ...
864 1.1 skrll INCML ...
865 1.1 skrll INCMH ...
866 1.1 skrll INCH ...
867 1.1 skrll GO $255,$255,0
868 1.1 skrll
869 1.1 skrll R_MMIX_PUSHJ: (FIXME: Relaxation...)
870 1.1 skrll PUSHJ $N,foo
871 1.1 skrll ->
872 1.1 skrll SETL $255,foo & ...
873 1.1 skrll INCML ...
874 1.1 skrll INCMH ...
875 1.1 skrll INCH ...
876 1.1 skrll PUSHGO $N,$255,0
877 1.1 skrll
878 1.1 skrll R_MMIX_JMP: (FIXME: Relaxation...)
879 1.1 skrll JMP foo
880 1.1 skrll ->
881 1.1 skrll SETL $255,foo & ...
882 1.1 skrll INCML ...
883 1.1 skrll INCMH ...
884 1.1 skrll INCH ...
885 1.1 skrll GO $255,$255,0
886 1.1 skrll
887 1.1 skrll R_MMIX_ADDR19 and R_MMIX_ADDR27 are just filled in. */
888 1.1 skrll
889 1.1 skrll static bfd_reloc_status_type
890 1.1.1.3 christos mmix_elf_perform_relocation (asection *isec, reloc_howto_type *howto,
891 1.1.1.3 christos void *datap, bfd_vma addr, bfd_vma value,
892 1.1.1.3 christos char **error_message)
893 1.1 skrll {
894 1.1 skrll bfd *abfd = isec->owner;
895 1.1 skrll bfd_reloc_status_type flag = bfd_reloc_ok;
896 1.1 skrll bfd_reloc_status_type r;
897 1.1 skrll int offs = 0;
898 1.1 skrll int reg = 255;
899 1.1 skrll
900 1.1 skrll /* The worst case bits are all similar SETL/INCML/INCMH/INCH sequences.
901 1.1 skrll We handle the differences here and the common sequence later. */
902 1.1 skrll switch (howto->type)
903 1.1 skrll {
904 1.1 skrll case R_MMIX_GETA:
905 1.1 skrll offs = 0;
906 1.1 skrll reg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
907 1.1 skrll
908 1.1 skrll /* We change to an absolute value. */
909 1.1 skrll value += addr;
910 1.1 skrll break;
911 1.1 skrll
912 1.1 skrll case R_MMIX_CBRANCH:
913 1.1 skrll {
914 1.1 skrll int in1 = bfd_get_16 (abfd, (bfd_byte *) datap) << 16;
915 1.1 skrll
916 1.1 skrll /* Invert the condition and prediction bit, and set the offset
917 1.1 skrll to five instructions ahead.
918 1.1 skrll
919 1.1 skrll We *can* do better if we want to. If the branch is found to be
920 1.1 skrll within limits, we could leave the branch as is; there'll just
921 1.1 skrll be a bunch of NOP:s after it. But we shouldn't see this
922 1.1 skrll sequence often enough that it's worth doing it. */
923 1.1 skrll
924 1.1 skrll bfd_put_32 (abfd,
925 1.1 skrll (((in1 ^ ((PRED_INV_BIT | COND_INV_BIT) << 24)) & ~0xffff)
926 1.1 skrll | (24/4)),
927 1.1 skrll (bfd_byte *) datap);
928 1.1 skrll
929 1.1 skrll /* Put a "GO $255,$255,0" after the common sequence. */
930 1.1 skrll bfd_put_32 (abfd,
931 1.1 skrll ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24) | 0xffff00,
932 1.1 skrll (bfd_byte *) datap + 20);
933 1.1 skrll
934 1.1 skrll /* Common sequence starts at offset 4. */
935 1.1 skrll offs = 4;
936 1.1 skrll
937 1.1 skrll /* We change to an absolute value. */
938 1.1 skrll value += addr;
939 1.1 skrll }
940 1.1 skrll break;
941 1.1 skrll
942 1.1 skrll case R_MMIX_PUSHJ_STUBBABLE:
943 1.1 skrll /* If the address fits, we're fine. */
944 1.1 skrll if ((value & 3) == 0
945 1.1 skrll /* Note rightshift 0; see R_MMIX_JMP case below. */
946 1.1 skrll && (r = bfd_check_overflow (complain_overflow_signed,
947 1.1 skrll howto->bitsize,
948 1.1 skrll 0,
949 1.1 skrll bfd_arch_bits_per_address (abfd),
950 1.1 skrll value)) == bfd_reloc_ok)
951 1.1 skrll goto pcrel_mmix_reloc_fits;
952 1.1 skrll else
953 1.1 skrll {
954 1.1 skrll bfd_size_type size = isec->rawsize ? isec->rawsize : isec->size;
955 1.1 skrll
956 1.1 skrll /* We have the bytes at the PUSHJ insn and need to get the
957 1.1 skrll position for the stub. There's supposed to be room allocated
958 1.1 skrll for the stub. */
959 1.1 skrll bfd_byte *stubcontents
960 1.1 skrll = ((bfd_byte *) datap
961 1.1 skrll - (addr - (isec->output_section->vma + isec->output_offset))
962 1.1 skrll + size
963 1.1 skrll + mmix_elf_section_data (isec)->pjs.stub_offset);
964 1.1 skrll bfd_vma stubaddr;
965 1.1 skrll
966 1.1.1.3 christos if (mmix_elf_section_data (isec)->pjs.n_pushj_relocs == 0)
967 1.1.1.3 christos {
968 1.1.1.3 christos /* This shouldn't happen when linking to ELF or mmo, so
969 1.1.1.3 christos this is an attempt to link to "binary", right? We
970 1.1.1.3 christos can't access the output bfd, so we can't verify that
971 1.1.1.3 christos assumption. We only know that the critical
972 1.1.1.3 christos mmix_elf_check_common_relocs has not been called,
973 1.1.1.3 christos which happens when the output format is different
974 1.1.1.3 christos from the input format (and is not mmo). */
975 1.1.1.3 christos if (! mmix_elf_section_data (isec)->has_warned_pushj)
976 1.1.1.3 christos {
977 1.1.1.3 christos /* For the first such error per input section, produce
978 1.1.1.3 christos a verbose message. */
979 1.1.1.3 christos *error_message
980 1.1.1.3 christos = _("invalid input relocation when producing"
981 1.1.1.7 christos " non-ELF, non-mmo format output;"
982 1.1.1.7 christos " please use the objcopy program to convert from"
983 1.1.1.3 christos " ELF or mmo,"
984 1.1.1.7 christos " or assemble using"
985 1.1.1.3 christos " \"-no-expand\" (for gcc, \"-Wa,-no-expand\"");
986 1.1.1.9 christos mmix_elf_section_data (isec)->has_warned_pushj = true;
987 1.1.1.3 christos return bfd_reloc_dangerous;
988 1.1.1.3 christos }
989 1.1.1.3 christos
990 1.1.1.3 christos /* For subsequent errors, return this one, which is
991 1.1.1.3 christos rate-limited but looks a little bit different,
992 1.1.1.3 christos hopefully without affecting user-friendliness. */
993 1.1.1.3 christos return bfd_reloc_overflow;
994 1.1.1.3 christos }
995 1.1.1.3 christos
996 1.1 skrll /* The address doesn't fit, so redirect the PUSHJ to the
997 1.1 skrll location of the stub. */
998 1.1 skrll r = mmix_elf_perform_relocation (isec,
999 1.1 skrll &elf_mmix_howto_table
1000 1.1 skrll [R_MMIX_ADDR19],
1001 1.1 skrll datap,
1002 1.1 skrll addr,
1003 1.1 skrll isec->output_section->vma
1004 1.1 skrll + isec->output_offset
1005 1.1 skrll + size
1006 1.1 skrll + (mmix_elf_section_data (isec)
1007 1.1 skrll ->pjs.stub_offset)
1008 1.1.1.3 christos - addr,
1009 1.1.1.3 christos error_message);
1010 1.1 skrll if (r != bfd_reloc_ok)
1011 1.1 skrll return r;
1012 1.1 skrll
1013 1.1 skrll stubaddr
1014 1.1 skrll = (isec->output_section->vma
1015 1.1 skrll + isec->output_offset
1016 1.1 skrll + size
1017 1.1 skrll + mmix_elf_section_data (isec)->pjs.stub_offset);
1018 1.1 skrll
1019 1.1 skrll /* We generate a simple JMP if that suffices, else the whole 5
1020 1.1 skrll insn stub. */
1021 1.1 skrll if (bfd_check_overflow (complain_overflow_signed,
1022 1.1 skrll elf_mmix_howto_table[R_MMIX_ADDR27].bitsize,
1023 1.1 skrll 0,
1024 1.1 skrll bfd_arch_bits_per_address (abfd),
1025 1.1 skrll addr + value - stubaddr) == bfd_reloc_ok)
1026 1.1 skrll {
1027 1.1 skrll bfd_put_32 (abfd, JMP_INSN_BYTE << 24, stubcontents);
1028 1.1 skrll r = mmix_elf_perform_relocation (isec,
1029 1.1 skrll &elf_mmix_howto_table
1030 1.1 skrll [R_MMIX_ADDR27],
1031 1.1 skrll stubcontents,
1032 1.1 skrll stubaddr,
1033 1.1.1.3 christos value + addr - stubaddr,
1034 1.1.1.3 christos error_message);
1035 1.1 skrll mmix_elf_section_data (isec)->pjs.stub_offset += 4;
1036 1.1 skrll
1037 1.1 skrll if (size + mmix_elf_section_data (isec)->pjs.stub_offset
1038 1.1 skrll > isec->size)
1039 1.1 skrll abort ();
1040 1.1 skrll
1041 1.1 skrll return r;
1042 1.1 skrll }
1043 1.1 skrll else
1044 1.1 skrll {
1045 1.1 skrll /* Put a "GO $255,0" after the common sequence. */
1046 1.1 skrll bfd_put_32 (abfd,
1047 1.1 skrll ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
1048 1.1 skrll | 0xff00, (bfd_byte *) stubcontents + 16);
1049 1.1 skrll
1050 1.1 skrll /* Prepare for the general code to set the first part of the
1051 1.1 skrll linker stub, and */
1052 1.1 skrll value += addr;
1053 1.1 skrll datap = stubcontents;
1054 1.1 skrll mmix_elf_section_data (isec)->pjs.stub_offset
1055 1.1 skrll += MAX_PUSHJ_STUB_SIZE;
1056 1.1 skrll }
1057 1.1 skrll }
1058 1.1 skrll break;
1059 1.1 skrll
1060 1.1 skrll case R_MMIX_PUSHJ:
1061 1.1 skrll {
1062 1.1 skrll int inreg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
1063 1.1 skrll
1064 1.1 skrll /* Put a "PUSHGO $N,$255,0" after the common sequence. */
1065 1.1 skrll bfd_put_32 (abfd,
1066 1.1 skrll ((PUSHGO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
1067 1.1 skrll | (inreg << 16)
1068 1.1 skrll | 0xff00,
1069 1.1 skrll (bfd_byte *) datap + 16);
1070 1.1 skrll
1071 1.1 skrll /* We change to an absolute value. */
1072 1.1 skrll value += addr;
1073 1.1 skrll }
1074 1.1 skrll break;
1075 1.1 skrll
1076 1.1 skrll case R_MMIX_JMP:
1077 1.1 skrll /* This one is a little special. If we get here on a non-relaxing
1078 1.1 skrll link, and the destination is actually in range, we don't need to
1079 1.1 skrll execute the nops.
1080 1.1 skrll If so, we fall through to the bit-fiddling relocs.
1081 1.1 skrll
1082 1.1 skrll FIXME: bfd_check_overflow seems broken; the relocation is
1083 1.1 skrll rightshifted before testing, so supply a zero rightshift. */
1084 1.1 skrll
1085 1.1 skrll if (! ((value & 3) == 0
1086 1.1 skrll && (r = bfd_check_overflow (complain_overflow_signed,
1087 1.1 skrll howto->bitsize,
1088 1.1 skrll 0,
1089 1.1 skrll bfd_arch_bits_per_address (abfd),
1090 1.1 skrll value)) == bfd_reloc_ok))
1091 1.1 skrll {
1092 1.1 skrll /* If the relocation doesn't fit in a JMP, we let the NOP:s be
1093 1.1 skrll modified below, and put a "GO $255,$255,0" after the
1094 1.1 skrll address-loading sequence. */
1095 1.1 skrll bfd_put_32 (abfd,
1096 1.1 skrll ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
1097 1.1 skrll | 0xffff00,
1098 1.1 skrll (bfd_byte *) datap + 16);
1099 1.1 skrll
1100 1.1 skrll /* We change to an absolute value. */
1101 1.1 skrll value += addr;
1102 1.1 skrll break;
1103 1.1 skrll }
1104 1.1 skrll /* FALLTHROUGH. */
1105 1.1 skrll case R_MMIX_ADDR19:
1106 1.1 skrll case R_MMIX_ADDR27:
1107 1.1 skrll pcrel_mmix_reloc_fits:
1108 1.1 skrll /* These must be in range, or else we emit an error. */
1109 1.1 skrll if ((value & 3) == 0
1110 1.1 skrll /* Note rightshift 0; see above. */
1111 1.1 skrll && (r = bfd_check_overflow (complain_overflow_signed,
1112 1.1 skrll howto->bitsize,
1113 1.1 skrll 0,
1114 1.1 skrll bfd_arch_bits_per_address (abfd),
1115 1.1 skrll value)) == bfd_reloc_ok)
1116 1.1 skrll {
1117 1.1 skrll bfd_vma in1
1118 1.1 skrll = bfd_get_32 (abfd, (bfd_byte *) datap);
1119 1.1 skrll bfd_vma highbit;
1120 1.1 skrll
1121 1.1 skrll if ((bfd_signed_vma) value < 0)
1122 1.1 skrll {
1123 1.1 skrll highbit = 1 << 24;
1124 1.1 skrll value += (1 << (howto->bitsize - 1));
1125 1.1 skrll }
1126 1.1 skrll else
1127 1.1 skrll highbit = 0;
1128 1.1 skrll
1129 1.1 skrll value >>= 2;
1130 1.1 skrll
1131 1.1 skrll bfd_put_32 (abfd,
1132 1.1 skrll (in1 & howto->src_mask)
1133 1.1 skrll | highbit
1134 1.1 skrll | (value & howto->dst_mask),
1135 1.1 skrll (bfd_byte *) datap);
1136 1.1 skrll
1137 1.1 skrll return bfd_reloc_ok;
1138 1.1 skrll }
1139 1.1 skrll else
1140 1.1 skrll return bfd_reloc_overflow;
1141 1.1 skrll
1142 1.1 skrll case R_MMIX_BASE_PLUS_OFFSET:
1143 1.1 skrll {
1144 1.1 skrll struct bpo_reloc_section_info *bpodata
1145 1.1 skrll = mmix_elf_section_data (isec)->bpo.reloc;
1146 1.1.1.3 christos asection *bpo_greg_section;
1147 1.1.1.3 christos struct bpo_greg_section_info *gregdata;
1148 1.1.1.3 christos size_t bpo_index;
1149 1.1.1.3 christos
1150 1.1.1.3 christos if (bpodata == NULL)
1151 1.1.1.3 christos {
1152 1.1.1.3 christos /* This shouldn't happen when linking to ELF or mmo, so
1153 1.1.1.3 christos this is an attempt to link to "binary", right? We
1154 1.1.1.3 christos can't access the output bfd, so we can't verify that
1155 1.1.1.3 christos assumption. We only know that the critical
1156 1.1.1.3 christos mmix_elf_check_common_relocs has not been called, which
1157 1.1.1.3 christos happens when the output format is different from the
1158 1.1.1.3 christos input format (and is not mmo). */
1159 1.1.1.3 christos if (! mmix_elf_section_data (isec)->has_warned_bpo)
1160 1.1.1.3 christos {
1161 1.1.1.3 christos /* For the first such error per input section, produce
1162 1.1.1.3 christos a verbose message. */
1163 1.1.1.3 christos *error_message
1164 1.1.1.3 christos = _("invalid input relocation when producing"
1165 1.1.1.7 christos " non-ELF, non-mmo format output;"
1166 1.1.1.7 christos " please use the objcopy program to convert from"
1167 1.1.1.3 christos " ELF or mmo,"
1168 1.1.1.7 christos " or compile using the gcc-option"
1169 1.1.1.3 christos " \"-mno-base-addresses\".");
1170 1.1.1.9 christos mmix_elf_section_data (isec)->has_warned_bpo = true;
1171 1.1.1.3 christos return bfd_reloc_dangerous;
1172 1.1.1.3 christos }
1173 1.1.1.3 christos
1174 1.1.1.3 christos /* For subsequent errors, return this one, which is
1175 1.1.1.3 christos rate-limited but looks a little bit different,
1176 1.1.1.3 christos hopefully without affecting user-friendliness. */
1177 1.1.1.3 christos return bfd_reloc_overflow;
1178 1.1.1.3 christos }
1179 1.1.1.3 christos
1180 1.1.1.3 christos bpo_greg_section = bpodata->bpo_greg_section;
1181 1.1.1.3 christos gregdata = mmix_elf_section_data (bpo_greg_section)->bpo.greg;
1182 1.1.1.3 christos bpo_index = gregdata->bpo_reloc_indexes[bpodata->bpo_index++];
1183 1.1 skrll
1184 1.1 skrll /* A consistency check: The value we now have in "relocation" must
1185 1.1 skrll be the same as the value we stored for that relocation. It
1186 1.1 skrll doesn't cost much, so can be left in at all times. */
1187 1.1 skrll if (value != gregdata->reloc_request[bpo_index].value)
1188 1.1 skrll {
1189 1.1.1.6 christos _bfd_error_handler
1190 1.1.1.6 christos /* xgettext:c-format */
1191 1.1.1.7 christos (_("%pB: Internal inconsistency error for value for\n\
1192 1.1.1.7 christos linker-allocated global register: linked: %#" PRIx64 " != relaxed: %#" PRIx64 ""),
1193 1.1.1.6 christos isec->owner,
1194 1.1.1.7 christos (uint64_t) value,
1195 1.1.1.7 christos (uint64_t) gregdata->reloc_request[bpo_index].value);
1196 1.1 skrll bfd_set_error (bfd_error_bad_value);
1197 1.1 skrll return bfd_reloc_overflow;
1198 1.1 skrll }
1199 1.1 skrll
1200 1.1 skrll /* Then store the register number and offset for that register
1201 1.1 skrll into datap and datap + 1 respectively. */
1202 1.1 skrll bfd_put_8 (abfd,
1203 1.1 skrll gregdata->reloc_request[bpo_index].regindex
1204 1.1 skrll + bpo_greg_section->output_section->vma / 8,
1205 1.1 skrll datap);
1206 1.1 skrll bfd_put_8 (abfd,
1207 1.1 skrll gregdata->reloc_request[bpo_index].offset,
1208 1.1 skrll ((unsigned char *) datap) + 1);
1209 1.1 skrll return bfd_reloc_ok;
1210 1.1 skrll }
1211 1.1 skrll
1212 1.1 skrll case R_MMIX_REG_OR_BYTE:
1213 1.1 skrll case R_MMIX_REG:
1214 1.1 skrll if (value > 255)
1215 1.1 skrll return bfd_reloc_overflow;
1216 1.1 skrll bfd_put_8 (abfd, value, datap);
1217 1.1 skrll return bfd_reloc_ok;
1218 1.1 skrll
1219 1.1 skrll default:
1220 1.1 skrll BAD_CASE (howto->type);
1221 1.1 skrll }
1222 1.1 skrll
1223 1.1 skrll /* This code adds the common SETL/INCML/INCMH/INCH worst-case
1224 1.1 skrll sequence. */
1225 1.1 skrll
1226 1.1 skrll /* Lowest two bits must be 0. We return bfd_reloc_overflow for
1227 1.1 skrll everything that looks strange. */
1228 1.1 skrll if (value & 3)
1229 1.1 skrll flag = bfd_reloc_overflow;
1230 1.1 skrll
1231 1.1 skrll bfd_put_32 (abfd,
1232 1.1 skrll (SETL_INSN_BYTE << 24) | (value & 0xffff) | (reg << 16),
1233 1.1 skrll (bfd_byte *) datap + offs);
1234 1.1 skrll bfd_put_32 (abfd,
1235 1.1 skrll (INCML_INSN_BYTE << 24) | ((value >> 16) & 0xffff) | (reg << 16),
1236 1.1 skrll (bfd_byte *) datap + offs + 4);
1237 1.1 skrll bfd_put_32 (abfd,
1238 1.1 skrll (INCMH_INSN_BYTE << 24) | ((value >> 32) & 0xffff) | (reg << 16),
1239 1.1 skrll (bfd_byte *) datap + offs + 8);
1240 1.1 skrll bfd_put_32 (abfd,
1241 1.1 skrll (INCH_INSN_BYTE << 24) | ((value >> 48) & 0xffff) | (reg << 16),
1242 1.1 skrll (bfd_byte *) datap + offs + 12);
1243 1.1 skrll
1244 1.1 skrll return flag;
1245 1.1 skrll }
1246 1.1 skrll
1247 1.1 skrll /* Set the howto pointer for an MMIX ELF reloc (type RELA). */
1248 1.1 skrll
1249 1.1.1.9 christos static bool
1250 1.1.1.7 christos mmix_info_to_howto_rela (bfd *abfd,
1251 1.1.1.3 christos arelent *cache_ptr,
1252 1.1.1.3 christos Elf_Internal_Rela *dst)
1253 1.1 skrll {
1254 1.1 skrll unsigned int r_type;
1255 1.1 skrll
1256 1.1 skrll r_type = ELF64_R_TYPE (dst->r_info);
1257 1.1.1.4 christos if (r_type >= (unsigned int) R_MMIX_max)
1258 1.1.1.4 christos {
1259 1.1.1.6 christos /* xgettext:c-format */
1260 1.1.1.7 christos _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1261 1.1.1.7 christos abfd, r_type);
1262 1.1.1.7 christos bfd_set_error (bfd_error_bad_value);
1263 1.1.1.9 christos return false;
1264 1.1.1.4 christos }
1265 1.1 skrll cache_ptr->howto = &elf_mmix_howto_table[r_type];
1266 1.1.1.9 christos return true;
1267 1.1 skrll }
1268 1.1 skrll
1269 1.1 skrll /* Any MMIX-specific relocation gets here at assembly time or when linking
1270 1.1 skrll to other formats (such as mmo); this is the relocation function from
1271 1.1 skrll the reloc_table. We don't get here for final pure ELF linking. */
1272 1.1 skrll
1273 1.1 skrll static bfd_reloc_status_type
1274 1.1.1.3 christos mmix_elf_reloc (bfd *abfd,
1275 1.1.1.3 christos arelent *reloc_entry,
1276 1.1.1.3 christos asymbol *symbol,
1277 1.1.1.3 christos void * data,
1278 1.1.1.3 christos asection *input_section,
1279 1.1.1.3 christos bfd *output_bfd,
1280 1.1.1.3 christos char **error_message)
1281 1.1 skrll {
1282 1.1 skrll bfd_vma relocation;
1283 1.1 skrll bfd_reloc_status_type r;
1284 1.1 skrll asection *reloc_target_output_section;
1285 1.1 skrll bfd_reloc_status_type flag = bfd_reloc_ok;
1286 1.1 skrll bfd_vma output_base = 0;
1287 1.1 skrll
1288 1.1 skrll r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1289 1.1 skrll input_section, output_bfd, error_message);
1290 1.1 skrll
1291 1.1 skrll /* If that was all that was needed (i.e. this isn't a final link, only
1292 1.1 skrll some segment adjustments), we're done. */
1293 1.1 skrll if (r != bfd_reloc_continue)
1294 1.1 skrll return r;
1295 1.1 skrll
1296 1.1 skrll if (bfd_is_und_section (symbol->section)
1297 1.1 skrll && (symbol->flags & BSF_WEAK) == 0
1298 1.1 skrll && output_bfd == (bfd *) NULL)
1299 1.1 skrll return bfd_reloc_undefined;
1300 1.1 skrll
1301 1.1 skrll /* Is the address of the relocation really within the section? */
1302 1.1 skrll if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1303 1.1 skrll return bfd_reloc_outofrange;
1304 1.1 skrll
1305 1.1 skrll /* Work out which section the relocation is targeted at and the
1306 1.1 skrll initial relocation command value. */
1307 1.1 skrll
1308 1.1 skrll /* Get symbol value. (Common symbols are special.) */
1309 1.1 skrll if (bfd_is_com_section (symbol->section))
1310 1.1 skrll relocation = 0;
1311 1.1 skrll else
1312 1.1 skrll relocation = symbol->value;
1313 1.1 skrll
1314 1.1.1.8 christos reloc_target_output_section = bfd_asymbol_section (symbol)->output_section;
1315 1.1 skrll
1316 1.1 skrll /* Here the variable relocation holds the final address of the symbol we
1317 1.1 skrll are relocating against, plus any addend. */
1318 1.1 skrll if (output_bfd)
1319 1.1 skrll output_base = 0;
1320 1.1 skrll else
1321 1.1 skrll output_base = reloc_target_output_section->vma;
1322 1.1 skrll
1323 1.1 skrll relocation += output_base + symbol->section->output_offset;
1324 1.1 skrll
1325 1.1 skrll if (output_bfd != (bfd *) NULL)
1326 1.1 skrll {
1327 1.1 skrll /* Add in supplied addend. */
1328 1.1 skrll relocation += reloc_entry->addend;
1329 1.1 skrll
1330 1.1 skrll /* This is a partial relocation, and we want to apply the
1331 1.1 skrll relocation to the reloc entry rather than the raw data.
1332 1.1 skrll Modify the reloc inplace to reflect what we now know. */
1333 1.1 skrll reloc_entry->addend = relocation;
1334 1.1 skrll reloc_entry->address += input_section->output_offset;
1335 1.1 skrll return flag;
1336 1.1 skrll }
1337 1.1 skrll
1338 1.1 skrll return mmix_final_link_relocate (reloc_entry->howto, input_section,
1339 1.1 skrll data, reloc_entry->address,
1340 1.1 skrll reloc_entry->addend, relocation,
1341 1.1 skrll bfd_asymbol_name (symbol),
1342 1.1.1.3 christos reloc_target_output_section,
1343 1.1.1.3 christos error_message);
1344 1.1 skrll }
1345 1.1 skrll
1346 1.1 skrll /* Relocate an MMIX ELF section. Modified from elf32-fr30.c; look to it
1348 1.1 skrll for guidance if you're thinking of copying this. */
1349 1.1.1.9 christos
1350 1.1.1.3 christos static int
1351 1.1.1.3 christos mmix_elf_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
1352 1.1.1.3 christos struct bfd_link_info *info,
1353 1.1.1.3 christos bfd *input_bfd,
1354 1.1.1.3 christos asection *input_section,
1355 1.1.1.3 christos bfd_byte *contents,
1356 1.1.1.3 christos Elf_Internal_Rela *relocs,
1357 1.1.1.3 christos Elf_Internal_Sym *local_syms,
1358 1.1 skrll asection **local_sections)
1359 1.1 skrll {
1360 1.1 skrll Elf_Internal_Shdr *symtab_hdr;
1361 1.1 skrll struct elf_link_hash_entry **sym_hashes;
1362 1.1 skrll Elf_Internal_Rela *rel;
1363 1.1 skrll Elf_Internal_Rela *relend;
1364 1.1 skrll bfd_size_type size;
1365 1.1 skrll size_t pjsno = 0;
1366 1.1 skrll
1367 1.1 skrll size = input_section->rawsize ? input_section->rawsize : input_section->size;
1368 1.1 skrll symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1369 1.1 skrll sym_hashes = elf_sym_hashes (input_bfd);
1370 1.1 skrll relend = relocs + input_section->reloc_count;
1371 1.1 skrll
1372 1.1 skrll /* Zero the stub area before we start. */
1373 1.1 skrll if (input_section->rawsize != 0
1374 1.1 skrll && input_section->size > input_section->rawsize)
1375 1.1 skrll memset (contents + input_section->rawsize, 0,
1376 1.1 skrll input_section->size - input_section->rawsize);
1377 1.1 skrll
1378 1.1 skrll for (rel = relocs; rel < relend; rel ++)
1379 1.1 skrll {
1380 1.1 skrll reloc_howto_type *howto;
1381 1.1 skrll unsigned long r_symndx;
1382 1.1 skrll Elf_Internal_Sym *sym;
1383 1.1 skrll asection *sec;
1384 1.1 skrll struct elf_link_hash_entry *h;
1385 1.1 skrll bfd_vma relocation;
1386 1.1 skrll bfd_reloc_status_type r;
1387 1.1 skrll const char *name = NULL;
1388 1.1.1.9 christos int r_type;
1389 1.1 skrll bool undefined_signalled = false;
1390 1.1 skrll
1391 1.1 skrll r_type = ELF64_R_TYPE (rel->r_info);
1392 1.1 skrll
1393 1.1 skrll if (r_type == R_MMIX_GNU_VTINHERIT
1394 1.1 skrll || r_type == R_MMIX_GNU_VTENTRY)
1395 1.1 skrll continue;
1396 1.1 skrll
1397 1.1 skrll r_symndx = ELF64_R_SYM (rel->r_info);
1398 1.1 skrll
1399 1.1 skrll howto = elf_mmix_howto_table + ELF64_R_TYPE (rel->r_info);
1400 1.1 skrll h = NULL;
1401 1.1 skrll sym = NULL;
1402 1.1 skrll sec = NULL;
1403 1.1 skrll
1404 1.1 skrll if (r_symndx < symtab_hdr->sh_info)
1405 1.1 skrll {
1406 1.1 skrll sym = local_syms + r_symndx;
1407 1.1 skrll sec = local_sections [r_symndx];
1408 1.1 skrll relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1409 1.1 skrll
1410 1.1 skrll name = bfd_elf_string_from_elf_section (input_bfd,
1411 1.1 skrll symtab_hdr->sh_link,
1412 1.1 skrll sym->st_name);
1413 1.1.1.8 christos if (name == NULL)
1414 1.1 skrll name = bfd_section_name (sec);
1415 1.1 skrll }
1416 1.1 skrll else
1417 1.1.1.9 christos {
1418 1.1 skrll bool unresolved_reloc, ignored;
1419 1.1 skrll
1420 1.1 skrll RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1421 1.1 skrll r_symndx, symtab_hdr, sym_hashes,
1422 1.1.1.4 christos h, sec, relocation,
1423 1.1.1.4 christos unresolved_reloc, undefined_signalled,
1424 1.1 skrll ignored);
1425 1.1 skrll name = h->root.root.string;
1426 1.1 skrll }
1427 1.1.1.3 christos
1428 1.1.1.2 christos if (sec != NULL && discarded_section (sec))
1429 1.1.1.3 christos RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1430 1.1 skrll rel, 1, relend, howto, 0, contents);
1431 1.1.1.4 christos
1432 1.1 skrll if (bfd_link_relocatable (info))
1433 1.1 skrll {
1434 1.1 skrll /* This is a relocatable link. For most relocs we don't have to
1435 1.1 skrll change anything, unless the reloc is against a section
1436 1.1 skrll symbol, in which case we have to adjust according to where
1437 1.1 skrll the section symbol winds up in the output section. */
1438 1.1 skrll if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1439 1.1 skrll rel->r_addend += sec->output_offset;
1440 1.1 skrll
1441 1.1 skrll /* For PUSHJ stub relocs however, we may need to change the
1442 1.1 skrll reloc and the section contents, if the reloc doesn't reach
1443 1.1 skrll beyond the end of the output section and previous stubs.
1444 1.1 skrll Then we change the section contents to be a PUSHJ to the end
1445 1.1 skrll of the input section plus stubs (we can do that without using
1446 1.1 skrll a reloc), and then we change the reloc to be a R_MMIX_PUSHJ
1447 1.1 skrll at the stub location. */
1448 1.1 skrll if (r_type == R_MMIX_PUSHJ_STUBBABLE)
1449 1.1 skrll {
1450 1.1 skrll /* We've already checked whether we need a stub; use that
1451 1.1 skrll knowledge. */
1452 1.1 skrll if (mmix_elf_section_data (input_section)->pjs.stub_size[pjsno]
1453 1.1 skrll != 0)
1454 1.1 skrll {
1455 1.1 skrll Elf_Internal_Rela relcpy;
1456 1.1 skrll
1457 1.1 skrll if (mmix_elf_section_data (input_section)
1458 1.1 skrll ->pjs.stub_size[pjsno] != MAX_PUSHJ_STUB_SIZE)
1459 1.1 skrll abort ();
1460 1.1 skrll
1461 1.1 skrll /* There's already a PUSHJ insn there, so just fill in
1462 1.1 skrll the offset bits to the stub. */
1463 1.1 skrll if (mmix_final_link_relocate (elf_mmix_howto_table
1464 1.1 skrll + R_MMIX_ADDR19,
1465 1.1 skrll input_section,
1466 1.1 skrll contents,
1467 1.1 skrll rel->r_offset,
1468 1.1 skrll 0,
1469 1.1 skrll input_section
1470 1.1 skrll ->output_section->vma
1471 1.1 skrll + input_section->output_offset
1472 1.1 skrll + size
1473 1.1 skrll + mmix_elf_section_data (input_section)
1474 1.1.1.3 christos ->pjs.stub_offset,
1475 1.1.1.9 christos NULL, NULL, NULL) != bfd_reloc_ok)
1476 1.1 skrll return false;
1477 1.1 skrll
1478 1.1 skrll /* Put a JMP insn at the stub; it goes with the
1479 1.1 skrll R_MMIX_JMP reloc. */
1480 1.1 skrll bfd_put_32 (output_bfd, JMP_INSN_BYTE << 24,
1481 1.1 skrll contents
1482 1.1 skrll + size
1483 1.1 skrll + mmix_elf_section_data (input_section)
1484 1.1 skrll ->pjs.stub_offset);
1485 1.1 skrll
1486 1.1 skrll /* Change the reloc to be at the stub, and to a full
1487 1.1 skrll R_MMIX_JMP reloc. */
1488 1.1 skrll rel->r_info = ELF64_R_INFO (r_symndx, R_MMIX_JMP);
1489 1.1 skrll rel->r_offset
1490 1.1 skrll = (size
1491 1.1 skrll + mmix_elf_section_data (input_section)
1492 1.1 skrll ->pjs.stub_offset);
1493 1.1 skrll
1494 1.1 skrll mmix_elf_section_data (input_section)->pjs.stub_offset
1495 1.1 skrll += MAX_PUSHJ_STUB_SIZE;
1496 1.1 skrll
1497 1.1 skrll /* Shift this reloc to the end of the relocs to maintain
1498 1.1 skrll the r_offset sorted reloc order. */
1499 1.1.1.11 christos relcpy = *rel;
1500 1.1 skrll memmove (rel, rel + 1, (char *) relend - (char *) (rel + 1));
1501 1.1 skrll relend[-1] = relcpy;
1502 1.1 skrll
1503 1.1 skrll /* Back up one reloc, or else we'd skip the next reloc
1504 1.1 skrll in turn. */
1505 1.1 skrll rel--;
1506 1.1 skrll }
1507 1.1 skrll
1508 1.1 skrll pjsno++;
1509 1.1 skrll }
1510 1.1 skrll continue;
1511 1.1 skrll }
1512 1.1 skrll
1513 1.1 skrll r = mmix_final_link_relocate (howto, input_section,
1514 1.1.1.3 christos contents, rel->r_offset,
1515 1.1 skrll rel->r_addend, relocation, name, sec, NULL);
1516 1.1 skrll
1517 1.1 skrll if (r != bfd_reloc_ok)
1518 1.1 skrll {
1519 1.1 skrll const char * msg = (const char *) NULL;
1520 1.1 skrll
1521 1.1 skrll switch (r)
1522 1.1 skrll {
1523 1.1.1.5 christos case bfd_reloc_overflow:
1524 1.1 skrll info->callbacks->reloc_overflow
1525 1.1 skrll (info, (h ? &h->root : NULL), name, howto->name,
1526 1.1 skrll (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1527 1.1 skrll break;
1528 1.1 skrll
1529 1.1 skrll case bfd_reloc_undefined:
1530 1.1 skrll /* We may have sent this message above. */
1531 1.1.1.5 christos if (! undefined_signalled)
1532 1.1.1.9 christos info->callbacks->undefined_symbol
1533 1.1.1.9 christos (info, name, input_bfd, input_section, rel->r_offset, true);
1534 1.1 skrll undefined_signalled = true;
1535 1.1 skrll break;
1536 1.1 skrll
1537 1.1 skrll case bfd_reloc_outofrange:
1538 1.1 skrll msg = _("internal error: out of range error");
1539 1.1 skrll break;
1540 1.1 skrll
1541 1.1 skrll case bfd_reloc_notsupported:
1542 1.1 skrll msg = _("internal error: unsupported relocation error");
1543 1.1 skrll break;
1544 1.1 skrll
1545 1.1 skrll case bfd_reloc_dangerous:
1546 1.1 skrll msg = _("internal error: dangerous relocation");
1547 1.1 skrll break;
1548 1.1 skrll
1549 1.1 skrll default:
1550 1.1 skrll msg = _("internal error: unknown error");
1551 1.1 skrll break;
1552 1.1 skrll }
1553 1.1 skrll
1554 1.1.1.5 christos if (msg)
1555 1.1.1.5 christos (*info->callbacks->warning) (info, msg, name, input_bfd,
1556 1.1 skrll input_section, rel->r_offset);
1557 1.1 skrll }
1558 1.1 skrll }
1559 1.1.1.9 christos
1560 1.1 skrll return true;
1561 1.1 skrll }
1562 1.1 skrll
1563 1.1 skrll /* Perform a single relocation. By default we use the standard BFD
1565 1.1 skrll routines. A few relocs we have to do ourselves. */
1566 1.1.1.3 christos
1567 1.1.1.3 christos static bfd_reloc_status_type
1568 1.1.1.3 christos mmix_final_link_relocate (reloc_howto_type *howto, asection *input_section,
1569 1.1.1.3 christos bfd_byte *contents, bfd_vma r_offset,
1570 1.1.1.3 christos bfd_signed_vma r_addend, bfd_vma relocation,
1571 1.1 skrll const char *symname, asection *symsec,
1572 1.1 skrll char **error_message)
1573 1.1 skrll {
1574 1.1 skrll bfd_reloc_status_type r = bfd_reloc_ok;
1575 1.1 skrll bfd_vma addr
1576 1.1 skrll = (input_section->output_section->vma
1577 1.1 skrll + input_section->output_offset
1578 1.1 skrll + r_offset);
1579 1.1 skrll bfd_signed_vma srel
1580 1.1 skrll = (bfd_signed_vma) relocation + r_addend;
1581 1.1 skrll
1582 1.1 skrll switch (howto->type)
1583 1.1 skrll {
1584 1.1 skrll /* All these are PC-relative. */
1585 1.1 skrll case R_MMIX_PUSHJ_STUBBABLE:
1586 1.1 skrll case R_MMIX_PUSHJ:
1587 1.1 skrll case R_MMIX_CBRANCH:
1588 1.1 skrll case R_MMIX_ADDR19:
1589 1.1 skrll case R_MMIX_GETA:
1590 1.1 skrll case R_MMIX_ADDR27:
1591 1.1 skrll case R_MMIX_JMP:
1592 1.1 skrll contents += r_offset;
1593 1.1 skrll
1594 1.1 skrll srel -= (input_section->output_section->vma
1595 1.1 skrll + input_section->output_offset
1596 1.1 skrll + r_offset);
1597 1.1.1.3 christos
1598 1.1 skrll r = mmix_elf_perform_relocation (input_section, howto, contents,
1599 1.1 skrll addr, srel, error_message);
1600 1.1 skrll break;
1601 1.1 skrll
1602 1.1 skrll case R_MMIX_BASE_PLUS_OFFSET:
1603 1.1 skrll if (symsec == NULL)
1604 1.1 skrll return bfd_reloc_undefined;
1605 1.1.1.8 christos
1606 1.1 skrll /* Check that we're not relocating against a register symbol. */
1607 1.1.1.8 christos if (strcmp (bfd_section_name (symsec),
1608 1.1 skrll MMIX_REG_CONTENTS_SECTION_NAME) == 0
1609 1.1 skrll || strcmp (bfd_section_name (symsec),
1610 1.1 skrll MMIX_REG_SECTION_NAME) == 0)
1611 1.1 skrll {
1612 1.1 skrll /* Note: This is separated out into two messages in order
1613 1.1.1.6 christos to ease the translation into other languages. */
1614 1.1.1.6 christos if (symname == NULL || *symname == 0)
1615 1.1.1.7 christos _bfd_error_handler
1616 1.1.1.7 christos /* xgettext:c-format */
1617 1.1.1.6 christos (_("%pB: base-plus-offset relocation against register symbol:"
1618 1.1 skrll " (unknown) in %pA"),
1619 1.1.1.6 christos input_section->owner, symsec);
1620 1.1.1.6 christos else
1621 1.1.1.7 christos _bfd_error_handler
1622 1.1.1.7 christos /* xgettext:c-format */
1623 1.1.1.6 christos (_("%pB: base-plus-offset relocation against register symbol:"
1624 1.1 skrll " %s in %pA"),
1625 1.1 skrll input_section->owner, symname, symsec);
1626 1.1 skrll return bfd_reloc_overflow;
1627 1.1 skrll }
1628 1.1 skrll goto do_mmix_reloc;
1629 1.1 skrll
1630 1.1 skrll case R_MMIX_REG_OR_BYTE:
1631 1.1 skrll case R_MMIX_REG:
1632 1.1 skrll /* For now, we handle these alike. They must refer to an register
1633 1.1 skrll symbol, which is either relative to the register section and in
1634 1.1 skrll the range 0..255, or is in the register contents section with vma
1635 1.1 skrll regno * 8. */
1636 1.1 skrll
1637 1.1 skrll /* FIXME: A better way to check for reg contents section?
1638 1.1 skrll FIXME: Postpone section->scaling to mmix_elf_perform_relocation? */
1639 1.1 skrll if (symsec == NULL)
1640 1.1.1.8 christos return bfd_reloc_undefined;
1641 1.1 skrll
1642 1.1 skrll if (strcmp (bfd_section_name (symsec),
1643 1.1 skrll MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1644 1.1 skrll {
1645 1.1 skrll if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
1646 1.1 skrll {
1647 1.1 skrll /* The bfd_reloc_outofrange return value, though intuitively
1648 1.1 skrll a better value, will not get us an error. */
1649 1.1 skrll return bfd_reloc_overflow;
1650 1.1 skrll }
1651 1.1.1.8 christos srel /= 8;
1652 1.1 skrll }
1653 1.1 skrll else if (strcmp (bfd_section_name (symsec),
1654 1.1 skrll MMIX_REG_SECTION_NAME) == 0)
1655 1.1 skrll {
1656 1.1 skrll if (srel < 0 || srel > 255)
1657 1.1 skrll /* The bfd_reloc_outofrange return value, though intuitively a
1658 1.1 skrll better value, will not get us an error. */
1659 1.1 skrll return bfd_reloc_overflow;
1660 1.1 skrll }
1661 1.1 skrll else
1662 1.1 skrll {
1663 1.1 skrll /* Note: This is separated out into two messages in order
1664 1.1.1.6 christos to ease the translation into other languages. */
1665 1.1.1.6 christos if (symname == NULL || *symname == 0)
1666 1.1.1.7 christos _bfd_error_handler
1667 1.1.1.7 christos /* xgettext:c-format */
1668 1.1.1.6 christos (_("%pB: register relocation against non-register symbol:"
1669 1.1 skrll " (unknown) in %pA"),
1670 1.1.1.6 christos input_section->owner, symsec);
1671 1.1.1.6 christos else
1672 1.1.1.7 christos _bfd_error_handler
1673 1.1.1.7 christos /* xgettext:c-format */
1674 1.1.1.6 christos (_("%pB: register relocation against non-register symbol:"
1675 1.1 skrll " %s in %pA"),
1676 1.1 skrll input_section->owner, symname, symsec);
1677 1.1 skrll
1678 1.1 skrll /* The bfd_reloc_outofrange return value, though intuitively a
1679 1.1 skrll better value, will not get us an error. */
1680 1.1 skrll return bfd_reloc_overflow;
1681 1.1 skrll }
1682 1.1 skrll do_mmix_reloc:
1683 1.1.1.3 christos contents += r_offset;
1684 1.1 skrll r = mmix_elf_perform_relocation (input_section, howto, contents,
1685 1.1 skrll addr, srel, error_message);
1686 1.1 skrll break;
1687 1.1 skrll
1688 1.1 skrll case R_MMIX_LOCAL:
1689 1.1 skrll /* This isn't a real relocation, it's just an assertion that the
1690 1.1 skrll final relocation value corresponds to a local register. We
1691 1.1 skrll ignore the actual relocation; nothing is changed. */
1692 1.1 skrll {
1693 1.1 skrll asection *regsec
1694 1.1 skrll = bfd_get_section_by_name (input_section->output_section->owner,
1695 1.1 skrll MMIX_REG_CONTENTS_SECTION_NAME);
1696 1.1 skrll bfd_vma first_global;
1697 1.1 skrll
1698 1.1 skrll /* Check that this is an absolute value, or a reference to the
1699 1.1 skrll register contents section or the register (symbol) section.
1700 1.1 skrll Absolute numbers can get here as undefined section. Undefined
1701 1.1 skrll symbols are signalled elsewhere, so there's no conflict in us
1702 1.1 skrll accidentally handling it. */
1703 1.1.1.8 christos if (!bfd_is_abs_section (symsec)
1704 1.1 skrll && !bfd_is_und_section (symsec)
1705 1.1.1.8 christos && strcmp (bfd_section_name (symsec),
1706 1.1 skrll MMIX_REG_CONTENTS_SECTION_NAME) != 0
1707 1.1 skrll && strcmp (bfd_section_name (symsec),
1708 1.1.1.6 christos MMIX_REG_SECTION_NAME) != 0)
1709 1.1.1.7 christos {
1710 1.1.1.6 christos _bfd_error_handler
1711 1.1 skrll (_("%pB: directive LOCAL valid only with a register or absolute value"),
1712 1.1 skrll input_section->owner);
1713 1.1 skrll
1714 1.1 skrll return bfd_reloc_overflow;
1715 1.1 skrll }
1716 1.1 skrll
1717 1.1 skrll /* If we don't have a register contents section, then $255 is the
1718 1.1 skrll first global register. */
1719 1.1 skrll if (regsec == NULL)
1720 1.1 skrll first_global = 255;
1721 1.1.1.8 christos else
1722 1.1.1.8 christos {
1723 1.1 skrll first_global = bfd_section_vma (regsec) / 8;
1724 1.1 skrll if (strcmp (bfd_section_name (symsec),
1725 1.1 skrll MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1726 1.1 skrll {
1727 1.1 skrll if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
1728 1.1 skrll /* The bfd_reloc_outofrange return value, though
1729 1.1 skrll intuitively a better value, will not get us an error. */
1730 1.1 skrll return bfd_reloc_overflow;
1731 1.1 skrll srel /= 8;
1732 1.1 skrll }
1733 1.1 skrll }
1734 1.1 skrll
1735 1.1 skrll if ((bfd_vma) srel >= first_global)
1736 1.1.1.6 christos {
1737 1.1.1.6 christos /* FIXME: Better error message. */
1738 1.1.1.7 christos _bfd_error_handler
1739 1.1.1.7 christos /* xgettext:c-format */
1740 1.1.1.7 christos (_("%pB: LOCAL directive: "
1741 1.1.1.7 christos "register $%" PRId64 " is not a local register;"
1742 1.1 skrll " first global register is $%" PRId64),
1743 1.1 skrll input_section->owner, (int64_t) srel, (int64_t) first_global);
1744 1.1 skrll
1745 1.1 skrll return bfd_reloc_overflow;
1746 1.1 skrll }
1747 1.1 skrll }
1748 1.1 skrll r = bfd_reloc_ok;
1749 1.1 skrll break;
1750 1.1 skrll
1751 1.1 skrll default:
1752 1.1 skrll r = _bfd_final_link_relocate (howto, input_section->owner, input_section,
1753 1.1 skrll contents, r_offset,
1754 1.1 skrll relocation, r_addend);
1755 1.1 skrll }
1756 1.1 skrll
1757 1.1 skrll return r;
1758 1.1 skrll }
1759 1.1 skrll
1760 1.1 skrll /* Return the section that should be marked against GC for a given
1762 1.1 skrll relocation. */
1763 1.1 skrll
1764 1.1 skrll static asection *
1765 1.1 skrll mmix_elf_gc_mark_hook (asection *sec,
1766 1.1 skrll struct bfd_link_info *info,
1767 1.1 skrll Elf_Internal_Rela *rel,
1768 1.1 skrll struct elf_link_hash_entry *h,
1769 1.1 skrll Elf_Internal_Sym *sym)
1770 1.1 skrll {
1771 1.1 skrll if (h != NULL)
1772 1.1 skrll switch (ELF64_R_TYPE (rel->r_info))
1773 1.1 skrll {
1774 1.1 skrll case R_MMIX_GNU_VTINHERIT:
1775 1.1 skrll case R_MMIX_GNU_VTENTRY:
1776 1.1 skrll return NULL;
1777 1.1 skrll }
1778 1.1 skrll
1779 1.1 skrll return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1780 1.1 skrll }
1781 1.1 skrll
1782 1.1.1.3 christos /* Sort register relocs to come before expanding relocs. */
1784 1.1 skrll
1785 1.1 skrll static int
1786 1.1 skrll mmix_elf_sort_relocs (const void * p1, const void * p2)
1787 1.1 skrll {
1788 1.1 skrll const Elf_Internal_Rela *r1 = (const Elf_Internal_Rela *) p1;
1789 1.1 skrll const Elf_Internal_Rela *r2 = (const Elf_Internal_Rela *) p2;
1790 1.1 skrll int r1_is_reg, r2_is_reg;
1791 1.1 skrll
1792 1.1 skrll /* Sort primarily on r_offset & ~3, so relocs are done to consecutive
1793 1.1 skrll insns. */
1794 1.1 skrll if ((r1->r_offset & ~(bfd_vma) 3) > (r2->r_offset & ~(bfd_vma) 3))
1795 1.1 skrll return 1;
1796 1.1 skrll else if ((r1->r_offset & ~(bfd_vma) 3) < (r2->r_offset & ~(bfd_vma) 3))
1797 1.1 skrll return -1;
1798 1.1 skrll
1799 1.1 skrll r1_is_reg
1800 1.1 skrll = (ELF64_R_TYPE (r1->r_info) == R_MMIX_REG_OR_BYTE
1801 1.1 skrll || ELF64_R_TYPE (r1->r_info) == R_MMIX_REG);
1802 1.1 skrll r2_is_reg
1803 1.1 skrll = (ELF64_R_TYPE (r2->r_info) == R_MMIX_REG_OR_BYTE
1804 1.1 skrll || ELF64_R_TYPE (r2->r_info) == R_MMIX_REG);
1805 1.1 skrll if (r1_is_reg != r2_is_reg)
1806 1.1 skrll return r2_is_reg - r1_is_reg;
1807 1.1 skrll
1808 1.1 skrll /* Neither or both are register relocs. Then sort on full offset. */
1809 1.1 skrll if (r1->r_offset > r2->r_offset)
1810 1.1 skrll return 1;
1811 1.1 skrll else if (r1->r_offset < r2->r_offset)
1812 1.1 skrll return -1;
1813 1.1 skrll return 0;
1814 1.1.1.9 christos }
1815 1.1.1.3 christos
1816 1.1.1.3 christos /* Subset of mmix_elf_check_relocs, common to ELF and mmo linking. */
1817 1.1.1.3 christos
1818 1.1.1.3 christos static bool
1819 1.1 skrll mmix_elf_check_common_relocs (bfd *abfd,
1820 1.1 skrll struct bfd_link_info *info,
1821 1.1 skrll asection *sec,
1822 1.1 skrll const Elf_Internal_Rela *relocs)
1823 1.1 skrll {
1824 1.1 skrll bfd *bpo_greg_owner = NULL;
1825 1.1 skrll asection *allocated_gregs_section = NULL;
1826 1.1 skrll struct bpo_greg_section_info *gregdata = NULL;
1827 1.1 skrll struct bpo_reloc_section_info *bpodata = NULL;
1828 1.1 skrll const Elf_Internal_Rela *rel;
1829 1.1 skrll const Elf_Internal_Rela *rel_end;
1830 1.1 skrll
1831 1.1 skrll /* We currently have to abuse this COFF-specific member, since there's
1832 1.1 skrll no target-machine-dedicated member. There's no alternative outside
1833 1.1 skrll the bfd_link_info struct; we can't specialize a hash-table since
1834 1.1 skrll they're different between ELF and mmo. */
1835 1.1 skrll bpo_greg_owner = (bfd *) info->base_file;
1836 1.1 skrll
1837 1.1.1.6 christos rel_end = relocs + sec->reloc_count;
1838 1.1 skrll for (rel = relocs; rel < rel_end; rel++)
1839 1.1 skrll {
1840 1.1 skrll switch (ELF64_R_TYPE (rel->r_info))
1841 1.1 skrll {
1842 1.1 skrll /* This relocation causes a GREG allocation. We need to count
1843 1.1 skrll them, and we need to create a section for them, so we need an
1844 1.1 skrll object to fake as the owner of that section. We can't use
1845 1.1.1.4 christos the ELF dynobj for this, since the ELF bits assume lots of
1846 1.1 skrll DSO-related stuff if that member is non-NULL. */
1847 1.1 skrll case R_MMIX_BASE_PLUS_OFFSET:
1848 1.1 skrll /* We don't do anything with this reloc for a relocatable link. */
1849 1.1 skrll if (bfd_link_relocatable (info))
1850 1.1 skrll break;
1851 1.1.1.3 christos
1852 1.1 skrll if (bpo_greg_owner == NULL)
1853 1.1 skrll {
1854 1.1 skrll bpo_greg_owner = abfd;
1855 1.1 skrll info->base_file = bpo_greg_owner;
1856 1.1 skrll }
1857 1.1 skrll
1858 1.1 skrll if (allocated_gregs_section == NULL)
1859 1.1 skrll allocated_gregs_section
1860 1.1 skrll = bfd_get_section_by_name (bpo_greg_owner,
1861 1.1 skrll MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
1862 1.1 skrll
1863 1.1 skrll if (allocated_gregs_section == NULL)
1864 1.1 skrll {
1865 1.1 skrll allocated_gregs_section
1866 1.1 skrll = bfd_make_section_with_flags (bpo_greg_owner,
1867 1.1 skrll MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME,
1868 1.1 skrll (SEC_HAS_CONTENTS
1869 1.1 skrll | SEC_IN_MEMORY
1870 1.1 skrll | SEC_LINKER_CREATED));
1871 1.1 skrll /* Setting both SEC_ALLOC and SEC_LOAD means the section is
1872 1.1 skrll treated like any other section, and we'd get errors for
1873 1.1.1.8 christos address overlap with the text section. Let's set none of
1874 1.1.1.9 christos those flags, as that is what currently happens for usual
1875 1.1 skrll GREG allocations, and that works. */
1876 1.1 skrll if (allocated_gregs_section == NULL
1877 1.1 skrll || !bfd_set_section_alignment (allocated_gregs_section, 3))
1878 1.1 skrll return false;
1879 1.1.1.9 christos
1880 1.1 skrll gregdata = (struct bpo_greg_section_info *)
1881 1.1 skrll bfd_zalloc (bpo_greg_owner, sizeof (struct bpo_greg_section_info));
1882 1.1 skrll if (gregdata == NULL)
1883 1.1 skrll return false;
1884 1.1 skrll mmix_elf_section_data (allocated_gregs_section)->bpo.greg
1885 1.1 skrll = gregdata;
1886 1.1 skrll }
1887 1.1 skrll else if (gregdata == NULL)
1888 1.1 skrll gregdata
1889 1.1 skrll = mmix_elf_section_data (allocated_gregs_section)->bpo.greg;
1890 1.1 skrll
1891 1.1 skrll /* Get ourselves some auxiliary info for the BPO-relocs. */
1892 1.1 skrll if (bpodata == NULL)
1893 1.1 skrll {
1894 1.1 skrll /* No use doing a separate iteration pass to find the upper
1895 1.1 skrll limit - just use the number of relocs. */
1896 1.1 skrll bpodata = (struct bpo_reloc_section_info *)
1897 1.1.1.9 christos bfd_alloc (bpo_greg_owner,
1898 1.1 skrll sizeof (struct bpo_reloc_section_info)
1899 1.1 skrll * (sec->reloc_count + 1));
1900 1.1 skrll if (bpodata == NULL)
1901 1.1 skrll return false;
1902 1.1 skrll mmix_elf_section_data (sec)->bpo.reloc = bpodata;
1903 1.1 skrll bpodata->first_base_plus_offset_reloc
1904 1.1 skrll = bpodata->bpo_index
1905 1.1 skrll = gregdata->n_max_bpo_relocs;
1906 1.1 skrll bpodata->bpo_greg_section
1907 1.1 skrll = allocated_gregs_section;
1908 1.1 skrll bpodata->n_bpo_relocs_this_section = 0;
1909 1.1 skrll }
1910 1.1 skrll
1911 1.1 skrll bpodata->n_bpo_relocs_this_section++;
1912 1.1 skrll gregdata->n_max_bpo_relocs++;
1913 1.1 skrll
1914 1.1 skrll /* We don't get another chance to set this before GC; we've not
1915 1.1 skrll set up any hook that runs before GC. */
1916 1.1 skrll gregdata->n_bpo_relocs
1917 1.1 skrll = gregdata->n_max_bpo_relocs;
1918 1.1 skrll break;
1919 1.1 skrll
1920 1.1 skrll case R_MMIX_PUSHJ_STUBBABLE:
1921 1.1 skrll mmix_elf_section_data (sec)->pjs.n_pushj_relocs++;
1922 1.1 skrll break;
1923 1.1 skrll }
1924 1.1 skrll }
1925 1.1 skrll
1926 1.1 skrll /* Allocate per-reloc stub storage and initialize it to the max stub
1927 1.1 skrll size. */
1928 1.1 skrll if (mmix_elf_section_data (sec)->pjs.n_pushj_relocs != 0)
1929 1.1 skrll {
1930 1.1 skrll size_t i;
1931 1.1 skrll
1932 1.1 skrll mmix_elf_section_data (sec)->pjs.stub_size
1933 1.1.1.9 christos = bfd_alloc (abfd, mmix_elf_section_data (sec)->pjs.n_pushj_relocs
1934 1.1 skrll * sizeof (mmix_elf_section_data (sec)
1935 1.1 skrll ->pjs.stub_size[0]));
1936 1.1 skrll if (mmix_elf_section_data (sec)->pjs.stub_size == NULL)
1937 1.1 skrll return false;
1938 1.1 skrll
1939 1.1.1.9 christos for (i = 0; i < mmix_elf_section_data (sec)->pjs.n_pushj_relocs; i++)
1940 1.1 skrll mmix_elf_section_data (sec)->pjs.stub_size[i] = MAX_PUSHJ_STUB_SIZE;
1941 1.1 skrll }
1942 1.1 skrll
1943 1.1 skrll return true;
1944 1.1.1.9 christos }
1945 1.1.1.3 christos
1946 1.1.1.3 christos /* Look through the relocs for a section during the first phase. */
1947 1.1.1.3 christos
1948 1.1.1.3 christos static bool
1949 1.1 skrll mmix_elf_check_relocs (bfd *abfd,
1950 1.1 skrll struct bfd_link_info *info,
1951 1.1 skrll asection *sec,
1952 1.1 skrll const Elf_Internal_Rela *relocs)
1953 1.1 skrll {
1954 1.1 skrll Elf_Internal_Shdr *symtab_hdr;
1955 1.1 skrll struct elf_link_hash_entry **sym_hashes;
1956 1.1 skrll const Elf_Internal_Rela *rel;
1957 1.1 skrll const Elf_Internal_Rela *rel_end;
1958 1.1 skrll
1959 1.1 skrll symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1960 1.1.1.3 christos sym_hashes = elf_sym_hashes (abfd);
1961 1.1 skrll
1962 1.1 skrll /* First we sort the relocs so that any register relocs come before
1963 1.1 skrll expansion-relocs to the same insn. FIXME: Not done for mmo. */
1964 1.1 skrll qsort ((void *) relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1965 1.1.1.9 christos mmix_elf_sort_relocs);
1966 1.1 skrll
1967 1.1.1.4 christos /* Do the common part. */
1968 1.1.1.9 christos if (!mmix_elf_check_common_relocs (abfd, info, sec, relocs))
1969 1.1 skrll return false;
1970 1.1 skrll
1971 1.1 skrll if (bfd_link_relocatable (info))
1972 1.1 skrll return true;
1973 1.1 skrll
1974 1.1 skrll rel_end = relocs + sec->reloc_count;
1975 1.1 skrll for (rel = relocs; rel < rel_end; rel++)
1976 1.1 skrll {
1977 1.1 skrll struct elf_link_hash_entry *h;
1978 1.1.1.6 christos unsigned long r_symndx;
1979 1.1 skrll
1980 1.1 skrll r_symndx = ELF64_R_SYM (rel->r_info);
1981 1.1 skrll if (r_symndx < symtab_hdr->sh_info)
1982 1.1 skrll h = NULL;
1983 1.1 skrll else
1984 1.1 skrll {
1985 1.1 skrll h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1986 1.1 skrll while (h->root.type == bfd_link_hash_indirect
1987 1.1 skrll || h->root.type == bfd_link_hash_warning)
1988 1.1 skrll h = (struct elf_link_hash_entry *) h->root.u.i.link;
1989 1.1.1.6 christos }
1990 1.1.1.6 christos
1991 1.1.1.6 christos switch (ELF64_R_TYPE (rel->r_info))
1992 1.1.1.6 christos {
1993 1.1.1.9 christos /* This relocation describes the C++ object vtable hierarchy.
1994 1.1.1.6 christos Reconstruct it for later use during GC. */
1995 1.1.1.6 christos case R_MMIX_GNU_VTINHERIT:
1996 1.1.1.6 christos if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1997 1.1.1.6 christos return false;
1998 1.1.1.6 christos break;
1999 1.1.1.8 christos
2000 1.1.1.9 christos /* This relocation describes which C++ vtable entries are actually
2001 1.1.1.6 christos used. Record for later use during GC. */
2002 1.1 skrll case R_MMIX_GNU_VTENTRY:
2003 1.1 skrll if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2004 1.1 skrll return false;
2005 1.1.1.9 christos break;
2006 1.1 skrll }
2007 1.1 skrll }
2008 1.1 skrll
2009 1.1 skrll return true;
2010 1.1 skrll }
2011 1.1.1.9 christos
2012 1.1.1.3 christos /* Wrapper for mmix_elf_check_common_relocs, called when linking to mmo.
2013 1.1 skrll Copied from elf_link_add_object_symbols. */
2014 1.1 skrll
2015 1.1 skrll bool
2016 1.1 skrll _bfd_mmix_check_all_relocs (bfd *abfd, struct bfd_link_info *info)
2017 1.1 skrll {
2018 1.1 skrll asection *o;
2019 1.1.1.9 christos
2020 1.1 skrll for (o = abfd->sections; o != NULL; o = o->next)
2021 1.1 skrll {
2022 1.1 skrll Elf_Internal_Rela *internal_relocs;
2023 1.1 skrll bool ok;
2024 1.1 skrll
2025 1.1 skrll if ((o->flags & SEC_RELOC) == 0
2026 1.1 skrll || o->reloc_count == 0
2027 1.1 skrll || ((info->strip == strip_all || info->strip == strip_debugger)
2028 1.1 skrll && (o->flags & SEC_DEBUGGING) != 0)
2029 1.1.1.3 christos || bfd_is_abs_section (o->output_section))
2030 1.1 skrll continue;
2031 1.1 skrll
2032 1.1 skrll internal_relocs
2033 1.1.1.9 christos = _bfd_elf_link_read_relocs (abfd, o, NULL,
2034 1.1 skrll (Elf_Internal_Rela *) NULL,
2035 1.1 skrll info->keep_memory);
2036 1.1 skrll if (internal_relocs == NULL)
2037 1.1 skrll return false;
2038 1.1 skrll
2039 1.1 skrll ok = mmix_elf_check_common_relocs (abfd, info, o, internal_relocs);
2040 1.1 skrll
2041 1.1.1.9 christos if (! info->keep_memory)
2042 1.1 skrll free (internal_relocs);
2043 1.1 skrll
2044 1.1.1.9 christos if (! ok)
2045 1.1 skrll return false;
2046 1.1 skrll }
2047 1.1 skrll
2048 1.1 skrll return true;
2049 1.1 skrll }
2050 1.1 skrll
2051 1.1.1.2 christos /* Change symbols relative to the reg contents section to instead be to
2053 1.1.1.3 christos the register section, and scale them down to correspond to the register
2054 1.1.1.3 christos number. */
2055 1.1.1.3 christos
2056 1.1.1.3 christos static int
2057 1.1 skrll mmix_elf_link_output_symbol_hook (struct bfd_link_info *info ATTRIBUTE_UNUSED,
2058 1.1 skrll const char *name ATTRIBUTE_UNUSED,
2059 1.1 skrll Elf_Internal_Sym *sym,
2060 1.1 skrll asection *input_sec,
2061 1.1 skrll struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
2062 1.1 skrll {
2063 1.1 skrll if (input_sec != NULL
2064 1.1 skrll && input_sec->name != NULL
2065 1.1 skrll && ELF_ST_TYPE (sym->st_info) != STT_SECTION
2066 1.1 skrll && strcmp (input_sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2067 1.1.1.2 christos {
2068 1.1 skrll sym->st_value /= 8;
2069 1.1 skrll sym->st_shndx = SHN_REGISTER;
2070 1.1 skrll }
2071 1.1 skrll
2072 1.1 skrll return 1;
2073 1.1 skrll }
2074 1.1 skrll
2075 1.1.1.9 christos /* We fake a register section that holds values that are register numbers.
2076 1.1.1.9 christos Having a SHN_REGISTER and register section translates better to other
2077 1.1.1.9 christos formats (e.g. mmo) than for example a STT_REGISTER attribute.
2078 1.1.1.9 christos This section faking is based on a construct in elf32-mips.c. */
2079 1.1.1.9 christos static asection mmix_elf_reg_section;
2080 1.1 skrll static const asymbol mmix_elf_reg_section_symbol =
2081 1.1 skrll GLOBAL_SYM_INIT (MMIX_REG_SECTION_NAME, &mmix_elf_reg_section);
2082 1.1 skrll static asection mmix_elf_reg_section =
2083 1.1 skrll BFD_FAKE_SECTION (mmix_elf_reg_section, &mmix_elf_reg_section_symbol,
2084 1.1.1.5 christos MMIX_REG_SECTION_NAME, 0, SEC_NO_FLAGS);
2085 1.1 skrll
2086 1.1 skrll /* Handle the special section numbers that a symbol may use. */
2087 1.1 skrll
2088 1.1 skrll void
2089 1.1 skrll mmix_elf_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED, asymbol *asym)
2090 1.1 skrll {
2091 1.1 skrll elf_symbol_type *elfsym;
2092 1.1 skrll
2093 1.1 skrll elfsym = (elf_symbol_type *) asym;
2094 1.1 skrll switch (elfsym->internal_elf_sym.st_shndx)
2095 1.1 skrll {
2096 1.1 skrll case SHN_REGISTER:
2097 1.1 skrll asym->section = &mmix_elf_reg_section;
2098 1.1 skrll break;
2099 1.1 skrll
2100 1.1 skrll default:
2101 1.1 skrll break;
2102 1.1 skrll }
2103 1.1.1.9 christos }
2104 1.1.1.3 christos
2105 1.1.1.3 christos /* Given a BFD section, try to locate the corresponding ELF section
2106 1.1.1.3 christos index. */
2107 1.1 skrll
2108 1.1.1.8 christos static bool
2109 1.1 skrll mmix_elf_section_from_bfd_section (bfd * abfd ATTRIBUTE_UNUSED,
2110 1.1 skrll asection * sec,
2111 1.1.1.9 christos int * retval)
2112 1.1 skrll {
2113 1.1.1.9 christos if (strcmp (bfd_section_name (sec), MMIX_REG_SECTION_NAME) == 0)
2114 1.1 skrll *retval = SHN_REGISTER;
2115 1.1 skrll else
2116 1.1 skrll return false;
2117 1.1 skrll
2118 1.1 skrll return true;
2119 1.1 skrll }
2120 1.1 skrll
2121 1.1 skrll /* Hook called by the linker routine which adds symbols from an object
2122 1.1 skrll file. We must handle the special SHN_REGISTER section number here.
2123 1.1.1.9 christos
2124 1.1.1.3 christos We also check that we only have *one* each of the section-start
2125 1.1.1.3 christos symbols, since otherwise having two with the same value would cause
2126 1.1.1.3 christos them to be "merged", but with the contents serialized. */
2127 1.1.1.3 christos
2128 1.1.1.3 christos static bool
2129 1.1.1.3 christos mmix_elf_add_symbol_hook (bfd *abfd,
2130 1.1.1.3 christos struct bfd_link_info *info ATTRIBUTE_UNUSED,
2131 1.1 skrll Elf_Internal_Sym *sym,
2132 1.1 skrll const char **namep ATTRIBUTE_UNUSED,
2133 1.1 skrll flagword *flagsp ATTRIBUTE_UNUSED,
2134 1.1 skrll asection **secp,
2135 1.1 skrll bfd_vma *valp ATTRIBUTE_UNUSED)
2136 1.1 skrll {
2137 1.1 skrll if (sym->st_shndx == SHN_REGISTER)
2138 1.1.1.9 christos {
2139 1.1 skrll *secp = bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
2140 1.1 skrll (*secp)->flags |= SEC_LINKER_CREATED;
2141 1.1 skrll }
2142 1.1 skrll else if ((*namep)[0] == '_' && (*namep)[1] == '_' && (*namep)[2] == '.'
2143 1.1.1.9 christos && startswith (*namep, MMIX_LOC_SECTION_START_SYMBOL_PREFIX))
2144 1.1.1.9 christos {
2145 1.1.1.9 christos /* See if we have another one. */
2146 1.1 skrll struct bfd_link_hash_entry *h = bfd_link_hash_lookup (info->hash,
2147 1.1 skrll *namep,
2148 1.1 skrll false,
2149 1.1 skrll false,
2150 1.1 skrll false);
2151 1.1.1.6 christos
2152 1.1.1.6 christos if (h != NULL && h->type != bfd_link_hash_undefined)
2153 1.1.1.7 christos {
2154 1.1.1.7 christos /* How do we get the asymbol (or really: the filename) from h?
2155 1.1.1.6 christos h->u.def.section->owner is NULL. */
2156 1.1.1.6 christos _bfd_error_handler
2157 1.1 skrll /* xgettext:c-format */
2158 1.1.1.9 christos (_("%pB: error: multiple definition of `%s'; start of %s "
2159 1.1 skrll "is set in a earlier linked file"),
2160 1.1 skrll abfd, *namep,
2161 1.1 skrll *namep + strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX));
2162 1.1.1.9 christos bfd_set_error (bfd_error_bad_value);
2163 1.1 skrll return false;
2164 1.1 skrll }
2165 1.1 skrll }
2166 1.1 skrll
2167 1.1.1.9 christos return true;
2168 1.1.1.3 christos }
2169 1.1 skrll
2170 1.1 skrll /* We consider symbols matching "L.*:[0-9]+" to be local symbols. */
2171 1.1 skrll
2172 1.1 skrll static bool
2173 1.1 skrll mmix_elf_is_local_label_name (bfd *abfd, const char *name)
2174 1.1 skrll {
2175 1.1.1.9 christos const char *colpos;
2176 1.1 skrll int digits;
2177 1.1 skrll
2178 1.1.1.9 christos /* Also include the default local-label definition. */
2179 1.1 skrll if (_bfd_elf_is_local_label_name (abfd, name))
2180 1.1 skrll return true;
2181 1.1 skrll
2182 1.1 skrll if (*name != 'L')
2183 1.1.1.9 christos return false;
2184 1.1 skrll
2185 1.1 skrll /* If there's no ":", or more than one, it's not a local symbol. */
2186 1.1 skrll colpos = strchr (name, ':');
2187 1.1.1.9 christos if (colpos == NULL || strchr (colpos + 1, ':') != NULL)
2188 1.1 skrll return false;
2189 1.1 skrll
2190 1.1 skrll /* Check that there are remaining characters and that they are digits. */
2191 1.1 skrll if (colpos[1] == 0)
2192 1.1 skrll return false;
2193 1.1 skrll
2194 1.1 skrll digits = strspn (colpos + 1, "0123456789");
2195 1.1.1.9 christos return digits != 0 && colpos[1 + digits] == 0;
2196 1.1.1.3 christos }
2197 1.1 skrll
2198 1.1 skrll /* We get rid of the register section here. */
2199 1.1 skrll
2200 1.1 skrll bool
2201 1.1 skrll mmix_elf_final_link (bfd *abfd, struct bfd_link_info *info)
2202 1.1 skrll {
2203 1.1 skrll /* We never output a register section, though we create one for
2204 1.1 skrll temporary measures. Check that nobody entered contents into it. */
2205 1.1 skrll asection *reg_section;
2206 1.1 skrll
2207 1.1.1.8 christos reg_section = bfd_get_section_by_name (abfd, MMIX_REG_SECTION_NAME);
2208 1.1.1.7 christos
2209 1.1 skrll if (reg_section != NULL)
2210 1.1 skrll {
2211 1.1 skrll /* FIXME: Pass error state gracefully. */
2212 1.1 skrll if (bfd_section_flags (reg_section) & SEC_HAS_CONTENTS)
2213 1.1 skrll _bfd_abort (__FILE__, __LINE__, _("register section has contents\n"));
2214 1.1 skrll
2215 1.1 skrll /* Really remove the section, if it hasn't already been done. */
2216 1.1 skrll if (!bfd_section_removed_from_list (abfd, reg_section))
2217 1.1 skrll {
2218 1.1 skrll bfd_section_list_remove (abfd, reg_section);
2219 1.1.1.9 christos --abfd->section_count;
2220 1.1 skrll }
2221 1.1 skrll }
2222 1.1 skrll
2223 1.1 skrll if (! bfd_elf_final_link (abfd, info))
2224 1.1 skrll return false;
2225 1.1 skrll
2226 1.1 skrll /* Since this section is marked SEC_LINKER_CREATED, it isn't output by
2227 1.1 skrll the regular linker machinery. We do it here, like other targets with
2228 1.1 skrll special sections. */
2229 1.1 skrll if (info->base_file != NULL)
2230 1.1 skrll {
2231 1.1 skrll asection *greg_section
2232 1.1 skrll = bfd_get_section_by_name ((bfd *) info->base_file,
2233 1.1 skrll MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2234 1.1.1.9 christos if (!bfd_set_section_contents (abfd,
2235 1.1 skrll greg_section->output_section,
2236 1.1.1.9 christos greg_section->contents,
2237 1.1 skrll (file_ptr) greg_section->output_offset,
2238 1.1 skrll greg_section->size))
2239 1.1 skrll return false;
2240 1.1 skrll }
2241 1.1 skrll return true;
2242 1.1 skrll }
2243 1.1.1.3 christos
2244 1.1.1.3 christos /* We need to include the maximum size of PUSHJ-stubs in the initial
2245 1.1.1.3 christos section size. This is expected to shrink during linker relaxation. */
2246 1.1 skrll
2247 1.1 skrll static void
2248 1.1 skrll mmix_set_relaxable_size (bfd *abfd ATTRIBUTE_UNUSED,
2249 1.1 skrll asection *sec,
2250 1.1 skrll void *ptr)
2251 1.1 skrll {
2252 1.1 skrll struct bfd_link_info *info = ptr;
2253 1.1 skrll
2254 1.1 skrll /* Make sure we only do this for section where we know we want this,
2255 1.1 skrll otherwise we might end up resetting the size of COMMONs. */
2256 1.1 skrll if (mmix_elf_section_data (sec)->pjs.n_pushj_relocs == 0)
2257 1.1 skrll return;
2258 1.1 skrll
2259 1.1 skrll sec->rawsize = sec->size;
2260 1.1.1.4 christos sec->size += (mmix_elf_section_data (sec)->pjs.n_pushj_relocs
2261 1.1 skrll * MAX_PUSHJ_STUB_SIZE);
2262 1.1 skrll
2263 1.1 skrll /* For use in relocatable link, we start with a max stubs size. See
2264 1.1 skrll mmix_elf_relax_section. */
2265 1.1 skrll if (bfd_link_relocatable (info) && sec->output_section)
2266 1.1 skrll mmix_elf_section_data (sec->output_section)->pjs.stubs_size_sum
2267 1.1 skrll += (mmix_elf_section_data (sec)->pjs.n_pushj_relocs
2268 1.1 skrll * MAX_PUSHJ_STUB_SIZE);
2269 1.1.1.9 christos }
2270 1.1.1.3 christos
2271 1.1.1.3 christos /* Initialize stuff for the linker-generated GREGs to match
2272 1.1 skrll R_MMIX_BASE_PLUS_OFFSET relocs seen by the linker. */
2273 1.1 skrll
2274 1.1 skrll bool
2275 1.1 skrll _bfd_mmix_before_linker_allocation (bfd *abfd ATTRIBUTE_UNUSED,
2276 1.1 skrll struct bfd_link_info *info)
2277 1.1 skrll {
2278 1.1 skrll asection *bpo_gregs_section;
2279 1.1 skrll bfd *bpo_greg_owner;
2280 1.1 skrll struct bpo_greg_section_info *gregdata;
2281 1.1 skrll size_t n_gregs;
2282 1.1 skrll bfd_vma gregs_size;
2283 1.1.1.4 christos size_t i;
2284 1.1 skrll size_t *bpo_reloc_indexes;
2285 1.1 skrll bfd *ibfd;
2286 1.1 skrll
2287 1.1 skrll /* Set the initial size of sections. */
2288 1.1 skrll for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2289 1.1 skrll bfd_map_over_sections (ibfd, mmix_set_relaxable_size, info);
2290 1.1 skrll
2291 1.1.1.9 christos /* The bpo_greg_owner bfd is supposed to have been set by
2292 1.1 skrll mmix_elf_check_relocs when the first R_MMIX_BASE_PLUS_OFFSET is seen.
2293 1.1 skrll If there is no such object, there was no R_MMIX_BASE_PLUS_OFFSET. */
2294 1.1 skrll bpo_greg_owner = (bfd *) info->base_file;
2295 1.1 skrll if (bpo_greg_owner == NULL)
2296 1.1 skrll return true;
2297 1.1 skrll
2298 1.1.1.9 christos bpo_gregs_section
2299 1.1 skrll = bfd_get_section_by_name (bpo_greg_owner,
2300 1.1 skrll MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2301 1.1 skrll
2302 1.1 skrll if (bpo_gregs_section == NULL)
2303 1.1.1.9 christos return true;
2304 1.1 skrll
2305 1.1 skrll /* We use the target-data handle in the ELF section data. */
2306 1.1 skrll gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
2307 1.1 skrll if (gregdata == NULL)
2308 1.1 skrll return false;
2309 1.1 skrll
2310 1.1 skrll n_gregs = gregdata->n_bpo_relocs;
2311 1.1 skrll gregdata->n_allocated_bpo_gregs = n_gregs;
2312 1.1 skrll
2313 1.1 skrll /* When this reaches zero during relaxation, all entries have been
2314 1.1 skrll filled in and the size of the linker gregs can be calculated. */
2315 1.1.1.8 christos gregdata->n_remaining_bpo_relocs_this_relaxation_round = n_gregs;
2316 1.1.1.9 christos
2317 1.1 skrll /* Set the zeroth-order estimate for the GREGs size. */
2318 1.1 skrll gregs_size = n_gregs * 8;
2319 1.1 skrll
2320 1.1 skrll if (!bfd_set_section_size (bpo_gregs_section, gregs_size))
2321 1.1 skrll return false;
2322 1.1 skrll
2323 1.1 skrll /* Allocate and set up the GREG arrays. They're filled in at relaxation
2324 1.1 skrll time. Note that we must use the max number ever noted for the array,
2325 1.1 skrll since the index numbers were created before GC. */
2326 1.1 skrll gregdata->reloc_request
2327 1.1 skrll = bfd_zalloc (bpo_greg_owner,
2328 1.1 skrll sizeof (struct bpo_reloc_request)
2329 1.1 skrll * gregdata->n_max_bpo_relocs);
2330 1.1 skrll
2331 1.1 skrll gregdata->bpo_reloc_indexes
2332 1.1.1.9 christos = bpo_reloc_indexes
2333 1.1 skrll = bfd_alloc (bpo_greg_owner,
2334 1.1 skrll gregdata->n_max_bpo_relocs
2335 1.1 skrll * sizeof (size_t));
2336 1.1 skrll if (bpo_reloc_indexes == NULL)
2337 1.1 skrll return false;
2338 1.1 skrll
2339 1.1 skrll /* The default order is an identity mapping. */
2340 1.1 skrll for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2341 1.1.1.9 christos {
2342 1.1 skrll bpo_reloc_indexes[i] = i;
2343 1.1 skrll gregdata->reloc_request[i].bpo_reloc_no = i;
2344 1.1 skrll }
2345 1.1 skrll
2346 1.1 skrll return true;
2347 1.1.1.9 christos }
2348 1.1.1.3 christos
2349 1.1.1.3 christos /* Fill in contents in the linker allocated gregs. Everything is
2351 1.1 skrll calculated at this point; we just move the contents into place here. */
2352 1.1 skrll
2353 1.1 skrll bool
2354 1.1 skrll _bfd_mmix_after_linker_allocation (bfd *abfd ATTRIBUTE_UNUSED,
2355 1.1 skrll struct bfd_link_info *link_info)
2356 1.1 skrll {
2357 1.1 skrll asection *bpo_gregs_section;
2358 1.1 skrll bfd *bpo_greg_owner;
2359 1.1 skrll struct bpo_greg_section_info *gregdata;
2360 1.1 skrll size_t n_gregs;
2361 1.1 skrll size_t i, j;
2362 1.1 skrll size_t lastreg;
2363 1.1 skrll bfd_byte *contents;
2364 1.1.1.9 christos
2365 1.1 skrll /* The bpo_greg_owner bfd is supposed to have been set by mmix_elf_check_relocs
2366 1.1 skrll when the first R_MMIX_BASE_PLUS_OFFSET is seen. If there is no such
2367 1.1 skrll object, there was no R_MMIX_BASE_PLUS_OFFSET. */
2368 1.1 skrll bpo_greg_owner = (bfd *) link_info->base_file;
2369 1.1 skrll if (bpo_greg_owner == NULL)
2370 1.1 skrll return true;
2371 1.1 skrll
2372 1.1 skrll bpo_gregs_section
2373 1.1 skrll = bfd_get_section_by_name (bpo_greg_owner,
2374 1.1.1.9 christos MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2375 1.1 skrll
2376 1.1 skrll /* This can't happen without DSO handling. When DSOs are handled
2377 1.1 skrll without any R_MMIX_BASE_PLUS_OFFSET seen, there will be no such
2378 1.1 skrll section. */
2379 1.1 skrll if (bpo_gregs_section == NULL)
2380 1.1.1.9 christos return true;
2381 1.1 skrll
2382 1.1 skrll /* We use the target-data handle in the ELF section data. */
2383 1.1 skrll
2384 1.1 skrll gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
2385 1.1 skrll if (gregdata == NULL)
2386 1.1 skrll return false;
2387 1.1.1.9 christos
2388 1.1.1.11 christos n_gregs = gregdata->n_allocated_bpo_gregs;
2389 1.1 skrll
2390 1.1 skrll bpo_gregs_section->contents
2391 1.1 skrll = contents = bfd_alloc (bpo_greg_owner, bpo_gregs_section->size);
2392 1.1 skrll if (contents == NULL)
2393 1.1 skrll return false;
2394 1.1 skrll bpo_gregs_section->alloced = 1;
2395 1.1 skrll
2396 1.1 skrll /* Sanity check: If these numbers mismatch, some relocation has not been
2397 1.1.1.6 christos accounted for and the rest of gregdata is probably inconsistent.
2398 1.1.1.6 christos It's a bug, but it's more helpful to identify it than segfaulting
2399 1.1.1.7 christos below. */
2400 1.1.1.7 christos if (gregdata->n_remaining_bpo_relocs_this_relaxation_round
2401 1.1.1.6 christos != gregdata->n_bpo_relocs)
2402 1.1.1.6 christos {
2403 1.1.1.9 christos _bfd_error_handler
2404 1.1 skrll /* xgettext:c-format */
2405 1.1 skrll (_("internal inconsistency: remaining %lu != max %lu;"
2406 1.1 skrll " please report this bug"),
2407 1.1 skrll (unsigned long) gregdata->n_remaining_bpo_relocs_this_relaxation_round,
2408 1.1 skrll (unsigned long) gregdata->n_bpo_relocs);
2409 1.1 skrll return false;
2410 1.1 skrll }
2411 1.1 skrll
2412 1.1 skrll for (lastreg = 255, i = 0, j = 0; j < n_gregs; i++)
2413 1.1 skrll if (gregdata->reloc_request[i].regindex != lastreg)
2414 1.1 skrll {
2415 1.1.1.9 christos bfd_put_64 (bpo_greg_owner, gregdata->reloc_request[i].value,
2416 1.1 skrll contents + j * 8);
2417 1.1 skrll lastreg = gregdata->reloc_request[i].regindex;
2418 1.1 skrll j++;
2419 1.1 skrll }
2420 1.1 skrll
2421 1.1 skrll return true;
2422 1.1.1.3 christos }
2423 1.1 skrll
2424 1.1 skrll /* Sort valid relocs to come before non-valid relocs, then on increasing
2425 1.1 skrll value. */
2426 1.1 skrll
2427 1.1 skrll static int
2428 1.1 skrll bpo_reloc_request_sort_fn (const void * p1, const void * p2)
2429 1.1 skrll {
2430 1.1 skrll const struct bpo_reloc_request *r1 = (const struct bpo_reloc_request *) p1;
2431 1.1 skrll const struct bpo_reloc_request *r2 = (const struct bpo_reloc_request *) p2;
2432 1.1 skrll
2433 1.1 skrll /* Primary function is validity; non-valid relocs sorted after valid
2434 1.1 skrll ones. */
2435 1.1 skrll if (r1->valid != r2->valid)
2436 1.1 skrll return r2->valid - r1->valid;
2437 1.1 skrll
2438 1.1 skrll /* Then sort on value. Don't simplify and return just the difference of
2439 1.1 skrll the values: the upper bits of the 64-bit value would be truncated on
2440 1.1 skrll a host with 32-bit ints. */
2441 1.1 skrll if (r1->value != r2->value)
2442 1.1 skrll return r1->value > r2->value ? 1 : -1;
2443 1.1 skrll
2444 1.1 skrll /* As a last re-sort, use the relocation number, so we get a stable
2445 1.1 skrll sort. The *addresses* aren't stable since items are swapped during
2446 1.1 skrll sorting. It depends on the qsort implementation if this actually
2447 1.1 skrll happens. */
2448 1.1 skrll return r1->bpo_reloc_no > r2->bpo_reloc_no
2449 1.1 skrll ? 1 : (r1->bpo_reloc_no < r2->bpo_reloc_no ? -1 : 0);
2450 1.1.1.5 christos }
2451 1.1.1.6 christos
2452 1.1 skrll /* For debug use only. Dumps the global register allocations resulting
2453 1.1 skrll from base-plus-offset relocs. */
2454 1.1 skrll
2455 1.1 skrll void
2456 1.1 skrll mmix_dump_bpo_gregs (struct bfd_link_info *link_info,
2457 1.1 skrll void (*pf) (const char *fmt, ...))
2458 1.1 skrll {
2459 1.1 skrll bfd *bpo_greg_owner;
2460 1.1 skrll asection *bpo_gregs_section;
2461 1.1 skrll struct bpo_greg_section_info *gregdata;
2462 1.1 skrll unsigned int i;
2463 1.1 skrll
2464 1.1 skrll if (link_info == NULL || link_info->base_file == NULL)
2465 1.1 skrll return;
2466 1.1 skrll
2467 1.1 skrll bpo_greg_owner = (bfd *) link_info->base_file;
2468 1.1 skrll
2469 1.1 skrll bpo_gregs_section
2470 1.1 skrll = bfd_get_section_by_name (bpo_greg_owner,
2471 1.1 skrll MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2472 1.1 skrll
2473 1.1 skrll if (bpo_gregs_section == NULL)
2474 1.1 skrll return;
2475 1.1 skrll
2476 1.1 skrll gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
2477 1.1 skrll if (gregdata == NULL)
2478 1.1 skrll return;
2479 1.1 skrll
2480 1.1 skrll if (pf == NULL)
2481 1.1 skrll pf = _bfd_error_handler;
2482 1.1 skrll
2483 1.1 skrll /* These format strings are not translated. They are for debug purposes
2484 1.1 skrll only and never displayed to an end user. Should they escape, we
2485 1.1 skrll surely want them in original. */
2486 1.1 skrll (*pf) (" n_bpo_relocs: %u\n n_max_bpo_relocs: %u\n n_remain...round: %u\n\
2487 1.1 skrll n_allocated_bpo_gregs: %u\n", gregdata->n_bpo_relocs,
2488 1.1 skrll gregdata->n_max_bpo_relocs,
2489 1.1 skrll gregdata->n_remaining_bpo_relocs_this_relaxation_round,
2490 1.1 skrll gregdata->n_allocated_bpo_gregs);
2491 1.1 skrll
2492 1.1 skrll if (gregdata->reloc_request)
2493 1.1 skrll for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2494 1.1 skrll (*pf) ("%4u (%4u)/%4u#%u: 0x%08lx%08lx r: %3u o: %3u\n",
2495 1.1 skrll i,
2496 1.1 skrll (gregdata->bpo_reloc_indexes != NULL
2497 1.1 skrll ? gregdata->bpo_reloc_indexes[i] : (size_t) -1),
2498 1.1 skrll gregdata->reloc_request[i].bpo_reloc_no,
2499 1.1 skrll gregdata->reloc_request[i].valid,
2500 1.1 skrll
2501 1.1 skrll (unsigned long) (gregdata->reloc_request[i].value >> 32),
2502 1.1 skrll (unsigned long) gregdata->reloc_request[i].value,
2503 1.1 skrll gregdata->reloc_request[i].regindex,
2504 1.1 skrll gregdata->reloc_request[i].offset);
2505 1.1.1.2 christos }
2506 1.1 skrll
2507 1.1 skrll /* This links all R_MMIX_BASE_PLUS_OFFSET relocs into a special array, and
2508 1.1 skrll when the last such reloc is done, an index-array is sorted according to
2509 1.1 skrll the values and iterated over to produce register numbers (indexed by 0
2510 1.1 skrll from the first allocated register number) and offsets for use in real
2511 1.1.1.9 christos relocation. (N.B.: Relocatable runs are handled, not just punted.)
2512 1.1.1.3 christos
2513 1.1.1.3 christos PUSHJ stub accounting is also done here.
2514 1.1.1.3 christos
2515 1.1.1.9 christos Symbol- and reloc-reading infrastructure copied from elf-m10200.c. */
2516 1.1 skrll
2517 1.1 skrll static bool
2518 1.1 skrll mmix_elf_relax_section (bfd *abfd,
2519 1.1 skrll asection *sec,
2520 1.1 skrll struct bfd_link_info *link_info,
2521 1.1 skrll bool *again)
2522 1.1 skrll {
2523 1.1 skrll Elf_Internal_Shdr *symtab_hdr;
2524 1.1 skrll Elf_Internal_Rela *internal_relocs;
2525 1.1 skrll Elf_Internal_Rela *irel, *irelend;
2526 1.1 skrll asection *bpo_gregs_section = NULL;
2527 1.1 skrll struct bpo_greg_section_info *gregdata;
2528 1.1.1.9 christos struct bpo_reloc_section_info *bpodata
2529 1.1 skrll = mmix_elf_section_data (sec)->bpo.reloc;
2530 1.1 skrll /* The initialization is to quiet compiler warnings. The value is to
2531 1.1 skrll spot a missing actual initialization. */
2532 1.1 skrll size_t bpono = (size_t) -1;
2533 1.1 skrll size_t pjsno = 0;
2534 1.1 skrll size_t pjsno_undefs = 0;
2535 1.1.1.9 christos Elf_Internal_Sym *isymbuf = NULL;
2536 1.1 skrll bfd_size_type size = sec->rawsize ? sec->rawsize : sec->size;
2537 1.1 skrll
2538 1.1 skrll mmix_elf_section_data (sec)->pjs.stubs_size_sum = 0;
2539 1.1 skrll
2540 1.1 skrll /* Assume nothing changes. */
2541 1.1 skrll *again = false;
2542 1.1 skrll
2543 1.1 skrll /* We don't have to do anything if this section does not have relocs, or
2544 1.1.1.6 christos if this is not a code section. */
2545 1.1 skrll if ((sec->flags & SEC_RELOC) == 0
2546 1.1 skrll || sec->reloc_count == 0
2547 1.1.1.9 christos || (sec->flags & SEC_CODE) == 0
2548 1.1 skrll || (sec->flags & SEC_LINKER_CREATED) != 0
2549 1.1 skrll /* If no R_MMIX_BASE_PLUS_OFFSET relocs and no PUSHJ-stub relocs,
2550 1.1 skrll then nothing to do. */
2551 1.1 skrll || (bpodata == NULL
2552 1.1 skrll && mmix_elf_section_data (sec)->pjs.n_pushj_relocs == 0))
2553 1.1 skrll return true;
2554 1.1 skrll
2555 1.1 skrll symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2556 1.1 skrll
2557 1.1 skrll if (bpodata != NULL)
2558 1.1 skrll {
2559 1.1 skrll bpo_gregs_section = bpodata->bpo_greg_section;
2560 1.1 skrll gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
2561 1.1 skrll bpono = bpodata->first_base_plus_offset_reloc;
2562 1.1.1.3 christos }
2563 1.1 skrll else
2564 1.1 skrll gregdata = NULL;
2565 1.1 skrll
2566 1.1 skrll /* Get a copy of the native relocations. */
2567 1.1 skrll internal_relocs
2568 1.1 skrll = _bfd_elf_link_read_relocs (abfd, sec, NULL,
2569 1.1 skrll (Elf_Internal_Rela *) NULL,
2570 1.1 skrll link_info->keep_memory);
2571 1.1 skrll if (internal_relocs == NULL)
2572 1.1 skrll goto error_return;
2573 1.1 skrll
2574 1.1 skrll /* Walk through them looking for relaxing opportunities. */
2575 1.1 skrll irelend = internal_relocs + sec->reloc_count;
2576 1.1 skrll for (irel = internal_relocs; irel < irelend; irel++)
2577 1.1 skrll {
2578 1.1 skrll bfd_vma symval;
2579 1.1 skrll struct elf_link_hash_entry *h = NULL;
2580 1.1 skrll
2581 1.1 skrll /* We only process two relocs. */
2582 1.1 skrll if (ELF64_R_TYPE (irel->r_info) != (int) R_MMIX_BASE_PLUS_OFFSET
2583 1.1.1.4 christos && ELF64_R_TYPE (irel->r_info) != (int) R_MMIX_PUSHJ_STUBBABLE)
2584 1.1 skrll continue;
2585 1.1 skrll
2586 1.1 skrll /* We process relocs in a distinctly different way when this is a
2587 1.1 skrll relocatable link (for one, we don't look at symbols), so we avoid
2588 1.1 skrll mixing its code with that for the "normal" relaxation. */
2589 1.1 skrll if (bfd_link_relocatable (link_info))
2590 1.1 skrll {
2591 1.1 skrll /* The only transformation in a relocatable link is to generate
2592 1.1 skrll a full stub at the location of the stub calculated for the
2593 1.1 skrll input section, if the relocated stub location, the end of the
2594 1.1 skrll output section plus earlier stubs, cannot be reached. Thus
2595 1.1 skrll relocatable linking can only lead to worse code, but it still
2596 1.1 skrll works. */
2597 1.1 skrll if (ELF64_R_TYPE (irel->r_info) == R_MMIX_PUSHJ_STUBBABLE)
2598 1.1 skrll {
2599 1.1 skrll /* If we can reach the end of the output-section and beyond
2600 1.1 skrll any current stubs, then we don't need a stub for this
2601 1.1 skrll reloc. The relaxed order of output stub allocation may
2602 1.1 skrll not exactly match the straightforward order, so we always
2603 1.1 skrll assume presence of output stubs, which will allow
2604 1.1 skrll relaxation only on relocations indifferent to the
2605 1.1 skrll presence of output stub allocations for other relocations
2606 1.1 skrll and thus the order of output stub allocation. */
2607 1.1 skrll if (bfd_check_overflow (complain_overflow_signed,
2608 1.1 skrll 19,
2609 1.1 skrll 0,
2610 1.1 skrll bfd_arch_bits_per_address (abfd),
2611 1.1 skrll /* Output-stub location. */
2612 1.1 skrll sec->output_section->rawsize
2613 1.1 skrll + (mmix_elf_section_data (sec
2614 1.1 skrll ->output_section)
2615 1.1 skrll ->pjs.stubs_size_sum)
2616 1.1 skrll /* Location of this PUSHJ reloc. */
2617 1.1 skrll - (sec->output_offset + irel->r_offset)
2618 1.1 skrll /* Don't count *this* stub twice. */
2619 1.1 skrll - (mmix_elf_section_data (sec)
2620 1.1 skrll ->pjs.stub_size[pjsno]
2621 1.1 skrll + MAX_PUSHJ_STUB_SIZE))
2622 1.1 skrll == bfd_reloc_ok)
2623 1.1 skrll mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 0;
2624 1.1 skrll
2625 1.1 skrll mmix_elf_section_data (sec)->pjs.stubs_size_sum
2626 1.1 skrll += mmix_elf_section_data (sec)->pjs.stub_size[pjsno];
2627 1.1 skrll
2628 1.1 skrll pjsno++;
2629 1.1 skrll }
2630 1.1 skrll
2631 1.1 skrll continue;
2632 1.1 skrll }
2633 1.1 skrll
2634 1.1 skrll /* Get the value of the symbol referred to by the reloc. */
2635 1.1 skrll if (ELF64_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2636 1.1 skrll {
2637 1.1 skrll /* A local symbol. */
2638 1.1 skrll Elf_Internal_Sym *isym;
2639 1.1 skrll asection *sym_sec;
2640 1.1 skrll
2641 1.1 skrll /* Read this BFD's local symbols if we haven't already. */
2642 1.1 skrll if (isymbuf == NULL)
2643 1.1 skrll {
2644 1.1 skrll isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2645 1.1 skrll if (isymbuf == NULL)
2646 1.1 skrll isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2647 1.1 skrll symtab_hdr->sh_info, 0,
2648 1.1 skrll NULL, NULL, NULL);
2649 1.1 skrll if (isymbuf == 0)
2650 1.1 skrll goto error_return;
2651 1.1 skrll }
2652 1.1 skrll
2653 1.1 skrll isym = isymbuf + ELF64_R_SYM (irel->r_info);
2654 1.1 skrll if (isym->st_shndx == SHN_UNDEF)
2655 1.1 skrll sym_sec = bfd_und_section_ptr;
2656 1.1 skrll else if (isym->st_shndx == SHN_ABS)
2657 1.1 skrll sym_sec = bfd_abs_section_ptr;
2658 1.1 skrll else if (isym->st_shndx == SHN_COMMON)
2659 1.1 skrll sym_sec = bfd_com_section_ptr;
2660 1.1 skrll else
2661 1.1 skrll sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2662 1.1 skrll symval = (isym->st_value
2663 1.1 skrll + sym_sec->output_section->vma
2664 1.1 skrll + sym_sec->output_offset);
2665 1.1 skrll }
2666 1.1 skrll else
2667 1.1 skrll {
2668 1.1.1.6 christos unsigned long indx;
2669 1.1.1.6 christos
2670 1.1.1.6 christos /* An external symbol. */
2671 1.1.1.6 christos indx = ELF64_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2672 1.1.1.6 christos h = elf_sym_hashes (abfd)[indx];
2673 1.1.1.6 christos BFD_ASSERT (h != NULL);
2674 1.1.1.6 christos if (h->root.type == bfd_link_hash_undefweak)
2675 1.1.1.6 christos /* FIXME: for R_MMIX_PUSHJ_STUBBABLE, there are alternatives to
2676 1.1.1.6 christos the canonical value 0 for an unresolved weak symbol to
2677 1.1.1.6 christos consider: as the debug-friendly approach, resolve to "abort"
2678 1.1.1.6 christos (or a port-specific function), or as the space-friendly
2679 1.1.1.6 christos approach resolve to the next instruction (like some other
2680 1.1.1.6 christos ports, notably ARM and AArch64). These alternatives require
2681 1.1.1.6 christos matching code in mmix_elf_perform_relocation or its caller. */
2682 1.1.1.6 christos symval = 0;
2683 1.1 skrll else if (h->root.type == bfd_link_hash_defined
2684 1.1 skrll || h->root.type == bfd_link_hash_defweak)
2685 1.1 skrll symval = (h->root.u.def.value
2686 1.1 skrll + h->root.u.def.section->output_section->vma
2687 1.1 skrll + h->root.u.def.section->output_offset);
2688 1.1 skrll else
2689 1.1 skrll {
2690 1.1 skrll /* This appears to be a reference to an undefined symbol. Just
2691 1.1 skrll ignore it--it will be caught by the regular reloc processing.
2692 1.1 skrll We need to keep BPO reloc accounting consistent, though
2693 1.1 skrll else we'll abort instead of emitting an error message. */
2694 1.1.1.9 christos if (ELF64_R_TYPE (irel->r_info) == R_MMIX_BASE_PLUS_OFFSET
2695 1.1.1.9 christos && gregdata != NULL)
2696 1.1.1.9 christos {
2697 1.1.1.9 christos gregdata->n_remaining_bpo_relocs_this_relaxation_round--;
2698 1.1.1.9 christos bpono++;
2699 1.1 skrll }
2700 1.1 skrll
2701 1.1 skrll /* Similarly, keep accounting consistent for PUSHJ
2702 1.1 skrll referring to an undefined symbol. */
2703 1.1 skrll if (ELF64_R_TYPE (irel->r_info) == R_MMIX_PUSHJ_STUBBABLE)
2704 1.1 skrll pjsno_undefs++;
2705 1.1 skrll continue;
2706 1.1 skrll }
2707 1.1 skrll }
2708 1.1 skrll
2709 1.1 skrll if (ELF64_R_TYPE (irel->r_info) == (int) R_MMIX_PUSHJ_STUBBABLE)
2710 1.1 skrll {
2711 1.1 skrll bfd_vma value = symval + irel->r_addend;
2712 1.1 skrll bfd_vma dot
2713 1.1 skrll = (sec->output_section->vma
2714 1.1 skrll + sec->output_offset
2715 1.1 skrll + irel->r_offset);
2716 1.1 skrll bfd_vma stubaddr
2717 1.1 skrll = (sec->output_section->vma
2718 1.1 skrll + sec->output_offset
2719 1.1 skrll + size
2720 1.1 skrll + mmix_elf_section_data (sec)->pjs.stubs_size_sum);
2721 1.1 skrll
2722 1.1 skrll if ((value & 3) == 0
2723 1.1 skrll && bfd_check_overflow (complain_overflow_signed,
2724 1.1 skrll 19,
2725 1.1 skrll 0,
2726 1.1 skrll bfd_arch_bits_per_address (abfd),
2727 1.1 skrll value - dot
2728 1.1 skrll - (value > dot
2729 1.1 skrll ? mmix_elf_section_data (sec)
2730 1.1 skrll ->pjs.stub_size[pjsno]
2731 1.1 skrll : 0))
2732 1.1 skrll == bfd_reloc_ok)
2733 1.1 skrll /* If the reloc fits, no stub is needed. */
2734 1.1 skrll mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 0;
2735 1.1 skrll else
2736 1.1 skrll /* Maybe we can get away with just a JMP insn? */
2737 1.1 skrll if ((value & 3) == 0
2738 1.1 skrll && bfd_check_overflow (complain_overflow_signed,
2739 1.1 skrll 27,
2740 1.1 skrll 0,
2741 1.1 skrll bfd_arch_bits_per_address (abfd),
2742 1.1 skrll value - stubaddr
2743 1.1 skrll - (value > dot
2744 1.1 skrll ? mmix_elf_section_data (sec)
2745 1.1 skrll ->pjs.stub_size[pjsno] - 4
2746 1.1 skrll : 0))
2747 1.1 skrll == bfd_reloc_ok)
2748 1.1 skrll /* Yep, account for a stub consisting of a single JMP insn. */
2749 1.1 skrll mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 4;
2750 1.1 skrll else
2751 1.1 skrll /* Nope, go for the full insn stub. It doesn't seem useful to
2752 1.1 skrll emit the intermediate sizes; those will only be useful for
2753 1.1 skrll a >64M program assuming contiguous code. */
2754 1.1 skrll mmix_elf_section_data (sec)->pjs.stub_size[pjsno]
2755 1.1 skrll = MAX_PUSHJ_STUB_SIZE;
2756 1.1 skrll
2757 1.1 skrll mmix_elf_section_data (sec)->pjs.stubs_size_sum
2758 1.1 skrll += mmix_elf_section_data (sec)->pjs.stub_size[pjsno];
2759 1.1 skrll pjsno++;
2760 1.1 skrll continue;
2761 1.1.1.9 christos }
2762 1.1 skrll
2763 1.1 skrll /* We're looking at a R_MMIX_BASE_PLUS_OFFSET reloc. */
2764 1.1 skrll
2765 1.1 skrll gregdata->reloc_request[gregdata->bpo_reloc_indexes[bpono]].value
2766 1.1 skrll = symval + irel->r_addend;
2767 1.1 skrll gregdata->reloc_request[gregdata->bpo_reloc_indexes[bpono++]].valid = true;
2768 1.1 skrll gregdata->n_remaining_bpo_relocs_this_relaxation_round--;
2769 1.1 skrll }
2770 1.1 skrll
2771 1.1 skrll /* Check if that was the last BPO-reloc. If so, sort the values and
2772 1.1 skrll calculate how many registers we need to cover them. Set the size of
2773 1.1 skrll the linker gregs, and if the number of registers changed, indicate
2774 1.1 skrll that we need to relax some more because we have more work to do. */
2775 1.1 skrll if (gregdata != NULL
2776 1.1 skrll && gregdata->n_remaining_bpo_relocs_this_relaxation_round == 0)
2777 1.1 skrll {
2778 1.1 skrll size_t i;
2779 1.1 skrll bfd_vma prev_base;
2780 1.1.1.3 christos size_t regindex;
2781 1.1 skrll
2782 1.1 skrll /* First, reset the remaining relocs for the next round. */
2783 1.1 skrll gregdata->n_remaining_bpo_relocs_this_relaxation_round
2784 1.1 skrll = gregdata->n_bpo_relocs;
2785 1.1 skrll
2786 1.1 skrll qsort (gregdata->reloc_request,
2787 1.1 skrll gregdata->n_max_bpo_relocs,
2788 1.1 skrll sizeof (struct bpo_reloc_request),
2789 1.1 skrll bpo_reloc_request_sort_fn);
2790 1.1 skrll
2791 1.1 skrll /* Recalculate indexes. When we find a change (however unlikely
2792 1.1 skrll after the initial iteration), we know we need to relax again,
2793 1.1 skrll since items in the GREG-array are sorted by increasing value and
2794 1.1 skrll stored in the relaxation phase. */
2795 1.1.1.9 christos for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2796 1.1 skrll if (gregdata->bpo_reloc_indexes[gregdata->reloc_request[i].bpo_reloc_no]
2797 1.1 skrll != i)
2798 1.1 skrll {
2799 1.1 skrll gregdata->bpo_reloc_indexes[gregdata->reloc_request[i].bpo_reloc_no]
2800 1.1 skrll = i;
2801 1.1 skrll *again = true;
2802 1.1 skrll }
2803 1.1 skrll
2804 1.1 skrll /* Allocate register numbers (indexing from 0). Stop at the first
2805 1.1 skrll non-valid reloc. */
2806 1.1 skrll for (i = 0, regindex = 0, prev_base = gregdata->reloc_request[0].value;
2807 1.1 skrll i < gregdata->n_bpo_relocs;
2808 1.1 skrll i++)
2809 1.1 skrll {
2810 1.1 skrll if (gregdata->reloc_request[i].value > prev_base + 255)
2811 1.1 skrll {
2812 1.1 skrll regindex++;
2813 1.1 skrll prev_base = gregdata->reloc_request[i].value;
2814 1.1 skrll }
2815 1.1 skrll gregdata->reloc_request[i].regindex = regindex;
2816 1.1 skrll gregdata->reloc_request[i].offset
2817 1.1 skrll = gregdata->reloc_request[i].value - prev_base;
2818 1.1 skrll }
2819 1.1 skrll
2820 1.1 skrll /* If it's not the same as the last time, we need to relax again,
2821 1.1.1.9 christos because the size of the section has changed. I'm not sure we
2822 1.1 skrll actually need to do any adjustments since the shrinking happens
2823 1.1 skrll at the start of this section, but better safe than sorry. */
2824 1.1 skrll if (gregdata->n_allocated_bpo_gregs != regindex + 1)
2825 1.1 skrll {
2826 1.1 skrll gregdata->n_allocated_bpo_gregs = regindex + 1;
2827 1.1 skrll *again = true;
2828 1.1 skrll }
2829 1.1 skrll
2830 1.1 skrll bpo_gregs_section->size = (regindex + 1) * 8;
2831 1.1 skrll }
2832 1.1 skrll
2833 1.1 skrll if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
2834 1.1 skrll {
2835 1.1 skrll if (! link_info->keep_memory)
2836 1.1 skrll free (isymbuf);
2837 1.1 skrll else
2838 1.1.1.9 christos {
2839 1.1.1.9 christos /* Cache the symbols for elf_link_input_bfd. */
2840 1.1.1.6 christos symtab_hdr->contents = (unsigned char *) isymbuf;
2841 1.1.1.9 christos }
2842 1.1 skrll }
2843 1.1 skrll
2844 1.1 skrll BFD_ASSERT(pjsno + pjsno_undefs
2845 1.1 skrll == mmix_elf_section_data (sec)->pjs.n_pushj_relocs);
2846 1.1 skrll
2847 1.1 skrll if (elf_section_data (sec)->relocs != internal_relocs)
2848 1.1 skrll free (internal_relocs);
2849 1.1 skrll
2850 1.1.1.9 christos if (sec->size < size + mmix_elf_section_data (sec)->pjs.stubs_size_sum)
2851 1.1 skrll abort ();
2852 1.1 skrll
2853 1.1.1.9 christos if (sec->size > size + mmix_elf_section_data (sec)->pjs.stubs_size_sum)
2854 1.1 skrll {
2855 1.1 skrll sec->size = size + mmix_elf_section_data (sec)->pjs.stubs_size_sum;
2856 1.1.1.9 christos *again = true;
2857 1.1 skrll }
2858 1.1.1.9 christos
2859 1.1 skrll return true;
2860 1.1.1.9 christos
2861 1.1 skrll error_return:
2862 1.1 skrll if ((unsigned char *) isymbuf != symtab_hdr->contents)
2863 1.1 skrll free (isymbuf);
2864 1.1.1.6 christos if (elf_section_data (sec)->relocs != internal_relocs)
2865 1.1.1.11 christos free (internal_relocs);
2866 1.1 skrll return false;
2867 1.1 skrll }
2868 1.1 skrll
2869 1.1 skrll #define ELF_ARCH bfd_arch_mmix
2871 1.1 skrll #define ELF_MACHINE_CODE EM_MMIX
2872 1.1 skrll #define ELF_TARGET_ID MMIX_ELF_DATA
2873 1.1 skrll
2874 1.1 skrll /* According to mmix-doc page 36 (paragraph 45), this should be (1LL << 48LL).
2875 1.1 skrll However, that's too much for something somewhere in the linker part of
2876 1.1 skrll BFD; perhaps the start-address has to be a non-zero multiple of this
2877 1.1 skrll number, or larger than this number. The symptom is that the linker
2878 1.1 skrll complains: "warning: allocated section `.text' not in segment". We
2879 1.1 skrll settle for 64k; the page-size used in examples is 8k.
2880 1.1 skrll #define ELF_MAXPAGESIZE 0x10000
2881 1.1.1.4 christos
2882 1.1 skrll Unfortunately, this causes excessive padding in the supposedly small
2883 1.1 skrll for-education programs that are the expected usage (where people would
2884 1.1 skrll inspect output). We stick to 256 bytes just to have *some* default
2885 1.1 skrll alignment. */
2886 1.1 skrll #define ELF_MAXPAGESIZE 0x100
2887 1.1 skrll
2888 1.1 skrll #define TARGET_BIG_SYM mmix_elf64_vec
2889 1.1 skrll #define TARGET_BIG_NAME "elf64-mmix"
2890 1.1 skrll
2891 1.1 skrll #define elf_info_to_howto_rel NULL
2892 1.1 skrll #define elf_info_to_howto mmix_info_to_howto_rela
2893 1.1 skrll #define elf_backend_relocate_section mmix_elf_relocate_section
2894 1.1 skrll #define elf_backend_gc_mark_hook mmix_elf_gc_mark_hook
2895 1.1.1.7 christos
2896 1.1 skrll #define elf_backend_link_output_symbol_hook \
2897 1.1.1.6 christos mmix_elf_link_output_symbol_hook
2898 1.1.1.6 christos #define elf_backend_add_symbol_hook mmix_elf_add_symbol_hook
2899 1.1.1.6 christos
2900 1.1 skrll #define elf_backend_check_relocs mmix_elf_check_relocs
2901 1.1 skrll #define elf_backend_symbol_processing mmix_elf_symbol_processing
2902 1.1 skrll #define elf_backend_omit_section_dynsym _bfd_elf_omit_section_dynsym_all
2903 1.1 skrll
2904 1.1 skrll #define bfd_elf64_bfd_copy_link_hash_symbol_type \
2905 1.1 skrll _bfd_generic_copy_link_hash_symbol_type
2906 1.1 skrll
2907 1.1 skrll #define bfd_elf64_bfd_is_local_label_name \
2908 1.1 skrll mmix_elf_is_local_label_name
2909 1.1 skrll
2910 1.1 skrll #define elf_backend_may_use_rel_p 0
2911 1.1 skrll #define elf_backend_may_use_rela_p 1
2912 1.1 skrll #define elf_backend_default_use_rela_p 1
2913 1.1 skrll
2914 1.1 skrll #define elf_backend_can_gc_sections 1
2915 1.1 skrll #define elf_backend_section_from_bfd_section \
2916 mmix_elf_section_from_bfd_section
2917
2918 #define bfd_elf64_new_section_hook mmix_elf_new_section_hook
2919 #define bfd_elf64_bfd_final_link mmix_elf_final_link
2920 #define bfd_elf64_bfd_relax_section mmix_elf_relax_section
2921
2922 #include "elf64-target.h"
2923