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