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