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