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