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