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