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