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