Change from OTHER_RELOCATING_SECTIONS to STACK_ADDR in elf32frv.sh
[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) \
cf6fb9ce 378 ((get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64) != 0)
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. */
2935 outrel[0].r_info = ELF_R_INFO (output_bfd, indx, R_MIPS_REL32);
2936
2937 /* Adjust the output offset of the relocation to reference the
2938 correct location in the output file. */
2939 outrel[0].r_offset += (input_section->output_section->vma
2940 + input_section->output_offset);
2941 outrel[1].r_offset += (input_section->output_section->vma
2942 + input_section->output_offset);
2943 outrel[2].r_offset += (input_section->output_section->vma
2944 + input_section->output_offset);
2945 }
2946
2947 /* Put the relocation back out. We have to use the special
2948 relocation outputter in the 64-bit case since the 64-bit
2949 relocation format is non-standard. */
2950 if (ABI_64_P (output_bfd))
2951 {
2952 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2953 (output_bfd, &outrel[0],
2954 (sreloc->contents
2955 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
2956 }
2957 else
2958 bfd_elf32_swap_reloc_out (output_bfd, &outrel[0],
2959 (((Elf32_External_Rel *)
2960 sreloc->contents)
2961 + sreloc->reloc_count));
2962
2963 /* Record the index of the first relocation referencing H. This
2964 information is later emitted in the .msym section. */
2965 if (h != NULL
2966 && (h->min_dyn_reloc_index == 0
2967 || sreloc->reloc_count < h->min_dyn_reloc_index))
2968 h->min_dyn_reloc_index = sreloc->reloc_count;
2969
2970 /* We've now added another relocation. */
2971 ++sreloc->reloc_count;
2972
2973 /* Make sure the output section is writable. The dynamic linker
2974 will be writing to it. */
2975 elf_section_data (input_section->output_section)->this_hdr.sh_flags
2976 |= SHF_WRITE;
2977
2978 /* On IRIX5, make an entry of compact relocation info. */
2979 if (! skip && IRIX_COMPAT (output_bfd) == ict_irix5)
2980 {
2981 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
2982 bfd_byte *cr;
2983
2984 if (scpt)
2985 {
2986 Elf32_crinfo cptrel;
2987
2988 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
2989 cptrel.vaddr = (rel->r_offset
2990 + input_section->output_section->vma
2991 + input_section->output_offset);
2992 if (r_type == R_MIPS_REL32)
2993 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
2994 else
2995 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
2996 mips_elf_set_cr_dist2to (cptrel, 0);
2997 cptrel.konst = *addendp;
2998
2999 cr = (scpt->contents
3000 + sizeof (Elf32_External_compact_rel));
3001 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
3002 ((Elf32_External_crinfo *) cr
3003 + scpt->reloc_count));
3004 ++scpt->reloc_count;
3005 }
3006 }
3007
3008 return true;
3009}
3010\f
3011/* Return the ISA for a MIPS e_flags value. */
3012
3013static INLINE int
3014elf_mips_isa (flags)
3015 flagword flags;
3016{
3017 switch (flags & EF_MIPS_ARCH)
3018 {
3019 case E_MIPS_ARCH_1:
3020 return 1;
3021 case E_MIPS_ARCH_2:
3022 return 2;
3023 case E_MIPS_ARCH_3:
3024 return 3;
3025 case E_MIPS_ARCH_4:
3026 return 4;
3027 case E_MIPS_ARCH_5:
3028 return 5;
3029 case E_MIPS_ARCH_32:
3030 return 32;
3031 case E_MIPS_ARCH_64:
3032 return 64;
3033 }
3034 return 4;
3035}
3036
3037/* Return the MACH for a MIPS e_flags value. */
3038
3039unsigned long
3040_bfd_elf_mips_mach (flags)
3041 flagword flags;
3042{
3043 switch (flags & EF_MIPS_MACH)
3044 {
3045 case E_MIPS_MACH_3900:
3046 return bfd_mach_mips3900;
3047
3048 case E_MIPS_MACH_4010:
3049 return bfd_mach_mips4010;
3050
3051 case E_MIPS_MACH_4100:
3052 return bfd_mach_mips4100;
3053
3054 case E_MIPS_MACH_4111:
3055 return bfd_mach_mips4111;
3056
3057 case E_MIPS_MACH_4650:
3058 return bfd_mach_mips4650;
3059
3060 case E_MIPS_MACH_SB1:
3061 return bfd_mach_mips_sb1;
3062
3063 default:
3064 switch (flags & EF_MIPS_ARCH)
3065 {
3066 default:
3067 case E_MIPS_ARCH_1:
3068 return bfd_mach_mips3000;
3069 break;
3070
3071 case E_MIPS_ARCH_2:
3072 return bfd_mach_mips6000;
3073 break;
3074
3075 case E_MIPS_ARCH_3:
3076 return bfd_mach_mips4000;
3077 break;
3078
3079 case E_MIPS_ARCH_4:
3080 return bfd_mach_mips8000;
3081 break;
3082
3083 case E_MIPS_ARCH_5:
3084 return bfd_mach_mips5;
3085 break;
3086
3087 case E_MIPS_ARCH_32:
3088 return bfd_mach_mipsisa32;
3089 break;
3090
3091 case E_MIPS_ARCH_64:
3092 return bfd_mach_mipsisa64;
3093 break;
3094 }
3095 }
3096
3097 return 0;
3098}
3099
3100/* Return printable name for ABI. */
3101
3102static INLINE char *
3103elf_mips_abi_name (abfd)
3104 bfd *abfd;
3105{
3106 flagword flags;
3107
3108 flags = elf_elfheader (abfd)->e_flags;
3109 switch (flags & EF_MIPS_ABI)
3110 {
3111 case 0:
3112 if (ABI_N32_P (abfd))
3113 return "N32";
3114 else if (ABI_64_P (abfd))
3115 return "64";
3116 else
3117 return "none";
3118 case E_MIPS_ABI_O32:
3119 return "O32";
3120 case E_MIPS_ABI_O64:
3121 return "O64";
3122 case E_MIPS_ABI_EABI32:
3123 return "EABI32";
3124 case E_MIPS_ABI_EABI64:
3125 return "EABI64";
3126 default:
3127 return "unknown abi";
3128 }
3129}
3130\f
3131/* MIPS ELF uses two common sections. One is the usual one, and the
3132 other is for small objects. All the small objects are kept
3133 together, and then referenced via the gp pointer, which yields
3134 faster assembler code. This is what we use for the small common
3135 section. This approach is copied from ecoff.c. */
3136static asection mips_elf_scom_section;
3137static asymbol mips_elf_scom_symbol;
3138static asymbol *mips_elf_scom_symbol_ptr;
3139
3140/* MIPS ELF also uses an acommon section, which represents an
3141 allocated common symbol which may be overridden by a
3142 definition in a shared library. */
3143static asection mips_elf_acom_section;
3144static asymbol mips_elf_acom_symbol;
3145static asymbol *mips_elf_acom_symbol_ptr;
3146
3147/* Handle the special MIPS section numbers that a symbol may use.
3148 This is used for both the 32-bit and the 64-bit ABI. */
3149
3150void
3151_bfd_mips_elf_symbol_processing (abfd, asym)
3152 bfd *abfd;
3153 asymbol *asym;
3154{
3155 elf_symbol_type *elfsym;
3156
3157 elfsym = (elf_symbol_type *) asym;
3158 switch (elfsym->internal_elf_sym.st_shndx)
3159 {
3160 case SHN_MIPS_ACOMMON:
3161 /* This section is used in a dynamically linked executable file.
3162 It is an allocated common section. The dynamic linker can
3163 either resolve these symbols to something in a shared
3164 library, or it can just leave them here. For our purposes,
3165 we can consider these symbols to be in a new section. */
3166 if (mips_elf_acom_section.name == NULL)
3167 {
3168 /* Initialize the acommon section. */
3169 mips_elf_acom_section.name = ".acommon";
3170 mips_elf_acom_section.flags = SEC_ALLOC;
3171 mips_elf_acom_section.output_section = &mips_elf_acom_section;
3172 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
3173 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
3174 mips_elf_acom_symbol.name = ".acommon";
3175 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
3176 mips_elf_acom_symbol.section = &mips_elf_acom_section;
3177 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
3178 }
3179 asym->section = &mips_elf_acom_section;
3180 break;
3181
3182 case SHN_COMMON:
3183 /* Common symbols less than the GP size are automatically
3184 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
3185 if (asym->value > elf_gp_size (abfd)
3186 || IRIX_COMPAT (abfd) == ict_irix6)
3187 break;
3188 /* Fall through. */
3189 case SHN_MIPS_SCOMMON:
3190 if (mips_elf_scom_section.name == NULL)
3191 {
3192 /* Initialize the small common section. */
3193 mips_elf_scom_section.name = ".scommon";
3194 mips_elf_scom_section.flags = SEC_IS_COMMON;
3195 mips_elf_scom_section.output_section = &mips_elf_scom_section;
3196 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
3197 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
3198 mips_elf_scom_symbol.name = ".scommon";
3199 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
3200 mips_elf_scom_symbol.section = &mips_elf_scom_section;
3201 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
3202 }
3203 asym->section = &mips_elf_scom_section;
3204 asym->value = elfsym->internal_elf_sym.st_size;
3205 break;
3206
3207 case SHN_MIPS_SUNDEFINED:
3208 asym->section = bfd_und_section_ptr;
3209 break;
3210
3211#if 0 /* for SGI_COMPAT */
3212 case SHN_MIPS_TEXT:
3213 asym->section = mips_elf_text_section_ptr;
3214 break;
3215
3216 case SHN_MIPS_DATA:
3217 asym->section = mips_elf_data_section_ptr;
3218 break;
3219#endif
3220 }
3221}
3222\f
3223/* Work over a section just before writing it out. This routine is
3224 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
3225 sections that need the SHF_MIPS_GPREL flag by name; there has to be
3226 a better way. */
3227
3228boolean
3229_bfd_mips_elf_section_processing (abfd, hdr)
3230 bfd *abfd;
3231 Elf_Internal_Shdr *hdr;
3232{
3233 if (hdr->sh_type == SHT_MIPS_REGINFO
3234 && hdr->sh_size > 0)
3235 {
3236 bfd_byte buf[4];
3237
3238 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
3239 BFD_ASSERT (hdr->contents == NULL);
3240
3241 if (bfd_seek (abfd,
3242 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
3243 SEEK_SET) != 0)
3244 return false;
3245 H_PUT_32 (abfd, elf_gp (abfd), buf);
3246 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
3247 return false;
3248 }
3249
3250 if (hdr->sh_type == SHT_MIPS_OPTIONS
3251 && hdr->bfd_section != NULL
3252 && elf_section_data (hdr->bfd_section) != NULL
3253 && elf_section_data (hdr->bfd_section)->tdata != NULL)
3254 {
3255 bfd_byte *contents, *l, *lend;
3256
3257 /* We stored the section contents in the elf_section_data tdata
3258 field in the set_section_contents routine. We save the
3259 section contents so that we don't have to read them again.
3260 At this point we know that elf_gp is set, so we can look
3261 through the section contents to see if there is an
3262 ODK_REGINFO structure. */
3263
3264 contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
3265 l = contents;
3266 lend = contents + hdr->sh_size;
3267 while (l + sizeof (Elf_External_Options) <= lend)
3268 {
3269 Elf_Internal_Options intopt;
3270
3271 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
3272 &intopt);
3273 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
3274 {
3275 bfd_byte buf[8];
3276
3277 if (bfd_seek (abfd,
3278 (hdr->sh_offset
3279 + (l - contents)
3280 + sizeof (Elf_External_Options)
3281 + (sizeof (Elf64_External_RegInfo) - 8)),
3282 SEEK_SET) != 0)
3283 return false;
3284 H_PUT_64 (abfd, elf_gp (abfd), buf);
3285 if (bfd_bwrite (buf, (bfd_size_type) 8, abfd) != 8)
3286 return false;
3287 }
3288 else if (intopt.kind == ODK_REGINFO)
3289 {
3290 bfd_byte buf[4];
3291
3292 if (bfd_seek (abfd,
3293 (hdr->sh_offset
3294 + (l - contents)
3295 + sizeof (Elf_External_Options)
3296 + (sizeof (Elf32_External_RegInfo) - 4)),
3297 SEEK_SET) != 0)
3298 return false;
3299 H_PUT_32 (abfd, elf_gp (abfd), buf);
3300 if (bfd_bwrite (buf, (bfd_size_type) 4, abfd) != 4)
3301 return false;
3302 }
3303 l += intopt.size;
3304 }
3305 }
3306
3307 if (hdr->bfd_section != NULL)
3308 {
3309 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
3310
3311 if (strcmp (name, ".sdata") == 0
3312 || strcmp (name, ".lit8") == 0
3313 || strcmp (name, ".lit4") == 0)
3314 {
3315 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3316 hdr->sh_type = SHT_PROGBITS;
3317 }
3318 else if (strcmp (name, ".sbss") == 0)
3319 {
3320 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
3321 hdr->sh_type = SHT_NOBITS;
3322 }
3323 else if (strcmp (name, ".srdata") == 0)
3324 {
3325 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
3326 hdr->sh_type = SHT_PROGBITS;
3327 }
3328 else if (strcmp (name, ".compact_rel") == 0)
3329 {
3330 hdr->sh_flags = 0;
3331 hdr->sh_type = SHT_PROGBITS;
3332 }
3333 else if (strcmp (name, ".rtproc") == 0)
3334 {
3335 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
3336 {
3337 unsigned int adjust;
3338
3339 adjust = hdr->sh_size % hdr->sh_addralign;
3340 if (adjust != 0)
3341 hdr->sh_size += hdr->sh_addralign - adjust;
3342 }
3343 }
3344 }
3345
3346 return true;
3347}
3348
3349/* Handle a MIPS specific section when reading an object file. This
3350 is called when elfcode.h finds a section with an unknown type.
3351 This routine supports both the 32-bit and 64-bit ELF ABI.
3352
3353 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
3354 how to. */
3355
3356boolean
3357_bfd_mips_elf_section_from_shdr (abfd, hdr, name)
3358 bfd *abfd;
3359 Elf_Internal_Shdr *hdr;
90937f86 3360 const char *name;
b49e97c9
TS
3361{
3362 flagword flags = 0;
3363
3364 /* There ought to be a place to keep ELF backend specific flags, but
3365 at the moment there isn't one. We just keep track of the
3366 sections by their name, instead. Fortunately, the ABI gives
3367 suggested names for all the MIPS specific sections, so we will
3368 probably get away with this. */
3369 switch (hdr->sh_type)
3370 {
3371 case SHT_MIPS_LIBLIST:
3372 if (strcmp (name, ".liblist") != 0)
3373 return false;
3374 break;
3375 case SHT_MIPS_MSYM:
3376 if (strcmp (name, ".msym") != 0)
3377 return false;
3378 break;
3379 case SHT_MIPS_CONFLICT:
3380 if (strcmp (name, ".conflict") != 0)
3381 return false;
3382 break;
3383 case SHT_MIPS_GPTAB:
3384 if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
3385 return false;
3386 break;
3387 case SHT_MIPS_UCODE:
3388 if (strcmp (name, ".ucode") != 0)
3389 return false;
3390 break;
3391 case SHT_MIPS_DEBUG:
3392 if (strcmp (name, ".mdebug") != 0)
3393 return false;
3394 flags = SEC_DEBUGGING;
3395 break;
3396 case SHT_MIPS_REGINFO:
3397 if (strcmp (name, ".reginfo") != 0
3398 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
3399 return false;
3400 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
3401 break;
3402 case SHT_MIPS_IFACE:
3403 if (strcmp (name, ".MIPS.interfaces") != 0)
3404 return false;
3405 break;
3406 case SHT_MIPS_CONTENT:
3407 if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
3408 return false;
3409 break;
3410 case SHT_MIPS_OPTIONS:
3411 if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) != 0)
3412 return false;
3413 break;
3414 case SHT_MIPS_DWARF:
3415 if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
3416 return false;
3417 break;
3418 case SHT_MIPS_SYMBOL_LIB:
3419 if (strcmp (name, ".MIPS.symlib") != 0)
3420 return false;
3421 break;
3422 case SHT_MIPS_EVENTS:
3423 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
3424 && strncmp (name, ".MIPS.post_rel",
3425 sizeof ".MIPS.post_rel" - 1) != 0)
3426 return false;
3427 break;
3428 default:
3429 return false;
3430 }
3431
3432 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
3433 return false;
3434
3435 if (flags)
3436 {
3437 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
3438 (bfd_get_section_flags (abfd,
3439 hdr->bfd_section)
3440 | flags)))
3441 return false;
3442 }
3443
3444 /* FIXME: We should record sh_info for a .gptab section. */
3445
3446 /* For a .reginfo section, set the gp value in the tdata information
3447 from the contents of this section. We need the gp value while
3448 processing relocs, so we just get it now. The .reginfo section
3449 is not used in the 64-bit MIPS ELF ABI. */
3450 if (hdr->sh_type == SHT_MIPS_REGINFO)
3451 {
3452 Elf32_External_RegInfo ext;
3453 Elf32_RegInfo s;
3454
3455 if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
3456 (file_ptr) 0,
3457 (bfd_size_type) sizeof ext))
3458 return false;
3459 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
3460 elf_gp (abfd) = s.ri_gp_value;
3461 }
3462
3463 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
3464 set the gp value based on what we find. We may see both
3465 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
3466 they should agree. */
3467 if (hdr->sh_type == SHT_MIPS_OPTIONS)
3468 {
3469 bfd_byte *contents, *l, *lend;
3470
3471 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
3472 if (contents == NULL)
3473 return false;
3474 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
3475 (file_ptr) 0, hdr->sh_size))
3476 {
3477 free (contents);
3478 return false;
3479 }
3480 l = contents;
3481 lend = contents + hdr->sh_size;
3482 while (l + sizeof (Elf_External_Options) <= lend)
3483 {
3484 Elf_Internal_Options intopt;
3485
3486 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
3487 &intopt);
3488 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
3489 {
3490 Elf64_Internal_RegInfo intreg;
3491
3492 bfd_mips_elf64_swap_reginfo_in
3493 (abfd,
3494 ((Elf64_External_RegInfo *)
3495 (l + sizeof (Elf_External_Options))),
3496 &intreg);
3497 elf_gp (abfd) = intreg.ri_gp_value;
3498 }
3499 else if (intopt.kind == ODK_REGINFO)
3500 {
3501 Elf32_RegInfo intreg;
3502
3503 bfd_mips_elf32_swap_reginfo_in
3504 (abfd,
3505 ((Elf32_External_RegInfo *)
3506 (l + sizeof (Elf_External_Options))),
3507 &intreg);
3508 elf_gp (abfd) = intreg.ri_gp_value;
3509 }
3510 l += intopt.size;
3511 }
3512 free (contents);
3513 }
3514
3515 return true;
3516}
3517
3518/* Set the correct type for a MIPS ELF section. We do this by the
3519 section name, which is a hack, but ought to work. This routine is
3520 used by both the 32-bit and the 64-bit ABI. */
3521
3522boolean
3523_bfd_mips_elf_fake_sections (abfd, hdr, sec)
3524 bfd *abfd;
3525 Elf32_Internal_Shdr *hdr;
3526 asection *sec;
3527{
3528 register const char *name;
3529
3530 name = bfd_get_section_name (abfd, sec);
3531
3532 if (strcmp (name, ".liblist") == 0)
3533 {
3534 hdr->sh_type = SHT_MIPS_LIBLIST;
3535 hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
3536 /* The sh_link field is set in final_write_processing. */
3537 }
3538 else if (strcmp (name, ".conflict") == 0)
3539 hdr->sh_type = SHT_MIPS_CONFLICT;
3540 else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
3541 {
3542 hdr->sh_type = SHT_MIPS_GPTAB;
3543 hdr->sh_entsize = sizeof (Elf32_External_gptab);
3544 /* The sh_info field is set in final_write_processing. */
3545 }
3546 else if (strcmp (name, ".ucode") == 0)
3547 hdr->sh_type = SHT_MIPS_UCODE;
3548 else if (strcmp (name, ".mdebug") == 0)
3549 {
3550 hdr->sh_type = SHT_MIPS_DEBUG;
8dc1a139 3551 /* In a shared object on IRIX 5.3, the .mdebug section has an
b49e97c9
TS
3552 entsize of 0. FIXME: Does this matter? */
3553 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
3554 hdr->sh_entsize = 0;
3555 else
3556 hdr->sh_entsize = 1;
3557 }
3558 else if (strcmp (name, ".reginfo") == 0)
3559 {
3560 hdr->sh_type = SHT_MIPS_REGINFO;
8dc1a139 3561 /* In a shared object on IRIX 5.3, the .reginfo section has an
b49e97c9
TS
3562 entsize of 0x18. FIXME: Does this matter? */
3563 if (SGI_COMPAT (abfd))
3564 {
3565 if ((abfd->flags & DYNAMIC) != 0)
3566 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
3567 else
3568 hdr->sh_entsize = 1;
3569 }
3570 else
3571 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
3572 }
3573 else if (SGI_COMPAT (abfd)
3574 && (strcmp (name, ".hash") == 0
3575 || strcmp (name, ".dynamic") == 0
3576 || strcmp (name, ".dynstr") == 0))
3577 {
3578 if (SGI_COMPAT (abfd))
3579 hdr->sh_entsize = 0;
3580#if 0
8dc1a139 3581 /* This isn't how the IRIX6 linker behaves. */
b49e97c9
TS
3582 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
3583#endif
3584 }
3585 else if (strcmp (name, ".got") == 0
3586 || strcmp (name, ".srdata") == 0
3587 || strcmp (name, ".sdata") == 0
3588 || strcmp (name, ".sbss") == 0
3589 || strcmp (name, ".lit4") == 0
3590 || strcmp (name, ".lit8") == 0)
3591 hdr->sh_flags |= SHF_MIPS_GPREL;
3592 else if (strcmp (name, ".MIPS.interfaces") == 0)
3593 {
3594 hdr->sh_type = SHT_MIPS_IFACE;
3595 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3596 }
3597 else if (strncmp (name, ".MIPS.content", strlen (".MIPS.content")) == 0)
3598 {
3599 hdr->sh_type = SHT_MIPS_CONTENT;
3600 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3601 /* The sh_info field is set in final_write_processing. */
3602 }
3603 else if (strcmp (name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
3604 {
3605 hdr->sh_type = SHT_MIPS_OPTIONS;
3606 hdr->sh_entsize = 1;
3607 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3608 }
3609 else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
3610 hdr->sh_type = SHT_MIPS_DWARF;
3611 else if (strcmp (name, ".MIPS.symlib") == 0)
3612 {
3613 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
3614 /* The sh_link and sh_info fields are set in
3615 final_write_processing. */
3616 }
3617 else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
3618 || strncmp (name, ".MIPS.post_rel",
3619 sizeof ".MIPS.post_rel" - 1) == 0)
3620 {
3621 hdr->sh_type = SHT_MIPS_EVENTS;
3622 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
3623 /* The sh_link field is set in final_write_processing. */
3624 }
3625 else if (strcmp (name, ".msym") == 0)
3626 {
3627 hdr->sh_type = SHT_MIPS_MSYM;
3628 hdr->sh_flags |= SHF_ALLOC;
3629 hdr->sh_entsize = 8;
3630 }
3631
3632 /* The generic elf_fake_sections will set up REL_HDR using the
3633 default kind of relocations. But, we may actually need both
3634 kinds of relocations, so we set up the second header here.
3635
3636 This is not necessary for the O32 ABI since that only uses Elf32_Rel
3637 relocations (cf. System V ABI, MIPS RISC Processor Supplement,
3638 3rd Edition, p. 4-17). It breaks the IRIX 5/6 32-bit ld, since one
3639 of the resulting empty .rela.<section> sections starts with
3640 sh_offset == object size, and ld doesn't allow that. While the check
3641 is arguably bogus for empty or SHT_NOBITS sections, it can easily be
3642 avoided by not emitting those useless sections in the first place. */
4a14403c
TS
3643 if ((IRIX_COMPAT (abfd) != ict_irix5 && (IRIX_COMPAT (abfd) != ict_irix6))
3644 && (sec->flags & SEC_RELOC) != 0)
b49e97c9
TS
3645 {
3646 struct bfd_elf_section_data *esd;
3647 bfd_size_type amt = sizeof (Elf_Internal_Shdr);
3648
3649 esd = elf_section_data (sec);
3650 BFD_ASSERT (esd->rel_hdr2 == NULL);
3651 esd->rel_hdr2 = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt);
3652 if (!esd->rel_hdr2)
3653 return false;
3654 _bfd_elf_init_reloc_shdr (abfd, esd->rel_hdr2, sec,
3655 !elf_section_data (sec)->use_rela_p);
3656 }
3657
3658 return true;
3659}
3660
3661/* Given a BFD section, try to locate the corresponding ELF section
3662 index. This is used by both the 32-bit and the 64-bit ABI.
3663 Actually, it's not clear to me that the 64-bit ABI supports these,
3664 but for non-PIC objects we will certainly want support for at least
3665 the .scommon section. */
3666
3667boolean
3668_bfd_mips_elf_section_from_bfd_section (abfd, sec, retval)
3669 bfd *abfd ATTRIBUTE_UNUSED;
3670 asection *sec;
3671 int *retval;
3672{
3673 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
3674 {
3675 *retval = SHN_MIPS_SCOMMON;
3676 return true;
3677 }
3678 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
3679 {
3680 *retval = SHN_MIPS_ACOMMON;
3681 return true;
3682 }
3683 return false;
3684}
3685\f
3686/* Hook called by the linker routine which adds symbols from an object
3687 file. We must handle the special MIPS section numbers here. */
3688
3689boolean
3690_bfd_mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3691 bfd *abfd;
3692 struct bfd_link_info *info;
3693 const Elf_Internal_Sym *sym;
3694 const char **namep;
3695 flagword *flagsp ATTRIBUTE_UNUSED;
3696 asection **secp;
3697 bfd_vma *valp;
3698{
3699 if (SGI_COMPAT (abfd)
3700 && (abfd->flags & DYNAMIC) != 0
3701 && strcmp (*namep, "_rld_new_interface") == 0)
3702 {
8dc1a139 3703 /* Skip IRIX5 rld entry name. */
b49e97c9
TS
3704 *namep = NULL;
3705 return true;
3706 }
3707
3708 switch (sym->st_shndx)
3709 {
3710 case SHN_COMMON:
3711 /* Common symbols less than the GP size are automatically
3712 treated as SHN_MIPS_SCOMMON symbols. */
3713 if (sym->st_size > elf_gp_size (abfd)
3714 || IRIX_COMPAT (abfd) == ict_irix6)
3715 break;
3716 /* Fall through. */
3717 case SHN_MIPS_SCOMMON:
3718 *secp = bfd_make_section_old_way (abfd, ".scommon");
3719 (*secp)->flags |= SEC_IS_COMMON;
3720 *valp = sym->st_size;
3721 break;
3722
3723 case SHN_MIPS_TEXT:
3724 /* This section is used in a shared object. */
3725 if (elf_tdata (abfd)->elf_text_section == NULL)
3726 {
3727 asymbol *elf_text_symbol;
3728 asection *elf_text_section;
3729 bfd_size_type amt = sizeof (asection);
3730
3731 elf_text_section = bfd_zalloc (abfd, amt);
3732 if (elf_text_section == NULL)
3733 return false;
3734
3735 amt = sizeof (asymbol);
3736 elf_text_symbol = bfd_zalloc (abfd, amt);
3737 if (elf_text_symbol == NULL)
3738 return false;
3739
3740 /* Initialize the section. */
3741
3742 elf_tdata (abfd)->elf_text_section = elf_text_section;
3743 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
3744
3745 elf_text_section->symbol = elf_text_symbol;
3746 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
3747
3748 elf_text_section->name = ".text";
3749 elf_text_section->flags = SEC_NO_FLAGS;
3750 elf_text_section->output_section = NULL;
3751 elf_text_section->owner = abfd;
3752 elf_text_symbol->name = ".text";
3753 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
3754 elf_text_symbol->section = elf_text_section;
3755 }
3756 /* This code used to do *secp = bfd_und_section_ptr if
3757 info->shared. I don't know why, and that doesn't make sense,
3758 so I took it out. */
3759 *secp = elf_tdata (abfd)->elf_text_section;
3760 break;
3761
3762 case SHN_MIPS_ACOMMON:
3763 /* Fall through. XXX Can we treat this as allocated data? */
3764 case SHN_MIPS_DATA:
3765 /* This section is used in a shared object. */
3766 if (elf_tdata (abfd)->elf_data_section == NULL)
3767 {
3768 asymbol *elf_data_symbol;
3769 asection *elf_data_section;
3770 bfd_size_type amt = sizeof (asection);
3771
3772 elf_data_section = bfd_zalloc (abfd, amt);
3773 if (elf_data_section == NULL)
3774 return false;
3775
3776 amt = sizeof (asymbol);
3777 elf_data_symbol = bfd_zalloc (abfd, amt);
3778 if (elf_data_symbol == NULL)
3779 return false;
3780
3781 /* Initialize the section. */
3782
3783 elf_tdata (abfd)->elf_data_section = elf_data_section;
3784 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
3785
3786 elf_data_section->symbol = elf_data_symbol;
3787 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
3788
3789 elf_data_section->name = ".data";
3790 elf_data_section->flags = SEC_NO_FLAGS;
3791 elf_data_section->output_section = NULL;
3792 elf_data_section->owner = abfd;
3793 elf_data_symbol->name = ".data";
3794 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
3795 elf_data_symbol->section = elf_data_section;
3796 }
3797 /* This code used to do *secp = bfd_und_section_ptr if
3798 info->shared. I don't know why, and that doesn't make sense,
3799 so I took it out. */
3800 *secp = elf_tdata (abfd)->elf_data_section;
3801 break;
3802
3803 case SHN_MIPS_SUNDEFINED:
3804 *secp = bfd_und_section_ptr;
3805 break;
3806 }
3807
3808 if (SGI_COMPAT (abfd)
3809 && ! info->shared
3810 && info->hash->creator == abfd->xvec
3811 && strcmp (*namep, "__rld_obj_head") == 0)
3812 {
3813 struct elf_link_hash_entry *h;
3814
3815 /* Mark __rld_obj_head as dynamic. */
3816 h = NULL;
3817 if (! (_bfd_generic_link_add_one_symbol
3818 (info, abfd, *namep, BSF_GLOBAL, *secp,
3819 (bfd_vma) *valp, (const char *) NULL, false,
3820 get_elf_backend_data (abfd)->collect,
3821 (struct bfd_link_hash_entry **) &h)))
3822 return false;
3823 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
3824 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3825 h->type = STT_OBJECT;
3826
3827 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3828 return false;
3829
3830 mips_elf_hash_table (info)->use_rld_obj_head = true;
3831 }
3832
3833 /* If this is a mips16 text symbol, add 1 to the value to make it
3834 odd. This will cause something like .word SYM to come up with
3835 the right value when it is loaded into the PC. */
3836 if (sym->st_other == STO_MIPS16)
3837 ++*valp;
3838
3839 return true;
3840}
3841
3842/* This hook function is called before the linker writes out a global
3843 symbol. We mark symbols as small common if appropriate. This is
3844 also where we undo the increment of the value for a mips16 symbol. */
3845
3846boolean
3847_bfd_mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
3848 bfd *abfd ATTRIBUTE_UNUSED;
3849 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3850 const char *name ATTRIBUTE_UNUSED;
3851 Elf_Internal_Sym *sym;
3852 asection *input_sec;
3853{
3854 /* If we see a common symbol, which implies a relocatable link, then
3855 if a symbol was small common in an input file, mark it as small
3856 common in the output file. */
3857 if (sym->st_shndx == SHN_COMMON
3858 && strcmp (input_sec->name, ".scommon") == 0)
3859 sym->st_shndx = SHN_MIPS_SCOMMON;
3860
3861 if (sym->st_other == STO_MIPS16
3862 && (sym->st_value & 1) != 0)
3863 --sym->st_value;
3864
3865 return true;
3866}
3867\f
3868/* Functions for the dynamic linker. */
3869
3870/* Create dynamic sections when linking against a dynamic object. */
3871
3872boolean
3873_bfd_mips_elf_create_dynamic_sections (abfd, info)
3874 bfd *abfd;
3875 struct bfd_link_info *info;
3876{
3877 struct elf_link_hash_entry *h;
3878 flagword flags;
3879 register asection *s;
3880 const char * const *namep;
3881
3882 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3883 | SEC_LINKER_CREATED | SEC_READONLY);
3884
3885 /* Mips ABI requests the .dynamic section to be read only. */
3886 s = bfd_get_section_by_name (abfd, ".dynamic");
3887 if (s != NULL)
3888 {
3889 if (! bfd_set_section_flags (abfd, s, flags))
3890 return false;
3891 }
3892
3893 /* We need to create .got section. */
3894 if (! mips_elf_create_got_section (abfd, info))
3895 return false;
3896
3897 /* Create the .msym section on IRIX6. It is used by the dynamic
3898 linker to speed up dynamic relocations, and to avoid computing
3899 the ELF hash for symbols. */
3900 if (IRIX_COMPAT (abfd) == ict_irix6
3901 && !mips_elf_create_msym_section (abfd))
3902 return false;
3903
3904 /* Create .stub section. */
3905 if (bfd_get_section_by_name (abfd,
3906 MIPS_ELF_STUB_SECTION_NAME (abfd)) == NULL)
3907 {
3908 s = bfd_make_section (abfd, MIPS_ELF_STUB_SECTION_NAME (abfd));
3909 if (s == NULL
3910 || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
3911 || ! bfd_set_section_alignment (abfd, s,
3912 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
3913 return false;
3914 }
3915
3916 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
3917 && !info->shared
3918 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
3919 {
3920 s = bfd_make_section (abfd, ".rld_map");
3921 if (s == NULL
3922 || ! bfd_set_section_flags (abfd, s, flags &~ (flagword) SEC_READONLY)
3923 || ! bfd_set_section_alignment (abfd, s,
3924 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
3925 return false;
3926 }
3927
3928 /* On IRIX5, we adjust add some additional symbols and change the
3929 alignments of several sections. There is no ABI documentation
3930 indicating that this is necessary on IRIX6, nor any evidence that
3931 the linker takes such action. */
3932 if (IRIX_COMPAT (abfd) == ict_irix5)
3933 {
3934 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
3935 {
3936 h = NULL;
3937 if (! (_bfd_generic_link_add_one_symbol
3938 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
3939 (bfd_vma) 0, (const char *) NULL, false,
3940 get_elf_backend_data (abfd)->collect,
3941 (struct bfd_link_hash_entry **) &h)))
3942 return false;
3943 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
3944 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3945 h->type = STT_SECTION;
3946
3947 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3948 return false;
3949 }
3950
3951 /* We need to create a .compact_rel section. */
3952 if (SGI_COMPAT (abfd))
3953 {
3954 if (!mips_elf_create_compact_rel_section (abfd, info))
3955 return false;
3956 }
3957
3958 /* Change aligments of some sections. */
3959 s = bfd_get_section_by_name (abfd, ".hash");
3960 if (s != NULL)
3961 bfd_set_section_alignment (abfd, s, 4);
3962 s = bfd_get_section_by_name (abfd, ".dynsym");
3963 if (s != NULL)
3964 bfd_set_section_alignment (abfd, s, 4);
3965 s = bfd_get_section_by_name (abfd, ".dynstr");
3966 if (s != NULL)
3967 bfd_set_section_alignment (abfd, s, 4);
3968 s = bfd_get_section_by_name (abfd, ".reginfo");
3969 if (s != NULL)
3970 bfd_set_section_alignment (abfd, s, 4);
3971 s = bfd_get_section_by_name (abfd, ".dynamic");
3972 if (s != NULL)
3973 bfd_set_section_alignment (abfd, s, 4);
3974 }
3975
3976 if (!info->shared)
3977 {
3978 h = NULL;
3979 if (SGI_COMPAT (abfd))
3980 {
3981 if (!(_bfd_generic_link_add_one_symbol
3982 (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
3983 (bfd_vma) 0, (const char *) NULL, false,
3984 get_elf_backend_data (abfd)->collect,
3985 (struct bfd_link_hash_entry **) &h)))
3986 return false;
3987 }
3988 else
3989 {
3990 /* For normal mips it is _DYNAMIC_LINKING. */
3991 if (!(_bfd_generic_link_add_one_symbol
3992 (info, abfd, "_DYNAMIC_LINKING", BSF_GLOBAL,
3993 bfd_abs_section_ptr, (bfd_vma) 0, (const char *) NULL, false,
3994 get_elf_backend_data (abfd)->collect,
3995 (struct bfd_link_hash_entry **) &h)))
3996 return false;
3997 }
3998 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
3999 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4000 h->type = STT_SECTION;
4001
4002 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4003 return false;
4004
4005 if (! mips_elf_hash_table (info)->use_rld_obj_head)
4006 {
4007 /* __rld_map is a four byte word located in the .data section
4008 and is filled in by the rtld to contain a pointer to
4009 the _r_debug structure. Its symbol value will be set in
4010 _bfd_mips_elf_finish_dynamic_symbol. */
4011 s = bfd_get_section_by_name (abfd, ".rld_map");
4012 BFD_ASSERT (s != NULL);
4013
4014 h = NULL;
4015 if (SGI_COMPAT (abfd))
4016 {
4017 if (!(_bfd_generic_link_add_one_symbol
4018 (info, abfd, "__rld_map", BSF_GLOBAL, s,
4019 (bfd_vma) 0, (const char *) NULL, false,
4020 get_elf_backend_data (abfd)->collect,
4021 (struct bfd_link_hash_entry **) &h)))
4022 return false;
4023 }
4024 else
4025 {
4026 /* For normal mips the symbol is __RLD_MAP. */
4027 if (!(_bfd_generic_link_add_one_symbol
4028 (info, abfd, "__RLD_MAP", BSF_GLOBAL, s,
4029 (bfd_vma) 0, (const char *) NULL, false,
4030 get_elf_backend_data (abfd)->collect,
4031 (struct bfd_link_hash_entry **) &h)))
4032 return false;
4033 }
4034 h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
4035 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4036 h->type = STT_OBJECT;
4037
4038 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
4039 return false;
4040 }
4041 }
4042
4043 return true;
4044}
4045\f
4046/* Look through the relocs for a section during the first phase, and
4047 allocate space in the global offset table. */
4048
4049boolean
4050_bfd_mips_elf_check_relocs (abfd, info, sec, relocs)
4051 bfd *abfd;
4052 struct bfd_link_info *info;
4053 asection *sec;
4054 const Elf_Internal_Rela *relocs;
4055{
4056 const char *name;
4057 bfd *dynobj;
4058 Elf_Internal_Shdr *symtab_hdr;
4059 struct elf_link_hash_entry **sym_hashes;
4060 struct mips_got_info *g;
4061 size_t extsymoff;
4062 const Elf_Internal_Rela *rel;
4063 const Elf_Internal_Rela *rel_end;
4064 asection *sgot;
4065 asection *sreloc;
4066 struct elf_backend_data *bed;
4067
4068 if (info->relocateable)
4069 return true;
4070
4071 dynobj = elf_hash_table (info)->dynobj;
4072 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4073 sym_hashes = elf_sym_hashes (abfd);
4074 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
4075
4076 /* Check for the mips16 stub sections. */
4077
4078 name = bfd_get_section_name (abfd, sec);
4079 if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
4080 {
4081 unsigned long r_symndx;
4082
4083 /* Look at the relocation information to figure out which symbol
4084 this is for. */
4085
4086 r_symndx = ELF_R_SYM (abfd, relocs->r_info);
4087
4088 if (r_symndx < extsymoff
4089 || sym_hashes[r_symndx - extsymoff] == NULL)
4090 {
4091 asection *o;
4092
4093 /* This stub is for a local symbol. This stub will only be
4094 needed if there is some relocation in this BFD, other
4095 than a 16 bit function call, which refers to this symbol. */
4096 for (o = abfd->sections; o != NULL; o = o->next)
4097 {
4098 Elf_Internal_Rela *sec_relocs;
4099 const Elf_Internal_Rela *r, *rend;
4100
4101 /* We can ignore stub sections when looking for relocs. */
4102 if ((o->flags & SEC_RELOC) == 0
4103 || o->reloc_count == 0
4104 || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
4105 sizeof FN_STUB - 1) == 0
4106 || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
4107 sizeof CALL_STUB - 1) == 0
4108 || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
4109 sizeof CALL_FP_STUB - 1) == 0)
4110 continue;
4111
4112 sec_relocs = (_bfd_elf32_link_read_relocs
4113 (abfd, o, (PTR) NULL,
4114 (Elf_Internal_Rela *) NULL,
4115 info->keep_memory));
4116 if (sec_relocs == NULL)
4117 return false;
4118
4119 rend = sec_relocs + o->reloc_count;
4120 for (r = sec_relocs; r < rend; r++)
4121 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
4122 && ELF_R_TYPE (abfd, r->r_info) != R_MIPS16_26)
4123 break;
4124
6cdc0ccc 4125 if (elf_section_data (o)->relocs != sec_relocs)
b49e97c9
TS
4126 free (sec_relocs);
4127
4128 if (r < rend)
4129 break;
4130 }
4131
4132 if (o == NULL)
4133 {
4134 /* There is no non-call reloc for this stub, so we do
4135 not need it. Since this function is called before
4136 the linker maps input sections to output sections, we
4137 can easily discard it by setting the SEC_EXCLUDE
4138 flag. */
4139 sec->flags |= SEC_EXCLUDE;
4140 return true;
4141 }
4142
4143 /* Record this stub in an array of local symbol stubs for
4144 this BFD. */
4145 if (elf_tdata (abfd)->local_stubs == NULL)
4146 {
4147 unsigned long symcount;
4148 asection **n;
4149 bfd_size_type amt;
4150
4151 if (elf_bad_symtab (abfd))
4152 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
4153 else
4154 symcount = symtab_hdr->sh_info;
4155 amt = symcount * sizeof (asection *);
4156 n = (asection **) bfd_zalloc (abfd, amt);
4157 if (n == NULL)
4158 return false;
4159 elf_tdata (abfd)->local_stubs = n;
4160 }
4161
4162 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
4163
4164 /* We don't need to set mips16_stubs_seen in this case.
4165 That flag is used to see whether we need to look through
4166 the global symbol table for stubs. We don't need to set
4167 it here, because we just have a local stub. */
4168 }
4169 else
4170 {
4171 struct mips_elf_link_hash_entry *h;
4172
4173 h = ((struct mips_elf_link_hash_entry *)
4174 sym_hashes[r_symndx - extsymoff]);
4175
4176 /* H is the symbol this stub is for. */
4177
4178 h->fn_stub = sec;
4179 mips_elf_hash_table (info)->mips16_stubs_seen = true;
4180 }
4181 }
4182 else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
4183 || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
4184 {
4185 unsigned long r_symndx;
4186 struct mips_elf_link_hash_entry *h;
4187 asection **loc;
4188
4189 /* Look at the relocation information to figure out which symbol
4190 this is for. */
4191
4192 r_symndx = ELF_R_SYM (abfd, relocs->r_info);
4193
4194 if (r_symndx < extsymoff
4195 || sym_hashes[r_symndx - extsymoff] == NULL)
4196 {
4197 /* This stub was actually built for a static symbol defined
4198 in the same file. We assume that all static symbols in
4199 mips16 code are themselves mips16, so we can simply
4200 discard this stub. Since this function is called before
4201 the linker maps input sections to output sections, we can
4202 easily discard it by setting the SEC_EXCLUDE flag. */
4203 sec->flags |= SEC_EXCLUDE;
4204 return true;
4205 }
4206
4207 h = ((struct mips_elf_link_hash_entry *)
4208 sym_hashes[r_symndx - extsymoff]);
4209
4210 /* H is the symbol this stub is for. */
4211
4212 if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
4213 loc = &h->call_fp_stub;
4214 else
4215 loc = &h->call_stub;
4216
4217 /* If we already have an appropriate stub for this function, we
4218 don't need another one, so we can discard this one. Since
4219 this function is called before the linker maps input sections
4220 to output sections, we can easily discard it by setting the
4221 SEC_EXCLUDE flag. We can also discard this section if we
4222 happen to already know that this is a mips16 function; it is
4223 not necessary to check this here, as it is checked later, but
4224 it is slightly faster to check now. */
4225 if (*loc != NULL || h->root.other == STO_MIPS16)
4226 {
4227 sec->flags |= SEC_EXCLUDE;
4228 return true;
4229 }
4230
4231 *loc = sec;
4232 mips_elf_hash_table (info)->mips16_stubs_seen = true;
4233 }
4234
4235 if (dynobj == NULL)
4236 {
4237 sgot = NULL;
4238 g = NULL;
4239 }
4240 else
4241 {
4242 sgot = mips_elf_got_section (dynobj);
4243 if (sgot == NULL)
4244 g = NULL;
4245 else
4246 {
4247 BFD_ASSERT (elf_section_data (sgot) != NULL);
4248 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4249 BFD_ASSERT (g != NULL);
4250 }
4251 }
4252
4253 sreloc = NULL;
4254 bed = get_elf_backend_data (abfd);
4255 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
4256 for (rel = relocs; rel < rel_end; ++rel)
4257 {
4258 unsigned long r_symndx;
4259 unsigned int r_type;
4260 struct elf_link_hash_entry *h;
4261
4262 r_symndx = ELF_R_SYM (abfd, rel->r_info);
4263 r_type = ELF_R_TYPE (abfd, rel->r_info);
4264
4265 if (r_symndx < extsymoff)
4266 h = NULL;
4267 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
4268 {
4269 (*_bfd_error_handler)
4270 (_("%s: Malformed reloc detected for section %s"),
4271 bfd_archive_filename (abfd), name);
4272 bfd_set_error (bfd_error_bad_value);
4273 return false;
4274 }
4275 else
4276 {
4277 h = sym_hashes[r_symndx - extsymoff];
4278
4279 /* This may be an indirect symbol created because of a version. */
4280 if (h != NULL)
4281 {
4282 while (h->root.type == bfd_link_hash_indirect)
4283 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4284 }
4285 }
4286
4287 /* Some relocs require a global offset table. */
4288 if (dynobj == NULL || sgot == NULL)
4289 {
4290 switch (r_type)
4291 {
4292 case R_MIPS_GOT16:
4293 case R_MIPS_CALL16:
4294 case R_MIPS_CALL_HI16:
4295 case R_MIPS_CALL_LO16:
4296 case R_MIPS_GOT_HI16:
4297 case R_MIPS_GOT_LO16:
4298 case R_MIPS_GOT_PAGE:
4299 case R_MIPS_GOT_OFST:
4300 case R_MIPS_GOT_DISP:
4301 if (dynobj == NULL)
4302 elf_hash_table (info)->dynobj = dynobj = abfd;
4303 if (! mips_elf_create_got_section (dynobj, info))
4304 return false;
4305 g = mips_elf_got_info (dynobj, &sgot);
4306 break;
4307
4308 case R_MIPS_32:
4309 case R_MIPS_REL32:
4310 case R_MIPS_64:
4311 if (dynobj == NULL
4312 && (info->shared || h != NULL)
4313 && (sec->flags & SEC_ALLOC) != 0)
4314 elf_hash_table (info)->dynobj = dynobj = abfd;
4315 break;
4316
4317 default:
4318 break;
4319 }
4320 }
4321
4322 if (!h && (r_type == R_MIPS_CALL_LO16
4323 || r_type == R_MIPS_GOT_LO16
4324 || r_type == R_MIPS_GOT_DISP))
4325 {
4326 /* We may need a local GOT entry for this relocation. We
4327 don't count R_MIPS_GOT_PAGE because we can estimate the
4328 maximum number of pages needed by looking at the size of
4329 the segment. Similar comments apply to R_MIPS_GOT16 and
4330 R_MIPS_CALL16. We don't count R_MIPS_GOT_HI16, or
4331 R_MIPS_CALL_HI16 because these are always followed by an
4332 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
4333
4334 This estimation is very conservative since we can merge
4335 duplicate entries in the GOT. In order to be less
4336 conservative, we could actually build the GOT here,
4337 rather than in relocate_section. */
4338 g->local_gotno++;
4339 sgot->_raw_size += MIPS_ELF_GOT_SIZE (dynobj);
4340 }
4341
4342 switch (r_type)
4343 {
4344 case R_MIPS_CALL16:
4345 if (h == NULL)
4346 {
4347 (*_bfd_error_handler)
4348 (_("%s: CALL16 reloc at 0x%lx not against global symbol"),
4349 bfd_archive_filename (abfd), (unsigned long) rel->r_offset);
4350 bfd_set_error (bfd_error_bad_value);
4351 return false;
4352 }
4353 /* Fall through. */
4354
4355 case R_MIPS_CALL_HI16:
4356 case R_MIPS_CALL_LO16:
4357 if (h != NULL)
4358 {
4359 /* This symbol requires a global offset table entry. */
4360 if (! mips_elf_record_global_got_symbol (h, info, g))
4361 return false;
4362
4363 /* We need a stub, not a plt entry for the undefined
4364 function. But we record it as if it needs plt. See
4365 elf_adjust_dynamic_symbol in elflink.h. */
4366 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4367 h->type = STT_FUNC;
4368 }
4369 break;
4370
4371 case R_MIPS_GOT16:
4372 case R_MIPS_GOT_HI16:
4373 case R_MIPS_GOT_LO16:
4374 case R_MIPS_GOT_DISP:
4375 /* This symbol requires a global offset table entry. */
4376 if (h && ! mips_elf_record_global_got_symbol (h, info, g))
4377 return false;
4378 break;
4379
4380 case R_MIPS_32:
4381 case R_MIPS_REL32:
4382 case R_MIPS_64:
4383 if ((info->shared || h != NULL)
4384 && (sec->flags & SEC_ALLOC) != 0)
4385 {
4386 if (sreloc == NULL)
4387 {
4388 const char *dname = ".rel.dyn";
4389
4390 sreloc = bfd_get_section_by_name (dynobj, dname);
4391 if (sreloc == NULL)
4392 {
4393 sreloc = bfd_make_section (dynobj, dname);
4394 if (sreloc == NULL
4395 || ! bfd_set_section_flags (dynobj, sreloc,
4396 (SEC_ALLOC
4397 | SEC_LOAD
4398 | SEC_HAS_CONTENTS
4399 | SEC_IN_MEMORY
4400 | SEC_LINKER_CREATED
4401 | SEC_READONLY))
4402 || ! bfd_set_section_alignment (dynobj, sreloc,
4403 4))
4404 return false;
4405 }
4406 }
4407#define MIPS_READONLY_SECTION (SEC_ALLOC | SEC_LOAD | SEC_READONLY)
4408 if (info->shared)
4409 {
4410 /* When creating a shared object, we must copy these
4411 reloc types into the output file as R_MIPS_REL32
4412 relocs. We make room for this reloc in the
4413 .rel.dyn reloc section. */
4414 mips_elf_allocate_dynamic_relocations (dynobj, 1);
4415 if ((sec->flags & MIPS_READONLY_SECTION)
4416 == MIPS_READONLY_SECTION)
4417 /* We tell the dynamic linker that there are
4418 relocations against the text segment. */
4419 info->flags |= DF_TEXTREL;
4420 }
4421 else
4422 {
4423 struct mips_elf_link_hash_entry *hmips;
4424
4425 /* We only need to copy this reloc if the symbol is
4426 defined in a dynamic object. */
4427 hmips = (struct mips_elf_link_hash_entry *) h;
4428 ++hmips->possibly_dynamic_relocs;
4429 if ((sec->flags & MIPS_READONLY_SECTION)
4430 == MIPS_READONLY_SECTION)
4431 /* We need it to tell the dynamic linker if there
4432 are relocations against the text segment. */
4433 hmips->readonly_reloc = true;
4434 }
4435
4436 /* Even though we don't directly need a GOT entry for
4437 this symbol, a symbol must have a dynamic symbol
4438 table index greater that DT_MIPS_GOTSYM if there are
4439 dynamic relocations against it. */
4440 if (h != NULL
4441 && ! mips_elf_record_global_got_symbol (h, info, g))
4442 return false;
4443 }
4444
4445 if (SGI_COMPAT (abfd))
4446 mips_elf_hash_table (info)->compact_rel_size +=
4447 sizeof (Elf32_External_crinfo);
4448 break;
4449
4450 case R_MIPS_26:
4451 case R_MIPS_GPREL16:
4452 case R_MIPS_LITERAL:
4453 case R_MIPS_GPREL32:
4454 if (SGI_COMPAT (abfd))
4455 mips_elf_hash_table (info)->compact_rel_size +=
4456 sizeof (Elf32_External_crinfo);
4457 break;
4458
4459 /* This relocation describes the C++ object vtable hierarchy.
4460 Reconstruct it for later use during GC. */
4461 case R_MIPS_GNU_VTINHERIT:
4462 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4463 return false;
4464 break;
4465
4466 /* This relocation describes which C++ vtable entries are actually
4467 used. Record for later use during GC. */
4468 case R_MIPS_GNU_VTENTRY:
4469 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
4470 return false;
4471 break;
4472
4473 default:
4474 break;
4475 }
4476
4477 /* We must not create a stub for a symbol that has relocations
4478 related to taking the function's address. */
4479 switch (r_type)
4480 {
4481 default:
4482 if (h != NULL)
4483 {
4484 struct mips_elf_link_hash_entry *mh;
4485
4486 mh = (struct mips_elf_link_hash_entry *) h;
4487 mh->no_fn_stub = true;
4488 }
4489 break;
4490 case R_MIPS_CALL16:
4491 case R_MIPS_CALL_HI16:
4492 case R_MIPS_CALL_LO16:
4493 break;
4494 }
4495
4496 /* If this reloc is not a 16 bit call, and it has a global
4497 symbol, then we will need the fn_stub if there is one.
4498 References from a stub section do not count. */
4499 if (h != NULL
4500 && r_type != R_MIPS16_26
4501 && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
4502 sizeof FN_STUB - 1) != 0
4503 && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
4504 sizeof CALL_STUB - 1) != 0
4505 && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
4506 sizeof CALL_FP_STUB - 1) != 0)
4507 {
4508 struct mips_elf_link_hash_entry *mh;
4509
4510 mh = (struct mips_elf_link_hash_entry *) h;
4511 mh->need_fn_stub = true;
4512 }
4513 }
4514
4515 return true;
4516}
4517\f
4518/* Adjust a symbol defined by a dynamic object and referenced by a
4519 regular object. The current definition is in some section of the
4520 dynamic object, but we're not including those sections. We have to
4521 change the definition to something the rest of the link can
4522 understand. */
4523
4524boolean
4525_bfd_mips_elf_adjust_dynamic_symbol (info, h)
4526 struct bfd_link_info *info;
4527 struct elf_link_hash_entry *h;
4528{
4529 bfd *dynobj;
4530 struct mips_elf_link_hash_entry *hmips;
4531 asection *s;
4532
4533 dynobj = elf_hash_table (info)->dynobj;
4534
4535 /* Make sure we know what is going on here. */
4536 BFD_ASSERT (dynobj != NULL
4537 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
4538 || h->weakdef != NULL
4539 || ((h->elf_link_hash_flags
4540 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4541 && (h->elf_link_hash_flags
4542 & ELF_LINK_HASH_REF_REGULAR) != 0
4543 && (h->elf_link_hash_flags
4544 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
4545
4546 /* If this symbol is defined in a dynamic object, we need to copy
4547 any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
4548 file. */
4549 hmips = (struct mips_elf_link_hash_entry *) h;
4550 if (! info->relocateable
4551 && hmips->possibly_dynamic_relocs != 0
4552 && (h->root.type == bfd_link_hash_defweak
4553 || (h->elf_link_hash_flags
4554 & ELF_LINK_HASH_DEF_REGULAR) == 0))
4555 {
4556 mips_elf_allocate_dynamic_relocations (dynobj,
4557 hmips->possibly_dynamic_relocs);
4558 if (hmips->readonly_reloc)
4559 /* We tell the dynamic linker that there are relocations
4560 against the text segment. */
4561 info->flags |= DF_TEXTREL;
4562 }
4563
4564 /* For a function, create a stub, if allowed. */
4565 if (! hmips->no_fn_stub
4566 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4567 {
4568 if (! elf_hash_table (info)->dynamic_sections_created)
4569 return true;
4570
4571 /* If this symbol is not defined in a regular file, then set
4572 the symbol to the stub location. This is required to make
4573 function pointers compare as equal between the normal
4574 executable and the shared library. */
4575 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4576 {
4577 /* We need .stub section. */
4578 s = bfd_get_section_by_name (dynobj,
4579 MIPS_ELF_STUB_SECTION_NAME (dynobj));
4580 BFD_ASSERT (s != NULL);
4581
4582 h->root.u.def.section = s;
4583 h->root.u.def.value = s->_raw_size;
4584
4585 /* XXX Write this stub address somewhere. */
4586 h->plt.offset = s->_raw_size;
4587
4588 /* Make room for this stub code. */
4589 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
4590
4591 /* The last half word of the stub will be filled with the index
4592 of this symbol in .dynsym section. */
4593 return true;
4594 }
4595 }
4596 else if ((h->type == STT_FUNC)
4597 && (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0)
4598 {
4599 /* This will set the entry for this symbol in the GOT to 0, and
4600 the dynamic linker will take care of this. */
4601 h->root.u.def.value = 0;
4602 return true;
4603 }
4604
4605 /* If this is a weak symbol, and there is a real definition, the
4606 processor independent code will have arranged for us to see the
4607 real definition first, and we can just use the same value. */
4608 if (h->weakdef != NULL)
4609 {
4610 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
4611 || h->weakdef->root.type == bfd_link_hash_defweak);
4612 h->root.u.def.section = h->weakdef->root.u.def.section;
4613 h->root.u.def.value = h->weakdef->root.u.def.value;
4614 return true;
4615 }
4616
4617 /* This is a reference to a symbol defined by a dynamic object which
4618 is not a function. */
4619
4620 return true;
4621}
4622\f
4623/* This function is called after all the input files have been read,
4624 and the input sections have been assigned to output sections. We
4625 check for any mips16 stub sections that we can discard. */
4626
4627boolean
4628_bfd_mips_elf_always_size_sections (output_bfd, info)
4629 bfd *output_bfd;
4630 struct bfd_link_info *info;
4631{
4632 asection *ri;
4633
4634 /* The .reginfo section has a fixed size. */
4635 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
4636 if (ri != NULL)
4637 bfd_set_section_size (output_bfd, ri,
4638 (bfd_size_type) sizeof (Elf32_External_RegInfo));
4639
4640 if (info->relocateable
4641 || ! mips_elf_hash_table (info)->mips16_stubs_seen)
4642 return true;
4643
4644 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4645 mips_elf_check_mips16_stubs,
4646 (PTR) NULL);
4647
4648 return true;
4649}
4650
4651/* Set the sizes of the dynamic sections. */
4652
4653boolean
4654_bfd_mips_elf_size_dynamic_sections (output_bfd, info)
4655 bfd *output_bfd;
4656 struct bfd_link_info *info;
4657{
4658 bfd *dynobj;
4659 asection *s;
4660 boolean reltext;
4661 struct mips_got_info *g = NULL;
4662
4663 dynobj = elf_hash_table (info)->dynobj;
4664 BFD_ASSERT (dynobj != NULL);
4665
4666 if (elf_hash_table (info)->dynamic_sections_created)
4667 {
4668 /* Set the contents of the .interp section to the interpreter. */
4669 if (! info->shared)
4670 {
4671 s = bfd_get_section_by_name (dynobj, ".interp");
4672 BFD_ASSERT (s != NULL);
4673 s->_raw_size
4674 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
4675 s->contents
4676 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
4677 }
4678 }
4679
4680 /* The check_relocs and adjust_dynamic_symbol entry points have
4681 determined the sizes of the various dynamic sections. Allocate
4682 memory for them. */
4683 reltext = false;
4684 for (s = dynobj->sections; s != NULL; s = s->next)
4685 {
4686 const char *name;
4687 boolean strip;
4688
4689 /* It's OK to base decisions on the section name, because none
4690 of the dynobj section names depend upon the input files. */
4691 name = bfd_get_section_name (dynobj, s);
4692
4693 if ((s->flags & SEC_LINKER_CREATED) == 0)
4694 continue;
4695
4696 strip = false;
4697
4698 if (strncmp (name, ".rel", 4) == 0)
4699 {
4700 if (s->_raw_size == 0)
4701 {
4702 /* We only strip the section if the output section name
4703 has the same name. Otherwise, there might be several
4704 input sections for this output section. FIXME: This
4705 code is probably not needed these days anyhow, since
4706 the linker now does not create empty output sections. */
4707 if (s->output_section != NULL
4708 && strcmp (name,
4709 bfd_get_section_name (s->output_section->owner,
4710 s->output_section)) == 0)
4711 strip = true;
4712 }
4713 else
4714 {
4715 const char *outname;
4716 asection *target;
4717
4718 /* If this relocation section applies to a read only
4719 section, then we probably need a DT_TEXTREL entry.
4720 If the relocation section is .rel.dyn, we always
4721 assert a DT_TEXTREL entry rather than testing whether
4722 there exists a relocation to a read only section or
4723 not. */
4724 outname = bfd_get_section_name (output_bfd,
4725 s->output_section);
4726 target = bfd_get_section_by_name (output_bfd, outname + 4);
4727 if ((target != NULL
4728 && (target->flags & SEC_READONLY) != 0
4729 && (target->flags & SEC_ALLOC) != 0)
4730 || strcmp (outname, ".rel.dyn") == 0)
4731 reltext = true;
4732
4733 /* We use the reloc_count field as a counter if we need
4734 to copy relocs into the output file. */
4735 if (strcmp (name, ".rel.dyn") != 0)
4736 s->reloc_count = 0;
4737 }
4738 }
4739 else if (strncmp (name, ".got", 4) == 0)
4740 {
4741 int i;
4742 bfd_size_type loadable_size = 0;
4743 bfd_size_type local_gotno;
4744 bfd *sub;
4745
4746 BFD_ASSERT (elf_section_data (s) != NULL);
4747 g = (struct mips_got_info *) elf_section_data (s)->tdata;
4748 BFD_ASSERT (g != NULL);
4749
4750 /* Calculate the total loadable size of the output. That
4751 will give us the maximum number of GOT_PAGE entries
4752 required. */
4753 for (sub = info->input_bfds; sub; sub = sub->link_next)
4754 {
4755 asection *subsection;
4756
4757 for (subsection = sub->sections;
4758 subsection;
4759 subsection = subsection->next)
4760 {
4761 if ((subsection->flags & SEC_ALLOC) == 0)
4762 continue;
4763 loadable_size += ((subsection->_raw_size + 0xf)
4764 &~ (bfd_size_type) 0xf);
4765 }
4766 }
4767 loadable_size += MIPS_FUNCTION_STUB_SIZE;
4768
4769 /* Assume there are two loadable segments consisting of
4770 contiguous sections. Is 5 enough? */
4771 local_gotno = (loadable_size >> 16) + 5;
4a14403c 4772 if (NEWABI_P (output_bfd))
b49e97c9
TS
4773 /* It's possible we will need GOT_PAGE entries as well as
4774 GOT16 entries. Often, these will be able to share GOT
4775 entries, but not always. */
4776 local_gotno *= 2;
4777
4778 g->local_gotno += local_gotno;
4779 s->_raw_size += local_gotno * MIPS_ELF_GOT_SIZE (dynobj);
4780
4781 /* There has to be a global GOT entry for every symbol with
4782 a dynamic symbol table index of DT_MIPS_GOTSYM or
4783 higher. Therefore, it make sense to put those symbols
4784 that need GOT entries at the end of the symbol table. We
4785 do that here. */
4786 if (! mips_elf_sort_hash_table (info, 1))
4787 return false;
4788
4789 if (g->global_gotsym != NULL)
4790 i = elf_hash_table (info)->dynsymcount - g->global_gotsym->dynindx;
4791 else
4792 /* If there are no global symbols, or none requiring
4793 relocations, then GLOBAL_GOTSYM will be NULL. */
4794 i = 0;
4795 g->global_gotno = i;
4796 s->_raw_size += i * MIPS_ELF_GOT_SIZE (dynobj);
4797 }
4798 else if (strcmp (name, MIPS_ELF_STUB_SECTION_NAME (output_bfd)) == 0)
4799 {
8dc1a139 4800 /* IRIX rld assumes that the function stub isn't at the end
b49e97c9
TS
4801 of .text section. So put a dummy. XXX */
4802 s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
4803 }
4804 else if (! info->shared
4805 && ! mips_elf_hash_table (info)->use_rld_obj_head
4806 && strncmp (name, ".rld_map", 8) == 0)
4807 {
4808 /* We add a room for __rld_map. It will be filled in by the
4809 rtld to contain a pointer to the _r_debug structure. */
4810 s->_raw_size += 4;
4811 }
4812 else if (SGI_COMPAT (output_bfd)
4813 && strncmp (name, ".compact_rel", 12) == 0)
4814 s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
4815 else if (strcmp (name, ".msym") == 0)
4816 s->_raw_size = (sizeof (Elf32_External_Msym)
4817 * (elf_hash_table (info)->dynsymcount
4818 + bfd_count_sections (output_bfd)));
4819 else if (strncmp (name, ".init", 5) != 0)
4820 {
4821 /* It's not one of our sections, so don't allocate space. */
4822 continue;
4823 }
4824
4825 if (strip)
4826 {
4827 _bfd_strip_section_from_output (info, s);
4828 continue;
4829 }
4830
4831 /* Allocate memory for the section contents. */
4832 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
4833 if (s->contents == NULL && s->_raw_size != 0)
4834 {
4835 bfd_set_error (bfd_error_no_memory);
4836 return false;
4837 }
4838 }
4839
4840 if (elf_hash_table (info)->dynamic_sections_created)
4841 {
4842 /* Add some entries to the .dynamic section. We fill in the
4843 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
4844 must add the entries now so that we get the correct size for
4845 the .dynamic section. The DT_DEBUG entry is filled in by the
4846 dynamic linker and used by the debugger. */
4847 if (! info->shared)
4848 {
4849 /* SGI object has the equivalence of DT_DEBUG in the
4850 DT_MIPS_RLD_MAP entry. */
4851 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
4852 return false;
4853 if (!SGI_COMPAT (output_bfd))
4854 {
4855 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
4856 return false;
4857 }
4858 }
4859 else
4860 {
4861 /* Shared libraries on traditional mips have DT_DEBUG. */
4862 if (!SGI_COMPAT (output_bfd))
4863 {
4864 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
4865 return false;
4866 }
4867 }
4868
4869 if (reltext && SGI_COMPAT (output_bfd))
4870 info->flags |= DF_TEXTREL;
4871
4872 if ((info->flags & DF_TEXTREL) != 0)
4873 {
4874 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
4875 return false;
4876 }
4877
4878 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
4879 return false;
4880
4881 if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
4882 {
4883 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
4884 return false;
4885
4886 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
4887 return false;
4888
4889 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
4890 return false;
4891 }
4892
4893 if (SGI_COMPAT (output_bfd))
4894 {
4895 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICTNO, 0))
4896 return false;
4897 }
4898
4899 if (SGI_COMPAT (output_bfd))
4900 {
4901 if (!MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLISTNO, 0))
4902 return false;
4903 }
4904
4905 if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
4906 {
4907 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_CONFLICT, 0))
4908 return false;
4909
4910 s = bfd_get_section_by_name (dynobj, ".liblist");
4911 BFD_ASSERT (s != NULL);
4912
4913 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LIBLIST, 0))
4914 return false;
4915 }
4916
4917 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
4918 return false;
4919
4920 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
4921 return false;
4922
4923#if 0
4924 /* Time stamps in executable files are a bad idea. */
4925 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_TIME_STAMP, 0))
4926 return false;
4927#endif
4928
4929#if 0 /* FIXME */
4930 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_ICHECKSUM, 0))
4931 return false;
4932#endif
4933
4934#if 0 /* FIXME */
4935 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_IVERSION, 0))
4936 return false;
4937#endif
4938
4939 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
4940 return false;
4941
4942 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
4943 return false;
4944
4945 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
4946 return false;
4947
4948 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
4949 return false;
4950
4951 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
4952 return false;
4953
4954 if (IRIX_COMPAT (dynobj) == ict_irix5
4955 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
4956 return false;
4957
4958 if (IRIX_COMPAT (dynobj) == ict_irix6
4959 && (bfd_get_section_by_name
4960 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
4961 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
4962 return false;
4963
4964 if (bfd_get_section_by_name (dynobj, ".msym")
4965 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_MSYM, 0))
4966 return false;
4967 }
4968
4969 return true;
4970}
4971\f
4972/* Relocate a MIPS ELF section. */
4973
4974boolean
4975_bfd_mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4976 contents, relocs, local_syms, local_sections)
4977 bfd *output_bfd;
4978 struct bfd_link_info *info;
4979 bfd *input_bfd;
4980 asection *input_section;
4981 bfd_byte *contents;
4982 Elf_Internal_Rela *relocs;
4983 Elf_Internal_Sym *local_syms;
4984 asection **local_sections;
4985{
4986 Elf_Internal_Rela *rel;
4987 const Elf_Internal_Rela *relend;
4988 bfd_vma addend = 0;
4989 boolean use_saved_addend_p = false;
4990 struct elf_backend_data *bed;
4991
4992 bed = get_elf_backend_data (output_bfd);
4993 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
4994 for (rel = relocs; rel < relend; ++rel)
4995 {
4996 const char *name;
4997 bfd_vma value;
4998 reloc_howto_type *howto;
4999 boolean require_jalx;
5000 /* True if the relocation is a RELA relocation, rather than a
5001 REL relocation. */
5002 boolean rela_relocation_p = true;
5003 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5004 const char * msg = (const char *) NULL;
5005
5006 /* Find the relocation howto for this relocation. */
4a14403c 5007 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
b49e97c9
TS
5008 {
5009 /* Some 32-bit code uses R_MIPS_64. In particular, people use
5010 64-bit code, but make sure all their addresses are in the
5011 lowermost or uppermost 32-bit section of the 64-bit address
5012 space. Thus, when they use an R_MIPS_64 they mean what is
5013 usually meant by R_MIPS_32, with the exception that the
5014 stored value is sign-extended to 64 bits. */
5a659663 5015 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
b49e97c9
TS
5016
5017 /* On big-endian systems, we need to lie about the position
5018 of the reloc. */
5019 if (bfd_big_endian (input_bfd))
5020 rel->r_offset += 4;
5021 }
5022 else
5023 /* NewABI defaults to RELA relocations. */
5024 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
5025 NEWABI_P (input_bfd));
5026
5027 if (!use_saved_addend_p)
5028 {
5029 Elf_Internal_Shdr *rel_hdr;
5030
5031 /* If these relocations were originally of the REL variety,
5032 we must pull the addend out of the field that will be
5033 relocated. Otherwise, we simply use the contents of the
5034 RELA relocation. To determine which flavor or relocation
5035 this is, we depend on the fact that the INPUT_SECTION's
5036 REL_HDR is read before its REL_HDR2. */
5037 rel_hdr = &elf_section_data (input_section)->rel_hdr;
5038 if ((size_t) (rel - relocs)
5039 >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
5040 rel_hdr = elf_section_data (input_section)->rel_hdr2;
5041 if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd))
5042 {
5043 /* Note that this is a REL relocation. */
5044 rela_relocation_p = false;
5045
5046 /* Get the addend, which is stored in the input file. */
5047 addend = mips_elf_obtain_contents (howto, rel, input_bfd,
5048 contents);
5049 addend &= howto->src_mask;
5a659663 5050 addend <<= howto->rightshift;
b49e97c9
TS
5051
5052 /* For some kinds of relocations, the ADDEND is a
5053 combination of the addend stored in two different
5054 relocations. */
5055 if (r_type == R_MIPS_HI16
5056 || r_type == R_MIPS_GNU_REL_HI16
5057 || (r_type == R_MIPS_GOT16
5058 && mips_elf_local_relocation_p (input_bfd, rel,
5059 local_sections, false)))
5060 {
5061 bfd_vma l;
5062 const Elf_Internal_Rela *lo16_relocation;
5063 reloc_howto_type *lo16_howto;
5064 unsigned int lo;
5065
5066 /* The combined value is the sum of the HI16 addend,
5067 left-shifted by sixteen bits, and the LO16
5068 addend, sign extended. (Usually, the code does
5069 a `lui' of the HI16 value, and then an `addiu' of
5070 the LO16 value.)
5071
5072 Scan ahead to find a matching LO16 relocation. */
5073 if (r_type == R_MIPS_GNU_REL_HI16)
5074 lo = R_MIPS_GNU_REL_LO16;
5075 else
5076 lo = R_MIPS_LO16;
5077 lo16_relocation = mips_elf_next_relocation (input_bfd, lo,
5078 rel, relend);
5079 if (lo16_relocation == NULL)
5080 return false;
5081
5082 /* Obtain the addend kept there. */
5a659663 5083 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, lo, false);
b49e97c9
TS
5084 l = mips_elf_obtain_contents (lo16_howto, lo16_relocation,
5085 input_bfd, contents);
5086 l &= lo16_howto->src_mask;
5a659663 5087 l <<= lo16_howto->rightshift;
b49e97c9
TS
5088 l = mips_elf_sign_extend (l, 16);
5089
5090 addend <<= 16;
5091
5092 /* Compute the combined addend. */
5093 addend += l;
5094
5095 /* If PC-relative, subtract the difference between the
5096 address of the LO part of the reloc and the address of
5097 the HI part. The relocation is relative to the LO
5098 part, but mips_elf_calculate_relocation() doesn't
5099 know its address or the difference from the HI part, so
5100 we subtract that difference here. See also the
5101 comment in mips_elf_calculate_relocation(). */
5102 if (r_type == R_MIPS_GNU_REL_HI16)
5103 addend -= (lo16_relocation->r_offset - rel->r_offset);
5104 }
5105 else if (r_type == R_MIPS16_GPREL)
5106 {
5107 /* The addend is scrambled in the object file. See
5108 mips_elf_perform_relocation for details on the
5109 format. */
5110 addend = (((addend & 0x1f0000) >> 5)
5111 | ((addend & 0x7e00000) >> 16)
5112 | (addend & 0x1f));
5113 }
5114 }
5115 else
5116 addend = rel->r_addend;
5117 }
5118
5119 if (info->relocateable)
5120 {
5121 Elf_Internal_Sym *sym;
5122 unsigned long r_symndx;
5123
4a14403c 5124 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
b49e97c9
TS
5125 && bfd_big_endian (input_bfd))
5126 rel->r_offset -= 4;
5127
5128 /* Since we're just relocating, all we need to do is copy
5129 the relocations back out to the object file, unless
5130 they're against a section symbol, in which case we need
5131 to adjust by the section offset, or unless they're GP
5132 relative in which case we need to adjust by the amount
5133 that we're adjusting GP in this relocateable object. */
5134
5135 if (! mips_elf_local_relocation_p (input_bfd, rel, local_sections,
5136 false))
5137 /* There's nothing to do for non-local relocations. */
5138 continue;
5139
5140 if (r_type == R_MIPS16_GPREL
5141 || r_type == R_MIPS_GPREL16
5142 || r_type == R_MIPS_GPREL32
5143 || r_type == R_MIPS_LITERAL)
5144 addend -= (_bfd_get_gp_value (output_bfd)
5145 - _bfd_get_gp_value (input_bfd));
b49e97c9
TS
5146
5147 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
5148 sym = local_syms + r_symndx;
5149 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5150 /* Adjust the addend appropriately. */
5151 addend += local_sections[r_symndx]->output_offset;
5152
5a659663
TS
5153 if (howto->partial_inplace)
5154 {
5155 /* If the relocation is for a R_MIPS_HI16 or R_MIPS_GOT16,
5156 then we only want to write out the high-order 16 bits.
5157 The subsequent R_MIPS_LO16 will handle the low-order bits.
5158 */
5159 if (r_type == R_MIPS_HI16 || r_type == R_MIPS_GOT16
5160 || r_type == R_MIPS_GNU_REL_HI16)
5161 addend = mips_elf_high (addend);
5162 else if (r_type == R_MIPS_HIGHER)
5163 addend = mips_elf_higher (addend);
5164 else if (r_type == R_MIPS_HIGHEST)
5165 addend = mips_elf_highest (addend);
5166 }
b49e97c9
TS
5167
5168 if (rela_relocation_p)
5169 /* If this is a RELA relocation, just update the addend.
5170 We have to cast away constness for REL. */
5171 rel->r_addend = addend;
5172 else
5173 {
5174 /* Otherwise, we have to write the value back out. Note
5175 that we use the source mask, rather than the
5176 destination mask because the place to which we are
5177 writing will be source of the addend in the final
5178 link. */
5a659663 5179 addend >>= howto->rightshift;
b49e97c9
TS
5180 addend &= howto->src_mask;
5181
5a659663 5182 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
5183 /* See the comment above about using R_MIPS_64 in the 32-bit
5184 ABI. Here, we need to update the addend. It would be
5185 possible to get away with just using the R_MIPS_32 reloc
5186 but for endianness. */
5187 {
5188 bfd_vma sign_bits;
5189 bfd_vma low_bits;
5190 bfd_vma high_bits;
5191
5192 if (addend & ((bfd_vma) 1 << 31))
5193#ifdef BFD64
5194 sign_bits = ((bfd_vma) 1 << 32) - 1;
5195#else
5196 sign_bits = -1;
5197#endif
5198 else
5199 sign_bits = 0;
5200
5201 /* If we don't know that we have a 64-bit type,
5202 do two separate stores. */
5203 if (bfd_big_endian (input_bfd))
5204 {
5205 /* Store the sign-bits (which are most significant)
5206 first. */
5207 low_bits = sign_bits;
5208 high_bits = addend;
5209 }
5210 else
5211 {
5212 low_bits = addend;
5213 high_bits = sign_bits;
5214 }
5215 bfd_put_32 (input_bfd, low_bits,
5216 contents + rel->r_offset);
5217 bfd_put_32 (input_bfd, high_bits,
5218 contents + rel->r_offset + 4);
5219 continue;
5220 }
5221
5222 if (! mips_elf_perform_relocation (info, howto, rel, addend,
5223 input_bfd, input_section,
5224 contents, false))
5225 return false;
5226 }
5227
5228 /* Go on to the next relocation. */
5229 continue;
5230 }
5231
5232 /* In the N32 and 64-bit ABIs there may be multiple consecutive
5233 relocations for the same offset. In that case we are
5234 supposed to treat the output of each relocation as the addend
5235 for the next. */
5236 if (rel + 1 < relend
5237 && rel->r_offset == rel[1].r_offset
5238 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
5239 use_saved_addend_p = true;
5240 else
5241 use_saved_addend_p = false;
5242
5a659663
TS
5243 addend >>= howto->rightshift;
5244
b49e97c9
TS
5245 /* Figure out what value we are supposed to relocate. */
5246 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
5247 input_section, info, rel,
5248 addend, howto, local_syms,
5249 local_sections, &value,
5250 &name, &require_jalx))
5251 {
5252 case bfd_reloc_continue:
5253 /* There's nothing to do. */
5254 continue;
5255
5256 case bfd_reloc_undefined:
5257 /* mips_elf_calculate_relocation already called the
5258 undefined_symbol callback. There's no real point in
5259 trying to perform the relocation at this point, so we
5260 just skip ahead to the next relocation. */
5261 continue;
5262
5263 case bfd_reloc_notsupported:
5264 msg = _("internal error: unsupported relocation error");
5265 info->callbacks->warning
5266 (info, msg, name, input_bfd, input_section, rel->r_offset);
5267 return false;
5268
5269 case bfd_reloc_overflow:
5270 if (use_saved_addend_p)
5271 /* Ignore overflow until we reach the last relocation for
5272 a given location. */
5273 ;
5274 else
5275 {
5276 BFD_ASSERT (name != NULL);
5277 if (! ((*info->callbacks->reloc_overflow)
5278 (info, name, howto->name, (bfd_vma) 0,
5279 input_bfd, input_section, rel->r_offset)))
5280 return false;
5281 }
5282 break;
5283
5284 case bfd_reloc_ok:
5285 break;
5286
5287 default:
5288 abort ();
5289 break;
5290 }
5291
5292 /* If we've got another relocation for the address, keep going
5293 until we reach the last one. */
5294 if (use_saved_addend_p)
5295 {
5296 addend = value;
5297 continue;
5298 }
5299
4a14403c 5300 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
5301 /* See the comment above about using R_MIPS_64 in the 32-bit
5302 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
5303 that calculated the right value. Now, however, we
5304 sign-extend the 32-bit result to 64-bits, and store it as a
5305 64-bit value. We are especially generous here in that we
5306 go to extreme lengths to support this usage on systems with
5307 only a 32-bit VMA. */
5308 {
5309 bfd_vma sign_bits;
5310 bfd_vma low_bits;
5311 bfd_vma high_bits;
5312
5313 if (value & ((bfd_vma) 1 << 31))
5314#ifdef BFD64
5315 sign_bits = ((bfd_vma) 1 << 32) - 1;
5316#else
5317 sign_bits = -1;
5318#endif
5319 else
5320 sign_bits = 0;
5321
5322 /* If we don't know that we have a 64-bit type,
5323 do two separate stores. */
5324 if (bfd_big_endian (input_bfd))
5325 {
5326 /* Undo what we did above. */
5327 rel->r_offset -= 4;
5328 /* Store the sign-bits (which are most significant)
5329 first. */
5330 low_bits = sign_bits;
5331 high_bits = value;
5332 }
5333 else
5334 {
5335 low_bits = value;
5336 high_bits = sign_bits;
5337 }
5338 bfd_put_32 (input_bfd, low_bits,
5339 contents + rel->r_offset);
5340 bfd_put_32 (input_bfd, high_bits,
5341 contents + rel->r_offset + 4);
5342 continue;
5343 }
5344
5345 /* Actually perform the relocation. */
5346 if (! mips_elf_perform_relocation (info, howto, rel, value,
5347 input_bfd, input_section,
5348 contents, require_jalx))
5349 return false;
5350 }
5351
5352 return true;
5353}
5354\f
5355/* If NAME is one of the special IRIX6 symbols defined by the linker,
5356 adjust it appropriately now. */
5357
5358static void
5359mips_elf_irix6_finish_dynamic_symbol (abfd, name, sym)
5360 bfd *abfd ATTRIBUTE_UNUSED;
5361 const char *name;
5362 Elf_Internal_Sym *sym;
5363{
5364 /* The linker script takes care of providing names and values for
5365 these, but we must place them into the right sections. */
5366 static const char* const text_section_symbols[] = {
5367 "_ftext",
5368 "_etext",
5369 "__dso_displacement",
5370 "__elf_header",
5371 "__program_header_table",
5372 NULL
5373 };
5374
5375 static const char* const data_section_symbols[] = {
5376 "_fdata",
5377 "_edata",
5378 "_end",
5379 "_fbss",
5380 NULL
5381 };
5382
5383 const char* const *p;
5384 int i;
5385
5386 for (i = 0; i < 2; ++i)
5387 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
5388 *p;
5389 ++p)
5390 if (strcmp (*p, name) == 0)
5391 {
5392 /* All of these symbols are given type STT_SECTION by the
5393 IRIX6 linker. */
5394 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5395
5396 /* The IRIX linker puts these symbols in special sections. */
5397 if (i == 0)
5398 sym->st_shndx = SHN_MIPS_TEXT;
5399 else
5400 sym->st_shndx = SHN_MIPS_DATA;
5401
5402 break;
5403 }
5404}
5405
5406/* Finish up dynamic symbol handling. We set the contents of various
5407 dynamic sections here. */
5408
5409boolean
5410_bfd_mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
5411 bfd *output_bfd;
5412 struct bfd_link_info *info;
5413 struct elf_link_hash_entry *h;
5414 Elf_Internal_Sym *sym;
5415{
5416 bfd *dynobj;
5417 bfd_vma gval;
5418 asection *sgot;
5419 asection *smsym;
5420 struct mips_got_info *g;
5421 const char *name;
5422 struct mips_elf_link_hash_entry *mh;
5423
5424 dynobj = elf_hash_table (info)->dynobj;
5425 gval = sym->st_value;
5426 mh = (struct mips_elf_link_hash_entry *) h;
5427
5428 if (h->plt.offset != (bfd_vma) -1)
5429 {
5430 asection *s;
5431 bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
5432
5433 /* This symbol has a stub. Set it up. */
5434
5435 BFD_ASSERT (h->dynindx != -1);
5436
5437 s = bfd_get_section_by_name (dynobj,
5438 MIPS_ELF_STUB_SECTION_NAME (dynobj));
5439 BFD_ASSERT (s != NULL);
5440
5441 /* FIXME: Can h->dynindex be more than 64K? */
5442 if (h->dynindx & 0xffff0000)
5443 return false;
5444
5445 /* Fill the stub. */
5446 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub);
5447 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + 4);
5448 bfd_put_32 (output_bfd, STUB_JALR, stub + 8);
5449 bfd_put_32 (output_bfd, STUB_LI16 (output_bfd) + h->dynindx, stub + 12);
5450
5451 BFD_ASSERT (h->plt.offset <= s->_raw_size);
5452 memcpy (s->contents + h->plt.offset, stub, MIPS_FUNCTION_STUB_SIZE);
5453
5454 /* Mark the symbol as undefined. plt.offset != -1 occurs
5455 only for the referenced symbol. */
5456 sym->st_shndx = SHN_UNDEF;
5457
5458 /* The run-time linker uses the st_value field of the symbol
5459 to reset the global offset table entry for this external
5460 to its stub address when unlinking a shared object. */
5461 gval = s->output_section->vma + s->output_offset + h->plt.offset;
5462 sym->st_value = gval;
5463 }
5464
5465 BFD_ASSERT (h->dynindx != -1
5466 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0);
5467
5468 sgot = mips_elf_got_section (dynobj);
5469 BFD_ASSERT (sgot != NULL);
5470 BFD_ASSERT (elf_section_data (sgot) != NULL);
5471 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5472 BFD_ASSERT (g != NULL);
5473
5474 /* Run through the global symbol table, creating GOT entries for all
5475 the symbols that need them. */
5476 if (g->global_gotsym != NULL
5477 && h->dynindx >= g->global_gotsym->dynindx)
5478 {
5479 bfd_vma offset;
5480 bfd_vma value;
5481
5482 if (sym->st_value)
5483 value = sym->st_value;
5484 else
5485 {
5486 /* For an entity defined in a shared object, this will be
5487 NULL. (For functions in shared objects for
5488 which we have created stubs, ST_VALUE will be non-NULL.
5489 That's because such the functions are now no longer defined
5490 in a shared object.) */
5491
5492 if (info->shared && h->root.type == bfd_link_hash_undefined)
5493 value = 0;
5494 else
5495 value = h->root.u.def.value;
5496 }
5497 offset = mips_elf_global_got_index (dynobj, h);
5498 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
5499 }
5500
5501 /* Create a .msym entry, if appropriate. */
5502 smsym = bfd_get_section_by_name (dynobj, ".msym");
5503 if (smsym)
5504 {
5505 Elf32_Internal_Msym msym;
5506
5507 msym.ms_hash_value = bfd_elf_hash (h->root.root.string);
5508 /* It is undocumented what the `1' indicates, but IRIX6 uses
5509 this value. */
5510 msym.ms_info = ELF32_MS_INFO (mh->min_dyn_reloc_index, 1);
5511 bfd_mips_elf_swap_msym_out
5512 (dynobj, &msym,
5513 ((Elf32_External_Msym *) smsym->contents) + h->dynindx);
5514 }
5515
5516 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
5517 name = h->root.root.string;
5518 if (strcmp (name, "_DYNAMIC") == 0
5519 || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
5520 sym->st_shndx = SHN_ABS;
5521 else if (strcmp (name, "_DYNAMIC_LINK") == 0
5522 || strcmp (name, "_DYNAMIC_LINKING") == 0)
5523 {
5524 sym->st_shndx = SHN_ABS;
5525 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5526 sym->st_value = 1;
5527 }
4a14403c 5528 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
b49e97c9
TS
5529 {
5530 sym->st_shndx = SHN_ABS;
5531 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5532 sym->st_value = elf_gp (output_bfd);
5533 }
5534 else if (SGI_COMPAT (output_bfd))
5535 {
5536 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
5537 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
5538 {
5539 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5540 sym->st_other = STO_PROTECTED;
5541 sym->st_value = 0;
5542 sym->st_shndx = SHN_MIPS_DATA;
5543 }
5544 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
5545 {
5546 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5547 sym->st_other = STO_PROTECTED;
5548 sym->st_value = mips_elf_hash_table (info)->procedure_count;
5549 sym->st_shndx = SHN_ABS;
5550 }
5551 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
5552 {
5553 if (h->type == STT_FUNC)
5554 sym->st_shndx = SHN_MIPS_TEXT;
5555 else if (h->type == STT_OBJECT)
5556 sym->st_shndx = SHN_MIPS_DATA;
5557 }
5558 }
5559
5560 /* Handle the IRIX6-specific symbols. */
5561 if (IRIX_COMPAT (output_bfd) == ict_irix6)
5562 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
5563
5564 if (! info->shared)
5565 {
5566 if (! mips_elf_hash_table (info)->use_rld_obj_head
5567 && (strcmp (name, "__rld_map") == 0
5568 || strcmp (name, "__RLD_MAP") == 0))
5569 {
5570 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
5571 BFD_ASSERT (s != NULL);
5572 sym->st_value = s->output_section->vma + s->output_offset;
5573 bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
5574 if (mips_elf_hash_table (info)->rld_value == 0)
5575 mips_elf_hash_table (info)->rld_value = sym->st_value;
5576 }
5577 else if (mips_elf_hash_table (info)->use_rld_obj_head
5578 && strcmp (name, "__rld_obj_head") == 0)
5579 {
5580 /* IRIX6 does not use a .rld_map section. */
5581 if (IRIX_COMPAT (output_bfd) == ict_irix5
5582 || IRIX_COMPAT (output_bfd) == ict_none)
5583 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
5584 != NULL);
5585 mips_elf_hash_table (info)->rld_value = sym->st_value;
5586 }
5587 }
5588
5589 /* If this is a mips16 symbol, force the value to be even. */
5590 if (sym->st_other == STO_MIPS16
5591 && (sym->st_value & 1) != 0)
5592 --sym->st_value;
5593
5594 return true;
5595}
5596
5597/* Finish up the dynamic sections. */
5598
5599boolean
5600_bfd_mips_elf_finish_dynamic_sections (output_bfd, info)
5601 bfd *output_bfd;
5602 struct bfd_link_info *info;
5603{
5604 bfd *dynobj;
5605 asection *sdyn;
5606 asection *sgot;
5607 struct mips_got_info *g;
5608
5609 dynobj = elf_hash_table (info)->dynobj;
5610
5611 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5612
5613 sgot = bfd_get_section_by_name (dynobj, ".got");
5614 if (sgot == NULL)
5615 g = NULL;
5616 else
5617 {
5618 BFD_ASSERT (elf_section_data (sgot) != NULL);
5619 g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5620 BFD_ASSERT (g != NULL);
5621 }
5622
5623 if (elf_hash_table (info)->dynamic_sections_created)
5624 {
5625 bfd_byte *b;
5626
5627 BFD_ASSERT (sdyn != NULL);
5628 BFD_ASSERT (g != NULL);
5629
5630 for (b = sdyn->contents;
5631 b < sdyn->contents + sdyn->_raw_size;
5632 b += MIPS_ELF_DYN_SIZE (dynobj))
5633 {
5634 Elf_Internal_Dyn dyn;
5635 const char *name;
5636 size_t elemsize;
5637 asection *s;
5638 boolean swap_out_p;
5639
5640 /* Read in the current dynamic entry. */
5641 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
5642
5643 /* Assume that we're going to modify it and write it out. */
5644 swap_out_p = true;
5645
5646 switch (dyn.d_tag)
5647 {
5648 case DT_RELENT:
5649 s = (bfd_get_section_by_name (dynobj, ".rel.dyn"));
5650 BFD_ASSERT (s != NULL);
5651 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
5652 break;
5653
5654 case DT_STRSZ:
5655 /* Rewrite DT_STRSZ. */
5656 dyn.d_un.d_val =
5657 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5658 break;
5659
5660 case DT_PLTGOT:
5661 name = ".got";
5662 goto get_vma;
5663 case DT_MIPS_CONFLICT:
5664 name = ".conflict";
5665 goto get_vma;
5666 case DT_MIPS_LIBLIST:
5667 name = ".liblist";
5668 get_vma:
5669 s = bfd_get_section_by_name (output_bfd, name);
5670 BFD_ASSERT (s != NULL);
5671 dyn.d_un.d_ptr = s->vma;
5672 break;
5673
5674 case DT_MIPS_RLD_VERSION:
5675 dyn.d_un.d_val = 1; /* XXX */
5676 break;
5677
5678 case DT_MIPS_FLAGS:
5679 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
5680 break;
5681
5682 case DT_MIPS_CONFLICTNO:
5683 name = ".conflict";
5684 elemsize = sizeof (Elf32_Conflict);
5685 goto set_elemno;
5686
5687 case DT_MIPS_LIBLISTNO:
5688 name = ".liblist";
5689 elemsize = sizeof (Elf32_Lib);
5690 set_elemno:
5691 s = bfd_get_section_by_name (output_bfd, name);
5692 if (s != NULL)
5693 {
5694 if (s->_cooked_size != 0)
5695 dyn.d_un.d_val = s->_cooked_size / elemsize;
5696 else
5697 dyn.d_un.d_val = s->_raw_size / elemsize;
5698 }
5699 else
5700 dyn.d_un.d_val = 0;
5701 break;
5702
5703 case DT_MIPS_TIME_STAMP:
5704 time ((time_t *) &dyn.d_un.d_val);
5705 break;
5706
5707 case DT_MIPS_ICHECKSUM:
5708 /* XXX FIXME: */
5709 swap_out_p = false;
5710 break;
5711
5712 case DT_MIPS_IVERSION:
5713 /* XXX FIXME: */
5714 swap_out_p = false;
5715 break;
5716
5717 case DT_MIPS_BASE_ADDRESS:
5718 s = output_bfd->sections;
5719 BFD_ASSERT (s != NULL);
5720 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
5721 break;
5722
5723 case DT_MIPS_LOCAL_GOTNO:
5724 dyn.d_un.d_val = g->local_gotno;
5725 break;
5726
5727 case DT_MIPS_UNREFEXTNO:
5728 /* The index into the dynamic symbol table which is the
5729 entry of the first external symbol that is not
5730 referenced within the same object. */
5731 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
5732 break;
5733
5734 case DT_MIPS_GOTSYM:
5735 if (g->global_gotsym)
5736 {
5737 dyn.d_un.d_val = g->global_gotsym->dynindx;
5738 break;
5739 }
5740 /* In case if we don't have global got symbols we default
5741 to setting DT_MIPS_GOTSYM to the same value as
5742 DT_MIPS_SYMTABNO, so we just fall through. */
5743
5744 case DT_MIPS_SYMTABNO:
5745 name = ".dynsym";
5746 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
5747 s = bfd_get_section_by_name (output_bfd, name);
5748 BFD_ASSERT (s != NULL);
5749
5750 if (s->_cooked_size != 0)
5751 dyn.d_un.d_val = s->_cooked_size / elemsize;
5752 else
5753 dyn.d_un.d_val = s->_raw_size / elemsize;
5754 break;
5755
5756 case DT_MIPS_HIPAGENO:
5757 dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
5758 break;
5759
5760 case DT_MIPS_RLD_MAP:
5761 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
5762 break;
5763
5764 case DT_MIPS_OPTIONS:
5765 s = (bfd_get_section_by_name
5766 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
5767 dyn.d_un.d_ptr = s->vma;
5768 break;
5769
5770 case DT_MIPS_MSYM:
5771 s = (bfd_get_section_by_name (output_bfd, ".msym"));
5772 dyn.d_un.d_ptr = s->vma;
5773 break;
5774
5775 default:
5776 swap_out_p = false;
5777 break;
5778 }
5779
5780 if (swap_out_p)
5781 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
5782 (dynobj, &dyn, b);
5783 }
5784 }
5785
5786 /* The first entry of the global offset table will be filled at
5787 runtime. The second entry will be used by some runtime loaders.
8dc1a139 5788 This isn't the case of IRIX rld. */
b49e97c9
TS
5789 if (sgot != NULL && sgot->_raw_size > 0)
5790 {
5791 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
5792 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0x80000000,
5793 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
5794 }
5795
5796 if (sgot != NULL)
5797 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
5798 = MIPS_ELF_GOT_SIZE (output_bfd);
5799
5800 {
5801 asection *smsym;
5802 asection *s;
5803 Elf32_compact_rel cpt;
5804
5805 /* ??? The section symbols for the output sections were set up in
5806 _bfd_elf_final_link. SGI sets the STT_NOTYPE attribute for these
5807 symbols. Should we do so? */
5808
5809 smsym = bfd_get_section_by_name (dynobj, ".msym");
5810 if (smsym != NULL)
5811 {
5812 Elf32_Internal_Msym msym;
5813
5814 msym.ms_hash_value = 0;
5815 msym.ms_info = ELF32_MS_INFO (0, 1);
5816
5817 for (s = output_bfd->sections; s != NULL; s = s->next)
5818 {
5819 long dynindx = elf_section_data (s)->dynindx;
5820
5821 bfd_mips_elf_swap_msym_out
5822 (output_bfd, &msym,
5823 (((Elf32_External_Msym *) smsym->contents)
5824 + dynindx));
5825 }
5826 }
5827
5828 if (SGI_COMPAT (output_bfd))
5829 {
5830 /* Write .compact_rel section out. */
5831 s = bfd_get_section_by_name (dynobj, ".compact_rel");
5832 if (s != NULL)
5833 {
5834 cpt.id1 = 1;
5835 cpt.num = s->reloc_count;
5836 cpt.id2 = 2;
5837 cpt.offset = (s->output_section->filepos
5838 + sizeof (Elf32_External_compact_rel));
5839 cpt.reserved0 = 0;
5840 cpt.reserved1 = 0;
5841 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
5842 ((Elf32_External_compact_rel *)
5843 s->contents));
5844
5845 /* Clean up a dummy stub function entry in .text. */
5846 s = bfd_get_section_by_name (dynobj,
5847 MIPS_ELF_STUB_SECTION_NAME (dynobj));
5848 if (s != NULL)
5849 {
5850 file_ptr dummy_offset;
5851
5852 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
5853 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
5854 memset (s->contents + dummy_offset, 0,
5855 MIPS_FUNCTION_STUB_SIZE);
5856 }
5857 }
5858 }
5859
5860 /* We need to sort the entries of the dynamic relocation section. */
5861
5862 if (!ABI_64_P (output_bfd))
5863 {
5864 asection *reldyn;
5865
5866 reldyn = bfd_get_section_by_name (dynobj, ".rel.dyn");
5867 if (reldyn != NULL && reldyn->reloc_count > 2)
5868 {
5869 reldyn_sorting_bfd = output_bfd;
5870 qsort ((Elf32_External_Rel *) reldyn->contents + 1,
5871 (size_t) reldyn->reloc_count - 1,
5872 sizeof (Elf32_External_Rel), sort_dynamic_relocs);
5873 }
5874 }
5875
5876 /* Clean up a first relocation in .rel.dyn. */
5877 s = bfd_get_section_by_name (dynobj, ".rel.dyn");
5878 if (s != NULL && s->_raw_size > 0)
5879 memset (s->contents, 0, MIPS_ELF_REL_SIZE (dynobj));
5880 }
5881
5882 return true;
5883}
5884
5885/* The final processing done just before writing out a MIPS ELF object
5886 file. This gets the MIPS architecture right based on the machine
5887 number. This is used by both the 32-bit and the 64-bit ABI. */
5888
5889void
5890_bfd_mips_elf_final_write_processing (abfd, linker)
5891 bfd *abfd;
5892 boolean linker ATTRIBUTE_UNUSED;
5893{
5894 unsigned long val;
5895 unsigned int i;
5896 Elf_Internal_Shdr **hdrpp;
5897 const char *name;
5898 asection *sec;
5899
5900 switch (bfd_get_mach (abfd))
5901 {
5902 default:
5903 case bfd_mach_mips3000:
5904 val = E_MIPS_ARCH_1;
5905 break;
5906
5907 case bfd_mach_mips3900:
5908 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
5909 break;
5910
5911 case bfd_mach_mips6000:
5912 val = E_MIPS_ARCH_2;
5913 break;
5914
5915 case bfd_mach_mips4000:
5916 case bfd_mach_mips4300:
5917 case bfd_mach_mips4400:
5918 case bfd_mach_mips4600:
5919 val = E_MIPS_ARCH_3;
5920 break;
5921
5922 case bfd_mach_mips4010:
5923 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
5924 break;
5925
5926 case bfd_mach_mips4100:
5927 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
5928 break;
5929
5930 case bfd_mach_mips4111:
5931 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
5932 break;
5933
5934 case bfd_mach_mips4650:
5935 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
5936 break;
5937
5938 case bfd_mach_mips5000:
5939 case bfd_mach_mips8000:
5940 case bfd_mach_mips10000:
5941 case bfd_mach_mips12000:
5942 val = E_MIPS_ARCH_4;
5943 break;
5944
5945 case bfd_mach_mips5:
5946 val = E_MIPS_ARCH_5;
5947 break;
5948
5949 case bfd_mach_mips_sb1:
5950 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
5951 break;
5952
5953 case bfd_mach_mipsisa32:
5954 val = E_MIPS_ARCH_32;
5955 break;
5956
5957 case bfd_mach_mipsisa64:
5958 val = E_MIPS_ARCH_64;
5959 }
5960
5961 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
5962 elf_elfheader (abfd)->e_flags |= val;
5963
5964 /* Set the sh_info field for .gptab sections and other appropriate
5965 info for each special section. */
5966 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
5967 i < elf_numsections (abfd);
5968 i++, hdrpp++)
5969 {
5970 switch ((*hdrpp)->sh_type)
5971 {
5972 case SHT_MIPS_MSYM:
5973 case SHT_MIPS_LIBLIST:
5974 sec = bfd_get_section_by_name (abfd, ".dynstr");
5975 if (sec != NULL)
5976 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
5977 break;
5978
5979 case SHT_MIPS_GPTAB:
5980 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
5981 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
5982 BFD_ASSERT (name != NULL
5983 && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
5984 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
5985 BFD_ASSERT (sec != NULL);
5986 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
5987 break;
5988
5989 case SHT_MIPS_CONTENT:
5990 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
5991 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
5992 BFD_ASSERT (name != NULL
5993 && strncmp (name, ".MIPS.content",
5994 sizeof ".MIPS.content" - 1) == 0);
5995 sec = bfd_get_section_by_name (abfd,
5996 name + sizeof ".MIPS.content" - 1);
5997 BFD_ASSERT (sec != NULL);
5998 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
5999 break;
6000
6001 case SHT_MIPS_SYMBOL_LIB:
6002 sec = bfd_get_section_by_name (abfd, ".dynsym");
6003 if (sec != NULL)
6004 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
6005 sec = bfd_get_section_by_name (abfd, ".liblist");
6006 if (sec != NULL)
6007 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
6008 break;
6009
6010 case SHT_MIPS_EVENTS:
6011 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
6012 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
6013 BFD_ASSERT (name != NULL);
6014 if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
6015 sec = bfd_get_section_by_name (abfd,
6016 name + sizeof ".MIPS.events" - 1);
6017 else
6018 {
6019 BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
6020 sizeof ".MIPS.post_rel" - 1) == 0);
6021 sec = bfd_get_section_by_name (abfd,
6022 (name
6023 + sizeof ".MIPS.post_rel" - 1));
6024 }
6025 BFD_ASSERT (sec != NULL);
6026 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
6027 break;
6028
6029 }
6030 }
6031}
6032\f
8dc1a139 6033/* When creating an IRIX5 executable, we need REGINFO and RTPROC
b49e97c9
TS
6034 segments. */
6035
6036int
6037_bfd_mips_elf_additional_program_headers (abfd)
6038 bfd *abfd;
6039{
6040 asection *s;
6041 int ret = 0;
6042
6043 /* See if we need a PT_MIPS_REGINFO segment. */
6044 s = bfd_get_section_by_name (abfd, ".reginfo");
6045 if (s && (s->flags & SEC_LOAD))
6046 ++ret;
6047
6048 /* See if we need a PT_MIPS_OPTIONS segment. */
6049 if (IRIX_COMPAT (abfd) == ict_irix6
6050 && bfd_get_section_by_name (abfd,
6051 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
6052 ++ret;
6053
6054 /* See if we need a PT_MIPS_RTPROC segment. */
6055 if (IRIX_COMPAT (abfd) == ict_irix5
6056 && bfd_get_section_by_name (abfd, ".dynamic")
6057 && bfd_get_section_by_name (abfd, ".mdebug"))
6058 ++ret;
6059
6060 return ret;
6061}
6062
8dc1a139 6063/* Modify the segment map for an IRIX5 executable. */
b49e97c9
TS
6064
6065boolean
6066_bfd_mips_elf_modify_segment_map (abfd)
6067 bfd *abfd;
6068{
6069 asection *s;
6070 struct elf_segment_map *m, **pm;
6071 bfd_size_type amt;
6072
6073 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
6074 segment. */
6075 s = bfd_get_section_by_name (abfd, ".reginfo");
6076 if (s != NULL && (s->flags & SEC_LOAD) != 0)
6077 {
6078 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
6079 if (m->p_type == PT_MIPS_REGINFO)
6080 break;
6081 if (m == NULL)
6082 {
6083 amt = sizeof *m;
6084 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
6085 if (m == NULL)
6086 return false;
6087
6088 m->p_type = PT_MIPS_REGINFO;
6089 m->count = 1;
6090 m->sections[0] = s;
6091
6092 /* We want to put it after the PHDR and INTERP segments. */
6093 pm = &elf_tdata (abfd)->segment_map;
6094 while (*pm != NULL
6095 && ((*pm)->p_type == PT_PHDR
6096 || (*pm)->p_type == PT_INTERP))
6097 pm = &(*pm)->next;
6098
6099 m->next = *pm;
6100 *pm = m;
6101 }
6102 }
6103
6104 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
6105 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
6106 PT_OPTIONS segement immediately following the program header
6107 table. */
4a14403c 6108 if (ABI_64_P (abfd))
b49e97c9
TS
6109 {
6110 for (s = abfd->sections; s; s = s->next)
6111 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
6112 break;
6113
6114 if (s)
6115 {
6116 struct elf_segment_map *options_segment;
6117
6118 /* Usually, there's a program header table. But, sometimes
6119 there's not (like when running the `ld' testsuite). So,
6120 if there's no program header table, we just put the
6121 options segement at the end. */
6122 for (pm = &elf_tdata (abfd)->segment_map;
6123 *pm != NULL;
6124 pm = &(*pm)->next)
6125 if ((*pm)->p_type == PT_PHDR)
6126 break;
6127
6128 amt = sizeof (struct elf_segment_map);
6129 options_segment = bfd_zalloc (abfd, amt);
6130 options_segment->next = *pm;
6131 options_segment->p_type = PT_MIPS_OPTIONS;
6132 options_segment->p_flags = PF_R;
6133 options_segment->p_flags_valid = true;
6134 options_segment->count = 1;
6135 options_segment->sections[0] = s;
6136 *pm = options_segment;
6137 }
6138 }
6139 else
6140 {
6141 if (IRIX_COMPAT (abfd) == ict_irix5)
6142 {
6143 /* If there are .dynamic and .mdebug sections, we make a room
6144 for the RTPROC header. FIXME: Rewrite without section names. */
6145 if (bfd_get_section_by_name (abfd, ".interp") == NULL
6146 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
6147 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
6148 {
6149 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
6150 if (m->p_type == PT_MIPS_RTPROC)
6151 break;
6152 if (m == NULL)
6153 {
6154 amt = sizeof *m;
6155 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
6156 if (m == NULL)
6157 return false;
6158
6159 m->p_type = PT_MIPS_RTPROC;
6160
6161 s = bfd_get_section_by_name (abfd, ".rtproc");
6162 if (s == NULL)
6163 {
6164 m->count = 0;
6165 m->p_flags = 0;
6166 m->p_flags_valid = 1;
6167 }
6168 else
6169 {
6170 m->count = 1;
6171 m->sections[0] = s;
6172 }
6173
6174 /* We want to put it after the DYNAMIC segment. */
6175 pm = &elf_tdata (abfd)->segment_map;
6176 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
6177 pm = &(*pm)->next;
6178 if (*pm != NULL)
6179 pm = &(*pm)->next;
6180
6181 m->next = *pm;
6182 *pm = m;
6183 }
6184 }
6185 }
8dc1a139 6186 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
b49e97c9
TS
6187 .dynstr, .dynsym, and .hash sections, and everything in
6188 between. */
6189 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
6190 pm = &(*pm)->next)
6191 if ((*pm)->p_type == PT_DYNAMIC)
6192 break;
6193 m = *pm;
6194 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
6195 {
6196 /* For a normal mips executable the permissions for the PT_DYNAMIC
6197 segment are read, write and execute. We do that here since
6198 the code in elf.c sets only the read permission. This matters
6199 sometimes for the dynamic linker. */
6200 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
6201 {
6202 m->p_flags = PF_R | PF_W | PF_X;
6203 m->p_flags_valid = 1;
6204 }
6205 }
6206 if (m != NULL
6207 && m->count == 1 && strcmp (m->sections[0]->name, ".dynamic") == 0)
6208 {
6209 static const char *sec_names[] =
6210 {
6211 ".dynamic", ".dynstr", ".dynsym", ".hash"
6212 };
6213 bfd_vma low, high;
6214 unsigned int i, c;
6215 struct elf_segment_map *n;
6216
6217 low = 0xffffffff;
6218 high = 0;
6219 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
6220 {
6221 s = bfd_get_section_by_name (abfd, sec_names[i]);
6222 if (s != NULL && (s->flags & SEC_LOAD) != 0)
6223 {
6224 bfd_size_type sz;
6225
6226 if (low > s->vma)
6227 low = s->vma;
6228 sz = s->_cooked_size;
6229 if (sz == 0)
6230 sz = s->_raw_size;
6231 if (high < s->vma + sz)
6232 high = s->vma + sz;
6233 }
6234 }
6235
6236 c = 0;
6237 for (s = abfd->sections; s != NULL; s = s->next)
6238 if ((s->flags & SEC_LOAD) != 0
6239 && s->vma >= low
6240 && ((s->vma
6241 + (s->_cooked_size !=
6242 0 ? s->_cooked_size : s->_raw_size)) <= high))
6243 ++c;
6244
6245 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
6246 n = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
6247 if (n == NULL)
6248 return false;
6249 *n = *m;
6250 n->count = c;
6251
6252 i = 0;
6253 for (s = abfd->sections; s != NULL; s = s->next)
6254 {
6255 if ((s->flags & SEC_LOAD) != 0
6256 && s->vma >= low
6257 && ((s->vma
6258 + (s->_cooked_size != 0 ?
6259 s->_cooked_size : s->_raw_size)) <= high))
6260 {
6261 n->sections[i] = s;
6262 ++i;
6263 }
6264 }
6265
6266 *pm = n;
6267 }
6268 }
6269
6270 return true;
6271}
6272\f
6273/* Return the section that should be marked against GC for a given
6274 relocation. */
6275
6276asection *
1e2f5b6e
AM
6277_bfd_mips_elf_gc_mark_hook (sec, info, rel, h, sym)
6278 asection *sec;
b49e97c9
TS
6279 struct bfd_link_info *info ATTRIBUTE_UNUSED;
6280 Elf_Internal_Rela *rel;
6281 struct elf_link_hash_entry *h;
6282 Elf_Internal_Sym *sym;
6283{
6284 /* ??? Do mips16 stub sections need to be handled special? */
6285
6286 if (h != NULL)
6287 {
1e2f5b6e 6288 switch (ELF_R_TYPE (sec->owner, rel->r_info))
b49e97c9
TS
6289 {
6290 case R_MIPS_GNU_VTINHERIT:
6291 case R_MIPS_GNU_VTENTRY:
6292 break;
6293
6294 default:
6295 switch (h->root.type)
6296 {
6297 case bfd_link_hash_defined:
6298 case bfd_link_hash_defweak:
6299 return h->root.u.def.section;
6300
6301 case bfd_link_hash_common:
6302 return h->root.u.c.p->section;
6303
6304 default:
6305 break;
6306 }
6307 }
6308 }
6309 else
1e2f5b6e 6310 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
b49e97c9
TS
6311
6312 return NULL;
6313}
6314
6315/* Update the got entry reference counts for the section being removed. */
6316
6317boolean
6318_bfd_mips_elf_gc_sweep_hook (abfd, info, sec, relocs)
6319 bfd *abfd ATTRIBUTE_UNUSED;
6320 struct bfd_link_info *info ATTRIBUTE_UNUSED;
6321 asection *sec ATTRIBUTE_UNUSED;
6322 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
6323{
6324#if 0
6325 Elf_Internal_Shdr *symtab_hdr;
6326 struct elf_link_hash_entry **sym_hashes;
6327 bfd_signed_vma *local_got_refcounts;
6328 const Elf_Internal_Rela *rel, *relend;
6329 unsigned long r_symndx;
6330 struct elf_link_hash_entry *h;
6331
6332 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6333 sym_hashes = elf_sym_hashes (abfd);
6334 local_got_refcounts = elf_local_got_refcounts (abfd);
6335
6336 relend = relocs + sec->reloc_count;
6337 for (rel = relocs; rel < relend; rel++)
6338 switch (ELF_R_TYPE (abfd, rel->r_info))
6339 {
6340 case R_MIPS_GOT16:
6341 case R_MIPS_CALL16:
6342 case R_MIPS_CALL_HI16:
6343 case R_MIPS_CALL_LO16:
6344 case R_MIPS_GOT_HI16:
6345 case R_MIPS_GOT_LO16:
4a14403c
TS
6346 case R_MIPS_GOT_DISP:
6347 case R_MIPS_GOT_PAGE:
6348 case R_MIPS_GOT_OFST:
b49e97c9
TS
6349 /* ??? It would seem that the existing MIPS code does no sort
6350 of reference counting or whatnot on its GOT and PLT entries,
6351 so it is not possible to garbage collect them at this time. */
6352 break;
6353
6354 default:
6355 break;
6356 }
6357#endif
6358
6359 return true;
6360}
6361\f
6362/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
6363 hiding the old indirect symbol. Process additional relocation
6364 information. Also called for weakdefs, in which case we just let
6365 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
6366
6367void
6368_bfd_mips_elf_copy_indirect_symbol (dir, ind)
6369 struct elf_link_hash_entry *dir, *ind;
6370{
6371 struct mips_elf_link_hash_entry *dirmips, *indmips;
6372
6373 _bfd_elf_link_hash_copy_indirect (dir, ind);
6374
6375 if (ind->root.type != bfd_link_hash_indirect)
6376 return;
6377
6378 dirmips = (struct mips_elf_link_hash_entry *) dir;
6379 indmips = (struct mips_elf_link_hash_entry *) ind;
6380 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
6381 if (indmips->readonly_reloc)
6382 dirmips->readonly_reloc = true;
6383 if (dirmips->min_dyn_reloc_index == 0
6384 || (indmips->min_dyn_reloc_index != 0
6385 && indmips->min_dyn_reloc_index < dirmips->min_dyn_reloc_index))
6386 dirmips->min_dyn_reloc_index = indmips->min_dyn_reloc_index;
6387 if (indmips->no_fn_stub)
6388 dirmips->no_fn_stub = true;
6389}
6390
6391void
6392_bfd_mips_elf_hide_symbol (info, entry, force_local)
6393 struct bfd_link_info *info;
6394 struct elf_link_hash_entry *entry;
6395 boolean force_local;
6396{
6397 bfd *dynobj;
6398 asection *got;
6399 struct mips_got_info *g;
6400 struct mips_elf_link_hash_entry *h;
7c5fcef7 6401
b49e97c9 6402 h = (struct mips_elf_link_hash_entry *) entry;
7c5fcef7
L
6403 if (h->forced_local)
6404 return;
6405 h->forced_local = true;
6406
b49e97c9
TS
6407 dynobj = elf_hash_table (info)->dynobj;
6408 got = bfd_get_section_by_name (dynobj, ".got");
6409 g = (struct mips_got_info *) elf_section_data (got)->tdata;
6410
6411 _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
6412
6413 /* FIXME: Do we allocate too much GOT space here? */
6414 g->local_gotno++;
6415 got->_raw_size += MIPS_ELF_GOT_SIZE (dynobj);
6416}
6417\f
d01414a5
TS
6418#define PDR_SIZE 32
6419
6420boolean
6421_bfd_mips_elf_discard_info (abfd, cookie, info)
6422 bfd *abfd;
6423 struct elf_reloc_cookie *cookie;
6424 struct bfd_link_info *info;
6425{
6426 asection *o;
6427 boolean ret = false;
6428 unsigned char *tdata;
6429 size_t i, skip;
6430
6431 o = bfd_get_section_by_name (abfd, ".pdr");
6432 if (! o)
6433 return false;
6434 if (o->_raw_size == 0)
6435 return false;
6436 if (o->_raw_size % PDR_SIZE != 0)
6437 return false;
6438 if (o->output_section != NULL
6439 && bfd_is_abs_section (o->output_section))
6440 return false;
6441
6442 tdata = bfd_zmalloc (o->_raw_size / PDR_SIZE);
6443 if (! tdata)
6444 return false;
6445
6446 cookie->rels = _bfd_elf32_link_read_relocs (abfd, o, (PTR) NULL,
6447 (Elf_Internal_Rela *) NULL,
6448 info->keep_memory);
6449 if (!cookie->rels)
6450 {
6451 free (tdata);
6452 return false;
6453 }
6454
6455 cookie->rel = cookie->rels;
6456 cookie->relend = cookie->rels + o->reloc_count;
6457
6458 for (i = 0, skip = 0; i < o->_raw_size; i ++)
6459 {
6460 if (_bfd_elf32_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
6461 {
6462 tdata[i] = 1;
6463 skip ++;
6464 }
6465 }
6466
6467 if (skip != 0)
6468 {
6469 elf_section_data (o)->tdata = tdata;
6470 o->_cooked_size = o->_raw_size - skip * PDR_SIZE;
6471 ret = true;
6472 }
6473 else
6474 free (tdata);
6475
6476 if (! info->keep_memory)
6477 free (cookie->rels);
6478
6479 return ret;
6480}
6481
53bfd6b4
MR
6482boolean
6483_bfd_mips_elf_ignore_discarded_relocs (sec)
6484 asection *sec;
6485{
6486 if (strcmp (sec->name, ".pdr") == 0)
6487 return true;
6488 return false;
6489}
d01414a5
TS
6490
6491boolean
6492_bfd_mips_elf_write_section (output_bfd, sec, contents)
6493 bfd *output_bfd;
6494 asection *sec;
6495 bfd_byte *contents;
6496{
6497 bfd_byte *to, *from, *end;
6498 int i;
6499
6500 if (strcmp (sec->name, ".pdr") != 0)
6501 return false;
6502
6503 if (elf_section_data (sec)->tdata == NULL)
6504 return false;
6505
6506 to = contents;
6507 end = contents + sec->_raw_size;
6508 for (from = contents, i = 0;
6509 from < end;
6510 from += PDR_SIZE, i++)
6511 {
6512 if (((unsigned char *) elf_section_data (sec)->tdata)[i] == 1)
6513 continue;
6514 if (to != from)
6515 memcpy (to, from, PDR_SIZE);
6516 to += PDR_SIZE;
6517 }
6518 bfd_set_section_contents (output_bfd, sec->output_section, contents,
6519 (file_ptr) sec->output_offset,
6520 sec->_cooked_size);
6521 return true;
6522}
53bfd6b4 6523\f
b49e97c9
TS
6524/* MIPS ELF uses a special find_nearest_line routine in order the
6525 handle the ECOFF debugging information. */
6526
6527struct mips_elf_find_line
6528{
6529 struct ecoff_debug_info d;
6530 struct ecoff_find_line i;
6531};
6532
6533boolean
6534_bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
6535 functionname_ptr, line_ptr)
6536 bfd *abfd;
6537 asection *section;
6538 asymbol **symbols;
6539 bfd_vma offset;
6540 const char **filename_ptr;
6541 const char **functionname_ptr;
6542 unsigned int *line_ptr;
6543{
6544 asection *msec;
6545
6546 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6547 filename_ptr, functionname_ptr,
6548 line_ptr))
6549 return true;
6550
6551 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6552 filename_ptr, functionname_ptr,
6553 line_ptr,
6554 (unsigned) (ABI_64_P (abfd) ? 8 : 0),
6555 &elf_tdata (abfd)->dwarf2_find_line_info))
6556 return true;
6557
6558 msec = bfd_get_section_by_name (abfd, ".mdebug");
6559 if (msec != NULL)
6560 {
6561 flagword origflags;
6562 struct mips_elf_find_line *fi;
6563 const struct ecoff_debug_swap * const swap =
6564 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
6565
6566 /* If we are called during a link, mips_elf_final_link may have
6567 cleared the SEC_HAS_CONTENTS field. We force it back on here
6568 if appropriate (which it normally will be). */
6569 origflags = msec->flags;
6570 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
6571 msec->flags |= SEC_HAS_CONTENTS;
6572
6573 fi = elf_tdata (abfd)->find_line_info;
6574 if (fi == NULL)
6575 {
6576 bfd_size_type external_fdr_size;
6577 char *fraw_src;
6578 char *fraw_end;
6579 struct fdr *fdr_ptr;
6580 bfd_size_type amt = sizeof (struct mips_elf_find_line);
6581
6582 fi = (struct mips_elf_find_line *) bfd_zalloc (abfd, amt);
6583 if (fi == NULL)
6584 {
6585 msec->flags = origflags;
6586 return false;
6587 }
6588
6589 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
6590 {
6591 msec->flags = origflags;
6592 return false;
6593 }
6594
6595 /* Swap in the FDR information. */
6596 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
6597 fi->d.fdr = (struct fdr *) bfd_alloc (abfd, amt);
6598 if (fi->d.fdr == NULL)
6599 {
6600 msec->flags = origflags;
6601 return false;
6602 }
6603 external_fdr_size = swap->external_fdr_size;
6604 fdr_ptr = fi->d.fdr;
6605 fraw_src = (char *) fi->d.external_fdr;
6606 fraw_end = (fraw_src
6607 + fi->d.symbolic_header.ifdMax * external_fdr_size);
6608 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
6609 (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
6610
6611 elf_tdata (abfd)->find_line_info = fi;
6612
6613 /* Note that we don't bother to ever free this information.
6614 find_nearest_line is either called all the time, as in
6615 objdump -l, so the information should be saved, or it is
6616 rarely called, as in ld error messages, so the memory
6617 wasted is unimportant. Still, it would probably be a
6618 good idea for free_cached_info to throw it away. */
6619 }
6620
6621 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
6622 &fi->i, filename_ptr, functionname_ptr,
6623 line_ptr))
6624 {
6625 msec->flags = origflags;
6626 return true;
6627 }
6628
6629 msec->flags = origflags;
6630 }
6631
6632 /* Fall back on the generic ELF find_nearest_line routine. */
6633
6634 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
6635 filename_ptr, functionname_ptr,
6636 line_ptr);
6637}
6638\f
6639/* When are writing out the .options or .MIPS.options section,
6640 remember the bytes we are writing out, so that we can install the
6641 GP value in the section_processing routine. */
6642
6643boolean
6644_bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
6645 bfd *abfd;
6646 sec_ptr section;
6647 PTR location;
6648 file_ptr offset;
6649 bfd_size_type count;
6650{
6651 if (strcmp (section->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
6652 {
6653 bfd_byte *c;
6654
6655 if (elf_section_data (section) == NULL)
6656 {
6657 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
6658 section->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
6659 if (elf_section_data (section) == NULL)
6660 return false;
6661 }
6662 c = (bfd_byte *) elf_section_data (section)->tdata;
6663 if (c == NULL)
6664 {
6665 bfd_size_type size;
6666
6667 if (section->_cooked_size != 0)
6668 size = section->_cooked_size;
6669 else
6670 size = section->_raw_size;
6671 c = (bfd_byte *) bfd_zalloc (abfd, size);
6672 if (c == NULL)
6673 return false;
6674 elf_section_data (section)->tdata = (PTR) c;
6675 }
6676
6677 memcpy (c + offset, location, (size_t) count);
6678 }
6679
6680 return _bfd_elf_set_section_contents (abfd, section, location, offset,
6681 count);
6682}
6683
6684/* This is almost identical to bfd_generic_get_... except that some
6685 MIPS relocations need to be handled specially. Sigh. */
6686
6687bfd_byte *
6688_bfd_elf_mips_get_relocated_section_contents (abfd, link_info, link_order,
6689 data, relocateable, symbols)
6690 bfd *abfd;
6691 struct bfd_link_info *link_info;
6692 struct bfd_link_order *link_order;
6693 bfd_byte *data;
6694 boolean relocateable;
6695 asymbol **symbols;
6696{
6697 /* Get enough memory to hold the stuff */
6698 bfd *input_bfd = link_order->u.indirect.section->owner;
6699 asection *input_section = link_order->u.indirect.section;
6700
6701 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
6702 arelent **reloc_vector = NULL;
6703 long reloc_count;
6704
6705 if (reloc_size < 0)
6706 goto error_return;
6707
6708 reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
6709 if (reloc_vector == NULL && reloc_size != 0)
6710 goto error_return;
6711
6712 /* read in the section */
6713 if (!bfd_get_section_contents (input_bfd,
6714 input_section,
6715 (PTR) data,
6716 (file_ptr) 0,
6717 input_section->_raw_size))
6718 goto error_return;
6719
6720 /* We're not relaxing the section, so just copy the size info */
6721 input_section->_cooked_size = input_section->_raw_size;
6722 input_section->reloc_done = true;
6723
6724 reloc_count = bfd_canonicalize_reloc (input_bfd,
6725 input_section,
6726 reloc_vector,
6727 symbols);
6728 if (reloc_count < 0)
6729 goto error_return;
6730
6731 if (reloc_count > 0)
6732 {
6733 arelent **parent;
6734 /* for mips */
6735 int gp_found;
6736 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
6737
6738 {
6739 struct bfd_hash_entry *h;
6740 struct bfd_link_hash_entry *lh;
6741 /* Skip all this stuff if we aren't mixing formats. */
6742 if (abfd && input_bfd
6743 && abfd->xvec == input_bfd->xvec)
6744 lh = 0;
6745 else
6746 {
6747 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
6748 lh = (struct bfd_link_hash_entry *) h;
6749 }
6750 lookup:
6751 if (lh)
6752 {
6753 switch (lh->type)
6754 {
6755 case bfd_link_hash_undefined:
6756 case bfd_link_hash_undefweak:
6757 case bfd_link_hash_common:
6758 gp_found = 0;
6759 break;
6760 case bfd_link_hash_defined:
6761 case bfd_link_hash_defweak:
6762 gp_found = 1;
6763 gp = lh->u.def.value;
6764 break;
6765 case bfd_link_hash_indirect:
6766 case bfd_link_hash_warning:
6767 lh = lh->u.i.link;
6768 /* @@FIXME ignoring warning for now */
6769 goto lookup;
6770 case bfd_link_hash_new:
6771 default:
6772 abort ();
6773 }
6774 }
6775 else
6776 gp_found = 0;
6777 }
6778 /* end mips */
6779 for (parent = reloc_vector; *parent != (arelent *) NULL;
6780 parent++)
6781 {
6782 char *error_message = (char *) NULL;
6783 bfd_reloc_status_type r;
6784
6785 /* Specific to MIPS: Deal with relocation types that require
6786 knowing the gp of the output bfd. */
6787 asymbol *sym = *(*parent)->sym_ptr_ptr;
6788 if (bfd_is_abs_section (sym->section) && abfd)
6789 {
6790 /* The special_function wouldn't get called anyways. */
6791 }
6792 else if (!gp_found)
6793 {
6794 /* The gp isn't there; let the special function code
6795 fall over on its own. */
6796 }
6797 else if ((*parent)->howto->special_function
6798 == _bfd_mips_elf32_gprel16_reloc)
6799 {
6800 /* bypass special_function call */
6801 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
6802 input_section, relocateable,
6803 (PTR) data, gp);
6804 goto skip_bfd_perform_relocation;
6805 }
6806 /* end mips specific stuff */
6807
6808 r = bfd_perform_relocation (input_bfd,
6809 *parent,
6810 (PTR) data,
6811 input_section,
6812 relocateable ? abfd : (bfd *) NULL,
6813 &error_message);
6814 skip_bfd_perform_relocation:
6815
6816 if (relocateable)
6817 {
6818 asection *os = input_section->output_section;
6819
6820 /* A partial link, so keep the relocs */
6821 os->orelocation[os->reloc_count] = *parent;
6822 os->reloc_count++;
6823 }
6824
6825 if (r != bfd_reloc_ok)
6826 {
6827 switch (r)
6828 {
6829 case bfd_reloc_undefined:
6830 if (!((*link_info->callbacks->undefined_symbol)
6831 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
6832 input_bfd, input_section, (*parent)->address,
6833 true)))
6834 goto error_return;
6835 break;
6836 case bfd_reloc_dangerous:
6837 BFD_ASSERT (error_message != (char *) NULL);
6838 if (!((*link_info->callbacks->reloc_dangerous)
6839 (link_info, error_message, input_bfd, input_section,
6840 (*parent)->address)))
6841 goto error_return;
6842 break;
6843 case bfd_reloc_overflow:
6844 if (!((*link_info->callbacks->reloc_overflow)
6845 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
6846 (*parent)->howto->name, (*parent)->addend,
6847 input_bfd, input_section, (*parent)->address)))
6848 goto error_return;
6849 break;
6850 case bfd_reloc_outofrange:
6851 default:
6852 abort ();
6853 break;
6854 }
6855
6856 }
6857 }
6858 }
6859 if (reloc_vector != NULL)
6860 free (reloc_vector);
6861 return data;
6862
6863error_return:
6864 if (reloc_vector != NULL)
6865 free (reloc_vector);
6866 return NULL;
6867}
6868\f
6869/* Create a MIPS ELF linker hash table. */
6870
6871struct bfd_link_hash_table *
6872_bfd_mips_elf_link_hash_table_create (abfd)
6873 bfd *abfd;
6874{
6875 struct mips_elf_link_hash_table *ret;
6876 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
6877
e2d34d7d 6878 ret = (struct mips_elf_link_hash_table *) bfd_malloc (amt);
b49e97c9
TS
6879 if (ret == (struct mips_elf_link_hash_table *) NULL)
6880 return NULL;
6881
6882 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
6883 mips_elf_link_hash_newfunc))
6884 {
e2d34d7d 6885 free (ret);
b49e97c9
TS
6886 return NULL;
6887 }
6888
6889#if 0
6890 /* We no longer use this. */
6891 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
6892 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
6893#endif
6894 ret->procedure_count = 0;
6895 ret->compact_rel_size = 0;
6896 ret->use_rld_obj_head = false;
6897 ret->rld_value = 0;
6898 ret->mips16_stubs_seen = false;
6899
6900 return &ret->root.root;
6901}
6902\f
6903/* We need to use a special link routine to handle the .reginfo and
6904 the .mdebug sections. We need to merge all instances of these
6905 sections together, not write them all out sequentially. */
6906
6907boolean
6908_bfd_mips_elf_final_link (abfd, info)
6909 bfd *abfd;
6910 struct bfd_link_info *info;
6911{
6912 asection **secpp;
6913 asection *o;
6914 struct bfd_link_order *p;
6915 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
6916 asection *rtproc_sec;
6917 Elf32_RegInfo reginfo;
6918 struct ecoff_debug_info debug;
6919 const struct ecoff_debug_swap *swap
6920 = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
6921 HDRR *symhdr = &debug.symbolic_header;
6922 PTR mdebug_handle = NULL;
6923 asection *s;
6924 EXTR esym;
6925 unsigned int i;
6926 bfd_size_type amt;
6927
6928 static const char * const secname[] =
6929 {
6930 ".text", ".init", ".fini", ".data",
6931 ".rodata", ".sdata", ".sbss", ".bss"
6932 };
6933 static const int sc[] =
6934 {
6935 scText, scInit, scFini, scData,
6936 scRData, scSData, scSBss, scBss
6937 };
6938
6939 /* If all the things we linked together were PIC, but we're
6940 producing an executable (rather than a shared object), then the
6941 resulting file is CPIC (i.e., it calls PIC code.) */
6942 if (!info->shared
6943 && !info->relocateable
6944 && elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
6945 {
6946 elf_elfheader (abfd)->e_flags &= ~EF_MIPS_PIC;
6947 elf_elfheader (abfd)->e_flags |= EF_MIPS_CPIC;
6948 }
6949
6950 /* We'd carefully arranged the dynamic symbol indices, and then the
6951 generic size_dynamic_sections renumbered them out from under us.
6952 Rather than trying somehow to prevent the renumbering, just do
6953 the sort again. */
6954 if (elf_hash_table (info)->dynamic_sections_created)
6955 {
6956 bfd *dynobj;
6957 asection *got;
6958 struct mips_got_info *g;
6959
6960 /* When we resort, we must tell mips_elf_sort_hash_table what
6961 the lowest index it may use is. That's the number of section
6962 symbols we're going to add. The generic ELF linker only
6963 adds these symbols when building a shared object. Note that
6964 we count the sections after (possibly) removing the .options
6965 section above. */
6966 if (! mips_elf_sort_hash_table (info, (info->shared
6967 ? bfd_count_sections (abfd) + 1
6968 : 1)))
6969 return false;
6970
6971 /* Make sure we didn't grow the global .got region. */
6972 dynobj = elf_hash_table (info)->dynobj;
6973 got = bfd_get_section_by_name (dynobj, ".got");
6974 g = (struct mips_got_info *) elf_section_data (got)->tdata;
6975
6976 if (g->global_gotsym != NULL)
6977 BFD_ASSERT ((elf_hash_table (info)->dynsymcount
6978 - g->global_gotsym->dynindx)
6979 <= g->global_gotno);
6980 }
6981
6982 /* On IRIX5, we omit the .options section. On IRIX6, however, we
6983 include it, even though we don't process it quite right. (Some
6984 entries are supposed to be merged.) Empirically, we seem to be
6985 better off including it then not. */
6986 if (IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
6987 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
6988 {
6989 if (strcmp ((*secpp)->name, MIPS_ELF_OPTIONS_SECTION_NAME (abfd)) == 0)
6990 {
6991 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
6992 if (p->type == bfd_indirect_link_order)
6993 p->u.indirect.section->flags &= ~SEC_HAS_CONTENTS;
6994 (*secpp)->link_order_head = NULL;
6995 bfd_section_list_remove (abfd, secpp);
6996 --abfd->section_count;
6997
6998 break;
6999 }
7000 }
7001
7002 /* We include .MIPS.options, even though we don't process it quite right.
7003 (Some entries are supposed to be merged.) At IRIX6 empirically we seem
7004 to be better off including it than not. */
7005 for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
7006 {
7007 if (strcmp ((*secpp)->name, ".MIPS.options") == 0)
7008 {
7009 for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
7010 if (p->type == bfd_indirect_link_order)
7011 p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
7012 (*secpp)->link_order_head = NULL;
7013 bfd_section_list_remove (abfd, secpp);
7014 --abfd->section_count;
7015
7016 break;
7017 }
7018 }
7019
7020 /* Get a value for the GP register. */
7021 if (elf_gp (abfd) == 0)
7022 {
7023 struct bfd_link_hash_entry *h;
7024
7025 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
7026 if (h != (struct bfd_link_hash_entry *) NULL
7027 && h->type == bfd_link_hash_defined)
7028 elf_gp (abfd) = (h->u.def.value
7029 + h->u.def.section->output_section->vma
7030 + h->u.def.section->output_offset);
7031 else if (info->relocateable)
7032 {
7033 bfd_vma lo = MINUS_ONE;
7034
7035 /* Find the GP-relative section with the lowest offset. */
7036 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
7037 if (o->vma < lo
7038 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
7039 lo = o->vma;
7040
7041 /* And calculate GP relative to that. */
7042 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
7043 }
7044 else
7045 {
7046 /* If the relocate_section function needs to do a reloc
7047 involving the GP value, it should make a reloc_dangerous
7048 callback to warn that GP is not defined. */
7049 }
7050 }
7051
7052 /* Go through the sections and collect the .reginfo and .mdebug
7053 information. */
7054 reginfo_sec = NULL;
7055 mdebug_sec = NULL;
7056 gptab_data_sec = NULL;
7057 gptab_bss_sec = NULL;
7058 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
7059 {
7060 if (strcmp (o->name, ".reginfo") == 0)
7061 {
7062 memset (&reginfo, 0, sizeof reginfo);
7063
7064 /* We have found the .reginfo section in the output file.
7065 Look through all the link_orders comprising it and merge
7066 the information together. */
7067 for (p = o->link_order_head;
7068 p != (struct bfd_link_order *) NULL;
7069 p = p->next)
7070 {
7071 asection *input_section;
7072 bfd *input_bfd;
7073 Elf32_External_RegInfo ext;
7074 Elf32_RegInfo sub;
7075
7076 if (p->type != bfd_indirect_link_order)
7077 {
7078 if (p->type == bfd_data_link_order)
7079 continue;
7080 abort ();
7081 }
7082
7083 input_section = p->u.indirect.section;
7084 input_bfd = input_section->owner;
7085
7086 /* The linker emulation code has probably clobbered the
7087 size to be zero bytes. */
7088 if (input_section->_raw_size == 0)
7089 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
7090
7091 if (! bfd_get_section_contents (input_bfd, input_section,
7092 (PTR) &ext,
7093 (file_ptr) 0,
7094 (bfd_size_type) sizeof ext))
7095 return false;
7096
7097 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
7098
7099 reginfo.ri_gprmask |= sub.ri_gprmask;
7100 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
7101 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
7102 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
7103 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
7104
7105 /* ri_gp_value is set by the function
7106 mips_elf32_section_processing when the section is
7107 finally written out. */
7108
7109 /* Hack: reset the SEC_HAS_CONTENTS flag so that
7110 elf_link_input_bfd ignores this section. */
7111 input_section->flags &= ~SEC_HAS_CONTENTS;
7112 }
7113
7114 /* Size has been set in _bfd_mips_elf_always_size_sections. */
7115 BFD_ASSERT(o->_raw_size == sizeof (Elf32_External_RegInfo));
7116
7117 /* Skip this section later on (I don't think this currently
7118 matters, but someday it might). */
7119 o->link_order_head = (struct bfd_link_order *) NULL;
7120
7121 reginfo_sec = o;
7122 }
7123
7124 if (strcmp (o->name, ".mdebug") == 0)
7125 {
7126 struct extsym_info einfo;
7127 bfd_vma last;
7128
7129 /* We have found the .mdebug section in the output file.
7130 Look through all the link_orders comprising it and merge
7131 the information together. */
7132 symhdr->magic = swap->sym_magic;
7133 /* FIXME: What should the version stamp be? */
7134 symhdr->vstamp = 0;
7135 symhdr->ilineMax = 0;
7136 symhdr->cbLine = 0;
7137 symhdr->idnMax = 0;
7138 symhdr->ipdMax = 0;
7139 symhdr->isymMax = 0;
7140 symhdr->ioptMax = 0;
7141 symhdr->iauxMax = 0;
7142 symhdr->issMax = 0;
7143 symhdr->issExtMax = 0;
7144 symhdr->ifdMax = 0;
7145 symhdr->crfd = 0;
7146 symhdr->iextMax = 0;
7147
7148 /* We accumulate the debugging information itself in the
7149 debug_info structure. */
7150 debug.line = NULL;
7151 debug.external_dnr = NULL;
7152 debug.external_pdr = NULL;
7153 debug.external_sym = NULL;
7154 debug.external_opt = NULL;
7155 debug.external_aux = NULL;
7156 debug.ss = NULL;
7157 debug.ssext = debug.ssext_end = NULL;
7158 debug.external_fdr = NULL;
7159 debug.external_rfd = NULL;
7160 debug.external_ext = debug.external_ext_end = NULL;
7161
7162 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
7163 if (mdebug_handle == (PTR) NULL)
7164 return false;
7165
7166 esym.jmptbl = 0;
7167 esym.cobol_main = 0;
7168 esym.weakext = 0;
7169 esym.reserved = 0;
7170 esym.ifd = ifdNil;
7171 esym.asym.iss = issNil;
7172 esym.asym.st = stLocal;
7173 esym.asym.reserved = 0;
7174 esym.asym.index = indexNil;
7175 last = 0;
7176 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
7177 {
7178 esym.asym.sc = sc[i];
7179 s = bfd_get_section_by_name (abfd, secname[i]);
7180 if (s != NULL)
7181 {
7182 esym.asym.value = s->vma;
7183 last = s->vma + s->_raw_size;
7184 }
7185 else
7186 esym.asym.value = last;
7187 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
7188 secname[i], &esym))
7189 return false;
7190 }
7191
7192 for (p = o->link_order_head;
7193 p != (struct bfd_link_order *) NULL;
7194 p = p->next)
7195 {
7196 asection *input_section;
7197 bfd *input_bfd;
7198 const struct ecoff_debug_swap *input_swap;
7199 struct ecoff_debug_info input_debug;
7200 char *eraw_src;
7201 char *eraw_end;
7202
7203 if (p->type != bfd_indirect_link_order)
7204 {
7205 if (p->type == bfd_data_link_order)
7206 continue;
7207 abort ();
7208 }
7209
7210 input_section = p->u.indirect.section;
7211 input_bfd = input_section->owner;
7212
7213 if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
7214 || (get_elf_backend_data (input_bfd)
7215 ->elf_backend_ecoff_debug_swap) == NULL)
7216 {
7217 /* I don't know what a non MIPS ELF bfd would be
7218 doing with a .mdebug section, but I don't really
7219 want to deal with it. */
7220 continue;
7221 }
7222
7223 input_swap = (get_elf_backend_data (input_bfd)
7224 ->elf_backend_ecoff_debug_swap);
7225
7226 BFD_ASSERT (p->size == input_section->_raw_size);
7227
7228 /* The ECOFF linking code expects that we have already
7229 read in the debugging information and set up an
7230 ecoff_debug_info structure, so we do that now. */
7231 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
7232 &input_debug))
7233 return false;
7234
7235 if (! (bfd_ecoff_debug_accumulate
7236 (mdebug_handle, abfd, &debug, swap, input_bfd,
7237 &input_debug, input_swap, info)))
7238 return false;
7239
7240 /* Loop through the external symbols. For each one with
7241 interesting information, try to find the symbol in
7242 the linker global hash table and save the information
7243 for the output external symbols. */
7244 eraw_src = input_debug.external_ext;
7245 eraw_end = (eraw_src
7246 + (input_debug.symbolic_header.iextMax
7247 * input_swap->external_ext_size));
7248 for (;
7249 eraw_src < eraw_end;
7250 eraw_src += input_swap->external_ext_size)
7251 {
7252 EXTR ext;
7253 const char *name;
7254 struct mips_elf_link_hash_entry *h;
7255
7256 (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
7257 if (ext.asym.sc == scNil
7258 || ext.asym.sc == scUndefined
7259 || ext.asym.sc == scSUndefined)
7260 continue;
7261
7262 name = input_debug.ssext + ext.asym.iss;
7263 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
7264 name, false, false, true);
7265 if (h == NULL || h->esym.ifd != -2)
7266 continue;
7267
7268 if (ext.ifd != -1)
7269 {
7270 BFD_ASSERT (ext.ifd
7271 < input_debug.symbolic_header.ifdMax);
7272 ext.ifd = input_debug.ifdmap[ext.ifd];
7273 }
7274
7275 h->esym = ext;
7276 }
7277
7278 /* Free up the information we just read. */
7279 free (input_debug.line);
7280 free (input_debug.external_dnr);
7281 free (input_debug.external_pdr);
7282 free (input_debug.external_sym);
7283 free (input_debug.external_opt);
7284 free (input_debug.external_aux);
7285 free (input_debug.ss);
7286 free (input_debug.ssext);
7287 free (input_debug.external_fdr);
7288 free (input_debug.external_rfd);
7289 free (input_debug.external_ext);
7290
7291 /* Hack: reset the SEC_HAS_CONTENTS flag so that
7292 elf_link_input_bfd ignores this section. */
7293 input_section->flags &= ~SEC_HAS_CONTENTS;
7294 }
7295
7296 if (SGI_COMPAT (abfd) && info->shared)
7297 {
7298 /* Create .rtproc section. */
7299 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
7300 if (rtproc_sec == NULL)
7301 {
7302 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
7303 | SEC_LINKER_CREATED | SEC_READONLY);
7304
7305 rtproc_sec = bfd_make_section (abfd, ".rtproc");
7306 if (rtproc_sec == NULL
7307 || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
7308 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
7309 return false;
7310 }
7311
7312 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
7313 info, rtproc_sec,
7314 &debug))
7315 return false;
7316 }
7317
7318 /* Build the external symbol information. */
7319 einfo.abfd = abfd;
7320 einfo.info = info;
7321 einfo.debug = &debug;
7322 einfo.swap = swap;
7323 einfo.failed = false;
7324 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
7325 mips_elf_output_extsym,
7326 (PTR) &einfo);
7327 if (einfo.failed)
7328 return false;
7329
7330 /* Set the size of the .mdebug section. */
7331 o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
7332
7333 /* Skip this section later on (I don't think this currently
7334 matters, but someday it might). */
7335 o->link_order_head = (struct bfd_link_order *) NULL;
7336
7337 mdebug_sec = o;
7338 }
7339
7340 if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
7341 {
7342 const char *subname;
7343 unsigned int c;
7344 Elf32_gptab *tab;
7345 Elf32_External_gptab *ext_tab;
7346 unsigned int j;
7347
7348 /* The .gptab.sdata and .gptab.sbss sections hold
7349 information describing how the small data area would
7350 change depending upon the -G switch. These sections
7351 not used in executables files. */
7352 if (! info->relocateable)
7353 {
7354 for (p = o->link_order_head;
7355 p != (struct bfd_link_order *) NULL;
7356 p = p->next)
7357 {
7358 asection *input_section;
7359
7360 if (p->type != bfd_indirect_link_order)
7361 {
7362 if (p->type == bfd_data_link_order)
7363 continue;
7364 abort ();
7365 }
7366
7367 input_section = p->u.indirect.section;
7368
7369 /* Hack: reset the SEC_HAS_CONTENTS flag so that
7370 elf_link_input_bfd ignores this section. */
7371 input_section->flags &= ~SEC_HAS_CONTENTS;
7372 }
7373
7374 /* Skip this section later on (I don't think this
7375 currently matters, but someday it might). */
7376 o->link_order_head = (struct bfd_link_order *) NULL;
7377
7378 /* Really remove the section. */
7379 for (secpp = &abfd->sections;
7380 *secpp != o;
7381 secpp = &(*secpp)->next)
7382 ;
7383 bfd_section_list_remove (abfd, secpp);
7384 --abfd->section_count;
7385
7386 continue;
7387 }
7388
7389 /* There is one gptab for initialized data, and one for
7390 uninitialized data. */
7391 if (strcmp (o->name, ".gptab.sdata") == 0)
7392 gptab_data_sec = o;
7393 else if (strcmp (o->name, ".gptab.sbss") == 0)
7394 gptab_bss_sec = o;
7395 else
7396 {
7397 (*_bfd_error_handler)
7398 (_("%s: illegal section name `%s'"),
7399 bfd_get_filename (abfd), o->name);
7400 bfd_set_error (bfd_error_nonrepresentable_section);
7401 return false;
7402 }
7403
7404 /* The linker script always combines .gptab.data and
7405 .gptab.sdata into .gptab.sdata, and likewise for
7406 .gptab.bss and .gptab.sbss. It is possible that there is
7407 no .sdata or .sbss section in the output file, in which
7408 case we must change the name of the output section. */
7409 subname = o->name + sizeof ".gptab" - 1;
7410 if (bfd_get_section_by_name (abfd, subname) == NULL)
7411 {
7412 if (o == gptab_data_sec)
7413 o->name = ".gptab.data";
7414 else
7415 o->name = ".gptab.bss";
7416 subname = o->name + sizeof ".gptab" - 1;
7417 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
7418 }
7419
7420 /* Set up the first entry. */
7421 c = 1;
7422 amt = c * sizeof (Elf32_gptab);
7423 tab = (Elf32_gptab *) bfd_malloc (amt);
7424 if (tab == NULL)
7425 return false;
7426 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
7427 tab[0].gt_header.gt_unused = 0;
7428
7429 /* Combine the input sections. */
7430 for (p = o->link_order_head;
7431 p != (struct bfd_link_order *) NULL;
7432 p = p->next)
7433 {
7434 asection *input_section;
7435 bfd *input_bfd;
7436 bfd_size_type size;
7437 unsigned long last;
7438 bfd_size_type gpentry;
7439
7440 if (p->type != bfd_indirect_link_order)
7441 {
7442 if (p->type == bfd_data_link_order)
7443 continue;
7444 abort ();
7445 }
7446
7447 input_section = p->u.indirect.section;
7448 input_bfd = input_section->owner;
7449
7450 /* Combine the gptab entries for this input section one
7451 by one. We know that the input gptab entries are
7452 sorted by ascending -G value. */
7453 size = bfd_section_size (input_bfd, input_section);
7454 last = 0;
7455 for (gpentry = sizeof (Elf32_External_gptab);
7456 gpentry < size;
7457 gpentry += sizeof (Elf32_External_gptab))
7458 {
7459 Elf32_External_gptab ext_gptab;
7460 Elf32_gptab int_gptab;
7461 unsigned long val;
7462 unsigned long add;
7463 boolean exact;
7464 unsigned int look;
7465
7466 if (! (bfd_get_section_contents
7467 (input_bfd, input_section, (PTR) &ext_gptab,
7468 (file_ptr) gpentry,
7469 (bfd_size_type) sizeof (Elf32_External_gptab))))
7470 {
7471 free (tab);
7472 return false;
7473 }
7474
7475 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
7476 &int_gptab);
7477 val = int_gptab.gt_entry.gt_g_value;
7478 add = int_gptab.gt_entry.gt_bytes - last;
7479
7480 exact = false;
7481 for (look = 1; look < c; look++)
7482 {
7483 if (tab[look].gt_entry.gt_g_value >= val)
7484 tab[look].gt_entry.gt_bytes += add;
7485
7486 if (tab[look].gt_entry.gt_g_value == val)
7487 exact = true;
7488 }
7489
7490 if (! exact)
7491 {
7492 Elf32_gptab *new_tab;
7493 unsigned int max;
7494
7495 /* We need a new table entry. */
7496 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
7497 new_tab = (Elf32_gptab *) bfd_realloc ((PTR) tab, amt);
7498 if (new_tab == NULL)
7499 {
7500 free (tab);
7501 return false;
7502 }
7503 tab = new_tab;
7504 tab[c].gt_entry.gt_g_value = val;
7505 tab[c].gt_entry.gt_bytes = add;
7506
7507 /* Merge in the size for the next smallest -G
7508 value, since that will be implied by this new
7509 value. */
7510 max = 0;
7511 for (look = 1; look < c; look++)
7512 {
7513 if (tab[look].gt_entry.gt_g_value < val
7514 && (max == 0
7515 || (tab[look].gt_entry.gt_g_value
7516 > tab[max].gt_entry.gt_g_value)))
7517 max = look;
7518 }
7519 if (max != 0)
7520 tab[c].gt_entry.gt_bytes +=
7521 tab[max].gt_entry.gt_bytes;
7522
7523 ++c;
7524 }
7525
7526 last = int_gptab.gt_entry.gt_bytes;
7527 }
7528
7529 /* Hack: reset the SEC_HAS_CONTENTS flag so that
7530 elf_link_input_bfd ignores this section. */
7531 input_section->flags &= ~SEC_HAS_CONTENTS;
7532 }
7533
7534 /* The table must be sorted by -G value. */
7535 if (c > 2)
7536 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
7537
7538 /* Swap out the table. */
7539 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
7540 ext_tab = (Elf32_External_gptab *) bfd_alloc (abfd, amt);
7541 if (ext_tab == NULL)
7542 {
7543 free (tab);
7544 return false;
7545 }
7546
7547 for (j = 0; j < c; j++)
7548 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
7549 free (tab);
7550
7551 o->_raw_size = c * sizeof (Elf32_External_gptab);
7552 o->contents = (bfd_byte *) ext_tab;
7553
7554 /* Skip this section later on (I don't think this currently
7555 matters, but someday it might). */
7556 o->link_order_head = (struct bfd_link_order *) NULL;
7557 }
7558 }
7559
7560 /* Invoke the regular ELF backend linker to do all the work. */
7561 if (ABI_64_P (abfd))
7562 {
7563#ifdef BFD64
7564 if (!bfd_elf64_bfd_final_link (abfd, info))
7565 return false;
7566#else
7567 abort ();
7568 return false;
7569#endif /* BFD64 */
7570 }
7571 else if (!bfd_elf32_bfd_final_link (abfd, info))
7572 return false;
7573
7574 /* Now write out the computed sections. */
7575
7576 if (reginfo_sec != (asection *) NULL)
7577 {
7578 Elf32_External_RegInfo ext;
7579
7580 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
7581 if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
7582 (file_ptr) 0,
7583 (bfd_size_type) sizeof ext))
7584 return false;
7585 }
7586
7587 if (mdebug_sec != (asection *) NULL)
7588 {
7589 BFD_ASSERT (abfd->output_has_begun);
7590 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
7591 swap, info,
7592 mdebug_sec->filepos))
7593 return false;
7594
7595 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
7596 }
7597
7598 if (gptab_data_sec != (asection *) NULL)
7599 {
7600 if (! bfd_set_section_contents (abfd, gptab_data_sec,
7601 gptab_data_sec->contents,
7602 (file_ptr) 0,
7603 gptab_data_sec->_raw_size))
7604 return false;
7605 }
7606
7607 if (gptab_bss_sec != (asection *) NULL)
7608 {
7609 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
7610 gptab_bss_sec->contents,
7611 (file_ptr) 0,
7612 gptab_bss_sec->_raw_size))
7613 return false;
7614 }
7615
7616 if (SGI_COMPAT (abfd))
7617 {
7618 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
7619 if (rtproc_sec != NULL)
7620 {
7621 if (! bfd_set_section_contents (abfd, rtproc_sec,
7622 rtproc_sec->contents,
7623 (file_ptr) 0,
7624 rtproc_sec->_raw_size))
7625 return false;
7626 }
7627 }
7628
7629 return true;
7630}
7631\f
7632/* Merge backend specific data from an object file to the output
7633 object file when linking. */
7634
7635boolean
7636_bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
7637 bfd *ibfd;
7638 bfd *obfd;
7639{
7640 flagword old_flags;
7641 flagword new_flags;
7642 boolean ok;
7643 boolean null_input_bfd = true;
7644 asection *sec;
7645
7646 /* Check if we have the same endianess */
82e51918 7647 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
b49e97c9
TS
7648 return false;
7649
7650 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7651 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7652 return true;
7653
7654 new_flags = elf_elfheader (ibfd)->e_flags;
7655 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
7656 old_flags = elf_elfheader (obfd)->e_flags;
7657
7658 if (! elf_flags_init (obfd))
7659 {
7660 elf_flags_init (obfd) = true;
7661 elf_elfheader (obfd)->e_flags = new_flags;
7662 elf_elfheader (obfd)->e_ident[EI_CLASS]
7663 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
7664
7665 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
7666 && bfd_get_arch_info (obfd)->the_default)
7667 {
7668 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
7669 bfd_get_mach (ibfd)))
7670 return false;
7671 }
7672
7673 return true;
7674 }
7675
7676 /* Check flag compatibility. */
7677
7678 new_flags &= ~EF_MIPS_NOREORDER;
7679 old_flags &= ~EF_MIPS_NOREORDER;
7680
7681 if (new_flags == old_flags)
7682 return true;
7683
7684 /* Check to see if the input BFD actually contains any sections.
7685 If not, its flags may not have been initialised either, but it cannot
7686 actually cause any incompatibility. */
7687 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7688 {
7689 /* Ignore synthetic sections and empty .text, .data and .bss sections
7690 which are automatically generated by gas. */
7691 if (strcmp (sec->name, ".reginfo")
7692 && strcmp (sec->name, ".mdebug")
7693 && ((!strcmp (sec->name, ".text")
7694 || !strcmp (sec->name, ".data")
7695 || !strcmp (sec->name, ".bss"))
7696 && sec->_raw_size != 0))
7697 {
7698 null_input_bfd = false;
7699 break;
7700 }
7701 }
7702 if (null_input_bfd)
7703 return true;
7704
7705 ok = true;
7706
7707 if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
7708 {
7709 new_flags &= ~EF_MIPS_PIC;
7710 old_flags &= ~EF_MIPS_PIC;
7711 (*_bfd_error_handler)
7712 (_("%s: linking PIC files with non-PIC files"),
7713 bfd_archive_filename (ibfd));
7714 ok = false;
7715 }
7716
7717 if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
7718 {
7719 new_flags &= ~EF_MIPS_CPIC;
7720 old_flags &= ~EF_MIPS_CPIC;
7721 (*_bfd_error_handler)
7722 (_("%s: linking abicalls files with non-abicalls files"),
7723 bfd_archive_filename (ibfd));
7724 ok = false;
7725 }
7726
7727 /* Compare the ISA's. */
7728 if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
7729 != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
7730 {
7731 int new_mach = new_flags & EF_MIPS_MACH;
7732 int old_mach = old_flags & EF_MIPS_MACH;
7733 int new_isa = elf_mips_isa (new_flags);
7734 int old_isa = elf_mips_isa (old_flags);
7735
7736 /* If either has no machine specified, just compare the general isa's.
7737 Some combinations of machines are ok, if the isa's match. */
7738 if (! new_mach
7739 || ! old_mach
7740 || new_mach == old_mach
7741 )
7742 {
7743 /* Don't warn about mixing code using 32-bit ISAs, or mixing code
7744 using 64-bit ISAs. They will normally use the same data sizes
7745 and calling conventions. */
7746
7747 if (( (new_isa == 1 || new_isa == 2 || new_isa == 32)
7748 ^ (old_isa == 1 || old_isa == 2 || old_isa == 32)) != 0)
7749 {
7750 (*_bfd_error_handler)
7751 (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
7752 bfd_archive_filename (ibfd), new_isa, old_isa);
7753 ok = false;
7754 }
7755 else
7756 {
7757 /* Do we need to update the mach field? */
7758 if (old_mach == 0 && new_mach != 0)
7759 elf_elfheader (obfd)->e_flags |= new_mach;
7760
7761 /* Do we need to update the ISA field? */
7762 if (new_isa > old_isa)
7763 {
7764 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_ARCH;
7765 elf_elfheader (obfd)->e_flags
7766 |= new_flags & EF_MIPS_ARCH;
7767 }
7768 }
7769 }
7770 else
7771 {
7772 (*_bfd_error_handler)
7773 (_("%s: ISA mismatch (%d) with previous modules (%d)"),
7774 bfd_archive_filename (ibfd),
7775 _bfd_elf_mips_mach (new_flags),
7776 _bfd_elf_mips_mach (old_flags));
7777 ok = false;
7778 }
7779
7780 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
7781 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
7782 }
7783
7784 /* Compare ABI's. The 64-bit ABI does not use EF_MIPS_ABI. But, it
7785 does set EI_CLASS differently from any 32-bit ABI. */
7786 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
7787 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7788 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
7789 {
7790 /* Only error if both are set (to different values). */
7791 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
7792 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7793 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
7794 {
7795 (*_bfd_error_handler)
7796 (_("%s: ABI mismatch: linking %s module with previous %s modules"),
7797 bfd_archive_filename (ibfd),
7798 elf_mips_abi_name (ibfd),
7799 elf_mips_abi_name (obfd));
7800 ok = false;
7801 }
7802 new_flags &= ~EF_MIPS_ABI;
7803 old_flags &= ~EF_MIPS_ABI;
7804 }
7805
7806 /* Warn about any other mismatches */
7807 if (new_flags != old_flags)
7808 {
7809 (*_bfd_error_handler)
7810 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
7811 bfd_archive_filename (ibfd), (unsigned long) new_flags,
7812 (unsigned long) old_flags);
7813 ok = false;
7814 }
7815
7816 if (! ok)
7817 {
7818 bfd_set_error (bfd_error_bad_value);
7819 return false;
7820 }
7821
7822 return true;
7823}
7824
7825/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
7826
7827boolean
7828_bfd_mips_elf_set_private_flags (abfd, flags)
7829 bfd *abfd;
7830 flagword flags;
7831{
7832 BFD_ASSERT (!elf_flags_init (abfd)
7833 || elf_elfheader (abfd)->e_flags == flags);
7834
7835 elf_elfheader (abfd)->e_flags = flags;
7836 elf_flags_init (abfd) = true;
7837 return true;
7838}
7839
7840boolean
7841_bfd_mips_elf_print_private_bfd_data (abfd, ptr)
7842 bfd *abfd;
7843 PTR ptr;
7844{
7845 FILE *file = (FILE *) ptr;
7846
7847 BFD_ASSERT (abfd != NULL && ptr != NULL);
7848
7849 /* Print normal ELF private data. */
7850 _bfd_elf_print_private_bfd_data (abfd, ptr);
7851
7852 /* xgettext:c-format */
7853 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
7854
7855 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
7856 fprintf (file, _(" [abi=O32]"));
7857 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
7858 fprintf (file, _(" [abi=O64]"));
7859 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
7860 fprintf (file, _(" [abi=EABI32]"));
7861 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
7862 fprintf (file, _(" [abi=EABI64]"));
7863 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
7864 fprintf (file, _(" [abi unknown]"));
7865 else if (ABI_N32_P (abfd))
7866 fprintf (file, _(" [abi=N32]"));
7867 else if (ABI_64_P (abfd))
7868 fprintf (file, _(" [abi=64]"));
7869 else
7870 fprintf (file, _(" [no abi set]"));
7871
7872 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
7873 fprintf (file, _(" [mips1]"));
7874 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
7875 fprintf (file, _(" [mips2]"));
7876 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
7877 fprintf (file, _(" [mips3]"));
7878 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
7879 fprintf (file, _(" [mips4]"));
7880 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
7881 fprintf (file, _(" [mips5]"));
7882 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
7883 fprintf (file, _(" [mips32]"));
7884 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
7885 fprintf (file, _(" [mips64]"));
7886 else
7887 fprintf (file, _(" [unknown ISA]"));
7888
40d32fc6
CD
7889 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
7890 fprintf (file, _(" [mdmx]"));
7891
7892 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
7893 fprintf (file, _(" [mips16]"));
7894
b49e97c9
TS
7895 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
7896 fprintf (file, _(" [32bitmode]"));
7897 else
7898 fprintf (file, _(" [not 32bitmode]"));
7899
7900 fputc ('\n', file);
7901
7902 return true;
7903}
This page took 0.403831 seconds and 4 git commands to generate.