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