* elf32-mips.c (mips_elf_final_link): Don't set CPIC when doing a
[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.) */
0dda5f7a
ILT
4178 if (!info->shared
4179 && !info->relocateable
4180 && elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
252b5132 4181 {
303f629d
MM
4182 elf_elfheader (abfd)->e_flags &= ~EF_MIPS_PIC;
4183 elf_elfheader (abfd)->e_flags |= EF_MIPS_CPIC;
252b5132
RH
4184 }
4185
303f629d
MM
4186 /* On IRIX5, we omit the .options section. On IRIX6, however, we
4187 include it, even though we don't process it quite right. (Some
4188 entries are supposed to be merged.) Empirically, we seem to be
4189 better off including it then not. */
4190 if (IRIX_COMPAT (abfd) == ict_irix5)
4191 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
4192 {
4193 if (strcmp ((*secpp)->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
4194 {
4195 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
4196 if (p->type == bfd_indirect_link_order)
4197 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
4198 (*secpp)->link_order_head = NULL;
4199 *secpp = (*secpp)->next;
4200 --abfd->section_count;
4201
4202 break;
4203 }
4204 }
4205
252b5132
RH
4206 /* Get a value for the GP register. */
4207 if (elf_gp (abfd) == 0)
4208 {
4209 struct bfd_link_hash_entry *h;
4210
4211 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4212 if (h != (struct bfd_link_hash_entry *) NULL
4213 && h->type == bfd_link_hash_defined)
4214 elf_gp (abfd) = (h->u.def.value
4215 + h->u.def.section->output_section->vma
4216 + h->u.def.section->output_offset);
4217 else if (info->relocateable)
4218 {
4219 bfd_vma lo;
4220
303f629d 4221 /* Find the GP-relative section with the lowest offset. */
252b5132
RH
4222 lo = (bfd_vma) -1;
4223 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
303f629d
MM
4224 if (o->vma < lo
4225 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
4226 lo = o->vma;
4227
4228 /* And calculate GP relative to that. */
252b5132
RH
4229 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
4230 }
4231 else
4232 {
4233 /* If the relocate_section function needs to do a reloc
4234 involving the GP value, it should make a reloc_dangerous
4235 callback to warn that GP is not defined. */
4236 }
4237 }
4238
4239 /* Go through the sections and collect the .reginfo and .mdebug
4240 information. */
4241 reginfo_sec = NULL;
4242 mdebug_sec = NULL;
4243 gptab_data_sec = NULL;
4244 gptab_bss_sec = NULL;
4245 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4246 {
4247 if (strcmp (o->name, ".reginfo") == 0)
4248 {
4249 memset (&reginfo, 0, sizeof reginfo);
4250
4251 /* We have found the .reginfo section in the output file.
4252 Look through all the link_orders comprising it and merge
4253 the information together. */
4254 for (p = o->link_order_head;
4255 p != (struct bfd_link_order *) NULL;
4256 p = p->next)
4257 {
4258 asection *input_section;
4259 bfd *input_bfd;
4260 Elf32_External_RegInfo ext;
4261 Elf32_RegInfo sub;
4262
4263 if (p->type != bfd_indirect_link_order)
4264 {
4265 if (p->type == bfd_fill_link_order)
4266 continue;
4267 abort ();
4268 }
4269
4270 input_section = p->u.indirect.section;
4271 input_bfd = input_section->owner;
4272
4273 /* The linker emulation code has probably clobbered the
4274 size to be zero bytes. */
4275 if (input_section->_raw_size == 0)
4276 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
4277
4278 if (! bfd_get_section_contents (input_bfd, input_section,
4279 (PTR) &ext,
4280 (file_ptr) 0,
4281 sizeof ext))
4282 return false;
4283
4284 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
4285
4286 reginfo.ri_gprmask |= sub.ri_gprmask;
4287 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
4288 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
4289 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
4290 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
4291
4292 /* ri_gp_value is set by the function
4293 mips_elf32_section_processing when the section is
4294 finally written out. */
4295
4296 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4297 elf_link_input_bfd ignores this section. */
4298 input_section->flags &=~ SEC_HAS_CONTENTS;
4299 }
4300
4301 /* Size has been set in mips_elf_always_size_sections */
4302 BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo));
4303
4304 /* Skip this section later on (I don't think this currently
4305 matters, but someday it might). */
4306 o->link_order_head = (struct bfd_link_order *) NULL;
4307
4308 reginfo_sec = o;
4309 }
4310
4311 if (strcmp (o->name, ".mdebug") == 0)
4312 {
4313 struct extsym_info einfo;
4314
4315 /* We have found the .mdebug section in the output file.
4316 Look through all the link_orders comprising it and merge
4317 the information together. */
4318 symhdr->magic = swap->sym_magic;
4319 /* FIXME: What should the version stamp be? */
4320 symhdr->vstamp = 0;
4321 symhdr->ilineMax = 0;
4322 symhdr->cbLine = 0;
4323 symhdr->idnMax = 0;
4324 symhdr->ipdMax = 0;
4325 symhdr->isymMax = 0;
4326 symhdr->ioptMax = 0;
4327 symhdr->iauxMax = 0;
4328 symhdr->issMax = 0;
4329 symhdr->issExtMax = 0;
4330 symhdr->ifdMax = 0;
4331 symhdr->crfd = 0;
4332 symhdr->iextMax = 0;
4333
4334 /* We accumulate the debugging information itself in the
4335 debug_info structure. */
4336 debug.line = NULL;
4337 debug.external_dnr = NULL;
4338 debug.external_pdr = NULL;
4339 debug.external_sym = NULL;
4340 debug.external_opt = NULL;
4341 debug.external_aux = NULL;
4342 debug.ss = NULL;
4343 debug.ssext = debug.ssext_end = NULL;
4344 debug.external_fdr = NULL;
4345 debug.external_rfd = NULL;
4346 debug.external_ext = debug.external_ext_end = NULL;
4347
4348 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
4349 if (mdebug_handle == (PTR) NULL)
4350 return false;
4351
4352 if (SGI_COMPAT (abfd))
4353 {
4354 asection *s;
4355 EXTR esym;
4356 bfd_vma last;
4357 unsigned int i;
4358 static const char * const name[] =
4359 { ".text", ".init", ".fini", ".data",
4360 ".rodata", ".sdata", ".sbss", ".bss" };
4361 static const int sc[] = { scText, scInit, scFini, scData,
4362 scRData, scSData, scSBss, scBss };
4363
4364 esym.jmptbl = 0;
4365 esym.cobol_main = 0;
4366 esym.weakext = 0;
4367 esym.reserved = 0;
4368 esym.ifd = ifdNil;
4369 esym.asym.iss = issNil;
4370 esym.asym.st = stLocal;
4371 esym.asym.reserved = 0;
4372 esym.asym.index = indexNil;
4373 last = 0;
4374 for (i = 0; i < 8; i++)
4375 {
4376 esym.asym.sc = sc[i];
4377 s = bfd_get_section_by_name (abfd, name[i]);
4378 if (s != NULL)
4379 {
4380 esym.asym.value = s->vma;
4381 last = s->vma + s->_raw_size;
4382 }
4383 else
4384 esym.asym.value = last;
4385
4386 if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
4387 name[i], &esym))
4388 return false;
4389 }
4390 }
4391
4392 for (p = o->link_order_head;
4393 p != (struct bfd_link_order *) NULL;
4394 p = p->next)
4395 {
4396 asection *input_section;
4397 bfd *input_bfd;
4398 const struct ecoff_debug_swap *input_swap;
4399 struct ecoff_debug_info input_debug;
4400 char *eraw_src;
4401 char *eraw_end;
4402
4403 if (p->type != bfd_indirect_link_order)
4404 {
4405 if (p->type == bfd_fill_link_order)
4406 continue;
4407 abort ();
4408 }
4409
4410 input_section = p->u.indirect.section;
4411 input_bfd = input_section->owner;
4412
4413 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4414 || (get_elf_backend_data (input_bfd)
4415 ->elf_backend_ecoff_debug_swap) == NULL)
4416 {
4417 /* I don't know what a non MIPS ELF bfd would be
4418 doing with a .mdebug section, but I don't really
4419 want to deal with it. */
4420 continue;
4421 }
4422
4423 input_swap = (get_elf_backend_data (input_bfd)
4424 ->elf_backend_ecoff_debug_swap);
4425
4426 BFD_ASSERT (p->size == input_section->_raw_size);
4427
4428 /* The ECOFF linking code expects that we have already
4429 read in the debugging information and set up an
4430 ecoff_debug_info structure, so we do that now. */
4431 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
4432 &input_debug))
4433 return false;
4434
4435 if (! (bfd_ecoff_debug_accumulate
4436 (mdebug_handle, abfd, &debug, swap, input_bfd,
4437 &input_debug, input_swap, info)))
4438 return false;
4439
4440 /* Loop through the external symbols. For each one with
4441 interesting information, try to find the symbol in
4442 the linker global hash table and save the information
4443 for the output external symbols. */
4444 eraw_src = input_debug.external_ext;
4445 eraw_end = (eraw_src
4446 + (input_debug.symbolic_header.iextMax
4447 * input_swap->external_ext_size));
4448 for (;
4449 eraw_src < eraw_end;
4450 eraw_src += input_swap->external_ext_size)
4451 {
4452 EXTR ext;
4453 const char *name;
4454 struct mips_elf_link_hash_entry *h;
4455
4456 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4457 if (ext.asym.sc == scNil
4458 || ext.asym.sc == scUndefined
4459 || ext.asym.sc == scSUndefined)
4460 continue;
4461
4462 name = input_debug.ssext + ext.asym.iss;
4463 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
4464 name, false, false, true);
4465 if (h == NULL || h->esym.ifd != -2)
4466 continue;
4467
4468 if (ext.ifd != -1)
4469 {
4470 BFD_ASSERT (ext.ifd
4471 < input_debug.symbolic_header.ifdMax);
4472 ext.ifd = input_debug.ifdmap[ext.ifd];
4473 }
4474
4475 h->esym = ext;
4476 }
4477
4478 /* Free up the information we just read. */
4479 free (input_debug.line);
4480 free (input_debug.external_dnr);
4481 free (input_debug.external_pdr);
4482 free (input_debug.external_sym);
4483 free (input_debug.external_opt);
4484 free (input_debug.external_aux);
4485 free (input_debug.ss);
4486 free (input_debug.ssext);
4487 free (input_debug.external_fdr);
4488 free (input_debug.external_rfd);
4489 free (input_debug.external_ext);
4490
4491 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4492 elf_link_input_bfd ignores this section. */
4493 input_section->flags &=~ SEC_HAS_CONTENTS;
4494 }
4495
4496 if (SGI_COMPAT (abfd) && info->shared)
4497 {
4498 /* Create .rtproc section. */
4499 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4500 if (rtproc_sec == NULL)
4501 {
4502 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
4503 | SEC_LINKER_CREATED | SEC_READONLY);
4504
4505 rtproc_sec = bfd_make_section (abfd, ".rtproc");
4506 if (rtproc_sec == NULL
4507 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4508 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
4509 return false;
4510 }
4511
4512 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
4513 info, rtproc_sec, &debug))
4514 return false;
4515 }
4516
4517 /* Build the external symbol information. */
4518 einfo.abfd = abfd;
4519 einfo.info = info;
4520 einfo.debug = &debug;
4521 einfo.swap = swap;
4522 einfo.failed = false;
4523 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4524 mips_elf_output_extsym,
4525 (PTR) &einfo);
4526 if (einfo.failed)
4527 return false;
4528
4529 /* Set the size of the .mdebug section. */
4530 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4531
4532 /* Skip this section later on (I don't think this currently
4533 matters, but someday it might). */
4534 o->link_order_head = (struct bfd_link_order *) NULL;
4535
4536 mdebug_sec = o;
4537 }
4538
4539 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4540 {
4541 const char *subname;
4542 unsigned int c;
4543 Elf32_gptab *tab;
4544 Elf32_External_gptab *ext_tab;
4545 unsigned int i;
4546
4547 /* The .gptab.sdata and .gptab.sbss sections hold
4548 information describing how the small data area would
4549 change depending upon the -G switch. These sections
4550 not used in executables files. */
4551 if (! info->relocateable)
4552 {
4553 asection **secpp;
4554
4555 for (p = o->link_order_head;
4556 p != (struct bfd_link_order *) NULL;
4557 p = p->next)
4558 {
4559 asection *input_section;
4560
4561 if (p->type != bfd_indirect_link_order)
4562 {
4563 if (p->type == bfd_fill_link_order)
4564 continue;
4565 abort ();
4566 }
4567
4568 input_section = p->u.indirect.section;
4569
4570 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4571 elf_link_input_bfd ignores this section. */
4572 input_section->flags &=~ SEC_HAS_CONTENTS;
4573 }
4574
4575 /* Skip this section later on (I don't think this
4576 currently matters, but someday it might). */
4577 o->link_order_head = (struct bfd_link_order *) NULL;
4578
4579 /* Really remove the section. */
4580 for (secpp = &abfd->sections;
4581 *secpp != o;
4582 secpp = &(*secpp)->next)
4583 ;
4584 *secpp = (*secpp)->next;
4585 --abfd->section_count;
4586
4587 continue;
4588 }
4589
4590 /* There is one gptab for initialized data, and one for
4591 uninitialized data. */
4592 if (strcmp (o->name, ".gptab.sdata") == 0)
4593 gptab_data_sec = o;
4594 else if (strcmp (o->name, ".gptab.sbss") == 0)
4595 gptab_bss_sec = o;
4596 else
4597 {
4598 (*_bfd_error_handler)
4599 (_("%s: illegal section name `%s'"),
4600 bfd_get_filename (abfd), o->name);
4601 bfd_set_error (bfd_error_nonrepresentable_section);
4602 return false;
4603 }
4604
4605 /* The linker script always combines .gptab.data and
4606 .gptab.sdata into .gptab.sdata, and likewise for
4607 .gptab.bss and .gptab.sbss. It is possible that there is
4608 no .sdata or .sbss section in the output file, in which
4609 case we must change the name of the output section. */
4610 subname = o->name + sizeof ".gptab" - 1;
4611 if (bfd_get_section_by_name (abfd, subname) == NULL)
4612 {
4613 if (o == gptab_data_sec)
4614 o->name = ".gptab.data";
4615 else
4616 o->name = ".gptab.bss";
4617 subname = o->name + sizeof ".gptab" - 1;
4618 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4619 }
4620
4621 /* Set up the first entry. */
4622 c = 1;
4623 tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
4624 if (tab == NULL)
4625 return false;
4626 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4627 tab[0].gt_header.gt_unused = 0;
4628
4629 /* Combine the input sections. */
4630 for (p = o->link_order_head;
4631 p != (struct bfd_link_order *) NULL;
4632 p = p->next)
4633 {
4634 asection *input_section;
4635 bfd *input_bfd;
4636 bfd_size_type size;
4637 unsigned long last;
4638 bfd_size_type gpentry;
4639
4640 if (p->type != bfd_indirect_link_order)
4641 {
4642 if (p->type == bfd_fill_link_order)
4643 continue;
4644 abort ();
4645 }
4646
4647 input_section = p->u.indirect.section;
4648 input_bfd = input_section->owner;
4649
4650 /* Combine the gptab entries for this input section one
4651 by one. We know that the input gptab entries are
4652 sorted by ascending -G value. */
4653 size = bfd_section_size (input_bfd, input_section);
4654 last = 0;
4655 for (gpentry = sizeof (Elf32_External_gptab);
4656 gpentry < size;
4657 gpentry += sizeof (Elf32_External_gptab))
4658 {
4659 Elf32_External_gptab ext_gptab;
4660 Elf32_gptab int_gptab;
4661 unsigned long val;
4662 unsigned long add;
4663 boolean exact;
4664 unsigned int look;
4665
4666 if (! (bfd_get_section_contents
4667 (input_bfd, input_section, (PTR) &ext_gptab,
4668 gpentry, sizeof (Elf32_External_gptab))))
4669 {
4670 free (tab);
4671 return false;
4672 }
4673
4674 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
4675 &int_gptab);
4676 val = int_gptab.gt_entry.gt_g_value;
4677 add = int_gptab.gt_entry.gt_bytes - last;
4678
4679 exact = false;
4680 for (look = 1; look < c; look++)
4681 {
4682 if (tab[look].gt_entry.gt_g_value >= val)
4683 tab[look].gt_entry.gt_bytes += add;
4684
4685 if (tab[look].gt_entry.gt_g_value == val)
4686 exact = true;
4687 }
4688
4689 if (! exact)
4690 {
4691 Elf32_gptab *new_tab;
4692 unsigned int max;
4693
4694 /* We need a new table entry. */
4695 new_tab = ((Elf32_gptab *)
4696 bfd_realloc ((PTR) tab,
4697 (c + 1) * sizeof (Elf32_gptab)));
4698 if (new_tab == NULL)
4699 {
4700 free (tab);
4701 return false;
4702 }
4703 tab = new_tab;
4704 tab[c].gt_entry.gt_g_value = val;
4705 tab[c].gt_entry.gt_bytes = add;
4706
4707 /* Merge in the size for the next smallest -G
4708 value, since that will be implied by this new
4709 value. */
4710 max = 0;
4711 for (look = 1; look < c; look++)
4712 {
4713 if (tab[look].gt_entry.gt_g_value < val
4714 && (max == 0
4715 || (tab[look].gt_entry.gt_g_value
4716 > tab[max].gt_entry.gt_g_value)))
4717 max = look;
4718 }
4719 if (max != 0)
4720 tab[c].gt_entry.gt_bytes +=
4721 tab[max].gt_entry.gt_bytes;
4722
4723 ++c;
4724 }
4725
4726 last = int_gptab.gt_entry.gt_bytes;
4727 }
4728
4729 /* Hack: reset the SEC_HAS_CONTENTS flag so that
4730 elf_link_input_bfd ignores this section. */
4731 input_section->flags &=~ SEC_HAS_CONTENTS;
4732 }
4733
4734 /* The table must be sorted by -G value. */
4735 if (c > 2)
4736 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4737
4738 /* Swap out the table. */
4739 ext_tab = ((Elf32_External_gptab *)
4740 bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
4741 if (ext_tab == NULL)
4742 {
4743 free (tab);
4744 return false;
4745 }
4746
4747 for (i = 0; i < c; i++)
4748 bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
4749 free (tab);
4750
4751 o->_raw_size = c * sizeof (Elf32_External_gptab);
4752 o->contents = (bfd_byte *) ext_tab;
4753
4754 /* Skip this section later on (I don't think this currently
4755 matters, but someday it might). */
4756 o->link_order_head = (struct bfd_link_order *) NULL;
4757 }
4758 }
4759
4760 /* Invoke the regular ELF backend linker to do all the work. */
4761 if (! bfd_elf32_bfd_final_link (abfd, info))
4762 return false;
4763
4764 /* Now write out the computed sections. */
4765
4766 if (reginfo_sec != (asection *) NULL)
4767 {
4768 Elf32_External_RegInfo ext;
4769
4770 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
4771 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4772 (file_ptr) 0, sizeof ext))
4773 return false;
4774 }
4775
4776 if (mdebug_sec != (asection *) NULL)
4777 {
4778 BFD_ASSERT (abfd->output_has_begun);
4779 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4780 swap, info,
4781 mdebug_sec->filepos))
4782 return false;
4783
4784 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4785 }
4786
4787 if (gptab_data_sec != (asection *) NULL)
4788 {
4789 if (! bfd_set_section_contents (abfd, gptab_data_sec,
4790 gptab_data_sec->contents,
4791 (file_ptr) 0,
4792 gptab_data_sec->_raw_size))
4793 return false;
4794 }
4795
4796 if (gptab_bss_sec != (asection *) NULL)
4797 {
4798 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4799 gptab_bss_sec->contents,
4800 (file_ptr) 0,
4801 gptab_bss_sec->_raw_size))
4802 return false;
4803 }
4804
4805 if (SGI_COMPAT (abfd))
4806 {
4807 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4808 if (rtproc_sec != NULL)
4809 {
4810 if (! bfd_set_section_contents (abfd, rtproc_sec,
4811 rtproc_sec->contents,
4812 (file_ptr) 0,
4813 rtproc_sec->_raw_size))
4814 return false;
4815 }
4816 }
4817
4818 return true;
4819}
4820
4821/* Handle a MIPS ELF HI16 reloc. */
4822
4823static void
4824mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
4825 bfd *input_bfd;
4826 Elf_Internal_Rela *relhi;
4827 Elf_Internal_Rela *rello;
4828 bfd_byte *contents;
4829 bfd_vma addend;
4830{
4831 bfd_vma insn;
4832 bfd_vma addlo;
4833
4834 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4835
4836 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4837 addlo &= 0xffff;
4838
4839 addend += ((insn & 0xffff) << 16) + addlo;
4840
4841 if ((addlo & 0x8000) != 0)
4842 addend -= 0x10000;
4843 if ((addend & 0x8000) != 0)
4844 addend += 0x10000;
4845
4846 bfd_put_32 (input_bfd,
4847 (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
4848 contents + relhi->r_offset);
4849}
4850
4851/* Handle a MIPS ELF local GOT16 reloc. */
4852
4853static boolean
4854mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
4855 contents, addend)
4856 bfd *output_bfd;
4857 bfd *input_bfd;
4858 asection *sgot;
4859 Elf_Internal_Rela *relhi;
4860 Elf_Internal_Rela *rello;
4861 bfd_byte *contents;
4862 bfd_vma addend;
4863{
4864 unsigned int assigned_gotno;
4865 unsigned int i;
4866 bfd_vma insn;
4867 bfd_vma addlo;
4868 bfd_vma address;
4869 bfd_vma hipage;
4870 bfd_byte *got_contents;
4871 struct mips_got_info *g;
4872
4873 insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4874
4875 addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4876 addlo &= 0xffff;
4877
4878 addend += ((insn & 0xffff) << 16) + addlo;
4879
4880 if ((addlo & 0x8000) != 0)
4881 addend -= 0x10000;
4882 if ((addend & 0x8000) != 0)
4883 addend += 0x10000;
4884
4885 /* Get a got entry representing requested hipage. */
4886 BFD_ASSERT (elf_section_data (sgot) != NULL);
4887 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4888 BFD_ASSERT (g != NULL);
4889
4890 assigned_gotno = g->assigned_gotno;
4891 got_contents = sgot->contents;
4892 hipage = addend & 0xffff0000;
4893
4894 for (i = MIPS_RESERVED_GOTNO; i < assigned_gotno; i++)
4895 {
4896 address = bfd_get_32 (input_bfd, got_contents + i * 4);
4897 if (hipage == (address & 0xffff0000))
4898 break;
4899 }
4900
4901 if (i == assigned_gotno)
4902 {
4903 if (assigned_gotno >= g->local_gotno)
4904 {
4905 (*_bfd_error_handler)
4906 (_("more got entries are needed for hipage relocations"));
4907 bfd_set_error (bfd_error_bad_value);
4908 return false;
4909 }
4910
4911 bfd_put_32 (input_bfd, hipage, got_contents + assigned_gotno * 4);
4912 ++g->assigned_gotno;
4913 }
4914
4915 i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
4916 bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
4917 contents + relhi->r_offset);
4918
4919 return true;
4920}
4921
4922/* Handle MIPS ELF CALL16 reloc and global GOT16 reloc. */
4923
4924static void
4925mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
4926 bfd *input_bfd;
4927 Elf_Internal_Rela *rel;
4928 bfd_byte *contents;
4929 bfd_vma offset;
4930{
4931 bfd_vma insn;
4932
4933 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
4934 bfd_put_32 (input_bfd,
4935 (insn & 0xffff0000) | (offset & 0xffff),
4936 contents + rel->r_offset);
4937}
4938
4939/* Relocate a MIPS ELF section. */
4940
4941static boolean
4942mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4943 contents, relocs, local_syms, local_sections)
4944 bfd *output_bfd;
4945 struct bfd_link_info *info;
4946 bfd *input_bfd;
4947 asection *input_section;
4948 bfd_byte *contents;
4949 Elf_Internal_Rela *relocs;
4950 Elf_Internal_Sym *local_syms;
4951 asection **local_sections;
4952{
4953 Elf_Internal_Shdr *symtab_hdr;
4954 size_t locsymcount;
4955 size_t extsymoff;
4956 asection *sgot, *sreloc, *scpt;
4957 bfd *dynobj;
4958 bfd_vma gp;
4959 Elf_Internal_Rela *rel;
4960 Elf_Internal_Rela *relend;
4961 struct mips_got_info *g;
4962
4963 dynobj = elf_hash_table (info)->dynobj;
4964 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4965
4966 sgot = NULL;
4967 sreloc = NULL;
4968 if (dynobj == NULL || ! SGI_COMPAT (output_bfd))
4969 scpt = NULL;
4970 else
4971 scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
4972 g = NULL;
4973
4974 if (elf_bad_symtab (input_bfd))
4975 {
4976 locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
4977 extsymoff = 0;
4978 }
4979 else
4980 {
4981 locsymcount = symtab_hdr->sh_info;
4982 extsymoff = symtab_hdr->sh_info;
4983 }
4984
4985 gp = _bfd_get_gp_value (output_bfd);
4986
4987 rel = relocs;
4988 relend = relocs + input_section->reloc_count;
4989 for (; rel < relend; rel++)
4990 {
4991 int r_type;
4992 reloc_howto_type *howto;
4993 unsigned long r_symndx;
4994 bfd_vma addend;
4995 struct elf_link_hash_entry *h;
4996 asection *sec;
4997 Elf_Internal_Sym *sym;
4998 struct mips_elf_link_hash_entry *mh;
4999 int other;
5000 bfd_reloc_status_type r;
5001
5002 r_type = ELF32_R_TYPE (rel->r_info);
5003 if (r_type == R_MIPS_GNU_VTINHERIT
5004 || r_type == R_MIPS_GNU_VTENTRY)
5005 continue;
5006 if ((r_type < 0 || r_type >= (int) R_MIPS_max)
5007 && r_type != R_MIPS16_26
5008 && r_type != R_MIPS16_GPREL)
5009 {
5010 bfd_set_error (bfd_error_bad_value);
5011 return false;
5012 }
5013 if (r_type == R_MIPS16_26)
5014 howto = &elf_mips16_jump_howto;
5015 else if (r_type == R_MIPS16_GPREL)
5016 howto = &elf_mips16_gprel_howto;
5017 else
5018 howto = elf_mips_howto_table + r_type;
5019
5020 if (dynobj != NULL
5021 && (r_type == R_MIPS_CALL16
5022 || r_type == R_MIPS_GOT16
5023 || r_type == R_MIPS_CALL_HI16
5024 || r_type == R_MIPS_CALL_LO16
5025 || r_type == R_MIPS_GOT_HI16
5026 || r_type == R_MIPS_GOT_LO16))
5027 {
5028 /* We need the .got section. */
5029 if (sgot == NULL)
5030 {
5031 sgot = bfd_get_section_by_name (dynobj, ".got");
5032 BFD_ASSERT (sgot != NULL);
5033 BFD_ASSERT (elf_section_data (sgot) != NULL);
5034 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5035 BFD_ASSERT (g != NULL);
5036 }
5037 }
5038
5039 r_symndx = ELF32_R_SYM (rel->r_info);
5040
5041 /* Mix in the change in GP address for a GP relative reloc. */
5042 if (r_type != R_MIPS_GPREL16
5043 && r_type != R_MIPS_LITERAL
5044 && r_type != R_MIPS_GPREL32
5045 && r_type != R_MIPS16_GPREL)
5046 addend = 0;
5047 else
5048 {
5049 if (gp == 0)
5050 {
5051 if (! ((*info->callbacks->reloc_dangerous)
5052 (info,
5053 _("GP relative relocation when GP not defined"),
5054 input_bfd, input_section,
5055 rel->r_offset)))
5056 return false;
5057 /* Only give the error once per link. */
5058 gp = 4;
5059 _bfd_set_gp_value (output_bfd, gp);
5060 }
5061
5062 if (r_symndx < extsymoff
5063 || (elf_bad_symtab (input_bfd)
5064 && local_sections[r_symndx] != NULL))
5065 {
5066 /* This is a relocation against a section. The current
5067 addend in the instruction is the difference between
5068 INPUT_SECTION->vma and the GP value of INPUT_BFD. We
5069 must change this to be the difference between the
5070 final definition (which will end up in RELOCATION)
5071 and the GP value of OUTPUT_BFD (which is in GP). */
5072 addend = elf_gp (input_bfd) - gp;
5073 }
5074 else if (! info->relocateable)
5075 {
5076 /* We are doing a final link. The current addend in the
5077 instruction is simply the desired offset into the
5078 symbol (normally zero). We want the instruction to
5079 hold the difference between the final definition of
5080 the symbol (which will end up in RELOCATION) and the
5081 GP value of OUTPUT_BFD (which is in GP). */
5082 addend = - gp;
5083 }
5084 else
5085 {
5086 /* We are generating relocateable output, and we aren't
5087 going to define this symbol, so we just leave the
5088 instruction alone. */
5089 addend = 0;
5090 }
5091 }
5092
5093 h = NULL;
5094 sym = NULL;
5095 sec = NULL;
5096 if (info->relocateable)
5097 {
5098 /* This is a relocateable link. We don't have to change
5099 anything, unless the reloc is against a section symbol,
5100 in which case we have to adjust according to where the
5101 section symbol winds up in the output section. */
5102 if (r_symndx >= locsymcount
5103 || (elf_bad_symtab (input_bfd)
5104 && local_sections[r_symndx] == NULL))
5105 r = bfd_reloc_ok;
5106 else
5107 {
5108 sym = local_syms + r_symndx;
5109 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5110 r = bfd_reloc_ok;
5111 else
5112 {
5113 sec = local_sections[r_symndx];
5114
5115 /* It would be logical to add sym->st_value here,
5116 but Irix 5 sometimes generates a garbage symbol
5117 value. */
5118 addend += sec->output_offset;
5119
5120 /* If this is HI16 or GOT16 with an associated LO16,
5121 adjust the addend accordingly. Otherwise, just
5122 relocate. */
5123 if (r_type == R_MIPS_64 && bfd_big_endian (input_bfd))
5124 r = _bfd_relocate_contents (howto, input_bfd,
5125 addend,
5126 contents + rel->r_offset + 4);
5127 else if (r_type != R_MIPS_HI16 && r_type != R_MIPS_GOT16)
5128 r = _bfd_relocate_contents (howto, input_bfd,
5129 addend,
5130 contents + rel->r_offset);
5131 else
5132 {
5133 Elf_Internal_Rela *lorel;
5134
5135 /* As a GNU extension, permit an arbitrary
5136 number of R_MIPS_HI16 relocs before the
5137 R_MIPS_LO16 reloc. This permits gcc to emit
5138 the HI and LO relocs itself. */
5139 if (r_type == R_MIPS_GOT16)
5140 lorel = rel + 1;
5141 else
5142 {
5143 for (lorel = rel + 1;
5144 (lorel < relend
5145 && (ELF32_R_TYPE (lorel->r_info)
5146 == R_MIPS_HI16));
5147 lorel++)
5148 ;
5149 }
5150 if (lorel < relend
5151 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
5152 {
5153 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
5154 contents, addend);
5155 r = bfd_reloc_ok;
5156 }
5157 else
5158 r = _bfd_relocate_contents (howto, input_bfd,
5159 addend,
5160 contents + rel->r_offset);
5161 }
5162 }
5163 }
5164 }
5165 else
5166 {
5167 bfd_vma relocation;
5168 boolean local;
5169 boolean undefined_error;
5170
5171 /* This is a final link. */
5172 undefined_error = false;
5173 sym = NULL;
5174 if (r_symndx < extsymoff
5175 || (elf_bad_symtab (input_bfd)
5176 && local_sections[r_symndx] != NULL))
5177 {
5178 local = true;
5179 sym = local_syms + r_symndx;
5180 sec = local_sections[r_symndx];
5181 relocation = (sec->output_section->vma
5182 + sec->output_offset);
5183
5184 /* It would be logical to always add sym->st_value here,
5185 but Irix 5 sometimes generates a garbage symbol
5186 value. */
5187 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
5188 relocation += sym->st_value;
5189
5190 /* mips16 text labels should be treated as odd. */
5191 if (sym->st_other == STO_MIPS16)
5192 ++relocation;
5193 }
5194 else
5195 {
5196 long indx;
5197
5198 local = false;
5199 indx = r_symndx - extsymoff;
5200 h = elf_sym_hashes (input_bfd)[indx];
5201 while (h->root.type == bfd_link_hash_indirect
5202 || h->root.type == bfd_link_hash_warning)
5203 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5204 if (strcmp (h->root.root.string, "_gp_disp") == 0)
5205 {
5206 if (gp == 0)
5207 {
5208 if (! ((*info->callbacks->reloc_dangerous)
5209 (info,
5210 _("_gp_disp used when GP not defined"),
5211 input_bfd, input_section,
5212 rel->r_offset)))
5213 return false;
5214 /* Only give the error once per link. */
5215 gp = 4;
5216 _bfd_set_gp_value (output_bfd, gp);
5217 relocation = 0;
5218 }
5219 else
5220 {
5221 sec = input_section;
5222 if (sec->output_section != NULL)
5223 relocation = (gp
5224 - (rel->r_offset
5225 + sec->output_section->vma
5226 + sec->output_offset));
5227 else
5228 relocation = gp - rel->r_offset;
5229 if (r_type == R_MIPS_LO16)
5230 relocation += 4;
5231 }
5232 }
5233 else if (h->root.type == bfd_link_hash_defined
5234 || h->root.type == bfd_link_hash_defweak)
5235 {
5236 sec = h->root.u.def.section;
5237 if (sec->output_section == NULL)
5238 relocation = 0;
5239 else
5240 relocation = (h->root.u.def.value
5241 + sec->output_section->vma
5242 + sec->output_offset);
5243 }
5244 else if (h->root.type == bfd_link_hash_undefweak)
5245 relocation = 0;
5246 else if (info->shared && !info->symbolic && !info->no_undefined)
5247 relocation = 0;
5248 else if (strcmp (h->root.root.string, "_DYNAMIC_LINK") == 0)
5249 {
5250 /* If this is a dynamic link, we should have created
5251 a _DYNAMIC_LINK symbol in
5252 mips_elf_create_dynamic_sections. Otherwise, we
5253 should define the symbol with a value of 0.
5254 FIXME: It should probably get into the symbol
5255 table somehow as well. */
5256 BFD_ASSERT (! info->shared);
5257 BFD_ASSERT (bfd_get_section_by_name (output_bfd,
5258 ".dynamic") == NULL);
5259 relocation = 0;
5260 }
5261 else
5262 {
5263 if (! ((*info->callbacks->undefined_symbol)
5264 (info, h->root.root.string, input_bfd,
5265 input_section, rel->r_offset)))
5266 return false;
5267 undefined_error = true;
5268 relocation = 0;
5269 }
5270 }
5271
5272 mh = (struct mips_elf_link_hash_entry *) h;
5273 if (h != NULL)
5274 other = h->other;
5275 else if (sym != NULL)
5276 other = sym->st_other;
5277 else
5278 other = 0;
5279
5280 /* If this function has an fn_stub, then it is a mips16
5281 function which needs a stub if it is called by a 32 bit
5282 function. If this reloc is anything other than a 16 bit
5283 call, redirect the reloc to the stub. We don't redirect
5284 relocs from other stub functions. */
5285 if (r_type != R_MIPS16_26
5286 && ((mh != NULL
5287 && mh->fn_stub != NULL)
5288 || (mh == NULL
5289 && elf_tdata (input_bfd)->local_stubs != NULL
5290 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5291 && strncmp (bfd_get_section_name (input_bfd, input_section),
5292 FN_STUB, sizeof FN_STUB - 1) != 0
5293 && strncmp (bfd_get_section_name (input_bfd, input_section),
5294 CALL_STUB, sizeof CALL_STUB - 1) != 0
5295 && strncmp (bfd_get_section_name (input_bfd, input_section),
5296 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) != 0)
5297 {
5298 if (mh != NULL)
5299 {
5300 BFD_ASSERT (mh->need_fn_stub);
5301 relocation = (mh->fn_stub->output_section->vma
5302 + mh->fn_stub->output_offset);
5303 }
5304 else
5305 {
5306 asection *fn_stub;
5307
5308 fn_stub = elf_tdata (input_bfd)->local_stubs[r_symndx];
5309 relocation = (fn_stub->output_section->vma
5310 + fn_stub->output_offset);
5311 }
5312
5313 /* RELOCATION now points to 32 bit code. */
5314 other = 0;
5315 }
5316
5317 /* If this function has a call_stub, then it is called by a
5318 mips16 function; the call needs to go through a stub if
5319 this function is a 32 bit function. If this reloc is a
5320 16 bit call, and the symbol is not a 16 bit function,
5321 then redirect the reloc to the stub. Note that we don't
5322 need to worry about calling the function through a
5323 function pointer; such calls are handled by routing
5324 through a special mips16 routine. We don't have to check
5325 whether this call is from a stub; it can't be, because a
5326 stub contains 32 bit code, and hence can not have a 16
5327 bit reloc. */
5328 if (r_type == R_MIPS16_26
5329 && mh != NULL
5330 && (mh->call_stub != NULL || mh->call_fp_stub != NULL)
5331 && other != STO_MIPS16)
5332 {
5333 asection *stub;
5334
5335 /* If both call_stub and call_fp_stub are defined, we
5336 can figure out which one to use by seeing which one
5337 appears in the input file. */
5338 if (mh->call_stub != NULL && mh->call_fp_stub != NULL)
5339 {
5340 asection *o;
5341
5342 stub = NULL;
5343 for (o = input_bfd->sections; o != NULL; o = o->next)
5344 {
5345 if (strncmp (bfd_get_section_name (input_bfd, o),
5346 CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5347 {
5348 stub = mh->call_fp_stub;
5349 break;
5350 }
5351 }
5352 if (stub == NULL)
5353 stub = mh->call_stub;
5354 }
5355 else if (mh->call_stub != NULL)
5356 stub = mh->call_stub;
5357 else
5358 stub = mh->call_fp_stub;
5359
5360 BFD_ASSERT (stub->_raw_size > 0);
5361 relocation = stub->output_section->vma + stub->output_offset;
5362 }
5363
5364 if (r_type == R_MIPS_HI16)
5365 {
5366 Elf_Internal_Rela *lorel;
5367
5368 /* As a GNU extension, permit an arbitrary number of
5369 R_MIPS_HI16 relocs before the R_MIPS_LO16 reloc.
5370 This permits gcc to emit the HI and LO relocs itself. */
5371 for (lorel = rel + 1;
5372 (lorel < relend
5373 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_HI16);
5374 lorel++)
5375 ;
5376 if (lorel < relend
5377 && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
5378 {
5379 mips_elf_relocate_hi16 (input_bfd, rel, lorel,
5380 contents, relocation + addend);
5381 r = bfd_reloc_ok;
5382 }
5383 else
5384 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5385 contents, rel->r_offset,
5386 relocation, addend);
5387 }
5388 else if (r_type == R_MIPS_GOT16 && local)
5389 {
5390 /* GOT16 must also have an associated LO16 in the local
5391 case. In this case, the addend is extracted and the
5392 section in which the referenced object is determined.
5393 Then the final address of the object is computed and
5394 the GOT entry for the hipage (an aligned 64kb chunk)
5395 is added to .got section if needed. The offset field
5396 of the GOT16-relocated instruction is replaced by the
5397 index of this GOT entry for the hipage. */
5398 if ((rel + 1) < relend
5399 && ELF32_R_TYPE ((rel + 1)->r_info) == R_MIPS_LO16)
5400 {
5401 if (! mips_elf_relocate_got_local (output_bfd, input_bfd,
5402 sgot, rel, rel + 1,
5403 contents,
5404 relocation + addend))
5405 return false;
5406 r = bfd_reloc_ok;
5407 }
5408 else
5409 r = bfd_reloc_outofrange;
5410 }
5411 else if (r_type == R_MIPS_CALL16
5412 || r_type == R_MIPS_GOT16
5413 || r_type == R_MIPS_CALL_LO16
5414 || r_type == R_MIPS_GOT_LO16)
5415 {
5416 bfd_vma offset;
5417
5418 /* This symbol must be registered as a global symbol
5419 having the corresponding got entry. */
5420 BFD_ASSERT (h->got.offset != (bfd_vma) -1);
5421
5422 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5423 BFD_ASSERT (g->local_gotno <= offset
5424 && offset < sgot->_raw_size);
5425 bfd_put_32 (output_bfd, relocation + addend,
5426 sgot->contents + offset);
5427 offset = (sgot->output_section->vma + sgot->output_offset
5428 + offset - gp);
5429 mips_elf_relocate_global_got (input_bfd, rel, contents,
5430 offset);
5431 r = bfd_reloc_ok;
5432 }
5433 else if (r_type == R_MIPS_CALL_HI16
5434 || r_type == R_MIPS_GOT_HI16)
5435 {
5436 bfd_vma offset;
5437
5438 /* This must be a global symbol with a got entry. The
5439 next reloc must be the corresponding LO16 reloc. */
5440 BFD_ASSERT (h != NULL && h->got.offset != (bfd_vma) -1);
5441 BFD_ASSERT ((rel + 1) < relend);
5442 BFD_ASSERT ((int) ELF32_R_TYPE ((rel + 1)->r_info)
5443 == (r_type == R_MIPS_CALL_HI16
5444 ? (int) R_MIPS_CALL_LO16
5445 : (int) R_MIPS_GOT_LO16));
5446
5447 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5448 BFD_ASSERT (g->local_gotno <= offset
5449 && offset < sgot->_raw_size);
5450 bfd_put_32 (output_bfd, relocation + addend,
5451 sgot->contents + offset);
5452 offset = (sgot->output_section->vma + sgot->output_offset
5453 + offset - gp);
5454 mips_elf_relocate_hi16 (input_bfd, rel, rel + 1, contents,
5455 offset);
5456 r = bfd_reloc_ok;
5457 }
5458 else if (r_type == R_MIPS_REL32
5459 || r_type == R_MIPS_32)
5460 {
5461 Elf_Internal_Rel outrel;
5462 Elf32_crinfo cptrel;
5463 bfd_byte *cr;
5464
5465 if ((info->shared
5466 || (elf_hash_table (info)->dynamic_sections_created
5467 && h != NULL
5468 && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
5469 == 0)))
5470 && (input_section->flags & SEC_ALLOC) != 0)
5471 {
5472 boolean skip;
5473
5474 /* When generating a shared object, these
5475 relocations are copied into the output file to be
5476 resolved at run time. */
5477 if (sreloc == NULL)
5478 {
5479 sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
5480 BFD_ASSERT (sreloc != NULL);
5481 }
5482
5483 skip = false;
5484
5485 if (elf_section_data (input_section)->stab_info == NULL)
5486 outrel.r_offset = rel->r_offset;
5487 else
5488 {
5489 bfd_vma off;
5490
5491 off = (_bfd_stab_section_offset
5492 (output_bfd, &elf_hash_table (info)->stab_info,
5493 input_section,
5494 &elf_section_data (input_section)->stab_info,
5495 rel->r_offset));
5496 if (off == (bfd_vma) -1)
5497 skip = true;
5498 outrel.r_offset = off;
5499 }
5500
5501 outrel.r_offset += (input_section->output_section->vma
5502 + input_section->output_offset);
5503
5504 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
5505
5506 if (skip)
5507 memset (&outrel, 0, sizeof outrel);
5508 else if (h != NULL
5509 && (! info->symbolic
5510 || (h->elf_link_hash_flags
5511 & ELF_LINK_HASH_DEF_REGULAR) == 0))
5512 {
5513 BFD_ASSERT (h->dynindx != -1);
5514 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_REL32);
5515 sec = input_section;
5516 }
5517 else
5518 {
5519 long indx;
5520
5521 if (h == NULL)
5522 sec = local_sections[r_symndx];
5523 else
5524 {
5525 BFD_ASSERT (h->root.type == bfd_link_hash_defined
5526 || (h->root.type
5527 == bfd_link_hash_defweak));
5528 sec = h->root.u.def.section;
5529 }
5530 if (sec != NULL && bfd_is_abs_section (sec))
5531 indx = 0;
5532 else if (sec == NULL || sec->owner == NULL)
5533 {
5534 bfd_set_error (bfd_error_bad_value);
5535 return false;
5536 }
5537 else
5538 {
5539 asection *osec;
5540
5541 osec = sec->output_section;
5542 indx = elf_section_data (osec)->dynindx;
5543 if (indx == 0)
5544 abort ();
5545 }
5546
5547 outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
5548 addend += relocation;
5549 }
5550
5551 if (! skip)
5552 bfd_put_32 (output_bfd, addend, contents + rel->r_offset);
5553
5554 bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5555 (((Elf32_External_Rel *)
5556 sreloc->contents)
5557 + sreloc->reloc_count));
5558 ++sreloc->reloc_count;
5559
5560 if (! skip && SGI_COMPAT (output_bfd))
5561 {
5562 if (scpt == NULL)
5563 continue;
5564
5565 /* Make an entry of compact relocation info. */
5566 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5567 cptrel.vaddr = (rel->r_offset
5568 + input_section->output_section->vma
5569 + input_section->output_offset);
5570 if (r_type == R_MIPS_REL32)
5571 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5572 else
5573 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5574 mips_elf_set_cr_dist2to (cptrel, 0);
5575 cptrel.konst = addend;
5576
5577 cr = (scpt->contents
5578 + sizeof (Elf32_External_compact_rel));
5579 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5580 ((Elf32_External_crinfo *) cr
5581 + scpt->reloc_count));
5582 ++scpt->reloc_count;
5583 }
5584
5585 /* This reloc will be computed at runtime, so
5586 there's no need to do anything now. */
5587 continue;
5588 }
5589 else
5590 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5591 contents, rel->r_offset,
5592 relocation, addend);
5593 }
5594 else if (r_type == R_MIPS_64)
5595 {
5596 bfd_size_type addr;
5597 unsigned long val;
5598
5599 /* Do a 32 bit relocation, and sign extend to 64 bits. */
5600 addr = rel->r_offset;
5601 if (bfd_big_endian (input_bfd))
5602 addr += 4;
5603 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5604 contents, addr, relocation,
5605 addend);
5606 val = bfd_get_32 (input_bfd, contents + addr);
5607 if ((val & 0x80000000) != 0)
5608 val = 0xffffffff;
5609 else
5610 val = 0;
5611 addr = rel->r_offset;
5612 if (bfd_little_endian (input_bfd))
5613 addr += 4;
5614 bfd_put_32 (input_bfd, val, contents + addr);
5615 }
5616 else if (r_type == R_MIPS_26 && other == STO_MIPS16)
5617 {
5618 unsigned long insn;
5619
5620 /* This is a jump to a mips16 routine from a mips32
5621 routine. We need to change jal into jalx. */
5622 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5623 if (((insn >> 26) & 0x3f) != 0x3
5624 && ((insn >> 26) & 0x3f) != 0x1d)
5625 {
5626 (*_bfd_error_handler)
5627 (_("%s: %s+0x%lx: jump to mips16 routine which is not jal"),
5628 bfd_get_filename (input_bfd),
5629 input_section->name,
5630 (unsigned long) rel->r_offset);
5631 bfd_set_error (bfd_error_bad_value);
5632 return false;
5633 }
5634 insn = (insn & 0x3ffffff) | (0x1d << 26);
5635 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5636 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5637 contents, rel->r_offset,
5638 relocation, addend);
5639 }
5640 else if (r_type == R_MIPS16_26)
5641 {
5642 /* It's easiest to do the normal relocation, and then
5643 dig out the instruction and swap the first word the
5644 way the mips16 expects it. If this is little endian,
5645 though, we need to swap the two words first, and then
5646 swap them back again later, so that the address looks
5647 right. */
5648
5649 if (bfd_little_endian (input_bfd))
5650 {
5651 unsigned long insn;
5652
5653 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5654 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5655 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5656 }
5657
5658 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5659 contents, rel->r_offset,
5660 relocation, addend);
5661 if (r == bfd_reloc_ok)
5662 {
5663 unsigned long insn;
5664
5665 if (bfd_little_endian (input_bfd))
5666 {
5667 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5668 insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5669 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5670 }
5671
5672 insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
5673 insn = ((insn & 0xfc00)
5674 | ((insn & 0x1f) << 5)
5675 | ((insn & 0x3e0) >> 5));
5676 /* If this is a jump to a 32 bit routine, then make
5677 it jalx. */
5678 if (other != STO_MIPS16)
5679 insn |= 0x400;
5680 bfd_put_16 (input_bfd, insn, contents + rel->r_offset);
5681 }
5682 }
5683 else if (r_type == R_MIPS16_GPREL)
5684 {
5685 unsigned short extend, insn;
5686 bfd_byte buf[4];
5687 unsigned long final;
5688
5689 /* Extract the addend into buf, run the regular reloc,
5690 and stuff the resulting value back into the
5691 instructions. */
5692 if (rel->r_offset > input_section->_raw_size)
5693 r = bfd_reloc_outofrange;
5694 else
5695 {
5696 extend = bfd_get_16 (input_bfd, contents + rel->r_offset);
5697 insn = bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
5698 bfd_put_32 (input_bfd,
5699 (((extend & 0x1f) << 11)
5700 | (extend & 0x7e0)
5701 | (insn & 0x1f)),
5702 buf);
5703 r = _bfd_final_link_relocate (howto, input_bfd,
5704 input_section, buf,
5705 (bfd_vma) 0, relocation,
5706 addend);
5707 final = bfd_get_32 (input_bfd, buf);
5708 bfd_put_16 (input_bfd,
5709 ((extend & 0xf800)
5710 | ((final >> 11) & 0x1f)
5711 | (final & 0x7e0)),
5712 contents + rel->r_offset);
5713 bfd_put_16 (input_bfd,
5714 ((insn & 0xffe0)
5715 | (final & 0x1f)),
5716 contents + rel->r_offset + 2);
5717 }
5718 }
5719 else
5720 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5721 contents, rel->r_offset,
5722 relocation, addend);
5723
5724 /* The jal instruction can only jump to an address which is
5725 divisible by 4, and it can only jump to an address with
5726 the same upper 4 bits as the PC. */
5727 if (r == bfd_reloc_ok
5728 && (r_type == R_MIPS16_26 || r_type == R_MIPS_26))
5729 {
5730 bfd_vma addr;
5731 bfd_vma pc;
5732 bfd_vma target;
5733
5734 pc = ((input_section->output_section->vma
5735 + input_section->output_offset
5736 + rel->r_offset)
5737 & 0xf0000000);
5738 target = bfd_get_32 (input_bfd, contents + rel->r_offset)
5739 & (howto->dst_mask);
5740 addr = (target << 2) + pc;
5741 if (other == STO_MIPS16)
5742 addr &= ~ (bfd_vma) 1;
5743 if ((addr & 3) != 0
5744 || ((addr & 0xf0000000)
5745 != pc))
5746 r = bfd_reloc_overflow;
5747 }
5748
5749 /* Don't bother to report a relocation overflow for a call
5750 to a weak undefined symbol with a value of zero. This
5751 permits us to use
5752 if (!f) f();
5753 even if we aren't in range to call address zero. */
5754 if (r == bfd_reloc_overflow
5755 && (r_type == R_MIPS16_26 || r_type == R_MIPS_26)
5756 && relocation + addend == 0
5757 && h != NULL
5758 && h->root.type == bfd_link_hash_undefweak)
5759 r = bfd_reloc_ok;
5760
5761 /* If we've already issued an error for an undefined symbol,
5762 don't issue another useless error. */
5763 if (undefined_error
5764 && (r == bfd_reloc_undefined || r == bfd_reloc_overflow))
5765 r = bfd_reloc_ok;
5766
a94a7c1c 5767 if (SGI_COMPAT (output_bfd)
252b5132
RH
5768 && scpt != NULL
5769 && (input_section->flags & SEC_ALLOC) != 0)
5770 {
5771 Elf32_crinfo cptrel;
5772 bfd_byte *cr;
5773
5774 /* Make an entry of compact relocation info. */
5775 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5776 cptrel.vaddr = (rel->r_offset
5777 + input_section->output_section->vma
5778 + input_section->output_offset);
5779
5780 switch (r_type)
5781 {
5782 case R_MIPS_26:
5783 mips_elf_set_cr_type (cptrel, CRT_MIPS_JMPAD);
5784 /* XXX How should we set dist2to in this case. */
5785 mips_elf_set_cr_dist2to (cptrel, 8);
5786 cptrel.konst = addend + relocation;
5787 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5788 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5789 ((Elf32_External_crinfo *) cr
5790 + scpt->reloc_count));
5791 ++scpt->reloc_count;
5792 break;
5793
5794 case R_MIPS_GPREL16:
5795 case R_MIPS_LITERAL:
5796 case R_MIPS_GPREL32:
5797 mips_elf_set_cr_type (cptrel, CRT_MIPS_GPHI_LO);
5798 cptrel.konst = gp - cptrel.vaddr;
5799 mips_elf_set_cr_dist2to (cptrel, 4);
5800 cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5801 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5802 ((Elf32_External_crinfo *) cr
5803 + scpt->reloc_count));
5804 ++scpt->reloc_count;
5805 break;
5806
5807 default:
5808 break;
5809 }
5810 }
5811 }
5812
5813 if (r != bfd_reloc_ok)
5814 {
5815 switch (r)
5816 {
5817 default:
5818 case bfd_reloc_outofrange:
5819 abort ();
5820 case bfd_reloc_overflow:
5821 {
5822 const char *name;
5823
5824 if (h != NULL)
5825 name = h->root.root.string;
5826 else
5827 {
5828 name = bfd_elf_string_from_elf_section (input_bfd,
5829 symtab_hdr->sh_link,
5830 sym->st_name);
5831 if (name == NULL)
5832 return false;
5833 if (*name == '\0')
5834 name = bfd_section_name (input_bfd, sec);
5835 }
5836 if (! ((*info->callbacks->reloc_overflow)
5837 (info, name, howto->name, (bfd_vma) 0,
5838 input_bfd, input_section, rel->r_offset)))
5839 return false;
5840 }
5841 break;
5842 }
5843 }
5844 }
5845
5846 return true;
5847}
5848
5849/* This hook function is called before the linker writes out a global
5850 symbol. We mark symbols as small common if appropriate. This is
5851 also where we undo the increment of the value for a mips16 symbol. */
5852
5853/*ARGSIGNORED*/
5854static boolean
5855mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
5856 bfd *abfd;
5857 struct bfd_link_info *info;
5858 const char *name;
5859 Elf_Internal_Sym *sym;
5860 asection *input_sec;
5861{
5862 /* If we see a common symbol, which implies a relocatable link, then
5863 if a symbol was small common in an input file, mark it as small
5864 common in the output file. */
5865 if (sym->st_shndx == SHN_COMMON
5866 && strcmp (input_sec->name, ".scommon") == 0)
5867 sym->st_shndx = SHN_MIPS_SCOMMON;
5868
5869 if (sym->st_other == STO_MIPS16
5870 && (sym->st_value & 1) != 0)
5871 --sym->st_value;
5872
5873 return true;
5874}
5875\f
5876/* Functions for the dynamic linker. */
5877
5878/* The name of the dynamic interpreter. This is put in the .interp
5879 section. */
5880
303f629d
MM
5881#define ELF_DYNAMIC_INTERPRETER(abfd) \
5882 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" : "/usr/lib/libc.so.1")
252b5132
RH
5883
5884/* Create dynamic sections when linking against a dynamic object. */
5885
5886static boolean
5887mips_elf_create_dynamic_sections (abfd, info)
5888 bfd *abfd;
5889 struct bfd_link_info *info;
5890{
5891 struct elf_link_hash_entry *h;
5892 flagword flags;
5893 register asection *s;
5894 const char * const *namep;
5895
5896 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5897 | SEC_LINKER_CREATED | SEC_READONLY);
5898
5899 /* Mips ABI requests the .dynamic section to be read only. */
5900 s = bfd_get_section_by_name (abfd, ".dynamic");
5901 if (s != NULL)
5902 {
5903 if (! bfd_set_section_flags (abfd, s, flags))
5904 return false;
5905 }
5906
5907 /* We need to create .got section. */
5908 if (! mips_elf_create_got_section (abfd, info))
5909 return false;
5910
c6142e5d
MM
5911 /* Create the .msym section on IRIX6. It is used by the dynamic
5912 linker to speed up dynamic relocations, and to avoid computing
5913 the ELF hash for symbols. */
5914 if (IRIX_COMPAT (abfd) == ict_irix6
5915 && !mips_elf_create_msym_section (abfd))
5916 return false;
5917
252b5132
RH
5918 /* Create .stub section. */
5919 if (bfd_get_section_by_name (abfd, ".stub") == NULL)
5920 {
5921 s = bfd_make_section (abfd, ".stub");
5922 if (s == NULL
5923 || ! bfd_set_section_flags (abfd, s, flags)
5924 || ! bfd_set_section_alignment (abfd, s, 2))
5925 return false;
5926 }
5927
5928 if (SGI_COMPAT (abfd)
5929 && !info->shared
5930 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
5931 {
5932 s = bfd_make_section (abfd, ".rld_map");
5933 if (s == NULL
5934 || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
5935 || ! bfd_set_section_alignment (abfd, s, 2))
5936 return false;
5937 }
5938
303f629d
MM
5939 /* On IRIX5, we adjust add some additional symbols and change the
5940 alignments of several sections. There is no ABI documentation
5941 indicating that this is necessary on IRIX6, nor any evidence that
5942 the linker takes such action. */
5943 if (IRIX_COMPAT (abfd) == ict_irix5)
252b5132
RH
5944 {
5945 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
5946 {
5947 h = NULL;
5948 if (! (_bfd_generic_link_add_one_symbol
5949 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
5950 (bfd_vma) 0, (const char *) NULL, false,
5951 get_elf_backend_data (abfd)->collect,
5952 (struct bfd_link_hash_entry **) &h)))
5953 return false;
5954 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5955 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5956 h->type = STT_SECTION;
5957
5958 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5959 return false;
5960 }
5961
5962 /* We need to create a .compact_rel section. */
5963 if (! mips_elf_create_compact_rel_section (abfd, info))
5964 return false;
5965
5966 /* Change aligments of some sections. */
5967 s = bfd_get_section_by_name (abfd, ".hash");
5968 if (s != NULL)
5969 bfd_set_section_alignment (abfd, s, 4);
5970 s = bfd_get_section_by_name (abfd, ".dynsym");
5971 if (s != NULL)
5972 bfd_set_section_alignment (abfd, s, 4);
5973 s = bfd_get_section_by_name (abfd, ".dynstr");
5974 if (s != NULL)
5975 bfd_set_section_alignment (abfd, s, 4);
5976 s = bfd_get_section_by_name (abfd, ".reginfo");
5977 if (s != NULL)
5978 bfd_set_section_alignment (abfd, s, 4);
5979 s = bfd_get_section_by_name (abfd, ".dynamic");
5980 if (s != NULL)
5981 bfd_set_section_alignment (abfd, s, 4);
5982 }
5983
5984 if (!info->shared)
5985 {
5986 h = NULL;
5987 if (! (_bfd_generic_link_add_one_symbol
5988 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
5989 (bfd_vma) 0, (const char *) NULL, false,
5990 get_elf_backend_data (abfd)->collect,
5991 (struct bfd_link_hash_entry **) &h)))
5992 return false;
5993 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5994 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5995 h->type = STT_SECTION;
5996
5997 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5998 return false;
5999
6000 if (! mips_elf_hash_table (info)->use_rld_obj_head)
6001 {
6002 /* __rld_map is a four byte word located in the .data section
6003 and is filled in by the rtld to contain a pointer to
6004 the _r_debug structure. Its symbol value will be set in
6005 mips_elf_finish_dynamic_symbol. */
6006 s = bfd_get_section_by_name (abfd, ".rld_map");
6007 BFD_ASSERT (s != NULL);
6008
6009 h = NULL;
6010 if (! (_bfd_generic_link_add_one_symbol
6011 (info, abfd, "__rld_map", BSF_GLOBAL, s,
6012 (bfd_vma) 0, (const char *) NULL, false,
6013 get_elf_backend_data (abfd)->collect,
6014 (struct bfd_link_hash_entry **) &h)))
6015 return false;
6016 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6017 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6018 h->type = STT_OBJECT;
6019
6020 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6021 return false;
6022 }
6023 }
6024
6025 return true;
6026}
6027
6028/* Create the .compact_rel section. */
6029
6030static boolean
6031mips_elf_create_compact_rel_section (abfd, info)
6032 bfd *abfd;
6033 struct bfd_link_info *info;
6034{
6035 flagword flags;
6036 register asection *s;
6037
6038 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
6039 {
6040 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
6041 | SEC_READONLY);
6042
6043 s = bfd_make_section (abfd, ".compact_rel");
6044 if (s == NULL
6045 || ! bfd_set_section_flags (abfd, s, flags)
6046 || ! bfd_set_section_alignment (abfd, s, 2))
6047 return false;
6048
6049 s->_raw_size = sizeof (Elf32_External_compact_rel);
6050 }
6051
6052 return true;
6053}
6054
6055/* Create the .got section to hold the global offset table. */
6056
6057static boolean
6058mips_elf_create_got_section (abfd, info)
6059 bfd *abfd;
6060 struct bfd_link_info *info;
6061{
6062 flagword flags;
6063 register asection *s;
6064 struct elf_link_hash_entry *h;
6065 struct mips_got_info *g;
6066
6067 /* This function may be called more than once. */
6068 if (bfd_get_section_by_name (abfd, ".got") != NULL)
6069 return true;
6070
6071 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6072 | SEC_LINKER_CREATED);
6073
6074 s = bfd_make_section (abfd, ".got");
6075 if (s == NULL
6076 || ! bfd_set_section_flags (abfd, s, flags)
6077 || ! bfd_set_section_alignment (abfd, s, 4))
6078 return false;
6079
6080 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
6081 linker script because we don't want to define the symbol if we
6082 are not creating a global offset table. */
6083 h = NULL;
6084 if (! (_bfd_generic_link_add_one_symbol
6085 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
6086 (bfd_vma) 0, (const char *) NULL, false,
6087 get_elf_backend_data (abfd)->collect,
6088 (struct bfd_link_hash_entry **) &h)))
6089 return false;
6090 h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
6091 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
6092 h->type = STT_OBJECT;
6093
6094 if (info->shared
6095 && ! bfd_elf32_link_record_dynamic_symbol (info, h))
6096 return false;
6097
6098 /* The first several global offset table entries are reserved. */
6099 s->_raw_size = MIPS_RESERVED_GOTNO * 4;
6100
6101 g = (struct mips_got_info *) bfd_alloc (abfd,
6102 sizeof (struct mips_got_info));
6103 if (g == NULL)
6104 return false;
6105 g->global_gotsym = 0;
6106 g->local_gotno = MIPS_RESERVED_GOTNO;
6107 g->assigned_gotno = MIPS_RESERVED_GOTNO;
6108 if (elf_section_data (s) == NULL)
6109 {
6110 s->used_by_bfd =
6111 (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
6112 if (elf_section_data (s) == NULL)
6113 return false;
6114 }
6115 elf_section_data (s)->tdata = (PTR) g;
6116
6117 return true;
6118}
6119
c6142e5d
MM
6120/* Returns the .msym section for ABFD, creating it if it does not
6121 already exist. Returns NULL to indicate error. */
6122
6123static asection *
6124mips_elf_create_msym_section (abfd)
6125 bfd *abfd;
6126{
6127 asection *s;
6128
6129 s = bfd_get_section_by_name (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
6130 if (!s)
6131 {
6132 s = bfd_make_section (abfd, MIPS_ELF_MSYM_SECTION_NAME (abfd));
6133 if (!s
6134 || !bfd_set_section_flags (abfd, s,
6135 SEC_ALLOC
6136 | SEC_LOAD
6137 | SEC_HAS_CONTENTS
6138 | SEC_LINKER_CREATED
6139 | SEC_READONLY)
6140 || !bfd_set_section_alignment (abfd, s, 2))
6141 return NULL;
6142 }
6143
6144 return s;
6145}
6146
252b5132
RH
6147/* Look through the relocs for a section during the first phase, and
6148 allocate space in the global offset table. */
6149
6150static boolean
6151mips_elf_check_relocs (abfd, info, sec, relocs)
6152 bfd *abfd;
6153 struct bfd_link_info *info;
6154 asection *sec;
6155 const Elf_Internal_Rela *relocs;
6156{
6157 const char *name;
6158 bfd *dynobj;
6159 Elf_Internal_Shdr *symtab_hdr;
6160 struct elf_link_hash_entry **sym_hashes;
6161 struct mips_got_info *g;
6162 size_t extsymoff;
6163 const Elf_Internal_Rela *rel;
6164 const Elf_Internal_Rela *rel_end;
6165 asection *sgot;
6166 asection *sreloc;
6167
6168 if (info->relocateable)
6169 return true;
6170
6171 dynobj = elf_hash_table (info)->dynobj;
6172 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6173 sym_hashes = elf_sym_hashes (abfd);
6174 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
6175
6176 /* Check for the mips16 stub sections. */
6177
6178 name = bfd_get_section_name (abfd, sec);
6179 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
6180 {
6181 unsigned long r_symndx;
6182
6183 /* Look at the relocation information to figure out which symbol
6184 this is for. */
6185
6186 r_symndx = ELF32_R_SYM (relocs->r_info);
6187
6188 if (r_symndx < extsymoff
6189 || sym_hashes[r_symndx - extsymoff] == NULL)
6190 {
6191 asection *o;
6192
6193 /* This stub is for a local symbol. This stub will only be
6194 needed if there is some relocation in this BFD, other
6195 than a 16 bit function call, which refers to this symbol. */
6196 for (o = abfd->sections; o != NULL; o = o->next)
6197 {
6198 Elf_Internal_Rela *sec_relocs;
6199 const Elf_Internal_Rela *r, *rend;
6200
6201 /* We can ignore stub sections when looking for relocs. */
6202 if ((o->flags & SEC_RELOC) == 0
6203 || o->reloc_count == 0
6204 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
6205 sizeof FN_STUB - 1) == 0
6206 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
6207 sizeof CALL_STUB - 1) == 0
6208 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
6209 sizeof CALL_FP_STUB - 1) == 0)
6210 continue;
6211
6212 sec_relocs = (_bfd_elf32_link_read_relocs
6213 (abfd, o, (PTR) NULL,
6214 (Elf_Internal_Rela *) NULL,
6215 info->keep_memory));
6216 if (sec_relocs == NULL)
6217 return false;
6218
6219 rend = sec_relocs + o->reloc_count;
6220 for (r = sec_relocs; r < rend; r++)
6221 if (ELF32_R_SYM (r->r_info) == r_symndx
6222 && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
6223 break;
6224
6225 if (! info->keep_memory)
6226 free (sec_relocs);
6227
6228 if (r < rend)
6229 break;
6230 }
6231
6232 if (o == NULL)
6233 {
6234 /* There is no non-call reloc for this stub, so we do
6235 not need it. Since this function is called before
6236 the linker maps input sections to output sections, we
6237 can easily discard it by setting the SEC_EXCLUDE
6238 flag. */
6239 sec->flags |= SEC_EXCLUDE;
6240 return true;
6241 }
6242
6243 /* Record this stub in an array of local symbol stubs for
6244 this BFD. */
6245 if (elf_tdata (abfd)->local_stubs == NULL)
6246 {
6247 unsigned long symcount;
6248 asection **n;
6249
6250 if (elf_bad_symtab (abfd))
6251 symcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
6252 else
6253 symcount = symtab_hdr->sh_info;
6254 n = (asection **) bfd_zalloc (abfd,
6255 symcount * sizeof (asection *));
6256 if (n == NULL)
6257 return false;
6258 elf_tdata (abfd)->local_stubs = n;
6259 }
6260
6261 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
6262
6263 /* We don't need to set mips16_stubs_seen in this case.
6264 That flag is used to see whether we need to look through
6265 the global symbol table for stubs. We don't need to set
6266 it here, because we just have a local stub. */
6267 }
6268 else
6269 {
6270 struct mips_elf_link_hash_entry *h;
6271
6272 h = ((struct mips_elf_link_hash_entry *)
6273 sym_hashes[r_symndx - extsymoff]);
6274
6275 /* H is the symbol this stub is for. */
6276
6277 h->fn_stub = sec;
6278 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6279 }
6280 }
6281 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
6282 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6283 {
6284 unsigned long r_symndx;
6285 struct mips_elf_link_hash_entry *h;
6286 asection **loc;
6287
6288 /* Look at the relocation information to figure out which symbol
6289 this is for. */
6290
6291 r_symndx = ELF32_R_SYM (relocs->r_info);
6292
6293 if (r_symndx < extsymoff
6294 || sym_hashes[r_symndx - extsymoff] == NULL)
6295 {
6296 /* This stub was actually built for a static symbol defined
6297 in the same file. We assume that all static symbols in
6298 mips16 code are themselves mips16, so we can simply
6299 discard this stub. Since this function is called before
6300 the linker maps input sections to output sections, we can
6301 easily discard it by setting the SEC_EXCLUDE flag. */
6302 sec->flags |= SEC_EXCLUDE;
6303 return true;
6304 }
6305
6306 h = ((struct mips_elf_link_hash_entry *)
6307 sym_hashes[r_symndx - extsymoff]);
6308
6309 /* H is the symbol this stub is for. */
6310
6311 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
6312 loc = &h->call_fp_stub;
6313 else
6314 loc = &h->call_stub;
6315
6316 /* If we already have an appropriate stub for this function, we
6317 don't need another one, so we can discard this one. Since
6318 this function is called before the linker maps input sections
6319 to output sections, we can easily discard it by setting the
6320 SEC_EXCLUDE flag. We can also discard this section if we
6321 happen to already know that this is a mips16 function; it is
6322 not necessary to check this here, as it is checked later, but
6323 it is slightly faster to check now. */
6324 if (*loc != NULL || h->root.other == STO_MIPS16)
6325 {
6326 sec->flags |= SEC_EXCLUDE;
6327 return true;
6328 }
6329
6330 *loc = sec;
6331 mips_elf_hash_table (info)->mips16_stubs_seen = true;
6332 }
6333
6334 if (dynobj == NULL)
6335 {
6336 sgot = NULL;
6337 g = NULL;
6338 }
6339 else
6340 {
6341 sgot = bfd_get_section_by_name (dynobj, ".got");
6342 if (sgot == NULL)
6343 g = NULL;
6344 else
6345 {
6346 BFD_ASSERT (elf_section_data (sgot) != NULL);
6347 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6348 BFD_ASSERT (g != NULL);
6349 }
6350 }
6351
6352 sreloc = NULL;
6353
6354 rel_end = relocs + sec->reloc_count;
6355 for (rel = relocs; rel < rel_end; rel++)
6356 {
6357 unsigned long r_symndx;
6358 struct elf_link_hash_entry *h;
6359
6360 r_symndx = ELF32_R_SYM (rel->r_info);
6361
6362 if (r_symndx < extsymoff)
6363 h = NULL;
6364 else
6365 {
6366 h = sym_hashes[r_symndx - extsymoff];
6367
6368 /* This may be an indirect symbol created because of a version. */
6369 if (h != NULL)
6370 {
6371 while (h->root.type == bfd_link_hash_indirect)
6372 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6373 }
6374 }
6375
6376 /* Some relocs require a global offset table. */
6377 if (dynobj == NULL || sgot == NULL)
6378 {
6379 switch (ELF32_R_TYPE (rel->r_info))
6380 {
6381 case R_MIPS_GOT16:
6382 case R_MIPS_CALL16:
6383 case R_MIPS_CALL_HI16:
6384 case R_MIPS_CALL_LO16:
6385 case R_MIPS_GOT_HI16:
6386 case R_MIPS_GOT_LO16:
6387 if (dynobj == NULL)
6388 elf_hash_table (info)->dynobj = dynobj = abfd;
6389 if (! mips_elf_create_got_section (dynobj, info))
6390 return false;
6391 sgot = bfd_get_section_by_name (dynobj, ".got");
6392 BFD_ASSERT (sgot != NULL);
6393 BFD_ASSERT (elf_section_data (sgot) != NULL);
6394 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6395 BFD_ASSERT (g != NULL);
6396 break;
6397
6398 case R_MIPS_32:
6399 case R_MIPS_REL32:
6400 if (dynobj == NULL
6401 && (info->shared || h != NULL)
6402 && (sec->flags & SEC_ALLOC) != 0)
6403 elf_hash_table (info)->dynobj = dynobj = abfd;
6404 break;
6405
6406 default:
6407 break;
6408 }
6409 }
6410
6411 switch (ELF32_R_TYPE (rel->r_info))
6412 {
6413 case R_MIPS_CALL16:
6414 case R_MIPS_CALL_HI16:
6415 case R_MIPS_CALL_LO16:
6416 /* This symbol requires a global offset table entry. */
6417
6418 if (h == NULL)
6419 {
6420 (*_bfd_error_handler)
6421 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
6422 bfd_get_filename (abfd), (unsigned long) rel->r_offset);
6423 bfd_set_error (bfd_error_bad_value);
6424 return false;
6425 }
6426
6427 /* Make sure this symbol is output as a dynamic symbol. */
6428 if (h->dynindx == -1)
6429 {
6430 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6431 return false;
6432 }
6433
6434 if (h->got.offset != (bfd_vma) -1)
6435 {
6436 /* We have already allocated space in the .got. */
6437 break;
6438 }
6439
6440 /* Note the index of the first global got symbol in .dynsym. */
6441 if (g->global_gotsym == 0
6442 || g->global_gotsym > (unsigned long) h->dynindx)
6443 g->global_gotsym = h->dynindx;
6444
6445 /* Make this symbol to have the corresponding got entry. */
6446 h->got.offset = 0;
6447
6448 /* We need a stub, not a plt entry for the undefined
6449 function. But we record it as if it needs plt. See
6450 elf_adjust_dynamic_symbol in elflink.h. */
6451 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
6452 h->type = STT_FUNC;
6453
6454 break;
6455
6456 case R_MIPS_GOT16:
6457 case R_MIPS_GOT_HI16:
6458 case R_MIPS_GOT_LO16:
6459 /* This symbol requires a global offset table entry. */
6460
6461 if (h != NULL)
6462 {
6463 /* Make sure this symbol is output as a dynamic symbol. */
6464 if (h->dynindx == -1)
6465 {
6466 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
6467 return false;
6468 }
6469
6470 if (h->got.offset != (bfd_vma) -1)
6471 {
6472 /* We have already allocated space in the .got. */
6473 break;
6474 }
6475 /* Note the index of the first global got symbol in
6476 .dynsym. */
6477 if (g->global_gotsym == 0
6478 || g->global_gotsym > (unsigned long) h->dynindx)
6479 g->global_gotsym = h->dynindx;
6480
6481 /* Make this symbol to be the global got symbol. */
6482 h->got.offset = 0;
6483 }
6484
6485 break;
6486
6487 case R_MIPS_32:
6488 case R_MIPS_REL32:
6489 if ((info->shared || h != NULL)
6490 && (sec->flags & SEC_ALLOC) != 0)
6491 {
6492 if (sreloc == NULL)
6493 {
6494 const char *name = ".rel.dyn";
6495
6496 sreloc = bfd_get_section_by_name (dynobj, name);
6497 if (sreloc == NULL)
6498 {
6499 sreloc = bfd_make_section (dynobj, name);
6500 if (sreloc == NULL
6501 || ! bfd_set_section_flags (dynobj, sreloc,
6502 (SEC_ALLOC
6503 | SEC_LOAD
6504 | SEC_HAS_CONTENTS
6505 | SEC_IN_MEMORY
6506 | SEC_LINKER_CREATED
6507 | SEC_READONLY))
6508 || ! bfd_set_section_alignment (dynobj, sreloc,
6509 4))
6510 return false;
6511 }
6512 }
6513 if (info->shared)
6514 {
6515 /* When creating a shared object, we must copy these
6516 reloc types into the output file as R_MIPS_REL32
6517 relocs. We make room for this reloc in the
6518 .rel.dyn reloc section */
6519 if (sreloc->_raw_size == 0)
6520 {
6521 /* Add a null element. */
6522 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6523 ++sreloc->reloc_count;
6524 }
6525 sreloc->_raw_size += sizeof (Elf32_External_Rel);
6526 }
6527 else
6528 {
6529 struct mips_elf_link_hash_entry *hmips;
6530
6531 /* We only need to copy this reloc if the symbol is
6532 defined in a dynamic object. */
6533 hmips = (struct mips_elf_link_hash_entry *) h;
6534 ++hmips->mips_32_relocs;
6535 }
6536 }
6537
6538 if (SGI_COMPAT (abfd))
6539 mips_elf_hash_table (info)->compact_rel_size +=
6540 sizeof (Elf32_External_crinfo);
6541
6542 break;
6543
6544 case R_MIPS_26:
6545 case R_MIPS_GPREL16:
6546 case R_MIPS_LITERAL:
6547 case R_MIPS_GPREL32:
6548 if (SGI_COMPAT (abfd))
6549 mips_elf_hash_table (info)->compact_rel_size +=
6550 sizeof (Elf32_External_crinfo);
6551 break;
6552
6553 /* This relocation describes the C++ object vtable hierarchy.
6554 Reconstruct it for later use during GC. */
6555 case R_MIPS_GNU_VTINHERIT:
6556 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6557 return false;
6558 break;
6559
6560 /* This relocation describes which C++ vtable entries are actually
6561 used. Record for later use during GC. */
6562 case R_MIPS_GNU_VTENTRY:
6563 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6564 return false;
6565 break;
6566
6567 default:
6568 break;
6569 }
6570
6571 /* If this reloc is not a 16 bit call, and it has a global
6572 symbol, then we will need the fn_stub if there is one.
6573 References from a stub section do not count. */
6574 if (h != NULL
6575 && ELF32_R_TYPE (rel->r_info) != R_MIPS16_26
6576 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
6577 sizeof FN_STUB - 1) != 0
6578 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
6579 sizeof CALL_STUB - 1) != 0
6580 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
6581 sizeof CALL_FP_STUB - 1) != 0)
6582 {
6583 struct mips_elf_link_hash_entry *mh;
6584
6585 mh = (struct mips_elf_link_hash_entry *) h;
6586 mh->need_fn_stub = true;
6587 }
6588 }
6589
6590 return true;
6591}
6592
6593/* Return the section that should be marked against GC for a given
6594 relocation. */
6595
6596static asection *
6597mips_elf_gc_mark_hook (abfd, info, rel, h, sym)
6598 bfd *abfd;
6599 struct bfd_link_info *info;
6600 Elf_Internal_Rela *rel;
6601 struct elf_link_hash_entry *h;
6602 Elf_Internal_Sym *sym;
6603{
6604 /* ??? Do mips16 stub sections need to be handled special? */
6605
6606 if (h != NULL)
6607 {
6608 switch (ELF32_R_TYPE (rel->r_info))
6609 {
6610 case R_MIPS_GNU_VTINHERIT:
6611 case R_MIPS_GNU_VTENTRY:
6612 break;
6613
6614 default:
6615 switch (h->root.type)
6616 {
6617 case bfd_link_hash_defined:
6618 case bfd_link_hash_defweak:
6619 return h->root.u.def.section;
6620
6621 case bfd_link_hash_common:
6622 return h->root.u.c.p->section;
6623
6624 default:
6625 break;
6626 }
6627 }
6628 }
6629 else
6630 {
6631 if (!(elf_bad_symtab (abfd)
6632 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
6633 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
6634 && sym->st_shndx != SHN_COMMON))
6635 {
6636 return bfd_section_from_elf_index (abfd, sym->st_shndx);
6637 }
6638 }
6639
6640 return NULL;
6641}
6642
6643/* Update the got entry reference counts for the section being removed. */
6644
6645static boolean
6646mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
6647 bfd *abfd;
6648 struct bfd_link_info *info;
6649 asection *sec;
6650 const Elf_Internal_Rela *relocs;
6651{
6652#if 0
6653 Elf_Internal_Shdr *symtab_hdr;
6654 struct elf_link_hash_entry **sym_hashes;
6655 bfd_signed_vma *local_got_refcounts;
6656 const Elf_Internal_Rela *rel, *relend;
6657 unsigned long r_symndx;
6658 struct elf_link_hash_entry *h;
6659
6660 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6661 sym_hashes = elf_sym_hashes (abfd);
6662 local_got_refcounts = elf_local_got_refcounts (abfd);
6663
6664 relend = relocs + sec->reloc_count;
6665 for (rel = relocs; rel < relend; rel++)
6666 switch (ELF32_R_TYPE (rel->r_info))
6667 {
6668 case R_MIPS_GOT16:
6669 case R_MIPS_CALL16:
6670 case R_MIPS_CALL_HI16:
6671 case R_MIPS_CALL_LO16:
6672 case R_MIPS_GOT_HI16:
6673 case R_MIPS_GOT_LO16:
6674 /* ??? It would seem that the existing MIPS code does no sort
6675 of reference counting or whatnot on its GOT and PLT entries,
6676 so it is not possible to garbage collect them at this time. */
6677 break;
6678
6679 default:
6680 break;
6681 }
6682#endif
6683
6684 return true;
6685}
6686
6687
6688/* Adjust a symbol defined by a dynamic object and referenced by a
6689 regular object. The current definition is in some section of the
6690 dynamic object, but we're not including those sections. We have to
6691 change the definition to something the rest of the link can
6692 understand. */
6693
6694static boolean
6695mips_elf_adjust_dynamic_symbol (info, h)
6696 struct bfd_link_info *info;
6697 struct elf_link_hash_entry *h;
6698{
6699 bfd *dynobj;
6700 struct mips_elf_link_hash_entry *hmips;
6701 asection *s;
6702
6703 dynobj = elf_hash_table (info)->dynobj;
6704
6705 /* Make sure we know what is going on here. */
6706 BFD_ASSERT (dynobj != NULL
6707 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
6708 || h->weakdef != NULL
6709 || ((h->elf_link_hash_flags
6710 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6711 && (h->elf_link_hash_flags
6712 & ELF_LINK_HASH_REF_REGULAR) != 0
6713 && (h->elf_link_hash_flags
6714 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
6715
6716 /* If this symbol is defined in a dynamic object, we need to copy
6717 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
6718 file. */
6719 hmips = (struct mips_elf_link_hash_entry *) h;
6720 if (! info->relocateable
6721 && hmips->mips_32_relocs != 0
6722 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6723 {
6724 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
6725 BFD_ASSERT (s != NULL);
6726
6727 if (s->_raw_size == 0)
6728 {
6729 /* Make room for a null element. */
6730 s->_raw_size += sizeof (Elf32_External_Rel);
6731 ++s->reloc_count;
6732 }
6733 s->_raw_size += hmips->mips_32_relocs * sizeof (Elf32_External_Rel);
6734 }
6735
6736 /* For a function, create a stub, if needed. */
6737 if (h->type == STT_FUNC
6738 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
6739 {
6740 if (! elf_hash_table (info)->dynamic_sections_created)
6741 return true;
6742
6743 /* If this symbol is not defined in a regular file, then set
6744 the symbol to the stub location. This is required to make
6745 function pointers compare as equal between the normal
6746 executable and the shared library. */
6747 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6748 {
6749 /* We need .stub section. */
303f629d
MM
6750 s = bfd_get_section_by_name (dynobj,
6751 MIPS_ELF_STUB_SECTION_NAME (dynobj));
252b5132
RH
6752 BFD_ASSERT (s != NULL);
6753
6754 h->root.u.def.section = s;
6755 h->root.u.def.value = s->_raw_size;
6756
6757 /* XXX Write this stub address somewhere. */
6758 h->plt.offset = s->_raw_size;
6759
6760 /* Make room for this stub code. */
6761 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6762
6763 /* The last half word of the stub will be filled with the index
6764 of this symbol in .dynsym section. */
6765 return true;
6766 }
6767 }
6768
6769 /* If this is a weak symbol, and there is a real definition, the
6770 processor independent code will have arranged for us to see the
6771 real definition first, and we can just use the same value. */
6772 if (h->weakdef != NULL)
6773 {
6774 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
6775 || h->weakdef->root.type == bfd_link_hash_defweak);
6776 h->root.u.def.section = h->weakdef->root.u.def.section;
6777 h->root.u.def.value = h->weakdef->root.u.def.value;
6778 return true;
6779 }
6780
6781 /* This is a reference to a symbol defined by a dynamic object which
6782 is not a function. */
6783
6784 return true;
6785}
6786
6787/* This function is called after all the input files have been read,
6788 and the input sections have been assigned to output sections. We
6789 check for any mips16 stub sections that we can discard. */
6790
6791static boolean mips_elf_check_mips16_stubs
6792 PARAMS ((struct mips_elf_link_hash_entry *, PTR));
6793
6794static boolean
6795mips_elf_always_size_sections (output_bfd, info)
6796 bfd *output_bfd;
6797 struct bfd_link_info *info;
6798{
6799 asection *ri;
6800
6801 /* The .reginfo section has a fixed size. */
6802 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
6803 if (ri != NULL)
6804 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
6805
6806 if (info->relocateable
6807 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
6808 return true;
6809
6810 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
6811 mips_elf_check_mips16_stubs,
6812 (PTR) NULL);
6813
6814 return true;
6815}
6816
6817/* Check the mips16 stubs for a particular symbol, and see if we can
6818 discard them. */
6819
6820/*ARGSUSED*/
6821static boolean
6822mips_elf_check_mips16_stubs (h, data)
6823 struct mips_elf_link_hash_entry *h;
6824 PTR data;
6825{
6826 if (h->fn_stub != NULL
6827 && ! h->need_fn_stub)
6828 {
6829 /* We don't need the fn_stub; the only references to this symbol
6830 are 16 bit calls. Clobber the size to 0 to prevent it from
6831 being included in the link. */
6832 h->fn_stub->_raw_size = 0;
6833 h->fn_stub->_cooked_size = 0;
6834 h->fn_stub->flags &= ~ SEC_RELOC;
6835 h->fn_stub->reloc_count = 0;
6836 h->fn_stub->flags |= SEC_EXCLUDE;
6837 }
6838
6839 if (h->call_stub != NULL
6840 && h->root.other == STO_MIPS16)
6841 {
6842 /* We don't need the call_stub; this is a 16 bit function, so
6843 calls from other 16 bit functions are OK. Clobber the size
6844 to 0 to prevent it from being included in the link. */
6845 h->call_stub->_raw_size = 0;
6846 h->call_stub->_cooked_size = 0;
6847 h->call_stub->flags &= ~ SEC_RELOC;
6848 h->call_stub->reloc_count = 0;
6849 h->call_stub->flags |= SEC_EXCLUDE;
6850 }
6851
6852 if (h->call_fp_stub != NULL
6853 && h->root.other == STO_MIPS16)
6854 {
6855 /* We don't need the call_stub; this is a 16 bit function, so
6856 calls from other 16 bit functions are OK. Clobber the size
6857 to 0 to prevent it from being included in the link. */
6858 h->call_fp_stub->_raw_size = 0;
6859 h->call_fp_stub->_cooked_size = 0;
6860 h->call_fp_stub->flags &= ~ SEC_RELOC;
6861 h->call_fp_stub->reloc_count = 0;
6862 h->call_fp_stub->flags |= SEC_EXCLUDE;
6863 }
6864
6865 return true;
6866}
6867
6868/* Set the sizes of the dynamic sections. */
6869
6870static boolean
6871mips_elf_size_dynamic_sections (output_bfd, info)
6872 bfd *output_bfd;
6873 struct bfd_link_info *info;
6874{
6875 bfd *dynobj;
6876 asection *s;
6877 boolean reltext;
6878 asection *sgot;
6879 struct mips_got_info *g;
6880
6881 dynobj = elf_hash_table (info)->dynobj;
6882 BFD_ASSERT (dynobj != NULL);
6883
6884 if (elf_hash_table (info)->dynamic_sections_created)
6885 {
6886 /* Set the contents of the .interp section to the interpreter. */
6887 if (! info->shared)
6888 {
6889 s = bfd_get_section_by_name (dynobj, ".interp");
6890 BFD_ASSERT (s != NULL);
303f629d
MM
6891 s->_raw_size
6892 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
6893 s->contents
6894 = (unsigned char *) ELF_DYNAMIC_INTERPRETER (output_bfd);
252b5132
RH
6895 }
6896 }
6897
6898 /* Recompute the size of .got for local entires (reserved and
6899 hipages) if needed. To estimate it, get the upper bound of total
6900 size of loadable sections. */
6901 sgot = bfd_get_section_by_name (dynobj, ".got");
6902
6903 if (sgot != NULL)
6904 {
6905 bfd_size_type loadable_size = 0;
6906 bfd_size_type local_gotno;
6907 struct _bfd *sub;
6908
6909 BFD_ASSERT (elf_section_data (sgot) != NULL);
6910 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6911 BFD_ASSERT (g != NULL);
6912
6913 for (sub = info->input_bfds; sub; sub = sub->link_next)
6914 for (s = sub->sections; s != NULL; s = s->next)
6915 {
6916 if ((s->flags & SEC_ALLOC) == 0)
6917 continue;
6918 loadable_size += (s->_raw_size + 0xf) & ~0xf;
6919 }
6920
6921 loadable_size += MIPS_FUNCTION_STUB_SIZE;
6922
6923 /* Assume there are two loadable segments consisting of
6924 contiguous sections. Is 5 enough? */
6925 local_gotno = (loadable_size >> 16) + 5 + MIPS_RESERVED_GOTNO;
6926 g->local_gotno = local_gotno;
6927 sgot->_raw_size += local_gotno * 4;
6928 }
6929
6930 /* The check_relocs and adjust_dynamic_symbol entry points have
6931 determined the sizes of the various dynamic sections. Allocate
6932 memory for them. */
6933 reltext = false;
6934 for (s = dynobj->sections; s != NULL; s = s->next)
6935 {
6936 const char *name;
6937 boolean strip;
6938
6939 /* It's OK to base decisions on the section name, because none
6940 of the dynobj section names depend upon the input files. */
6941 name = bfd_get_section_name (dynobj, s);
6942
6943 if ((s->flags & SEC_LINKER_CREATED) == 0)
6944 continue;
6945
6946 strip = false;
6947
6948 if (strncmp (name, ".rel", 4) == 0)
6949 {
6950 if (s->_raw_size == 0)
6951 {
6952 /* We only strip the section if the output section name
6953 has the same name. Otherwise, there might be several
6954 input sections for this output section. FIXME: This
6955 code is probably not needed these days anyhow, since
6956 the linker now does not create empty output sections. */
6957 if (s->output_section != NULL
6958 && strcmp (name,
6959 bfd_get_section_name (s->output_section->owner,
6960 s->output_section)) == 0)
6961 strip = true;
6962 }
6963 else
6964 {
6965 const char *outname;
6966 asection *target;
6967
6968 /* If this relocation section applies to a read only
6969 section, then we probably need a DT_TEXTREL entry.
6970 If the relocation section is .rel.dyn, we always
6971 assert a DT_TEXTREL entry rather than testing whether
6972 there exists a relocation to a read only section or
6973 not. */
6974 outname = bfd_get_section_name (output_bfd,
6975 s->output_section);
6976 target = bfd_get_section_by_name (output_bfd, outname + 4);
6977 if ((target != NULL
6978 && (target->flags & SEC_READONLY) != 0
6979 && (target->flags & SEC_ALLOC) != 0)
6980 || strcmp (outname, ".rel.dyn") == 0)
6981 reltext = true;
6982
6983 /* We use the reloc_count field as a counter if we need
6984 to copy relocs into the output file. */
6985 if (strcmp (name, ".rel.dyn") != 0)
6986 s->reloc_count = 0;
6987 }
6988 }
6989 else if (strncmp (name, ".got", 4) == 0)
6990 {
6991 int i;
6992
6993 BFD_ASSERT (elf_section_data (s) != NULL);
6994 g = (struct mips_got_info *) elf_section_data (s)->tdata;
6995 BFD_ASSERT (g != NULL);
6996
6997 /* Fix the size of .got section for the correspondence of
6998 global symbols and got entries. This adds some useless
6999 got entries. Is this required by ABI really? */
7000 i = elf_hash_table (info)->dynsymcount - g->global_gotsym;
7001 s->_raw_size += i * 4;
7002 }
303f629d 7003 else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
252b5132
RH
7004 {
7005 /* Irix rld assumes that the function stub isn't at the end
7006 of .text section. So put a dummy. XXX */
7007 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
7008 }
7009 else if (! info->shared
7010 && ! mips_elf_hash_table (info)->use_rld_obj_head
7011 && strncmp (name, ".rld_map", 8) == 0)
7012 {
7013 /* We add a room for __rld_map. It will be filled in by the
7014 rtld to contain a pointer to the _r_debug structure. */
7015 s->_raw_size += 4;
7016 }
7017 else if (SGI_COMPAT (output_bfd)
7018 && strncmp (name, ".compact_rel", 12) == 0)
7019 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
c6142e5d
MM
7020 else if (strcmp (name, MIPS_ELF_MSYM_SECTION_NAME (output_bfd))
7021 == 0)
7022 s->_raw_size = (sizeof (Elf32_External_Msym)
7023 * (elf_hash_table (info)->dynsymcount
7024 + bfd_count_sections (output_bfd)));
252b5132
RH
7025 else if (strncmp (name, ".init", 5) != 0)
7026 {
7027 /* It's not one of our sections, so don't allocate space. */
7028 continue;
7029 }
7030
7031 if (strip)
7032 {
7033 _bfd_strip_section_from_output (s);
7034 continue;
7035 }
7036
7037 /* Allocate memory for the section contents. */
303f629d 7038 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
252b5132
RH
7039 if (s->contents == NULL && s->_raw_size != 0)
7040 {
7041 bfd_set_error (bfd_error_no_memory);
7042 return false;
7043 }
252b5132
RH
7044 }
7045
7046 if (elf_hash_table (info)->dynamic_sections_created)
7047 {
7048 /* Add some entries to the .dynamic section. We fill in the
7049 values later, in elf_mips_finish_dynamic_sections, but we
7050 must add the entries now so that we get the correct size for
7051 the .dynamic section. The DT_DEBUG entry is filled in by the
7052 dynamic linker and used by the debugger. */
7053 if (! info->shared)
7054 {
7055 if (SGI_COMPAT (output_bfd))
7056 {
7057 /* SGI object has the equivalence of DT_DEBUG in the
7058 DT_MIPS_RLD_MAP entry. */
7059 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_MAP, 0))
7060 return false;
7061 }
7062 else
7063 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
7064 return false;
7065 }
7066
7067 if (reltext)
7068 {
7069 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
7070 return false;
7071 }
7072
7073 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
7074 return false;
7075
7076 if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
7077 {
7078 if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0))
7079 return false;
7080
7081 if (! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0))
7082 return false;
7083
7084 if (! bfd_elf32_add_dynamic_entry (info, DT_RELENT, 0))
7085 return false;
7086 }
7087
7088 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICTNO, 0))
7089 return false;
7090
7091 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLISTNO, 0))
7092 return false;
7093
7094 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
7095 {
7096 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICT, 0))
7097 return false;
7098
7099 s = bfd_get_section_by_name (dynobj, ".liblist");
7100 BFD_ASSERT (s != NULL);
7101
7102 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLIST, 0))
7103 return false;
7104 }
7105
7106 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_VERSION, 0))
7107 return false;
7108
7109 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_FLAGS, 0))
7110 return false;
7111
7112#if 0
7113 /* Time stamps in executable files are a bad idea. */
7114 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_TIME_STAMP, 0))
7115 return false;
7116#endif
7117
7118#if 0 /* FIXME */
7119 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_ICHECKSUM, 0))
7120 return false;
7121#endif
7122
7123#if 0 /* FIXME */
7124 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_IVERSION, 0))
7125 return false;
7126#endif
7127
7128 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_BASE_ADDRESS, 0))
7129 return false;
7130
7131 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LOCAL_GOTNO, 0))
7132 return false;
7133
7134 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_SYMTABNO, 0))
7135 return false;
7136
7137 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_UNREFEXTNO, 0))
7138 return false;
7139
7140 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_GOTSYM, 0))
7141 return false;
7142
7143 if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_HIPAGENO, 0))
7144 return false;
7145
7146#if 0 /* (SGI_COMPAT) */
7147 if (! bfd_get_section_by_name (dynobj, ".init"))
7148 if (! bfd_elf32_add_dynamic_entry (info, DT_INIT, 0))
7149 return false;
7150
7151 if (! bfd_get_section_by_name (dynobj, ".fini"))
7152 if (! bfd_elf32_add_dynamic_entry (info, DT_FINI, 0))
7153 return false;
7154#endif
c6142e5d
MM
7155
7156 if (bfd_get_section_by_name (dynobj,
7157 MIPS_ELF_MSYM_SECTION_NAME (dynobj))
7158 && !bfd_elf32_add_dynamic_entry (info, DT_MIPS_MSYM, 0))
7159 return false;
252b5132
RH
7160 }
7161
7162 /* If we use dynamic linking, we generate a section symbol for each
7163 output section. These are local symbols, which means that they
7164 must come first in the dynamic symbol table.
7165 That means we must increment the dynamic symbol index of every
7166 other dynamic symbol. */
7167 {
7168 unsigned int c, i;
7169 struct mips_got_info *g;
7170
7171 c = 0;
7172 if (elf_hash_table (info)->dynamic_sections_created)
7173 {
7174#if 0
7175 /* We no longer try to restrict the set of sections which get
7176 dynamic symbol table entries, since it fails if we have
7177 other random sections which need dynamic relocations. */
7178 const char * const *namep;
7179 bfd_size_type strindex;
7180 struct bfd_strtab_hash *dynstr;
7181
7182 if (SGI_COMPAT (output_bfd))
7183 {
7184 c = SIZEOF_MIPS_DYNSYM_SECNAMES - 1;
7185 elf_link_hash_traverse (elf_hash_table (info),
7186 mips_elf_adjust_dynindx,
7187 (PTR) &c);
7188 elf_hash_table (info)->dynsymcount += c;
7189
7190 dynstr = elf_hash_table (info)->dynstr;
7191 BFD_ASSERT (dynstr != NULL);
7192
7193 for (i = 1, namep = mips_elf_dynsym_sec_names;
7194 *namep != NULL;
7195 i++, namep++)
7196 {
7197 s = bfd_get_section_by_name (output_bfd, *namep);
7198 if (s != NULL)
7199 elf_section_data (s)->dynindx = i;
7200
7201 strindex = _bfd_stringtab_add (dynstr, *namep, true, false);
7202 if (strindex == (bfd_size_type) -1)
7203 return false;
7204
7205 mips_elf_hash_table (info)->dynsym_sec_strindex[i] = strindex;
7206 }
7207 }
7208 else
7209#endif /* 0 */
7210 {
7211 c = bfd_count_sections (output_bfd);
7212 elf_link_hash_traverse (elf_hash_table (info),
9886bd43 7213 _bfd_elf_link_adjust_dynindx,
252b5132
RH
7214 (PTR) &c);
7215 elf_hash_table (info)->dynsymcount += c;
7216
7217 for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
7218 {
7219 elf_section_data (s)->dynindx = i;
7220 /* These symbols will have no names, so we don't need to
7221 fiddle with dynstr_index. */
7222 }
7223 }
7224 }
7225
7226 if (sgot != NULL)
7227 {
7228 BFD_ASSERT (elf_section_data (sgot) != NULL);
7229 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7230 BFD_ASSERT (g != NULL);
7231
7232 /* If there are no global got symbols, fake the last symbol so
7233 for safety. */
7234 if (g->global_gotsym)
7235 g->global_gotsym += c;
7236 else
7237 g->global_gotsym = elf_hash_table (info)->dynsymcount - 1;
7238 }
7239 }
7240
7241 return true;
7242}
7243
252b5132
RH
7244/* Finish up dynamic symbol handling. We set the contents of various
7245 dynamic sections here. */
7246
7247static boolean
7248mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
7249 bfd *output_bfd;
7250 struct bfd_link_info *info;
7251 struct elf_link_hash_entry *h;
7252 Elf_Internal_Sym *sym;
7253{
7254 bfd *dynobj;
7255 bfd_vma gval;
7256 asection *sgot;
c6142e5d 7257 asection *smsym;
252b5132
RH
7258 struct mips_got_info *g;
7259 const char *name;
c6142e5d 7260 struct mips_elf_link_hash_entry *mh;
252b5132
RH
7261
7262 dynobj = elf_hash_table (info)->dynobj;
7263 gval = sym->st_value;
c6142e5d 7264 mh = (struct mips_elf_link_hash_entry *) h;
252b5132
RH
7265
7266 if (h->plt.offset != (bfd_vma) -1)
7267 {
7268 asection *s;
7269 bfd_byte *p;
7270 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
7271
7272 /* This symbol has a stub. Set it up. */
7273
7274 BFD_ASSERT (h->dynindx != -1);
7275
303f629d
MM
7276 s = bfd_get_section_by_name (dynobj,
7277 MIPS_ELF_STUB_SECTION_NAME (dynobj));
252b5132
RH
7278 BFD_ASSERT (s != NULL);
7279
7280 /* Fill the stub. */
7281 p = stub;
7282 bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
7283 p += 4;
7284 bfd_put_32 (output_bfd, STUB_MOVE, p);
7285 p += 4;
7286
7287 /* FIXME: Can h->dynindex be more than 64K? */
7288 if (h->dynindx & 0xffff0000)
7289 return false;
7290
7291 bfd_put_32 (output_bfd, STUB_JALR, p);
7292 p += 4;
7293 bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
7294
7295 BFD_ASSERT (h->plt.offset <= s->_raw_size);
7296 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
7297
7298 /* Mark the symbol as undefined. plt.offset != -1 occurs
7299 only for the referenced symbol. */
7300 sym->st_shndx = SHN_UNDEF;
7301
7302 /* The run-time linker uses the st_value field of the symbol
7303 to reset the global offset table entry for this external
7304 to its stub address when unlinking a shared object. */
7305 gval = s->output_section->vma + s->output_offset + h->plt.offset;
7306 sym->st_value = gval;
7307 }
7308
7309 BFD_ASSERT (h->dynindx != -1);
7310
7311 sgot = bfd_get_section_by_name (dynobj, ".got");
7312 BFD_ASSERT (sgot != NULL);
7313 BFD_ASSERT (elf_section_data (sgot) != NULL);
7314 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7315 BFD_ASSERT (g != NULL);
7316
7317 if ((unsigned long) h->dynindx >= g->global_gotsym)
7318 {
7319 bfd_size_type offset;
7320
7321 /* This symbol has an entry in the global offset table. Set its
7322 value to the corresponding got entry, if needed. */
7323 if (h->got.offset == (bfd_vma) -1)
7324 {
7325 offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
7326 BFD_ASSERT (g->local_gotno * 4 <= offset
7327 && offset < sgot->_raw_size);
7328 bfd_put_32 (output_bfd, gval, sgot->contents + offset);
7329 }
7330 }
7331
c6142e5d
MM
7332 /* Create a .msym entry, if appropriate. */
7333 smsym = bfd_get_section_by_name (dynobj,
7334 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
7335 if (smsym)
7336 {
7337 Elf32_Internal_Msym msym;
7338
7339 msym.ms_hash_value = bfd_elf_hash (h->root.root.string);
7340 /* It is undocumented what the `1' indicates, but IRIX6 uses
7341 this value. */
7342 msym.ms_info = ELF32_MS_INFO (mh->min_dyn_reloc_index, 1);
7343 bfd_mips_elf_swap_msym_out
7344 (dynobj, &msym,
7345 ((Elf32_External_Msym *) smsym->contents) + h->dynindx);
7346 }
7347
252b5132
RH
7348 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
7349 name = h->root.root.string;
7350 if (strcmp (name, "_DYNAMIC") == 0
7351 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
7352 sym->st_shndx = SHN_ABS;
7353 else if (strcmp (name, "_DYNAMIC_LINK") == 0)
7354 {
7355 sym->st_shndx = SHN_ABS;
7356 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7357 sym->st_value = 1;
7358 }
7359 else if (SGI_COMPAT (output_bfd))
7360 {
7361 if (strcmp (name, "_gp_disp") == 0)
7362 {
7363 sym->st_shndx = SHN_ABS;
7364 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7365 sym->st_value = elf_gp (output_bfd);
7366 }
7367 else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
7368 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
7369 {
7370 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7371 sym->st_other = STO_PROTECTED;
7372 sym->st_value = 0;
7373 sym->st_shndx = SHN_MIPS_DATA;
7374 }
7375 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
7376 {
7377 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7378 sym->st_other = STO_PROTECTED;
7379 sym->st_value = mips_elf_hash_table (info)->procedure_count;
7380 sym->st_shndx = SHN_ABS;
7381 }
7382 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
7383 {
7384 if (h->type == STT_FUNC)
7385 sym->st_shndx = SHN_MIPS_TEXT;
7386 else if (h->type == STT_OBJECT)
7387 sym->st_shndx = SHN_MIPS_DATA;
7388 }
7389 }
7390
7391 if (SGI_COMPAT (output_bfd)
7392 && ! info->shared)
7393 {
7394 if (! mips_elf_hash_table (info)->use_rld_obj_head
7395 && strcmp (name, "__rld_map") == 0)
7396 {
7397 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
7398 BFD_ASSERT (s != NULL);
7399 sym->st_value = s->output_section->vma + s->output_offset;
7400 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
7401 if (mips_elf_hash_table (info)->rld_value == 0)
7402 mips_elf_hash_table (info)->rld_value = sym->st_value;
7403 }
7404 else if (mips_elf_hash_table (info)->use_rld_obj_head
7405 && strcmp (name, "__rld_obj_head") == 0)
7406 {
303f629d
MM
7407 /* IRIX6 does not use a .rld_map section. */
7408 if (IRIX_COMPAT (output_bfd) == ict_irix5)
7409 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
7410 != NULL);
252b5132
RH
7411 mips_elf_hash_table (info)->rld_value = sym->st_value;
7412 }
7413 }
7414
7415 /* If this is a mips16 symbol, force the value to be even. */
7416 if (sym->st_other == STO_MIPS16
7417 && (sym->st_value & 1) != 0)
7418 --sym->st_value;
7419
7420 return true;
7421}
7422
7423/* Finish up the dynamic sections. */
7424
7425static boolean
7426mips_elf_finish_dynamic_sections (output_bfd, info)
7427 bfd *output_bfd;
7428 struct bfd_link_info *info;
7429{
7430 bfd *dynobj;
7431 asection *sdyn;
7432 asection *sgot;
7433 struct mips_got_info *g;
7434
7435 dynobj = elf_hash_table (info)->dynobj;
7436
7437 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
7438
7439 sgot = bfd_get_section_by_name (dynobj, ".got");
7440 if (sgot == NULL)
7441 g = NULL;
7442 else
7443 {
7444 BFD_ASSERT (elf_section_data (sgot) != NULL);
7445 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
7446 BFD_ASSERT (g != NULL);
7447 }
7448
7449 if (elf_hash_table (info)->dynamic_sections_created)
7450 {
7451 Elf32_External_Dyn *dyncon, *dynconend;
7452
7453 BFD_ASSERT (sdyn != NULL);
7454 BFD_ASSERT (g != NULL);
7455
7456 dyncon = (Elf32_External_Dyn *) sdyn->contents;
7457 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
7458 for (; dyncon < dynconend; dyncon++)
7459 {
7460 Elf_Internal_Dyn dyn;
7461 const char *name;
7462 size_t elemsize;
7463 asection *s;
7464
7465 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
7466
7467 switch (dyn.d_tag)
7468 {
7469 default:
7470 break;
7471
7472 case DT_RELENT:
7473 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7474 BFD_ASSERT (s != NULL);
7475 dyn.d_un.d_val = sizeof (Elf32_External_Rel);
7476 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7477 break;
7478
7479 case DT_STRSZ:
7480 /* Rewrite DT_STRSZ. */
7481 dyn.d_un.d_val =
7482 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
7483 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7484 break;
7485
7486 case DT_PLTGOT:
7487 name = ".got";
7488 goto get_vma;
7489 case DT_MIPS_CONFLICT:
7490 name = ".conflict";
7491 goto get_vma;
7492 case DT_MIPS_LIBLIST:
7493 name = ".liblist";
7494 get_vma:
7495 s = bfd_get_section_by_name (output_bfd, name);
7496 BFD_ASSERT (s != NULL);
7497 dyn.d_un.d_ptr = s->vma;
7498 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7499 break;
7500
7501 case DT_MIPS_RLD_VERSION:
7502 dyn.d_un.d_val = 1; /* XXX */
7503 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7504 break;
7505
7506 case DT_MIPS_FLAGS:
7507 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
7508 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7509 break;
7510
7511 case DT_MIPS_CONFLICTNO:
7512 name = ".conflict";
7513 elemsize = sizeof (Elf32_Conflict);
7514 goto set_elemno;
7515
7516 case DT_MIPS_LIBLISTNO:
7517 name = ".liblist";
7518 elemsize = sizeof (Elf32_Lib);
7519 set_elemno:
7520 s = bfd_get_section_by_name (output_bfd, name);
7521 if (s != NULL)
7522 {
7523 if (s->_cooked_size != 0)
7524 dyn.d_un.d_val = s->_cooked_size / elemsize;
7525 else
7526 dyn.d_un.d_val = s->_raw_size / elemsize;
7527 }
7528 else
7529 dyn.d_un.d_val = 0;
7530
7531 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7532 break;
7533
7534 case DT_MIPS_TIME_STAMP:
7535 time ((time_t *) &dyn.d_un.d_val);
7536 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7537 break;
7538
7539 case DT_MIPS_ICHECKSUM:
7540 /* XXX FIXME: */
7541 break;
7542
7543 case DT_MIPS_IVERSION:
7544 /* XXX FIXME: */
7545 break;
7546
7547 case DT_MIPS_BASE_ADDRESS:
7548 s = output_bfd->sections;
7549 BFD_ASSERT (s != NULL);
7550 dyn.d_un.d_ptr = s->vma & ~(0xffff);
7551 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7552 break;
7553
7554 case DT_MIPS_LOCAL_GOTNO:
7555 dyn.d_un.d_val = g->local_gotno;
7556 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7557 break;
7558
7559 case DT_MIPS_SYMTABNO:
7560 name = ".dynsym";
7561 elemsize = sizeof (Elf32_External_Sym);
7562 s = bfd_get_section_by_name (output_bfd, name);
7563 BFD_ASSERT (s != NULL);
7564
7565 if (s->_cooked_size != 0)
7566 dyn.d_un.d_val = s->_cooked_size / elemsize;
7567 else
7568 dyn.d_un.d_val = s->_raw_size / elemsize;
7569 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7570 break;
7571
7572 case DT_MIPS_UNREFEXTNO:
7573#if 0
7574 dyn.d_un.d_val = SIZEOF_MIPS_DYNSYM_SECNAMES;
7575#else
7576 dyn.d_un.d_val = bfd_count_sections (output_bfd);
7577#endif
7578 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7579 break;
7580
7581 case DT_MIPS_GOTSYM:
7582 dyn.d_un.d_val = g->global_gotsym;
7583 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7584 break;
7585
7586 case DT_MIPS_HIPAGENO:
7587 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
7588 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7589 break;
7590
7591 case DT_MIPS_RLD_MAP:
7592 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
7593 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7594 break;
7595
c6142e5d
MM
7596 case DT_MIPS_MSYM:
7597 s = (bfd_get_section_by_name
7598 (output_bfd, MIPS_ELF_MSYM_SECTION_NAME (output_bfd)));
7599 dyn.d_un.d_ptr = s->vma;
7600 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7601 break;
252b5132
RH
7602 }
7603 }
7604 }
7605
7606 /* The first entry of the global offset table will be filled at
7607 runtime. The second entry will be used by some runtime loaders.
7608 This isn't the case of Irix rld. */
7609 if (sgot != NULL && sgot->_raw_size > 0)
7610 {
7611 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
7612 bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, sgot->contents + 4);
7613 }
7614
7615 if (sgot != NULL)
7616 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
7617
7618 {
7619 asection *sdynsym;
c6142e5d 7620 asection *smsym;
252b5132
RH
7621 asection *s;
7622 Elf_Internal_Sym sym;
7623 Elf32_compact_rel cpt;
7624
7625 /* Set up the section symbols for the output sections. SGI sets
7626 the STT_NOTYPE attribute for these symbols. Should we do so? */
7627
7628 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
c6142e5d
MM
7629 smsym = bfd_get_section_by_name (dynobj,
7630 MIPS_ELF_MSYM_SECTION_NAME (dynobj));
252b5132
RH
7631 if (sdynsym != NULL)
7632 {
7633#if 0
7634 const char *name;
7635 const char * const * namep = mips_elf_dynsym_sec_names;
7636 unsigned int i;
7637 bfd_vma last;
7638 long dindx;
7639
7640 /* We no longer try to restrict the set of sections which get
7641 dynamic symbol table entries, since it fails if we have
7642 other random sections which need dynamic relocations. */
7643 if (SGI_COMPAT (output_bfd))
7644 {
7645 sym.st_size = 0;
7646 sym.st_name = 0;
7647 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7648 sym.st_other = 0;
7649
7650 i = 0;
7651 last = 0;
7652 dindx = 0;
7653 while ((name = *namep++) != NULL)
7654 {
7655 s = bfd_get_section_by_name (output_bfd, name);
7656 if (s != NULL)
7657 {
7658 sym.st_value = s->vma;
7659 dindx = elf_section_data (s)->dynindx;
7660 last = s->vma + s->_raw_size;
7661 }
7662 else
7663 {
7664 sym.st_value = last;
7665 dindx++;
7666 }
7667
7668 sym.st_shndx = (i < MIPS_TEXT_DYNSYM_SECNO
7669 ? SHN_MIPS_TEXT
7670 : SHN_MIPS_DATA);
7671 ++i;
7672 sym.st_name =
7673 mips_elf_hash_table (info)->dynsym_sec_strindex[dindx];
7674
7675 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7676 (((Elf32_External_Sym *)
7677 sdynsym->contents)
7678 + dindx));
7679 }
7680
7681 /* Set the sh_info field of the output .dynsym section to
7682 the index of the first global symbol. */
7683 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7684 SIZEOF_MIPS_DYNSYM_SECNAMES;
7685 }
7686 else
7687#endif /* 0 */
7688 {
c6142e5d
MM
7689 Elf32_Internal_Msym msym;
7690
252b5132
RH
7691 sym.st_size = 0;
7692 sym.st_name = 0;
7693 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7694 sym.st_other = 0;
7695
c6142e5d
MM
7696 msym.ms_hash_value = 0;
7697 msym.ms_info = ELF32_MS_INFO (0, 1);
7698
252b5132
RH
7699 for (s = output_bfd->sections; s != NULL; s = s->next)
7700 {
7701 int indx;
c6142e5d 7702 long dynindx;
252b5132
RH
7703
7704 sym.st_value = s->vma;
7705
7706 indx = elf_section_data (s)->this_idx;
7707 BFD_ASSERT (indx > 0);
7708 sym.st_shndx = indx;
c6142e5d
MM
7709
7710 dynindx = elf_section_data (s)->dynindx;
7711
7712 bfd_elf32_swap_symbol_out
7713 (output_bfd, &sym,
7714 (((Elf32_External_Sym *) sdynsym->contents)
7715 + dynindx));
7716
7717 if (smsym)
7718 bfd_mips_elf_swap_msym_out
7719 (output_bfd, &msym,
7720 (((Elf32_External_Msym *) smsym->contents)
7721 + dynindx));
252b5132
RH
7722 }
7723
7724 /* Set the sh_info field of the output .dynsym section to
7725 the index of the first global symbol. */
7726 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7727 bfd_count_sections (output_bfd) + 1;
7728 }
7729 }
7730
7731 if (SGI_COMPAT (output_bfd))
7732 {
7733 /* Write .compact_rel section out. */
7734 s = bfd_get_section_by_name (dynobj, ".compact_rel");
7735 if (s != NULL)
7736 {
7737 cpt.id1 = 1;
7738 cpt.num = s->reloc_count;
7739 cpt.id2 = 2;
7740 cpt.offset = (s->output_section->filepos
7741 + sizeof (Elf32_External_compact_rel));
7742 cpt.reserved0 = 0;
7743 cpt.reserved1 = 0;
7744 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
7745 ((Elf32_External_compact_rel *)
7746 s->contents));
7747
7748 /* Clean up a dummy stub function entry in .text. */
303f629d
MM
7749 s = bfd_get_section_by_name (dynobj,
7750 MIPS_ELF_STUB_SECTION_NAME (dynobj));
252b5132
RH
7751 if (s != NULL)
7752 {
7753 file_ptr dummy_offset;
7754
7755 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
7756 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
7757 memset (s->contents + dummy_offset, 0,
7758 MIPS_FUNCTION_STUB_SIZE);
7759 }
7760 }
7761 }
7762
7763 /* Clean up a first relocation in .rel.dyn. */
7764 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7765 if (s != NULL && s->_raw_size > 0)
7766 memset (s->contents, 0, sizeof (Elf32_External_Rel));
7767 }
7768
7769 return true;
7770}
7771\f
7772/* This is almost identical to bfd_generic_get_... except that some
7773 MIPS relocations need to be handled specially. Sigh. */
7774
7775static bfd_byte *
7776elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
7777 relocateable, symbols)
7778 bfd *abfd;
7779 struct bfd_link_info *link_info;
7780 struct bfd_link_order *link_order;
7781 bfd_byte *data;
7782 boolean relocateable;
7783 asymbol **symbols;
7784{
7785 /* Get enough memory to hold the stuff */
7786 bfd *input_bfd = link_order->u.indirect.section->owner;
7787 asection *input_section = link_order->u.indirect.section;
7788
7789 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
7790 arelent **reloc_vector = NULL;
7791 long reloc_count;
7792
7793 if (reloc_size < 0)
7794 goto error_return;
7795
7796 reloc_vector = (arelent **) bfd_malloc (reloc_size);
7797 if (reloc_vector == NULL && reloc_size != 0)
7798 goto error_return;
7799
7800 /* read in the section */
7801 if (!bfd_get_section_contents (input_bfd,
7802 input_section,
7803 (PTR) data,
7804 0,
7805 input_section->_raw_size))
7806 goto error_return;
7807
7808 /* We're not relaxing the section, so just copy the size info */
7809 input_section->_cooked_size = input_section->_raw_size;
7810 input_section->reloc_done = true;
7811
7812 reloc_count = bfd_canonicalize_reloc (input_bfd,
7813 input_section,
7814 reloc_vector,
7815 symbols);
7816 if (reloc_count < 0)
7817 goto error_return;
7818
7819 if (reloc_count > 0)
7820 {
7821 arelent **parent;
7822 /* for mips */
7823 int gp_found;
7824 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
7825
7826 {
7827 struct bfd_hash_entry *h;
7828 struct bfd_link_hash_entry *lh;
7829 /* Skip all this stuff if we aren't mixing formats. */
7830 if (abfd && input_bfd
7831 && abfd->xvec == input_bfd->xvec)
7832 lh = 0;
7833 else
7834 {
7835 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
7836 lh = (struct bfd_link_hash_entry *) h;
7837 }
7838 lookup:
7839 if (lh)
7840 {
7841 switch (lh->type)
7842 {
7843 case bfd_link_hash_undefined:
7844 case bfd_link_hash_undefweak:
7845 case bfd_link_hash_common:
7846 gp_found = 0;
7847 break;
7848 case bfd_link_hash_defined:
7849 case bfd_link_hash_defweak:
7850 gp_found = 1;
7851 gp = lh->u.def.value;
7852 break;
7853 case bfd_link_hash_indirect:
7854 case bfd_link_hash_warning:
7855 lh = lh->u.i.link;
7856 /* @@FIXME ignoring warning for now */
7857 goto lookup;
7858 case bfd_link_hash_new:
7859 default:
7860 abort ();
7861 }
7862 }
7863 else
7864 gp_found = 0;
7865 }
7866 /* end mips */
7867 for (parent = reloc_vector; *parent != (arelent *) NULL;
7868 parent++)
7869 {
7870 char *error_message = (char *) NULL;
7871 bfd_reloc_status_type r;
7872
7873 /* Specific to MIPS: Deal with relocation types that require
7874 knowing the gp of the output bfd. */
7875 asymbol *sym = *(*parent)->sym_ptr_ptr;
7876 if (bfd_is_abs_section (sym->section) && abfd)
7877 {
7878 /* The special_function wouldn't get called anyways. */
7879 }
7880 else if (!gp_found)
7881 {
7882 /* The gp isn't there; let the special function code
7883 fall over on its own. */
7884 }
7885 else if ((*parent)->howto->special_function
7886 == _bfd_mips_elf_gprel16_reloc)
7887 {
7888 /* bypass special_function call */
7889 r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
7890 relocateable, (PTR) data, gp);
7891 goto skip_bfd_perform_relocation;
7892 }
7893 /* end mips specific stuff */
7894
7895 r = bfd_perform_relocation (input_bfd,
7896 *parent,
7897 (PTR) data,
7898 input_section,
7899 relocateable ? abfd : (bfd *) NULL,
7900 &error_message);
7901 skip_bfd_perform_relocation:
7902
7903 if (relocateable)
7904 {
7905 asection *os = input_section->output_section;
7906
7907 /* A partial link, so keep the relocs */
7908 os->orelocation[os->reloc_count] = *parent;
7909 os->reloc_count++;
7910 }
7911
7912 if (r != bfd_reloc_ok)
7913 {
7914 switch (r)
7915 {
7916 case bfd_reloc_undefined:
7917 if (!((*link_info->callbacks->undefined_symbol)
7918 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7919 input_bfd, input_section, (*parent)->address)))
7920 goto error_return;
7921 break;
7922 case bfd_reloc_dangerous:
7923 BFD_ASSERT (error_message != (char *) NULL);
7924 if (!((*link_info->callbacks->reloc_dangerous)
7925 (link_info, error_message, input_bfd, input_section,
7926 (*parent)->address)))
7927 goto error_return;
7928 break;
7929 case bfd_reloc_overflow:
7930 if (!((*link_info->callbacks->reloc_overflow)
7931 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7932 (*parent)->howto->name, (*parent)->addend,
7933 input_bfd, input_section, (*parent)->address)))
7934 goto error_return;
7935 break;
7936 case bfd_reloc_outofrange:
7937 default:
7938 abort ();
7939 break;
7940 }
7941
7942 }
7943 }
7944 }
7945 if (reloc_vector != NULL)
7946 free (reloc_vector);
7947 return data;
7948
7949error_return:
7950 if (reloc_vector != NULL)
7951 free (reloc_vector);
7952 return NULL;
7953}
7954#define bfd_elf32_bfd_get_relocated_section_contents \
7955 elf32_mips_get_relocated_section_contents
7956\f
7957/* ECOFF swapping routines. These are used when dealing with the
7958 .mdebug section, which is in the ECOFF debugging format. */
7959static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
7960{
7961 /* Symbol table magic number. */
7962 magicSym,
7963 /* Alignment of debugging information. E.g., 4. */
7964 4,
7965 /* Sizes of external symbolic information. */
7966 sizeof (struct hdr_ext),
7967 sizeof (struct dnr_ext),
7968 sizeof (struct pdr_ext),
7969 sizeof (struct sym_ext),
7970 sizeof (struct opt_ext),
7971 sizeof (struct fdr_ext),
7972 sizeof (struct rfd_ext),
7973 sizeof (struct ext_ext),
7974 /* Functions to swap in external symbolic data. */
7975 ecoff_swap_hdr_in,
7976 ecoff_swap_dnr_in,
7977 ecoff_swap_pdr_in,
7978 ecoff_swap_sym_in,
7979 ecoff_swap_opt_in,
7980 ecoff_swap_fdr_in,
7981 ecoff_swap_rfd_in,
7982 ecoff_swap_ext_in,
7983 _bfd_ecoff_swap_tir_in,
7984 _bfd_ecoff_swap_rndx_in,
7985 /* Functions to swap out external symbolic data. */
7986 ecoff_swap_hdr_out,
7987 ecoff_swap_dnr_out,
7988 ecoff_swap_pdr_out,
7989 ecoff_swap_sym_out,
7990 ecoff_swap_opt_out,
7991 ecoff_swap_fdr_out,
7992 ecoff_swap_rfd_out,
7993 ecoff_swap_ext_out,
7994 _bfd_ecoff_swap_tir_out,
7995 _bfd_ecoff_swap_rndx_out,
7996 /* Function to read in symbolic data. */
7997 _bfd_mips_elf_read_ecoff_info
7998};
7999\f
8000#define TARGET_LITTLE_SYM bfd_elf32_littlemips_vec
8001#define TARGET_LITTLE_NAME "elf32-littlemips"
8002#define TARGET_BIG_SYM bfd_elf32_bigmips_vec
8003#define TARGET_BIG_NAME "elf32-bigmips"
8004#define ELF_ARCH bfd_arch_mips
8005#define ELF_MACHINE_CODE EM_MIPS
8006
8007/* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
8008 a value of 0x1000, and we are compatible. */
8009#define ELF_MAXPAGESIZE 0x1000
8010
8011#define elf_backend_collect true
8012#define elf_backend_type_change_ok true
8013#define elf_backend_can_gc_sections true
3f830999 8014#define elf_info_to_howto mips_info_to_howto_rela
252b5132
RH
8015#define elf_info_to_howto_rel mips_info_to_howto_rel
8016#define elf_backend_sym_is_global mips_elf_sym_is_global
8017#define elf_backend_object_p mips_elf32_object_p
8018#define elf_backend_section_from_shdr mips_elf32_section_from_shdr
8019#define elf_backend_fake_sections _bfd_mips_elf_fake_sections
8020#define elf_backend_section_from_bfd_section \
8021 _bfd_mips_elf_section_from_bfd_section
8022#define elf_backend_section_processing mips_elf32_section_processing
8023#define elf_backend_symbol_processing _bfd_mips_elf_symbol_processing
8024#define elf_backend_additional_program_headers \
8025 mips_elf_additional_program_headers
8026#define elf_backend_modify_segment_map mips_elf_modify_segment_map
8027#define elf_backend_final_write_processing \
8028 _bfd_mips_elf_final_write_processing
8029#define elf_backend_ecoff_debug_swap &mips_elf32_ecoff_debug_swap
8030
8031#define bfd_elf32_bfd_is_local_label_name \
8032 mips_elf_is_local_label_name
8033#define bfd_elf32_find_nearest_line _bfd_mips_elf_find_nearest_line
8034#define bfd_elf32_set_section_contents _bfd_mips_elf_set_section_contents
8035#define bfd_elf32_bfd_link_hash_table_create \
8036 mips_elf_link_hash_table_create
8037#define bfd_elf32_bfd_final_link mips_elf_final_link
8038#define bfd_elf32_bfd_copy_private_bfd_data \
8039 _bfd_mips_elf_copy_private_bfd_data
8040#define bfd_elf32_bfd_merge_private_bfd_data \
8041 _bfd_mips_elf_merge_private_bfd_data
8042#define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
8043#define bfd_elf32_bfd_print_private_bfd_data \
8044 _bfd_mips_elf_print_private_bfd_data
8045#define elf_backend_add_symbol_hook mips_elf_add_symbol_hook
8046#define elf_backend_create_dynamic_sections \
8047 mips_elf_create_dynamic_sections
8048#define elf_backend_check_relocs mips_elf_check_relocs
8049#define elf_backend_adjust_dynamic_symbol \
8050 mips_elf_adjust_dynamic_symbol
8051#define elf_backend_always_size_sections \
8052 mips_elf_always_size_sections
8053#define elf_backend_size_dynamic_sections \
8054 mips_elf_size_dynamic_sections
8055#define elf_backend_relocate_section mips_elf_relocate_section
8056#define elf_backend_link_output_symbol_hook \
8057 mips_elf_link_output_symbol_hook
8058#define elf_backend_finish_dynamic_symbol \
8059 mips_elf_finish_dynamic_symbol
8060#define elf_backend_finish_dynamic_sections \
8061 mips_elf_finish_dynamic_sections
8062#define elf_backend_gc_mark_hook mips_elf_gc_mark_hook
8063#define elf_backend_gc_sweep_hook mips_elf_gc_sweep_hook
8064
8065#define elf_backend_got_header_size (4*MIPS_RESERVED_GOTNO)
8066#define elf_backend_plt_header_size 0
8067
8068#include "elf32-target.h"
This page took 0.349792 seconds and 4 git commands to generate.