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