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