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