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