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