* elf.c (elf_fake_sections): Fix up .tbss sh_size and sh_type.
[deliverable/binutils-gdb.git] / bfd / elf32-mips.c
CommitLineData
efcbd82c 1/* MIPS-specific support for 32-bit ELF
3f0a7f7f 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
be3ccd9c 3 Free Software Foundation, Inc.
252b5132
RH
4
5 Most of the information added by Ian Lance Taylor, Cygnus Support,
6 <ian@cygnus.com>.
103186c6
MM
7 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
8 <mark@codesourcery.com>
f7cb7d68
UC
9 Traditional MIPS targets support added by Koundinya.K, Dansk Data
10 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
252b5132
RH
11
12This file is part of BFD, the Binary File Descriptor library.
13
14This program is free software; you can redistribute it and/or modify
15it under the terms of the GNU General Public License as published by
16the Free Software Foundation; either version 2 of the License, or
17(at your option) any later version.
18
19This program is distributed in the hope that it will be useful,
20but WITHOUT ANY WARRANTY; without even the implied warranty of
21MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22GNU General Public License for more details.
23
24You should have received a copy of the GNU General Public License
25along with this program; if not, write to the Free Software
26Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27
28/* This file handles MIPS ELF targets. SGI Irix 5 uses a slightly
29 different MIPS ELF from other targets. This matters when linking.
30 This file supports both, switching at runtime. */
31
32#include "bfd.h"
33#include "sysdep.h"
34#include "libbfd.h"
35#include "bfdlink.h"
36#include "genlink.h"
37#include "elf-bfd.h"
c6e90b02 38#include "elfxx-mips.h"
252b5132
RH
39#include "elf/mips.h"
40
41/* Get the ECOFF swapping routines. */
42#include "coff/sym.h"
43#include "coff/symconst.h"
44#include "coff/internal.h"
45#include "coff/ecoff.h"
46#include "coff/mips.h"
23e2c83b 47#define ECOFF_SIGNED_32
252b5132
RH
48#include "ecoffswap.h"
49
c6e90b02
TS
50static bfd_reloc_status_type mips_elf_hi16_reloc
51 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
52static bfd_reloc_status_type mips_elf_lo16_reloc
53 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
54static bfd_reloc_status_type mips_elf_got16_reloc
55 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
56static bfd_reloc_status_type mips_elf_gprel32_reloc
57 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
252b5132
RH
58static bfd_reloc_status_type mips32_64bit_reloc
59 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
60static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
61 PARAMS ((bfd *, bfd_reloc_code_real_type));
c6e90b02
TS
62static reloc_howto_type *mips_elf32_rtype_to_howto
63 PARAMS ((unsigned int, boolean));
252b5132
RH
64static void mips_info_to_howto_rel
65 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
3f830999
MM
66static void mips_info_to_howto_rela
67 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
252b5132 68static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
c6e90b02 69static boolean mips_elf32_object_p PARAMS ((bfd *));
252b5132
RH
70static boolean mips_elf_is_local_label_name
71 PARAMS ((bfd *, const char *));
252b5132
RH
72static bfd_reloc_status_type mips16_jump_reloc
73 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
74static bfd_reloc_status_type mips16_gprel_reloc
75 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
252b5132
RH
76static bfd_reloc_status_type mips_elf_final_gp
77 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
7403cb63 78static boolean mips_elf_assign_gp PARAMS ((bfd *, bfd_vma *));
c6e90b02 79static boolean elf32_mips_grok_prstatus
9e80ff3a 80 PARAMS ((bfd *, Elf_Internal_Note *));
c6e90b02 81static boolean elf32_mips_grok_psinfo
9e80ff3a 82 PARAMS ((bfd *, Elf_Internal_Note *));
c6e90b02
TS
83static irix_compat_t elf32_mips_irix_compat
84 PARAMS ((bfd *));
252b5132 85
cb7394f2
TS
86extern const bfd_target bfd_elf32_bigmips_vec;
87extern const bfd_target bfd_elf32_littlemips_vec;
adb76a3e 88
a94a7c1c 89/* Nonzero if ABFD is using the N32 ABI. */
a94a7c1c
MM
90#define ABI_N32_P(abfd) \
91 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
92
4e8a9624 93/* Whether we are trying to be compatible with IRIX at all. */
a94a7c1c 94#define SGI_COMPAT(abfd) \
c6e90b02 95 (elf32_mips_irix_compat (abfd) != ict_none)
103186c6 96
252b5132
RH
97/* The number of local .got entries we reserve. */
98#define MIPS_RESERVED_GOTNO (2)
99
3f830999
MM
100/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
101 from smaller values. Start with zero, widen, *then* decrement. */
102#define MINUS_ONE (((bfd_vma)0) - 1)
103
d75bc93d
TS
104/* The relocation table used for SHT_REL sections. */
105
106static reloc_howto_type elf_mips_howto_table_rel[] =
107{
108 /* No relocation. */
109 HOWTO (R_MIPS_NONE, /* type */
110 0, /* rightshift */
111 0, /* size (0 = byte, 1 = short, 2 = long) */
112 0, /* bitsize */
113 false, /* pc_relative */
114 0, /* bitpos */
115 complain_overflow_dont, /* complain_on_overflow */
116 bfd_elf_generic_reloc, /* special_function */
117 "R_MIPS_NONE", /* name */
118 false, /* partial_inplace */
119 0, /* src_mask */
120 0, /* dst_mask */
121 false), /* pcrel_offset */
122
123 /* 16 bit relocation. */
124 HOWTO (R_MIPS_16, /* type */
125 0, /* rightshift */
126 2, /* size (0 = byte, 1 = short, 2 = long) */
127 16, /* bitsize */
128 false, /* pc_relative */
129 0, /* bitpos */
130 complain_overflow_signed, /* complain_on_overflow */
131 bfd_elf_generic_reloc, /* special_function */
132 "R_MIPS_16", /* name */
133 true, /* partial_inplace */
134 0x0000ffff, /* src_mask */
135 0x0000ffff, /* dst_mask */
136 false), /* pcrel_offset */
137
138 /* 32 bit relocation. */
139 HOWTO (R_MIPS_32, /* type */
140 0, /* rightshift */
141 2, /* size (0 = byte, 1 = short, 2 = long) */
142 32, /* bitsize */
143 false, /* pc_relative */
144 0, /* bitpos */
145 complain_overflow_dont, /* complain_on_overflow */
146 bfd_elf_generic_reloc, /* special_function */
147 "R_MIPS_32", /* name */
148 true, /* partial_inplace */
149 0xffffffff, /* src_mask */
150 0xffffffff, /* dst_mask */
151 false), /* pcrel_offset */
152
153 /* 32 bit symbol relative relocation. */
154 HOWTO (R_MIPS_REL32, /* type */
155 0, /* rightshift */
156 2, /* size (0 = byte, 1 = short, 2 = long) */
157 32, /* bitsize */
158 false, /* pc_relative */
159 0, /* bitpos */
160 complain_overflow_dont, /* complain_on_overflow */
161 bfd_elf_generic_reloc, /* special_function */
162 "R_MIPS_REL32", /* name */
163 true, /* partial_inplace */
164 0xffffffff, /* src_mask */
165 0xffffffff, /* dst_mask */
166 false), /* pcrel_offset */
167
168 /* 26 bit jump address. */
169 HOWTO (R_MIPS_26, /* type */
170 2, /* rightshift */
171 2, /* size (0 = byte, 1 = short, 2 = long) */
172 26, /* bitsize */
173 false, /* pc_relative */
174 0, /* bitpos */
175 complain_overflow_dont, /* complain_on_overflow */
176 /* This needs complex overflow
177 detection, because the upper four
178 bits must match the PC + 4. */
179 bfd_elf_generic_reloc, /* special_function */
180 "R_MIPS_26", /* name */
181 true, /* partial_inplace */
182 0x03ffffff, /* src_mask */
183 0x03ffffff, /* dst_mask */
184 false), /* pcrel_offset */
185
186 /* High 16 bits of symbol value. */
187 HOWTO (R_MIPS_HI16, /* type */
188 0, /* rightshift */
189 2, /* size (0 = byte, 1 = short, 2 = long) */
190 16, /* bitsize */
191 false, /* pc_relative */
192 0, /* bitpos */
193 complain_overflow_dont, /* complain_on_overflow */
c6e90b02 194 mips_elf_hi16_reloc, /* special_function */
d75bc93d
TS
195 "R_MIPS_HI16", /* name */
196 true, /* partial_inplace */
197 0x0000ffff, /* src_mask */
198 0x0000ffff, /* dst_mask */
199 false), /* pcrel_offset */
200
201 /* Low 16 bits of symbol value. */
202 HOWTO (R_MIPS_LO16, /* type */
203 0, /* rightshift */
204 2, /* size (0 = byte, 1 = short, 2 = long) */
205 16, /* bitsize */
206 false, /* pc_relative */
207 0, /* bitpos */
208 complain_overflow_dont, /* complain_on_overflow */
c6e90b02 209 mips_elf_lo16_reloc, /* special_function */
d75bc93d
TS
210 "R_MIPS_LO16", /* name */
211 true, /* partial_inplace */
212 0x0000ffff, /* src_mask */
213 0x0000ffff, /* dst_mask */
214 false), /* pcrel_offset */
215
216 /* GP relative reference. */
217 HOWTO (R_MIPS_GPREL16, /* type */
218 0, /* rightshift */
219 2, /* size (0 = byte, 1 = short, 2 = long) */
220 16, /* bitsize */
221 false, /* pc_relative */
222 0, /* bitpos */
223 complain_overflow_signed, /* complain_on_overflow */
c6e90b02 224 _bfd_mips_elf32_gprel16_reloc, /* special_function */
d75bc93d
TS
225 "R_MIPS_GPREL16", /* name */
226 true, /* partial_inplace */
227 0x0000ffff, /* src_mask */
228 0x0000ffff, /* dst_mask */
229 false), /* pcrel_offset */
230
231 /* Reference to literal section. */
232 HOWTO (R_MIPS_LITERAL, /* type */
233 0, /* rightshift */
234 2, /* size (0 = byte, 1 = short, 2 = long) */
235 16, /* bitsize */
236 false, /* pc_relative */
237 0, /* bitpos */
238 complain_overflow_signed, /* complain_on_overflow */
c6e90b02 239 _bfd_mips_elf32_gprel16_reloc, /* special_function */
d75bc93d
TS
240 "R_MIPS_LITERAL", /* name */
241 true, /* partial_inplace */
242 0x0000ffff, /* src_mask */
243 0x0000ffff, /* dst_mask */
244 false), /* pcrel_offset */
245
246 /* Reference to global offset table. */
247 HOWTO (R_MIPS_GOT16, /* type */
248 0, /* rightshift */
249 2, /* size (0 = byte, 1 = short, 2 = long) */
250 16, /* bitsize */
251 false, /* pc_relative */
252 0, /* bitpos */
253 complain_overflow_signed, /* complain_on_overflow */
c6e90b02 254 mips_elf_got16_reloc, /* special_function */
d75bc93d
TS
255 "R_MIPS_GOT16", /* name */
256 true, /* partial_inplace */
257 0x0000ffff, /* src_mask */
258 0x0000ffff, /* dst_mask */
259 false), /* pcrel_offset */
260
261 /* 16 bit PC relative reference. */
262 HOWTO (R_MIPS_PC16, /* type */
263 0, /* rightshift */
264 2, /* size (0 = byte, 1 = short, 2 = long) */
265 16, /* bitsize */
266 true, /* pc_relative */
267 0, /* bitpos */
268 complain_overflow_signed, /* complain_on_overflow */
269 bfd_elf_generic_reloc, /* special_function */
270 "R_MIPS_PC16", /* name */
271 true, /* partial_inplace */
272 0x0000ffff, /* src_mask */
273 0x0000ffff, /* dst_mask */
274 true), /* pcrel_offset */
275
276 /* 16 bit call through global offset table. */
277 HOWTO (R_MIPS_CALL16, /* type */
278 0, /* rightshift */
279 2, /* size (0 = byte, 1 = short, 2 = long) */
280 16, /* bitsize */
281 false, /* pc_relative */
282 0, /* bitpos */
283 complain_overflow_signed, /* complain_on_overflow */
284 bfd_elf_generic_reloc, /* special_function */
285 "R_MIPS_CALL16", /* name */
286 true, /* partial_inplace */
287 0x0000ffff, /* src_mask */
288 0x0000ffff, /* dst_mask */
289 false), /* pcrel_offset */
290
291 /* 32 bit GP relative reference. */
292 HOWTO (R_MIPS_GPREL32, /* type */
293 0, /* rightshift */
294 2, /* size (0 = byte, 1 = short, 2 = long) */
295 32, /* bitsize */
296 false, /* pc_relative */
297 0, /* bitpos */
298 complain_overflow_dont, /* complain_on_overflow */
c6e90b02 299 mips_elf_gprel32_reloc, /* special_function */
d75bc93d
TS
300 "R_MIPS_GPREL32", /* name */
301 true, /* partial_inplace */
302 0xffffffff, /* src_mask */
303 0xffffffff, /* dst_mask */
304 false), /* pcrel_offset */
305
306 /* The remaining relocs are defined on Irix 5, although they are
307 not defined by the ABI. */
308 EMPTY_HOWTO (13),
309 EMPTY_HOWTO (14),
310 EMPTY_HOWTO (15),
311
312 /* A 5 bit shift field. */
313 HOWTO (R_MIPS_SHIFT5, /* type */
314 0, /* rightshift */
315 2, /* size (0 = byte, 1 = short, 2 = long) */
316 5, /* bitsize */
317 false, /* pc_relative */
318 6, /* bitpos */
319 complain_overflow_bitfield, /* complain_on_overflow */
320 bfd_elf_generic_reloc, /* special_function */
321 "R_MIPS_SHIFT5", /* name */
322 true, /* partial_inplace */
323 0x000007c0, /* src_mask */
324 0x000007c0, /* dst_mask */
325 false), /* pcrel_offset */
326
327 /* A 6 bit shift field. */
328 /* FIXME: This is not handled correctly; a special function is
329 needed to put the most significant bit in the right place. */
330 HOWTO (R_MIPS_SHIFT6, /* type */
331 0, /* rightshift */
332 2, /* size (0 = byte, 1 = short, 2 = long) */
333 6, /* bitsize */
334 false, /* pc_relative */
335 6, /* bitpos */
336 complain_overflow_bitfield, /* complain_on_overflow */
337 bfd_elf_generic_reloc, /* special_function */
338 "R_MIPS_SHIFT6", /* name */
339 true, /* partial_inplace */
340 0x000007c4, /* src_mask */
341 0x000007c4, /* dst_mask */
342 false), /* pcrel_offset */
343
344 /* A 64 bit relocation. */
345 HOWTO (R_MIPS_64, /* type */
346 0, /* rightshift */
347 4, /* size (0 = byte, 1 = short, 2 = long) */
348 64, /* bitsize */
349 false, /* pc_relative */
350 0, /* bitpos */
351 complain_overflow_dont, /* complain_on_overflow */
352 mips32_64bit_reloc, /* special_function */
353 "R_MIPS_64", /* name */
354 true, /* partial_inplace */
355 MINUS_ONE, /* src_mask */
356 MINUS_ONE, /* dst_mask */
357 false), /* pcrel_offset */
358
359 /* Displacement in the global offset table. */
360 HOWTO (R_MIPS_GOT_DISP, /* type */
361 0, /* rightshift */
362 2, /* size (0 = byte, 1 = short, 2 = long) */
363 16, /* bitsize */
364 false, /* pc_relative */
365 0, /* bitpos */
366 complain_overflow_signed, /* complain_on_overflow */
367 bfd_elf_generic_reloc, /* special_function */
368 "R_MIPS_GOT_DISP", /* name */
369 true, /* partial_inplace */
370 0x0000ffff, /* src_mask */
371 0x0000ffff, /* dst_mask */
372 false), /* pcrel_offset */
373
374 /* Displacement to page pointer in the global offset table. */
375 HOWTO (R_MIPS_GOT_PAGE, /* type */
376 0, /* rightshift */
377 2, /* size (0 = byte, 1 = short, 2 = long) */
378 16, /* bitsize */
379 false, /* pc_relative */
380 0, /* bitpos */
381 complain_overflow_signed, /* complain_on_overflow */
382 bfd_elf_generic_reloc, /* special_function */
383 "R_MIPS_GOT_PAGE", /* name */
384 true, /* partial_inplace */
385 0x0000ffff, /* src_mask */
386 0x0000ffff, /* dst_mask */
387 false), /* pcrel_offset */
388
389 /* Offset from page pointer in the global offset table. */
390 HOWTO (R_MIPS_GOT_OFST, /* type */
391 0, /* rightshift */
392 2, /* size (0 = byte, 1 = short, 2 = long) */
393 16, /* bitsize */
394 false, /* pc_relative */
395 0, /* bitpos */
396 complain_overflow_signed, /* complain_on_overflow */
397 bfd_elf_generic_reloc, /* special_function */
398 "R_MIPS_GOT_OFST", /* name */
399 true, /* partial_inplace */
400 0x0000ffff, /* src_mask */
401 0x0000ffff, /* dst_mask */
402 false), /* pcrel_offset */
403
404 /* High 16 bits of displacement in global offset table. */
405 HOWTO (R_MIPS_GOT_HI16, /* type */
406 0, /* rightshift */
407 2, /* size (0 = byte, 1 = short, 2 = long) */
408 16, /* bitsize */
409 false, /* pc_relative */
410 0, /* bitpos */
411 complain_overflow_dont, /* complain_on_overflow */
412 bfd_elf_generic_reloc, /* special_function */
413 "R_MIPS_GOT_HI16", /* name */
414 true, /* partial_inplace */
415 0x0000ffff, /* src_mask */
416 0x0000ffff, /* dst_mask */
417 false), /* pcrel_offset */
418
419 /* Low 16 bits of displacement in global offset table. */
420 HOWTO (R_MIPS_GOT_LO16, /* type */
421 0, /* rightshift */
422 2, /* size (0 = byte, 1 = short, 2 = long) */
423 16, /* bitsize */
424 false, /* pc_relative */
425 0, /* bitpos */
426 complain_overflow_dont, /* complain_on_overflow */
427 bfd_elf_generic_reloc, /* special_function */
428 "R_MIPS_GOT_LO16", /* name */
429 true, /* partial_inplace */
430 0x0000ffff, /* src_mask */
431 0x0000ffff, /* dst_mask */
432 false), /* pcrel_offset */
433
434 /* 64 bit subtraction. Used in the N32 ABI. */
435 HOWTO (R_MIPS_SUB, /* type */
436 0, /* rightshift */
437 4, /* size (0 = byte, 1 = short, 2 = long) */
438 64, /* bitsize */
439 false, /* pc_relative */
440 0, /* bitpos */
441 complain_overflow_dont, /* complain_on_overflow */
442 bfd_elf_generic_reloc, /* special_function */
443 "R_MIPS_SUB", /* name */
444 true, /* partial_inplace */
445 MINUS_ONE, /* src_mask */
446 MINUS_ONE, /* dst_mask */
447 false), /* pcrel_offset */
448
449 /* Used to cause the linker to insert and delete instructions? */
450 EMPTY_HOWTO (R_MIPS_INSERT_A),
451 EMPTY_HOWTO (R_MIPS_INSERT_B),
452 EMPTY_HOWTO (R_MIPS_DELETE),
453
454 /* Get the higher value of a 64 bit addend. */
455 HOWTO (R_MIPS_HIGHER, /* type */
456 0, /* rightshift */
457 2, /* size (0 = byte, 1 = short, 2 = long) */
458 16, /* bitsize */
459 false, /* pc_relative */
460 0, /* bitpos */
461 complain_overflow_dont, /* complain_on_overflow */
462 bfd_elf_generic_reloc, /* special_function */
463 "R_MIPS_HIGHER", /* name */
464 true, /* partial_inplace */
465 0x0000ffff, /* src_mask */
466 0x0000ffff, /* dst_mask */
467 false), /* pcrel_offset */
468
469 /* Get the highest value of a 64 bit addend. */
470 HOWTO (R_MIPS_HIGHEST, /* type */
471 0, /* rightshift */
472 2, /* size (0 = byte, 1 = short, 2 = long) */
473 16, /* bitsize */
474 false, /* pc_relative */
475 0, /* bitpos */
476 complain_overflow_dont, /* complain_on_overflow */
477 bfd_elf_generic_reloc, /* special_function */
478 "R_MIPS_HIGHEST", /* name */
479 true, /* partial_inplace */
480 0x0000ffff, /* src_mask */
481 0x0000ffff, /* dst_mask */
482 false), /* pcrel_offset */
483
484 /* High 16 bits of displacement in global offset table. */
485 HOWTO (R_MIPS_CALL_HI16, /* type */
486 0, /* rightshift */
487 2, /* size (0 = byte, 1 = short, 2 = long) */
488 16, /* bitsize */
489 false, /* pc_relative */
490 0, /* bitpos */
491 complain_overflow_dont, /* complain_on_overflow */
492 bfd_elf_generic_reloc, /* special_function */
493 "R_MIPS_CALL_HI16", /* name */
494 true, /* partial_inplace */
495 0x0000ffff, /* src_mask */
496 0x0000ffff, /* dst_mask */
497 false), /* pcrel_offset */
498
499 /* Low 16 bits of displacement in global offset table. */
500 HOWTO (R_MIPS_CALL_LO16, /* type */
501 0, /* rightshift */
502 2, /* size (0 = byte, 1 = short, 2 = long) */
503 16, /* bitsize */
504 false, /* pc_relative */
505 0, /* bitpos */
506 complain_overflow_dont, /* complain_on_overflow */
507 bfd_elf_generic_reloc, /* special_function */
508 "R_MIPS_CALL_LO16", /* name */
509 true, /* partial_inplace */
510 0x0000ffff, /* src_mask */
511 0x0000ffff, /* dst_mask */
512 false), /* pcrel_offset */
513
514 /* Section displacement. */
515 HOWTO (R_MIPS_SCN_DISP, /* type */
516 0, /* rightshift */
517 2, /* size (0 = byte, 1 = short, 2 = long) */
518 32, /* bitsize */
519 false, /* pc_relative */
520 0, /* bitpos */
521 complain_overflow_dont, /* complain_on_overflow */
522 bfd_elf_generic_reloc, /* special_function */
523 "R_MIPS_SCN_DISP", /* name */
524 true, /* partial_inplace */
525 0xffffffff, /* src_mask */
526 0xffffffff, /* dst_mask */
527 false), /* pcrel_offset */
528
529 EMPTY_HOWTO (R_MIPS_REL16),
530 EMPTY_HOWTO (R_MIPS_ADD_IMMEDIATE),
531 EMPTY_HOWTO (R_MIPS_PJUMP),
532 EMPTY_HOWTO (R_MIPS_RELGOT),
533
534 /* Protected jump conversion. This is an optimization hint. No
535 relocation is required for correctness. */
536 HOWTO (R_MIPS_JALR, /* type */
537 0, /* rightshift */
538 2, /* size (0 = byte, 1 = short, 2 = long) */
539 32, /* bitsize */
540 false, /* pc_relative */
541 0, /* bitpos */
542 complain_overflow_dont, /* complain_on_overflow */
543 bfd_elf_generic_reloc, /* special_function */
544 "R_MIPS_JALR", /* name */
545 false, /* partial_inplace */
546 0x00000000, /* src_mask */
547 0x00000000, /* dst_mask */
548 false), /* pcrel_offset */
549};
550
c6e90b02
TS
551/* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
552 is a hack to make the linker think that we need 64 bit values. */
553static reloc_howto_type elf_mips_ctor64_howto =
554 HOWTO (R_MIPS_64, /* type */
252b5132 555 0, /* rightshift */
c6e90b02 556 4, /* size (0 = byte, 1 = short, 2 = long) */
252b5132
RH
557 32, /* bitsize */
558 false, /* pc_relative */
559 0, /* bitpos */
c6e90b02
TS
560 complain_overflow_signed, /* complain_on_overflow */
561 mips32_64bit_reloc, /* special_function */
562 "R_MIPS_64", /* name */
563 true, /* partial_inplace */
564 0xffffffff, /* src_mask */
252b5132 565 0xffffffff, /* dst_mask */
c6e90b02 566 false); /* pcrel_offset */
252b5132 567
c6e90b02
TS
568/* The reloc used for the mips16 jump instruction. */
569static reloc_howto_type elf_mips16_jump_howto =
570 HOWTO (R_MIPS16_26, /* type */
252b5132
RH
571 2, /* rightshift */
572 2, /* size (0 = byte, 1 = short, 2 = long) */
573 26, /* bitsize */
574 false, /* pc_relative */
575 0, /* bitpos */
576 complain_overflow_dont, /* complain_on_overflow */
c6e90b02
TS
577 /* This needs complex overflow
578 detection, because the upper four
579 bits must match the PC. */
580 mips16_jump_reloc, /* special_function */
581 "R_MIPS16_26", /* name */
582 true, /* partial_inplace */
583 0x3ffffff, /* src_mask */
584 0x3ffffff, /* dst_mask */
585 false); /* pcrel_offset */
252b5132 586
c6e90b02
TS
587/* The reloc used for the mips16 gprel instruction. */
588static reloc_howto_type elf_mips16_gprel_howto =
589 HOWTO (R_MIPS16_GPREL, /* type */
252b5132
RH
590 0, /* rightshift */
591 2, /* size (0 = byte, 1 = short, 2 = long) */
592 16, /* bitsize */
593 false, /* pc_relative */
594 0, /* bitpos */
c6e90b02
TS
595 complain_overflow_signed, /* complain_on_overflow */
596 mips16_gprel_reloc, /* special_function */
597 "R_MIPS16_GPREL", /* name */
598 true, /* partial_inplace */
599 0x07ff001f, /* src_mask */
600 0x07ff001f, /* dst_mask */
601 false); /* pcrel_offset */
252b5132 602
c6e90b02
TS
603/* GNU extensions for embedded-pic. */
604/* High 16 bits of symbol value, pc-relative. */
605static reloc_howto_type elf_mips_gnu_rel_hi16 =
606 HOWTO (R_MIPS_GNU_REL_HI16, /* type */
252b5132
RH
607 0, /* rightshift */
608 2, /* size (0 = byte, 1 = short, 2 = long) */
609 16, /* bitsize */
c6e90b02 610 true, /* pc_relative */
252b5132
RH
611 0, /* bitpos */
612 complain_overflow_dont, /* complain_on_overflow */
c6e90b02
TS
613 mips_elf_hi16_reloc, /* special_function */
614 "R_MIPS_GNU_REL_HI16", /* name */
615 true, /* partial_inplace */
616 0xffff, /* src_mask */
617 0xffff, /* dst_mask */
618 true); /* pcrel_offset */
252b5132 619
c6e90b02
TS
620/* Low 16 bits of symbol value, pc-relative. */
621static reloc_howto_type elf_mips_gnu_rel_lo16 =
622 HOWTO (R_MIPS_GNU_REL_LO16, /* type */
252b5132
RH
623 0, /* rightshift */
624 2, /* size (0 = byte, 1 = short, 2 = long) */
625 16, /* bitsize */
c6e90b02 626 true, /* pc_relative */
252b5132 627 0, /* bitpos */
c6e90b02
TS
628 complain_overflow_dont, /* complain_on_overflow */
629 mips_elf_lo16_reloc, /* special_function */
630 "R_MIPS_GNU_REL_LO16", /* name */
631 true, /* partial_inplace */
632 0xffff, /* src_mask */
633 0xffff, /* dst_mask */
634 true); /* pcrel_offset */
252b5132 635
c6e90b02
TS
636/* 16 bit offset for pc-relative branches. */
637static reloc_howto_type elf_mips_gnu_rel16_s2 =
638 HOWTO (R_MIPS_GNU_REL16_S2, /* type */
639 2, /* rightshift */
252b5132
RH
640 2, /* size (0 = byte, 1 = short, 2 = long) */
641 16, /* bitsize */
c6e90b02 642 true, /* pc_relative */
252b5132
RH
643 0, /* bitpos */
644 complain_overflow_signed, /* complain_on_overflow */
c6e90b02
TS
645 bfd_elf_generic_reloc, /* special_function */
646 "R_MIPS_GNU_REL16_S2", /* name */
647 true, /* partial_inplace */
648 0xffff, /* src_mask */
649 0xffff, /* dst_mask */
650 true); /* pcrel_offset */
252b5132 651
c6e90b02
TS
652/* 64 bit pc-relative. */
653static reloc_howto_type elf_mips_gnu_pcrel64 =
654 HOWTO (R_MIPS_PC64, /* type */
252b5132 655 0, /* rightshift */
c6e90b02
TS
656 4, /* size (0 = byte, 1 = short, 2 = long) */
657 64, /* bitsize */
658 true, /* pc_relative */
252b5132
RH
659 0, /* bitpos */
660 complain_overflow_signed, /* complain_on_overflow */
c6e90b02
TS
661 bfd_elf_generic_reloc, /* special_function */
662 "R_MIPS_PC64", /* name */
663 true, /* partial_inplace */
664 MINUS_ONE, /* src_mask */
665 MINUS_ONE, /* dst_mask */
666 true); /* pcrel_offset */
252b5132 667
c6e90b02
TS
668/* 32 bit pc-relative. */
669static reloc_howto_type elf_mips_gnu_pcrel32 =
670 HOWTO (R_MIPS_PC32, /* type */
252b5132
RH
671 0, /* rightshift */
672 2, /* size (0 = byte, 1 = short, 2 = long) */
c6e90b02 673 32, /* bitsize */
252b5132
RH
674 true, /* pc_relative */
675 0, /* bitpos */
676 complain_overflow_signed, /* complain_on_overflow */
677 bfd_elf_generic_reloc, /* special_function */
c6e90b02
TS
678 "R_MIPS_PC32", /* name */
679 true, /* partial_inplace */
680 0xffffffff, /* src_mask */
681 0xffffffff, /* dst_mask */
682 true); /* pcrel_offset */
252b5132 683
c6e90b02
TS
684/* GNU extension to record C++ vtable hierarchy */
685static reloc_howto_type elf_mips_gnu_vtinherit_howto =
686 HOWTO (R_MIPS_GNU_VTINHERIT, /* type */
252b5132
RH
687 0, /* rightshift */
688 2, /* size (0 = byte, 1 = short, 2 = long) */
c6e90b02 689 0, /* bitsize */
252b5132
RH
690 false, /* pc_relative */
691 0, /* bitpos */
c6e90b02
TS
692 complain_overflow_dont, /* complain_on_overflow */
693 NULL, /* special_function */
694 "R_MIPS_GNU_VTINHERIT", /* name */
252b5132 695 false, /* partial_inplace */
d75bc93d 696 0, /* src_mask */
c6e90b02
TS
697 0, /* dst_mask */
698 false); /* pcrel_offset */
252b5132 699
c6e90b02
TS
700/* GNU extension to record C++ vtable member usage */
701static reloc_howto_type elf_mips_gnu_vtentry_howto =
702 HOWTO (R_MIPS_GNU_VTENTRY, /* type */
252b5132
RH
703 0, /* rightshift */
704 2, /* size (0 = byte, 1 = short, 2 = long) */
c6e90b02 705 0, /* bitsize */
252b5132
RH
706 false, /* pc_relative */
707 0, /* bitpos */
708 complain_overflow_dont, /* complain_on_overflow */
c6e90b02
TS
709 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
710 "R_MIPS_GNU_VTENTRY", /* name */
d75bc93d
TS
711 false, /* partial_inplace */
712 0, /* src_mask */
c6e90b02
TS
713 0, /* dst_mask */
714 false); /* pcrel_offset */
252b5132
RH
715
716/* Do a R_MIPS_HI16 relocation. This has to be done in combination
c6e90b02
TS
717 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
718 the HI16. Here we just save the information we need; we do the
719 actual relocation when we see the LO16.
c6142e5d 720
c6e90b02
TS
721 MIPS ELF requires that the LO16 immediately follow the HI16. As a
722 GNU extension, for non-pc-relative relocations, we permit an
723 arbitrary number of HI16 relocs to be associated with a single LO16
724 reloc. This extension permits gcc to output the HI and LO relocs
725 itself.
c6142e5d 726
c6e90b02
TS
727 This cannot be done for PC-relative relocations because both the HI16
728 and LO16 parts of the relocations must be done relative to the LO16
729 part, and there can be carry to or borrow from the HI16 part. */
103186c6 730
c6e90b02 731struct mips_hi16
103186c6 732{
c6e90b02
TS
733 struct mips_hi16 *next;
734 bfd_byte *addr;
735 bfd_vma addend;
736};
be3ccd9c 737
c6e90b02 738/* FIXME: This should not be a static variable. */
103186c6 739
c6e90b02 740static struct mips_hi16 *mips_hi16_list;
252b5132 741
c6e90b02
TS
742static bfd_reloc_status_type
743mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data, input_section,
744 output_bfd, error_message)
745 bfd *abfd ATTRIBUTE_UNUSED;
746 arelent *reloc_entry;
747 asymbol *symbol;
748 PTR data;
749 asection *input_section;
750 bfd *output_bfd;
751 char **error_message;
252b5132 752{
c6e90b02
TS
753 bfd_reloc_status_type ret;
754 bfd_vma relocation;
755 struct mips_hi16 *n;
252b5132 756
cb7394f2 757 /* If we're relocating, and this is an external symbol, we don't want
c6e90b02
TS
758 to change anything. */
759 if (output_bfd != (bfd *) NULL
760 && (symbol->flags & BSF_SECTION_SYM) == 0
761 && reloc_entry->addend == 0)
252b5132 762 {
c6e90b02
TS
763 reloc_entry->address += input_section->output_offset;
764 return bfd_reloc_ok;
252b5132 765 }
252b5132 766
c6e90b02 767 ret = bfd_reloc_ok;
252b5132 768
c6e90b02
TS
769 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
770 {
771 boolean relocateable;
772 bfd_vma gp;
252b5132 773
c6e90b02
TS
774 if (ret == bfd_reloc_undefined)
775 abort ();
252b5132 776
c6e90b02
TS
777 if (output_bfd != NULL)
778 relocateable = true;
252b5132 779 else
252b5132 780 {
c6e90b02
TS
781 relocateable = false;
782 output_bfd = symbol->section->output_section->owner;
252b5132
RH
783 }
784
c6e90b02
TS
785 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
786 error_message, &gp);
787 if (ret != bfd_reloc_ok)
788 return ret;
252b5132 789
c6e90b02 790 relocation = gp - reloc_entry->address;
252b5132
RH
791 }
792 else
793 {
c6e90b02
TS
794 if (bfd_is_und_section (symbol->section)
795 && output_bfd == (bfd *) NULL)
796 ret = bfd_reloc_undefined;
252b5132 797
c6e90b02
TS
798 if (bfd_is_com_section (symbol->section))
799 relocation = 0;
252b5132 800 else
c6e90b02
TS
801 relocation = symbol->value;
802 }
252b5132 803
c6e90b02
TS
804 relocation += symbol->section->output_section->vma;
805 relocation += symbol->section->output_offset;
806 relocation += reloc_entry->addend;
9117d219 807
c6e90b02
TS
808 if (reloc_entry->address > input_section->_cooked_size)
809 return bfd_reloc_outofrange;
9117d219 810
c6e90b02
TS
811 /* Save the information, and let LO16 do the actual relocation. */
812 n = (struct mips_hi16 *) bfd_malloc ((bfd_size_type) sizeof *n);
813 if (n == NULL)
814 return bfd_reloc_outofrange;
815 n->addr = (bfd_byte *) data + reloc_entry->address;
816 n->addend = relocation;
817 n->next = mips_hi16_list;
818 mips_hi16_list = n;
252b5132 819
c6e90b02
TS
820 if (output_bfd != (bfd *) NULL)
821 reloc_entry->address += input_section->output_offset;
252b5132 822
c6e90b02 823 return ret;
252b5132
RH
824}
825
c6e90b02
TS
826/* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
827 inplace relocation; this function exists in order to do the
828 R_MIPS_HI16 relocation described above. */
252b5132 829
c6e90b02
TS
830static bfd_reloc_status_type
831mips_elf_lo16_reloc (abfd, reloc_entry, symbol, data, input_section,
832 output_bfd, error_message)
252b5132 833 bfd *abfd;
c6e90b02
TS
834 arelent *reloc_entry;
835 asymbol *symbol;
836 PTR data;
837 asection *input_section;
838 bfd *output_bfd;
839 char **error_message;
252b5132 840{
c6e90b02 841 arelent gp_disp_relent;
252b5132 842
c6e90b02 843 if (mips_hi16_list != NULL)
252b5132 844 {
c6e90b02 845 struct mips_hi16 *l;
252b5132 846
c6e90b02
TS
847 l = mips_hi16_list;
848 while (l != NULL)
849 {
850 unsigned long insn;
851 unsigned long val;
852 unsigned long vallo;
853 struct mips_hi16 *next;
252b5132 854
c6e90b02
TS
855 /* Do the HI16 relocation. Note that we actually don't need
856 to know anything about the LO16 itself, except where to
857 find the low 16 bits of the addend needed by the LO16. */
858 insn = bfd_get_32 (abfd, l->addr);
859 vallo = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
252b5132 860
c6e90b02
TS
861 /* The low order 16 bits are always treated as a signed
862 value. */
863 vallo = ((vallo & 0xffff) ^ 0x8000) - 0x8000;
864 val = ((insn & 0xffff) << 16) + vallo;
865 val += l->addend;
252b5132 866
c6e90b02
TS
867 /* If PC-relative, we need to subtract out the address of the LO
868 half of the HI/LO. (The actual relocation is relative
869 to that instruction.) */
870 if (reloc_entry->howto->pc_relative)
871 val -= reloc_entry->address;
252b5132 872
c6e90b02
TS
873 /* At this point, "val" has the value of the combined HI/LO
874 pair. If the low order 16 bits (which will be used for
875 the LO16 insn) are negative, then we will need an
876 adjustment for the high order 16 bits. */
877 val += 0x8000;
878 val = (val >> 16) & 0xffff;
252b5132 879
c6e90b02
TS
880 insn &= ~ (bfd_vma) 0xffff;
881 insn |= val;
882 bfd_put_32 (abfd, (bfd_vma) insn, l->addr);
8a20f077 883
c6e90b02
TS
884 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
885 {
886 gp_disp_relent = *reloc_entry;
887 reloc_entry = &gp_disp_relent;
888 reloc_entry->addend = l->addend;
889 }
8a20f077 890
c6e90b02
TS
891 next = l->next;
892 free (l);
893 l = next;
894 }
252b5132 895
c6e90b02 896 mips_hi16_list = NULL;
43917054 897 }
c6e90b02 898 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
252b5132 899 {
c6e90b02
TS
900 bfd_reloc_status_type ret;
901 bfd_vma gp, relocation;
252b5132 902
c6e90b02 903 /* FIXME: Does this case ever occur? */
252b5132 904
c6e90b02
TS
905 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
906 if (ret != bfd_reloc_ok)
907 return ret;
252b5132 908
c6e90b02
TS
909 relocation = gp - reloc_entry->address;
910 relocation += symbol->section->output_section->vma;
911 relocation += symbol->section->output_offset;
912 relocation += reloc_entry->addend;
252b5132 913
c6e90b02
TS
914 if (reloc_entry->address > input_section->_cooked_size)
915 return bfd_reloc_outofrange;
252b5132 916
c6e90b02
TS
917 gp_disp_relent = *reloc_entry;
918 reloc_entry = &gp_disp_relent;
919 reloc_entry->addend = relocation - 4;
252b5132
RH
920 }
921
c6e90b02
TS
922 /* Now do the LO16 reloc in the usual way. */
923 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
924 input_section, output_bfd, error_message);
252b5132
RH
925}
926
c6e90b02
TS
927/* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
928 table used for PIC code. If the symbol is an external symbol, the
929 instruction is modified to contain the offset of the appropriate
930 entry in the global offset table. If the symbol is a section
931 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
932 addends are combined to form the real addend against the section
933 symbol; the GOT16 is modified to contain the offset of an entry in
934 the global offset table, and the LO16 is modified to offset it
935 appropriately. Thus an offset larger than 16 bits requires a
936 modified value in the global offset table.
252b5132 937
c6e90b02
TS
938 This implementation suffices for the assembler, but the linker does
939 not yet know how to create global offset tables. */
252b5132 940
c6e90b02
TS
941static bfd_reloc_status_type
942mips_elf_got16_reloc (abfd, reloc_entry, symbol, data, input_section,
943 output_bfd, error_message)
944 bfd *abfd;
945 arelent *reloc_entry;
946 asymbol *symbol;
947 PTR data;
948 asection *input_section;
252b5132 949 bfd *output_bfd;
c6e90b02 950 char **error_message;
252b5132 951{
cb7394f2 952 /* If we're relocating, and this is an external symbol, we don't want
c6e90b02
TS
953 to change anything. */
954 if (output_bfd != (bfd *) NULL
955 && (symbol->flags & BSF_SECTION_SYM) == 0
956 && reloc_entry->addend == 0)
957 {
958 reloc_entry->address += input_section->output_offset;
959 return bfd_reloc_ok;
960 }
252b5132 961
c6e90b02
TS
962 /* If we're relocating, and this is a local symbol, we can handle it
963 just like HI16. */
964 if (output_bfd != (bfd *) NULL
965 && (symbol->flags & BSF_SECTION_SYM) != 0)
966 return mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
967 input_section, output_bfd, error_message);
252b5132 968
c6e90b02 969 abort ();
252b5132
RH
970}
971
c6e90b02
TS
972/* Set the GP value for OUTPUT_BFD. Returns false if this is a
973 dangerous relocation. */
252b5132 974
252b5132 975static boolean
c6e90b02
TS
976mips_elf_assign_gp (output_bfd, pgp)
977 bfd *output_bfd;
978 bfd_vma *pgp;
252b5132 979{
c6e90b02
TS
980 unsigned int count;
981 asymbol **sym;
982 unsigned int i;
e92d460e 983
c6e90b02
TS
984 /* If we've already figured out what GP will be, just return it. */
985 *pgp = _bfd_get_gp_value (output_bfd);
986 if (*pgp)
987 return true;
988
989 count = bfd_get_symcount (output_bfd);
990 sym = bfd_get_outsymbols (output_bfd);
252b5132 991
c6e90b02
TS
992 /* The linker script will have created a symbol named `_gp' with the
993 appropriate value. */
994 if (sym == (asymbol **) NULL)
995 i = count;
996 else
252b5132 997 {
c6e90b02
TS
998 for (i = 0; i < count; i++, sym++)
999 {
1000 register const char *name;
1001
1002 name = bfd_asymbol_name (*sym);
1003 if (*name == '_' && strcmp (name, "_gp") == 0)
1004 {
1005 *pgp = bfd_asymbol_value (*sym);
1006 _bfd_set_gp_value (output_bfd, *pgp);
1007 break;
1008 }
1009 }
252b5132
RH
1010 }
1011
c6e90b02 1012 if (i >= count)
252b5132 1013 {
c6e90b02
TS
1014 /* Only get the error once. */
1015 *pgp = 4;
1016 _bfd_set_gp_value (output_bfd, *pgp);
1017 return false;
252b5132
RH
1018 }
1019
1020 return true;
1021}
1022
c6e90b02
TS
1023/* We have to figure out the gp value, so that we can adjust the
1024 symbol value correctly. We look up the symbol _gp in the output
1025 BFD. If we can't find it, we're stuck. We cache it in the ELF
1026 target data. We don't need to adjust the symbol value for an
1027 external symbol if we are producing relocateable output. */
252b5132 1028
c6e90b02
TS
1029static bfd_reloc_status_type
1030mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
252b5132 1031 bfd *output_bfd;
c6e90b02
TS
1032 asymbol *symbol;
1033 boolean relocateable;
1034 char **error_message;
1035 bfd_vma *pgp;
252b5132 1036{
c6e90b02
TS
1037 if (bfd_is_und_section (symbol->section)
1038 && ! relocateable)
252b5132 1039 {
c6e90b02
TS
1040 *pgp = 0;
1041 return bfd_reloc_undefined;
252b5132
RH
1042 }
1043
c6e90b02
TS
1044 *pgp = _bfd_get_gp_value (output_bfd);
1045 if (*pgp == 0
1046 && (! relocateable
1047 || (symbol->flags & BSF_SECTION_SYM) != 0))
252b5132 1048 {
c6e90b02 1049 if (relocateable)
252b5132 1050 {
c6e90b02
TS
1051 /* Make up a value. */
1052 *pgp = symbol->section->output_section->vma + 0x4000;
1053 _bfd_set_gp_value (output_bfd, *pgp);
252b5132 1054 }
c6e90b02 1055 else if (!mips_elf_assign_gp (output_bfd, pgp))
252b5132 1056 {
c6e90b02
TS
1057 *error_message =
1058 (char *) _("GP relative relocation when _gp not defined");
1059 return bfd_reloc_dangerous;
252b5132 1060 }
252b5132
RH
1061 }
1062
c6e90b02
TS
1063 return bfd_reloc_ok;
1064}
252b5132 1065
c6e90b02
TS
1066/* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1067 become the offset from the gp register. This function also handles
1068 R_MIPS_LITERAL relocations, although those can be handled more
1069 cleverly because the entries in the .lit8 and .lit4 sections can be
1070 merged. */
252b5132 1071
c6e90b02
TS
1072bfd_reloc_status_type
1073_bfd_mips_elf32_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1074 output_bfd, error_message)
1075 bfd *abfd;
1076 arelent *reloc_entry;
1077 asymbol *symbol;
1078 PTR data;
1079 asection *input_section;
1080 bfd *output_bfd;
1081 char **error_message;
1082{
1083 boolean relocateable;
1084 bfd_reloc_status_type ret;
1085 bfd_vma gp;
252b5132 1086
c6e90b02
TS
1087 /* If we're relocating, and this is an external symbol with no
1088 addend, we don't want to change anything. We will only have an
1089 addend if this is a newly created reloc, not read from an ELF
1090 file. */
1091 if (output_bfd != (bfd *) NULL
1092 && (symbol->flags & BSF_SECTION_SYM) == 0
1093 && reloc_entry->addend == 0)
1094 {
1095 reloc_entry->address += input_section->output_offset;
1096 return bfd_reloc_ok;
1097 }
252b5132 1098
c6e90b02
TS
1099 if (output_bfd != (bfd *) NULL)
1100 relocateable = true;
1101 else
1102 {
1103 relocateable = false;
1104 output_bfd = symbol->section->output_section->owner;
1105 }
252b5132 1106
c6e90b02
TS
1107 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1108 &gp);
1109 if (ret != bfd_reloc_ok)
1110 return ret;
252b5132 1111
c6e90b02
TS
1112 return _bfd_mips_elf_gprel16_with_gp (abfd, symbol, reloc_entry,
1113 input_section, relocateable,
1114 data, gp);
1115}
252b5132 1116
cb7394f2
TS
1117/* Do a R_MIPS_GPREL32 relocation. This is a 32 bit value which must
1118 become the offset from the gp register. */
252b5132 1119
c6e90b02
TS
1120static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1121 arelent *, asection *,
1122 boolean, PTR, bfd_vma));
252b5132 1123
c6e90b02
TS
1124static bfd_reloc_status_type
1125mips_elf_gprel32_reloc (abfd, reloc_entry, symbol, data, input_section,
1126 output_bfd, error_message)
1127 bfd *abfd;
1128 arelent *reloc_entry;
1129 asymbol *symbol;
1130 PTR data;
1131 asection *input_section;
1132 bfd *output_bfd;
1133 char **error_message;
1134{
1135 boolean relocateable;
1136 bfd_reloc_status_type ret;
1137 bfd_vma gp;
252b5132 1138
c6e90b02
TS
1139 /* If we're relocating, and this is an external symbol with no
1140 addend, we don't want to change anything. We will only have an
1141 addend if this is a newly created reloc, not read from an ELF
1142 file. */
1143 if (output_bfd != (bfd *) NULL
1144 && (symbol->flags & BSF_SECTION_SYM) == 0
1145 && reloc_entry->addend == 0)
1146 {
1147 *error_message = (char *)
1148 _("32bits gp relative relocation occurs for an external symbol");
1149 return bfd_reloc_outofrange;
1150 }
252b5132 1151
c6e90b02
TS
1152 if (output_bfd != (bfd *) NULL)
1153 {
1154 relocateable = true;
1155 gp = _bfd_get_gp_value (output_bfd);
1156 }
1157 else
1158 {
1159 relocateable = false;
1160 output_bfd = symbol->section->output_section->owner;
252b5132 1161
c6e90b02
TS
1162 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1163 error_message, &gp);
1164 if (ret != bfd_reloc_ok)
1165 return ret;
1166 }
252b5132 1167
c6e90b02
TS
1168 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1169 relocateable, data, gp);
1170}
252b5132 1171
c6e90b02
TS
1172static bfd_reloc_status_type
1173gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1174 gp)
1175 bfd *abfd;
1176 asymbol *symbol;
1177 arelent *reloc_entry;
1178 asection *input_section;
1179 boolean relocateable;
1180 PTR data;
1181 bfd_vma gp;
1182{
1183 bfd_vma relocation;
1184 unsigned long val;
252b5132 1185
c6e90b02
TS
1186 if (bfd_is_com_section (symbol->section))
1187 relocation = 0;
1188 else
1189 relocation = symbol->value;
252b5132 1190
c6e90b02
TS
1191 relocation += symbol->section->output_section->vma;
1192 relocation += symbol->section->output_offset;
252b5132 1193
c6e90b02
TS
1194 if (reloc_entry->address > input_section->_cooked_size)
1195 return bfd_reloc_outofrange;
252b5132 1196
c6e90b02
TS
1197 if (reloc_entry->howto->src_mask == 0)
1198 {
1199 /* This case arises with the 64-bit MIPS ELF ABI. */
1200 val = 0;
1201 }
1202 else
1203 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
252b5132 1204
c6e90b02
TS
1205 /* Set val to the offset into the section or symbol. */
1206 val += reloc_entry->addend;
252b5132 1207
c6e90b02
TS
1208 /* Adjust val for the final section location and GP value. If we
1209 are producing relocateable output, we don't want to do this for
1210 an external symbol. */
1211 if (! relocateable
1212 || (symbol->flags & BSF_SECTION_SYM) != 0)
1213 val += relocation - gp;
252b5132 1214
c6e90b02 1215 bfd_put_32 (abfd, (bfd_vma) val, (bfd_byte *) data + reloc_entry->address);
c6142e5d 1216
c6e90b02
TS
1217 if (relocateable)
1218 reloc_entry->address += input_section->output_offset;
252b5132 1219
c6e90b02 1220 return bfd_reloc_ok;
252b5132
RH
1221}
1222
c6e90b02
TS
1223/* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
1224 generated when addresses are 64 bits. The upper 32 bits are a simple
1225 sign extension. */
7403cb63 1226
c6e90b02
TS
1227static bfd_reloc_status_type
1228mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
1229 output_bfd, error_message)
1230 bfd *abfd;
1231 arelent *reloc_entry;
1232 asymbol *symbol;
1233 PTR data;
1234 asection *input_section;
1235 bfd *output_bfd;
1236 char **error_message;
7403cb63 1237{
c6e90b02
TS
1238 bfd_reloc_status_type r;
1239 arelent reloc32;
1240 unsigned long val;
1241 bfd_size_type addr;
7403cb63 1242
c6e90b02
TS
1243 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1244 input_section, output_bfd, error_message);
1245 if (r != bfd_reloc_continue)
1246 return r;
be3ccd9c 1247
c6e90b02
TS
1248 /* Do a normal 32 bit relocation on the lower 32 bits. */
1249 reloc32 = *reloc_entry;
1250 if (bfd_big_endian (abfd))
1251 reloc32.address += 4;
1252 reloc32.howto = &elf_mips_howto_table_rel[R_MIPS_32];
1253 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1254 output_bfd, error_message);
be3ccd9c 1255
c6e90b02
TS
1256 /* Sign extend into the upper 32 bits. */
1257 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1258 if ((val & 0x80000000) != 0)
1259 val = 0xffffffff;
1260 else
1261 val = 0;
1262 addr = reloc_entry->address;
1263 if (bfd_little_endian (abfd))
1264 addr += 4;
1265 bfd_put_32 (abfd, (bfd_vma) val, (bfd_byte *) data + addr);
1266
1267 return r;
7403cb63
MM
1268}
1269
c6e90b02 1270/* Handle a mips16 jump. */
252b5132 1271
c6e90b02
TS
1272static bfd_reloc_status_type
1273mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section,
1274 output_bfd, error_message)
1275 bfd *abfd ATTRIBUTE_UNUSED;
1276 arelent *reloc_entry;
1277 asymbol *symbol;
1278 PTR data ATTRIBUTE_UNUSED;
1279 asection *input_section;
252b5132 1280 bfd *output_bfd;
c6e90b02 1281 char **error_message ATTRIBUTE_UNUSED;
252b5132 1282{
c6e90b02
TS
1283 if (output_bfd != (bfd *) NULL
1284 && (symbol->flags & BSF_SECTION_SYM) == 0
1285 && reloc_entry->addend == 0)
252b5132 1286 {
c6e90b02
TS
1287 reloc_entry->address += input_section->output_offset;
1288 return bfd_reloc_ok;
1289 }
252b5132 1290
c6e90b02
TS
1291 /* FIXME. */
1292 {
1293 static boolean warned;
252b5132 1294
c6e90b02
TS
1295 if (! warned)
1296 (*_bfd_error_handler)
1297 (_("Linking mips16 objects into %s format is not supported"),
1298 bfd_get_target (input_section->output_section->owner));
1299 warned = true;
1300 }
252b5132 1301
c6e90b02
TS
1302 return bfd_reloc_undefined;
1303}
252b5132 1304
c6e90b02 1305/* Handle a mips16 GP relative reloc. */
252b5132 1306
c6e90b02
TS
1307static bfd_reloc_status_type
1308mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
1309 output_bfd, error_message)
1310 bfd *abfd;
1311 arelent *reloc_entry;
1312 asymbol *symbol;
1313 PTR data;
1314 asection *input_section;
1315 bfd *output_bfd;
1316 char **error_message;
1317{
1318 boolean relocateable;
1319 bfd_reloc_status_type ret;
1320 bfd_vma gp;
1321 unsigned short extend, insn;
1322 unsigned long final;
252b5132 1323
c6e90b02
TS
1324 /* If we're relocating, and this is an external symbol with no
1325 addend, we don't want to change anything. We will only have an
1326 addend if this is a newly created reloc, not read from an ELF
1327 file. */
1328 if (output_bfd != NULL
1329 && (symbol->flags & BSF_SECTION_SYM) == 0
1330 && reloc_entry->addend == 0)
1331 {
1332 reloc_entry->address += input_section->output_offset;
1333 return bfd_reloc_ok;
1334 }
252b5132 1335
c6e90b02
TS
1336 if (output_bfd != NULL)
1337 relocateable = true;
1338 else
1339 {
1340 relocateable = false;
1341 output_bfd = symbol->section->output_section->owner;
252b5132
RH
1342 }
1343
c6e90b02
TS
1344 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1345 &gp);
1346 if (ret != bfd_reloc_ok)
1347 return ret;
252b5132 1348
c6e90b02
TS
1349 if (reloc_entry->address > input_section->_cooked_size)
1350 return bfd_reloc_outofrange;
252b5132 1351
c6e90b02
TS
1352 /* Pick up the mips16 extend instruction and the real instruction. */
1353 extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
1354 insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
252b5132 1355
c6e90b02
TS
1356 /* Stuff the current addend back as a 32 bit value, do the usual
1357 relocation, and then clean up. */
1358 bfd_put_32 (abfd,
1359 (bfd_vma) (((extend & 0x1f) << 11)
1360 | (extend & 0x7e0)
1361 | (insn & 0x1f)),
1362 (bfd_byte *) data + reloc_entry->address);
252b5132 1363
c6e90b02
TS
1364 ret = _bfd_mips_elf_gprel16_with_gp (abfd, symbol, reloc_entry,
1365 input_section, relocateable, data, gp);
c6142e5d 1366
c6e90b02
TS
1367 final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1368 bfd_put_16 (abfd,
1369 (bfd_vma) ((extend & 0xf800)
1370 | ((final >> 11) & 0x1f)
1371 | (final & 0x7e0)),
1372 (bfd_byte *) data + reloc_entry->address);
1373 bfd_put_16 (abfd,
1374 (bfd_vma) ((insn & 0xffe0)
1375 | (final & 0x1f)),
1376 (bfd_byte *) data + reloc_entry->address + 2);
252b5132 1377
c6e90b02
TS
1378 return ret;
1379}
7403cb63 1380
c6e90b02 1381/* A mapping from BFD reloc types to MIPS ELF reloc types. */
252b5132 1382
c6e90b02 1383struct elf_reloc_map {
cb7394f2
TS
1384 bfd_reloc_code_real_type bfd_val;
1385 enum elf_mips_reloc_type elf_val;
c6e90b02 1386};
252b5132 1387
c6e90b02
TS
1388static const struct elf_reloc_map mips_reloc_map[] =
1389{
1390 { BFD_RELOC_NONE, R_MIPS_NONE, },
1391 { BFD_RELOC_16, R_MIPS_16 },
1392 { BFD_RELOC_32, R_MIPS_32 },
cb7394f2 1393 /* There is no BFD reloc for R_MIPS_REL32. */
c6e90b02
TS
1394 { BFD_RELOC_64, R_MIPS_64 },
1395 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1396 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1397 { BFD_RELOC_LO16, R_MIPS_LO16 },
1398 { BFD_RELOC_GPREL16, R_MIPS_GPREL16 },
1399 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1400 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1401 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1402 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1403 { BFD_RELOC_GPREL32, R_MIPS_GPREL32 },
1404 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1405 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1406 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1407 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 },
1408 { BFD_RELOC_MIPS_SUB, R_MIPS_SUB },
1409 { BFD_RELOC_MIPS_GOT_PAGE, R_MIPS_GOT_PAGE },
1410 { BFD_RELOC_MIPS_GOT_OFST, R_MIPS_GOT_OFST },
1411 { BFD_RELOC_MIPS_GOT_DISP, R_MIPS_GOT_DISP }
1412};
252b5132 1413
c6e90b02 1414/* Given a BFD reloc type, return a howto structure. */
252b5132 1415
c6e90b02
TS
1416static reloc_howto_type *
1417bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1418 bfd *abfd;
1419 bfd_reloc_code_real_type code;
252b5132 1420{
c6e90b02 1421 unsigned int i;
cb7394f2 1422 reloc_howto_type *howto_table = elf_mips_howto_table_rel;
252b5132 1423
cb7394f2
TS
1424 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map);
1425 i++)
252b5132 1426 {
cb7394f2
TS
1427 if (mips_reloc_map[i].bfd_val == code)
1428 return &howto_table[(int) mips_reloc_map[i].elf_val];
252b5132
RH
1429 }
1430
c6e90b02 1431 switch (code)
252b5132 1432 {
c6e90b02
TS
1433 default:
1434 bfd_set_error (bfd_error_bad_value);
1435 return NULL;
252b5132 1436
c6e90b02
TS
1437 case BFD_RELOC_CTOR:
1438 /* We need to handle BFD_RELOC_CTOR specially.
1439 Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the
1440 size of addresses on this architecture. */
1441 if (bfd_arch_bits_per_address (abfd) == 32)
cb7394f2 1442 return &howto_table[(int) R_MIPS_32];
c6e90b02
TS
1443 else
1444 return &elf_mips_ctor64_howto;
252b5132 1445
c6e90b02
TS
1446 case BFD_RELOC_MIPS16_JMP:
1447 return &elf_mips16_jump_howto;
1448 case BFD_RELOC_MIPS16_GPREL:
1449 return &elf_mips16_gprel_howto;
1450 case BFD_RELOC_VTABLE_INHERIT:
1451 return &elf_mips_gnu_vtinherit_howto;
1452 case BFD_RELOC_VTABLE_ENTRY:
1453 return &elf_mips_gnu_vtentry_howto;
1454 case BFD_RELOC_PCREL_HI16_S:
1455 return &elf_mips_gnu_rel_hi16;
1456 case BFD_RELOC_PCREL_LO16:
1457 return &elf_mips_gnu_rel_lo16;
1458 case BFD_RELOC_16_PCREL_S2:
1459 return &elf_mips_gnu_rel16_s2;
1460 case BFD_RELOC_64_PCREL:
1461 return &elf_mips_gnu_pcrel64;
1462 case BFD_RELOC_32_PCREL:
1463 return &elf_mips_gnu_pcrel32;
1464 }
1465}
252b5132 1466
c6e90b02 1467/* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
252b5132 1468
c6e90b02
TS
1469static reloc_howto_type *
1470mips_elf32_rtype_to_howto (r_type, rela_p)
1471 unsigned int r_type;
1472 boolean rela_p ATTRIBUTE_UNUSED;
1473{
1474 switch (r_type)
1475 {
1476 case R_MIPS16_26:
1477 return &elf_mips16_jump_howto;
c6e90b02
TS
1478 case R_MIPS16_GPREL:
1479 return &elf_mips16_gprel_howto;
c6e90b02
TS
1480 case R_MIPS_GNU_VTINHERIT:
1481 return &elf_mips_gnu_vtinherit_howto;
c6e90b02
TS
1482 case R_MIPS_GNU_VTENTRY:
1483 return &elf_mips_gnu_vtentry_howto;
c6e90b02
TS
1484 case R_MIPS_GNU_REL_HI16:
1485 return &elf_mips_gnu_rel_hi16;
c6e90b02
TS
1486 case R_MIPS_GNU_REL_LO16:
1487 return &elf_mips_gnu_rel_lo16;
c6e90b02
TS
1488 case R_MIPS_GNU_REL16_S2:
1489 return &elf_mips_gnu_rel16_s2;
c6e90b02
TS
1490 case R_MIPS_PC64:
1491 return &elf_mips_gnu_pcrel64;
c6e90b02
TS
1492 case R_MIPS_PC32:
1493 return &elf_mips_gnu_pcrel32;
c6e90b02
TS
1494 default:
1495 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1496 return &elf_mips_howto_table_rel[r_type];
1497 }
1498}
252b5132 1499
c6e90b02 1500/* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
252b5132 1501
c6e90b02
TS
1502static void
1503mips_info_to_howto_rel (abfd, cache_ptr, dst)
1504 bfd *abfd;
1505 arelent *cache_ptr;
1506 Elf32_Internal_Rel *dst;
1507{
1508 unsigned int r_type;
252b5132 1509
c6e90b02
TS
1510 r_type = ELF32_R_TYPE (dst->r_info);
1511 cache_ptr->howto = mips_elf32_rtype_to_howto (r_type, false);
252b5132 1512
c6e90b02
TS
1513 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1514 value for the object file. We get the addend now, rather than
1515 when we do the relocation, because the symbol manipulations done
1516 by the linker may cause us to lose track of the input BFD. */
1517 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1518 && (r_type == (unsigned int) R_MIPS_GPREL16
1519 || r_type == (unsigned int) R_MIPS_LITERAL))
1520 cache_ptr->addend = elf_gp (abfd);
1521}
5499724a 1522
c6e90b02 1523/* Given a MIPS Elf32_Internal_Rela, fill in an arelent structure. */
252b5132 1524
c6e90b02
TS
1525static void
1526mips_info_to_howto_rela (abfd, cache_ptr, dst)
1527 bfd *abfd;
1528 arelent *cache_ptr;
1529 Elf32_Internal_Rela *dst;
1530{
1531 /* Since an Elf32_Internal_Rel is an initial prefix of an
1532 Elf32_Internal_Rela, we can just use mips_info_to_howto_rel
1533 above. */
1534 mips_info_to_howto_rel (abfd, cache_ptr, (Elf32_Internal_Rel *) dst);
252b5132 1535
c6e90b02
TS
1536 /* If we ever need to do any extra processing with dst->r_addend
1537 (the field omitted in an Elf32_Internal_Rel) we can do it here. */
1538}
1539\f
1540/* Determine whether a symbol is global for the purposes of splitting
1541 the symbol table into global symbols and local symbols. At least
1542 on Irix 5, this split must be between section symbols and all other
1543 symbols. On most ELF targets the split is between static symbols
1544 and externally visible symbols. */
252b5132 1545
c6e90b02
TS
1546static boolean
1547mips_elf_sym_is_global (abfd, sym)
1548 bfd *abfd ATTRIBUTE_UNUSED;
1549 asymbol *sym;
1550{
1551 if (SGI_COMPAT (abfd))
1552 return (sym->flags & BSF_SECTION_SYM) == 0;
1553 else
1554 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1555 || bfd_is_und_section (bfd_get_section (sym))
1556 || bfd_is_com_section (bfd_get_section (sym)));
1557}
1558\f
1559/* Set the right machine number for a MIPS ELF file. */
7403cb63 1560
c6e90b02
TS
1561static boolean
1562mips_elf32_object_p (abfd)
1563 bfd *abfd;
1564{
1565 unsigned long mach;
103186c6 1566
c6e90b02
TS
1567 /* Irix 5 and 6 are broken. Object file symbol tables are not always
1568 sorted correctly such that local symbols precede global symbols,
1569 and the sh_info field in the symbol table is not always right. */
1570 if (SGI_COMPAT (abfd))
1571 elf_bad_symtab (abfd) = true;
103186c6 1572
c6e90b02
TS
1573 mach = _bfd_elf_mips_mach (elf_elfheader (abfd)->e_flags);
1574 bfd_default_set_arch_mach (abfd, bfd_arch_mips, mach);
252b5132 1575
c6e90b02
TS
1576 return true;
1577}
1578\f
1579/* MIPS ELF local labels start with '$', not 'L'. */
252b5132 1580
c6e90b02
TS
1581static boolean
1582mips_elf_is_local_label_name (abfd, name)
1583 bfd *abfd;
1584 const char *name;
1585{
1586 if (name[0] == '$')
1587 return true;
252b5132 1588
c6e90b02
TS
1589 /* On Irix 6, the labels go back to starting with '.', so we accept
1590 the generic ELF local label syntax as well. */
1591 return _bfd_elf_is_local_label_name (abfd, name);
252b5132
RH
1592}
1593\f
c6e90b02 1594/* Support for core dump NOTE sections. */
bb0082d6 1595static boolean
c6e90b02 1596elf32_mips_grok_prstatus (abfd, note)
bb0082d6
AM
1597 bfd *abfd;
1598 Elf_Internal_Note *note;
1599{
1600 int offset;
dc810e39 1601 unsigned int raw_size;
bb0082d6
AM
1602
1603 switch (note->descsz)
1604 {
1605 default:
1606 return false;
1607
1608 case 256: /* Linux/MIPS */
1609 /* pr_cursig */
1610 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1611
1612 /* pr_pid */
1613 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1614
1615 /* pr_reg */
1616 offset = 72;
1617 raw_size = 180;
1618
1619 break;
1620 }
1621
1622 /* Make a ".reg/999" section. */
936e320b
AM
1623 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1624 raw_size, note->descpos + offset);
bb0082d6
AM
1625}
1626
9e80ff3a 1627static boolean
c6e90b02 1628elf32_mips_grok_psinfo (abfd, note)
bb0082d6
AM
1629 bfd *abfd;
1630 Elf_Internal_Note *note;
1631{
1632 switch (note->descsz)
1633 {
1634 default:
1635 return false;
1636
1637 case 128: /* Linux/MIPS elf_prpsinfo */
1638 elf_tdata (abfd)->core_program
1639 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
1640 elf_tdata (abfd)->core_command
1641 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
1642 }
1643
1644 /* Note that for some reason, a spurious space is tacked
1645 onto the end of the args in some (at least one anyway)
1646 implementations, so strip it off if it exists. */
1647
1648 {
1649 char *command = elf_tdata (abfd)->core_command;
1650 int n = strlen (command);
1651
1652 if (0 < n && command[n - 1] == ' ')
1653 command[n - 1] = '\0';
1654 }
1655
1656 return true;
1657}
1658\f
c6e90b02
TS
1659/* Depending on the target vector we generate some version of Irix
1660 executables or "normal" MIPS ELF ABI executables. */
1661static irix_compat_t
1662elf32_mips_irix_compat (abfd)
1663 bfd *abfd;
1664{
cb7394f2
TS
1665 if ((abfd->xvec == &bfd_elf32_bigmips_vec)
1666 || (abfd->xvec == &bfd_elf32_littlemips_vec))
c6e90b02 1667 return ict_irix5;
cb7394f2
TS
1668 else
1669 return ict_none;
c6e90b02 1670}
73d074b4 1671\f
030d18fb
CD
1672/* Given a data section and an in-memory embedded reloc section, store
1673 relocation information into the embedded reloc section which can be
1674 used at runtime to relocate the data section. This is called by the
1675 linker when the --embedded-relocs switch is used. This is called
1676 after the add_symbols entry point has been called for all the
1677 objects, and before the final_link entry point is called. */
1678
1679boolean
1680bfd_mips_elf32_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
1681 bfd *abfd;
1682 struct bfd_link_info *info;
1683 asection *datasec;
1684 asection *relsec;
1685 char **errmsg;
1686{
1687 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc
AM
1688 Elf_Internal_Sym *isymbuf = NULL;
1689 Elf_Internal_Rela *internal_relocs = NULL;
030d18fb
CD
1690 Elf_Internal_Rela *irel, *irelend;
1691 bfd_byte *p;
030d18fb
CD
1692
1693 BFD_ASSERT (! info->relocateable);
1694
1695 *errmsg = NULL;
1696
1697 if (datasec->reloc_count == 0)
1698 return true;
1699
030d18fb
CD
1700 /* Read this BFD's symbols if we haven't done so already, or get the cached
1701 copy if it exists. */
6cdc0ccc
AM
1702 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1703 if (symtab_hdr->sh_info != 0)
030d18fb 1704 {
6cdc0ccc
AM
1705 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1706 if (isymbuf == NULL)
1707 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1708 symtab_hdr->sh_info, 0,
1709 NULL, NULL, NULL);
1710 if (isymbuf == NULL)
030d18fb
CD
1711 goto error_return;
1712 }
1713
1714 /* Get a copy of the native relocations. */
1715 internal_relocs = (_bfd_elf32_link_read_relocs
1716 (abfd, datasec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1717 info->keep_memory));
1718 if (internal_relocs == NULL)
1719 goto error_return;
030d18fb
CD
1720
1721 relsec->contents = (bfd_byte *) bfd_alloc (abfd, datasec->reloc_count * 12);
1722 if (relsec->contents == NULL)
1723 goto error_return;
1724
1725 p = relsec->contents;
1726
1727 irelend = internal_relocs + datasec->reloc_count;
1728
1729 for (irel = internal_relocs; irel < irelend; irel++, p += 12)
1730 {
1731 asection *targetsec;
1732
1733 /* We are going to write a four byte longword into the runtime
c6e90b02
TS
1734 reloc section. The longword will be the address in the data
1735 section which must be relocated. It is followed by the name
1736 of the target section NUL-padded or truncated to 8
1737 characters. */
030d18fb
CD
1738
1739 /* We can only relocate absolute longword relocs at run time. */
1740 if ((ELF32_R_TYPE (irel->r_info) != (int) R_MIPS_32) &&
1741 (ELF32_R_TYPE (irel->r_info) != (int) R_MIPS_64))
1742 {
1743 *errmsg = _("unsupported reloc type");
1744 bfd_set_error (bfd_error_bad_value);
1745 goto error_return;
1746 }
1747 /* Get the target section referred to by the reloc. */
1748 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1749 {
6cdc0ccc 1750 Elf_Internal_Sym *isym;
030d18fb
CD
1751
1752 /* A local symbol. */
6cdc0ccc
AM
1753 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1754 targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
030d18fb
CD
1755 }
1756 else
1757 {
1758 unsigned long indx;
1759 struct elf_link_hash_entry *h;
1760
1761 /* An external symbol. */
1762 indx = ELF32_R_SYM (irel->r_info);
1763 h = elf_sym_hashes (abfd)[indx];
1764 targetsec = NULL;
1765 /*
c6e90b02
TS
1766 For some reason, in certain programs, the symbol will
1767 not be in the hash table. It seems to happen when you
1768 declare a static table of pointers to const external structures.
1769 In this case, the relocs are relative to data, not
1770 text, so just treating it like an undefined link
1771 should be sufficient. */
030d18fb
CD
1772 BFD_ASSERT(h != NULL);
1773 if (h->root.type == bfd_link_hash_defined
1774 || h->root.type == bfd_link_hash_defweak)
1775 targetsec = h->root.u.def.section;
1776 }
1777
1778
1779 /*
c6e90b02
TS
1780 Set the low bit of the relocation offset if it's a MIPS64 reloc.
1781 Relocations will always be on (at least) 32-bit boundaries. */
030d18fb
CD
1782
1783 bfd_put_32 (abfd, ((irel->r_offset + datasec->output_offset) +
1784 ((ELF32_R_TYPE (irel->r_info) == (int) R_MIPS_64) ? 1 : 0)),
1785 p);
1786 memset (p + 4, 0, 8);
1787 if (targetsec != NULL)
1788 strncpy (p + 4, targetsec->output_section->name, 8);
1789 }
1790
6cdc0ccc
AM
1791 if (internal_relocs != NULL
1792 && elf_section_data (datasec)->relocs != internal_relocs)
1793 free (internal_relocs);
1794 if (isymbuf != NULL
1795 && symtab_hdr->contents != (unsigned char *) isymbuf)
1796 free (isymbuf);
030d18fb
CD
1797 return true;
1798
1799 error_return:
6cdc0ccc
AM
1800 if (internal_relocs != NULL
1801 && elf_section_data (datasec)->relocs != internal_relocs)
1802 free (internal_relocs);
1803 if (isymbuf != NULL
1804 && symtab_hdr->contents != (unsigned char *) isymbuf)
1805 free (isymbuf);
030d18fb
CD
1806 return false;
1807}
1808\f
252b5132
RH
1809/* ECOFF swapping routines. These are used when dealing with the
1810 .mdebug section, which is in the ECOFF debugging format. */
be3ccd9c 1811static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap = {
252b5132
RH
1812 /* Symbol table magic number. */
1813 magicSym,
1814 /* Alignment of debugging information. E.g., 4. */
1815 4,
1816 /* Sizes of external symbolic information. */
1817 sizeof (struct hdr_ext),
1818 sizeof (struct dnr_ext),
1819 sizeof (struct pdr_ext),
1820 sizeof (struct sym_ext),
1821 sizeof (struct opt_ext),
1822 sizeof (struct fdr_ext),
1823 sizeof (struct rfd_ext),
1824 sizeof (struct ext_ext),
1825 /* Functions to swap in external symbolic data. */
1826 ecoff_swap_hdr_in,
1827 ecoff_swap_dnr_in,
1828 ecoff_swap_pdr_in,
1829 ecoff_swap_sym_in,
1830 ecoff_swap_opt_in,
1831 ecoff_swap_fdr_in,
1832 ecoff_swap_rfd_in,
1833 ecoff_swap_ext_in,
1834 _bfd_ecoff_swap_tir_in,
1835 _bfd_ecoff_swap_rndx_in,
1836 /* Functions to swap out external symbolic data. */
1837 ecoff_swap_hdr_out,
1838 ecoff_swap_dnr_out,
1839 ecoff_swap_pdr_out,
1840 ecoff_swap_sym_out,
1841 ecoff_swap_opt_out,
1842 ecoff_swap_fdr_out,
1843 ecoff_swap_rfd_out,
1844 ecoff_swap_ext_out,
1845 _bfd_ecoff_swap_tir_out,
1846 _bfd_ecoff_swap_rndx_out,
1847 /* Function to read in symbolic data. */
1848 _bfd_mips_elf_read_ecoff_info
1849};
1850\f
252b5132
RH
1851#define ELF_ARCH bfd_arch_mips
1852#define ELF_MACHINE_CODE EM_MIPS
1853
1854/* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
1855 a value of 0x1000, and we are compatible. */
1856#define ELF_MAXPAGESIZE 0x1000
1857
1858#define elf_backend_collect true
1859#define elf_backend_type_change_ok true
1860#define elf_backend_can_gc_sections true
3f830999 1861#define elf_info_to_howto mips_info_to_howto_rela
252b5132
RH
1862#define elf_info_to_howto_rel mips_info_to_howto_rel
1863#define elf_backend_sym_is_global mips_elf_sym_is_global
c6e90b02 1864#define elf_backend_object_p mips_elf32_object_p
d75bc93d
TS
1865#define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
1866#define elf_backend_section_processing _bfd_mips_elf_section_processing
103186c6 1867#define elf_backend_section_from_shdr _bfd_mips_elf_section_from_shdr
252b5132
RH
1868#define elf_backend_fake_sections _bfd_mips_elf_fake_sections
1869#define elf_backend_section_from_bfd_section \
1870 _bfd_mips_elf_section_from_bfd_section
103186c6 1871#define elf_backend_add_symbol_hook _bfd_mips_elf_add_symbol_hook
d75bc93d
TS
1872#define elf_backend_link_output_symbol_hook \
1873 _bfd_mips_elf_link_output_symbol_hook
103186c6
MM
1874#define elf_backend_create_dynamic_sections \
1875 _bfd_mips_elf_create_dynamic_sections
1876#define elf_backend_check_relocs _bfd_mips_elf_check_relocs
1877#define elf_backend_adjust_dynamic_symbol \
1878 _bfd_mips_elf_adjust_dynamic_symbol
1879#define elf_backend_always_size_sections \
1880 _bfd_mips_elf_always_size_sections
1881#define elf_backend_size_dynamic_sections \
1882 _bfd_mips_elf_size_dynamic_sections
1883#define elf_backend_relocate_section _bfd_mips_elf_relocate_section
103186c6
MM
1884#define elf_backend_finish_dynamic_symbol \
1885 _bfd_mips_elf_finish_dynamic_symbol
1886#define elf_backend_finish_dynamic_sections \
1887 _bfd_mips_elf_finish_dynamic_sections
d75bc93d
TS
1888#define elf_backend_final_write_processing \
1889 _bfd_mips_elf_final_write_processing
1890#define elf_backend_additional_program_headers \
1891 _bfd_mips_elf_additional_program_headers
1892#define elf_backend_modify_segment_map _bfd_mips_elf_modify_segment_map
103186c6
MM
1893#define elf_backend_gc_mark_hook _bfd_mips_elf_gc_mark_hook
1894#define elf_backend_gc_sweep_hook _bfd_mips_elf_gc_sweep_hook
8a20f077
UC
1895#define elf_backend_copy_indirect_symbol \
1896 _bfd_mips_elf_copy_indirect_symbol
b305ef96 1897#define elf_backend_hide_symbol _bfd_mips_elf_hide_symbol
c6e90b02
TS
1898#define elf_backend_grok_prstatus elf32_mips_grok_prstatus
1899#define elf_backend_grok_psinfo elf32_mips_grok_psinfo
d75bc93d
TS
1900#define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
1901
1902#define elf_backend_got_header_size (4 * MIPS_RESERVED_GOTNO)
1903#define elf_backend_plt_header_size 0
1904#define elf_backend_may_use_rel_p 1
1905#define elf_backend_may_use_rela_p 0
1906#define elf_backend_default_use_rela_p 0
1907#define elf_backend_sign_extend_vma true
b305ef96 1908
d01414a5 1909#define elf_backend_discard_info _bfd_mips_elf_discard_info
73d074b4 1910#define elf_backend_ignore_discarded_relocs \
53bfd6b4 1911 _bfd_mips_elf_ignore_discarded_relocs
c6e90b02
TS
1912#define elf_backend_mips_irix_compat elf32_mips_irix_compat
1913#define elf_backend_mips_rtype_to_howto mips_elf32_rtype_to_howto
252b5132
RH
1914#define bfd_elf32_bfd_is_local_label_name \
1915 mips_elf_is_local_label_name
1916#define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
1917#define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
c6e90b02
TS
1918#define bfd_elf32_bfd_get_relocated_section_contents \
1919 _bfd_elf_mips_get_relocated_section_contents
252b5132 1920#define bfd_elf32_bfd_link_hash_table_create \
103186c6
MM
1921 _bfd_mips_elf_link_hash_table_create
1922#define bfd_elf32_bfd_final_link _bfd_mips_elf_final_link
252b5132
RH
1923#define bfd_elf32_bfd_merge_private_bfd_data \
1924 _bfd_mips_elf_merge_private_bfd_data
1925#define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
1926#define bfd_elf32_bfd_print_private_bfd_data \
1927 _bfd_mips_elf_print_private_bfd_data
e364195d 1928
d75bc93d
TS
1929/* Support for SGI-ish mips targets. */
1930#define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
1931#define TARGET_LITTLE_NAME "elf32-littlemips"
1932#define TARGET_BIG_SYM bfd_elf32_bigmips_vec
1933#define TARGET_BIG_NAME "elf32-bigmips"
1934
1935#include "elf32-target.h"
e364195d 1936
d75bc93d
TS
1937/* Support for traditional mips targets. */
1938#define INCLUDED_TARGET_FILE /* More a type of flag. */
e364195d
UC
1939
1940#undef TARGET_LITTLE_SYM
1941#undef TARGET_LITTLE_NAME
1942#undef TARGET_BIG_SYM
1943#undef TARGET_BIG_NAME
1944
1945#define TARGET_LITTLE_SYM bfd_elf32_tradlittlemips_vec
1946#define TARGET_LITTLE_NAME "elf32-tradlittlemips"
1947#define TARGET_BIG_SYM bfd_elf32_tradbigmips_vec
1948#define TARGET_BIG_NAME "elf32-tradbigmips"
1949
c6e90b02 1950/* Include the target file again for this target. */
e364195d 1951#include "elf32-target.h"
This page took 0.356447 seconds and 4 git commands to generate.