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