* reloc.c (BFD_RELOC_MIPS_SUB): New relocation.
[deliverable/binutils-gdb.git] / bfd / elf32-mips.c
CommitLineData
252b5132
RH
1/* MIPS-specific support for 32-bit ELF
2 Copyright 1993, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
3
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
5 <ian@cygnus.com>.
6
7This file is part of BFD, the Binary File Descriptor library.
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22
23/* This file handles MIPS ELF targets. SGI Irix 5 uses a slightly
24 different MIPS ELF from other targets. This matters when linking.
25 This file supports both, switching at runtime. */
26
27#include "bfd.h"
28#include "sysdep.h"
29#include "libbfd.h"
30#include "bfdlink.h"
31#include "genlink.h"
32#include "elf-bfd.h"
33#include "elf/mips.h"
34
35/* Get the ECOFF swapping routines. */
36#include "coff/sym.h"
37#include "coff/symconst.h"
38#include "coff/internal.h"
39#include "coff/ecoff.h"
40#include "coff/mips.h"
41#define ECOFF_32
42#include "ecoffswap.h"
43
44static bfd_reloc_status_type mips32_64bit_reloc
45 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
46static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
47 PARAMS ((bfd *, bfd_reloc_code_real_type));
48static void mips_info_to_howto_rel
49 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
3f830999
MM
50static void mips_info_to_howto_rela
51 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
252b5132
RH
52static void bfd_mips_elf32_swap_gptab_in
53 PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
54static void bfd_mips_elf32_swap_gptab_out
55 PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
56static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
57static boolean mips_elf32_object_p PARAMS ((bfd *));
58static boolean mips_elf_create_procedure_table
59 PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
60 struct ecoff_debug_info *));
61static int mips_elf_additional_program_headers PARAMS ((bfd *));
62static boolean mips_elf_modify_segment_map PARAMS ((bfd *));
63static INLINE int elf_mips_isa PARAMS ((flagword));
64static INLINE int elf_mips_mach PARAMS ((flagword));
65static INLINE char* elf_mips_abi_name PARAMS ((flagword));
66static boolean mips_elf32_section_from_shdr
67 PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
68static boolean mips_elf32_section_processing
69 PARAMS ((bfd *, Elf32_Internal_Shdr *));
70static boolean mips_elf_is_local_label_name
71 PARAMS ((bfd *, const char *));
72static struct bfd_hash_entry *mips_elf_link_hash_newfunc
73 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
74static struct bfd_link_hash_table *mips_elf_link_hash_table_create
75 PARAMS ((bfd *));
76static int gptab_compare PARAMS ((const void *, const void *));
77static boolean mips_elf_final_link
78 PARAMS ((bfd *, struct bfd_link_info *));
79static void mips_elf_relocate_hi16
80 PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
81 bfd_vma));
82static boolean mips_elf_relocate_got_local
83 PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
84 Elf_Internal_Rela *, bfd_byte *, bfd_vma));
85static void mips_elf_relocate_global_got
86 PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
87static bfd_reloc_status_type mips16_jump_reloc
88 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
89static bfd_reloc_status_type mips16_gprel_reloc
90 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
91static boolean mips_elf_adjust_dynindx
92 PARAMS ((struct elf_link_hash_entry *, PTR));
93static boolean mips_elf_relocate_section
94 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
95 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
96static boolean mips_elf_link_output_symbol_hook
97 PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *,
98 asection *));
99static boolean mips_elf_create_dynamic_sections
100 PARAMS ((bfd *, struct bfd_link_info *));
101static boolean mips_elf_create_compact_rel_section
102 PARAMS ((bfd *, struct bfd_link_info *));
103static boolean mips_elf_create_got_section
104 PARAMS ((bfd *, struct bfd_link_info *));
105static boolean mips_elf_check_relocs
106 PARAMS ((bfd *, struct bfd_link_info *, asection *,
107 const Elf_Internal_Rela *));
108static boolean mips_elf_adjust_dynamic_symbol
109 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
110static boolean mips_elf_always_size_sections
111 PARAMS ((bfd *, struct bfd_link_info *));
112static boolean mips_elf_size_dynamic_sections
113 PARAMS ((bfd *, struct bfd_link_info *));
114static boolean mips_elf_finish_dynamic_symbol
115 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
116 Elf_Internal_Sym *));
117static boolean mips_elf_finish_dynamic_sections
118 PARAMS ((bfd *, struct bfd_link_info *));
119static boolean mips_elf_add_symbol_hook
120 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
121 const char **, flagword *, asection **, bfd_vma *));
122static bfd_reloc_status_type mips_elf_final_gp
123 PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
124static bfd_byte *elf32_mips_get_relocated_section_contents
125 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
126 bfd_byte *, boolean, asymbol **));
127
128/* This is true for Irix 5 executables, false for normal MIPS ELF ABI
129 executables. FIXME: At the moment, we default to always generating
130 Irix 5 executables. */
131
132#define SGI_COMPAT(abfd) (1)
133
134/* This structure is used to hold .got information when linking. It
135 is stored in the tdata field of the bfd_elf_section_data structure. */
136
137struct mips_got_info
138{
139 /* The symbol index of the first global .got symbol. */
140 unsigned long global_gotsym;
141 /* The number of local .got entries. */
142 unsigned int local_gotno;
143 /* The number of local .got entries we have used. */
144 unsigned int assigned_gotno;
145};
146
147/* The number of local .got entries we reserve. */
148#define MIPS_RESERVED_GOTNO (2)
149
150/* Instructions which appear in a stub. For some reason the stub is
151 slightly different on an SGI system. */
152#define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
153#define STUB_LW(abfd) \
154 (SGI_COMPAT (abfd) \
155 ? 0x8f998010 /* lw t9,0x8010(gp) */ \
156 : 0x8f998000) /* lw t9,0x8000(gp) */
157#define STUB_MOVE 0x03e07825 /* move t7,ra */
158#define STUB_JALR 0x0320f809 /* jal t9 */
159#define STUB_LI16 0x34180000 /* ori t8,zero,0 */
160#define MIPS_FUNCTION_STUB_SIZE (16)
161
162#if 0
163/* We no longer try to identify particular sections for the .dynsym
164 section. When we do, we wind up crashing if there are other random
165 sections with relocations. */
166
167/* Names of sections which appear in the .dynsym section in an Irix 5
168 executable. */
169
170static const char * const mips_elf_dynsym_sec_names[] =
171{
172 ".text",
173 ".init",
174 ".fini",
175 ".data",
176 ".rodata",
177 ".sdata",
178 ".sbss",
179 ".bss",
180 NULL
181};
182
183#define SIZEOF_MIPS_DYNSYM_SECNAMES \
184 (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
185
186/* The number of entries in mips_elf_dynsym_sec_names which go in the
187 text segment. */
188
189#define MIPS_TEXT_DYNSYM_SECNO (3)
190
191#endif /* 0 */
192
193/* The names of the runtime procedure table symbols used on Irix 5. */
194
195static const char * const mips_elf_dynsym_rtproc_names[] =
196{
197 "_procedure_table",
198 "_procedure_string_table",
199 "_procedure_table_size",
200 NULL
201};
202
203/* These structures are used to generate the .compact_rel section on
204 Irix 5. */
205
206typedef struct
207{
208 unsigned long id1; /* Always one? */
209 unsigned long num; /* Number of compact relocation entries. */
210 unsigned long id2; /* Always two? */
211 unsigned long offset; /* The file offset of the first relocation. */
212 unsigned long reserved0; /* Zero? */
213 unsigned long reserved1; /* Zero? */
214} Elf32_compact_rel;
215
216typedef struct
217{
218 bfd_byte id1[4];
219 bfd_byte num[4];
220 bfd_byte id2[4];
221 bfd_byte offset[4];
222 bfd_byte reserved0[4];
223 bfd_byte reserved1[4];
224} Elf32_External_compact_rel;
225
226typedef struct
227{
228 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
229 unsigned int rtype : 4; /* Relocation types. See below. */
230 unsigned int dist2to : 8;
231 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
232 unsigned long konst; /* KONST field. See below. */
233 unsigned long vaddr; /* VADDR to be relocated. */
234} Elf32_crinfo;
235
236typedef struct
237{
238 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
239 unsigned int rtype : 4; /* Relocation types. See below. */
240 unsigned int dist2to : 8;
241 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
242 unsigned long konst; /* KONST field. See below. */
243} Elf32_crinfo2;
244
245typedef struct
246{
247 bfd_byte info[4];
248 bfd_byte konst[4];
249 bfd_byte vaddr[4];
250} Elf32_External_crinfo;
251
252typedef struct
253{
254 bfd_byte info[4];
255 bfd_byte konst[4];
256} Elf32_External_crinfo2;
257
258/* These are the constants used to swap the bitfields in a crinfo. */
259
260#define CRINFO_CTYPE (0x1)
261#define CRINFO_CTYPE_SH (31)
262#define CRINFO_RTYPE (0xf)
263#define CRINFO_RTYPE_SH (27)
264#define CRINFO_DIST2TO (0xff)
265#define CRINFO_DIST2TO_SH (19)
266#define CRINFO_RELVADDR (0x7ffff)
267#define CRINFO_RELVADDR_SH (0)
268
269/* A compact relocation info has long (3 words) or short (2 words)
270 formats. A short format doesn't have VADDR field and relvaddr
271 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
272#define CRF_MIPS_LONG 1
273#define CRF_MIPS_SHORT 0
274
275/* There are 4 types of compact relocation at least. The value KONST
276 has different meaning for each type:
277
278 (type) (konst)
279 CT_MIPS_REL32 Address in data
280 CT_MIPS_WORD Address in word (XXX)
281 CT_MIPS_GPHI_LO GP - vaddr
282 CT_MIPS_JMPAD Address to jump
283 */
284
285#define CRT_MIPS_REL32 0xa
286#define CRT_MIPS_WORD 0xb
287#define CRT_MIPS_GPHI_LO 0xc
288#define CRT_MIPS_JMPAD 0xd
289
290#define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
291#define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
292#define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
293#define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
294
295static void bfd_elf32_swap_compact_rel_out
296 PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
297static void bfd_elf32_swap_crinfo_out
298 PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
299
300#define USE_REL 1 /* MIPS uses REL relocations instead of RELA */
301
3f830999
MM
302/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
303 from smaller values. Start with zero, widen, *then* decrement. */
304#define MINUS_ONE (((bfd_vma)0) - 1)
305
252b5132
RH
306static reloc_howto_type elf_mips_howto_table[] =
307{
308 /* No relocation. */
309 HOWTO (R_MIPS_NONE, /* type */
310 0, /* rightshift */
311 0, /* size (0 = byte, 1 = short, 2 = long) */
312 0, /* bitsize */
313 false, /* pc_relative */
314 0, /* bitpos */
315 complain_overflow_dont, /* complain_on_overflow */
316 bfd_elf_generic_reloc, /* special_function */
317 "R_MIPS_NONE", /* name */
318 false, /* partial_inplace */
319 0, /* src_mask */
320 0, /* dst_mask */
321 false), /* pcrel_offset */
322
323 /* 16 bit relocation. */
324 HOWTO (R_MIPS_16, /* type */
325 0, /* rightshift */
326 1, /* size (0 = byte, 1 = short, 2 = long) */
327 16, /* bitsize */
328 false, /* pc_relative */
329 0, /* bitpos */
330 complain_overflow_bitfield, /* complain_on_overflow */
331 bfd_elf_generic_reloc, /* special_function */
332 "R_MIPS_16", /* name */
333 true, /* partial_inplace */
334 0xffff, /* src_mask */
335 0xffff, /* dst_mask */
336 false), /* pcrel_offset */
337
338 /* 32 bit relocation. */
339 HOWTO (R_MIPS_32, /* type */
340 0, /* rightshift */
341 2, /* size (0 = byte, 1 = short, 2 = long) */
342 32, /* bitsize */
343 false, /* pc_relative */
344 0, /* bitpos */
345 complain_overflow_bitfield, /* complain_on_overflow */
346 bfd_elf_generic_reloc, /* special_function */
347 "R_MIPS_32", /* name */
348 true, /* partial_inplace */
349 0xffffffff, /* src_mask */
350 0xffffffff, /* dst_mask */
351 false), /* pcrel_offset */
352
353 /* 32 bit symbol relative relocation. */
354 HOWTO (R_MIPS_REL32, /* type */
355 0, /* rightshift */
356 2, /* size (0 = byte, 1 = short, 2 = long) */
357 32, /* bitsize */
358 false, /* pc_relative */
359 0, /* bitpos */
360 complain_overflow_bitfield, /* complain_on_overflow */
361 bfd_elf_generic_reloc, /* special_function */
362 "R_MIPS_REL32", /* name */
363 true, /* partial_inplace */
364 0xffffffff, /* src_mask */
365 0xffffffff, /* dst_mask */
366 false), /* pcrel_offset */
367
368 /* 26 bit branch address. */
369 HOWTO (R_MIPS_26, /* type */
370 2, /* rightshift */
371 2, /* size (0 = byte, 1 = short, 2 = long) */
372 26, /* bitsize */
373 false, /* pc_relative */
374 0, /* bitpos */
375 complain_overflow_dont, /* complain_on_overflow */
376 /* This needs complex overflow
377 detection, because the upper four
378 bits must match the PC. */
379 bfd_elf_generic_reloc, /* special_function */
380 "R_MIPS_26", /* name */
381 true, /* partial_inplace */
382 0x3ffffff, /* src_mask */
383 0x3ffffff, /* dst_mask */
384 false), /* pcrel_offset */
385
386 /* High 16 bits of symbol value. */
387 HOWTO (R_MIPS_HI16, /* type */
388 0, /* rightshift */
389 2, /* size (0 = byte, 1 = short, 2 = long) */
390 16, /* bitsize */
391 false, /* pc_relative */
392 0, /* bitpos */
393 complain_overflow_dont, /* complain_on_overflow */
394 _bfd_mips_elf_hi16_reloc, /* special_function */
395 "R_MIPS_HI16", /* name */
396 true, /* partial_inplace */
397 0xffff, /* src_mask */
398 0xffff, /* dst_mask */
399 false), /* pcrel_offset */
400
401 /* Low 16 bits of symbol value. */
402 HOWTO (R_MIPS_LO16, /* type */
403 0, /* rightshift */
404 2, /* size (0 = byte, 1 = short, 2 = long) */
405 16, /* bitsize */
406 false, /* pc_relative */
407 0, /* bitpos */
408 complain_overflow_dont, /* complain_on_overflow */
409 _bfd_mips_elf_lo16_reloc, /* special_function */
410 "R_MIPS_LO16", /* name */
411 true, /* partial_inplace */
412 0xffff, /* src_mask */
413 0xffff, /* dst_mask */
414 false), /* pcrel_offset */
415
416 /* GP relative reference. */
417 HOWTO (R_MIPS_GPREL16, /* type */
418 0, /* rightshift */
419 2, /* size (0 = byte, 1 = short, 2 = long) */
420 16, /* bitsize */
421 false, /* pc_relative */
422 0, /* bitpos */
423 complain_overflow_signed, /* complain_on_overflow */
424 _bfd_mips_elf_gprel16_reloc, /* special_function */
425 "R_MIPS_GPREL16", /* name */
426 true, /* partial_inplace */
427 0xffff, /* src_mask */
428 0xffff, /* dst_mask */
429 false), /* pcrel_offset */
430
431 /* Reference to literal section. */
432 HOWTO (R_MIPS_LITERAL, /* type */
433 0, /* rightshift */
434 2, /* size (0 = byte, 1 = short, 2 = long) */
435 16, /* bitsize */
436 false, /* pc_relative */
437 0, /* bitpos */
438 complain_overflow_signed, /* complain_on_overflow */
439 _bfd_mips_elf_gprel16_reloc, /* special_function */
440 "R_MIPS_LITERAL", /* name */
441 true, /* partial_inplace */
442 0xffff, /* src_mask */
443 0xffff, /* dst_mask */
444 false), /* pcrel_offset */
445
446 /* Reference to global offset table. */
447 HOWTO (R_MIPS_GOT16, /* type */
448 0, /* rightshift */
449 2, /* size (0 = byte, 1 = short, 2 = long) */
450 16, /* bitsize */
451 false, /* pc_relative */
452 0, /* bitpos */
453 complain_overflow_signed, /* complain_on_overflow */
454 _bfd_mips_elf_got16_reloc, /* special_function */
455 "R_MIPS_GOT16", /* name */
456 false, /* partial_inplace */
457 0, /* src_mask */
458 0xffff, /* dst_mask */
459 false), /* pcrel_offset */
460
461 /* 16 bit PC relative reference. */
462 HOWTO (R_MIPS_PC16, /* type */
463 0, /* rightshift */
464 2, /* size (0 = byte, 1 = short, 2 = long) */
465 16, /* bitsize */
466 true, /* pc_relative */
467 0, /* bitpos */
468 complain_overflow_signed, /* complain_on_overflow */
469 bfd_elf_generic_reloc, /* special_function */
470 "R_MIPS_PC16", /* name */
471 true, /* partial_inplace */
472 0xffff, /* src_mask */
473 0xffff, /* dst_mask */
474 false), /* pcrel_offset */
475
476 /* 16 bit call through global offset table. */
477 /* FIXME: This is not handled correctly. */
478 HOWTO (R_MIPS_CALL16, /* type */
479 0, /* rightshift */
480 2, /* size (0 = byte, 1 = short, 2 = long) */
481 16, /* bitsize */
482 false, /* pc_relative */
483 0, /* bitpos */
484 complain_overflow_signed, /* complain_on_overflow */
485 bfd_elf_generic_reloc, /* special_function */
486 "R_MIPS_CALL16", /* name */
487 false, /* partial_inplace */
488 0, /* src_mask */
489 0xffff, /* dst_mask */
490 false), /* pcrel_offset */
491
492 /* 32 bit GP relative reference. */
493 HOWTO (R_MIPS_GPREL32, /* type */
494 0, /* rightshift */
495 2, /* size (0 = byte, 1 = short, 2 = long) */
496 32, /* bitsize */
497 false, /* pc_relative */
498 0, /* bitpos */
499 complain_overflow_bitfield, /* complain_on_overflow */
500 _bfd_mips_elf_gprel32_reloc, /* special_function */
501 "R_MIPS_GPREL32", /* name */
502 true, /* partial_inplace */
503 0xffffffff, /* src_mask */
504 0xffffffff, /* dst_mask */
505 false), /* pcrel_offset */
506
507 /* The remaining relocs are defined on Irix 5, although they are
508 not defined by the ABI. */
509 { 13 },
510 { 14 },
511 { 15 },
512
513 /* A 5 bit shift field. */
514 HOWTO (R_MIPS_SHIFT5, /* type */
515 0, /* rightshift */
516 2, /* size (0 = byte, 1 = short, 2 = long) */
517 5, /* bitsize */
518 false, /* pc_relative */
519 6, /* bitpos */
520 complain_overflow_bitfield, /* complain_on_overflow */
521 bfd_elf_generic_reloc, /* special_function */
522 "R_MIPS_SHIFT5", /* name */
523 true, /* partial_inplace */
524 0x000007c0, /* src_mask */
525 0x000007c0, /* dst_mask */
526 false), /* pcrel_offset */
527
528 /* A 6 bit shift field. */
529 /* FIXME: This is not handled correctly; a special function is
530 needed to put the most significant bit in the right place. */
531 HOWTO (R_MIPS_SHIFT6, /* type */
532 0, /* rightshift */
533 2, /* size (0 = byte, 1 = short, 2 = long) */
534 6, /* bitsize */
535 false, /* pc_relative */
536 6, /* bitpos */
537 complain_overflow_bitfield, /* complain_on_overflow */
538 bfd_elf_generic_reloc, /* special_function */
539 "R_MIPS_SHIFT6", /* name */
540 true, /* partial_inplace */
541 0x000007c4, /* src_mask */
542 0x000007c4, /* dst_mask */
543 false), /* pcrel_offset */
544
545 /* A 64 bit relocation. This is used in 32 bit ELF when addresses
546 are 64 bits long; the upper 32 bits are simply a sign extension.
547 The fields of the howto should be the same as for R_MIPS_32,
548 other than the type, name, and special_function. */
549 HOWTO (R_MIPS_64, /* type */
550 0, /* rightshift */
551 2, /* size (0 = byte, 1 = short, 2 = long) */
552 32, /* bitsize */
553 false, /* pc_relative */
554 0, /* bitpos */
555 complain_overflow_bitfield, /* complain_on_overflow */
556 mips32_64bit_reloc, /* special_function */
557 "R_MIPS_64", /* name */
558 true, /* partial_inplace */
559 0xffffffff, /* src_mask */
560 0xffffffff, /* dst_mask */
561 false), /* pcrel_offset */
562
563 /* Displacement in the global offset table. */
564 /* FIXME: Not handled correctly. */
565 HOWTO (R_MIPS_GOT_DISP, /* type */
566 0, /* rightshift */
567 2, /* size (0 = byte, 1 = short, 2 = long) */
568 16, /* bitsize */
569 false, /* pc_relative */
570 0, /* bitpos */
571 complain_overflow_bitfield, /* complain_on_overflow */
572 bfd_elf_generic_reloc, /* special_function */
573 "R_MIPS_GOT_DISP", /* name */
574 true, /* partial_inplace */
575 0x0000ffff, /* src_mask */
576 0x0000ffff, /* dst_mask */
577 false), /* pcrel_offset */
578
579 /* Displacement to page pointer in the global offset table. */
580 /* FIXME: Not handled correctly. */
581 HOWTO (R_MIPS_GOT_PAGE, /* type */
582 0, /* rightshift */
583 2, /* size (0 = byte, 1 = short, 2 = long) */
584 16, /* bitsize */
585 false, /* pc_relative */
586 0, /* bitpos */
587 complain_overflow_bitfield, /* complain_on_overflow */
588 bfd_elf_generic_reloc, /* special_function */
589 "R_MIPS_GOT_PAGE", /* name */
590 true, /* partial_inplace */
591 0x0000ffff, /* src_mask */
592 0x0000ffff, /* dst_mask */
593 false), /* pcrel_offset */
594
595 /* Offset from page pointer in the global offset table. */
596 /* FIXME: Not handled correctly. */
597 HOWTO (R_MIPS_GOT_OFST, /* type */
598 0, /* rightshift */
599 2, /* size (0 = byte, 1 = short, 2 = long) */
600 16, /* bitsize */
601 false, /* pc_relative */
602 0, /* bitpos */
603 complain_overflow_bitfield, /* complain_on_overflow */
604 bfd_elf_generic_reloc, /* special_function */
605 "R_MIPS_GOT_OFST", /* name */
606 true, /* partial_inplace */
607 0x0000ffff, /* src_mask */
608 0x0000ffff, /* dst_mask */
609 false), /* pcrel_offset */
610
611 /* High 16 bits of displacement in global offset table. */
612 /* FIXME: Not handled correctly. */
613 HOWTO (R_MIPS_GOT_HI16, /* type */
614 0, /* rightshift */
615 2, /* size (0 = byte, 1 = short, 2 = long) */
616 16, /* bitsize */
617 false, /* pc_relative */
618 0, /* bitpos */
619 complain_overflow_dont, /* complain_on_overflow */
620 bfd_elf_generic_reloc, /* special_function */
621 "R_MIPS_GOT_HI16", /* name */
622 true, /* partial_inplace */
623 0x0000ffff, /* src_mask */
624 0x0000ffff, /* dst_mask */
625 false), /* pcrel_offset */
626
627 /* Low 16 bits of displacement in global offset table. */
628 /* FIXME: Not handled correctly. */
629 HOWTO (R_MIPS_GOT_LO16, /* type */
630 0, /* rightshift */
631 2, /* size (0 = byte, 1 = short, 2 = long) */
632 16, /* bitsize */
633 false, /* pc_relative */
634 0, /* bitpos */
635 complain_overflow_dont, /* complain_on_overflow */
636 bfd_elf_generic_reloc, /* special_function */
637 "R_MIPS_GOT_LO16", /* name */
638 true, /* partial_inplace */
639 0x0000ffff, /* src_mask */
640 0x0000ffff, /* dst_mask */
641 false), /* pcrel_offset */
642
3f830999
MM
643 /* 64 bit subtraction. Used in the N32 ABI. */
644 /* FIXME: Not handled correctly. */
645 HOWTO (R_MIPS_SUB, /* type */
646 0, /* rightshift */
647 4, /* size (0 = byte, 1 = short, 2 = long) */
648 64, /* bitsize */
649 false, /* pc_relative */
650 0, /* bitpos */
651 complain_overflow_bitfield, /* complain_on_overflow */
652 bfd_elf_generic_reloc, /* special_function */
653 "R_MIPS_SUB", /* name */
654 true, /* partial_inplace */
655 MINUS_ONE, /* src_mask */
656 MINUS_ONE, /* dst_mask */
657 false), /* pcrel_offset */
252b5132
RH
658
659 /* Used to cause the linker to insert and delete instructions? */
660 { R_MIPS_INSERT_A },
661 { R_MIPS_INSERT_B },
662 { R_MIPS_DELETE },
663
664 /* Get the higher values of a 64 bit addend. Presumably not used in
665 32 bit ELF. */
666 { R_MIPS_HIGHER },
667 { R_MIPS_HIGHEST },
668
669 /* High 16 bits of displacement in global offset table. */
670 /* FIXME: Not handled correctly. */
671 HOWTO (R_MIPS_CALL_HI16, /* type */
672 0, /* rightshift */
673 2, /* size (0 = byte, 1 = short, 2 = long) */
674 16, /* bitsize */
675 false, /* pc_relative */
676 0, /* bitpos */
677 complain_overflow_dont, /* complain_on_overflow */
678 bfd_elf_generic_reloc, /* special_function */
679 "R_MIPS_CALL_HI16", /* name */
680 true, /* partial_inplace */
681 0x0000ffff, /* src_mask */
682 0x0000ffff, /* dst_mask */
683 false), /* pcrel_offset */
684
685 /* Low 16 bits of displacement in global offset table. */
686 /* FIXME: Not handled correctly. */
687 HOWTO (R_MIPS_CALL_LO16, /* type */
688 0, /* rightshift */
689 2, /* size (0 = byte, 1 = short, 2 = long) */
690 16, /* bitsize */
691 false, /* pc_relative */
692 0, /* bitpos */
693 complain_overflow_dont, /* complain_on_overflow */
694 bfd_elf_generic_reloc, /* special_function */
695 "R_MIPS_CALL_LO16", /* name */
696 true, /* partial_inplace */
697 0x0000ffff, /* src_mask */
698 0x0000ffff, /* dst_mask */
699 false), /* pcrel_offset */
700
701 { R_MIPS_SCN_DISP },
702 { R_MIPS_REL16 },
703 { R_MIPS_ADD_IMMEDIATE },
704 { R_MIPS_PJUMP },
705 { R_MIPS_RELGOT }
706};
707
708/* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link. This
709 is a hack to make the linker think that we need 64 bit values. */
710static reloc_howto_type elf_mips_ctor64_howto =
711 HOWTO (R_MIPS_64, /* type */
712 0, /* rightshift */
713 4, /* size (0 = byte, 1 = short, 2 = long) */
714 32, /* bitsize */
715 false, /* pc_relative */
716 0, /* bitpos */
717 complain_overflow_signed, /* complain_on_overflow */
718 mips32_64bit_reloc, /* special_function */
719 "R_MIPS_64", /* name */
720 true, /* partial_inplace */
721 0xffffffff, /* src_mask */
722 0xffffffff, /* dst_mask */
723 false); /* pcrel_offset */
724
725/* The reloc used for the mips16 jump instruction. */
726static reloc_howto_type elf_mips16_jump_howto =
727 HOWTO (R_MIPS16_26, /* type */
728 2, /* rightshift */
729 2, /* size (0 = byte, 1 = short, 2 = long) */
730 26, /* bitsize */
731 false, /* pc_relative */
732 0, /* bitpos */
733 complain_overflow_dont, /* complain_on_overflow */
734 /* This needs complex overflow
735 detection, because the upper four
736 bits must match the PC. */
737 mips16_jump_reloc, /* special_function */
738 "R_MIPS16_26", /* name */
739 true, /* partial_inplace */
740 0x3ffffff, /* src_mask */
741 0x3ffffff, /* dst_mask */
742 false); /* pcrel_offset */
743
744/* The reloc used for the mips16 gprel instruction. The src_mask and
745 dsk_mask for this howto do not reflect the actual instruction, in
746 which the value is not contiguous; the masks are for the
747 convenience of the relocate_section routine. */
748static reloc_howto_type elf_mips16_gprel_howto =
749 HOWTO (R_MIPS16_GPREL, /* type */
750 0, /* rightshift */
751 2, /* size (0 = byte, 1 = short, 2 = long) */
752 16, /* bitsize */
753 false, /* pc_relative */
754 0, /* bitpos */
755 complain_overflow_signed, /* complain_on_overflow */
756 mips16_gprel_reloc, /* special_function */
757 "R_MIPS16_GPREL", /* name */
758 true, /* partial_inplace */
759 0xffff, /* src_mask */
760 0xffff, /* dst_mask */
761 false); /* pcrel_offset */
762
763
764/* GNU extension to record C++ vtable hierarchy */
765static reloc_howto_type elf_mips_gnu_vtinherit_howto =
766 HOWTO (R_MIPS_GNU_VTINHERIT, /* type */
767 0, /* rightshift */
768 2, /* size (0 = byte, 1 = short, 2 = long) */
769 0, /* bitsize */
770 false, /* pc_relative */
771 0, /* bitpos */
772 complain_overflow_dont, /* complain_on_overflow */
773 NULL, /* special_function */
774 "R_MIPS_GNU_VTINHERIT", /* name */
775 false, /* partial_inplace */
776 0, /* src_mask */
777 0, /* dst_mask */
778 false); /* pcrel_offset */
779
780/* GNU extension to record C++ vtable member usage */
781static reloc_howto_type elf_mips_gnu_vtentry_howto =
782 HOWTO (R_MIPS_GNU_VTENTRY, /* type */
783 0, /* rightshift */
784 2, /* size (0 = byte, 1 = short, 2 = long) */
785 0, /* bitsize */
786 false, /* pc_relative */
787 0, /* bitpos */
788 complain_overflow_dont, /* complain_on_overflow */
789 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
790 "R_MIPS_GNU_VTENTRY", /* name */
791 false, /* partial_inplace */
792 0, /* src_mask */
793 0, /* dst_mask */
794 false); /* pcrel_offset */
795
796/* Do a R_MIPS_HI16 relocation. This has to be done in combination
797 with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
798 the HI16. Here we just save the information we need; we do the
799 actual relocation when we see the LO16. MIPS ELF requires that the
800 LO16 immediately follow the HI16. As a GNU extension, we permit an
801 arbitrary number of HI16 relocs to be associated with a single LO16
802 reloc. This extension permits gcc to output the HI and LO relocs
803 itself. */
804
805struct mips_hi16
806{
807 struct mips_hi16 *next;
808 bfd_byte *addr;
809 bfd_vma addend;
810};
811
812/* FIXME: This should not be a static variable. */
813
814static struct mips_hi16 *mips_hi16_list;
815
816bfd_reloc_status_type
817_bfd_mips_elf_hi16_reloc (abfd,
818 reloc_entry,
819 symbol,
820 data,
821 input_section,
822 output_bfd,
823 error_message)
824 bfd *abfd;
825 arelent *reloc_entry;
826 asymbol *symbol;
827 PTR data;
828 asection *input_section;
829 bfd *output_bfd;
830 char **error_message;
831{
832 bfd_reloc_status_type ret;
833 bfd_vma relocation;
834 struct mips_hi16 *n;
835
836 /* If we're relocating, and this an external symbol, we don't want
837 to change anything. */
838 if (output_bfd != (bfd *) NULL
839 && (symbol->flags & BSF_SECTION_SYM) == 0
840 && reloc_entry->addend == 0)
841 {
842 reloc_entry->address += input_section->output_offset;
843 return bfd_reloc_ok;
844 }
845
846 ret = bfd_reloc_ok;
847
848 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
849 {
850 boolean relocateable;
851 bfd_vma gp;
852
853 if (ret == bfd_reloc_undefined)
854 abort ();
855
856 if (output_bfd != NULL)
857 relocateable = true;
858 else
859 {
860 relocateable = false;
861 output_bfd = symbol->section->output_section->owner;
862 }
863
864 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
865 error_message, &gp);
866 if (ret != bfd_reloc_ok)
867 return ret;
868
869 relocation = gp - reloc_entry->address;
870 }
871 else
872 {
873 if (bfd_is_und_section (symbol->section)
874 && output_bfd == (bfd *) NULL)
875 ret = bfd_reloc_undefined;
876
877 if (bfd_is_com_section (symbol->section))
878 relocation = 0;
879 else
880 relocation = symbol->value;
881 }
882
883 relocation += symbol->section->output_section->vma;
884 relocation += symbol->section->output_offset;
885 relocation += reloc_entry->addend;
886
887 if (reloc_entry->address > input_section->_cooked_size)
888 return bfd_reloc_outofrange;
889
890 /* Save the information, and let LO16 do the actual relocation. */
891 n = (struct mips_hi16 *) bfd_malloc (sizeof *n);
892 if (n == NULL)
893 return bfd_reloc_outofrange;
894 n->addr = (bfd_byte *) data + reloc_entry->address;
895 n->addend = relocation;
896 n->next = mips_hi16_list;
897 mips_hi16_list = n;
898
899 if (output_bfd != (bfd *) NULL)
900 reloc_entry->address += input_section->output_offset;
901
902 return ret;
903}
904
905/* Do a R_MIPS_LO16 relocation. This is a straightforward 16 bit
906 inplace relocation; this function exists in order to do the
907 R_MIPS_HI16 relocation described above. */
908
909bfd_reloc_status_type
910_bfd_mips_elf_lo16_reloc (abfd,
911 reloc_entry,
912 symbol,
913 data,
914 input_section,
915 output_bfd,
916 error_message)
917 bfd *abfd;
918 arelent *reloc_entry;
919 asymbol *symbol;
920 PTR data;
921 asection *input_section;
922 bfd *output_bfd;
923 char **error_message;
924{
925 arelent gp_disp_relent;
926
927 if (mips_hi16_list != NULL)
928 {
929 struct mips_hi16 *l;
930
931 l = mips_hi16_list;
932 while (l != NULL)
933 {
934 unsigned long insn;
935 unsigned long val;
936 unsigned long vallo;
937 struct mips_hi16 *next;
938
939 /* Do the HI16 relocation. Note that we actually don't need
940 to know anything about the LO16 itself, except where to
941 find the low 16 bits of the addend needed by the LO16. */
942 insn = bfd_get_32 (abfd, l->addr);
943 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
944 & 0xffff);
945 val = ((insn & 0xffff) << 16) + vallo;
946 val += l->addend;
947
948 /* The low order 16 bits are always treated as a signed
949 value. Therefore, a negative value in the low order bits
950 requires an adjustment in the high order bits. We need
951 to make this adjustment in two ways: once for the bits we
952 took from the data, and once for the bits we are putting
953 back in to the data. */
954 if ((vallo & 0x8000) != 0)
955 val -= 0x10000;
956 if ((val & 0x8000) != 0)
957 val += 0x10000;
958
959 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
960 bfd_put_32 (abfd, insn, l->addr);
961
962 if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
963 {
964 gp_disp_relent = *reloc_entry;
965 reloc_entry = &gp_disp_relent;
966 reloc_entry->addend = l->addend;
967 }
968
969 next = l->next;
970 free (l);
971 l = next;
972 }
973
974 mips_hi16_list = NULL;
975 }
976 else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
977 {
978 bfd_reloc_status_type ret;
979 bfd_vma gp, relocation;
980
981 /* FIXME: Does this case ever occur? */
982
983 ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
984 if (ret != bfd_reloc_ok)
985 return ret;
986
987 relocation = gp - reloc_entry->address;
988 relocation += symbol->section->output_section->vma;
989 relocation += symbol->section->output_offset;
990 relocation += reloc_entry->addend;
991
992 if (reloc_entry->address > input_section->_cooked_size)
993 return bfd_reloc_outofrange;
994
995 gp_disp_relent = *reloc_entry;
996 reloc_entry = &gp_disp_relent;
997 reloc_entry->addend = relocation - 4;
998 }
999
1000 /* Now do the LO16 reloc in the usual way. */
1001 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1002 input_section, output_bfd, error_message);
1003}
1004
1005/* Do a R_MIPS_GOT16 reloc. This is a reloc against the global offset
1006 table used for PIC code. If the symbol is an external symbol, the
1007 instruction is modified to contain the offset of the appropriate
1008 entry in the global offset table. If the symbol is a section
1009 symbol, the next reloc is a R_MIPS_LO16 reloc. The two 16 bit
1010 addends are combined to form the real addend against the section
1011 symbol; the GOT16 is modified to contain the offset of an entry in
1012 the global offset table, and the LO16 is modified to offset it
1013 appropriately. Thus an offset larger than 16 bits requires a
1014 modified value in the global offset table.
1015
1016 This implementation suffices for the assembler, but the linker does
1017 not yet know how to create global offset tables. */
1018
1019bfd_reloc_status_type
1020_bfd_mips_elf_got16_reloc (abfd,
1021 reloc_entry,
1022 symbol,
1023 data,
1024 input_section,
1025 output_bfd,
1026 error_message)
1027 bfd *abfd;
1028 arelent *reloc_entry;
1029 asymbol *symbol;
1030 PTR data;
1031 asection *input_section;
1032 bfd *output_bfd;
1033 char **error_message;
1034{
1035 /* If we're relocating, and this an external symbol, we don't want
1036 to change anything. */
1037 if (output_bfd != (bfd *) NULL
1038 && (symbol->flags & BSF_SECTION_SYM) == 0
1039 && reloc_entry->addend == 0)
1040 {
1041 reloc_entry->address += input_section->output_offset;
1042 return bfd_reloc_ok;
1043 }
1044
1045 /* If we're relocating, and this is a local symbol, we can handle it
1046 just like HI16. */
1047 if (output_bfd != (bfd *) NULL
1048 && (symbol->flags & BSF_SECTION_SYM) != 0)
1049 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1050 input_section, output_bfd, error_message);
1051
1052 abort ();
1053}
1054
1055/* We have to figure out the gp value, so that we can adjust the
1056 symbol value correctly. We look up the symbol _gp in the output
1057 BFD. If we can't find it, we're stuck. We cache it in the ELF
1058 target data. We don't need to adjust the symbol value for an
1059 external symbol if we are producing relocateable output. */
1060
1061static bfd_reloc_status_type
1062mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
1063 bfd *output_bfd;
1064 asymbol *symbol;
1065 boolean relocateable;
1066 char **error_message;
1067 bfd_vma *pgp;
1068{
1069 if (bfd_is_und_section (symbol->section)
1070 && ! relocateable)
1071 {
1072 *pgp = 0;
1073 return bfd_reloc_undefined;
1074 }
1075
1076 *pgp = _bfd_get_gp_value (output_bfd);
1077 if (*pgp == 0
1078 && (! relocateable
1079 || (symbol->flags & BSF_SECTION_SYM) != 0))
1080 {
1081 if (relocateable)
1082 {
1083 /* Make up a value. */
1084 *pgp = symbol->section->output_section->vma + 0x4000;
1085 _bfd_set_gp_value (output_bfd, *pgp);
1086 }
1087 else
1088 {
1089 unsigned int count;
1090 asymbol **sym;
1091 unsigned int i;
1092
1093 count = bfd_get_symcount (output_bfd);
1094 sym = bfd_get_outsymbols (output_bfd);
1095
1096 if (sym == (asymbol **) NULL)
1097 i = count;
1098 else
1099 {
1100 for (i = 0; i < count; i++, sym++)
1101 {
1102 register CONST char *name;
1103
1104 name = bfd_asymbol_name (*sym);
1105 if (*name == '_' && strcmp (name, "_gp") == 0)
1106 {
1107 *pgp = bfd_asymbol_value (*sym);
1108 _bfd_set_gp_value (output_bfd, *pgp);
1109 break;
1110 }
1111 }
1112 }
1113
1114 if (i >= count)
1115 {
1116 /* Only get the error once. */
1117 *pgp = 4;
1118 _bfd_set_gp_value (output_bfd, *pgp);
1119 *error_message =
1120 (char *) _("GP relative relocation when _gp not defined");
1121 return bfd_reloc_dangerous;
1122 }
1123 }
1124 }
1125
1126 return bfd_reloc_ok;
1127}
1128
1129/* Do a R_MIPS_GPREL16 relocation. This is a 16 bit value which must
1130 become the offset from the gp register. This function also handles
1131 R_MIPS_LITERAL relocations, although those can be handled more
1132 cleverly because the entries in the .lit8 and .lit4 sections can be
1133 merged. */
1134
1135static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
1136 arelent *, asection *,
1137 boolean, PTR, bfd_vma));
1138
1139bfd_reloc_status_type
1140_bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1141 output_bfd, error_message)
1142 bfd *abfd;
1143 arelent *reloc_entry;
1144 asymbol *symbol;
1145 PTR data;
1146 asection *input_section;
1147 bfd *output_bfd;
1148 char **error_message;
1149{
1150 boolean relocateable;
1151 bfd_reloc_status_type ret;
1152 bfd_vma gp;
1153
1154 /* If we're relocating, and this is an external symbol with no
1155 addend, we don't want to change anything. We will only have an
1156 addend if this is a newly created reloc, not read from an ELF
1157 file. */
1158 if (output_bfd != (bfd *) NULL
1159 && (symbol->flags & BSF_SECTION_SYM) == 0
1160 && reloc_entry->addend == 0)
1161 {
1162 reloc_entry->address += input_section->output_offset;
1163 return bfd_reloc_ok;
1164 }
1165
1166 if (output_bfd != (bfd *) NULL)
1167 relocateable = true;
1168 else
1169 {
1170 relocateable = false;
1171 output_bfd = symbol->section->output_section->owner;
1172 }
1173
1174 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1175 &gp);
1176 if (ret != bfd_reloc_ok)
1177 return ret;
1178
1179 return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1180 relocateable, data, gp);
1181}
1182
1183static bfd_reloc_status_type
1184gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1185 gp)
1186 bfd *abfd;
1187 asymbol *symbol;
1188 arelent *reloc_entry;
1189 asection *input_section;
1190 boolean relocateable;
1191 PTR data;
1192 bfd_vma gp;
1193{
1194 bfd_vma relocation;
1195 unsigned long insn;
1196 unsigned long val;
1197
1198 if (bfd_is_com_section (symbol->section))
1199 relocation = 0;
1200 else
1201 relocation = symbol->value;
1202
1203 relocation += symbol->section->output_section->vma;
1204 relocation += symbol->section->output_offset;
1205
1206 if (reloc_entry->address > input_section->_cooked_size)
1207 return bfd_reloc_outofrange;
1208
1209 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1210
1211 /* Set val to the offset into the section or symbol. */
1212 if (reloc_entry->howto->src_mask == 0)
1213 {
1214 /* This case occurs with the 64-bit MIPS ELF ABI. */
1215 val = reloc_entry->addend;
1216 }
1217 else
1218 {
1219 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1220 if (val & 0x8000)
1221 val -= 0x10000;
1222 }
1223
1224 /* Adjust val for the final section location and GP value. If we
1225 are producing relocateable output, we don't want to do this for
1226 an external symbol. */
1227 if (! relocateable
1228 || (symbol->flags & BSF_SECTION_SYM) != 0)
1229 val += relocation - gp;
1230
1231 insn = (insn &~ 0xffff) | (val & 0xffff);
1232 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1233
1234 if (relocateable)
1235 reloc_entry->address += input_section->output_offset;
1236
1237 /* Make sure it fit in 16 bits. */
1238 if (val >= 0x8000 && val < 0xffff8000)
1239 return bfd_reloc_overflow;
1240
1241 return bfd_reloc_ok;
1242}
1243
1244/* Do a R_MIPS_GPREL32 relocation. Is this 32 bit value the offset
1245 from the gp register? XXX */
1246
1247static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1248 arelent *, asection *,
1249 boolean, PTR, bfd_vma));
1250
1251bfd_reloc_status_type
1252_bfd_mips_elf_gprel32_reloc (abfd,
1253 reloc_entry,
1254 symbol,
1255 data,
1256 input_section,
1257 output_bfd,
1258 error_message)
1259 bfd *abfd;
1260 arelent *reloc_entry;
1261 asymbol *symbol;
1262 PTR data;
1263 asection *input_section;
1264 bfd *output_bfd;
1265 char **error_message;
1266{
1267 boolean relocateable;
1268 bfd_reloc_status_type ret;
1269 bfd_vma gp;
1270
1271 /* If we're relocating, and this is an external symbol with no
1272 addend, we don't want to change anything. We will only have an
1273 addend if this is a newly created reloc, not read from an ELF
1274 file. */
1275 if (output_bfd != (bfd *) NULL
1276 && (symbol->flags & BSF_SECTION_SYM) == 0
1277 && reloc_entry->addend == 0)
1278 {
1279 *error_message = (char *)
1280 _("32bits gp relative relocation occurs for an external symbol");
1281 return bfd_reloc_outofrange;
1282 }
1283
1284 if (output_bfd != (bfd *) NULL)
1285 {
1286 relocateable = true;
1287 gp = _bfd_get_gp_value (output_bfd);
1288 }
1289 else
1290 {
1291 relocateable = false;
1292 output_bfd = symbol->section->output_section->owner;
1293
1294 ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1295 error_message, &gp);
1296 if (ret != bfd_reloc_ok)
1297 return ret;
1298 }
1299
1300 return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1301 relocateable, data, gp);
1302}
1303
1304static bfd_reloc_status_type
1305gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1306 gp)
1307 bfd *abfd;
1308 asymbol *symbol;
1309 arelent *reloc_entry;
1310 asection *input_section;
1311 boolean relocateable;
1312 PTR data;
1313 bfd_vma gp;
1314{
1315 bfd_vma relocation;
1316 unsigned long val;
1317
1318 if (bfd_is_com_section (symbol->section))
1319 relocation = 0;
1320 else
1321 relocation = symbol->value;
1322
1323 relocation += symbol->section->output_section->vma;
1324 relocation += symbol->section->output_offset;
1325
1326 if (reloc_entry->address > input_section->_cooked_size)
1327 return bfd_reloc_outofrange;
1328
1329 if (reloc_entry->howto->src_mask == 0)
1330 {
1331 /* This case arises with the 64-bit MIPS ELF ABI. */
1332 val = 0;
1333 }
1334 else
1335 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1336
1337 /* Set val to the offset into the section or symbol. */
1338 val += reloc_entry->addend;
1339
1340 /* Adjust val for the final section location and GP value. If we
1341 are producing relocateable output, we don't want to do this for
1342 an external symbol. */
1343 if (! relocateable
1344 || (symbol->flags & BSF_SECTION_SYM) != 0)
1345 val += relocation - gp;
1346
1347 bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1348
1349 if (relocateable)
1350 reloc_entry->address += input_section->output_offset;
1351
1352 return bfd_reloc_ok;
1353}
1354
1355/* Handle a 64 bit reloc in a 32 bit MIPS ELF file. These are
1356 generated when addreses are 64 bits. The upper 32 bits are a simle
1357 sign extension. */
1358
1359static bfd_reloc_status_type
1360mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
1361 output_bfd, error_message)
1362 bfd *abfd;
1363 arelent *reloc_entry;
1364 asymbol *symbol;
1365 PTR data;
1366 asection *input_section;
1367 bfd *output_bfd;
1368 char **error_message;
1369{
1370 bfd_reloc_status_type r;
1371 arelent reloc32;
1372 unsigned long val;
1373 bfd_size_type addr;
1374
1375 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1376 input_section, output_bfd, error_message);
1377 if (r != bfd_reloc_continue)
1378 return r;
1379
1380 /* Do a normal 32 bit relocation on the lower 32 bits. */
1381 reloc32 = *reloc_entry;
1382 if (bfd_big_endian (abfd))
1383 reloc32.address += 4;
1384 reloc32.howto = &elf_mips_howto_table[R_MIPS_32];
1385 r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1386 output_bfd, error_message);
1387
1388 /* Sign extend into the upper 32 bits. */
1389 val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1390 if ((val & 0x80000000) != 0)
1391 val = 0xffffffff;
1392 else
1393 val = 0;
1394 addr = reloc_entry->address;
1395 if (bfd_little_endian (abfd))
1396 addr += 4;
1397 bfd_put_32 (abfd, val, (bfd_byte *) data + addr);
1398
1399 return r;
1400}
1401
1402/* Handle a mips16 jump. */
1403
1404static bfd_reloc_status_type
1405mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section,
1406 output_bfd, error_message)
1407 bfd *abfd;
1408 arelent *reloc_entry;
1409 asymbol *symbol;
1410 PTR data;
1411 asection *input_section;
1412 bfd *output_bfd;
1413 char **error_message;
1414{
1415 if (output_bfd != (bfd *) NULL
1416 && (symbol->flags & BSF_SECTION_SYM) == 0
1417 && reloc_entry->addend == 0)
1418 {
1419 reloc_entry->address += input_section->output_offset;
1420 return bfd_reloc_ok;
1421 }
1422
1423 /* FIXME. */
1424 {
1425 static boolean warned;
1426
1427 if (! warned)
1428 (*_bfd_error_handler)
1429 (_("Linking mips16 objects into %s format is not supported"),
1430 bfd_get_target (input_section->output_section->owner));
1431 warned = true;
1432 }
1433
1434 return bfd_reloc_undefined;
1435}
1436
1437/* Handle a mips16 GP relative reloc. */
1438
1439static bfd_reloc_status_type
1440mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
1441 output_bfd, error_message)
1442 bfd *abfd;
1443 arelent *reloc_entry;
1444 asymbol *symbol;
1445 PTR data;
1446 asection *input_section;
1447 bfd *output_bfd;
1448 char **error_message;
1449{
1450 boolean relocateable;
1451 bfd_reloc_status_type ret;
1452 bfd_vma gp;
1453 unsigned short extend, insn;
1454 unsigned long final;
1455
1456 /* If we're relocating, and this is an external symbol with no
1457 addend, we don't want to change anything. We will only have an
1458 addend if this is a newly created reloc, not read from an ELF
1459 file. */
1460 if (output_bfd != NULL
1461 && (symbol->flags & BSF_SECTION_SYM) == 0
1462 && reloc_entry->addend == 0)
1463 {
1464 reloc_entry->address += input_section->output_offset;
1465 return bfd_reloc_ok;
1466 }
1467
1468 if (output_bfd != NULL)
1469 relocateable = true;
1470 else
1471 {
1472 relocateable = false;
1473 output_bfd = symbol->section->output_section->owner;
1474 }
1475
1476 ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1477 &gp);
1478 if (ret != bfd_reloc_ok)
1479 return ret;
1480
1481 if (reloc_entry->address > input_section->_cooked_size)
1482 return bfd_reloc_outofrange;
1483
1484 /* Pick up the mips16 extend instruction and the real instruction. */
1485 extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
1486 insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
1487
1488 /* Stuff the current addend back as a 32 bit value, do the usual
1489 relocation, and then clean up. */
1490 bfd_put_32 (abfd,
1491 (((extend & 0x1f) << 11)
1492 | (extend & 0x7e0)
1493 | (insn & 0x1f)),
1494 (bfd_byte *) data + reloc_entry->address);
1495
1496 ret = gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1497 relocateable, data, gp);
1498
1499 final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1500 bfd_put_16 (abfd,
1501 ((extend & 0xf800)
1502 | ((final >> 11) & 0x1f)
1503 | (final & 0x7e0)),
1504 (bfd_byte *) data + reloc_entry->address);
1505 bfd_put_16 (abfd,
1506 ((insn & 0xffe0)
1507 | (final & 0x1f)),
1508 (bfd_byte *) data + reloc_entry->address + 2);
1509
1510 return ret;
1511}
1512
1513/* Return the ISA for a MIPS e_flags value. */
1514
1515static INLINE int
1516elf_mips_isa (flags)
1517 flagword flags;
1518{
1519 switch (flags & EF_MIPS_ARCH)
1520 {
1521 case E_MIPS_ARCH_1:
1522 return 1;
1523 case E_MIPS_ARCH_2:
1524 return 2;
1525 case E_MIPS_ARCH_3:
1526 return 3;
1527 case E_MIPS_ARCH_4:
1528 return 4;
1529 }
1530 return 4;
1531}
1532
1533/* Return the MACH for a MIPS e_flags value. */
1534
1535static INLINE int
1536elf_mips_mach (flags)
1537 flagword flags;
1538{
1539 switch (flags & EF_MIPS_MACH)
1540 {
1541 case E_MIPS_MACH_3900:
1542 return bfd_mach_mips3900;
1543
1544 case E_MIPS_MACH_4010:
1545 return bfd_mach_mips4010;
1546
1547 case E_MIPS_MACH_4100:
1548 return bfd_mach_mips4100;
1549
1550 case E_MIPS_MACH_4111:
1551 return bfd_mach_mips4111;
1552
1553 case E_MIPS_MACH_4650:
1554 return bfd_mach_mips4650;
1555
1556 default:
1557 switch (flags & EF_MIPS_ARCH)
1558 {
1559 default:
1560 case E_MIPS_ARCH_1:
1561 return bfd_mach_mips3000;
1562 break;
1563
1564 case E_MIPS_ARCH_2:
1565 return bfd_mach_mips6000;
1566 break;
1567
1568 case E_MIPS_ARCH_3:
1569 return bfd_mach_mips4000;
1570 break;
1571
1572 case E_MIPS_ARCH_4:
1573 return bfd_mach_mips8000;
1574 break;
1575 }
1576 }
1577
1578 return 0;
1579}
1580
1581/* Return printable name for ABI from flagword. */
1582
1583static INLINE char*
1584elf_mips_abi_name (flags)
1585 flagword flags;
1586{
1587 switch (flags & EF_MIPS_ABI)
1588 {
1589 case 0:
1590 return "none";
1591 case E_MIPS_ABI_O32:
1592 return "O32";
1593 case E_MIPS_ABI_O64:
1594 return "O64";
1595 case E_MIPS_ABI_EABI32:
1596 return "EABI32";
1597 case E_MIPS_ABI_EABI64:
1598 return "EABI64";
1599 default:
1600 return "unknown abi";
1601 }
1602}
1603
1604/* A mapping from BFD reloc types to MIPS ELF reloc types. */
1605
1606struct elf_reloc_map {
1607 bfd_reloc_code_real_type bfd_reloc_val;
1608 enum elf_mips_reloc_type elf_reloc_val;
1609};
1610
1611static CONST struct elf_reloc_map mips_reloc_map[] =
1612{
1613 { BFD_RELOC_NONE, R_MIPS_NONE, },
1614 { BFD_RELOC_16, R_MIPS_16 },
1615 { BFD_RELOC_32, R_MIPS_32 },
1616 { BFD_RELOC_64, R_MIPS_64 },
1617 { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1618 { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1619 { BFD_RELOC_LO16, R_MIPS_LO16 },
1620 { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
1621 { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1622 { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1623 { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1624 { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1625 { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 },
1626 { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1627 { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1628 { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
3f830999
MM
1629 { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 },
1630 { BFD_RELOC_MIPS_SUB, R_MIPS_SUB },
1631 { BFD_RELOC_MIPS_GOT_PAGE, R_MIPS_GOT_PAGE },
1632 { BFD_RELOC_MIPS_GOT_OFST, R_MIPS_GOT_OFST },
1633 { BFD_RELOC_MIPS_GOT_DISP, R_MIPS_GOT_DISP }
252b5132
RH
1634};
1635
1636/* Given a BFD reloc type, return a howto structure. */
1637
1638static reloc_howto_type *
1639bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1640 bfd *abfd;
1641 bfd_reloc_code_real_type code;
1642{
1643 unsigned int i;
1644
1645 for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
1646 {
1647 if (mips_reloc_map[i].bfd_reloc_val == code)
1648 return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
1649 }
1650
1651 switch (code)
1652 {
1653 default:
1654 bfd_set_error (bfd_error_bad_value);
1655 return NULL;
1656
1657 case BFD_RELOC_CTOR:
1658 /* We need to handle BFD_RELOC_CTOR specially.
1659 Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the
1660 size of addresses on this architecture. */
1661 if (bfd_arch_bits_per_address (abfd) == 32)
1662 return &elf_mips_howto_table[(int) R_MIPS_32];
1663 else
1664 return &elf_mips_ctor64_howto;
1665
1666 case BFD_RELOC_MIPS16_JMP:
1667 return &elf_mips16_jump_howto;
1668 case BFD_RELOC_MIPS16_GPREL:
1669 return &elf_mips16_gprel_howto;
1670 case BFD_RELOC_VTABLE_INHERIT:
1671 return &elf_mips_gnu_vtinherit_howto;
1672 case BFD_RELOC_VTABLE_ENTRY:
1673 return &elf_mips_gnu_vtentry_howto;
1674 }
1675}
1676
3f830999 1677/* Given a MIPS Elf32_Internal_Rel, fill in an arelent structure. */
252b5132
RH
1678
1679static void
1680mips_info_to_howto_rel (abfd, cache_ptr, dst)
1681 bfd *abfd;
1682 arelent *cache_ptr;
1683 Elf32_Internal_Rel *dst;
1684{
1685 unsigned int r_type;
1686
1687 r_type = ELF32_R_TYPE (dst->r_info);
1688 switch (r_type)
1689 {
1690 case R_MIPS16_26:
1691 cache_ptr->howto = &elf_mips16_jump_howto;
1692 break;
1693 case R_MIPS16_GPREL:
1694 cache_ptr->howto = &elf_mips16_gprel_howto;
1695 break;
1696 case R_MIPS_GNU_VTINHERIT:
1697 cache_ptr->howto = &elf_mips_gnu_vtinherit_howto;
1698 break;
1699 case R_MIPS_GNU_VTENTRY:
1700 cache_ptr->howto = &elf_mips_gnu_vtentry_howto;
1701 break;
1702
1703 default:
1704 BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1705 cache_ptr->howto = &elf_mips_howto_table[r_type];
1706 break;
1707 }
1708
1709 /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1710 value for the object file. We get the addend now, rather than
1711 when we do the relocation, because the symbol manipulations done
1712 by the linker may cause us to lose track of the input BFD. */
1713 if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1714 && (r_type == (unsigned int) R_MIPS_GPREL16
1715 || r_type == (unsigned int) R_MIPS_LITERAL))
1716 cache_ptr->addend = elf_gp (abfd);
1717}
3f830999
MM
1718
1719/* Given a MIPS Elf32_Internal_Rela, fill in an arelent structure. */
1720
1721static void
1722mips_info_to_howto_rela (abfd, cache_ptr, dst)
1723 bfd *abfd;
1724 arelent *cache_ptr;
1725 Elf32_Internal_Rela *dst;
1726{
1727 /* Since an Elf32_Internal_Rel is an initial prefix of an
1728 Elf32_Internal_Rela, we can just use mips_info_to_howto_rel
1729 above. */
1730 mips_info_to_howto_rel (abfd, cache_ptr, (Elf32_Internal_Rel *) dst);
1731
1732 /* If we ever need to do any extra processing with dst->r_addend
1733 (the field omitted in an Elf32_Internal_Rel) we can do it here. */
1734}
252b5132
RH
1735\f
1736/* A .reginfo section holds a single Elf32_RegInfo structure. These
1737 routines swap this structure in and out. They are used outside of
1738 BFD, so they are globally visible. */
1739
1740void
1741bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
1742 bfd *abfd;
1743 const Elf32_External_RegInfo *ex;
1744 Elf32_RegInfo *in;
1745{
1746 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1747 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1748 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1749 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1750 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1751 in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
1752}
1753
1754void
1755bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
1756 bfd *abfd;
1757 const Elf32_RegInfo *in;
1758 Elf32_External_RegInfo *ex;
1759{
1760 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1761 (bfd_byte *) ex->ri_gprmask);
1762 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1763 (bfd_byte *) ex->ri_cprmask[0]);
1764 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1765 (bfd_byte *) ex->ri_cprmask[1]);
1766 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1767 (bfd_byte *) ex->ri_cprmask[2]);
1768 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1769 (bfd_byte *) ex->ri_cprmask[3]);
1770 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
1771 (bfd_byte *) ex->ri_gp_value);
1772}
1773
1774/* In the 64 bit ABI, the .MIPS.options section holds register
1775 information in an Elf64_Reginfo structure. These routines swap
1776 them in and out. They are globally visible because they are used
1777 outside of BFD. These routines are here so that gas can call them
1778 without worrying about whether the 64 bit ABI has been included. */
1779
1780void
1781bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
1782 bfd *abfd;
1783 const Elf64_External_RegInfo *ex;
1784 Elf64_Internal_RegInfo *in;
1785{
1786 in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1787 in->ri_pad = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_pad);
1788 in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1789 in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1790 in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1791 in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1792 in->ri_gp_value = bfd_h_get_64 (abfd, (bfd_byte *) ex->ri_gp_value);
1793}
1794
1795void
1796bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
1797 bfd *abfd;
1798 const Elf64_Internal_RegInfo *in;
1799 Elf64_External_RegInfo *ex;
1800{
1801 bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1802 (bfd_byte *) ex->ri_gprmask);
1803 bfd_h_put_32 (abfd, (bfd_vma) in->ri_pad,
1804 (bfd_byte *) ex->ri_pad);
1805 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1806 (bfd_byte *) ex->ri_cprmask[0]);
1807 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1808 (bfd_byte *) ex->ri_cprmask[1]);
1809 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1810 (bfd_byte *) ex->ri_cprmask[2]);
1811 bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1812 (bfd_byte *) ex->ri_cprmask[3]);
1813 bfd_h_put_64 (abfd, (bfd_vma) in->ri_gp_value,
1814 (bfd_byte *) ex->ri_gp_value);
1815}
1816
1817/* Swap an entry in a .gptab section. Note that these routines rely
1818 on the equivalence of the two elements of the union. */
1819
1820static void
1821bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
1822 bfd *abfd;
1823 const Elf32_External_gptab *ex;
1824 Elf32_gptab *in;
1825{
1826 in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
1827 in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
1828}
1829
1830static void
1831bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
1832 bfd *abfd;
1833 const Elf32_gptab *in;
1834 Elf32_External_gptab *ex;
1835{
1836 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
1837 ex->gt_entry.gt_g_value);
1838 bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
1839 ex->gt_entry.gt_bytes);
1840}
1841
1842static void
1843bfd_elf32_swap_compact_rel_out (abfd, in, ex)
1844 bfd *abfd;
1845 const Elf32_compact_rel *in;
1846 Elf32_External_compact_rel *ex;
1847{
1848 bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1);
1849 bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num);
1850 bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2);
1851 bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset);
1852 bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0);
1853 bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1);
1854}
1855
1856static void
1857bfd_elf32_swap_crinfo_out (abfd, in, ex)
1858 bfd *abfd;
1859 const Elf32_crinfo *in;
1860 Elf32_External_crinfo *ex;
1861{
1862 unsigned long l;
1863
1864 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1865 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1866 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1867 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1868 bfd_h_put_32 (abfd, (bfd_vma) l, ex->info);
1869 bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst);
1870 bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr);
1871}
1872
1873/* Swap in an options header. */
1874
1875void
1876bfd_mips_elf_swap_options_in (abfd, ex, in)
1877 bfd *abfd;
1878 const Elf_External_Options *ex;
1879 Elf_Internal_Options *in;
1880{
1881 in->kind = bfd_h_get_8 (abfd, ex->kind);
1882 in->size = bfd_h_get_8 (abfd, ex->size);
1883 in->section = bfd_h_get_16 (abfd, ex->section);
1884 in->info = bfd_h_get_32 (abfd, ex->info);
1885}
1886
1887/* Swap out an options header. */
1888
1889void
1890bfd_mips_elf_swap_options_out (abfd, in, ex)
1891 bfd *abfd;
1892 const Elf_Internal_Options *in;
1893 Elf_External_Options *ex;
1894{
1895 bfd_h_put_8 (abfd, in->kind, ex->kind);
1896 bfd_h_put_8 (abfd, in->size, ex->size);
1897 bfd_h_put_16 (abfd, in->section, ex->section);
1898 bfd_h_put_32 (abfd, in->info, ex->info);
1899}
1900\f
1901/* Determine whether a symbol is global for the purposes of splitting
1902 the symbol table into global symbols and local symbols. At least
1903 on Irix 5, this split must be between section symbols and all other
1904 symbols. On most ELF targets the split is between static symbols
1905 and externally visible symbols. */
1906
1907/*ARGSUSED*/
1908static boolean
1909mips_elf_sym_is_global (abfd, sym)
1910 bfd *abfd;
1911 asymbol *sym;
1912{
1913 return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
1914}
1915\f
1916/* Set the right machine number for a MIPS ELF file. This is used for
1917 both the 32-bit and the 64-bit ABI. */
1918
1919boolean
1920_bfd_mips_elf_object_p (abfd)
1921 bfd *abfd;
1922{
1923 bfd_default_set_arch_mach (abfd, bfd_arch_mips,
1924 elf_mips_mach (elf_elfheader (abfd)->e_flags));
1925 return true;
1926}
1927
1928/* Set the right machine number for a 32-bit MIPS ELF file. */
1929
1930static boolean
1931mips_elf32_object_p (abfd)
1932 bfd *abfd;
1933{
1934 /* Irix 5 is broken. Object file symbol tables are not always
1935 sorted correctly such that local symbols precede global symbols,
1936 and the sh_info field in the symbol table is not always right. */
1937 elf_bad_symtab (abfd) = true;
1938
1939 return _bfd_mips_elf_object_p (abfd);
1940}
1941
1942/* The final processing done just before writing out a MIPS ELF object
1943 file. This gets the MIPS architecture right based on the machine
1944 number. This is used by both the 32-bit and the 64-bit ABI. */
1945
1946/*ARGSUSED*/
1947void
1948_bfd_mips_elf_final_write_processing (abfd, linker)
1949 bfd *abfd;
1950 boolean linker;
1951{
1952 unsigned long val;
1953 unsigned int i;
1954 Elf_Internal_Shdr **hdrpp;
1955 const char *name;
1956 asection *sec;
1957
1958 switch (bfd_get_mach (abfd))
1959 {
1960 default:
1961 case bfd_mach_mips3000:
1962 val = E_MIPS_ARCH_1;
1963 break;
1964
1965 case bfd_mach_mips3900:
1966 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
1967 break;
1968
1969 case bfd_mach_mips6000:
1970 val = E_MIPS_ARCH_2;
1971 break;
1972
1973 case bfd_mach_mips4000:
1974 case bfd_mach_mips4300:
1975 val = E_MIPS_ARCH_3;
1976 break;
1977
1978 case bfd_mach_mips4010:
1979 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
1980 break;
1981
1982 case bfd_mach_mips4100:
1983 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
1984 break;
1985
1986 case bfd_mach_mips4111:
1987 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
1988 break;
1989
1990 case bfd_mach_mips4650:
1991 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
1992 break;
1993
1994 case bfd_mach_mips8000:
1995 val = E_MIPS_ARCH_4;
1996 break;
1997 }
1998
1999 elf_elfheader (abfd)->e_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2000 elf_elfheader (abfd)->e_flags |= val;
2001
2002 /* Set the sh_info field for .gptab sections and other appropriate
2003 info for each special section. */
2004 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
2005 i < elf_elfheader (abfd)->e_shnum;
2006 i++, hdrpp++)
2007 {
2008 switch ((*hdrpp)->sh_type)
2009 {
2010 case SHT_MIPS_LIBLIST:
2011 sec = bfd_get_section_by_name (abfd, ".dynstr");
2012 if (sec != NULL)
2013 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2014 break;
2015
2016 case SHT_MIPS_GPTAB:
2017 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2018 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2019 BFD_ASSERT (name != NULL
2020 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
2021 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
2022 BFD_ASSERT (sec != NULL);
2023 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2024 break;
2025
2026 case SHT_MIPS_CONTENT:
2027 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2028 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2029 BFD_ASSERT (name != NULL
2030 && strncmp (name, ".MIPS.content",
2031 sizeof ".MIPS.content" - 1) == 0);
2032 sec = bfd_get_section_by_name (abfd,
2033 name + sizeof ".MIPS.content" - 1);
2034 BFD_ASSERT (sec != NULL);
3f830999 2035 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
252b5132
RH
2036 break;
2037
2038 case SHT_MIPS_SYMBOL_LIB:
2039 sec = bfd_get_section_by_name (abfd, ".dynsym");
2040 if (sec != NULL)
2041 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2042 sec = bfd_get_section_by_name (abfd, ".liblist");
2043 if (sec != NULL)
2044 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
2045 break;
2046
2047 case SHT_MIPS_EVENTS:
2048 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
2049 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
2050 BFD_ASSERT (name != NULL);
2051 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
2052 sec = bfd_get_section_by_name (abfd,
2053 name + sizeof ".MIPS.events" - 1);
2054 else
2055 {
2056 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
2057 sizeof ".MIPS.post_rel" - 1) == 0);
2058 sec = bfd_get_section_by_name (abfd,
2059 (name
2060 + sizeof ".MIPS.post_rel" - 1));
2061 }
2062 BFD_ASSERT (sec != NULL);
2063 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
2064 break;
2065
2066 }
2067 }
2068}
2069\f
2070/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
2071
2072boolean
2073_bfd_mips_elf_set_private_flags (abfd, flags)
2074 bfd *abfd;
2075 flagword flags;
2076{
2077 BFD_ASSERT (!elf_flags_init (abfd)
2078 || elf_elfheader (abfd)->e_flags == flags);
2079
2080 elf_elfheader (abfd)->e_flags = flags;
2081 elf_flags_init (abfd) = true;
2082 return true;
2083}
2084
2085/* Copy backend specific data from one object module to another */
2086
2087boolean
2088_bfd_mips_elf_copy_private_bfd_data (ibfd, obfd)
2089 bfd *ibfd;
2090 bfd *obfd;
2091{
2092 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2093 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2094 return true;
2095
2096 BFD_ASSERT (!elf_flags_init (obfd)
2097 || (elf_elfheader (obfd)->e_flags
2098 == elf_elfheader (ibfd)->e_flags));
2099
2100 elf_gp (obfd) = elf_gp (ibfd);
2101 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
2102 elf_flags_init (obfd) = true;
2103 return true;
2104}
2105
2106/* Merge backend specific data from an object file to the output
2107 object file when linking. */
2108
2109boolean
2110_bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
2111 bfd *ibfd;
2112 bfd *obfd;
2113{
2114 flagword old_flags;
2115 flagword new_flags;
2116 boolean ok;
2117
2118 /* Check if we have the same endianess */
2119 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2120 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2121 {
2122 const char *msg;
2123
2124 if (bfd_big_endian (ibfd))
2125 msg = _("%s: compiled for a big endian system and target is little endian");
2126 else
2127 msg = _("%s: compiled for a little endian system and target is big endian");
2128
2129 (*_bfd_error_handler) (msg, bfd_get_filename (ibfd));
2130
2131 bfd_set_error (bfd_error_wrong_format);
2132 return false;
2133 }
2134
2135 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2136 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2137 return true;
2138
2139 new_flags = elf_elfheader (ibfd)->e_flags;
2140 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
2141 old_flags = elf_elfheader (obfd)->e_flags;
2142
2143 if (! elf_flags_init (obfd))
2144 {
2145 elf_flags_init (obfd) = true;
2146 elf_elfheader (obfd)->e_flags = new_flags;
2147
2148 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2149 && bfd_get_arch_info (obfd)->the_default)
2150 {
2151 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2152 bfd_get_mach (ibfd)))
2153 return false;
2154 }
2155
2156 return true;
2157 }
2158
2159 /* Check flag compatibility. */
2160
2161 new_flags &= ~EF_MIPS_NOREORDER;
2162 old_flags &= ~EF_MIPS_NOREORDER;
2163
2164 if (new_flags == old_flags)
2165 return true;
2166
2167 ok = true;
2168
2169 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
2170 {
2171 new_flags &= ~EF_MIPS_PIC;
2172 old_flags &= ~EF_MIPS_PIC;
2173 (*_bfd_error_handler)
2174 (_("%s: linking PIC files with non-PIC files"),
2175 bfd_get_filename (ibfd));
2176 ok = false;
2177 }
2178
2179 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
2180 {
2181 new_flags &= ~EF_MIPS_CPIC;
2182 old_flags &= ~EF_MIPS_CPIC;
2183 (*_bfd_error_handler)
2184 (_("%s: linking abicalls files with non-abicalls files"),
2185 bfd_get_filename (ibfd));
2186 ok = false;
2187 }
2188
2189 /* Compare the ISA's. */
2190 if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
2191 != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
2192 {
2193 int new_mach = new_flags & EF_MIPS_MACH;
2194 int old_mach = old_flags & EF_MIPS_MACH;
2195 int new_isa = elf_mips_isa (new_flags);
2196 int old_isa = elf_mips_isa (old_flags);
2197
2198 /* If either has no machine specified, just compare the general isa's.
2199 Some combinations of machines are ok, if the isa's match. */
2200 if (! new_mach
2201 || ! old_mach
2202 || new_mach == old_mach
2203 )
2204 {
2205 /* Don't warn about mixing -mips1 and -mips2 code, or mixing -mips3
2206 and -mips4 code. They will normally use the same data sizes and
2207 calling conventions. */
2208
2209 if ((new_isa == 1 || new_isa == 2)
2210 ? (old_isa != 1 && old_isa != 2)
2211 : (old_isa == 1 || old_isa == 2))
2212 {
2213 (*_bfd_error_handler)
2214 (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
2215 bfd_get_filename (ibfd), new_isa, old_isa);
2216 ok = false;
2217 }
2218 }
2219
2220 else
2221 {
2222 (*_bfd_error_handler)
2223 (_("%s: ISA mismatch (%d) with previous modules (%d)"),
2224 bfd_get_filename (ibfd),
2225 elf_mips_mach (new_flags),
2226 elf_mips_mach (old_flags));
2227 ok = false;
2228 }
2229
2230 new_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2231 old_flags &= ~ (EF_MIPS_ARCH | EF_MIPS_MACH);
2232 }
2233
2234 /* Compare ABI's */
2235 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI))
2236 {
2237 /* Only error if both are set (to different values). */
2238 if ((new_flags & EF_MIPS_ABI)
2239 && (old_flags & EF_MIPS_ABI))
2240 {
2241 (*_bfd_error_handler)
2242 (_("%s: ABI mismatch: linking %s module with previous %s modules"),
2243 bfd_get_filename (ibfd),
2244 elf_mips_abi_name (new_flags),
2245 elf_mips_abi_name (old_flags));
2246 ok = false;
2247 }
2248 new_flags &= ~EF_MIPS_ABI;
2249 old_flags &= ~EF_MIPS_ABI;
2250 }
2251
2252 /* Warn about any other mismatches */
2253 if (new_flags != old_flags)
2254 {
2255 (*_bfd_error_handler)
2256 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2257 bfd_get_filename (ibfd), (unsigned long) new_flags,
2258 (unsigned long) old_flags);
2259 ok = false;
2260 }
2261
2262 if (! ok)
2263 {
2264 bfd_set_error (bfd_error_bad_value);
2265 return false;
2266 }
2267
2268 return true;
2269}
2270\f
2271static boolean
2272_bfd_mips_elf_print_private_bfd_data (abfd, ptr)
2273 bfd *abfd;
2274 PTR ptr;
2275{
2276 FILE *file = (FILE *) ptr;
2277
2278 BFD_ASSERT (abfd != NULL && ptr != NULL);
2279
2280 /* Print normal ELF private data. */
2281 _bfd_elf_print_private_bfd_data (abfd, ptr);
2282
2283 /* xgettext:c-format */
2284 fprintf (file, _ ("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
2285
2286 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
2287 fprintf (file, _ (" [abi=O32]"));
2288 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
2289 fprintf (file, _ (" [abi=O64]"));
2290 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
2291 fprintf (file, _ (" [abi=EABI32]"));
2292 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
2293 fprintf (file, _ (" [abi=EABI64]"));
2294 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
2295 fprintf (file, _ (" [abi unknown]"));
2296 else
2297 fprintf (file, _ (" [no abi set]"));
2298
2299 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
2300 fprintf (file, _ (" [mips1]"));
2301 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
2302 fprintf (file, _ (" [mips2]"));
2303 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
2304 fprintf (file, _ (" [mips3]"));
2305 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
2306 fprintf (file, _ (" [mips4]"));
2307 else
2308 fprintf (file, _ (" [unknown ISA]"));
2309
2310 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
2311 fprintf (file, _ (" [32bitmode]"));
2312 else
2313 fprintf (file, _ (" [not 32bitmode]"));
2314
2315 fputc ('\n', file);
2316
2317 return true;
2318}
2319\f
2320/* Handle a MIPS specific section when reading an object file. This
2321 is called when elfcode.h finds a section with an unknown type.
2322 This routine supports both the 32-bit and 64-bit ELF ABI.
2323
2324 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
2325 how to. */
2326
2327boolean
2328_bfd_mips_elf_section_from_shdr (abfd, hdr, name)
2329 bfd *abfd;
2330 Elf_Internal_Shdr *hdr;
2331 const char *name;
2332{
2333 flagword flags = 0;
2334
2335 /* There ought to be a place to keep ELF backend specific flags, but
2336 at the moment there isn't one. We just keep track of the
2337 sections by their name, instead. Fortunately, the ABI gives
2338 suggested names for all the MIPS specific sections, so we will
2339 probably get away with this. */
2340 switch (hdr->sh_type)
2341 {
2342 case SHT_MIPS_LIBLIST:
2343 if (strcmp (name, ".liblist") != 0)
2344 return false;
2345 break;
2346 case SHT_MIPS_MSYM:
2347 if (strcmp (name, ".msym") != 0)
2348 return false;
2349 break;
2350 case SHT_MIPS_CONFLICT:
2351 if (strcmp (name, ".conflict") != 0)
2352 return false;
2353 break;
2354 case SHT_MIPS_GPTAB:
2355 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
2356 return false;
2357 break;
2358 case SHT_MIPS_UCODE:
2359 if (strcmp (name, ".ucode") != 0)
2360 return false;
2361 break;
2362 case SHT_MIPS_DEBUG:
2363 if (strcmp (name, ".mdebug") != 0)
2364 return false;
2365 flags = SEC_DEBUGGING;
2366 break;
2367 case SHT_MIPS_REGINFO:
2368 if (strcmp (name, ".reginfo") != 0
2369 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
2370 return false;
2371 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
2372 break;
2373 case SHT_MIPS_IFACE:
2374 if (strcmp (name, ".MIPS.interfaces") != 0)
2375 return false;
2376 break;
2377 case SHT_MIPS_CONTENT:
2378 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
2379 return false;
2380 break;
2381 case SHT_MIPS_OPTIONS:
2382 if (strcmp (name, ".options") != 0
2383 && strcmp (name, ".MIPS.options") != 0)
2384 return false;
2385 break;
2386 case SHT_MIPS_DWARF:
2387 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
2388 return false;
2389 break;
2390 case SHT_MIPS_SYMBOL_LIB:
2391 if (strcmp (name, ".MIPS.symlib") != 0)
2392 return false;
2393 break;
2394 case SHT_MIPS_EVENTS:
2395 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
2396 && strncmp (name, ".MIPS.post_rel",
2397 sizeof ".MIPS.post_rel" - 1) != 0)
2398 return false;
2399 break;
2400 default:
2401 return false;
2402 }
2403
2404 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2405 return false;
2406
2407 if (flags)
2408 {
2409 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2410 (bfd_get_section_flags (abfd,
2411 hdr->bfd_section)
2412 | flags)))
2413 return false;
2414 }
2415
2416 return true;
2417}
2418
2419/* Handle a 32-bit MIPS ELF specific section. */
2420
2421static boolean
2422mips_elf32_section_from_shdr (abfd, hdr, name)
2423 bfd *abfd;
2424 Elf_Internal_Shdr *hdr;
2425 char *name;
2426{
2427 if (! _bfd_mips_elf_section_from_shdr (abfd, hdr, name))
2428 return false;
2429
2430 /* FIXME: We should record sh_info for a .gptab section. */
2431
2432 /* For a .reginfo section, set the gp value in the tdata information
2433 from the contents of this section. We need the gp value while
2434 processing relocs, so we just get it now. The .reginfo section
2435 is not used in the 64-bit MIPS ELF ABI. */
2436 if (hdr->sh_type == SHT_MIPS_REGINFO)
2437 {
2438 Elf32_External_RegInfo ext;
2439 Elf32_RegInfo s;
2440
2441 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
2442 (file_ptr) 0, sizeof ext))
2443 return false;
2444 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
2445 elf_gp (abfd) = s.ri_gp_value;
2446 }
2447
2448 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
2449 set the gp value based on what we find. We may see both
2450 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
2451 they should agree. */
2452 if (hdr->sh_type == SHT_MIPS_OPTIONS)
2453 {
2454 bfd_byte *contents, *l, *lend;
2455
2456 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
2457 if (contents == NULL)
2458 return false;
2459 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
2460 (file_ptr) 0, hdr->sh_size))
2461 {
2462 free (contents);
2463 return false;
2464 }
2465 l = contents;
2466 lend = contents + hdr->sh_size;
2467 while (l + sizeof (Elf_External_Options) <= lend)
2468 {
2469 Elf_Internal_Options intopt;
2470
2471 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2472 &intopt);
2473 if (intopt.kind == ODK_REGINFO)
2474 {
2475 Elf32_RegInfo intreg;
2476
2477 bfd_mips_elf32_swap_reginfo_in
2478 (abfd,
2479 ((Elf32_External_RegInfo *)
2480 (l + sizeof (Elf_External_Options))),
2481 &intreg);
2482 elf_gp (abfd) = intreg.ri_gp_value;
2483 }
2484 l += intopt.size;
2485 }
2486 free (contents);
2487 }
2488
2489 return true;
2490}
2491
2492/* Set the correct type for a MIPS ELF section. We do this by the
2493 section name, which is a hack, but ought to work. This routine is
2494 used by both the 32-bit and the 64-bit ABI. */
2495
2496boolean
2497_bfd_mips_elf_fake_sections (abfd, hdr, sec)
2498 bfd *abfd;
2499 Elf32_Internal_Shdr *hdr;
2500 asection *sec;
2501{
2502 register const char *name;
2503
2504 name = bfd_get_section_name (abfd, sec);
2505
2506 if (strcmp (name, ".liblist") == 0)
2507 {
2508 hdr->sh_type = SHT_MIPS_LIBLIST;
2509 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
2510 /* The sh_link field is set in final_write_processing. */
2511 }
2512 else if (strcmp (name, ".msym") == 0)
2513 {
2514 hdr->sh_type = SHT_MIPS_MSYM;
2515 hdr->sh_entsize = 8;
2516 /* FIXME: Set the sh_info field. */
2517 }
2518 else if (strcmp (name, ".conflict") == 0)
2519 hdr->sh_type = SHT_MIPS_CONFLICT;
2520 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
2521 {
2522 hdr->sh_type = SHT_MIPS_GPTAB;
2523 hdr->sh_entsize = sizeof (Elf32_External_gptab);
2524 /* The sh_info field is set in final_write_processing. */
2525 }
2526 else if (strcmp (name, ".ucode") == 0)
2527 hdr->sh_type = SHT_MIPS_UCODE;
2528 else if (strcmp (name, ".mdebug") == 0)
2529 {
2530 hdr->sh_type = SHT_MIPS_DEBUG;
2531 /* In a shared object on Irix 5.3, the .mdebug section has an
2532 entsize of 0. FIXME: Does this matter? */
2533 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2534 hdr->sh_entsize = 0;
2535 else
2536 hdr->sh_entsize = 1;
2537 }
2538 else if (strcmp (name, ".reginfo") == 0)
2539 {
2540 hdr->sh_type = SHT_MIPS_REGINFO;
2541 /* In a shared object on Irix 5.3, the .reginfo section has an
2542 entsize of 0x18. FIXME: Does this matter? */
2543 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2544 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
2545 else
2546 hdr->sh_entsize = 1;
2547 }
2548 else if (SGI_COMPAT (abfd)
2549 && (strcmp (name, ".hash") == 0
2550 || strcmp (name, ".dynamic") == 0
2551 || strcmp (name, ".dynstr") == 0))
2552 {
2553 hdr->sh_entsize = 0;
2554#if 0
2555 /* This isn't how the Irix 6 linker behaves. */
2556 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
2557#endif
2558 }
2559 else if (strcmp (name, ".got") == 0
2560 || strcmp (name, ".sdata") == 0
2561 || strcmp (name, ".sbss") == 0
2562 || strcmp (name, ".lit4") == 0
2563 || strcmp (name, ".lit8") == 0)
2564 hdr->sh_flags |= SHF_MIPS_GPREL;
2565 else if (strcmp (name, ".MIPS.interfaces") == 0)
2566 {
2567 hdr->sh_type = SHT_MIPS_IFACE;
2568 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2569 }
3f830999 2570 else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
252b5132
RH
2571 {
2572 hdr->sh_type = SHT_MIPS_CONTENT;
3f830999 2573 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
252b5132
RH
2574 /* The sh_info field is set in final_write_processing. */
2575 }
2576 else if (strcmp (name, ".options") == 0
2577 || strcmp (name, ".MIPS.options") == 0)
2578 {
2579 hdr->sh_type = SHT_MIPS_OPTIONS;
2580 hdr->sh_entsize = 1;
2581 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2582 }
2583 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
2584 hdr->sh_type = SHT_MIPS_DWARF;
2585 else if (strcmp (name, ".MIPS.symlib") == 0)
2586 {
2587 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
2588 /* The sh_link and sh_info fields are set in
2589 final_write_processing. */
2590 }
2591 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
2592 || strncmp (name, ".MIPS.post_rel",
2593 sizeof ".MIPS.post_rel" - 1) == 0)
2594 {
2595 hdr->sh_type = SHT_MIPS_EVENTS;
2596 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2597 /* The sh_link field is set in final_write_processing. */
2598 }
2599
2600 return true;
2601}
2602
2603/* Given a BFD section, try to locate the corresponding ELF section
2604 index. This is used by both the 32-bit and the 64-bit ABI.
2605 Actually, it's not clear to me that the 64-bit ABI supports these,
2606 but for non-PIC objects we will certainly want support for at least
2607 the .scommon section. */
2608
2609boolean
2610_bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
2611 bfd *abfd;
2612 Elf32_Internal_Shdr *hdr;
2613 asection *sec;
2614 int *retval;
2615{
2616 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2617 {
2618 *retval = SHN_MIPS_SCOMMON;
2619 return true;
2620 }
2621 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
2622 {
2623 *retval = SHN_MIPS_ACOMMON;
2624 return true;
2625 }
2626 return false;
2627}
2628
2629/* When are writing out the .options or .MIPS.options section,
2630 remember the bytes we are writing out, so that we can install the
2631 GP value in the section_processing routine. */
2632
2633boolean
2634_bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
2635 bfd *abfd;
2636 sec_ptr section;
2637 PTR location;
2638 file_ptr offset;
2639 bfd_size_type count;
2640{
2641 if (strcmp (section->name, ".options") == 0
2642 || strcmp (section->name, ".MIPS.options") == 0)
2643 {
2644 bfd_byte *c;
2645
2646 if (elf_section_data (section) == NULL)
2647 {
2648 section->used_by_bfd =
2649 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
2650 if (elf_section_data (section) == NULL)
2651 return false;
2652 }
2653 c = (bfd_byte *) elf_section_data (section)->tdata;
2654 if (c == NULL)
2655 {
2656 bfd_size_type size;
2657
2658 if (section->_cooked_size != 0)
2659 size = section->_cooked_size;
2660 else
2661 size = section->_raw_size;
2662 c = (bfd_byte *) bfd_zalloc (abfd, size);
2663 if (c == NULL)
2664 return false;
2665 elf_section_data (section)->tdata = (PTR) c;
2666 }
2667
2668 memcpy (c + offset, location, count);
2669 }
2670
2671 return _bfd_elf_set_section_contents (abfd, section, location, offset,
2672 count);
2673}
2674
2675/* Work over a section just before writing it out. This routine is
2676 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
2677 sections that need the SHF_MIPS_GPREL flag by name; there has to be
2678 a better way. */
2679
2680boolean
2681_bfd_mips_elf_section_processing (abfd, hdr)
2682 bfd *abfd;
2683 Elf_Internal_Shdr *hdr;
2684{
2685 if (hdr->bfd_section != NULL)
2686 {
2687 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
2688
2689 if (strcmp (name, ".sdata") == 0)
2690 {
2691 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2692 hdr->sh_type = SHT_PROGBITS;
2693 }
2694 else if (strcmp (name, ".sbss") == 0)
2695 {
2696 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2697 hdr->sh_type = SHT_NOBITS;
2698 }
2699 else if (strcmp (name, ".lit8") == 0
2700 || strcmp (name, ".lit4") == 0)
2701 {
2702 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2703 hdr->sh_type = SHT_PROGBITS;
2704 }
2705 else if (strcmp (name, ".compact_rel") == 0)
2706 {
2707 hdr->sh_flags = 0;
2708 hdr->sh_type = SHT_PROGBITS;
2709 }
2710 else if (strcmp (name, ".rtproc") == 0)
2711 {
2712 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
2713 {
2714 unsigned int adjust;
2715
2716 adjust = hdr->sh_size % hdr->sh_addralign;
2717 if (adjust != 0)
2718 hdr->sh_size += hdr->sh_addralign - adjust;
2719 }
2720 }
2721 }
2722
2723 return true;
2724}
2725
2726/* Work over a section just before writing it out. We update the GP
2727 value in the SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS sections based
2728 on the value we are using. */
2729
2730static boolean
2731mips_elf32_section_processing (abfd, hdr)
2732 bfd *abfd;
2733 Elf32_Internal_Shdr *hdr;
2734{
2735 if (hdr->sh_type == SHT_MIPS_REGINFO)
2736 {
2737 bfd_byte buf[4];
2738
2739 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
2740 BFD_ASSERT (hdr->contents == NULL);
2741
2742 if (bfd_seek (abfd,
2743 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
2744 SEEK_SET) == -1)
2745 return false;
2746 bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
2747 if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
2748 return false;
2749 }
2750
2751 if (hdr->sh_type == SHT_MIPS_OPTIONS
2752 && hdr->bfd_section != NULL
2753 && elf_section_data (hdr->bfd_section) != NULL
2754 && elf_section_data (hdr->bfd_section)->tdata != NULL)
2755 {
2756 bfd_byte *contents, *l, *lend;
2757
2758 /* We stored the section contents in the elf_section_data tdata
2759 field in the set_section_contents routine. We save the
2760 section contents so that we don't have to read them again.
2761 At this point we know that elf_gp is set, so we can look
2762 through the section contents to see if there is an
2763 ODK_REGINFO structure. */
2764
2765 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
2766 l = contents;
2767 lend = contents + hdr->sh_size;
2768 while (l + sizeof (Elf_External_Options) <= lend)
2769 {
2770 Elf_Internal_Options intopt;
2771
2772 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2773 &intopt);
2774 if (intopt.kind == ODK_REGINFO)
2775 {
2776 bfd_byte buf[4];
2777
2778 if (bfd_seek (abfd,
2779 (hdr->sh_offset
2780 + (l - contents)
2781 + sizeof (Elf_External_Options)
2782 + (sizeof (Elf32_External_RegInfo) - 4)),
2783 SEEK_SET) == -1)
2784 return false;
2785 bfd_h_put_32 (abfd, elf_gp (abfd), buf);
2786 if (bfd_write (buf, 1, 4, abfd) != 4)
2787 return false;
2788 }
2789 l += intopt.size;
2790 }
2791 }
2792
2793 return _bfd_mips_elf_section_processing (abfd, hdr);
2794}
2795\f
2796/* MIPS ELF uses two common sections. One is the usual one, and the
2797 other is for small objects. All the small objects are kept
2798 together, and then referenced via the gp pointer, which yields
2799 faster assembler code. This is what we use for the small common
2800 section. This approach is copied from ecoff.c. */
2801static asection mips_elf_scom_section;
2802static asymbol mips_elf_scom_symbol;
2803static asymbol *mips_elf_scom_symbol_ptr;
2804
2805/* MIPS ELF also uses an acommon section, which represents an
2806 allocated common symbol which may be overridden by a
2807 definition in a shared library. */
2808static asection mips_elf_acom_section;
2809static asymbol mips_elf_acom_symbol;
2810static asymbol *mips_elf_acom_symbol_ptr;
2811
2812/* The Irix 5 support uses two virtual sections, which represent
2813 text/data symbols defined in dynamic objects. */
2814static asection mips_elf_text_section;
2815static asection *mips_elf_text_section_ptr;
2816static asymbol mips_elf_text_symbol;
2817static asymbol *mips_elf_text_symbol_ptr;
2818
2819static asection mips_elf_data_section;
2820static asection *mips_elf_data_section_ptr;
2821static asymbol mips_elf_data_symbol;
2822static asymbol *mips_elf_data_symbol_ptr;
2823
2824/* Handle the special MIPS section numbers that a symbol may use.
2825 This is used for both the 32-bit and the 64-bit ABI. */
2826
2827void
2828_bfd_mips_elf_symbol_processing (abfd, asym)
2829 bfd *abfd;
2830 asymbol *asym;
2831{
2832 elf_symbol_type *elfsym;
2833
2834 elfsym = (elf_symbol_type *) asym;
2835 switch (elfsym->internal_elf_sym.st_shndx)
2836 {
2837 case SHN_MIPS_ACOMMON:
2838 /* This section is used in a dynamically linked executable file.
2839 It is an allocated common section. The dynamic linker can
2840 either resolve these symbols to something in a shared
2841 library, or it can just leave them here. For our purposes,
2842 we can consider these symbols to be in a new section. */
2843 if (mips_elf_acom_section.name == NULL)
2844 {
2845 /* Initialize the acommon section. */
2846 mips_elf_acom_section.name = ".acommon";
2847 mips_elf_acom_section.flags = SEC_ALLOC;
2848 mips_elf_acom_section.output_section = &mips_elf_acom_section;
2849 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
2850 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
2851 mips_elf_acom_symbol.name = ".acommon";
2852 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
2853 mips_elf_acom_symbol.section = &mips_elf_acom_section;
2854 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
2855 }
2856 asym->section = &mips_elf_acom_section;
2857 break;
2858
2859 case SHN_COMMON:
2860 /* Common symbols less than the GP size are automatically
2861 treated as SHN_MIPS_SCOMMON symbols. */
2862 if (asym->value > elf_gp_size (abfd))
2863 break;
2864 /* Fall through. */
2865 case SHN_MIPS_SCOMMON:
2866 if (mips_elf_scom_section.name == NULL)
2867 {
2868 /* Initialize the small common section. */
2869 mips_elf_scom_section.name = ".scommon";
2870 mips_elf_scom_section.flags = SEC_IS_COMMON;
2871 mips_elf_scom_section.output_section = &mips_elf_scom_section;
2872 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
2873 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
2874 mips_elf_scom_symbol.name = ".scommon";
2875 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
2876 mips_elf_scom_symbol.section = &mips_elf_scom_section;
2877 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
2878 }
2879 asym->section = &mips_elf_scom_section;
2880 asym->value = elfsym->internal_elf_sym.st_size;
2881 break;
2882
2883 case SHN_MIPS_SUNDEFINED:
2884 asym->section = bfd_und_section_ptr;
2885 break;
2886
2887#if 0 /* for SGI_COMPAT */
2888 case SHN_MIPS_TEXT:
2889 asym->section = mips_elf_text_section_ptr;
2890 break;
2891
2892 case SHN_MIPS_DATA:
2893 asym->section = mips_elf_data_section_ptr;
2894 break;
2895#endif
2896 }
2897}
2898\f
2899/* When creating an Irix 5 executable, we need REGINFO and RTPROC
2900 segments. */
2901
2902static int
2903mips_elf_additional_program_headers (abfd)
2904 bfd *abfd;
2905{
2906 asection *s;
2907 int ret;
2908
2909 ret = 0;
2910
2911 if (! SGI_COMPAT (abfd))
2912 return ret;
2913
2914 s = bfd_get_section_by_name (abfd, ".reginfo");
2915 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2916 {
2917 /* We need a PT_MIPS_REGINFO segment. */
2918 ++ret;
2919 }
2920
2921 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL
2922 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2923 {
2924 /* We need a PT_MIPS_RTPROC segment. */
2925 ++ret;
2926 }
2927
2928 return ret;
2929}
2930
2931/* Modify the segment map for an Irix 5 executable. */
2932
2933static boolean
2934mips_elf_modify_segment_map (abfd)
2935 bfd *abfd;
2936{
2937 asection *s;
2938 struct elf_segment_map *m, **pm;
2939
2940 if (! SGI_COMPAT (abfd))
2941 return true;
2942
2943 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
2944 segment. */
2945 s = bfd_get_section_by_name (abfd, ".reginfo");
2946 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2947 {
2948 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2949 if (m->p_type == PT_MIPS_REGINFO)
2950 break;
2951 if (m == NULL)
2952 {
2953 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2954 if (m == NULL)
2955 return false;
2956
2957 m->p_type = PT_MIPS_REGINFO;
2958 m->count = 1;
2959 m->sections[0] = s;
2960
2961 /* We want to put it after the PHDR and INTERP segments. */
2962 pm = &elf_tdata (abfd)->segment_map;
2963 while (*pm != NULL
2964 && ((*pm)->p_type == PT_PHDR
2965 || (*pm)->p_type == PT_INTERP))
2966 pm = &(*pm)->next;
2967
2968 m->next = *pm;
2969 *pm = m;
2970 }
2971 }
2972
2973 /* If there are .dynamic and .mdebug sections, we make a room for
2974 the RTPROC header. FIXME: Rewrite without section names. */
2975 if (bfd_get_section_by_name (abfd, ".interp") == NULL
2976 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
2977 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2978 {
2979 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2980 if (m->p_type == PT_MIPS_RTPROC)
2981 break;
2982 if (m == NULL)
2983 {
2984 m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2985 if (m == NULL)
2986 return false;
2987
2988 m->p_type = PT_MIPS_RTPROC;
2989
2990 s = bfd_get_section_by_name (abfd, ".rtproc");
2991 if (s == NULL)
2992 {
2993 m->count = 0;
2994 m->p_flags = 0;
2995 m->p_flags_valid = 1;
2996 }
2997 else
2998 {
2999 m->count = 1;
3000 m->sections[0] = s;
3001 }
3002
3003 /* We want to put it after the DYNAMIC segment. */
3004 pm = &elf_tdata (abfd)->segment_map;
3005 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
3006 pm = &(*pm)->next;
3007 if (*pm != NULL)
3008 pm = &(*pm)->next;
3009
3010 m->next = *pm;
3011 *pm = m;
3012 }
3013 }
3014
3015 /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic, .dynstr,
3016 .dynsym, and .hash sections, and everything in between. */
3017 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
3018 if ((*pm)->p_type == PT_DYNAMIC)
3019 break;
3020 m = *pm;
3021 if (m != NULL
3022 && m->count == 1
3023 && strcmp (m->sections[0]->name, ".dynamic") == 0)
3024 {
3025 static const char *sec_names[] =
3026 { ".dynamic", ".dynstr", ".dynsym", ".hash" };
3027 bfd_vma low, high;
3028 unsigned int i, c;
3029 struct elf_segment_map *n;
3030
3031 low = 0xffffffff;
3032 high = 0;
3033 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
3034 {
3035 s = bfd_get_section_by_name (abfd, sec_names[i]);
3036 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3037 {
3038 bfd_size_type sz;
3039
3040 if (low > s->vma)
3041 low = s->vma;
3042 sz = s->_cooked_size;
3043 if (sz == 0)
3044 sz = s->_raw_size;
3045 if (high < s->vma + sz)
3046 high = s->vma + sz;
3047 }
3048 }
3049
3050 c = 0;
3051 for (s = abfd->sections; s != NULL; s = s->next)
3052 if ((s->flags & SEC_LOAD) != 0
3053 && s->vma >= low
3054 && ((s->vma
3055 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3056 <= high))
3057 ++c;
3058
3059 n = ((struct elf_segment_map *)
3060 bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
3061 if (n == NULL)
3062 return false;
3063 *n = *m;
3064 n->count = c;
3065
3066 i = 0;
3067 for (s = abfd->sections; s != NULL; s = s->next)
3068 {
3069 if ((s->flags & SEC_LOAD) != 0
3070 && s->vma >= low
3071 && ((s->vma
3072 + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
3073 <= high))
3074 {
3075 n->sections[i] = s;
3076 ++i;
3077 }
3078 }
3079
3080 *pm = n;
3081 }
3082
3083 return true;
3084}
3085\f
3086/* The structure of the runtime procedure descriptor created by the
3087 loader for use by the static exception system. */
3088
3089typedef struct runtime_pdr {
3090 bfd_vma adr; /* memory address of start of procedure */
3091 long regmask; /* save register mask */
3092 long regoffset; /* save register offset */
3093 long fregmask; /* save floating point register mask */
3094 long fregoffset; /* save floating point register offset */
3095 long frameoffset; /* frame size */
3096 short framereg; /* frame pointer register */
3097 short pcreg; /* offset or reg of return pc */
3098 long irpss; /* index into the runtime string table */
3099 long reserved;
3100 struct exception_info *exception_info;/* pointer to exception array */
3101} RPDR, *pRPDR;
3102#define cbRPDR sizeof(RPDR)
3103#define rpdNil ((pRPDR) 0)
3104
3105/* Swap RPDR (runtime procedure table entry) for output. */
3106
3107static void ecoff_swap_rpdr_out
3108 PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
3109
3110static void
3111ecoff_swap_rpdr_out (abfd, in, ex)
3112 bfd *abfd;
3113 const RPDR *in;
3114 struct rpdr_ext *ex;
3115{
3116 /* ecoff_put_off was defined in ecoffswap.h. */
3117 ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
3118 bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
3119 bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
3120 bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
3121 bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
3122 bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
3123
3124 bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
3125 bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
3126
3127 bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
3128#if 0 /* FIXME */
3129 ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
3130#endif
3131}
3132\f
3133/* Read ECOFF debugging information from a .mdebug section into a
3134 ecoff_debug_info structure. */
3135
3136boolean
3137_bfd_mips_elf_read_ecoff_info (abfd, section, debug)
3138 bfd *abfd;
3139 asection *section;
3140 struct ecoff_debug_info *debug;
3141{
3142 HDRR *symhdr;
3143 const struct ecoff_debug_swap *swap;
3144 char *ext_hdr = NULL;
3145
3146 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3147 memset (debug, 0, sizeof(*debug));
3148
3149 ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
3150 if (ext_hdr == NULL && swap->external_hdr_size != 0)
3151 goto error_return;
3152
3153 if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
3154 swap->external_hdr_size)
3155 == false)
3156 goto error_return;
3157
3158 symhdr = &debug->symbolic_header;
3159 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
3160
3161 /* The symbolic header contains absolute file offsets and sizes to
3162 read. */
3163#define READ(ptr, offset, count, size, type) \
3164 if (symhdr->count == 0) \
3165 debug->ptr = NULL; \
3166 else \
3167 { \
3168 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
3169 if (debug->ptr == NULL) \
3170 goto error_return; \
3171 if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
3172 || (bfd_read (debug->ptr, size, symhdr->count, \
3173 abfd) != size * symhdr->count)) \
3174 goto error_return; \
3175 }
3176
3177 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
3178 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
3179 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
3180 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
3181 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
3182 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
3183 union aux_ext *);
3184 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
3185 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
3186 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
3187 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
3188 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
3189#undef READ
3190
3191 debug->fdr = NULL;
3192 debug->adjust = NULL;
3193
3194 return true;
3195
3196 error_return:
3197 if (ext_hdr != NULL)
3198 free (ext_hdr);
3199 if (debug->line != NULL)
3200 free (debug->line);
3201 if (debug->external_dnr != NULL)
3202 free (debug->external_dnr);
3203 if (debug->external_pdr != NULL)
3204 free (debug->external_pdr);
3205 if (debug->external_sym != NULL)
3206 free (debug->external_sym);
3207 if (debug->external_opt != NULL)
3208 free (debug->external_opt);
3209 if (debug->external_aux != NULL)
3210 free (debug->external_aux);
3211 if (debug->ss != NULL)
3212 free (debug->ss);
3213 if (debug->ssext != NULL)
3214 free (debug->ssext);
3215 if (debug->external_fdr != NULL)
3216 free (debug->external_fdr);
3217 if (debug->external_rfd != NULL)
3218 free (debug->external_rfd);
3219 if (debug->external_ext != NULL)
3220 free (debug->external_ext);
3221 return false;
3222}
3223\f
3224/* MIPS ELF local labels start with '$', not 'L'. */
3225
3226/*ARGSUSED*/
3227static boolean
3228mips_elf_is_local_label_name (abfd, name)
3229 bfd *abfd;
3230 const char *name;
3231{
3232 if (name[0] == '$')
3233 return true;
3234
3235 /* On Irix 6, the labels go back to starting with '.', so we accept
3236 the generic ELF local label syntax as well. */
3237 return _bfd_elf_is_local_label_name (abfd, name);
3238}
3239
3240/* MIPS ELF uses a special find_nearest_line routine in order the
3241 handle the ECOFF debugging information. */
3242
3243struct mips_elf_find_line
3244{
3245 struct ecoff_debug_info d;
3246 struct ecoff_find_line i;
3247};
3248
3249boolean
3250_bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
3251 functionname_ptr, line_ptr)
3252 bfd *abfd;
3253 asection *section;
3254 asymbol **symbols;
3255 bfd_vma offset;
3256 const char **filename_ptr;
3257 const char **functionname_ptr;
3258 unsigned int *line_ptr;
3259{
3260 asection *msec;
3261
3262 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
3263 filename_ptr, functionname_ptr,
3264 line_ptr))
3265 return true;
3266
3267 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
3268 filename_ptr, functionname_ptr,
3269 line_ptr))
3270 return true;
3271
3272 msec = bfd_get_section_by_name (abfd, ".mdebug");
3273 if (msec != NULL)
3274 {
3275 flagword origflags;
3276 struct mips_elf_find_line *fi;
3277 const struct ecoff_debug_swap * const swap =
3278 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3279
3280 /* If we are called during a link, mips_elf_final_link may have
3281 cleared the SEC_HAS_CONTENTS field. We force it back on here
3282 if appropriate (which it normally will be). */
3283 origflags = msec->flags;
3284 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
3285 msec->flags |= SEC_HAS_CONTENTS;
3286
3287 fi = elf_tdata (abfd)->find_line_info;
3288 if (fi == NULL)
3289 {
3290 bfd_size_type external_fdr_size;
3291 char *fraw_src;
3292 char *fraw_end;
3293 struct fdr *fdr_ptr;
3294
3295 fi = ((struct mips_elf_find_line *)
3296 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
3297 if (fi == NULL)
3298 {
3299 msec->flags = origflags;
3300 return false;
3301 }
3302
3303 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
3304 {
3305 msec->flags = origflags;
3306 return false;
3307 }
3308
3309 /* Swap in the FDR information. */
3310 fi->d.fdr = ((struct fdr *)
3311 bfd_alloc (abfd,
3312 (fi->d.symbolic_header.ifdMax *
3313 sizeof (struct fdr))));
3314 if (fi->d.fdr == NULL)
3315 {
3316 msec->flags = origflags;
3317 return false;
3318 }
3319 external_fdr_size = swap->external_fdr_size;
3320 fdr_ptr = fi->d.fdr;
3321 fraw_src = (char *) fi->d.external_fdr;
3322 fraw_end = (fraw_src
3323 + fi->d.symbolic_header.ifdMax * external_fdr_size);
3324 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
3325 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
3326
3327 elf_tdata (abfd)->find_line_info = fi;
3328
3329 /* Note that we don't bother to ever free this information.
3330 find_nearest_line is either called all the time, as in
3331 objdump -l, so the information should be saved, or it is
3332 rarely called, as in ld error messages, so the memory
3333 wasted is unimportant. Still, it would probably be a
3334 good idea for free_cached_info to throw it away. */
3335 }
3336
3337 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
3338 &fi->i, filename_ptr, functionname_ptr,
3339 line_ptr))
3340 {
3341 msec->flags = origflags;
3342 return true;
3343 }
3344
3345 msec->flags = origflags;
3346 }
3347
3348 /* Fall back on the generic ELF find_nearest_line routine. */
3349
3350 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
3351 filename_ptr, functionname_ptr,
3352 line_ptr);
3353}
3354\f
3355 /* The mips16 compiler uses a couple of special sections to handle
3356 floating point arguments.
3357
3358 Section names that look like .mips16.fn.FNNAME contain stubs that
3359 copy floating point arguments from the fp regs to the gp regs and
3360 then jump to FNNAME. If any 32 bit function calls FNNAME, the
3361 call should be redirected to the stub instead. If no 32 bit
3362 function calls FNNAME, the stub should be discarded. We need to
3363 consider any reference to the function, not just a call, because
3364 if the address of the function is taken we will need the stub,
3365 since the address might be passed to a 32 bit function.
3366
3367 Section names that look like .mips16.call.FNNAME contain stubs
3368 that copy floating point arguments from the gp regs to the fp
3369 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
3370 then any 16 bit function that calls FNNAME should be redirected
3371 to the stub instead. If FNNAME is not a 32 bit function, the
3372 stub should be discarded.
3373
3374 .mips16.call.fp.FNNAME sections are similar, but contain stubs
3375 which call FNNAME and then copy the return value from the fp regs
3376 to the gp regs. These stubs store the return value in $18 while
3377 calling FNNAME; any function which might call one of these stubs
3378 must arrange to save $18 around the call. (This case is not
3379 needed for 32 bit functions that call 16 bit functions, because
3380 16 bit functions always return floating point values in both
3381 $f0/$f1 and $2/$3.)
3382
3383 Note that in all cases FNNAME might be defined statically.
3384 Therefore, FNNAME is not used literally. Instead, the relocation
3385 information will indicate which symbol the section is for.
3386
3387 We record any stubs that we find in the symbol table. */
3388
3389#define FN_STUB ".mips16.fn."
3390#define CALL_STUB ".mips16.call."
3391#define CALL_FP_STUB ".mips16.call.fp."
3392
3393/* The MIPS ELF linker needs additional information for each symbol in
3394 the global hash table. */
3395
3396struct mips_elf_link_hash_entry
3397{
3398 struct elf_link_hash_entry root;
3399
3400 /* External symbol information. */
3401 EXTR esym;
3402
3403 /* Number of MIPS_32 or MIPS_REL32 relocs against this symbol. */
3404 unsigned int mips_32_relocs;
3405
3406 /* If there is a stub that 32 bit functions should use to call this
3407 16 bit function, this points to the section containing the stub. */
3408 asection *fn_stub;
3409
3410 /* Whether we need the fn_stub; this is set if this symbol appears
3411 in any relocs other than a 16 bit call. */
3412 boolean need_fn_stub;
3413
3414 /* If there is a stub that 16 bit functions should use to call this
3415 32 bit function, this points to the section containing the stub. */
3416 asection *call_stub;
3417
3418 /* This is like the call_stub field, but it is used if the function
3419 being called returns a floating point value. */
3420 asection *call_fp_stub;
3421};
3422
3423/* MIPS ELF linker hash table. */
3424
3425struct mips_elf_link_hash_table
3426{
3427 struct elf_link_hash_table root;
3428#if 0
3429 /* We no longer use this. */
3430 /* String section indices for the dynamic section symbols. */
3431 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
3432#endif
3433 /* The number of .rtproc entries. */
3434 bfd_size_type procedure_count;
3435 /* The size of the .compact_rel section (if SGI_COMPAT). */
3436 bfd_size_type compact_rel_size;
3437 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
3438 entry is set to the address of __rld_obj_head as in Irix 5. */
3439 boolean use_rld_obj_head;
3440 /* This is the value of the __rld_map or __rld_obj_head symbol. */
3441 bfd_vma rld_value;
3442 /* This is set if we see any mips16 stub sections. */
3443 boolean mips16_stubs_seen;
3444};
3445
3446/* Look up an entry in a MIPS ELF linker hash table. */
3447
3448#define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
3449 ((struct mips_elf_link_hash_entry *) \
3450 elf_link_hash_lookup (&(table)->root, (string), (create), \
3451 (copy), (follow)))
3452
3453/* Traverse a MIPS ELF linker hash table. */
3454
3455#define mips_elf_link_hash_traverse(table, func, info) \
3456 (elf_link_hash_traverse \
3457 (&(table)->root, \
3458 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
3459 (info)))
3460
3461/* Get the MIPS ELF linker hash table from a link_info structure. */
3462
3463#define mips_elf_hash_table(p) \
3464 ((struct mips_elf_link_hash_table *) ((p)->hash))
3465
3466static boolean mips_elf_output_extsym
3467 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
3468
3469/* Create an entry in a MIPS ELF linker hash table. */
3470
3471static struct bfd_hash_entry *
3472mips_elf_link_hash_newfunc (entry, table, string)
3473 struct bfd_hash_entry *entry;
3474 struct bfd_hash_table *table;
3475 const char *string;
3476{
3477 struct mips_elf_link_hash_entry *ret =
3478 (struct mips_elf_link_hash_entry *) entry;
3479
3480 /* Allocate the structure if it has not already been allocated by a
3481 subclass. */
3482 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3483 ret = ((struct mips_elf_link_hash_entry *)
3484 bfd_hash_allocate (table,
3485 sizeof (struct mips_elf_link_hash_entry)));
3486 if (ret == (struct mips_elf_link_hash_entry *) NULL)
3487 return (struct bfd_hash_entry *) ret;
3488
3489 /* Call the allocation method of the superclass. */
3490 ret = ((struct mips_elf_link_hash_entry *)
3491 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3492 table, string));
3493 if (ret != (struct mips_elf_link_hash_entry *) NULL)
3494 {
3495 /* Set local fields. */
3496 memset (&ret->esym, 0, sizeof (EXTR));
3497 /* We use -2 as a marker to indicate that the information has
3498 not been set. -1 means there is no associated ifd. */
3499 ret->esym.ifd = -2;
3500 ret->mips_32_relocs = 0;
3501 ret->fn_stub = NULL;
3502 ret->need_fn_stub = false;
3503 ret->call_stub = NULL;
3504 ret->call_fp_stub = NULL;
3505 }
3506
3507 return (struct bfd_hash_entry *) ret;
3508}
3509
3510/* Create a MIPS ELF linker hash table. */
3511
3512static struct bfd_link_hash_table *
3513mips_elf_link_hash_table_create (abfd)
3514 bfd *abfd;
3515{
3516 struct mips_elf_link_hash_table *ret;
3517
3518 ret = ((struct mips_elf_link_hash_table *)
3519 bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
3520 if (ret == (struct mips_elf_link_hash_table *) NULL)
3521 return NULL;
3522
3523 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3524 mips_elf_link_hash_newfunc))
3525 {
3526 bfd_release (abfd, ret);
3527 return NULL;
3528 }
3529
3530#if 0
3531 /* We no longer use this. */
3532 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
3533 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
3534#endif
3535 ret->procedure_count = 0;
3536 ret->compact_rel_size = 0;
3537 ret->use_rld_obj_head = false;
3538 ret->rld_value = 0;
3539 ret->mips16_stubs_seen = false;
3540
3541 return &ret->root.root;
3542}
3543
3544/* Hook called by the linker routine which adds symbols from an object
3545 file. We must handle the special MIPS section numbers here. */
3546
3547/*ARGSUSED*/
3548static boolean
3549mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3550 bfd *abfd;
3551 struct bfd_link_info *info;
3552 const Elf_Internal_Sym *sym;
3553 const char **namep;
3554 flagword *flagsp;
3555 asection **secp;
3556 bfd_vma *valp;
3557{
3558 if (SGI_COMPAT (abfd)
3559 && (abfd->flags & DYNAMIC) != 0
3560 && strcmp (*namep, "_rld_new_interface") == 0)
3561 {
3562 /* Skip Irix 5 rld entry name. */
3563 *namep = NULL;
3564 return true;
3565 }
3566
3567 switch (sym->st_shndx)
3568 {
3569 case SHN_COMMON:
3570 /* Common symbols less than the GP size are automatically
3571 treated as SHN_MIPS_SCOMMON symbols. */
3572 if (sym->st_size > elf_gp_size (abfd))
3573 break;
3574 /* Fall through. */
3575 case SHN_MIPS_SCOMMON:
3576 *secp = bfd_make_section_old_way (abfd, ".scommon");
3577 (*secp)->flags |= SEC_IS_COMMON;
3578 *valp = sym->st_size;
3579 break;
3580
3581 case SHN_MIPS_TEXT:
3582 /* This section is used in a shared object. */
3583 if (mips_elf_text_section_ptr == NULL)
3584 {
3585 /* Initialize the section. */
3586 mips_elf_text_section.name = ".text";
3587 mips_elf_text_section.flags = SEC_NO_FLAGS;
3588 mips_elf_text_section.output_section = NULL;
3589 mips_elf_text_section.symbol = &mips_elf_text_symbol;
3590 mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
3591 mips_elf_text_symbol.name = ".text";
3592 mips_elf_text_symbol.flags = BSF_SECTION_SYM;
3593 mips_elf_text_symbol.section = &mips_elf_text_section;
3594 mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
3595 mips_elf_text_section_ptr = &mips_elf_text_section;
3596 }
3597 /* This code used to do *secp = bfd_und_section_ptr if
3598 info->shared. I don't know why, and that doesn't make sense,
3599 so I took it out. */
3600 *secp = mips_elf_text_section_ptr;
3601 break;
3602
3603 case SHN_MIPS_ACOMMON:
3604 /* Fall through. XXX Can we treat this as allocated data? */
3605 case SHN_MIPS_DATA:
3606 /* This section is used in a shared object. */
3607 if (mips_elf_data_section_ptr == NULL)
3608 {
3609 /* Initialize the section. */
3610 mips_elf_data_section.name = ".data";
3611 mips_elf_data_section.flags = SEC_NO_FLAGS;
3612 mips_elf_data_section.output_section = NULL;
3613 mips_elf_data_section.symbol = &mips_elf_data_symbol;
3614 mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
3615 mips_elf_data_symbol.name = ".data";
3616 mips_elf_data_symbol.flags = BSF_SECTION_SYM;
3617 mips_elf_data_symbol.section = &mips_elf_data_section;
3618 mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
3619 mips_elf_data_section_ptr = &mips_elf_data_section;
3620 }
3621 /* This code used to do *secp = bfd_und_section_ptr if
3622 info->shared. I don't know why, and that doesn't make sense,
3623 so I took it out. */
3624 *secp = mips_elf_data_section_ptr;
3625 break;
3626
3627 case SHN_MIPS_SUNDEFINED:
3628 *secp = bfd_und_section_ptr;
3629 break;
3630 }
3631
3632 if (SGI_COMPAT (abfd)
3633 && ! info->shared
3634 && info->hash->creator == abfd->xvec
3635 && strcmp (*namep, "__rld_obj_head") == 0)
3636 {
3637 struct elf_link_hash_entry *h;
3638
3639 /* Mark __rld_obj_head as dynamic. */
3640 h = NULL;
3641 if (! (_bfd_generic_link_add_one_symbol
3642 (info, abfd, *namep, BSF_GLOBAL, *secp,
3643 (bfd_vma) *valp, (const char *) NULL, false,
3644 get_elf_backend_data (abfd)->collect,
3645 (struct bfd_link_hash_entry **) &h)))
3646 return false;
3647 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
3648 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3649 h->type = STT_OBJECT;
3650
3651 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3652 return false;
3653
3654 mips_elf_hash_table (info)->use_rld_obj_head = true;
3655 }
3656
3657 /* If this is a mips16 text symbol, add 1 to the value to make it
3658 odd. This will cause something like .word SYM to come up with
3659 the right value when it is loaded into the PC. */
3660 if (sym->st_other == STO_MIPS16)
3661 ++*valp;
3662
3663 return true;
3664}
3665
3666/* Structure used to pass information to mips_elf_output_extsym. */
3667
3668struct extsym_info
3669{
3670 bfd *abfd;
3671 struct bfd_link_info *info;
3672 struct ecoff_debug_info *debug;
3673 const struct ecoff_debug_swap *swap;
3674 boolean failed;
3675};
3676
3677/* This routine is used to write out ECOFF debugging external symbol
3678 information. It is called via mips_elf_link_hash_traverse. The
3679 ECOFF external symbol information must match the ELF external
3680 symbol information. Unfortunately, at this point we don't know
3681 whether a symbol is required by reloc information, so the two
3682 tables may wind up being different. We must sort out the external
3683 symbol information before we can set the final size of the .mdebug
3684 section, and we must set the size of the .mdebug section before we
3685 can relocate any sections, and we can't know which symbols are
3686 required by relocation until we relocate the sections.
3687 Fortunately, it is relatively unlikely that any symbol will be
3688 stripped but required by a reloc. In particular, it can not happen
3689 when generating a final executable. */
3690
3691static boolean
3692mips_elf_output_extsym (h, data)
3693 struct mips_elf_link_hash_entry *h;
3694 PTR data;
3695{
3696 struct extsym_info *einfo = (struct extsym_info *) data;
3697 boolean strip;
3698 asection *sec, *output_section;
3699
3700 if (h->root.indx == -2)
3701 strip = false;
3702 else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3703 || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
3704 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3705 && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
3706 strip = true;
3707 else if (einfo->info->strip == strip_all
3708 || (einfo->info->strip == strip_some
3709 && bfd_hash_lookup (einfo->info->keep_hash,
3710 h->root.root.root.string,
3711 false, false) == NULL))
3712 strip = true;
3713 else
3714 strip = false;
3715
3716 if (strip)
3717 return true;
3718
3719 if (h->esym.ifd == -2)
3720 {
3721 h->esym.jmptbl = 0;
3722 h->esym.cobol_main = 0;
3723 h->esym.weakext = 0;
3724 h->esym.reserved = 0;
3725 h->esym.ifd = ifdNil;
3726 h->esym.asym.value = 0;
3727 h->esym.asym.st = stGlobal;
3728
3729 if (SGI_COMPAT (einfo->abfd)
3730 && (h->root.root.type == bfd_link_hash_undefined
3731 || h->root.root.type == bfd_link_hash_undefweak))
3732 {
3733 const char *name;
3734
3735 /* Use undefined class. Also, set class and type for some
3736 special symbols. */
3737 name = h->root.root.root.string;
3738 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3739 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3740 {
3741 h->esym.asym.sc = scData;
3742 h->esym.asym.st = stLabel;
3743 h->esym.asym.value = 0;
3744 }
3745 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3746 {
3747 h->esym.asym.sc = scAbs;
3748 h->esym.asym.st = stLabel;
3749 h->esym.asym.value =
3750 mips_elf_hash_table (einfo->info)->procedure_count;
3751 }
3752 else if (strcmp (name, "_gp_disp") == 0)
3753 {
3754 h->esym.asym.sc = scAbs;
3755 h->esym.asym.st = stLabel;
3756 h->esym.asym.value = elf_gp (einfo->abfd);
3757 }
3758 else
3759 h->esym.asym.sc = scUndefined;
3760 }
3761 else if (h->root.root.type != bfd_link_hash_defined
3762 && h->root.root.type != bfd_link_hash_defweak)
3763 h->esym.asym.sc = scAbs;
3764 else
3765 {
3766 const char *name;
3767
3768 sec = h->root.root.u.def.section;
3769 output_section = sec->output_section;
3770
3771 /* When making a shared library and symbol h is the one from
3772 the another shared library, OUTPUT_SECTION may be null. */
3773 if (output_section == NULL)
3774 h->esym.asym.sc = scUndefined;
3775 else
3776 {
3777 name = bfd_section_name (output_section->owner, output_section);
3778
3779 if (strcmp (name, ".text") == 0)
3780 h->esym.asym.sc = scText;
3781 else if (strcmp (name, ".data") == 0)
3782 h->esym.asym.sc = scData;
3783 else if (strcmp (name, ".sdata") == 0)
3784 h->esym.asym.sc = scSData;
3785 else if (strcmp (name, ".rodata") == 0
3786 || strcmp (name, ".rdata") == 0)
3787 h->esym.asym.sc = scRData;
3788 else if (strcmp (name, ".bss") == 0)
3789 h->esym.asym.sc = scBss;
3790 else if (strcmp (name, ".sbss") == 0)
3791 h->esym.asym.sc = scSBss;
3792 else if (strcmp (name, ".init") == 0)
3793 h->esym.asym.sc = scInit;
3794 else if (strcmp (name, ".fini") == 0)
3795 h->esym.asym.sc = scFini;
3796 else
3797 h->esym.asym.sc = scAbs;
3798 }
3799 }
3800
3801 h->esym.asym.reserved = 0;
3802 h->esym.asym.index = indexNil;
3803 }
3804
3805 if (h->root.root.type == bfd_link_hash_common)
3806 h->esym.asym.value = h->root.root.u.c.size;
3807 else if (h->root.root.type == bfd_link_hash_defined
3808 || h->root.root.type == bfd_link_hash_defweak)
3809 {
3810 if (h->esym.asym.sc == scCommon)
3811 h->esym.asym.sc = scBss;
3812 else if (h->esym.asym.sc == scSCommon)
3813 h->esym.asym.sc = scSBss;
3814
3815 sec = h->root.root.u.def.section;
3816 output_section = sec->output_section;
3817 if (output_section != NULL)
3818 h->esym.asym.value = (h->root.root.u.def.value
3819 + sec->output_offset
3820 + output_section->vma);
3821 else
3822 h->esym.asym.value = 0;
3823 }
3824 else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3825 {
3826 /* Set type and value for a symbol with a function stub. */
3827 h->esym.asym.st = stProc;
3828 sec = h->root.root.u.def.section;
3829 if (sec == NULL)
3830 h->esym.asym.value = 0;
3831 else
3832 {
3833 output_section = sec->output_section;
3834 if (output_section != NULL)
3835 h->esym.asym.value = (h->root.plt.offset
3836 + sec->output_offset
3837 + output_section->vma);
3838 else
3839 h->esym.asym.value = 0;
3840 }
3841#if 0 /* FIXME? */
3842 h->esym.ifd = 0;
3843#endif
3844 }
3845
3846 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3847 h->root.root.root.string,
3848 &h->esym))
3849 {
3850 einfo->failed = true;
3851 return false;
3852 }
3853
3854 return true;
3855}
3856
3857/* Create a runtime procedure table from the .mdebug section. */
3858
3859static boolean
3860mips_elf_create_procedure_table (handle, abfd, info, s, debug)
3861 PTR handle;
3862 bfd *abfd;
3863 struct bfd_link_info *info;
3864 asection *s;
3865 struct ecoff_debug_info *debug;
3866{
3867 const struct ecoff_debug_swap *swap;
3868 HDRR *hdr = &debug->symbolic_header;
3869 RPDR *rpdr, *rp;
3870 struct rpdr_ext *erp;
3871 PTR rtproc;
3872 struct pdr_ext *epdr;
3873 struct sym_ext *esym;
3874 char *ss, **sv;
3875 char *str;
3876 unsigned long size, count;
3877 unsigned long sindex;
3878 unsigned long i;
3879 PDR pdr;
3880 SYMR sym;
3881 const char *no_name_func = _("static procedure (no name)");
3882
3883 epdr = NULL;
3884 rpdr = NULL;
3885 esym = NULL;
3886 ss = NULL;
3887 sv = NULL;
3888
3889 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3890
3891 sindex = strlen (no_name_func) + 1;
3892 count = hdr->ipdMax;
3893 if (count > 0)
3894 {
3895 size = swap->external_pdr_size;
3896
3897 epdr = (struct pdr_ext *) bfd_malloc (size * count);
3898 if (epdr == NULL)
3899 goto error_return;
3900
3901 if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
3902 goto error_return;
3903
3904 size = sizeof (RPDR);
3905 rp = rpdr = (RPDR *) bfd_malloc (size * count);
3906 if (rpdr == NULL)
3907 goto error_return;
3908
3909 sv = (char **) bfd_malloc (sizeof (char *) * count);
3910 if (sv == NULL)
3911 goto error_return;
3912
3913 count = hdr->isymMax;
3914 size = swap->external_sym_size;
3915 esym = (struct sym_ext *) bfd_malloc (size * count);
3916 if (esym == NULL)
3917 goto error_return;
3918
3919 if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
3920 goto error_return;
3921
3922 count = hdr->issMax;
3923 ss = (char *) bfd_malloc (count);
3924 if (ss == NULL)
3925 goto error_return;
3926 if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
3927 goto error_return;
3928
3929 count = hdr->ipdMax;
3930 for (i = 0; i < count; i++, rp++)
3931 {
3932 (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
3933 (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
3934 rp->adr = sym.value;
3935 rp->regmask = pdr.regmask;
3936 rp->regoffset = pdr.regoffset;
3937 rp->fregmask = pdr.fregmask;
3938 rp->fregoffset = pdr.fregoffset;
3939 rp->frameoffset = pdr.frameoffset;
3940 rp->framereg = pdr.framereg;
3941 rp->pcreg = pdr.pcreg;
3942 rp->irpss = sindex;
3943 sv[i] = ss + sym.iss;
3944 sindex += strlen (sv[i]) + 1;
3945 }
3946 }
3947
3948 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
3949 size = BFD_ALIGN (size, 16);
3950 rtproc = (PTR) bfd_alloc (abfd, size);
3951 if (rtproc == NULL)
3952 {
3953 mips_elf_hash_table (info)->procedure_count = 0;
3954 goto error_return;
3955 }
3956
3957 mips_elf_hash_table (info)->procedure_count = count + 2;
3958
3959 erp = (struct rpdr_ext *) rtproc;
3960 memset (erp, 0, sizeof (struct rpdr_ext));
3961 erp++;
3962 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
3963 strcpy (str, no_name_func);
3964 str += strlen (no_name_func) + 1;
3965 for (i = 0; i < count; i++)
3966 {
3967 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
3968 strcpy (str, sv[i]);
3969 str += strlen (sv[i]) + 1;
3970 }
3971 ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
3972
3973 /* Set the size and contents of .rtproc section. */
3974 s->_raw_size = size;
3975 s->contents = (bfd_byte *) rtproc;
3976
3977 /* Skip this section later on (I don't think this currently
3978 matters, but someday it might). */
3979 s->link_order_head = (struct bfd_link_order *) NULL;
3980
3981 if (epdr != NULL)
3982 free (epdr);
3983 if (rpdr != NULL)
3984 free (rpdr);
3985 if (esym != NULL)
3986 free (esym);
3987 if (ss != NULL)
3988 free (ss);
3989 if (sv != NULL)
3990 free (sv);
3991
3992 return true;
3993
3994 error_return:
3995 if (epdr != NULL)
3996 free (epdr);
3997 if (rpdr != NULL)
3998 free (rpdr);
3999 if (esym != NULL)
4000 free (esym);
4001 if (ss != NULL)
4002 free (ss);
4003 if (sv != NULL)
4004 free (sv);
4005 return false;
4006}
4007
4008/* A comparison routine used to sort .gptab entries. */
4009
4010static int
4011gptab_compare (p1, p2)
4012 const PTR p1;
4013 const PTR p2;
4014{
4015 const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
4016 const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
4017
4018 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
4019}
4020
4021/* We need to use a special link routine to handle the .reginfo and
4022 the .mdebug sections. We need to merge all instances of these
4023 sections together, not write them all out sequentially. */
4024
4025static boolean
4026mips_elf_final_link (abfd, info)
4027 bfd *abfd;
4028 struct bfd_link_info *info;
4029{
4030 asection **secpp;
4031 asection *o;
4032 struct bfd_link_order *p;
4033 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
4034 asection *rtproc_sec;
4035 Elf32_RegInfo reginfo;
4036 struct ecoff_debug_info debug;
4037 const struct ecoff_debug_swap *swap
4038 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
4039 HDRR *symhdr = &debug.symbolic_header;
4040 PTR mdebug_handle = NULL;
4041
4042 /* Drop the .options section, since it has special semantics which I
4043 haven't bothered to figure out. */
4044 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
4045 {
4046 if (strcmp ((*secpp)->name, ".options") == 0)
4047 {
4048 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
4049 if (p->type == bfd_indirect_link_order)
4050 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
4051 (*secpp)->link_order_head = NULL;
4052 *secpp = (*secpp)->next;
4053 --abfd->section_count;
4054 break;
4055 }
4056 }
4057
4058 /* Get a value for the GP register. */
4059 if (elf_gp (abfd) == 0)
4060 {
4061 struct bfd_link_hash_entry *h;
4062
4063 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4064 if (h != (struct bfd_link_hash_entry *) NULL
4065 && h->type == bfd_link_hash_defined)
4066 elf_gp (abfd) = (h->u.def.value
4067 + h->u.def.section->output_section->vma
4068 + h->u.def.section->output_offset);
4069 else if (info->relocateable)
4070 {
4071 bfd_vma lo;
4072
4073 /* Make up a value. */
4074 lo = (bfd_vma) -1;
4075 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4076 {
4077 if (o->vma < lo
4078 && (strcmp (o->name, ".sbss") == 0
4079 || strcmp (o->name, ".sdata") == 0
4080 || strcmp (o->name, ".lit4") == 0
4081 || strcmp (o->name, ".lit8") == 0))
4082 lo = o->vma;
4083 }
4084 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
4085 }
4086 else
4087 {
4088 /* If the relocate_section function needs to do a reloc
4089 involving the GP value, it should make a reloc_dangerous
4090 callback to warn that GP is not defined. */
4091 }
4092 }
4093
4094 /* Go through the sections and collect the .reginfo and .mdebug
4095 information. */
4096 reginfo_sec = NULL;
4097 mdebug_sec = NULL;
4098 gptab_data_sec = NULL;
4099 gptab_bss_sec = NULL;
4100 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4101 {
4102 if (strcmp (o->name, ".reginfo") == 0)
4103 {
4104 memset (&reginfo, 0, sizeof reginfo);
4105
4106 /* We have found the .reginfo section in the output file.
4107 Look through all the link_orders comprising it and merge
4108 the information together. */
4109 for (p = o->link_order_head;
4110 p != (struct bfd_link_order *) NULL;
4111 p = p->next)
4112 {
4113 asection *input_section;
4114 bfd *input_bfd;
4115 Elf32_External_RegInfo ext;
4116 Elf32_RegInfo sub;
4117
4118 if (p->type != bfd_indirect_link_order)
4119 {
4120 if (p->type == bfd_fill_link_order)
4121 continue;
4122 abort ();
4123 }
4124
4125 input_section = p->u.indirect.section;
4126 input_bfd = input_section->owner;
4127
4128 /* The linker emulation code has probably clobbered the
4129 size to be zero bytes. */
4130 if (input_section->_raw_size == 0)
4131 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
4132
4133 if (! bfd_get_section_contents (input_bfd, input_section,
4134 (PTR) &ext,
4135 (file_ptr) 0,
4136 sizeof ext))
4137 return false;
4138
4139 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
4140
4141 reginfo.ri_gprmask |= sub.ri_gprmask;
4142 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4143 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4144 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4145 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4146
4147 /* ri_gp_value is set by the function
4148 mips_elf32_section_processing when the section is
4149 finally written out. */
4150
4151 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4152 elf_link_input_bfd ignores this section. */
4153 input_section->flags &=~ SEC_HAS_CONTENTS;
4154 }
4155
4156 /* Size has been set in mips_elf_always_size_sections */
4157 BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo));
4158
4159 /* Skip this section later on (I don't think this currently
4160 matters, but someday it might). */
4161 o->link_order_head = (struct bfd_link_order *) NULL;
4162
4163 reginfo_sec = o;
4164 }
4165
4166 if (strcmp (o->name, ".mdebug") == 0)
4167 {
4168 struct extsym_info einfo;
4169
4170 /* We have found the .mdebug section in the output file.
4171 Look through all the link_orders comprising it and merge
4172 the information together. */
4173 symhdr->magic = swap->sym_magic;
4174 /* FIXME: What should the version stamp be? */
4175 symhdr->vstamp = 0;
4176 symhdr->ilineMax = 0;
4177 symhdr->cbLine = 0;
4178 symhdr->idnMax = 0;
4179 symhdr->ipdMax = 0;
4180 symhdr->isymMax = 0;
4181 symhdr->ioptMax = 0;
4182 symhdr->iauxMax = 0;
4183 symhdr->issMax = 0;
4184 symhdr->issExtMax = 0;
4185 symhdr->ifdMax = 0;
4186 symhdr->crfd = 0;
4187 symhdr->iextMax = 0;
4188
4189 /* We accumulate the debugging information itself in the
4190 debug_info structure. */
4191 debug.line = NULL;
4192 debug.external_dnr = NULL;
4193 debug.external_pdr = NULL;
4194 debug.external_sym = NULL;
4195 debug.external_opt = NULL;
4196 debug.external_aux = NULL;
4197 debug.ss = NULL;
4198 debug.ssext = debug.ssext_end = NULL;
4199 debug.external_fdr = NULL;
4200 debug.external_rfd = NULL;
4201 debug.external_ext = debug.external_ext_end = NULL;
4202
4203 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4204 if (mdebug_handle == (PTR) NULL)
4205 return false;
4206
4207 if (SGI_COMPAT (abfd))
4208 {
4209 asection *s;
4210 EXTR esym;
4211 bfd_vma last;
4212 unsigned int i;
4213 static const char * const name[] =
4214 { ".text", ".init", ".fini", ".data",
4215 ".rodata", ".sdata", ".sbss", ".bss" };
4216 static const int sc[] = { scText, scInit, scFini, scData,
4217 scRData, scSData, scSBss, scBss };
4218
4219 esym.jmptbl = 0;
4220 esym.cobol_main = 0;
4221 esym.weakext = 0;
4222 esym.reserved = 0;
4223 esym.ifd = ifdNil;
4224 esym.asym.iss = issNil;
4225 esym.asym.st = stLocal;
4226 esym.asym.reserved = 0;
4227 esym.asym.index = indexNil;
4228 last = 0;
4229 for (i = 0; i < 8; i++)
4230 {
4231 esym.asym.sc = sc[i];
4232 s = bfd_get_section_by_name (abfd, name[i]);
4233 if (s != NULL)
4234 {
4235 esym.asym.value = s->vma;
4236 last = s->vma + s->_raw_size;
4237 }
4238 else
4239 esym.asym.value = last;
4240
4241 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4242 name[i], &esym))
4243 return false;
4244 }
4245 }
4246
4247 for (p = o->link_order_head;
4248 p != (struct bfd_link_order *) NULL;
4249 p = p->next)
4250 {
4251 asection *input_section;
4252 bfd *input_bfd;
4253 const struct ecoff_debug_swap *input_swap;
4254 struct ecoff_debug_info input_debug;
4255 char *eraw_src;
4256 char *eraw_end;
4257
4258 if (p->type != bfd_indirect_link_order)
4259 {
4260 if (p->type == bfd_fill_link_order)
4261 continue;
4262 abort ();
4263 }
4264
4265 input_section = p->u.indirect.section;
4266 input_bfd = input_section->owner;
4267
4268 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4269 || (get_elf_backend_data (input_bfd)
4270 ->elf_backend_ecoff_debug_swap) == NULL)
4271 {
4272 /* I don't know what a non MIPS ELF bfd would be
4273 doing with a .mdebug section, but I don't really
4274 want to deal with it. */
4275 continue;
4276 }
4277
4278 input_swap = (get_elf_backend_data (input_bfd)
4279 ->elf_backend_ecoff_debug_swap);
4280
4281 BFD_ASSERT (p->size == input_section->_raw_size);
4282
4283 /* The ECOFF linking code expects that we have already
4284 read in the debugging information and set up an
4285 ecoff_debug_info structure, so we do that now. */
4286 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
4287 &input_debug))
4288 return false;
4289
4290 if (! (bfd_ecoff_debug_accumulate
4291 (mdebug_handle, abfd, &debug, swap, input_bfd,
4292 &input_debug, input_swap, info)))
4293 return false;
4294
4295 /* Loop through the external symbols. For each one with
4296 interesting information, try to find the symbol in
4297 the linker global hash table and save the information
4298 for the output external symbols. */
4299 eraw_src = input_debug.external_ext;
4300 eraw_end = (eraw_src
4301 + (input_debug.symbolic_header.iextMax
4302 * input_swap->external_ext_size));
4303 for (;
4304 eraw_src < eraw_end;
4305 eraw_src += input_swap->external_ext_size)
4306 {
4307 EXTR ext;
4308 const char *name;
4309 struct mips_elf_link_hash_entry *h;
4310
4311 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4312 if (ext.asym.sc == scNil
4313 || ext.asym.sc == scUndefined
4314 || ext.asym.sc == scSUndefined)
4315 continue;
4316
4317 name = input_debug.ssext + ext.asym.iss;
4318 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
4319 name, false, false, true);
4320 if (h == NULL || h->esym.ifd != -2)
4321 continue;
4322
4323 if (ext.ifd != -1)
4324 {
4325 BFD_ASSERT (ext.ifd
4326 < input_debug.symbolic_header.ifdMax);
4327 ext.ifd = input_debug.ifdmap[ext.ifd];
4328 }
4329
4330 h->esym = ext;
4331 }
4332
4333 /* Free up the information we just read. */
4334 free (input_debug.line);
4335 free (input_debug.external_dnr);
4336 free (input_debug.external_pdr);
4337 free (input_debug.external_sym);
4338 free (input_debug.external_opt);
4339 free (input_debug.external_aux);
4340 free (input_debug.ss);
4341 free (input_debug.ssext);
4342 free (input_debug.external_fdr);
4343 free (input_debug.external_rfd);
4344 free (input_debug.external_ext);
4345
4346 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4347 elf_link_input_bfd ignores this section. */
4348 input_section->flags &=~ SEC_HAS_CONTENTS;
4349 }
4350
4351 if (SGI_COMPAT (abfd) && info->shared)
4352 {
4353 /* Create .rtproc section. */
4354 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4355 if (rtproc_sec == NULL)
4356 {
4357 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
4358 | SEC_LINKER_CREATED | SEC_READONLY);
4359
4360 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4361 if (rtproc_sec == NULL
4362 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4363 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
4364 return false;
4365 }
4366
4367 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
4368 info, rtproc_sec, &debug))
4369 return false;
4370 }
4371
4372 /* Build the external symbol information. */
4373 einfo.abfd = abfd;
4374 einfo.info = info;
4375 einfo.debug = &debug;
4376 einfo.swap = swap;
4377 einfo.failed = false;
4378 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4379 mips_elf_output_extsym,
4380 (PTR) &einfo);
4381 if (einfo.failed)
4382 return false;
4383
4384 /* Set the size of the .mdebug section. */
4385 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4386
4387 /* Skip this section later on (I don't think this currently
4388 matters, but someday it might). */
4389 o->link_order_head = (struct bfd_link_order *) NULL;
4390
4391 mdebug_sec = o;
4392 }
4393
4394 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4395 {
4396 const char *subname;
4397 unsigned int c;
4398 Elf32_gptab *tab;
4399 Elf32_External_gptab *ext_tab;
4400 unsigned int i;
4401
4402 /* The .gptab.sdata and .gptab.sbss sections hold
4403 information describing how the small data area would
4404 change depending upon the -G switch. These sections
4405 not used in executables files. */
4406 if (! info->relocateable)
4407 {
4408 asection **secpp;
4409
4410 for (p = o->link_order_head;
4411 p != (struct bfd_link_order *) NULL;
4412 p = p->next)
4413 {
4414 asection *input_section;
4415
4416 if (p->type != bfd_indirect_link_order)
4417 {
4418 if (p->type == bfd_fill_link_order)
4419 continue;
4420 abort ();
4421 }
4422
4423 input_section = p->u.indirect.section;
4424
4425 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4426 elf_link_input_bfd ignores this section. */
4427 input_section->flags &=~ SEC_HAS_CONTENTS;
4428 }
4429
4430 /* Skip this section later on (I don't think this
4431 currently matters, but someday it might). */
4432 o->link_order_head = (struct bfd_link_order *) NULL;
4433
4434 /* Really remove the section. */
4435 for (secpp = &abfd->sections;
4436 *secpp != o;
4437 secpp = &(*secpp)->next)
4438 ;
4439 *secpp = (*secpp)->next;
4440 --abfd->section_count;
4441
4442 continue;
4443 }
4444
4445 /* There is one gptab for initialized data, and one for
4446 uninitialized data. */
4447 if (strcmp (o->name, ".gptab.sdata") == 0)
4448 gptab_data_sec = o;
4449 else if (strcmp (o->name, ".gptab.sbss") == 0)
4450 gptab_bss_sec = o;
4451 else
4452 {
4453 (*_bfd_error_handler)
4454 (_("%s: illegal section name `%s'"),
4455 bfd_get_filename (abfd), o->name);
4456 bfd_set_error (bfd_error_nonrepresentable_section);
4457 return false;
4458 }
4459
4460 /* The linker script always combines .gptab.data and
4461 .gptab.sdata into .gptab.sdata, and likewise for
4462 .gptab.bss and .gptab.sbss. It is possible that there is
4463 no .sdata or .sbss section in the output file, in which
4464 case we must change the name of the output section. */
4465 subname = o->name + sizeof ".gptab" - 1;
4466 if (bfd_get_section_by_name (abfd, subname) == NULL)
4467 {
4468 if (o == gptab_data_sec)
4469 o->name = ".gptab.data";
4470 else
4471 o->name = ".gptab.bss";
4472 subname = o->name + sizeof ".gptab" - 1;
4473 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4474 }
4475
4476 /* Set up the first entry. */
4477 c = 1;
4478 tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
4479 if (tab == NULL)
4480 return false;
4481 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4482 tab[0].gt_header.gt_unused = 0;
4483
4484 /* Combine the input sections. */
4485 for (p = o->link_order_head;
4486 p != (struct bfd_link_order *) NULL;
4487 p = p->next)
4488 {
4489 asection *input_section;
4490 bfd *input_bfd;
4491 bfd_size_type size;
4492 unsigned long last;
4493 bfd_size_type gpentry;
4494
4495 if (p->type != bfd_indirect_link_order)
4496 {
4497 if (p->type == bfd_fill_link_order)
4498 continue;
4499 abort ();
4500 }
4501
4502 input_section = p->u.indirect.section;
4503 input_bfd = input_section->owner;
4504
4505 /* Combine the gptab entries for this input section one
4506 by one. We know that the input gptab entries are
4507 sorted by ascending -G value. */
4508 size = bfd_section_size (input_bfd, input_section);
4509 last = 0;
4510 for (gpentry = sizeof (Elf32_External_gptab);
4511 gpentry < size;
4512 gpentry += sizeof (Elf32_External_gptab))
4513 {
4514 Elf32_External_gptab ext_gptab;
4515 Elf32_gptab int_gptab;
4516 unsigned long val;
4517 unsigned long add;
4518 boolean exact;
4519 unsigned int look;
4520
4521 if (! (bfd_get_section_contents
4522 (input_bfd, input_section, (PTR) &ext_gptab,
4523 gpentry, sizeof (Elf32_External_gptab))))
4524 {
4525 free (tab);
4526 return false;
4527 }
4528
4529 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
4530 &int_gptab);
4531 val = int_gptab.gt_entry.gt_g_value;
4532 add = int_gptab.gt_entry.gt_bytes - last;
4533
4534 exact = false;
4535 for (look = 1; look < c; look++)
4536 {
4537 if (tab[look].gt_entry.gt_g_value >= val)
4538 tab[look].gt_entry.gt_bytes += add;
4539
4540 if (tab[look].gt_entry.gt_g_value == val)
4541 exact = true;
4542 }
4543
4544 if (! exact)
4545 {
4546 Elf32_gptab *new_tab;
4547 unsigned int max;
4548
4549 /* We need a new table entry. */
4550 new_tab = ((Elf32_gptab *)
4551 bfd_realloc ((PTR) tab,
4552 (c + 1) * sizeof (Elf32_gptab)));
4553 if (new_tab == NULL)
4554 {
4555 free (tab);
4556 return false;
4557 }
4558 tab = new_tab;
4559 tab[c].gt_entry.gt_g_value = val;
4560 tab[c].gt_entry.gt_bytes = add;
4561
4562 /* Merge in the size for the next smallest -G
4563 value, since that will be implied by this new
4564 value. */
4565 max = 0;
4566 for (look = 1; look < c; look++)
4567 {
4568 if (tab[look].gt_entry.gt_g_value < val
4569 && (max == 0
4570 || (tab[look].gt_entry.gt_g_value
4571 > tab[max].gt_entry.gt_g_value)))
4572 max = look;
4573 }
4574 if (max != 0)
4575 tab[c].gt_entry.gt_bytes +=
4576 tab[max].gt_entry.gt_bytes;
4577
4578 ++c;
4579 }
4580
4581 last = int_gptab.gt_entry.gt_bytes;
4582 }
4583
4584 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4585 elf_link_input_bfd ignores this section. */
4586 input_section->flags &=~ SEC_HAS_CONTENTS;
4587 }
4588
4589 /* The table must be sorted by -G value. */
4590 if (c > 2)
4591 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4592
4593 /* Swap out the table. */
4594 ext_tab = ((Elf32_External_gptab *)
4595 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
4596 if (ext_tab == NULL)
4597 {
4598 free (tab);
4599 return false;
4600 }
4601
4602 for (i = 0; i < c; i++)
4603 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
4604 free (tab);
4605
4606 o->_raw_size = c * sizeof (Elf32_External_gptab);
4607 o->contents = (bfd_byte *) ext_tab;
4608
4609 /* Skip this section later on (I don't think this currently
4610 matters, but someday it might). */
4611 o->link_order_head = (struct bfd_link_order *) NULL;
4612 }
4613 }
4614
4615 /* Invoke the regular ELF backend linker to do all the work. */
4616 if (! bfd_elf32_bfd_final_link (abfd, info))
4617 return false;
4618
4619 /* Now write out the computed sections. */
4620
4621 if (reginfo_sec != (asection *) NULL)
4622 {
4623 Elf32_External_RegInfo ext;
4624
4625 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
4626 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4627 (file_ptr) 0, sizeof ext))
4628 return false;
4629 }
4630
4631 if (mdebug_sec != (asection *) NULL)
4632 {
4633 BFD_ASSERT (abfd->output_has_begun);
4634 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4635 swap, info,
4636 mdebug_sec->filepos))
4637 return false;
4638
4639 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4640 }
4641
4642 if (gptab_data_sec != (asection *) NULL)
4643 {
4644 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4645 gptab_data_sec->contents,
4646 (file_ptr) 0,
4647 gptab_data_sec->_raw_size))
4648 return false;
4649 }
4650
4651 if (gptab_bss_sec != (asection *) NULL)
4652 {
4653 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4654 gptab_bss_sec->contents,
4655 (file_ptr) 0,
4656 gptab_bss_sec->_raw_size))
4657 return false;
4658 }
4659
4660 if (SGI_COMPAT (abfd))
4661 {
4662 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4663 if (rtproc_sec != NULL)
4664 {
4665 if (! bfd_set_section_contents (abfd, rtproc_sec,
4666 rtproc_sec->contents,
4667 (file_ptr) 0,
4668 rtproc_sec->_raw_size))
4669 return false;
4670 }
4671 }
4672
4673 return true;
4674}
4675
4676/* Handle a MIPS ELF HI16 reloc. */
4677
4678static void
4679mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
4680 bfd *input_bfd;
4681 Elf_Internal_Rela *relhi;
4682 Elf_Internal_Rela *rello;
4683 bfd_byte *contents;
4684 bfd_vma addend;
4685{
4686 bfd_vma insn;
4687 bfd_vma addlo;
4688
4689 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4690
4691 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4692 addlo &= 0xffff;
4693
4694 addend += ((insn & 0xffff) << 16) + addlo;
4695
4696 if ((addlo & 0x8000) != 0)
4697 addend -= 0x10000;
4698 if ((addend & 0x8000) != 0)
4699 addend += 0x10000;
4700
4701 bfd_put_32 (input_bfd,
4702 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
4703 contents + relhi->r_offset);
4704}
4705
4706/* Handle a MIPS ELF local GOT16 reloc. */
4707
4708static boolean
4709mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
4710 contents, addend)
4711 bfd *output_bfd;
4712 bfd *input_bfd;
4713 asection *sgot;
4714 Elf_Internal_Rela *relhi;
4715 Elf_Internal_Rela *rello;
4716 bfd_byte *contents;
4717 bfd_vma addend;
4718{
4719 unsigned int assigned_gotno;
4720 unsigned int i;
4721 bfd_vma insn;
4722 bfd_vma addlo;
4723 bfd_vma address;
4724 bfd_vma hipage;
4725 bfd_byte *got_contents;
4726 struct mips_got_info *g;
4727
4728 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4729
4730 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4731 addlo &= 0xffff;
4732
4733 addend += ((insn & 0xffff) << 16) + addlo;
4734
4735 if ((addlo & 0x8000) != 0)
4736 addend -= 0x10000;
4737 if ((addend & 0x8000) != 0)
4738 addend += 0x10000;
4739
4740 /* Get a got entry representing requested hipage. */
4741 BFD_ASSERT (elf_section_data (sgot) != NULL);
4742 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4743 BFD_ASSERT (g != NULL);
4744
4745 assigned_gotno = g->assigned_gotno;
4746 got_contents = sgot->contents;
4747 hipage = addend & 0xffff0000;
4748
4749 for (i = MIPS_RESERVED_GOTNO; i < assigned_gotno; i++)
4750 {
4751 address = bfd_get_32 (input_bfd, got_contents + i * 4);
4752 if (hipage == (address & 0xffff0000))
4753 break;
4754 }
4755
4756 if (i == assigned_gotno)
4757 {
4758 if (assigned_gotno >= g->local_gotno)
4759 {
4760 (*_bfd_error_handler)
4761 (_("more got entries are needed for hipage relocations"));
4762 bfd_set_error (bfd_error_bad_value);
4763 return false;
4764 }
4765
4766 bfd_put_32 (input_bfd, hipage, got_contents + assigned_gotno * 4);
4767 ++g->assigned_gotno;
4768 }
4769
4770 i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
4771 bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
4772 contents + relhi->r_offset);
4773
4774 return true;
4775}
4776
4777/* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
4778
4779static void
4780mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
4781 bfd *input_bfd;
4782 Elf_Internal_Rela *rel;
4783 bfd_byte *contents;
4784 bfd_vma offset;
4785{
4786 bfd_vma insn;
4787
4788 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
4789 bfd_put_32 (input_bfd,
4790 (insn & 0xffff0000) | (offset & 0xffff),
4791 contents + rel->r_offset);
4792}
4793
4794/* Relocate a MIPS ELF section. */
4795
4796static boolean
4797mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4798 contents, relocs, local_syms, local_sections)
4799 bfd *output_bfd;
4800 struct bfd_link_info *info;
4801 bfd *input_bfd;
4802 asection *input_section;
4803 bfd_byte *contents;
4804 Elf_Internal_Rela *relocs;
4805 Elf_Internal_Sym *local_syms;
4806 asection **local_sections;
4807{
4808 Elf_Internal_Shdr *symtab_hdr;
4809 size_t locsymcount;
4810 size_t extsymoff;
4811 asection *sgot, *sreloc, *scpt;
4812 bfd *dynobj;
4813 bfd_vma gp;
4814 Elf_Internal_Rela *rel;
4815 Elf_Internal_Rela *relend;
4816 struct mips_got_info *g;
4817
4818 dynobj = elf_hash_table (info)->dynobj;
4819 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4820
4821 sgot = NULL;
4822 sreloc = NULL;
4823 if (dynobj == NULL || ! SGI_COMPAT (output_bfd))
4824 scpt = NULL;
4825 else
4826 scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
4827 g = NULL;
4828
4829 if (elf_bad_symtab (input_bfd))
4830 {
4831 locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
4832 extsymoff = 0;
4833 }
4834 else
4835 {
4836 locsymcount = symtab_hdr->sh_info;
4837 extsymoff = symtab_hdr->sh_info;
4838 }
4839
4840 gp = _bfd_get_gp_value (output_bfd);
4841
4842 rel = relocs;
4843 relend = relocs + input_section->reloc_count;
4844 for (; rel < relend; rel++)
4845 {
4846 int r_type;
4847 reloc_howto_type *howto;
4848 unsigned long r_symndx;
4849 bfd_vma addend;
4850 struct elf_link_hash_entry *h;
4851 asection *sec;
4852 Elf_Internal_Sym *sym;
4853 struct mips_elf_link_hash_entry *mh;
4854 int other;
4855 bfd_reloc_status_type r;
4856
4857 r_type = ELF32_R_TYPE (rel->r_info);
4858 if (r_type == R_MIPS_GNU_VTINHERIT
4859 || r_type == R_MIPS_GNU_VTENTRY)
4860 continue;
4861 if ((r_type < 0 || r_type >= (int) R_MIPS_max)
4862 && r_type != R_MIPS16_26
4863 && r_type != R_MIPS16_GPREL)
4864 {
4865 bfd_set_error (bfd_error_bad_value);
4866 return false;
4867 }
4868 if (r_type == R_MIPS16_26)
4869 howto = &elf_mips16_jump_howto;
4870 else if (r_type == R_MIPS16_GPREL)
4871 howto = &elf_mips16_gprel_howto;
4872 else
4873 howto = elf_mips_howto_table + r_type;
4874
4875 if (dynobj != NULL
4876 && (r_type == R_MIPS_CALL16
4877 || r_type == R_MIPS_GOT16
4878 || r_type == R_MIPS_CALL_HI16
4879 || r_type == R_MIPS_CALL_LO16
4880 || r_type == R_MIPS_GOT_HI16
4881 || r_type == R_MIPS_GOT_LO16))
4882 {
4883 /* We need the .got section. */
4884 if (sgot == NULL)
4885 {
4886 sgot = bfd_get_section_by_name (dynobj, ".got");
4887 BFD_ASSERT (sgot != NULL);
4888 BFD_ASSERT (elf_section_data (sgot) != NULL);
4889 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4890 BFD_ASSERT (g != NULL);
4891 }
4892 }
4893
4894 r_symndx = ELF32_R_SYM (rel->r_info);
4895
4896 /* Mix in the change in GP address for a GP relative reloc. */
4897 if (r_type != R_MIPS_GPREL16
4898 && r_type != R_MIPS_LITERAL
4899 && r_type != R_MIPS_GPREL32
4900 && r_type != R_MIPS16_GPREL)
4901 addend = 0;
4902 else
4903 {
4904 if (gp == 0)
4905 {
4906 if (! ((*info->callbacks->reloc_dangerous)
4907 (info,
4908 _("GP relative relocation when GP not defined"),
4909 input_bfd, input_section,
4910 rel->r_offset)))
4911 return false;
4912 /* Only give the error once per link. */
4913 gp = 4;
4914 _bfd_set_gp_value (output_bfd, gp);
4915 }
4916
4917 if (r_symndx < extsymoff
4918 || (elf_bad_symtab (input_bfd)
4919 && local_sections[r_symndx] != NULL))
4920 {
4921 /* This is a relocation against a section. The current
4922 addend in the instruction is the difference between
4923 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
4924 must change this to be the difference between the
4925 final definition (which will end up in RELOCATION)
4926 and the GP value of OUTPUT_BFD (which is in GP). */
4927 addend = elf_gp (input_bfd) - gp;
4928 }
4929 else if (! info->relocateable)
4930 {
4931 /* We are doing a final link. The current addend in the
4932 instruction is simply the desired offset into the
4933 symbol (normally zero). We want the instruction to
4934 hold the difference between the final definition of
4935 the symbol (which will end up in RELOCATION) and the
4936 GP value of OUTPUT_BFD (which is in GP). */
4937 addend = - gp;
4938 }
4939 else
4940 {
4941 /* We are generating relocateable output, and we aren't
4942 going to define this symbol, so we just leave the
4943 instruction alone. */
4944 addend = 0;
4945 }
4946 }
4947
4948 h = NULL;
4949 sym = NULL;
4950 sec = NULL;
4951 if (info->relocateable)
4952 {
4953 /* This is a relocateable link. We don't have to change
4954 anything, unless the reloc is against a section symbol,
4955 in which case we have to adjust according to where the
4956 section symbol winds up in the output section. */
4957 if (r_symndx >= locsymcount
4958 || (elf_bad_symtab (input_bfd)
4959 && local_sections[r_symndx] == NULL))
4960 r = bfd_reloc_ok;
4961 else
4962 {
4963 sym = local_syms + r_symndx;
4964 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4965 r = bfd_reloc_ok;
4966 else
4967 {
4968 sec = local_sections[r_symndx];
4969
4970 /* It would be logical to add sym->st_value here,
4971 but Irix 5 sometimes generates a garbage symbol
4972 value. */
4973 addend += sec->output_offset;
4974
4975 /* If this is HI16 or GOT16 with an associated LO16,
4976 adjust the addend accordingly. Otherwise, just
4977 relocate. */
4978 if (r_type == R_MIPS_64 && bfd_big_endian (input_bfd))
4979 r = _bfd_relocate_contents (howto, input_bfd,
4980 addend,
4981 contents + rel->r_offset + 4);
4982 else if (r_type != R_MIPS_HI16 && r_type != R_MIPS_GOT16)
4983 r = _bfd_relocate_contents (howto, input_bfd,
4984 addend,
4985 contents + rel->r_offset);
4986 else
4987 {
4988 Elf_Internal_Rela *lorel;
4989
4990 /* As a GNU extension, permit an arbitrary
4991 number of R_MIPS_HI16 relocs before the
4992 R_MIPS_LO16 reloc. This permits gcc to emit
4993 the HI and LO relocs itself. */
4994 if (r_type == R_MIPS_GOT16)
4995 lorel = rel + 1;
4996 else
4997 {
4998 for (lorel = rel + 1;
4999 (lorel < relend
5000 && (ELF32_R_TYPE (lorel->r_info)
5001 == R_MIPS_HI16));
5002 lorel++)
5003 ;
5004 }
5005 if (lorel < relend
5006 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
5007 {
5008 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
5009 contents, addend);
5010 r = bfd_reloc_ok;
5011 }
5012 else
5013 r = _bfd_relocate_contents (howto, input_bfd,
5014 addend,
5015 contents + rel->r_offset);
5016 }
5017 }
5018 }
5019 }
5020 else
5021 {
5022 bfd_vma relocation;
5023 boolean local;
5024 boolean undefined_error;
5025
5026 /* This is a final link. */
5027 undefined_error = false;
5028 sym = NULL;
5029 if (r_symndx < extsymoff
5030 || (elf_bad_symtab (input_bfd)
5031 && local_sections[r_symndx] != NULL))
5032 {
5033 local = true;
5034 sym = local_syms + r_symndx;
5035 sec = local_sections[r_symndx];
5036 relocation = (sec->output_section->vma
5037 + sec->output_offset);
5038
5039 /* It would be logical to always add sym->st_value here,
5040 but Irix 5 sometimes generates a garbage symbol
5041 value. */
5042 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5043 relocation += sym->st_value;
5044
5045 /* mips16 text labels should be treated as odd. */
5046 if (sym->st_other == STO_MIPS16)
5047 ++relocation;
5048 }
5049 else
5050 {
5051 long indx;
5052
5053 local = false;
5054 indx = r_symndx - extsymoff;
5055 h = elf_sym_hashes (input_bfd)[indx];
5056 while (h->root.type == bfd_link_hash_indirect
5057 || h->root.type == bfd_link_hash_warning)
5058 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5059 if (strcmp (h->root.root.string, "_gp_disp") == 0)
5060 {
5061 if (gp == 0)
5062 {
5063 if (! ((*info->callbacks->reloc_dangerous)
5064 (info,
5065 _("_gp_disp used when GP not defined"),
5066 input_bfd, input_section,
5067 rel->r_offset)))
5068 return false;
5069 /* Only give the error once per link. */
5070 gp = 4;
5071 _bfd_set_gp_value (output_bfd, gp);
5072 relocation = 0;
5073 }
5074 else
5075 {
5076 sec = input_section;
5077 if (sec->output_section != NULL)
5078 relocation = (gp
5079 - (rel->r_offset
5080 + sec->output_section->vma
5081 + sec->output_offset));
5082 else
5083 relocation = gp - rel->r_offset;
5084 if (r_type == R_MIPS_LO16)
5085 relocation += 4;
5086 }
5087 }
5088 else if (h->root.type == bfd_link_hash_defined
5089 || h->root.type == bfd_link_hash_defweak)
5090 {
5091 sec = h->root.u.def.section;
5092 if (sec->output_section == NULL)
5093 relocation = 0;
5094 else
5095 relocation = (h->root.u.def.value
5096 + sec->output_section->vma
5097 + sec->output_offset);
5098 }
5099 else if (h->root.type == bfd_link_hash_undefweak)
5100 relocation = 0;
5101 else if (info->shared && !info->symbolic && !info->no_undefined)
5102 relocation = 0;
5103 else if (strcmp (h->root.root.string, "_DYNAMIC_LINK") == 0)
5104 {
5105 /* If this is a dynamic link, we should have created
5106 a _DYNAMIC_LINK symbol in
5107 mips_elf_create_dynamic_sections. Otherwise, we
5108 should define the symbol with a value of 0.
5109 FIXME: It should probably get into the symbol
5110 table somehow as well. */
5111 BFD_ASSERT (! info->shared);
5112 BFD_ASSERT (bfd_get_section_by_name (output_bfd,
5113 ".dynamic") == NULL);
5114 relocation = 0;
5115 }
5116 else
5117 {
5118 if (! ((*info->callbacks->undefined_symbol)
5119 (info, h->root.root.string, input_bfd,
5120 input_section, rel->r_offset)))
5121 return false;
5122 undefined_error = true;
5123 relocation = 0;
5124 }
5125 }
5126
5127 mh = (struct mips_elf_link_hash_entry *) h;
5128 if (h != NULL)
5129 other = h->other;
5130 else if (sym != NULL)
5131 other = sym->st_other;
5132 else
5133 other = 0;
5134
5135 /* If this function has an fn_stub, then it is a mips16
5136 function which needs a stub if it is called by a 32 bit
5137 function. If this reloc is anything other than a 16 bit
5138 call, redirect the reloc to the stub. We don't redirect
5139 relocs from other stub functions. */
5140 if (r_type != R_MIPS16_26
5141 && ((mh != NULL
5142 && mh->fn_stub != NULL)
5143 || (mh == NULL
5144 && elf_tdata (input_bfd)->local_stubs != NULL
5145 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5146 && strncmp (bfd_get_section_name (input_bfd, input_section),
5147 FN_STUB, sizeof FN_STUB - 1) != 0
5148 && strncmp (bfd_get_section_name (input_bfd, input_section),
5149 CALL_STUB, sizeof CALL_STUB - 1) != 0
5150 && strncmp (bfd_get_section_name (input_bfd, input_section),
5151 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) != 0)
5152 {
5153 if (mh != NULL)
5154 {
5155 BFD_ASSERT (mh->need_fn_stub);
5156 relocation = (mh->fn_stub->output_section->vma
5157 + mh->fn_stub->output_offset);
5158 }
5159 else
5160 {
5161 asection *fn_stub;
5162
5163 fn_stub = elf_tdata (input_bfd)->local_stubs[r_symndx];
5164 relocation = (fn_stub->output_section->vma
5165 + fn_stub->output_offset);
5166 }
5167
5168 /* RELOCATION now points to 32 bit code. */
5169 other = 0;
5170 }
5171
5172 /* If this function has a call_stub, then it is called by a
5173 mips16 function; the call needs to go through a stub if
5174 this function is a 32 bit function. If this reloc is a
5175 16 bit call, and the symbol is not a 16 bit function,
5176 then redirect the reloc to the stub. Note that we don't
5177 need to worry about calling the function through a
5178 function pointer; such calls are handled by routing
5179 through a special mips16 routine. We don't have to check
5180 whether this call is from a stub; it can't be, because a
5181 stub contains 32 bit code, and hence can not have a 16
5182 bit reloc. */
5183 if (r_type == R_MIPS16_26
5184 && mh != NULL
5185 && (mh->call_stub != NULL || mh->call_fp_stub != NULL)
5186 && other != STO_MIPS16)
5187 {
5188 asection *stub;
5189
5190 /* If both call_stub and call_fp_stub are defined, we
5191 can figure out which one to use by seeing which one
5192 appears in the input file. */
5193 if (mh->call_stub != NULL && mh->call_fp_stub != NULL)
5194 {
5195 asection *o;
5196
5197 stub = NULL;
5198 for (o = input_bfd->sections; o != NULL; o = o->next)
5199 {
5200 if (strncmp (bfd_get_section_name (input_bfd, o),
5201 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5202 {
5203 stub = mh->call_fp_stub;
5204 break;
5205 }
5206 }
5207 if (stub == NULL)
5208 stub = mh->call_stub;
5209 }
5210 else if (mh->call_stub != NULL)
5211 stub = mh->call_stub;
5212 else
5213 stub = mh->call_fp_stub;
5214
5215 BFD_ASSERT (stub->_raw_size > 0);
5216 relocation = stub->output_section->vma + stub->output_offset;
5217 }
5218
5219 if (r_type == R_MIPS_HI16)
5220 {
5221 Elf_Internal_Rela *lorel;
5222
5223 /* As a GNU extension, permit an arbitrary number of
5224 R_MIPS_HI16 relocs before the R_MIPS_LO16 reloc.
5225 This permits gcc to emit the HI and LO relocs itself. */
5226 for (lorel = rel + 1;
5227 (lorel < relend
5228 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_HI16);
5229 lorel++)
5230 ;
5231 if (lorel < relend
5232 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
5233 {
5234 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
5235 contents, relocation + addend);
5236 r = bfd_reloc_ok;
5237 }
5238 else
5239 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5240 contents, rel->r_offset,
5241 relocation, addend);
5242 }
5243 else if (r_type == R_MIPS_GOT16 && local)
5244 {
5245 /* GOT16 must also have an associated LO16 in the local
5246 case. In this case, the addend is extracted and the
5247 section in which the referenced object is determined.
5248 Then the final address of the object is computed and
5249 the GOT entry for the hipage (an aligned 64kb chunk)
5250 is added to .got section if needed. The offset field
5251 of the GOT16-relocated instruction is replaced by the
5252 index of this GOT entry for the hipage. */
5253 if ((rel + 1) < relend
5254 && ELF32_R_TYPE ((rel + 1)->r_info) == R_MIPS_LO16)
5255 {
5256 if (! mips_elf_relocate_got_local (output_bfd, input_bfd,
5257 sgot, rel, rel + 1,
5258 contents,
5259 relocation + addend))
5260 return false;
5261 r = bfd_reloc_ok;
5262 }
5263 else
5264 r = bfd_reloc_outofrange;
5265 }
5266 else if (r_type == R_MIPS_CALL16
5267 || r_type == R_MIPS_GOT16
5268 || r_type == R_MIPS_CALL_LO16
5269 || r_type == R_MIPS_GOT_LO16)
5270 {
5271 bfd_vma offset;
5272
5273 /* This symbol must be registered as a global symbol
5274 having the corresponding got entry. */
5275 BFD_ASSERT (h->got.offset != (bfd_vma) -1);
5276
5277 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5278 BFD_ASSERT (g->local_gotno <= offset
5279 && offset < sgot->_raw_size);
5280 bfd_put_32 (output_bfd, relocation + addend,
5281 sgot->contents + offset);
5282 offset = (sgot->output_section->vma + sgot->output_offset
5283 + offset - gp);
5284 mips_elf_relocate_global_got (input_bfd, rel, contents,
5285 offset);
5286 r = bfd_reloc_ok;
5287 }
5288 else if (r_type == R_MIPS_CALL_HI16
5289 || r_type == R_MIPS_GOT_HI16)
5290 {
5291 bfd_vma offset;
5292
5293 /* This must be a global symbol with a got entry. The
5294 next reloc must be the corresponding LO16 reloc. */
5295 BFD_ASSERT (h != NULL && h->got.offset != (bfd_vma) -1);
5296 BFD_ASSERT ((rel + 1) < relend);
5297 BFD_ASSERT ((int) ELF32_R_TYPE ((rel + 1)->r_info)
5298 == (r_type == R_MIPS_CALL_HI16
5299 ? (int) R_MIPS_CALL_LO16
5300 : (int) R_MIPS_GOT_LO16));
5301
5302 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5303 BFD_ASSERT (g->local_gotno <= offset
5304 && offset < sgot->_raw_size);
5305 bfd_put_32 (output_bfd, relocation + addend,
5306 sgot->contents + offset);
5307 offset = (sgot->output_section->vma + sgot->output_offset
5308 + offset - gp);
5309 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1, contents,
5310 offset);
5311 r = bfd_reloc_ok;
5312 }
5313 else if (r_type == R_MIPS_REL32
5314 || r_type == R_MIPS_32)
5315 {
5316 Elf_Internal_Rel outrel;
5317 Elf32_crinfo cptrel;
5318 bfd_byte *cr;
5319
5320 if ((info->shared
5321 || (elf_hash_table (info)->dynamic_sections_created
5322 && h != NULL
5323 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
5324 == 0)))
5325 && (input_section->flags & SEC_ALLOC) != 0)
5326 {
5327 boolean skip;
5328
5329 /* When generating a shared object, these
5330 relocations are copied into the output file to be
5331 resolved at run time. */
5332 if (sreloc == NULL)
5333 {
5334 sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
5335 BFD_ASSERT (sreloc != NULL);
5336 }
5337
5338 skip = false;
5339
5340 if (elf_section_data (input_section)->stab_info == NULL)
5341 outrel.r_offset = rel->r_offset;
5342 else
5343 {
5344 bfd_vma off;
5345
5346 off = (_bfd_stab_section_offset
5347 (output_bfd, &elf_hash_table (info)->stab_info,
5348 input_section,
5349 &elf_section_data (input_section)->stab_info,
5350 rel->r_offset));
5351 if (off == (bfd_vma) -1)
5352 skip = true;
5353 outrel.r_offset = off;
5354 }
5355
5356 outrel.r_offset += (input_section->output_section->vma
5357 + input_section->output_offset);
5358
5359 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
5360
5361 if (skip)
5362 memset (&outrel, 0, sizeof outrel);
5363 else if (h != NULL
5364 && (! info->symbolic
5365 || (h->elf_link_hash_flags
5366 & ELF_LINK_HASH_DEF_REGULAR) == 0))
5367 {
5368 BFD_ASSERT (h->dynindx != -1);
5369 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_REL32);
5370 sec = input_section;
5371 }
5372 else
5373 {
5374 long indx;
5375
5376 if (h == NULL)
5377 sec = local_sections[r_symndx];
5378 else
5379 {
5380 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5381 || (h->root.type
5382 == bfd_link_hash_defweak));
5383 sec = h->root.u.def.section;
5384 }
5385 if (sec != NULL && bfd_is_abs_section (sec))
5386 indx = 0;
5387 else if (sec == NULL || sec->owner == NULL)
5388 {
5389 bfd_set_error (bfd_error_bad_value);
5390 return false;
5391 }
5392 else
5393 {
5394 asection *osec;
5395
5396 osec = sec->output_section;
5397 indx = elf_section_data (osec)->dynindx;
5398 if (indx == 0)
5399 abort ();
5400 }
5401
5402 outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
5403 addend += relocation;
5404 }
5405
5406 if (! skip)
5407 bfd_put_32 (output_bfd, addend, contents + rel->r_offset);
5408
5409 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5410 (((Elf32_External_Rel *)
5411 sreloc->contents)
5412 + sreloc->reloc_count));
5413 ++sreloc->reloc_count;
5414
5415 if (! skip && SGI_COMPAT (output_bfd))
5416 {
5417 if (scpt == NULL)
5418 continue;
5419
5420 /* Make an entry of compact relocation info. */
5421 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5422 cptrel.vaddr = (rel->r_offset
5423 + input_section->output_section->vma
5424 + input_section->output_offset);
5425 if (r_type == R_MIPS_REL32)
5426 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5427 else
5428 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5429 mips_elf_set_cr_dist2to (cptrel, 0);
5430 cptrel.konst = addend;
5431
5432 cr = (scpt->contents
5433 + sizeof (Elf32_External_compact_rel));
5434 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5435 ((Elf32_External_crinfo *) cr
5436 + scpt->reloc_count));
5437 ++scpt->reloc_count;
5438 }
5439
5440 /* This reloc will be computed at runtime, so
5441 there's no need to do anything now. */
5442 continue;
5443 }
5444 else
5445 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5446 contents, rel->r_offset,
5447 relocation, addend);
5448 }
5449 else if (r_type == R_MIPS_64)
5450 {
5451 bfd_size_type addr;
5452 unsigned long val;
5453
5454 /* Do a 32 bit relocation, and sign extend to 64 bits. */
5455 addr = rel->r_offset;
5456 if (bfd_big_endian (input_bfd))
5457 addr += 4;
5458 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5459 contents, addr, relocation,
5460 addend);
5461 val = bfd_get_32 (input_bfd, contents + addr);
5462 if ((val & 0x80000000) != 0)
5463 val = 0xffffffff;
5464 else
5465 val = 0;
5466 addr = rel->r_offset;
5467 if (bfd_little_endian (input_bfd))
5468 addr += 4;
5469 bfd_put_32 (input_bfd, val, contents + addr);
5470 }
5471 else if (r_type == R_MIPS_26 && other == STO_MIPS16)
5472 {
5473 unsigned long insn;
5474
5475 /* This is a jump to a mips16 routine from a mips32
5476 routine. We need to change jal into jalx. */
5477 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5478 if (((insn >> 26) & 0x3f) != 0x3
5479 && ((insn >> 26) & 0x3f) != 0x1d)
5480 {
5481 (*_bfd_error_handler)
5482 (_("%s: %s+0x%lx: jump to mips16 routine which is not jal"),
5483 bfd_get_filename (input_bfd),
5484 input_section->name,
5485 (unsigned long) rel->r_offset);
5486 bfd_set_error (bfd_error_bad_value);
5487 return false;
5488 }
5489 insn = (insn & 0x3ffffff) | (0x1d << 26);
5490 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5491 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5492 contents, rel->r_offset,
5493 relocation, addend);
5494 }
5495 else if (r_type == R_MIPS16_26)
5496 {
5497 /* It's easiest to do the normal relocation, and then
5498 dig out the instruction and swap the first word the
5499 way the mips16 expects it. If this is little endian,
5500 though, we need to swap the two words first, and then
5501 swap them back again later, so that the address looks
5502 right. */
5503
5504 if (bfd_little_endian (input_bfd))
5505 {
5506 unsigned long insn;
5507
5508 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5509 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5510 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5511 }
5512
5513 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5514 contents, rel->r_offset,
5515 relocation, addend);
5516 if (r == bfd_reloc_ok)
5517 {
5518 unsigned long insn;
5519
5520 if (bfd_little_endian (input_bfd))
5521 {
5522 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5523 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5524 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5525 }
5526
5527 insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
5528 insn = ((insn & 0xfc00)
5529 | ((insn & 0x1f) << 5)
5530 | ((insn & 0x3e0) >> 5));
5531 /* If this is a jump to a 32 bit routine, then make
5532 it jalx. */
5533 if (other != STO_MIPS16)
5534 insn |= 0x400;
5535 bfd_put_16 (input_bfd, insn, contents + rel->r_offset);
5536 }
5537 }
5538 else if (r_type == R_MIPS16_GPREL)
5539 {
5540 unsigned short extend, insn;
5541 bfd_byte buf[4];
5542 unsigned long final;
5543
5544 /* Extract the addend into buf, run the regular reloc,
5545 and stuff the resulting value back into the
5546 instructions. */
5547 if (rel->r_offset > input_section->_raw_size)
5548 r = bfd_reloc_outofrange;
5549 else
5550 {
5551 extend = bfd_get_16 (input_bfd, contents + rel->r_offset);
5552 insn = bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
5553 bfd_put_32 (input_bfd,
5554 (((extend & 0x1f) << 11)
5555 | (extend & 0x7e0)
5556 | (insn & 0x1f)),
5557 buf);
5558 r = _bfd_final_link_relocate (howto, input_bfd,
5559 input_section, buf,
5560 (bfd_vma) 0, relocation,
5561 addend);
5562 final = bfd_get_32 (input_bfd, buf);
5563 bfd_put_16 (input_bfd,
5564 ((extend & 0xf800)
5565 | ((final >> 11) & 0x1f)
5566 | (final & 0x7e0)),
5567 contents + rel->r_offset);
5568 bfd_put_16 (input_bfd,
5569 ((insn & 0xffe0)
5570 | (final & 0x1f)),
5571 contents + rel->r_offset + 2);
5572 }
5573 }
5574 else
5575 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5576 contents, rel->r_offset,
5577 relocation, addend);
5578
5579 /* The jal instruction can only jump to an address which is
5580 divisible by 4, and it can only jump to an address with
5581 the same upper 4 bits as the PC. */
5582 if (r == bfd_reloc_ok
5583 && (r_type == R_MIPS16_26 || r_type == R_MIPS_26))
5584 {
5585 bfd_vma addr;
5586 bfd_vma pc;
5587 bfd_vma target;
5588
5589 pc = ((input_section->output_section->vma
5590 + input_section->output_offset
5591 + rel->r_offset)
5592 & 0xf0000000);
5593 target = bfd_get_32 (input_bfd, contents + rel->r_offset)
5594 & (howto->dst_mask);
5595 addr = (target << 2) + pc;
5596 if (other == STO_MIPS16)
5597 addr &= ~ (bfd_vma) 1;
5598 if ((addr & 3) != 0
5599 || ((addr & 0xf0000000)
5600 != pc))
5601 r = bfd_reloc_overflow;
5602 }
5603
5604 /* Don't bother to report a relocation overflow for a call
5605 to a weak undefined symbol with a value of zero. This
5606 permits us to use
5607 if (!f) f();
5608 even if we aren't in range to call address zero. */
5609 if (r == bfd_reloc_overflow
5610 && (r_type == R_MIPS16_26 || r_type == R_MIPS_26)
5611 && relocation + addend == 0
5612 && h != NULL
5613 && h->root.type == bfd_link_hash_undefweak)
5614 r = bfd_reloc_ok;
5615
5616 /* If we've already issued an error for an undefined symbol,
5617 don't issue another useless error. */
5618 if (undefined_error
5619 && (r == bfd_reloc_undefined || r == bfd_reloc_overflow))
5620 r = bfd_reloc_ok;
5621
5622 if (SGI_COMPAT (abfd)
5623 && scpt != NULL
5624 && (input_section->flags & SEC_ALLOC) != 0)
5625 {
5626 Elf32_crinfo cptrel;
5627 bfd_byte *cr;
5628
5629 /* Make an entry of compact relocation info. */
5630 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5631 cptrel.vaddr = (rel->r_offset
5632 + input_section->output_section->vma
5633 + input_section->output_offset);
5634
5635 switch (r_type)
5636 {
5637 case R_MIPS_26:
5638 mips_elf_set_cr_type (cptrel, CRT_MIPS_JMPAD);
5639 /* XXX How should we set dist2to in this case. */
5640 mips_elf_set_cr_dist2to (cptrel, 8);
5641 cptrel.konst = addend + relocation;
5642 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5643 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5644 ((Elf32_External_crinfo *) cr
5645 + scpt->reloc_count));
5646 ++scpt->reloc_count;
5647 break;
5648
5649 case R_MIPS_GPREL16:
5650 case R_MIPS_LITERAL:
5651 case R_MIPS_GPREL32:
5652 mips_elf_set_cr_type (cptrel, CRT_MIPS_GPHI_LO);
5653 cptrel.konst = gp - cptrel.vaddr;
5654 mips_elf_set_cr_dist2to (cptrel, 4);
5655 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5656 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5657 ((Elf32_External_crinfo *) cr
5658 + scpt->reloc_count));
5659 ++scpt->reloc_count;
5660 break;
5661
5662 default:
5663 break;
5664 }
5665 }
5666 }
5667
5668 if (r != bfd_reloc_ok)
5669 {
5670 switch (r)
5671 {
5672 default:
5673 case bfd_reloc_outofrange:
5674 abort ();
5675 case bfd_reloc_overflow:
5676 {
5677 const char *name;
5678
5679 if (h != NULL)
5680 name = h->root.root.string;
5681 else
5682 {
5683 name = bfd_elf_string_from_elf_section (input_bfd,
5684 symtab_hdr->sh_link,
5685 sym->st_name);
5686 if (name == NULL)
5687 return false;
5688 if (*name == '\0')
5689 name = bfd_section_name (input_bfd, sec);
5690 }
5691 if (! ((*info->callbacks->reloc_overflow)
5692 (info, name, howto->name, (bfd_vma) 0,
5693 input_bfd, input_section, rel->r_offset)))
5694 return false;
5695 }
5696 break;
5697 }
5698 }
5699 }
5700
5701 return true;
5702}
5703
5704/* This hook function is called before the linker writes out a global
5705 symbol. We mark symbols as small common if appropriate. This is
5706 also where we undo the increment of the value for a mips16 symbol. */
5707
5708/*ARGSIGNORED*/
5709static boolean
5710mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
5711 bfd *abfd;
5712 struct bfd_link_info *info;
5713 const char *name;
5714 Elf_Internal_Sym *sym;
5715 asection *input_sec;
5716{
5717 /* If we see a common symbol, which implies a relocatable link, then
5718 if a symbol was small common in an input file, mark it as small
5719 common in the output file. */
5720 if (sym->st_shndx == SHN_COMMON
5721 && strcmp (input_sec->name, ".scommon") == 0)
5722 sym->st_shndx = SHN_MIPS_SCOMMON;
5723
5724 if (sym->st_other == STO_MIPS16
5725 && (sym->st_value & 1) != 0)
5726 --sym->st_value;
5727
5728 return true;
5729}
5730\f
5731/* Functions for the dynamic linker. */
5732
5733/* The name of the dynamic interpreter. This is put in the .interp
5734 section. */
5735
5736#define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
5737
5738/* Create dynamic sections when linking against a dynamic object. */
5739
5740static boolean
5741mips_elf_create_dynamic_sections (abfd, info)
5742 bfd *abfd;
5743 struct bfd_link_info *info;
5744{
5745 struct elf_link_hash_entry *h;
5746 flagword flags;
5747 register asection *s;
5748 const char * const *namep;
5749
5750 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5751 | SEC_LINKER_CREATED | SEC_READONLY);
5752
5753 /* Mips ABI requests the .dynamic section to be read only. */
5754 s = bfd_get_section_by_name (abfd, ".dynamic");
5755 if (s != NULL)
5756 {
5757 if (! bfd_set_section_flags (abfd, s, flags))
5758 return false;
5759 }
5760
5761 /* We need to create .got section. */
5762 if (! mips_elf_create_got_section (abfd, info))
5763 return false;
5764
5765 /* Create .stub section. */
5766 if (bfd_get_section_by_name (abfd, ".stub") == NULL)
5767 {
5768 s = bfd_make_section (abfd, ".stub");
5769 if (s == NULL
5770 || ! bfd_set_section_flags (abfd, s, flags)
5771 || ! bfd_set_section_alignment (abfd, s, 2))
5772 return false;
5773 }
5774
5775 if (SGI_COMPAT (abfd)
5776 && !info->shared
5777 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
5778 {
5779 s = bfd_make_section (abfd, ".rld_map");
5780 if (s == NULL
5781 || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
5782 || ! bfd_set_section_alignment (abfd, s, 2))
5783 return false;
5784 }
5785
5786 if (SGI_COMPAT (abfd))
5787 {
5788 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
5789 {
5790 h = NULL;
5791 if (! (_bfd_generic_link_add_one_symbol
5792 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
5793 (bfd_vma) 0, (const char *) NULL, false,
5794 get_elf_backend_data (abfd)->collect,
5795 (struct bfd_link_hash_entry **) &h)))
5796 return false;
5797 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5798 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5799 h->type = STT_SECTION;
5800
5801 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5802 return false;
5803 }
5804
5805 /* We need to create a .compact_rel section. */
5806 if (! mips_elf_create_compact_rel_section (abfd, info))
5807 return false;
5808
5809 /* Change aligments of some sections. */
5810 s = bfd_get_section_by_name (abfd, ".hash");
5811 if (s != NULL)
5812 bfd_set_section_alignment (abfd, s, 4);
5813 s = bfd_get_section_by_name (abfd, ".dynsym");
5814 if (s != NULL)
5815 bfd_set_section_alignment (abfd, s, 4);
5816 s = bfd_get_section_by_name (abfd, ".dynstr");
5817 if (s != NULL)
5818 bfd_set_section_alignment (abfd, s, 4);
5819 s = bfd_get_section_by_name (abfd, ".reginfo");
5820 if (s != NULL)
5821 bfd_set_section_alignment (abfd, s, 4);
5822 s = bfd_get_section_by_name (abfd, ".dynamic");
5823 if (s != NULL)
5824 bfd_set_section_alignment (abfd, s, 4);
5825 }
5826
5827 if (!info->shared)
5828 {
5829 h = NULL;
5830 if (! (_bfd_generic_link_add_one_symbol
5831 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
5832 (bfd_vma) 0, (const char *) NULL, false,
5833 get_elf_backend_data (abfd)->collect,
5834 (struct bfd_link_hash_entry **) &h)))
5835 return false;
5836 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5837 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5838 h->type = STT_SECTION;
5839
5840 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5841 return false;
5842
5843 if (! mips_elf_hash_table (info)->use_rld_obj_head)
5844 {
5845 /* __rld_map is a four byte word located in the .data section
5846 and is filled in by the rtld to contain a pointer to
5847 the _r_debug structure. Its symbol value will be set in
5848 mips_elf_finish_dynamic_symbol. */
5849 s = bfd_get_section_by_name (abfd, ".rld_map");
5850 BFD_ASSERT (s != NULL);
5851
5852 h = NULL;
5853 if (! (_bfd_generic_link_add_one_symbol
5854 (info, abfd, "__rld_map", BSF_GLOBAL, s,
5855 (bfd_vma) 0, (const char *) NULL, false,
5856 get_elf_backend_data (abfd)->collect,
5857 (struct bfd_link_hash_entry **) &h)))
5858 return false;
5859 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5860 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5861 h->type = STT_OBJECT;
5862
5863 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5864 return false;
5865 }
5866 }
5867
5868 return true;
5869}
5870
5871/* Create the .compact_rel section. */
5872
5873static boolean
5874mips_elf_create_compact_rel_section (abfd, info)
5875 bfd *abfd;
5876 struct bfd_link_info *info;
5877{
5878 flagword flags;
5879 register asection *s;
5880
5881 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
5882 {
5883 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5884 | SEC_READONLY);
5885
5886 s = bfd_make_section (abfd, ".compact_rel");
5887 if (s == NULL
5888 || ! bfd_set_section_flags (abfd, s, flags)
5889 || ! bfd_set_section_alignment (abfd, s, 2))
5890 return false;
5891
5892 s->_raw_size = sizeof (Elf32_External_compact_rel);
5893 }
5894
5895 return true;
5896}
5897
5898/* Create the .got section to hold the global offset table. */
5899
5900static boolean
5901mips_elf_create_got_section (abfd, info)
5902 bfd *abfd;
5903 struct bfd_link_info *info;
5904{
5905 flagword flags;
5906 register asection *s;
5907 struct elf_link_hash_entry *h;
5908 struct mips_got_info *g;
5909
5910 /* This function may be called more than once. */
5911 if (bfd_get_section_by_name (abfd, ".got") != NULL)
5912 return true;
5913
5914 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5915 | SEC_LINKER_CREATED);
5916
5917 s = bfd_make_section (abfd, ".got");
5918 if (s == NULL
5919 || ! bfd_set_section_flags (abfd, s, flags)
5920 || ! bfd_set_section_alignment (abfd, s, 4))
5921 return false;
5922
5923 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5924 linker script because we don't want to define the symbol if we
5925 are not creating a global offset table. */
5926 h = NULL;
5927 if (! (_bfd_generic_link_add_one_symbol
5928 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5929 (bfd_vma) 0, (const char *) NULL, false,
5930 get_elf_backend_data (abfd)->collect,
5931 (struct bfd_link_hash_entry **) &h)))
5932 return false;
5933 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5934 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5935 h->type = STT_OBJECT;
5936
5937 if (info->shared
5938 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
5939 return false;
5940
5941 /* The first several global offset table entries are reserved. */
5942 s->_raw_size = MIPS_RESERVED_GOTNO * 4;
5943
5944 g = (struct mips_got_info *) bfd_alloc (abfd,
5945 sizeof (struct mips_got_info));
5946 if (g == NULL)
5947 return false;
5948 g->global_gotsym = 0;
5949 g->local_gotno = MIPS_RESERVED_GOTNO;
5950 g->assigned_gotno = MIPS_RESERVED_GOTNO;
5951 if (elf_section_data (s) == NULL)
5952 {
5953 s->used_by_bfd =
5954 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
5955 if (elf_section_data (s) == NULL)
5956 return false;
5957 }
5958 elf_section_data (s)->tdata = (PTR) g;
5959
5960 return true;
5961}
5962
5963/* Look through the relocs for a section during the first phase, and
5964 allocate space in the global offset table. */
5965
5966static boolean
5967mips_elf_check_relocs (abfd, info, sec, relocs)
5968 bfd *abfd;
5969 struct bfd_link_info *info;
5970 asection *sec;
5971 const Elf_Internal_Rela *relocs;
5972{
5973 const char *name;
5974 bfd *dynobj;
5975 Elf_Internal_Shdr *symtab_hdr;
5976 struct elf_link_hash_entry **sym_hashes;
5977 struct mips_got_info *g;
5978 size_t extsymoff;
5979 const Elf_Internal_Rela *rel;
5980 const Elf_Internal_Rela *rel_end;
5981 asection *sgot;
5982 asection *sreloc;
5983
5984 if (info->relocateable)
5985 return true;
5986
5987 dynobj = elf_hash_table (info)->dynobj;
5988 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5989 sym_hashes = elf_sym_hashes (abfd);
5990 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
5991
5992 /* Check for the mips16 stub sections. */
5993
5994 name = bfd_get_section_name (abfd, sec);
5995 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
5996 {
5997 unsigned long r_symndx;
5998
5999 /* Look at the relocation information to figure out which symbol
6000 this is for. */
6001
6002 r_symndx = ELF32_R_SYM (relocs->r_info);
6003
6004 if (r_symndx < extsymoff
6005 || sym_hashes[r_symndx - extsymoff] == NULL)
6006 {
6007 asection *o;
6008
6009 /* This stub is for a local symbol. This stub will only be
6010 needed if there is some relocation in this BFD, other
6011 than a 16 bit function call, which refers to this symbol. */
6012 for (o = abfd->sections; o != NULL; o = o->next)
6013 {
6014 Elf_Internal_Rela *sec_relocs;
6015 const Elf_Internal_Rela *r, *rend;
6016
6017 /* We can ignore stub sections when looking for relocs. */
6018 if ((o->flags & SEC_RELOC) == 0
6019 || o->reloc_count == 0
6020 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
6021 sizeof FN_STUB - 1) == 0
6022 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
6023 sizeof CALL_STUB - 1) == 0
6024 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
6025 sizeof CALL_FP_STUB - 1) == 0)
6026 continue;
6027
6028 sec_relocs = (_bfd_elf32_link_read_relocs
6029 (abfd, o, (PTR) NULL,
6030 (Elf_Internal_Rela *) NULL,
6031 info->keep_memory));
6032 if (sec_relocs == NULL)
6033 return false;
6034
6035 rend = sec_relocs + o->reloc_count;
6036 for (r = sec_relocs; r < rend; r++)
6037 if (ELF32_R_SYM (r->r_info) == r_symndx
6038 && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
6039 break;
6040
6041 if (! info->keep_memory)
6042 free (sec_relocs);
6043
6044 if (r < rend)
6045 break;
6046 }
6047
6048 if (o == NULL)
6049 {
6050 /* There is no non-call reloc for this stub, so we do
6051 not need it. Since this function is called before
6052 the linker maps input sections to output sections, we
6053 can easily discard it by setting the SEC_EXCLUDE
6054 flag. */
6055 sec->flags |= SEC_EXCLUDE;
6056 return true;
6057 }
6058
6059 /* Record this stub in an array of local symbol stubs for
6060 this BFD. */
6061 if (elf_tdata (abfd)->local_stubs == NULL)
6062 {
6063 unsigned long symcount;
6064 asection **n;
6065
6066 if (elf_bad_symtab (abfd))
6067 symcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
6068 else
6069 symcount = symtab_hdr->sh_info;
6070 n = (asection **) bfd_zalloc (abfd,
6071 symcount * sizeof (asection *));
6072 if (n == NULL)
6073 return false;
6074 elf_tdata (abfd)->local_stubs = n;
6075 }
6076
6077 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
6078
6079 /* We don't need to set mips16_stubs_seen in this case.
6080 That flag is used to see whether we need to look through
6081 the global symbol table for stubs. We don't need to set
6082 it here, because we just have a local stub. */
6083 }
6084 else
6085 {
6086 struct mips_elf_link_hash_entry *h;
6087
6088 h = ((struct mips_elf_link_hash_entry *)
6089 sym_hashes[r_symndx - extsymoff]);
6090
6091 /* H is the symbol this stub is for. */
6092
6093 h->fn_stub = sec;
6094 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6095 }
6096 }
6097 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
6098 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6099 {
6100 unsigned long r_symndx;
6101 struct mips_elf_link_hash_entry *h;
6102 asection **loc;
6103
6104 /* Look at the relocation information to figure out which symbol
6105 this is for. */
6106
6107 r_symndx = ELF32_R_SYM (relocs->r_info);
6108
6109 if (r_symndx < extsymoff
6110 || sym_hashes[r_symndx - extsymoff] == NULL)
6111 {
6112 /* This stub was actually built for a static symbol defined
6113 in the same file. We assume that all static symbols in
6114 mips16 code are themselves mips16, so we can simply
6115 discard this stub. Since this function is called before
6116 the linker maps input sections to output sections, we can
6117 easily discard it by setting the SEC_EXCLUDE flag. */
6118 sec->flags |= SEC_EXCLUDE;
6119 return true;
6120 }
6121
6122 h = ((struct mips_elf_link_hash_entry *)
6123 sym_hashes[r_symndx - extsymoff]);
6124
6125 /* H is the symbol this stub is for. */
6126
6127 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6128 loc = &h->call_fp_stub;
6129 else
6130 loc = &h->call_stub;
6131
6132 /* If we already have an appropriate stub for this function, we
6133 don't need another one, so we can discard this one. Since
6134 this function is called before the linker maps input sections
6135 to output sections, we can easily discard it by setting the
6136 SEC_EXCLUDE flag. We can also discard this section if we
6137 happen to already know that this is a mips16 function; it is
6138 not necessary to check this here, as it is checked later, but
6139 it is slightly faster to check now. */
6140 if (*loc != NULL || h->root.other == STO_MIPS16)
6141 {
6142 sec->flags |= SEC_EXCLUDE;
6143 return true;
6144 }
6145
6146 *loc = sec;
6147 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6148 }
6149
6150 if (dynobj == NULL)
6151 {
6152 sgot = NULL;
6153 g = NULL;
6154 }
6155 else
6156 {
6157 sgot = bfd_get_section_by_name (dynobj, ".got");
6158 if (sgot == NULL)
6159 g = NULL;
6160 else
6161 {
6162 BFD_ASSERT (elf_section_data (sgot) != NULL);
6163 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6164 BFD_ASSERT (g != NULL);
6165 }
6166 }
6167
6168 sreloc = NULL;
6169
6170 rel_end = relocs + sec->reloc_count;
6171 for (rel = relocs; rel < rel_end; rel++)
6172 {
6173 unsigned long r_symndx;
6174 struct elf_link_hash_entry *h;
6175
6176 r_symndx = ELF32_R_SYM (rel->r_info);
6177
6178 if (r_symndx < extsymoff)
6179 h = NULL;
6180 else
6181 {
6182 h = sym_hashes[r_symndx - extsymoff];
6183
6184 /* This may be an indirect symbol created because of a version. */
6185 if (h != NULL)
6186 {
6187 while (h->root.type == bfd_link_hash_indirect)
6188 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6189 }
6190 }
6191
6192 /* Some relocs require a global offset table. */
6193 if (dynobj == NULL || sgot == NULL)
6194 {
6195 switch (ELF32_R_TYPE (rel->r_info))
6196 {
6197 case R_MIPS_GOT16:
6198 case R_MIPS_CALL16:
6199 case R_MIPS_CALL_HI16:
6200 case R_MIPS_CALL_LO16:
6201 case R_MIPS_GOT_HI16:
6202 case R_MIPS_GOT_LO16:
6203 if (dynobj == NULL)
6204 elf_hash_table (info)->dynobj = dynobj = abfd;
6205 if (! mips_elf_create_got_section (dynobj, info))
6206 return false;
6207 sgot = bfd_get_section_by_name (dynobj, ".got");
6208 BFD_ASSERT (sgot != NULL);
6209 BFD_ASSERT (elf_section_data (sgot) != NULL);
6210 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6211 BFD_ASSERT (g != NULL);
6212 break;
6213
6214 case R_MIPS_32:
6215 case R_MIPS_REL32:
6216 if (dynobj == NULL
6217 && (info->shared || h != NULL)
6218 && (sec->flags & SEC_ALLOC) != 0)
6219 elf_hash_table (info)->dynobj = dynobj = abfd;
6220 break;
6221
6222 default:
6223 break;
6224 }
6225 }
6226
6227 switch (ELF32_R_TYPE (rel->r_info))
6228 {
6229 case R_MIPS_CALL16:
6230 case R_MIPS_CALL_HI16:
6231 case R_MIPS_CALL_LO16:
6232 /* This symbol requires a global offset table entry. */
6233
6234 if (h == NULL)
6235 {
6236 (*_bfd_error_handler)
6237 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
6238 bfd_get_filename (abfd), (unsigned long) rel->r_offset);
6239 bfd_set_error (bfd_error_bad_value);
6240 return false;
6241 }
6242
6243 /* Make sure this symbol is output as a dynamic symbol. */
6244 if (h->dynindx == -1)
6245 {
6246 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6247 return false;
6248 }
6249
6250 if (h->got.offset != (bfd_vma) -1)
6251 {
6252 /* We have already allocated space in the .got. */
6253 break;
6254 }
6255
6256 /* Note the index of the first global got symbol in .dynsym. */
6257 if (g->global_gotsym == 0
6258 || g->global_gotsym > (unsigned long) h->dynindx)
6259 g->global_gotsym = h->dynindx;
6260
6261 /* Make this symbol to have the corresponding got entry. */
6262 h->got.offset = 0;
6263
6264 /* We need a stub, not a plt entry for the undefined
6265 function. But we record it as if it needs plt. See
6266 elf_adjust_dynamic_symbol in elflink.h. */
6267 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
6268 h->type = STT_FUNC;
6269
6270 break;
6271
6272 case R_MIPS_GOT16:
6273 case R_MIPS_GOT_HI16:
6274 case R_MIPS_GOT_LO16:
6275 /* This symbol requires a global offset table entry. */
6276
6277 if (h != NULL)
6278 {
6279 /* Make sure this symbol is output as a dynamic symbol. */
6280 if (h->dynindx == -1)
6281 {
6282 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6283 return false;
6284 }
6285
6286 if (h->got.offset != (bfd_vma) -1)
6287 {
6288 /* We have already allocated space in the .got. */
6289 break;
6290 }
6291 /* Note the index of the first global got symbol in
6292 .dynsym. */
6293 if (g->global_gotsym == 0
6294 || g->global_gotsym > (unsigned long) h->dynindx)
6295 g->global_gotsym = h->dynindx;
6296
6297 /* Make this symbol to be the global got symbol. */
6298 h->got.offset = 0;
6299 }
6300
6301 break;
6302
6303 case R_MIPS_32:
6304 case R_MIPS_REL32:
6305 if ((info->shared || h != NULL)
6306 && (sec->flags & SEC_ALLOC) != 0)
6307 {
6308 if (sreloc == NULL)
6309 {
6310 const char *name = ".rel.dyn";
6311
6312 sreloc = bfd_get_section_by_name (dynobj, name);
6313 if (sreloc == NULL)
6314 {
6315 sreloc = bfd_make_section (dynobj, name);
6316 if (sreloc == NULL
6317 || ! bfd_set_section_flags (dynobj, sreloc,
6318 (SEC_ALLOC
6319 | SEC_LOAD
6320 | SEC_HAS_CONTENTS
6321 | SEC_IN_MEMORY
6322 | SEC_LINKER_CREATED
6323 | SEC_READONLY))
6324 || ! bfd_set_section_alignment (dynobj, sreloc,
6325 4))
6326 return false;
6327 }
6328 }
6329 if (info->shared)
6330 {
6331 /* When creating a shared object, we must copy these
6332 reloc types into the output file as R_MIPS_REL32
6333 relocs. We make room for this reloc in the
6334 .rel.dyn reloc section */
6335 if (sreloc->_raw_size == 0)
6336 {
6337 /* Add a null element. */
6338 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6339 ++sreloc->reloc_count;
6340 }
6341 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6342 }
6343 else
6344 {
6345 struct mips_elf_link_hash_entry *hmips;
6346
6347 /* We only need to copy this reloc if the symbol is
6348 defined in a dynamic object. */
6349 hmips = (struct mips_elf_link_hash_entry *) h;
6350 ++hmips->mips_32_relocs;
6351 }
6352 }
6353
6354 if (SGI_COMPAT (abfd))
6355 mips_elf_hash_table (info)->compact_rel_size +=
6356 sizeof (Elf32_External_crinfo);
6357
6358 break;
6359
6360 case R_MIPS_26:
6361 case R_MIPS_GPREL16:
6362 case R_MIPS_LITERAL:
6363 case R_MIPS_GPREL32:
6364 if (SGI_COMPAT (abfd))
6365 mips_elf_hash_table (info)->compact_rel_size +=
6366 sizeof (Elf32_External_crinfo);
6367 break;
6368
6369 /* This relocation describes the C++ object vtable hierarchy.
6370 Reconstruct it for later use during GC. */
6371 case R_MIPS_GNU_VTINHERIT:
6372 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6373 return false;
6374 break;
6375
6376 /* This relocation describes which C++ vtable entries are actually
6377 used. Record for later use during GC. */
6378 case R_MIPS_GNU_VTENTRY:
6379 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6380 return false;
6381 break;
6382
6383 default:
6384 break;
6385 }
6386
6387 /* If this reloc is not a 16 bit call, and it has a global
6388 symbol, then we will need the fn_stub if there is one.
6389 References from a stub section do not count. */
6390 if (h != NULL
6391 && ELF32_R_TYPE (rel->r_info) != R_MIPS16_26
6392 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
6393 sizeof FN_STUB - 1) != 0
6394 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
6395 sizeof CALL_STUB - 1) != 0
6396 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
6397 sizeof CALL_FP_STUB - 1) != 0)
6398 {
6399 struct mips_elf_link_hash_entry *mh;
6400
6401 mh = (struct mips_elf_link_hash_entry *) h;
6402 mh->need_fn_stub = true;
6403 }
6404 }
6405
6406 return true;
6407}
6408
6409/* Return the section that should be marked against GC for a given
6410 relocation. */
6411
6412static asection *
6413mips_elf_gc_mark_hook (abfd, info, rel, h, sym)
6414 bfd *abfd;
6415 struct bfd_link_info *info;
6416 Elf_Internal_Rela *rel;
6417 struct elf_link_hash_entry *h;
6418 Elf_Internal_Sym *sym;
6419{
6420 /* ??? Do mips16 stub sections need to be handled special? */
6421
6422 if (h != NULL)
6423 {
6424 switch (ELF32_R_TYPE (rel->r_info))
6425 {
6426 case R_MIPS_GNU_VTINHERIT:
6427 case R_MIPS_GNU_VTENTRY:
6428 break;
6429
6430 default:
6431 switch (h->root.type)
6432 {
6433 case bfd_link_hash_defined:
6434 case bfd_link_hash_defweak:
6435 return h->root.u.def.section;
6436
6437 case bfd_link_hash_common:
6438 return h->root.u.c.p->section;
6439
6440 default:
6441 break;
6442 }
6443 }
6444 }
6445 else
6446 {
6447 if (!(elf_bad_symtab (abfd)
6448 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
6449 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
6450 && sym->st_shndx != SHN_COMMON))
6451 {
6452 return bfd_section_from_elf_index (abfd, sym->st_shndx);
6453 }
6454 }
6455
6456 return NULL;
6457}
6458
6459/* Update the got entry reference counts for the section being removed. */
6460
6461static boolean
6462mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
6463 bfd *abfd;
6464 struct bfd_link_info *info;
6465 asection *sec;
6466 const Elf_Internal_Rela *relocs;
6467{
6468#if 0
6469 Elf_Internal_Shdr *symtab_hdr;
6470 struct elf_link_hash_entry **sym_hashes;
6471 bfd_signed_vma *local_got_refcounts;
6472 const Elf_Internal_Rela *rel, *relend;
6473 unsigned long r_symndx;
6474 struct elf_link_hash_entry *h;
6475
6476 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6477 sym_hashes = elf_sym_hashes (abfd);
6478 local_got_refcounts = elf_local_got_refcounts (abfd);
6479
6480 relend = relocs + sec->reloc_count;
6481 for (rel = relocs; rel < relend; rel++)
6482 switch (ELF32_R_TYPE (rel->r_info))
6483 {
6484 case R_MIPS_GOT16:
6485 case R_MIPS_CALL16:
6486 case R_MIPS_CALL_HI16:
6487 case R_MIPS_CALL_LO16:
6488 case R_MIPS_GOT_HI16:
6489 case R_MIPS_GOT_LO16:
6490 /* ??? It would seem that the existing MIPS code does no sort
6491 of reference counting or whatnot on its GOT and PLT entries,
6492 so it is not possible to garbage collect them at this time. */
6493 break;
6494
6495 default:
6496 break;
6497 }
6498#endif
6499
6500 return true;
6501}
6502
6503
6504/* Adjust a symbol defined by a dynamic object and referenced by a
6505 regular object. The current definition is in some section of the
6506 dynamic object, but we're not including those sections. We have to
6507 change the definition to something the rest of the link can
6508 understand. */
6509
6510static boolean
6511mips_elf_adjust_dynamic_symbol (info, h)
6512 struct bfd_link_info *info;
6513 struct elf_link_hash_entry *h;
6514{
6515 bfd *dynobj;
6516 struct mips_elf_link_hash_entry *hmips;
6517 asection *s;
6518
6519 dynobj = elf_hash_table (info)->dynobj;
6520
6521 /* Make sure we know what is going on here. */
6522 BFD_ASSERT (dynobj != NULL
6523 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
6524 || h->weakdef != NULL
6525 || ((h->elf_link_hash_flags
6526 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6527 && (h->elf_link_hash_flags
6528 & ELF_LINK_HASH_REF_REGULAR) != 0
6529 && (h->elf_link_hash_flags
6530 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
6531
6532 /* If this symbol is defined in a dynamic object, we need to copy
6533 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
6534 file. */
6535 hmips = (struct mips_elf_link_hash_entry *) h;
6536 if (! info->relocateable
6537 && hmips->mips_32_relocs != 0
6538 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6539 {
6540 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
6541 BFD_ASSERT (s != NULL);
6542
6543 if (s->_raw_size == 0)
6544 {
6545 /* Make room for a null element. */
6546 s->_raw_size += sizeof (Elf32_External_Rel);
6547 ++s->reloc_count;
6548 }
6549 s->_raw_size += hmips->mips_32_relocs * sizeof (Elf32_External_Rel);
6550 }
6551
6552 /* For a function, create a stub, if needed. */
6553 if (h->type == STT_FUNC
6554 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
6555 {
6556 if (! elf_hash_table (info)->dynamic_sections_created)
6557 return true;
6558
6559 /* If this symbol is not defined in a regular file, then set
6560 the symbol to the stub location. This is required to make
6561 function pointers compare as equal between the normal
6562 executable and the shared library. */
6563 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6564 {
6565 /* We need .stub section. */
6566 s = bfd_get_section_by_name (dynobj, ".stub");
6567 BFD_ASSERT (s != NULL);
6568
6569 h->root.u.def.section = s;
6570 h->root.u.def.value = s->_raw_size;
6571
6572 /* XXX Write this stub address somewhere. */
6573 h->plt.offset = s->_raw_size;
6574
6575 /* Make room for this stub code. */
6576 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6577
6578 /* The last half word of the stub will be filled with the index
6579 of this symbol in .dynsym section. */
6580 return true;
6581 }
6582 }
6583
6584 /* If this is a weak symbol, and there is a real definition, the
6585 processor independent code will have arranged for us to see the
6586 real definition first, and we can just use the same value. */
6587 if (h->weakdef != NULL)
6588 {
6589 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
6590 || h->weakdef->root.type == bfd_link_hash_defweak);
6591 h->root.u.def.section = h->weakdef->root.u.def.section;
6592 h->root.u.def.value = h->weakdef->root.u.def.value;
6593 return true;
6594 }
6595
6596 /* This is a reference to a symbol defined by a dynamic object which
6597 is not a function. */
6598
6599 return true;
6600}
6601
6602/* This function is called after all the input files have been read,
6603 and the input sections have been assigned to output sections. We
6604 check for any mips16 stub sections that we can discard. */
6605
6606static boolean mips_elf_check_mips16_stubs
6607 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
6608
6609static boolean
6610mips_elf_always_size_sections (output_bfd, info)
6611 bfd *output_bfd;
6612 struct bfd_link_info *info;
6613{
6614 asection *ri;
6615
6616 /* The .reginfo section has a fixed size. */
6617 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
6618 if (ri != NULL)
6619 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
6620
6621 if (info->relocateable
6622 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
6623 return true;
6624
6625 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
6626 mips_elf_check_mips16_stubs,
6627 (PTR) NULL);
6628
6629 return true;
6630}
6631
6632/* Check the mips16 stubs for a particular symbol, and see if we can
6633 discard them. */
6634
6635/*ARGSUSED*/
6636static boolean
6637mips_elf_check_mips16_stubs (h, data)
6638 struct mips_elf_link_hash_entry *h;
6639 PTR data;
6640{
6641 if (h->fn_stub != NULL
6642 && ! h->need_fn_stub)
6643 {
6644 /* We don't need the fn_stub; the only references to this symbol
6645 are 16 bit calls. Clobber the size to 0 to prevent it from
6646 being included in the link. */
6647 h->fn_stub->_raw_size = 0;
6648 h->fn_stub->_cooked_size = 0;
6649 h->fn_stub->flags &= ~ SEC_RELOC;
6650 h->fn_stub->reloc_count = 0;
6651 h->fn_stub->flags |= SEC_EXCLUDE;
6652 }
6653
6654 if (h->call_stub != NULL
6655 && h->root.other == STO_MIPS16)
6656 {
6657 /* We don't need the call_stub; this is a 16 bit function, so
6658 calls from other 16 bit functions are OK. Clobber the size
6659 to 0 to prevent it from being included in the link. */
6660 h->call_stub->_raw_size = 0;
6661 h->call_stub->_cooked_size = 0;
6662 h->call_stub->flags &= ~ SEC_RELOC;
6663 h->call_stub->reloc_count = 0;
6664 h->call_stub->flags |= SEC_EXCLUDE;
6665 }
6666
6667 if (h->call_fp_stub != NULL
6668 && h->root.other == STO_MIPS16)
6669 {
6670 /* We don't need the call_stub; this is a 16 bit function, so
6671 calls from other 16 bit functions are OK. Clobber the size
6672 to 0 to prevent it from being included in the link. */
6673 h->call_fp_stub->_raw_size = 0;
6674 h->call_fp_stub->_cooked_size = 0;
6675 h->call_fp_stub->flags &= ~ SEC_RELOC;
6676 h->call_fp_stub->reloc_count = 0;
6677 h->call_fp_stub->flags |= SEC_EXCLUDE;
6678 }
6679
6680 return true;
6681}
6682
6683/* Set the sizes of the dynamic sections. */
6684
6685static boolean
6686mips_elf_size_dynamic_sections (output_bfd, info)
6687 bfd *output_bfd;
6688 struct bfd_link_info *info;
6689{
6690 bfd *dynobj;
6691 asection *s;
6692 boolean reltext;
6693 asection *sgot;
6694 struct mips_got_info *g;
6695
6696 dynobj = elf_hash_table (info)->dynobj;
6697 BFD_ASSERT (dynobj != NULL);
6698
6699 if (elf_hash_table (info)->dynamic_sections_created)
6700 {
6701 /* Set the contents of the .interp section to the interpreter. */
6702 if (! info->shared)
6703 {
6704 s = bfd_get_section_by_name (dynobj, ".interp");
6705 BFD_ASSERT (s != NULL);
6706 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
6707 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
6708 }
6709 }
6710
6711 /* Recompute the size of .got for local entires (reserved and
6712 hipages) if needed. To estimate it, get the upper bound of total
6713 size of loadable sections. */
6714 sgot = bfd_get_section_by_name (dynobj, ".got");
6715
6716 if (sgot != NULL)
6717 {
6718 bfd_size_type loadable_size = 0;
6719 bfd_size_type local_gotno;
6720 struct _bfd *sub;
6721
6722 BFD_ASSERT (elf_section_data (sgot) != NULL);
6723 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6724 BFD_ASSERT (g != NULL);
6725
6726 for (sub = info->input_bfds; sub; sub = sub->link_next)
6727 for (s = sub->sections; s != NULL; s = s->next)
6728 {
6729 if ((s->flags & SEC_ALLOC) == 0)
6730 continue;
6731 loadable_size += (s->_raw_size + 0xf) & ~0xf;
6732 }
6733
6734 loadable_size += MIPS_FUNCTION_STUB_SIZE;
6735
6736 /* Assume there are two loadable segments consisting of
6737 contiguous sections. Is 5 enough? */
6738 local_gotno = (loadable_size >> 16) + 5 + MIPS_RESERVED_GOTNO;
6739 g->local_gotno = local_gotno;
6740 sgot->_raw_size += local_gotno * 4;
6741 }
6742
6743 /* The check_relocs and adjust_dynamic_symbol entry points have
6744 determined the sizes of the various dynamic sections. Allocate
6745 memory for them. */
6746 reltext = false;
6747 for (s = dynobj->sections; s != NULL; s = s->next)
6748 {
6749 const char *name;
6750 boolean strip;
6751
6752 /* It's OK to base decisions on the section name, because none
6753 of the dynobj section names depend upon the input files. */
6754 name = bfd_get_section_name (dynobj, s);
6755
6756 if ((s->flags & SEC_LINKER_CREATED) == 0)
6757 continue;
6758
6759 strip = false;
6760
6761 if (strncmp (name, ".rel", 4) == 0)
6762 {
6763 if (s->_raw_size == 0)
6764 {
6765 /* We only strip the section if the output section name
6766 has the same name. Otherwise, there might be several
6767 input sections for this output section. FIXME: This
6768 code is probably not needed these days anyhow, since
6769 the linker now does not create empty output sections. */
6770 if (s->output_section != NULL
6771 && strcmp (name,
6772 bfd_get_section_name (s->output_section->owner,
6773 s->output_section)) == 0)
6774 strip = true;
6775 }
6776 else
6777 {
6778 const char *outname;
6779 asection *target;
6780
6781 /* If this relocation section applies to a read only
6782 section, then we probably need a DT_TEXTREL entry.
6783 If the relocation section is .rel.dyn, we always
6784 assert a DT_TEXTREL entry rather than testing whether
6785 there exists a relocation to a read only section or
6786 not. */
6787 outname = bfd_get_section_name (output_bfd,
6788 s->output_section);
6789 target = bfd_get_section_by_name (output_bfd, outname + 4);
6790 if ((target != NULL
6791 && (target->flags & SEC_READONLY) != 0
6792 && (target->flags & SEC_ALLOC) != 0)
6793 || strcmp (outname, ".rel.dyn") == 0)
6794 reltext = true;
6795
6796 /* We use the reloc_count field as a counter if we need
6797 to copy relocs into the output file. */
6798 if (strcmp (name, ".rel.dyn") != 0)
6799 s->reloc_count = 0;
6800 }
6801 }
6802 else if (strncmp (name, ".got", 4) == 0)
6803 {
6804 int i;
6805
6806 BFD_ASSERT (elf_section_data (s) != NULL);
6807 g = (struct mips_got_info *) elf_section_data (s)->tdata;
6808 BFD_ASSERT (g != NULL);
6809
6810 /* Fix the size of .got section for the correspondence of
6811 global symbols and got entries. This adds some useless
6812 got entries. Is this required by ABI really? */
6813 i = elf_hash_table (info)->dynsymcount - g->global_gotsym;
6814 s->_raw_size += i * 4;
6815 }
6816 else if (strncmp (name, ".stub", 5) == 0)
6817 {
6818 /* Irix rld assumes that the function stub isn't at the end
6819 of .text section. So put a dummy. XXX */
6820 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6821 }
6822 else if (! info->shared
6823 && ! mips_elf_hash_table (info)->use_rld_obj_head
6824 && strncmp (name, ".rld_map", 8) == 0)
6825 {
6826 /* We add a room for __rld_map. It will be filled in by the
6827 rtld to contain a pointer to the _r_debug structure. */
6828 s->_raw_size += 4;
6829 }
6830 else if (SGI_COMPAT (output_bfd)
6831 && strncmp (name, ".compact_rel", 12) == 0)
6832 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
6833 else if (strncmp (name, ".init", 5) != 0)
6834 {
6835 /* It's not one of our sections, so don't allocate space. */
6836 continue;
6837 }
6838
6839 if (strip)
6840 {
6841 _bfd_strip_section_from_output (s);
6842 continue;
6843 }
6844
6845 /* Allocate memory for the section contents. */
6846 s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
6847 if (s->contents == NULL && s->_raw_size != 0)
6848 {
6849 bfd_set_error (bfd_error_no_memory);
6850 return false;
6851 }
6852 memset (s->contents, 0, s->_raw_size);
6853 }
6854
6855 if (elf_hash_table (info)->dynamic_sections_created)
6856 {
6857 /* Add some entries to the .dynamic section. We fill in the
6858 values later, in elf_mips_finish_dynamic_sections, but we
6859 must add the entries now so that we get the correct size for
6860 the .dynamic section. The DT_DEBUG entry is filled in by the
6861 dynamic linker and used by the debugger. */
6862 if (! info->shared)
6863 {
6864 if (SGI_COMPAT (output_bfd))
6865 {
6866 /* SGI object has the equivalence of DT_DEBUG in the
6867 DT_MIPS_RLD_MAP entry. */
6868 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_MAP, 0))
6869 return false;
6870 }
6871 else
6872 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
6873 return false;
6874 }
6875
6876 if (reltext)
6877 {
6878 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
6879 return false;
6880 }
6881
6882 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
6883 return false;
6884
6885 if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
6886 {
6887 if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0))
6888 return false;
6889
6890 if (! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0))
6891 return false;
6892
6893 if (! bfd_elf32_add_dynamic_entry (info, DT_RELENT, 0))
6894 return false;
6895 }
6896
6897 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICTNO, 0))
6898 return false;
6899
6900 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLISTNO, 0))
6901 return false;
6902
6903 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
6904 {
6905 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICT, 0))
6906 return false;
6907
6908 s = bfd_get_section_by_name (dynobj, ".liblist");
6909 BFD_ASSERT (s != NULL);
6910
6911 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLIST, 0))
6912 return false;
6913 }
6914
6915 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_VERSION, 0))
6916 return false;
6917
6918 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_FLAGS, 0))
6919 return false;
6920
6921#if 0
6922 /* Time stamps in executable files are a bad idea. */
6923 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_TIME_STAMP, 0))
6924 return false;
6925#endif
6926
6927#if 0 /* FIXME */
6928 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_ICHECKSUM, 0))
6929 return false;
6930#endif
6931
6932#if 0 /* FIXME */
6933 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_IVERSION, 0))
6934 return false;
6935#endif
6936
6937 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_BASE_ADDRESS, 0))
6938 return false;
6939
6940 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LOCAL_GOTNO, 0))
6941 return false;
6942
6943 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_SYMTABNO, 0))
6944 return false;
6945
6946 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_UNREFEXTNO, 0))
6947 return false;
6948
6949 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_GOTSYM, 0))
6950 return false;
6951
6952 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_HIPAGENO, 0))
6953 return false;
6954
6955#if 0 /* (SGI_COMPAT) */
6956 if (! bfd_get_section_by_name (dynobj, ".init"))
6957 if (! bfd_elf32_add_dynamic_entry (info, DT_INIT, 0))
6958 return false;
6959
6960 if (! bfd_get_section_by_name (dynobj, ".fini"))
6961 if (! bfd_elf32_add_dynamic_entry (info, DT_FINI, 0))
6962 return false;
6963#endif
6964 }
6965
6966 /* If we use dynamic linking, we generate a section symbol for each
6967 output section. These are local symbols, which means that they
6968 must come first in the dynamic symbol table.
6969 That means we must increment the dynamic symbol index of every
6970 other dynamic symbol. */
6971 {
6972 unsigned int c, i;
6973 struct mips_got_info *g;
6974
6975 c = 0;
6976 if (elf_hash_table (info)->dynamic_sections_created)
6977 {
6978#if 0
6979 /* We no longer try to restrict the set of sections which get
6980 dynamic symbol table entries, since it fails if we have
6981 other random sections which need dynamic relocations. */
6982 const char * const *namep;
6983 bfd_size_type strindex;
6984 struct bfd_strtab_hash *dynstr;
6985
6986 if (SGI_COMPAT (output_bfd))
6987 {
6988 c = SIZEOF_MIPS_DYNSYM_SECNAMES - 1;
6989 elf_link_hash_traverse (elf_hash_table (info),
6990 mips_elf_adjust_dynindx,
6991 (PTR) &c);
6992 elf_hash_table (info)->dynsymcount += c;
6993
6994 dynstr = elf_hash_table (info)->dynstr;
6995 BFD_ASSERT (dynstr != NULL);
6996
6997 for (i = 1, namep = mips_elf_dynsym_sec_names;
6998 *namep != NULL;
6999 i++, namep++)
7000 {
7001 s = bfd_get_section_by_name (output_bfd, *namep);
7002 if (s != NULL)
7003 elf_section_data (s)->dynindx = i;
7004
7005 strindex = _bfd_stringtab_add (dynstr, *namep, true, false);
7006 if (strindex == (bfd_size_type) -1)
7007 return false;
7008
7009 mips_elf_hash_table (info)->dynsym_sec_strindex[i] = strindex;
7010 }
7011 }
7012 else
7013#endif /* 0 */
7014 {
7015 c = bfd_count_sections (output_bfd);
7016 elf_link_hash_traverse (elf_hash_table (info),
7017 mips_elf_adjust_dynindx,
7018 (PTR) &c);
7019 elf_hash_table (info)->dynsymcount += c;
7020
7021 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
7022 {
7023 elf_section_data (s)->dynindx = i;
7024 /* These symbols will have no names, so we don't need to
7025 fiddle with dynstr_index. */
7026 }
7027 }
7028 }
7029
7030 if (sgot != NULL)
7031 {
7032 BFD_ASSERT (elf_section_data (sgot) != NULL);
7033 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7034 BFD_ASSERT (g != NULL);
7035
7036 /* If there are no global got symbols, fake the last symbol so
7037 for safety. */
7038 if (g->global_gotsym)
7039 g->global_gotsym += c;
7040 else
7041 g->global_gotsym = elf_hash_table (info)->dynsymcount - 1;
7042 }
7043 }
7044
7045 return true;
7046}
7047
7048/* Increment the index of a dynamic symbol by a given amount. Called
7049 via elf_link_hash_traverse. */
7050
7051static boolean
7052mips_elf_adjust_dynindx (h, cparg)
7053 struct elf_link_hash_entry *h;
7054 PTR cparg;
7055{
7056 unsigned int *cp = (unsigned int *) cparg;
7057
7058 if (h->dynindx != -1)
7059 h->dynindx += *cp;
7060 return true;
7061}
7062
7063/* Finish up dynamic symbol handling. We set the contents of various
7064 dynamic sections here. */
7065
7066static boolean
7067mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
7068 bfd *output_bfd;
7069 struct bfd_link_info *info;
7070 struct elf_link_hash_entry *h;
7071 Elf_Internal_Sym *sym;
7072{
7073 bfd *dynobj;
7074 bfd_vma gval;
7075 asection *sgot;
7076 struct mips_got_info *g;
7077 const char *name;
7078
7079 dynobj = elf_hash_table (info)->dynobj;
7080 gval = sym->st_value;
7081
7082 if (h->plt.offset != (bfd_vma) -1)
7083 {
7084 asection *s;
7085 bfd_byte *p;
7086 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
7087
7088 /* This symbol has a stub. Set it up. */
7089
7090 BFD_ASSERT (h->dynindx != -1);
7091
7092 s = bfd_get_section_by_name (dynobj, ".stub");
7093 BFD_ASSERT (s != NULL);
7094
7095 /* Fill the stub. */
7096 p = stub;
7097 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
7098 p += 4;
7099 bfd_put_32 (output_bfd, STUB_MOVE, p);
7100 p += 4;
7101
7102 /* FIXME: Can h->dynindex be more than 64K? */
7103 if (h->dynindx & 0xffff0000)
7104 return false;
7105
7106 bfd_put_32 (output_bfd, STUB_JALR, p);
7107 p += 4;
7108 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
7109
7110 BFD_ASSERT (h->plt.offset <= s->_raw_size);
7111 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
7112
7113 /* Mark the symbol as undefined. plt.offset != -1 occurs
7114 only for the referenced symbol. */
7115 sym->st_shndx = SHN_UNDEF;
7116
7117 /* The run-time linker uses the st_value field of the symbol
7118 to reset the global offset table entry for this external
7119 to its stub address when unlinking a shared object. */
7120 gval = s->output_section->vma + s->output_offset + h->plt.offset;
7121 sym->st_value = gval;
7122 }
7123
7124 BFD_ASSERT (h->dynindx != -1);
7125
7126 sgot = bfd_get_section_by_name (dynobj, ".got");
7127 BFD_ASSERT (sgot != NULL);
7128 BFD_ASSERT (elf_section_data (sgot) != NULL);
7129 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7130 BFD_ASSERT (g != NULL);
7131
7132 if ((unsigned long) h->dynindx >= g->global_gotsym)
7133 {
7134 bfd_size_type offset;
7135
7136 /* This symbol has an entry in the global offset table. Set its
7137 value to the corresponding got entry, if needed. */
7138 if (h->got.offset == (bfd_vma) -1)
7139 {
7140 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
7141 BFD_ASSERT (g->local_gotno * 4 <= offset
7142 && offset < sgot->_raw_size);
7143 bfd_put_32 (output_bfd, gval, sgot->contents + offset);
7144 }
7145 }
7146
7147 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
7148 name = h->root.root.string;
7149 if (strcmp (name, "_DYNAMIC") == 0
7150 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
7151 sym->st_shndx = SHN_ABS;
7152 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
7153 {
7154 sym->st_shndx = SHN_ABS;
7155 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7156 sym->st_value = 1;
7157 }
7158 else if (SGI_COMPAT (output_bfd))
7159 {
7160 if (strcmp (name, "_gp_disp") == 0)
7161 {
7162 sym->st_shndx = SHN_ABS;
7163 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7164 sym->st_value = elf_gp (output_bfd);
7165 }
7166 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
7167 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
7168 {
7169 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7170 sym->st_other = STO_PROTECTED;
7171 sym->st_value = 0;
7172 sym->st_shndx = SHN_MIPS_DATA;
7173 }
7174 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
7175 {
7176 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7177 sym->st_other = STO_PROTECTED;
7178 sym->st_value = mips_elf_hash_table (info)->procedure_count;
7179 sym->st_shndx = SHN_ABS;
7180 }
7181 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
7182 {
7183 if (h->type == STT_FUNC)
7184 sym->st_shndx = SHN_MIPS_TEXT;
7185 else if (h->type == STT_OBJECT)
7186 sym->st_shndx = SHN_MIPS_DATA;
7187 }
7188 }
7189
7190 if (SGI_COMPAT (output_bfd)
7191 && ! info->shared)
7192 {
7193 if (! mips_elf_hash_table (info)->use_rld_obj_head
7194 && strcmp (name, "__rld_map") == 0)
7195 {
7196 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7197 BFD_ASSERT (s != NULL);
7198 sym->st_value = s->output_section->vma + s->output_offset;
7199 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
7200 if (mips_elf_hash_table (info)->rld_value == 0)
7201 mips_elf_hash_table (info)->rld_value = sym->st_value;
7202 }
7203 else if (mips_elf_hash_table (info)->use_rld_obj_head
7204 && strcmp (name, "__rld_obj_head") == 0)
7205 {
7206 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7207 BFD_ASSERT (s != NULL);
7208 mips_elf_hash_table (info)->rld_value = sym->st_value;
7209 }
7210 }
7211
7212 /* If this is a mips16 symbol, force the value to be even. */
7213 if (sym->st_other == STO_MIPS16
7214 && (sym->st_value & 1) != 0)
7215 --sym->st_value;
7216
7217 return true;
7218}
7219
7220/* Finish up the dynamic sections. */
7221
7222static boolean
7223mips_elf_finish_dynamic_sections (output_bfd, info)
7224 bfd *output_bfd;
7225 struct bfd_link_info *info;
7226{
7227 bfd *dynobj;
7228 asection *sdyn;
7229 asection *sgot;
7230 struct mips_got_info *g;
7231
7232 dynobj = elf_hash_table (info)->dynobj;
7233
7234 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
7235
7236 sgot = bfd_get_section_by_name (dynobj, ".got");
7237 if (sgot == NULL)
7238 g = NULL;
7239 else
7240 {
7241 BFD_ASSERT (elf_section_data (sgot) != NULL);
7242 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7243 BFD_ASSERT (g != NULL);
7244 }
7245
7246 if (elf_hash_table (info)->dynamic_sections_created)
7247 {
7248 Elf32_External_Dyn *dyncon, *dynconend;
7249
7250 BFD_ASSERT (sdyn != NULL);
7251 BFD_ASSERT (g != NULL);
7252
7253 dyncon = (Elf32_External_Dyn *) sdyn->contents;
7254 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
7255 for (; dyncon < dynconend; dyncon++)
7256 {
7257 Elf_Internal_Dyn dyn;
7258 const char *name;
7259 size_t elemsize;
7260 asection *s;
7261
7262 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
7263
7264 switch (dyn.d_tag)
7265 {
7266 default:
7267 break;
7268
7269 case DT_RELENT:
7270 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7271 BFD_ASSERT (s != NULL);
7272 dyn.d_un.d_val = sizeof (Elf32_External_Rel);
7273 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7274 break;
7275
7276 case DT_STRSZ:
7277 /* Rewrite DT_STRSZ. */
7278 dyn.d_un.d_val =
7279 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
7280 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7281 break;
7282
7283 case DT_PLTGOT:
7284 name = ".got";
7285 goto get_vma;
7286 case DT_MIPS_CONFLICT:
7287 name = ".conflict";
7288 goto get_vma;
7289 case DT_MIPS_LIBLIST:
7290 name = ".liblist";
7291 get_vma:
7292 s = bfd_get_section_by_name (output_bfd, name);
7293 BFD_ASSERT (s != NULL);
7294 dyn.d_un.d_ptr = s->vma;
7295 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7296 break;
7297
7298 case DT_MIPS_RLD_VERSION:
7299 dyn.d_un.d_val = 1; /* XXX */
7300 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7301 break;
7302
7303 case DT_MIPS_FLAGS:
7304 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
7305 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7306 break;
7307
7308 case DT_MIPS_CONFLICTNO:
7309 name = ".conflict";
7310 elemsize = sizeof (Elf32_Conflict);
7311 goto set_elemno;
7312
7313 case DT_MIPS_LIBLISTNO:
7314 name = ".liblist";
7315 elemsize = sizeof (Elf32_Lib);
7316 set_elemno:
7317 s = bfd_get_section_by_name (output_bfd, name);
7318 if (s != NULL)
7319 {
7320 if (s->_cooked_size != 0)
7321 dyn.d_un.d_val = s->_cooked_size / elemsize;
7322 else
7323 dyn.d_un.d_val = s->_raw_size / elemsize;
7324 }
7325 else
7326 dyn.d_un.d_val = 0;
7327
7328 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7329 break;
7330
7331 case DT_MIPS_TIME_STAMP:
7332 time ((time_t *) &dyn.d_un.d_val);
7333 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7334 break;
7335
7336 case DT_MIPS_ICHECKSUM:
7337 /* XXX FIXME: */
7338 break;
7339
7340 case DT_MIPS_IVERSION:
7341 /* XXX FIXME: */
7342 break;
7343
7344 case DT_MIPS_BASE_ADDRESS:
7345 s = output_bfd->sections;
7346 BFD_ASSERT (s != NULL);
7347 dyn.d_un.d_ptr = s->vma & ~(0xffff);
7348 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7349 break;
7350
7351 case DT_MIPS_LOCAL_GOTNO:
7352 dyn.d_un.d_val = g->local_gotno;
7353 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7354 break;
7355
7356 case DT_MIPS_SYMTABNO:
7357 name = ".dynsym";
7358 elemsize = sizeof (Elf32_External_Sym);
7359 s = bfd_get_section_by_name (output_bfd, name);
7360 BFD_ASSERT (s != NULL);
7361
7362 if (s->_cooked_size != 0)
7363 dyn.d_un.d_val = s->_cooked_size / elemsize;
7364 else
7365 dyn.d_un.d_val = s->_raw_size / elemsize;
7366 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7367 break;
7368
7369 case DT_MIPS_UNREFEXTNO:
7370#if 0
7371 dyn.d_un.d_val = SIZEOF_MIPS_DYNSYM_SECNAMES;
7372#else
7373 dyn.d_un.d_val = bfd_count_sections (output_bfd);
7374#endif
7375 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7376 break;
7377
7378 case DT_MIPS_GOTSYM:
7379 dyn.d_un.d_val = g->global_gotsym;
7380 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7381 break;
7382
7383 case DT_MIPS_HIPAGENO:
7384 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
7385 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7386 break;
7387
7388 case DT_MIPS_RLD_MAP:
7389 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
7390 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7391 break;
7392
7393 }
7394 }
7395 }
7396
7397 /* The first entry of the global offset table will be filled at
7398 runtime. The second entry will be used by some runtime loaders.
7399 This isn't the case of Irix rld. */
7400 if (sgot != NULL && sgot->_raw_size > 0)
7401 {
7402 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
7403 bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, sgot->contents + 4);
7404 }
7405
7406 if (sgot != NULL)
7407 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
7408
7409 {
7410 asection *sdynsym;
7411 asection *s;
7412 Elf_Internal_Sym sym;
7413 Elf32_compact_rel cpt;
7414
7415 /* Set up the section symbols for the output sections. SGI sets
7416 the STT_NOTYPE attribute for these symbols. Should we do so? */
7417
7418 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
7419 if (sdynsym != NULL)
7420 {
7421#if 0
7422 const char *name;
7423 const char * const * namep = mips_elf_dynsym_sec_names;
7424 unsigned int i;
7425 bfd_vma last;
7426 long dindx;
7427
7428 /* We no longer try to restrict the set of sections which get
7429 dynamic symbol table entries, since it fails if we have
7430 other random sections which need dynamic relocations. */
7431 if (SGI_COMPAT (output_bfd))
7432 {
7433 sym.st_size = 0;
7434 sym.st_name = 0;
7435 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7436 sym.st_other = 0;
7437
7438 i = 0;
7439 last = 0;
7440 dindx = 0;
7441 while ((name = *namep++) != NULL)
7442 {
7443 s = bfd_get_section_by_name (output_bfd, name);
7444 if (s != NULL)
7445 {
7446 sym.st_value = s->vma;
7447 dindx = elf_section_data (s)->dynindx;
7448 last = s->vma + s->_raw_size;
7449 }
7450 else
7451 {
7452 sym.st_value = last;
7453 dindx++;
7454 }
7455
7456 sym.st_shndx = (i < MIPS_TEXT_DYNSYM_SECNO
7457 ? SHN_MIPS_TEXT
7458 : SHN_MIPS_DATA);
7459 ++i;
7460 sym.st_name =
7461 mips_elf_hash_table (info)->dynsym_sec_strindex[dindx];
7462
7463 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7464 (((Elf32_External_Sym *)
7465 sdynsym->contents)
7466 + dindx));
7467 }
7468
7469 /* Set the sh_info field of the output .dynsym section to
7470 the index of the first global symbol. */
7471 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7472 SIZEOF_MIPS_DYNSYM_SECNAMES;
7473 }
7474 else
7475#endif /* 0 */
7476 {
7477 sym.st_size = 0;
7478 sym.st_name = 0;
7479 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7480 sym.st_other = 0;
7481
7482 for (s = output_bfd->sections; s != NULL; s = s->next)
7483 {
7484 int indx;
7485
7486 sym.st_value = s->vma;
7487
7488 indx = elf_section_data (s)->this_idx;
7489 BFD_ASSERT (indx > 0);
7490 sym.st_shndx = indx;
7491
7492 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7493 (((Elf32_External_Sym *)
7494 sdynsym->contents)
7495 + elf_section_data (s)->dynindx));
7496 }
7497
7498 /* Set the sh_info field of the output .dynsym section to
7499 the index of the first global symbol. */
7500 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7501 bfd_count_sections (output_bfd) + 1;
7502 }
7503 }
7504
7505 if (SGI_COMPAT (output_bfd))
7506 {
7507 /* Write .compact_rel section out. */
7508 s = bfd_get_section_by_name (dynobj, ".compact_rel");
7509 if (s != NULL)
7510 {
7511 cpt.id1 = 1;
7512 cpt.num = s->reloc_count;
7513 cpt.id2 = 2;
7514 cpt.offset = (s->output_section->filepos
7515 + sizeof (Elf32_External_compact_rel));
7516 cpt.reserved0 = 0;
7517 cpt.reserved1 = 0;
7518 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
7519 ((Elf32_External_compact_rel *)
7520 s->contents));
7521
7522 /* Clean up a dummy stub function entry in .text. */
7523 s = bfd_get_section_by_name (dynobj, ".stub");
7524 if (s != NULL)
7525 {
7526 file_ptr dummy_offset;
7527
7528 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
7529 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
7530 memset (s->contents + dummy_offset, 0,
7531 MIPS_FUNCTION_STUB_SIZE);
7532 }
7533 }
7534 }
7535
7536 /* Clean up a first relocation in .rel.dyn. */
7537 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7538 if (s != NULL && s->_raw_size > 0)
7539 memset (s->contents, 0, sizeof (Elf32_External_Rel));
7540 }
7541
7542 return true;
7543}
7544\f
7545/* This is almost identical to bfd_generic_get_... except that some
7546 MIPS relocations need to be handled specially. Sigh. */
7547
7548static bfd_byte *
7549elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
7550 relocateable, symbols)
7551 bfd *abfd;
7552 struct bfd_link_info *link_info;
7553 struct bfd_link_order *link_order;
7554 bfd_byte *data;
7555 boolean relocateable;
7556 asymbol **symbols;
7557{
7558 /* Get enough memory to hold the stuff */
7559 bfd *input_bfd = link_order->u.indirect.section->owner;
7560 asection *input_section = link_order->u.indirect.section;
7561
7562 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
7563 arelent **reloc_vector = NULL;
7564 long reloc_count;
7565
7566 if (reloc_size < 0)
7567 goto error_return;
7568
7569 reloc_vector = (arelent **) bfd_malloc (reloc_size);
7570 if (reloc_vector == NULL && reloc_size != 0)
7571 goto error_return;
7572
7573 /* read in the section */
7574 if (!bfd_get_section_contents (input_bfd,
7575 input_section,
7576 (PTR) data,
7577 0,
7578 input_section->_raw_size))
7579 goto error_return;
7580
7581 /* We're not relaxing the section, so just copy the size info */
7582 input_section->_cooked_size = input_section->_raw_size;
7583 input_section->reloc_done = true;
7584
7585 reloc_count = bfd_canonicalize_reloc (input_bfd,
7586 input_section,
7587 reloc_vector,
7588 symbols);
7589 if (reloc_count < 0)
7590 goto error_return;
7591
7592 if (reloc_count > 0)
7593 {
7594 arelent **parent;
7595 /* for mips */
7596 int gp_found;
7597 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
7598
7599 {
7600 struct bfd_hash_entry *h;
7601 struct bfd_link_hash_entry *lh;
7602 /* Skip all this stuff if we aren't mixing formats. */
7603 if (abfd && input_bfd
7604 && abfd->xvec == input_bfd->xvec)
7605 lh = 0;
7606 else
7607 {
7608 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
7609 lh = (struct bfd_link_hash_entry *) h;
7610 }
7611 lookup:
7612 if (lh)
7613 {
7614 switch (lh->type)
7615 {
7616 case bfd_link_hash_undefined:
7617 case bfd_link_hash_undefweak:
7618 case bfd_link_hash_common:
7619 gp_found = 0;
7620 break;
7621 case bfd_link_hash_defined:
7622 case bfd_link_hash_defweak:
7623 gp_found = 1;
7624 gp = lh->u.def.value;
7625 break;
7626 case bfd_link_hash_indirect:
7627 case bfd_link_hash_warning:
7628 lh = lh->u.i.link;
7629 /* @@FIXME ignoring warning for now */
7630 goto lookup;
7631 case bfd_link_hash_new:
7632 default:
7633 abort ();
7634 }
7635 }
7636 else
7637 gp_found = 0;
7638 }
7639 /* end mips */
7640 for (parent = reloc_vector; *parent != (arelent *) NULL;
7641 parent++)
7642 {
7643 char *error_message = (char *) NULL;
7644 bfd_reloc_status_type r;
7645
7646 /* Specific to MIPS: Deal with relocation types that require
7647 knowing the gp of the output bfd. */
7648 asymbol *sym = *(*parent)->sym_ptr_ptr;
7649 if (bfd_is_abs_section (sym->section) && abfd)
7650 {
7651 /* The special_function wouldn't get called anyways. */
7652 }
7653 else if (!gp_found)
7654 {
7655 /* The gp isn't there; let the special function code
7656 fall over on its own. */
7657 }
7658 else if ((*parent)->howto->special_function
7659 == _bfd_mips_elf_gprel16_reloc)
7660 {
7661 /* bypass special_function call */
7662 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
7663 relocateable, (PTR) data, gp);
7664 goto skip_bfd_perform_relocation;
7665 }
7666 /* end mips specific stuff */
7667
7668 r = bfd_perform_relocation (input_bfd,
7669 *parent,
7670 (PTR) data,
7671 input_section,
7672 relocateable ? abfd : (bfd *) NULL,
7673 &error_message);
7674 skip_bfd_perform_relocation:
7675
7676 if (relocateable)
7677 {
7678 asection *os = input_section->output_section;
7679
7680 /* A partial link, so keep the relocs */
7681 os->orelocation[os->reloc_count] = *parent;
7682 os->reloc_count++;
7683 }
7684
7685 if (r != bfd_reloc_ok)
7686 {
7687 switch (r)
7688 {
7689 case bfd_reloc_undefined:
7690 if (!((*link_info->callbacks->undefined_symbol)
7691 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7692 input_bfd, input_section, (*parent)->address)))
7693 goto error_return;
7694 break;
7695 case bfd_reloc_dangerous:
7696 BFD_ASSERT (error_message != (char *) NULL);
7697 if (!((*link_info->callbacks->reloc_dangerous)
7698 (link_info, error_message, input_bfd, input_section,
7699 (*parent)->address)))
7700 goto error_return;
7701 break;
7702 case bfd_reloc_overflow:
7703 if (!((*link_info->callbacks->reloc_overflow)
7704 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7705 (*parent)->howto->name, (*parent)->addend,
7706 input_bfd, input_section, (*parent)->address)))
7707 goto error_return;
7708 break;
7709 case bfd_reloc_outofrange:
7710 default:
7711 abort ();
7712 break;
7713 }
7714
7715 }
7716 }
7717 }
7718 if (reloc_vector != NULL)
7719 free (reloc_vector);
7720 return data;
7721
7722error_return:
7723 if (reloc_vector != NULL)
7724 free (reloc_vector);
7725 return NULL;
7726}
7727#define bfd_elf32_bfd_get_relocated_section_contents \
7728 elf32_mips_get_relocated_section_contents
7729\f
7730/* ECOFF swapping routines. These are used when dealing with the
7731 .mdebug section, which is in the ECOFF debugging format. */
7732static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
7733{
7734 /* Symbol table magic number. */
7735 magicSym,
7736 /* Alignment of debugging information. E.g., 4. */
7737 4,
7738 /* Sizes of external symbolic information. */
7739 sizeof (struct hdr_ext),
7740 sizeof (struct dnr_ext),
7741 sizeof (struct pdr_ext),
7742 sizeof (struct sym_ext),
7743 sizeof (struct opt_ext),
7744 sizeof (struct fdr_ext),
7745 sizeof (struct rfd_ext),
7746 sizeof (struct ext_ext),
7747 /* Functions to swap in external symbolic data. */
7748 ecoff_swap_hdr_in,
7749 ecoff_swap_dnr_in,
7750 ecoff_swap_pdr_in,
7751 ecoff_swap_sym_in,
7752 ecoff_swap_opt_in,
7753 ecoff_swap_fdr_in,
7754 ecoff_swap_rfd_in,
7755 ecoff_swap_ext_in,
7756 _bfd_ecoff_swap_tir_in,
7757 _bfd_ecoff_swap_rndx_in,
7758 /* Functions to swap out external symbolic data. */
7759 ecoff_swap_hdr_out,
7760 ecoff_swap_dnr_out,
7761 ecoff_swap_pdr_out,
7762 ecoff_swap_sym_out,
7763 ecoff_swap_opt_out,
7764 ecoff_swap_fdr_out,
7765 ecoff_swap_rfd_out,
7766 ecoff_swap_ext_out,
7767 _bfd_ecoff_swap_tir_out,
7768 _bfd_ecoff_swap_rndx_out,
7769 /* Function to read in symbolic data. */
7770 _bfd_mips_elf_read_ecoff_info
7771};
7772\f
7773#define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
7774#define TARGET_LITTLE_NAME "elf32-littlemips"
7775#define TARGET_BIG_SYM bfd_elf32_bigmips_vec
7776#define TARGET_BIG_NAME "elf32-bigmips"
7777#define ELF_ARCH bfd_arch_mips
7778#define ELF_MACHINE_CODE EM_MIPS
7779
7780/* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
7781 a value of 0x1000, and we are compatible. */
7782#define ELF_MAXPAGESIZE 0x1000
7783
7784#define elf_backend_collect true
7785#define elf_backend_type_change_ok true
7786#define elf_backend_can_gc_sections true
3f830999 7787#define elf_info_to_howto mips_info_to_howto_rela
252b5132
RH
7788#define elf_info_to_howto_rel mips_info_to_howto_rel
7789#define elf_backend_sym_is_global mips_elf_sym_is_global
7790#define elf_backend_object_p mips_elf32_object_p
7791#define elf_backend_section_from_shdr mips_elf32_section_from_shdr
7792#define elf_backend_fake_sections _bfd_mips_elf_fake_sections
7793#define elf_backend_section_from_bfd_section \
7794 _bfd_mips_elf_section_from_bfd_section
7795#define elf_backend_section_processing mips_elf32_section_processing
7796#define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
7797#define elf_backend_additional_program_headers \
7798 mips_elf_additional_program_headers
7799#define elf_backend_modify_segment_map mips_elf_modify_segment_map
7800#define elf_backend_final_write_processing \
7801 _bfd_mips_elf_final_write_processing
7802#define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
7803
7804#define bfd_elf32_bfd_is_local_label_name \
7805 mips_elf_is_local_label_name
7806#define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
7807#define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
7808#define bfd_elf32_bfd_link_hash_table_create \
7809 mips_elf_link_hash_table_create
7810#define bfd_elf32_bfd_final_link mips_elf_final_link
7811#define bfd_elf32_bfd_copy_private_bfd_data \
7812 _bfd_mips_elf_copy_private_bfd_data
7813#define bfd_elf32_bfd_merge_private_bfd_data \
7814 _bfd_mips_elf_merge_private_bfd_data
7815#define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
7816#define bfd_elf32_bfd_print_private_bfd_data \
7817 _bfd_mips_elf_print_private_bfd_data
7818#define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
7819#define elf_backend_create_dynamic_sections \
7820 mips_elf_create_dynamic_sections
7821#define elf_backend_check_relocs mips_elf_check_relocs
7822#define elf_backend_adjust_dynamic_symbol \
7823 mips_elf_adjust_dynamic_symbol
7824#define elf_backend_always_size_sections \
7825 mips_elf_always_size_sections
7826#define elf_backend_size_dynamic_sections \
7827 mips_elf_size_dynamic_sections
7828#define elf_backend_relocate_section mips_elf_relocate_section
7829#define elf_backend_link_output_symbol_hook \
7830 mips_elf_link_output_symbol_hook
7831#define elf_backend_finish_dynamic_symbol \
7832 mips_elf_finish_dynamic_symbol
7833#define elf_backend_finish_dynamic_sections \
7834 mips_elf_finish_dynamic_sections
7835#define elf_backend_gc_mark_hook mips_elf_gc_mark_hook
7836#define elf_backend_gc_sweep_hook mips_elf_gc_sweep_hook
7837
7838#define elf_backend_got_header_size (4*MIPS_RESERVED_GOTNO)
7839#define elf_backend_plt_header_size 0
7840
7841#include "elf32-target.h"
This page took 0.311335 seconds and 4 git commands to generate.