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