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