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