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