Coverage Report

Created: 2023-06-29 07:13

/src/binutils-gdb/bfd/elfxx-mips.c
Line
Count
Source (jump to first uncovered line)
1
/* MIPS-specific support for ELF
2
   Copyright (C) 1993-2023 Free Software Foundation, Inc.
3
4
   Most of the information added by Ian Lance Taylor, Cygnus Support,
5
   <ian@cygnus.com>.
6
   N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
7
   <mark@codesourcery.com>
8
   Traditional MIPS targets support added by Koundinya.K, Dansk Data
9
   Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
10
11
   This file is part of BFD, the Binary File Descriptor library.
12
13
   This program is free software; you can redistribute it and/or modify
14
   it under the terms of the GNU General Public License as published by
15
   the Free Software Foundation; either version 3 of the License, or
16
   (at your option) any later version.
17
18
   This program is distributed in the hope that it will be useful,
19
   but WITHOUT ANY WARRANTY; without even the implied warranty of
20
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
   GNU General Public License for more details.
22
23
   You should have received a copy of the GNU General Public License
24
   along with this program; if not, write to the Free Software
25
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
26
   MA 02110-1301, USA.  */
27
28
29
/* This file handles functionality common to the different MIPS ABI's.  */
30
31
#include "sysdep.h"
32
#include "bfd.h"
33
#include "libbfd.h"
34
#include "libiberty.h"
35
#include "elf-bfd.h"
36
#include "ecoff-bfd.h"
37
#include "elfxx-mips.h"
38
#include "elf/mips.h"
39
#include "elf-vxworks.h"
40
#include "dwarf2.h"
41
42
/* Get the ECOFF swapping routines.  */
43
#include "coff/sym.h"
44
#include "coff/symconst.h"
45
#include "coff/ecoff.h"
46
#include "coff/mips.h"
47
48
#include "hashtab.h"
49
50
/* Types of TLS GOT entry.  */
51
enum mips_got_tls_type {
52
  GOT_TLS_NONE,
53
  GOT_TLS_GD,
54
  GOT_TLS_LDM,
55
  GOT_TLS_IE
56
};
57
58
/* This structure is used to hold information about one GOT entry.
59
   There are four types of entry:
60
61
      (1) an absolute address
62
      requires: abfd == NULL
63
      fields: d.address
64
65
      (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
66
      requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
67
      fields: abfd, symndx, d.addend, tls_type
68
69
      (3) a SYMBOL address, where SYMBOL is not local to an input bfd
70
      requires: abfd != NULL, symndx == -1
71
      fields: d.h, tls_type
72
73
      (4) a TLS LDM slot
74
      requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
75
      fields: none; there's only one of these per GOT.  */
76
struct mips_got_entry
77
{
78
  /* One input bfd that needs the GOT entry.  */
79
  bfd *abfd;
80
  /* The index of the symbol, as stored in the relocation r_info, if
81
     we have a local symbol; -1 otherwise.  */
82
  long symndx;
83
  union
84
  {
85
    /* If abfd == NULL, an address that must be stored in the got.  */
86
    bfd_vma address;
87
    /* If abfd != NULL && symndx != -1, the addend of the relocation
88
       that should be added to the symbol value.  */
89
    bfd_vma addend;
90
    /* If abfd != NULL && symndx == -1, the hash table entry
91
       corresponding to a symbol in the GOT.  The symbol's entry
92
       is in the local area if h->global_got_area is GGA_NONE,
93
       otherwise it is in the global area.  */
94
    struct mips_elf_link_hash_entry *h;
95
  } d;
96
97
  /* The TLS type of this GOT entry.  An LDM GOT entry will be a local
98
     symbol entry with r_symndx == 0.  */
99
  unsigned char tls_type;
100
101
  /* True if we have filled in the GOT contents for a TLS entry,
102
     and created the associated relocations.  */
103
  unsigned char tls_initialized;
104
105
  /* The offset from the beginning of the .got section to the entry
106
     corresponding to this symbol+addend.  If it's a global symbol
107
     whose offset is yet to be decided, it's going to be -1.  */
108
  long gotidx;
109
};
110
111
/* This structure represents a GOT page reference from an input bfd.
112
   Each instance represents a symbol + ADDEND, where the representation
113
   of the symbol depends on whether it is local to the input bfd.
114
   If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
115
   Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
116
117
   Page references with SYMNDX >= 0 always become page references
118
   in the output.  Page references with SYMNDX < 0 only become page
119
   references if the symbol binds locally; in other cases, the page
120
   reference decays to a global GOT reference.  */
121
struct mips_got_page_ref
122
{
123
  long symndx;
124
  union
125
  {
126
    struct mips_elf_link_hash_entry *h;
127
    bfd *abfd;
128
  } u;
129
  bfd_vma addend;
130
};
131
132
/* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
133
   The structures form a non-overlapping list that is sorted by increasing
134
   MIN_ADDEND.  */
135
struct mips_got_page_range
136
{
137
  struct mips_got_page_range *next;
138
  bfd_signed_vma min_addend;
139
  bfd_signed_vma max_addend;
140
};
141
142
/* This structure describes the range of addends that are applied to page
143
   relocations against a given section.  */
144
struct mips_got_page_entry
145
{
146
  /* The section that these entries are based on.  */
147
  asection *sec;
148
  /* The ranges for this page entry.  */
149
  struct mips_got_page_range *ranges;
150
  /* The maximum number of page entries needed for RANGES.  */
151
  bfd_vma num_pages;
152
};
153
154
/* This structure is used to hold .got information when linking.  */
155
156
struct mips_got_info
157
{
158
  /* The number of global .got entries.  */
159
  unsigned int global_gotno;
160
  /* The number of global .got entries that are in the GGA_RELOC_ONLY area.  */
161
  unsigned int reloc_only_gotno;
162
  /* The number of .got slots used for TLS.  */
163
  unsigned int tls_gotno;
164
  /* The first unused TLS .got entry.  Used only during
165
     mips_elf_initialize_tls_index.  */
166
  unsigned int tls_assigned_gotno;
167
  /* The number of local .got entries, eventually including page entries.  */
168
  unsigned int local_gotno;
169
  /* The maximum number of page entries needed.  */
170
  unsigned int page_gotno;
171
  /* The number of relocations needed for the GOT entries.  */
172
  unsigned int relocs;
173
  /* The first unused local .got entry.  */
174
  unsigned int assigned_low_gotno;
175
  /* The last unused local .got entry.  */
176
  unsigned int assigned_high_gotno;
177
  /* A hash table holding members of the got.  */
178
  struct htab *got_entries;
179
  /* A hash table holding mips_got_page_ref structures.  */
180
  struct htab *got_page_refs;
181
  /* A hash table of mips_got_page_entry structures.  */
182
  struct htab *got_page_entries;
183
  /* In multi-got links, a pointer to the next got (err, rather, most
184
     of the time, it points to the previous got).  */
185
  struct mips_got_info *next;
186
};
187
188
/* Structure passed when merging bfds' gots.  */
189
190
struct mips_elf_got_per_bfd_arg
191
{
192
  /* The output bfd.  */
193
  bfd *obfd;
194
  /* The link information.  */
195
  struct bfd_link_info *info;
196
  /* A pointer to the primary got, i.e., the one that's going to get
197
     the implicit relocations from DT_MIPS_LOCAL_GOTNO and
198
     DT_MIPS_GOTSYM.  */
199
  struct mips_got_info *primary;
200
  /* A non-primary got we're trying to merge with other input bfd's
201
     gots.  */
202
  struct mips_got_info *current;
203
  /* The maximum number of got entries that can be addressed with a
204
     16-bit offset.  */
205
  unsigned int max_count;
206
  /* The maximum number of page entries needed by each got.  */
207
  unsigned int max_pages;
208
  /* The total number of global entries which will live in the
209
     primary got and be automatically relocated.  This includes
210
     those not referenced by the primary GOT but included in
211
     the "master" GOT.  */
212
  unsigned int global_count;
213
};
214
215
/* A structure used to pass information to htab_traverse callbacks
216
   when laying out the GOT.  */
217
218
struct mips_elf_traverse_got_arg
219
{
220
  struct bfd_link_info *info;
221
  struct mips_got_info *g;
222
  int value;
223
};
224
225
struct _mips_elf_section_data
226
{
227
  struct bfd_elf_section_data elf;
228
  union
229
  {
230
    bfd_byte *tdata;
231
  } u;
232
};
233
234
#define mips_elf_section_data(sec) \
235
0
  ((struct _mips_elf_section_data *) elf_section_data (sec))
236
237
#define is_mips_elf(bfd)        \
238
0
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour  \
239
0
   && elf_tdata (bfd) != NULL        \
240
0
   && elf_object_id (bfd) == MIPS_ELF_DATA)
241
242
/* The ABI says that every symbol used by dynamic relocations must have
243
   a global GOT entry.  Among other things, this provides the dynamic
244
   linker with a free, directly-indexed cache.  The GOT can therefore
245
   contain symbols that are not referenced by GOT relocations themselves
246
   (in other words, it may have symbols that are not referenced by things
247
   like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
248
249
   GOT relocations are less likely to overflow if we put the associated
250
   GOT entries towards the beginning.  We therefore divide the global
251
   GOT entries into two areas: "normal" and "reloc-only".  Entries in
252
   the first area can be used for both dynamic relocations and GP-relative
253
   accesses, while those in the "reloc-only" area are for dynamic
254
   relocations only.
255
256
   These GGA_* ("Global GOT Area") values are organised so that lower
257
   values are more general than higher values.  Also, non-GGA_NONE
258
   values are ordered by the position of the area in the GOT.  */
259
0
#define GGA_NORMAL 0
260
0
#define GGA_RELOC_ONLY 1
261
0
#define GGA_NONE 2
262
263
/* Information about a non-PIC interface to a PIC function.  There are
264
   two ways of creating these interfaces.  The first is to add:
265
266
  lui $25,%hi(func)
267
  addiu $25,$25,%lo(func)
268
269
   immediately before a PIC function "func".  The second is to add:
270
271
  lui $25,%hi(func)
272
  j func
273
  addiu $25,$25,%lo(func)
274
275
   to a separate trampoline section.
276
277
   Stubs of the first kind go in a new section immediately before the
278
   target function.  Stubs of the second kind go in a single section
279
   pointed to by the hash table's "strampoline" field.  */
280
struct mips_elf_la25_stub {
281
  /* The generated section that contains this stub.  */
282
  asection *stub_section;
283
284
  /* The offset of the stub from the start of STUB_SECTION.  */
285
  bfd_vma offset;
286
287
  /* One symbol for the original function.  Its location is available
288
     in H->root.root.u.def.  */
289
  struct mips_elf_link_hash_entry *h;
290
};
291
292
/* Macros for populating a mips_elf_la25_stub.  */
293
294
#define LA25_LUI(VAL) (0x3c190000 | (VAL))  /* lui t9,VAL */
295
#define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
296
#define LA25_BC(VAL) (0xc8000000 | (((VAL) >> 2) & 0x3ffffff)) /* bc VAL */
297
#define LA25_ADDIU(VAL) (0x27390000 | (VAL))  /* addiu t9,t9,VAL */
298
#define LA25_LUI_MICROMIPS(VAL)           \
299
0
  (0x41b90000 | (VAL))        /* lui t9,VAL */
300
#define LA25_J_MICROMIPS(VAL)           \
301
0
  (0xd4000000 | (((VAL) >> 1) & 0x3ffffff))  /* j VAL */
302
#define LA25_ADDIU_MICROMIPS(VAL)         \
303
0
  (0x33390000 | (VAL))        /* addiu t9,t9,VAL */
304
305
/* This structure is passed to mips_elf_sort_hash_table_f when sorting
306
   the dynamic symbols.  */
307
308
struct mips_elf_hash_sort_data
309
{
310
  /* The symbol in the global GOT with the lowest dynamic symbol table
311
     index.  */
312
  struct elf_link_hash_entry *low;
313
  /* The least dynamic symbol table index corresponding to a non-TLS
314
     symbol with a GOT entry.  */
315
  bfd_size_type min_got_dynindx;
316
  /* The greatest dynamic symbol table index corresponding to a symbol
317
     with a GOT entry that is not referenced (e.g., a dynamic symbol
318
     with dynamic relocations pointing to it from non-primary GOTs).  */
319
  bfd_size_type max_unref_got_dynindx;
320
  /* The greatest dynamic symbol table index corresponding to a local
321
     symbol.  */
322
  bfd_size_type max_local_dynindx;
323
  /* The greatest dynamic symbol table index corresponding to an external
324
     symbol without a GOT entry.  */
325
  bfd_size_type max_non_got_dynindx;
326
  /* If non-NULL, output BFD for .MIPS.xhash finalization.  */
327
  bfd *output_bfd;
328
  /* If non-NULL, pointer to contents of .MIPS.xhash for filling in
329
     real final dynindx.  */
330
  bfd_byte *mipsxhash;
331
};
332
333
/* We make up to two PLT entries if needed, one for standard MIPS code
334
   and one for compressed code, either a MIPS16 or microMIPS one.  We
335
   keep a separate record of traditional lazy-binding stubs, for easier
336
   processing.  */
337
338
struct plt_entry
339
{
340
  /* Traditional SVR4 stub offset, or -1 if none.  */
341
  bfd_vma stub_offset;
342
343
  /* Standard PLT entry offset, or -1 if none.  */
344
  bfd_vma mips_offset;
345
346
  /* Compressed PLT entry offset, or -1 if none.  */
347
  bfd_vma comp_offset;
348
349
  /* The corresponding .got.plt index, or -1 if none.  */
350
  bfd_vma gotplt_index;
351
352
  /* Whether we need a standard PLT entry.  */
353
  unsigned int need_mips : 1;
354
355
  /* Whether we need a compressed PLT entry.  */
356
  unsigned int need_comp : 1;
357
};
358
359
/* The MIPS ELF linker needs additional information for each symbol in
360
   the global hash table.  */
361
362
struct mips_elf_link_hash_entry
363
{
364
  struct elf_link_hash_entry root;
365
366
  /* External symbol information.  */
367
  EXTR esym;
368
369
  /* The la25 stub we have created for ths symbol, if any.  */
370
  struct mips_elf_la25_stub *la25_stub;
371
372
  /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
373
     this symbol.  */
374
  unsigned int possibly_dynamic_relocs;
375
376
  /* If there is a stub that 32 bit functions should use to call this
377
     16 bit function, this points to the section containing the stub.  */
378
  asection *fn_stub;
379
380
  /* If there is a stub that 16 bit functions should use to call this
381
     32 bit function, this points to the section containing the stub.  */
382
  asection *call_stub;
383
384
  /* This is like the call_stub field, but it is used if the function
385
     being called returns a floating point value.  */
386
  asection *call_fp_stub;
387
388
  /* If non-zero, location in .MIPS.xhash to write real final dynindx.  */
389
  bfd_vma mipsxhash_loc;
390
391
  /* The highest GGA_* value that satisfies all references to this symbol.  */
392
  unsigned int global_got_area : 2;
393
394
  /* True if all GOT relocations against this symbol are for calls.  This is
395
     a looser condition than no_fn_stub below, because there may be other
396
     non-call non-GOT relocations against the symbol.  */
397
  unsigned int got_only_for_calls : 1;
398
399
  /* True if one of the relocations described by possibly_dynamic_relocs
400
     is against a readonly section.  */
401
  unsigned int readonly_reloc : 1;
402
403
  /* True if there is a relocation against this symbol that must be
404
     resolved by the static linker (in other words, if the relocation
405
     cannot possibly be made dynamic).  */
406
  unsigned int has_static_relocs : 1;
407
408
  /* True if we must not create a .MIPS.stubs entry for this symbol.
409
     This is set, for example, if there are relocations related to
410
     taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
411
     See "MIPS ABI Supplement, 3rd Edition", p. 4-20.  */
412
  unsigned int no_fn_stub : 1;
413
414
  /* Whether we need the fn_stub; this is true if this symbol appears
415
     in any relocs other than a 16 bit call.  */
416
  unsigned int need_fn_stub : 1;
417
418
  /* True if this symbol is referenced by branch relocations from
419
     any non-PIC input file.  This is used to determine whether an
420
     la25 stub is required.  */
421
  unsigned int has_nonpic_branches : 1;
422
423
  /* Does this symbol need a traditional MIPS lazy-binding stub
424
     (as opposed to a PLT entry)?  */
425
  unsigned int needs_lazy_stub : 1;
426
427
  /* Does this symbol resolve to a PLT entry?  */
428
  unsigned int use_plt_entry : 1;
429
};
430
431
/* MIPS ELF linker hash table.  */
432
433
struct mips_elf_link_hash_table
434
{
435
  struct elf_link_hash_table root;
436
437
  /* The number of .rtproc entries.  */
438
  bfd_size_type procedure_count;
439
440
  /* The size of the .compact_rel section (if SGI_COMPAT).  */
441
  bfd_size_type compact_rel_size;
442
443
  /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
444
     is set to the address of __rld_obj_head as in IRIX5 and IRIX6.  */
445
  bool use_rld_obj_head;
446
447
  /* The  __rld_map or __rld_obj_head symbol. */
448
  struct elf_link_hash_entry *rld_symbol;
449
450
  /* This is set if we see any mips16 stub sections.  */
451
  bool mips16_stubs_seen;
452
453
  /* True if we can generate copy relocs and PLTs.  */
454
  bool use_plts_and_copy_relocs;
455
456
  /* True if we can only use 32-bit microMIPS instructions.  */
457
  bool insn32;
458
459
  /* True if we suppress checks for invalid branches between ISA modes.  */
460
  bool ignore_branch_isa;
461
462
  /* True if we are targetting R6 compact branches.  */
463
  bool compact_branches;
464
465
  /* True if we already reported the small-data section overflow.  */
466
  bool small_data_overflow_reported;
467
468
  /* True if we use the special `__gnu_absolute_zero' symbol.  */
469
  bool use_absolute_zero;
470
471
  /* True if we have been configured for a GNU target.  */
472
  bool gnu_target;
473
474
  /* Shortcuts to some dynamic sections, or NULL if they are not
475
     being used.  */
476
  asection *srelplt2;
477
  asection *sstubs;
478
479
  /* The master GOT information.  */
480
  struct mips_got_info *got_info;
481
482
  /* The global symbol in the GOT with the lowest index in the dynamic
483
     symbol table.  */
484
  struct elf_link_hash_entry *global_gotsym;
485
486
  /* The size of the PLT header in bytes.  */
487
  bfd_vma plt_header_size;
488
489
  /* The size of a standard PLT entry in bytes.  */
490
  bfd_vma plt_mips_entry_size;
491
492
  /* The size of a compressed PLT entry in bytes.  */
493
  bfd_vma plt_comp_entry_size;
494
495
  /* The offset of the next standard PLT entry to create.  */
496
  bfd_vma plt_mips_offset;
497
498
  /* The offset of the next compressed PLT entry to create.  */
499
  bfd_vma plt_comp_offset;
500
501
  /* The index of the next .got.plt entry to create.  */
502
  bfd_vma plt_got_index;
503
504
  /* The number of functions that need a lazy-binding stub.  */
505
  bfd_vma lazy_stub_count;
506
507
  /* The size of a function stub entry in bytes.  */
508
  bfd_vma function_stub_size;
509
510
  /* The number of reserved entries at the beginning of the GOT.  */
511
  unsigned int reserved_gotno;
512
513
  /* The section used for mips_elf_la25_stub trampolines.
514
     See the comment above that structure for details.  */
515
  asection *strampoline;
516
517
  /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
518
     pairs.  */
519
  htab_t la25_stubs;
520
521
  /* A function FN (NAME, IS, OS) that creates a new input section
522
     called NAME and links it to output section OS.  If IS is nonnull,
523
     the new section should go immediately before it, otherwise it
524
     should go at the (current) beginning of OS.
525
526
     The function returns the new section on success, otherwise it
527
     returns null.  */
528
  asection *(*add_stub_section) (const char *, asection *, asection *);
529
530
  /* Is the PLT header compressed?  */
531
  unsigned int plt_header_is_comp : 1;
532
};
533
534
/* Get the MIPS ELF linker hash table from a link_info structure.  */
535
536
#define mips_elf_hash_table(p) \
537
0
  ((is_elf_hash_table ((p)->hash)          \
538
0
    && elf_hash_table_id (elf_hash_table (p)) == MIPS_ELF_DATA)   \
539
0
   ? (struct mips_elf_link_hash_table *) (p)->hash : NULL)
540
541
/* A structure used to communicate with htab_traverse callbacks.  */
542
struct mips_htab_traverse_info
543
{
544
  /* The usual link-wide information.  */
545
  struct bfd_link_info *info;
546
  bfd *output_bfd;
547
548
  /* Starts off FALSE and is set to TRUE if the link should be aborted.  */
549
  bool error;
550
};
551
552
/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
553
   R_MIPS_GOT16.  REL is the relocation, INPUT_SECTION is the section
554
   that contains the relocation field and DATA points to the start of
555
   INPUT_SECTION.  */
556
557
struct mips_hi16
558
{
559
  struct mips_hi16 *next;
560
  bfd_byte *data;
561
  asection *input_section;
562
  arelent rel;
563
};
564
565
/* MIPS ELF private object data.  */
566
567
struct mips_elf_obj_tdata
568
{
569
  /* Generic ELF private object data.  */
570
  struct elf_obj_tdata root;
571
572
  /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output.  */
573
  bfd *abi_fp_bfd;
574
575
  /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output.  */
576
  bfd *abi_msa_bfd;
577
578
  /* The abiflags for this object.  */
579
  Elf_Internal_ABIFlags_v0 abiflags;
580
  bool abiflags_valid;
581
582
  /* The GOT requirements of input bfds.  */
583
  struct mips_got_info *got;
584
585
  /* Used by _bfd_mips_elf_find_nearest_line.  The structure could be
586
     included directly in this one, but there's no point to wasting
587
     the memory just for the infrequently called find_nearest_line.  */
588
  struct mips_elf_find_line *find_line_info;
589
590
  /* An array of stub sections indexed by symbol number.  */
591
  asection **local_stubs;
592
  asection **local_call_stubs;
593
594
  /* The Irix 5 support uses two virtual sections, which represent
595
     text/data symbols defined in dynamic objects.  */
596
  asymbol *elf_data_symbol;
597
  asymbol *elf_text_symbol;
598
  asection *elf_data_section;
599
  asection *elf_text_section;
600
601
  struct mips_hi16 *mips_hi16_list;
602
};
603
604
/* Get MIPS ELF private object data from BFD's tdata.  */
605
606
#define mips_elf_tdata(bfd) \
607
738k
  ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
608
609
#define TLS_RELOC_P(r_type) \
610
0
  (r_type == R_MIPS_TLS_DTPMOD32    \
611
0
   || r_type == R_MIPS_TLS_DTPMOD64    \
612
0
   || r_type == R_MIPS_TLS_DTPREL32    \
613
0
   || r_type == R_MIPS_TLS_DTPREL64    \
614
0
   || r_type == R_MIPS_TLS_GD      \
615
0
   || r_type == R_MIPS_TLS_LDM      \
616
0
   || r_type == R_MIPS_TLS_DTPREL_HI16    \
617
0
   || r_type == R_MIPS_TLS_DTPREL_LO16    \
618
0
   || r_type == R_MIPS_TLS_GOTTPREL    \
619
0
   || r_type == R_MIPS_TLS_TPREL32    \
620
0
   || r_type == R_MIPS_TLS_TPREL64    \
621
0
   || r_type == R_MIPS_TLS_TPREL_HI16    \
622
0
   || r_type == R_MIPS_TLS_TPREL_LO16    \
623
0
   || r_type == R_MIPS16_TLS_GD      \
624
0
   || r_type == R_MIPS16_TLS_LDM    \
625
0
   || r_type == R_MIPS16_TLS_DTPREL_HI16  \
626
0
   || r_type == R_MIPS16_TLS_DTPREL_LO16  \
627
0
   || r_type == R_MIPS16_TLS_GOTTPREL    \
628
0
   || r_type == R_MIPS16_TLS_TPREL_HI16    \
629
0
   || r_type == R_MIPS16_TLS_TPREL_LO16    \
630
0
   || r_type == R_MICROMIPS_TLS_GD    \
631
0
   || r_type == R_MICROMIPS_TLS_LDM    \
632
0
   || r_type == R_MICROMIPS_TLS_DTPREL_HI16  \
633
0
   || r_type == R_MICROMIPS_TLS_DTPREL_LO16  \
634
0
   || r_type == R_MICROMIPS_TLS_GOTTPREL  \
635
0
   || r_type == R_MICROMIPS_TLS_TPREL_HI16  \
636
0
   || r_type == R_MICROMIPS_TLS_TPREL_LO16)
637
638
/* Structure used to pass information to mips_elf_output_extsym.  */
639
640
struct extsym_info
641
{
642
  bfd *abfd;
643
  struct bfd_link_info *info;
644
  struct ecoff_debug_info *debug;
645
  const struct ecoff_debug_swap *swap;
646
  bool failed;
647
};
648
649
/* The names of the runtime procedure table symbols used on IRIX5.  */
650
651
static const char * const mips_elf_dynsym_rtproc_names[] =
652
{
653
  "_procedure_table",
654
  "_procedure_string_table",
655
  "_procedure_table_size",
656
  NULL
657
};
658
659
/* These structures are used to generate the .compact_rel section on
660
   IRIX5.  */
661
662
typedef struct
663
{
664
  unsigned long id1;    /* Always one?  */
665
  unsigned long num;    /* Number of compact relocation entries.  */
666
  unsigned long id2;    /* Always two?  */
667
  unsigned long offset;   /* The file offset of the first relocation.  */
668
  unsigned long reserved0;  /* Zero?  */
669
  unsigned long reserved1;  /* Zero?  */
670
} Elf32_compact_rel;
671
672
typedef struct
673
{
674
  bfd_byte id1[4];
675
  bfd_byte num[4];
676
  bfd_byte id2[4];
677
  bfd_byte offset[4];
678
  bfd_byte reserved0[4];
679
  bfd_byte reserved1[4];
680
} Elf32_External_compact_rel;
681
682
typedef struct
683
{
684
  unsigned int ctype : 1; /* 1: long 0: short format. See below.  */
685
  unsigned int rtype : 4; /* Relocation types. See below.  */
686
  unsigned int dist2to : 8;
687
  unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
688
  unsigned long konst;    /* KONST field. See below.  */
689
  unsigned long vaddr;    /* VADDR to be relocated.  */
690
} Elf32_crinfo;
691
692
typedef struct
693
{
694
  unsigned int ctype : 1; /* 1: long 0: short format. See below.  */
695
  unsigned int rtype : 4; /* Relocation types. See below.  */
696
  unsigned int dist2to : 8;
697
  unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
698
  unsigned long konst;    /* KONST field. See below.  */
699
} Elf32_crinfo2;
700
701
typedef struct
702
{
703
  bfd_byte info[4];
704
  bfd_byte konst[4];
705
  bfd_byte vaddr[4];
706
} Elf32_External_crinfo;
707
708
typedef struct
709
{
710
  bfd_byte info[4];
711
  bfd_byte konst[4];
712
} Elf32_External_crinfo2;
713
714
/* These are the constants used to swap the bitfields in a crinfo.  */
715
716
0
#define CRINFO_CTYPE (0x1U)
717
0
#define CRINFO_CTYPE_SH (31)
718
0
#define CRINFO_RTYPE (0xfU)
719
0
#define CRINFO_RTYPE_SH (27)
720
0
#define CRINFO_DIST2TO (0xffU)
721
0
#define CRINFO_DIST2TO_SH (19)
722
0
#define CRINFO_RELVADDR (0x7ffffU)
723
0
#define CRINFO_RELVADDR_SH (0)
724
725
/* A compact relocation info has long (3 words) or short (2 words)
726
   formats.  A short format doesn't have VADDR field and relvaddr
727
   fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
728
#define CRF_MIPS_LONG     1
729
#define CRF_MIPS_SHORT      0
730
731
/* There are 4 types of compact relocation at least. The value KONST
732
   has different meaning for each type:
733
734
   (type)   (konst)
735
   CT_MIPS_REL32  Address in data
736
   CT_MIPS_WORD   Address in word (XXX)
737
   CT_MIPS_GPHI_LO  GP - vaddr
738
   CT_MIPS_JMPAD  Address to jump
739
   */
740
741
#define CRT_MIPS_REL32      0xa
742
#define CRT_MIPS_WORD     0xb
743
#define CRT_MIPS_GPHI_LO    0xc
744
#define CRT_MIPS_JMPAD      0xd
745
746
0
#define mips_elf_set_cr_format(x,format)  ((x).ctype = (format))
747
0
#define mips_elf_set_cr_type(x,type)    ((x).rtype = (type))
748
0
#define mips_elf_set_cr_dist2to(x,v)    ((x).dist2to = (v))
749
0
#define mips_elf_set_cr_relvaddr(x,d)   ((x).relvaddr = (d)<<2)
750

751
/* The structure of the runtime procedure descriptor created by the
752
   loader for use by the static exception system.  */
753
754
typedef struct runtime_pdr {
755
  bfd_vma adr;    /* Memory address of start of procedure.  */
756
  long  regmask;  /* Save register mask.  */
757
  long  regoffset;  /* Save register offset.  */
758
  long  fregmask; /* Save floating point register mask.  */
759
  long  fregoffset; /* Save floating point register offset.  */
760
  long  frameoffset;  /* Frame size.  */
761
  short framereg; /* Frame pointer register.  */
762
  short pcreg;    /* Offset or reg of return pc.  */
763
  long  irpss;    /* Index into the runtime string table.  */
764
  long  reserved;
765
  struct exception_info *exception_info;/* Pointer to exception array.  */
766
} RPDR, *pRPDR;
767
#define cbRPDR sizeof (RPDR)
768
#define rpdNil ((pRPDR) 0)
769

770
static struct mips_got_entry *mips_elf_create_local_got_entry
771
  (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
772
   struct mips_elf_link_hash_entry *, int);
773
static bool mips_elf_sort_hash_table_f
774
  (struct mips_elf_link_hash_entry *, void *);
775
static bfd_vma mips_elf_high
776
  (bfd_vma);
777
static bool mips_elf_create_dynamic_relocation
778
  (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
779
   struct mips_elf_link_hash_entry *, asection *, bfd_vma,
780
   bfd_vma *, asection *);
781
static bfd_vma mips_elf_adjust_gp
782
  (bfd *, struct mips_got_info *, bfd *);
783
784
/* This will be used when we sort the dynamic relocation records.  */
785
static bfd *reldyn_sorting_bfd;
786
787
/* True if ABFD is for CPUs with load interlocking that include
788
   non-MIPS1 CPUs and R3900.  */
789
#define LOAD_INTERLOCKS_P(abfd) \
790
0
  (   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
791
0
   || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
792
793
/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
794
   This should be safe for all architectures.  We enable this predicate
795
   for RM9000 for now.  */
796
#define JAL_TO_BAL_P(abfd) \
797
0
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
798
799
/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
800
   This should be safe for all architectures.  We enable this predicate for
801
   all CPUs.  */
802
0
#define JALR_TO_BAL_P(abfd) 1
803
804
/* True if ABFD is for CPUs that are faster if JR is converted to B.
805
   This should be safe for all architectures.  We enable this predicate for
806
   all CPUs.  */
807
0
#define JR_TO_B_P(abfd) 1
808
809
/* True if ABFD is a PIC object.  */
810
#define PIC_OBJECT_P(abfd) \
811
0
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
812
813
/* Nonzero if ABFD is using the O32 ABI.  */
814
#define ABI_O32_P(abfd) \
815
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
816
817
/* Nonzero if ABFD is using the N32 ABI.  */
818
#define ABI_N32_P(abfd) \
819
1.62k
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
820
821
/* Nonzero if ABFD is using the N64 ABI.  */
822
#define ABI_64_P(abfd) \
823
1.58k
  (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
824
825
/* Nonzero if ABFD is using NewABI conventions.  */
826
711
#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
827
828
/* Nonzero if ABFD has microMIPS code.  */
829
#define MICROMIPS_P(abfd) \
830
693
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
831
832
/* Nonzero if ABFD is MIPS R6.  */
833
#define MIPSR6_P(abfd) \
834
0
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6 \
835
0
    || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
836
837
/* The IRIX compatibility level we are striving for.  */
838
#define IRIX_COMPAT(abfd) \
839
539
  (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
840
841
/* Whether we are trying to be compatible with IRIX at all.  */
842
#define SGI_COMPAT(abfd) \
843
974
  (IRIX_COMPAT (abfd) != ict_none)
844
845
/* The name of the options section.  */
846
#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
847
705
  (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
848
849
/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
850
   Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME.  */
851
#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
852
2.75k
  (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
853
854
/* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section.  */
855
#define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
856
13
  (strcmp (NAME, ".MIPS.abiflags") == 0)
857
858
/* Whether the section is readonly.  */
859
#define MIPS_ELF_READONLY_SECTION(sec) \
860
0
  ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))    \
861
0
   == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
862
863
/* The name of the stub section.  */
864
0
#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
865
866
/* The size of an external REL relocation.  */
867
#define MIPS_ELF_REL_SIZE(abfd) \
868
0
  (get_elf_backend_data (abfd)->s->sizeof_rel)
869
870
/* The size of an external RELA relocation.  */
871
#define MIPS_ELF_RELA_SIZE(abfd) \
872
0
  (get_elf_backend_data (abfd)->s->sizeof_rela)
873
874
/* The size of an external dynamic table entry.  */
875
#define MIPS_ELF_DYN_SIZE(abfd) \
876
0
  (get_elf_backend_data (abfd)->s->sizeof_dyn)
877
878
/* The size of a GOT entry.  */
879
#define MIPS_ELF_GOT_SIZE(abfd) \
880
0
  (get_elf_backend_data (abfd)->s->arch_size / 8)
881
882
/* The size of the .rld_map section. */
883
#define MIPS_ELF_RLD_MAP_SIZE(abfd) \
884
0
  (get_elf_backend_data (abfd)->s->arch_size / 8)
885
886
/* The size of a symbol-table entry.  */
887
#define MIPS_ELF_SYM_SIZE(abfd) \
888
0
  (get_elf_backend_data (abfd)->s->sizeof_sym)
889
890
/* The default alignment for sections, as a power of two.  */
891
#define MIPS_ELF_LOG_FILE_ALIGN(abfd)       \
892
0
  (get_elf_backend_data (abfd)->s->log_file_align)
893
894
/* Get word-sized data.  */
895
#define MIPS_ELF_GET_WORD(abfd, ptr) \
896
  (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
897
898
/* Put out word-sized data.  */
899
#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
900
0
  (ABI_64_P (abfd)       \
901
0
   ? bfd_put_64 (abfd, val, ptr)   \
902
0
   : bfd_put_32 (abfd, val, ptr))
903
904
/* The opcode for word-sized loads (LW or LD).  */
905
#define MIPS_ELF_LOAD_WORD(abfd) \
906
0
  (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
907
908
/* Add a dynamic symbol table-entry.  */
909
#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val)  \
910
0
  _bfd_elf_add_dynamic_entry (info, tag, val)
911
912
#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela)      \
913
9
  (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
914
915
/* The name of the dynamic relocation section.  */
916
#define MIPS_ELF_REL_DYN_NAME(INFO) \
917
0
  (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
918
0
   ? ".rela.dyn" : ".rel.dyn")
919
920
/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
921
   from smaller values.  Start with zero, widen, *then* decrement.  */
922
0
#define MINUS_ONE (((bfd_vma)0) - 1)
923
0
#define MINUS_TWO (((bfd_vma)0) - 2)
924
925
/* The value to write into got[1] for SVR4 targets, to identify it is
926
   a GNU object.  The dynamic linker can then use got[1] to store the
927
   module pointer.  */
928
#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
929
  ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
930
931
/* The offset of $gp from the beginning of the .got section.  */
932
#define ELF_MIPS_GP_OFFSET(INFO) \
933
0
  (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
934
0
   ? 0x0 : 0x7ff0)
935
936
/* The maximum size of the GOT for it to be addressable using 16-bit
937
   offsets from $gp.  */
938
0
#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
939
940
/* Instructions which appear in a stub.  */
941
#define STUB_LW(abfd)             \
942
  ((ABI_64_P (abfd)             \
943
    ? 0xdf998010        /* ld t9,0x8010(gp) */  \
944
    : 0x8f998010))        /* lw t9,0x8010(gp) */
945
#define STUB_MOVE 0x03e07825      /* or t7,ra,zero */
946
#define STUB_LUI(VAL) (0x3c180000 + (VAL))  /* lui t8,VAL */
947
#define STUB_JALR 0x0320f809      /* jalr ra,t9 */
948
#define STUB_JALRC 0xf8190000     /* jalrc ra,t9 */
949
#define STUB_ORI(VAL) (0x37180000 + (VAL))  /* ori t8,t8,VAL */
950
#define STUB_LI16U(VAL) (0x34180000 + (VAL))  /* ori t8,zero,VAL unsigned */
951
#define STUB_LI16S(abfd, VAL)           \
952
   ((ABI_64_P (abfd)              \
953
    ? (0x64180000 + (VAL))  /* daddiu t8,zero,VAL sign extended */  \
954
    : (0x24180000 + (VAL))))  /* addiu t8,zero,VAL sign extended */
955
956
/* Likewise for the microMIPS ASE.  */
957
#define STUB_LW_MICROMIPS(abfd)           \
958
0
  (ABI_64_P (abfd)             \
959
0
   ? 0xdf3c8010          /* ld t9,0x8010(gp) */  \
960
0
   : 0xff3c8010)        /* lw t9,0x8010(gp) */
961
#define STUB_MOVE_MICROMIPS 0x0dff    /* move t7,ra */
962
0
#define STUB_MOVE32_MICROMIPS 0x001f7a90  /* or t7,ra,zero */
963
#define STUB_LUI_MICROMIPS(VAL)           \
964
0
   (0x41b80000 + (VAL))        /* lui t8,VAL */
965
#define STUB_JALR_MICROMIPS 0x45d9    /* jalr t9 */
966
0
#define STUB_JALR32_MICROMIPS 0x03f90f3c  /* jalr ra,t9 */
967
#define STUB_ORI_MICROMIPS(VAL)           \
968
0
  (0x53180000 + (VAL))        /* ori t8,t8,VAL */
969
#define STUB_LI16U_MICROMIPS(VAL)         \
970
0
  (0x53000000 + (VAL))        /* ori t8,zero,VAL unsigned */
971
#define STUB_LI16S_MICROMIPS(abfd, VAL)         \
972
0
   (ABI_64_P (abfd)             \
973
0
    ? 0x5f000000 + (VAL)  /* daddiu t8,zero,VAL sign extended */  \
974
0
    : 0x33000000 + (VAL))  /* addiu t8,zero,VAL sign extended */
975
976
0
#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
977
0
#define MIPS_FUNCTION_STUB_BIG_SIZE 20
978
0
#define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
979
0
#define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
980
0
#define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
981
0
#define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
982
983
/* The name of the dynamic interpreter.  This is put in the .interp
984
   section.  */
985
986
#define ELF_DYNAMIC_INTERPRETER(abfd)   \
987
0
   (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1"  \
988
0
    : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1"  \
989
0
    : "/usr/lib/libc.so.1")
990
991
#ifdef BFD64
992
#define MNAME(bfd,pre,pos) \
993
  (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
994
#define ELF_R_SYM(bfd, i)         \
995
0
  (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
996
#define ELF_R_TYPE(bfd, i)          \
997
0
  (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
998
#define ELF_R_INFO(bfd, s, t)         \
999
0
  (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
1000
#else
1001
#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
1002
#define ELF_R_SYM(bfd, i)         \
1003
  (ELF32_R_SYM (i))
1004
#define ELF_R_TYPE(bfd, i)          \
1005
  (ELF32_R_TYPE (i))
1006
#define ELF_R_INFO(bfd, s, t)         \
1007
  (ELF32_R_INFO (s, t))
1008
#endif
1009

1010
  /* The mips16 compiler uses a couple of special sections to handle
1011
     floating point arguments.
1012
1013
     Section names that look like .mips16.fn.FNNAME contain stubs that
1014
     copy floating point arguments from the fp regs to the gp regs and
1015
     then jump to FNNAME.  If any 32 bit function calls FNNAME, the
1016
     call should be redirected to the stub instead.  If no 32 bit
1017
     function calls FNNAME, the stub should be discarded.  We need to
1018
     consider any reference to the function, not just a call, because
1019
     if the address of the function is taken we will need the stub,
1020
     since the address might be passed to a 32 bit function.
1021
1022
     Section names that look like .mips16.call.FNNAME contain stubs
1023
     that copy floating point arguments from the gp regs to the fp
1024
     regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
1025
     then any 16 bit function that calls FNNAME should be redirected
1026
     to the stub instead.  If FNNAME is not a 32 bit function, the
1027
     stub should be discarded.
1028
1029
     .mips16.call.fp.FNNAME sections are similar, but contain stubs
1030
     which call FNNAME and then copy the return value from the fp regs
1031
     to the gp regs.  These stubs store the return value in $18 while
1032
     calling FNNAME; any function which might call one of these stubs
1033
     must arrange to save $18 around the call.  (This case is not
1034
     needed for 32 bit functions that call 16 bit functions, because
1035
     16 bit functions always return floating point values in both
1036
     $f0/$f1 and $2/$3.)
1037
1038
     Note that in all cases FNNAME might be defined statically.
1039
     Therefore, FNNAME is not used literally.  Instead, the relocation
1040
     information will indicate which symbol the section is for.
1041
1042
     We record any stubs that we find in the symbol table.  */
1043
1044
0
#define FN_STUB ".mips16.fn."
1045
0
#define CALL_STUB ".mips16.call."
1046
0
#define CALL_FP_STUB ".mips16.call.fp."
1047
1048
0
#define FN_STUB_P(name) startswith (name, FN_STUB)
1049
0
#define CALL_STUB_P(name) startswith (name, CALL_STUB)
1050
0
#define CALL_FP_STUB_P(name) startswith (name, CALL_FP_STUB)
1051

1052
/* The format of the first PLT entry in an O32 executable.  */
1053
static const bfd_vma mips_o32_exec_plt0_entry[] =
1054
{
1055
  0x3c1c0000, /* lui $28, %hi(&GOTPLT[0])       */
1056
  0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28)       */
1057
  0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0])      */
1058
  0x031cc023, /* subu $24, $24, $28         */
1059
  0x03e07825, /* or t7, ra, zero          */
1060
  0x0018c082, /* srl $24, $24, 2          */
1061
  0x0320f809, /* jalr $25           */
1062
  0x2718fffe  /* subu $24, $24, 2         */
1063
};
1064
1065
/* The format of the first PLT entry in an O32 executable using compact
1066
   jumps.  */
1067
static const bfd_vma mipsr6_o32_exec_plt0_entry_compact[] =
1068
{
1069
  0x3c1c0000, /* lui $28, %hi(&GOTPLT[0])       */
1070
  0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28)       */
1071
  0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0])      */
1072
  0x031cc023, /* subu $24, $24, $28         */
1073
  0x03e07821, /* move $15, $31  # 32-bit move (addu)    */
1074
  0x0018c082, /* srl $24, $24, 2          */
1075
  0x2718fffe, /* subu $24, $24, 2         */
1076
  0xf8190000  /* jalrc $25            */
1077
};
1078
1079
/* The format of the first PLT entry in an N32 executable.  Different
1080
   because gp ($28) is not available; we use t2 ($14) instead.  */
1081
static const bfd_vma mips_n32_exec_plt0_entry[] =
1082
{
1083
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1084
  0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14)       */
1085
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1086
  0x030ec023, /* subu $24, $24, $14         */
1087
  0x03e07825, /* or t7, ra, zero          */
1088
  0x0018c082, /* srl $24, $24, 2          */
1089
  0x0320f809, /* jalr $25           */
1090
  0x2718fffe  /* subu $24, $24, 2         */
1091
};
1092
1093
/* The format of the first PLT entry in an N32 executable using compact
1094
   jumps.  Different because gp ($28) is not available; we use t2 ($14)
1095
   instead.  */
1096
static const bfd_vma mipsr6_n32_exec_plt0_entry_compact[] =
1097
{
1098
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1099
  0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14)       */
1100
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1101
  0x030ec023, /* subu $24, $24, $14         */
1102
  0x03e07821, /* move $15, $31  # 32-bit move (addu)    */
1103
  0x0018c082, /* srl $24, $24, 2          */
1104
  0x2718fffe, /* subu $24, $24, 2         */
1105
  0xf8190000  /* jalrc $25            */
1106
};
1107
1108
/* The format of the first PLT entry in an N64 executable.  Different
1109
   from N32 because of the increased size of GOT entries.  */
1110
static const bfd_vma mips_n64_exec_plt0_entry[] =
1111
{
1112
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1113
  0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14)       */
1114
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1115
  0x030ec023, /* subu $24, $24, $14         */
1116
  0x03e07825, /* or t7, ra, zero          */
1117
  0x0018c0c2, /* srl $24, $24, 3          */
1118
  0x0320f809, /* jalr $25           */
1119
  0x2718fffe  /* subu $24, $24, 2         */
1120
};
1121
1122
/* The format of the first PLT entry in an N64 executable using compact
1123
   jumps.  Different from N32 because of the increased size of GOT
1124
   entries.  */
1125
static const bfd_vma mipsr6_n64_exec_plt0_entry_compact[] =
1126
{
1127
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1128
  0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14)       */
1129
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1130
  0x030ec023, /* subu $24, $24, $14         */
1131
  0x03e0782d, /* move $15, $31  # 64-bit move (daddu)   */
1132
  0x0018c0c2, /* srl $24, $24, 3          */
1133
  0x2718fffe, /* subu $24, $24, 2         */
1134
  0xf8190000  /* jalrc $25            */
1135
};
1136
1137
1138
/* The format of the microMIPS first PLT entry in an O32 executable.
1139
   We rely on v0 ($2) rather than t8 ($24) to contain the address
1140
   of the GOTPLT entry handled, so this stub may only be used when
1141
   all the subsequent PLT entries are microMIPS code too.
1142
1143
   The trailing NOP is for alignment and correct disassembly only.  */
1144
static const bfd_vma micromips_o32_exec_plt0_entry[] =
1145
{
1146
  0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - .     */
1147
  0xff23, 0x0000, /* lw $25, 0($3)        */
1148
  0x0535,   /* subu $2, $2, $3        */
1149
  0x2525,   /* srl $2, $2, 2        */
1150
  0x3302, 0xfffe, /* subu $24, $2, 2        */
1151
  0x0dff,   /* move $15, $31        */
1152
  0x45f9,   /* jalrs $25          */
1153
  0x0f83,   /* move $28, $3         */
1154
  0x0c00    /* nop            */
1155
};
1156
1157
/* The format of the microMIPS first PLT entry in an O32 executable
1158
   in the insn32 mode.  */
1159
static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1160
{
1161
  0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0])     */
1162
  0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28)     */
1163
  0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0])    */
1164
  0x0398, 0xc1d0, /* subu $24, $24, $28       */
1165
  0x001f, 0x7a90, /* or $15, $31, zero        */
1166
  0x0318, 0x1040, /* srl $24, $24, 2        */
1167
  0x03f9, 0x0f3c, /* jalr $25         */
1168
  0x3318, 0xfffe  /* subu $24, $24, 2       */
1169
};
1170
1171
/* The format of subsequent standard PLT entries.  */
1172
static const bfd_vma mips_exec_plt_entry[] =
1173
{
1174
  0x3c0f0000, /* lui $15, %hi(.got.plt entry)     */
1175
  0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15)    */
1176
  0x25f80000, /* addiu $24, $15, %lo(.got.plt entry)    */
1177
  0x03200008  /* jr $25         */
1178
};
1179
1180
static const bfd_vma mipsr6_exec_plt_entry[] =
1181
{
1182
  0x3c0f0000, /* lui $15, %hi(.got.plt entry)     */
1183
  0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15)    */
1184
  0x25f80000, /* addiu $24, $15, %lo(.got.plt entry)    */
1185
  0x03200009  /* jr $25         */
1186
};
1187
1188
static const bfd_vma mipsr6_exec_plt_entry_compact[] =
1189
{
1190
  0x3c0f0000, /* lui $15, %hi(.got.plt entry)     */
1191
  0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15)    */
1192
  0x25f80000, /* addiu $24, $15, %lo(.got.plt entry)    */
1193
  0xd8190000  /* jic $25, 0         */
1194
};
1195
1196
/* The format of subsequent MIPS16 o32 PLT entries.  We use v0 ($2)
1197
   and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1198
   directly addressable.  */
1199
static const bfd_vma mips16_o32_exec_plt_entry[] =
1200
{
1201
  0xb203,   /* lw $2, 12($pc)     */
1202
  0x9a60,   /* lw $3, 0($2)       */
1203
  0x651a,   /* move $24, $2       */
1204
  0xeb00,   /* jr $3        */
1205
  0x653b,   /* move $25, $3       */
1206
  0x6500,   /* nop          */
1207
  0x0000, 0x0000  /* .word (.got.plt entry)   */
1208
};
1209
1210
/* The format of subsequent microMIPS o32 PLT entries.  We use v0 ($2)
1211
   as a temporary because t8 ($24) is not addressable with ADDIUPC.  */
1212
static const bfd_vma micromips_o32_exec_plt_entry[] =
1213
{
1214
  0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1215
  0xff22, 0x0000, /* lw $25, 0($2)      */
1216
  0x4599,   /* jr $25       */
1217
  0x0f02    /* move $24, $2       */
1218
};
1219
1220
/* The format of subsequent microMIPS o32 PLT entries in the insn32 mode.  */
1221
static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1222
{
1223
  0x41af, 0x0000, /* lui $15, %hi(.got.plt entry)   */
1224
  0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1225
  0x0019, 0x0f3c, /* jr $25       */
1226
  0x330f, 0x0000  /* addiu $24, $15, %lo(.got.plt entry)  */
1227
};
1228
1229
/* The format of the first PLT entry in a VxWorks executable.  */
1230
static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1231
{
1232
  0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_)   */
1233
  0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1234
  0x8f390008, /* lw t9, 8(t9)         */
1235
  0x00000000, /* nop            */
1236
  0x03200008, /* jr t9          */
1237
  0x00000000  /* nop            */
1238
};
1239
1240
/* The format of subsequent PLT entries.  */
1241
static const bfd_vma mips_vxworks_exec_plt_entry[] =
1242
{
1243
  0x10000000, /* b .PLT_resolver      */
1244
  0x24180000, /* li t8, <pltindex>      */
1245
  0x3c190000, /* lui t9, %hi(<.got.plt slot>)   */
1246
  0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1247
  0x8f390000, /* lw t9, 0(t9)       */
1248
  0x00000000, /* nop          */
1249
  0x03200008, /* jr t9        */
1250
  0x00000000  /* nop          */
1251
};
1252
1253
/* The format of the first PLT entry in a VxWorks shared object.  */
1254
static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1255
{
1256
  0x8f990008, /* lw t9, 8(gp)   */
1257
  0x00000000, /* nop      */
1258
  0x03200008, /* jr t9    */
1259
  0x00000000, /* nop      */
1260
  0x00000000, /* nop      */
1261
  0x00000000  /* nop      */
1262
};
1263
1264
/* The format of subsequent PLT entries.  */
1265
static const bfd_vma mips_vxworks_shared_plt_entry[] =
1266
{
1267
  0x10000000, /* b .PLT_resolver  */
1268
  0x24180000  /* li t8, <pltindex>  */
1269
};
1270

1271
/* microMIPS 32-bit opcode helper installer.  */
1272
1273
static void
1274
bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1275
0
{
1276
0
  bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1277
0
  bfd_put_16 (abfd,  opcode    & 0xffff, ptr + 2);
1278
0
}
1279
1280
/* microMIPS 32-bit opcode helper retriever.  */
1281
1282
static bfd_vma
1283
bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1284
0
{
1285
0
  return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1286
0
}
1287

1288
/* Look up an entry in a MIPS ELF linker hash table.  */
1289
1290
#define mips_elf_link_hash_lookup(table, string, create, copy, follow)  \
1291
0
  ((struct mips_elf_link_hash_entry *)          \
1292
0
   elf_link_hash_lookup (&(table)->root, (string), (create),    \
1293
0
       (copy), (follow)))
1294
1295
/* Traverse a MIPS ELF linker hash table.  */
1296
1297
#define mips_elf_link_hash_traverse(table, func, info)      \
1298
0
  (elf_link_hash_traverse           \
1299
0
   (&(table)->root,              \
1300
0
    (bool (*) (struct elf_link_hash_entry *, void *)) (func),   \
1301
0
    (info)))
1302
1303
/* Find the base offsets for thread-local storage in this object,
1304
   for GD/LD and IE/LE respectively.  */
1305
1306
0
#define TP_OFFSET 0x7000
1307
0
#define DTP_OFFSET 0x8000
1308
1309
static bfd_vma
1310
dtprel_base (struct bfd_link_info *info)
1311
0
{
1312
  /* If tls_sec is NULL, we should have signalled an error already.  */
1313
0
  if (elf_hash_table (info)->tls_sec == NULL)
1314
0
    return 0;
1315
0
  return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1316
0
}
1317
1318
static bfd_vma
1319
tprel_base (struct bfd_link_info *info)
1320
0
{
1321
  /* If tls_sec is NULL, we should have signalled an error already.  */
1322
0
  if (elf_hash_table (info)->tls_sec == NULL)
1323
0
    return 0;
1324
0
  return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1325
0
}
1326
1327
/* Create an entry in a MIPS ELF linker hash table.  */
1328
1329
static struct bfd_hash_entry *
1330
mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1331
          struct bfd_hash_table *table, const char *string)
1332
0
{
1333
0
  struct mips_elf_link_hash_entry *ret =
1334
0
    (struct mips_elf_link_hash_entry *) entry;
1335
1336
  /* Allocate the structure if it has not already been allocated by a
1337
     subclass.  */
1338
0
  if (ret == NULL)
1339
0
    ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1340
0
  if (ret == NULL)
1341
0
    return (struct bfd_hash_entry *) ret;
1342
1343
  /* Call the allocation method of the superclass.  */
1344
0
  ret = ((struct mips_elf_link_hash_entry *)
1345
0
   _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1346
0
             table, string));
1347
0
  if (ret != NULL)
1348
0
    {
1349
      /* Set local fields.  */
1350
0
      memset (&ret->esym, 0, sizeof (EXTR));
1351
      /* We use -2 as a marker to indicate that the information has
1352
   not been set.  -1 means there is no associated ifd.  */
1353
0
      ret->esym.ifd = -2;
1354
0
      ret->la25_stub = 0;
1355
0
      ret->possibly_dynamic_relocs = 0;
1356
0
      ret->fn_stub = NULL;
1357
0
      ret->call_stub = NULL;
1358
0
      ret->call_fp_stub = NULL;
1359
0
      ret->mipsxhash_loc = 0;
1360
0
      ret->global_got_area = GGA_NONE;
1361
0
      ret->got_only_for_calls = true;
1362
0
      ret->readonly_reloc = false;
1363
0
      ret->has_static_relocs = false;
1364
0
      ret->no_fn_stub = false;
1365
0
      ret->need_fn_stub = false;
1366
0
      ret->has_nonpic_branches = false;
1367
0
      ret->needs_lazy_stub = false;
1368
0
      ret->use_plt_entry = false;
1369
0
    }
1370
1371
0
  return (struct bfd_hash_entry *) ret;
1372
0
}
1373
1374
/* Allocate MIPS ELF private object data.  */
1375
1376
bool
1377
_bfd_mips_elf_mkobject (bfd *abfd)
1378
1.56M
{
1379
1.56M
  return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1380
1.56M
          MIPS_ELF_DATA);
1381
1.56M
}
1382
1383
/* MIPS ELF uses a special find_nearest_line routine in order the
1384
   handle the ECOFF debugging information.  */
1385
1386
struct mips_elf_find_line
1387
{
1388
  struct ecoff_debug_info d;
1389
  struct ecoff_find_line i;
1390
};
1391
1392
bool
1393
_bfd_mips_elf_free_cached_info (bfd *abfd)
1394
62.0k
{
1395
62.0k
  struct mips_elf_obj_tdata *tdata;
1396
1397
62.0k
  if ((bfd_get_format (abfd) == bfd_object
1398
62.0k
       || bfd_get_format (abfd) == bfd_core)
1399
62.0k
      && (tdata = mips_elf_tdata (abfd)) != NULL)
1400
25.8k
    {
1401
25.8k
      BFD_ASSERT (tdata->root.object_id == MIPS_ELF_DATA);
1402
25.9k
      while (tdata->mips_hi16_list != NULL)
1403
18
  {
1404
18
    struct mips_hi16 *hi = tdata->mips_hi16_list;
1405
18
    tdata->mips_hi16_list = hi->next;
1406
18
    free (hi);
1407
18
  }
1408
25.8k
      if (tdata->find_line_info != NULL)
1409
272
  _bfd_ecoff_free_ecoff_debug_info (&tdata->find_line_info->d);
1410
25.8k
    }
1411
62.0k
  return _bfd_elf_free_cached_info (abfd);
1412
62.0k
}
1413
1414
bool
1415
_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1416
336k
{
1417
336k
  if (!sec->used_by_bfd)
1418
336k
    {
1419
336k
      struct _mips_elf_section_data *sdata;
1420
336k
      size_t amt = sizeof (*sdata);
1421
1422
336k
      sdata = bfd_zalloc (abfd, amt);
1423
336k
      if (sdata == NULL)
1424
0
  return false;
1425
336k
      sec->used_by_bfd = sdata;
1426
336k
    }
1427
1428
336k
  return _bfd_elf_new_section_hook (abfd, sec);
1429
336k
}
1430

1431
/* Read ECOFF debugging information from a .mdebug section into a
1432
   ecoff_debug_info structure.  */
1433
1434
bool
1435
_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1436
             struct ecoff_debug_info *debug)
1437
3.74k
{
1438
3.74k
  HDRR *symhdr;
1439
3.74k
  const struct ecoff_debug_swap *swap;
1440
3.74k
  char *ext_hdr;
1441
1442
3.74k
  swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1443
3.74k
  memset (debug, 0, sizeof (*debug));
1444
1445
3.74k
  ext_hdr = bfd_malloc (swap->external_hdr_size);
1446
3.74k
  if (ext_hdr == NULL && swap->external_hdr_size != 0)
1447
0
    goto error_return;
1448
1449
3.74k
  if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1450
3.74k
          swap->external_hdr_size))
1451
228
    goto error_return;
1452
1453
3.51k
  symhdr = &debug->symbolic_header;
1454
3.51k
  (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1455
3.51k
  free (ext_hdr);
1456
3.51k
  ext_hdr = NULL;
1457
1458
  /* The symbolic header contains absolute file offsets and sizes to
1459
     read.  */
1460
3.51k
#define READ(ptr, offset, count, size)          \
1461
17.7k
  do                  \
1462
17.7k
    {                 \
1463
17.7k
      size_t amt;             \
1464
17.7k
      debug->ptr = NULL;            \
1465
17.7k
      if (symhdr->count == 0)           \
1466
17.7k
  break;               \
1467
17.7k
      if (_bfd_mul_overflow (size, symhdr->count, &amt))   \
1468
6.90k
  {               \
1469
0
    bfd_set_error (bfd_error_file_too_big);     \
1470
0
    goto error_return;            \
1471
0
  }                \
1472
6.90k
      if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0)   \
1473
6.90k
  goto error_return;           \
1474
6.90k
      debug->ptr = _bfd_malloc_and_read (abfd, amt + 1, amt);   \
1475
5.49k
      if (debug->ptr == NULL)           \
1476
5.49k
  goto error_return;           \
1477
5.49k
      ((char *) debug->ptr)[amt] = 0;         \
1478
3.66k
    } while (0)
1479
1480
3.51k
  READ (line, cbLineOffset, cbLine, sizeof (unsigned char));
1481
2.31k
  READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size);
1482
2.05k
  READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size);
1483
1.86k
  READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size);
1484
1.73k
  READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size);
1485
1.54k
  READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext));
1486
1.34k
  READ (ss, cbSsOffset, issMax, sizeof (char));
1487
1.18k
  READ (ssext, cbSsExtOffset, issExtMax, sizeof (char));
1488
974
  READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size);
1489
704
  READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size);
1490
525
  READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size);
1491
272
#undef READ
1492
1493
272
  return true;
1494
1495
3.46k
 error_return:
1496
3.46k
  free (ext_hdr);
1497
3.46k
  _bfd_ecoff_free_ecoff_debug_info (debug);
1498
3.46k
  return false;
1499
525
}
1500

1501
/* Swap RPDR (runtime procedure table entry) for output.  */
1502
1503
static void
1504
ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1505
0
{
1506
0
  H_PUT_S32 (abfd, in->adr, ex->p_adr);
1507
0
  H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1508
0
  H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1509
0
  H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1510
0
  H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1511
0
  H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1512
1513
0
  H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1514
0
  H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1515
1516
0
  H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1517
0
}
1518
1519
/* Create a runtime procedure table from the .mdebug section.  */
1520
1521
static bool
1522
mips_elf_create_procedure_table (void *handle, bfd *abfd,
1523
         struct bfd_link_info *info, asection *s,
1524
         struct ecoff_debug_info *debug)
1525
0
{
1526
0
  const struct ecoff_debug_swap *swap;
1527
0
  HDRR *hdr = &debug->symbolic_header;
1528
0
  RPDR *rpdr, *rp;
1529
0
  struct rpdr_ext *erp;
1530
0
  void *rtproc;
1531
0
  struct pdr_ext *epdr;
1532
0
  struct sym_ext *esym;
1533
0
  char *ss, **sv;
1534
0
  char *str;
1535
0
  bfd_size_type size;
1536
0
  bfd_size_type count;
1537
0
  unsigned long sindex;
1538
0
  unsigned long i;
1539
0
  PDR pdr;
1540
0
  SYMR sym;
1541
0
  const char *no_name_func = _("static procedure (no name)");
1542
1543
0
  epdr = NULL;
1544
0
  rpdr = NULL;
1545
0
  esym = NULL;
1546
0
  ss = NULL;
1547
0
  sv = NULL;
1548
1549
0
  swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1550
1551
0
  sindex = strlen (no_name_func) + 1;
1552
0
  count = hdr->ipdMax;
1553
0
  if (count > 0)
1554
0
    {
1555
0
      size = swap->external_pdr_size;
1556
1557
0
      epdr = bfd_malloc (size * count);
1558
0
      if (epdr == NULL)
1559
0
  goto error_return;
1560
1561
0
      if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1562
0
  goto error_return;
1563
1564
0
      size = sizeof (RPDR);
1565
0
      rp = rpdr = bfd_malloc (size * count);
1566
0
      if (rpdr == NULL)
1567
0
  goto error_return;
1568
1569
0
      size = sizeof (char *);
1570
0
      sv = bfd_malloc (size * count);
1571
0
      if (sv == NULL)
1572
0
  goto error_return;
1573
1574
0
      count = hdr->isymMax;
1575
0
      size = swap->external_sym_size;
1576
0
      esym = bfd_malloc (size * count);
1577
0
      if (esym == NULL)
1578
0
  goto error_return;
1579
1580
0
      if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1581
0
  goto error_return;
1582
1583
0
      count = hdr->issMax;
1584
0
      ss = bfd_malloc (count);
1585
0
      if (ss == NULL)
1586
0
  goto error_return;
1587
0
      if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1588
0
  goto error_return;
1589
1590
0
      count = hdr->ipdMax;
1591
0
      for (i = 0; i < (unsigned long) count; i++, rp++)
1592
0
  {
1593
0
    (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1594
0
    (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1595
0
    rp->adr = sym.value;
1596
0
    rp->regmask = pdr.regmask;
1597
0
    rp->regoffset = pdr.regoffset;
1598
0
    rp->fregmask = pdr.fregmask;
1599
0
    rp->fregoffset = pdr.fregoffset;
1600
0
    rp->frameoffset = pdr.frameoffset;
1601
0
    rp->framereg = pdr.framereg;
1602
0
    rp->pcreg = pdr.pcreg;
1603
0
    rp->irpss = sindex;
1604
0
    sv[i] = ss + sym.iss;
1605
0
    sindex += strlen (sv[i]) + 1;
1606
0
  }
1607
0
    }
1608
1609
0
  size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1610
0
  size = BFD_ALIGN (size, 16);
1611
0
  rtproc = bfd_alloc (abfd, size);
1612
0
  if (rtproc == NULL)
1613
0
    {
1614
0
      mips_elf_hash_table (info)->procedure_count = 0;
1615
0
      goto error_return;
1616
0
    }
1617
1618
0
  mips_elf_hash_table (info)->procedure_count = count + 2;
1619
1620
0
  erp = rtproc;
1621
0
  memset (erp, 0, sizeof (struct rpdr_ext));
1622
0
  erp++;
1623
0
  str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1624
0
  strcpy (str, no_name_func);
1625
0
  str += strlen (no_name_func) + 1;
1626
0
  for (i = 0; i < count; i++)
1627
0
    {
1628
0
      ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1629
0
      strcpy (str, sv[i]);
1630
0
      str += strlen (sv[i]) + 1;
1631
0
    }
1632
0
  H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1633
1634
  /* Set the size and contents of .rtproc section.  */
1635
0
  s->size = size;
1636
0
  s->contents = rtproc;
1637
1638
  /* Skip this section later on (I don't think this currently
1639
     matters, but someday it might).  */
1640
0
  s->map_head.link_order = NULL;
1641
1642
0
  free (epdr);
1643
0
  free (rpdr);
1644
0
  free (esym);
1645
0
  free (ss);
1646
0
  free (sv);
1647
0
  return true;
1648
1649
0
 error_return:
1650
0
  free (epdr);
1651
0
  free (rpdr);
1652
0
  free (esym);
1653
0
  free (ss);
1654
0
  free (sv);
1655
0
  return false;
1656
0
}
1657

1658
/* We're going to create a stub for H.  Create a symbol for the stub's
1659
   value and size, to help make the disassembly easier to read.  */
1660
1661
static bool
1662
mips_elf_create_stub_symbol (struct bfd_link_info *info,
1663
           struct mips_elf_link_hash_entry *h,
1664
           const char *prefix, asection *s, bfd_vma value,
1665
           bfd_vma size)
1666
0
{
1667
0
  bool micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
1668
0
  struct bfd_link_hash_entry *bh;
1669
0
  struct elf_link_hash_entry *elfh;
1670
0
  char *name;
1671
0
  bool res;
1672
1673
0
  if (micromips_p)
1674
0
    value |= 1;
1675
1676
  /* Create a new symbol.  */
1677
0
  name = concat (prefix, h->root.root.root.string, NULL);
1678
0
  bh = NULL;
1679
0
  res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1680
0
            BSF_LOCAL, s, value, NULL,
1681
0
            true, false, &bh);
1682
0
  free (name);
1683
0
  if (! res)
1684
0
    return false;
1685
1686
  /* Make it a local function.  */
1687
0
  elfh = (struct elf_link_hash_entry *) bh;
1688
0
  elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1689
0
  elfh->size = size;
1690
0
  elfh->forced_local = 1;
1691
0
  if (micromips_p)
1692
0
    elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
1693
0
  return true;
1694
0
}
1695
1696
/* We're about to redefine H.  Create a symbol to represent H's
1697
   current value and size, to help make the disassembly easier
1698
   to read.  */
1699
1700
static bool
1701
mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1702
             struct mips_elf_link_hash_entry *h,
1703
             const char *prefix)
1704
0
{
1705
0
  struct bfd_link_hash_entry *bh;
1706
0
  struct elf_link_hash_entry *elfh;
1707
0
  char *name;
1708
0
  asection *s;
1709
0
  bfd_vma value;
1710
0
  bool res;
1711
1712
  /* Read the symbol's value.  */
1713
0
  BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1714
0
        || h->root.root.type == bfd_link_hash_defweak);
1715
0
  s = h->root.root.u.def.section;
1716
0
  value = h->root.root.u.def.value;
1717
1718
  /* Create a new symbol.  */
1719
0
  name = concat (prefix, h->root.root.root.string, NULL);
1720
0
  bh = NULL;
1721
0
  res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1722
0
            BSF_LOCAL, s, value, NULL,
1723
0
            true, false, &bh);
1724
0
  free (name);
1725
0
  if (! res)
1726
0
    return false;
1727
1728
  /* Make it local and copy the other attributes from H.  */
1729
0
  elfh = (struct elf_link_hash_entry *) bh;
1730
0
  elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1731
0
  elfh->other = h->root.other;
1732
0
  elfh->size = h->root.size;
1733
0
  elfh->forced_local = 1;
1734
0
  return true;
1735
0
}
1736
1737
/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1738
   function rather than to a hard-float stub.  */
1739
1740
static bool
1741
section_allows_mips16_refs_p (asection *section)
1742
0
{
1743
0
  const char *name;
1744
1745
0
  name = bfd_section_name (section);
1746
0
  return (FN_STUB_P (name)
1747
0
    || CALL_STUB_P (name)
1748
0
    || CALL_FP_STUB_P (name)
1749
0
    || strcmp (name, ".pdr") == 0);
1750
0
}
1751
1752
/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1753
   stub section of some kind.  Return the R_SYMNDX of the target
1754
   function, or 0 if we can't decide which function that is.  */
1755
1756
static unsigned long
1757
mips16_stub_symndx (const struct elf_backend_data *bed,
1758
        asection *sec ATTRIBUTE_UNUSED,
1759
        const Elf_Internal_Rela *relocs,
1760
        const Elf_Internal_Rela *relend)
1761
0
{
1762
0
  int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1763
0
  const Elf_Internal_Rela *rel;
1764
1765
  /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1766
     one in a compound relocation.  */
1767
0
  for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1768
0
    if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1769
0
      return ELF_R_SYM (sec->owner, rel->r_info);
1770
1771
  /* Otherwise trust the first relocation, whatever its kind.  This is
1772
     the traditional behavior.  */
1773
0
  if (relocs < relend)
1774
0
    return ELF_R_SYM (sec->owner, relocs->r_info);
1775
1776
0
  return 0;
1777
0
}
1778
1779
/* Check the mips16 stubs for a particular symbol, and see if we can
1780
   discard them.  */
1781
1782
static void
1783
mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1784
           struct mips_elf_link_hash_entry *h)
1785
0
{
1786
  /* Dynamic symbols must use the standard call interface, in case other
1787
     objects try to call them.  */
1788
0
  if (h->fn_stub != NULL
1789
0
      && h->root.dynindx != -1)
1790
0
    {
1791
0
      mips_elf_create_shadow_symbol (info, h, ".mips16.");
1792
0
      h->need_fn_stub = true;
1793
0
    }
1794
1795
0
  if (h->fn_stub != NULL
1796
0
      && ! h->need_fn_stub)
1797
0
    {
1798
      /* We don't need the fn_stub; the only references to this symbol
1799
   are 16 bit calls.  Clobber the size to 0 to prevent it from
1800
   being included in the link.  */
1801
0
      h->fn_stub->size = 0;
1802
0
      h->fn_stub->flags &= ~SEC_RELOC;
1803
0
      h->fn_stub->reloc_count = 0;
1804
0
      h->fn_stub->flags |= SEC_EXCLUDE;
1805
0
      h->fn_stub->output_section = bfd_abs_section_ptr;
1806
0
    }
1807
1808
0
  if (h->call_stub != NULL
1809
0
      && ELF_ST_IS_MIPS16 (h->root.other))
1810
0
    {
1811
      /* We don't need the call_stub; this is a 16 bit function, so
1812
   calls from other 16 bit functions are OK.  Clobber the size
1813
   to 0 to prevent it from being included in the link.  */
1814
0
      h->call_stub->size = 0;
1815
0
      h->call_stub->flags &= ~SEC_RELOC;
1816
0
      h->call_stub->reloc_count = 0;
1817
0
      h->call_stub->flags |= SEC_EXCLUDE;
1818
0
      h->call_stub->output_section = bfd_abs_section_ptr;
1819
0
    }
1820
1821
0
  if (h->call_fp_stub != NULL
1822
0
      && ELF_ST_IS_MIPS16 (h->root.other))
1823
0
    {
1824
      /* We don't need the call_stub; this is a 16 bit function, so
1825
   calls from other 16 bit functions are OK.  Clobber the size
1826
   to 0 to prevent it from being included in the link.  */
1827
0
      h->call_fp_stub->size = 0;
1828
0
      h->call_fp_stub->flags &= ~SEC_RELOC;
1829
0
      h->call_fp_stub->reloc_count = 0;
1830
0
      h->call_fp_stub->flags |= SEC_EXCLUDE;
1831
0
      h->call_fp_stub->output_section = bfd_abs_section_ptr;
1832
0
    }
1833
0
}
1834
1835
/* Hashtable callbacks for mips_elf_la25_stubs.  */
1836
1837
static hashval_t
1838
mips_elf_la25_stub_hash (const void *entry_)
1839
0
{
1840
0
  const struct mips_elf_la25_stub *entry;
1841
1842
0
  entry = (struct mips_elf_la25_stub *) entry_;
1843
0
  return entry->h->root.root.u.def.section->id
1844
0
    + entry->h->root.root.u.def.value;
1845
0
}
1846
1847
static int
1848
mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1849
0
{
1850
0
  const struct mips_elf_la25_stub *entry1, *entry2;
1851
1852
0
  entry1 = (struct mips_elf_la25_stub *) entry1_;
1853
0
  entry2 = (struct mips_elf_la25_stub *) entry2_;
1854
0
  return ((entry1->h->root.root.u.def.section
1855
0
     == entry2->h->root.root.u.def.section)
1856
0
    && (entry1->h->root.root.u.def.value
1857
0
        == entry2->h->root.root.u.def.value));
1858
0
}
1859
1860
/* Called by the linker to set up the la25 stub-creation code.  FN is
1861
   the linker's implementation of add_stub_function.  Return true on
1862
   success.  */
1863
1864
bool
1865
_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1866
        asection *(*fn) (const char *, asection *,
1867
             asection *))
1868
0
{
1869
0
  struct mips_elf_link_hash_table *htab;
1870
1871
0
  htab = mips_elf_hash_table (info);
1872
0
  if (htab == NULL)
1873
0
    return false;
1874
1875
0
  htab->add_stub_section = fn;
1876
0
  htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1877
0
              mips_elf_la25_stub_eq, NULL);
1878
0
  if (htab->la25_stubs == NULL)
1879
0
    return false;
1880
1881
0
  return true;
1882
0
}
1883
1884
/* Return true if H is a locally-defined PIC function, in the sense
1885
   that it or its fn_stub might need $25 to be valid on entry.
1886
   Note that MIPS16 functions set up $gp using PC-relative instructions,
1887
   so they themselves never need $25 to be valid.  Only non-MIPS16
1888
   entry points are of interest here.  */
1889
1890
static bool
1891
mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1892
0
{
1893
0
  return ((h->root.root.type == bfd_link_hash_defined
1894
0
     || h->root.root.type == bfd_link_hash_defweak)
1895
0
    && h->root.def_regular
1896
0
    && !bfd_is_abs_section (h->root.root.u.def.section)
1897
0
    && !bfd_is_und_section (h->root.root.u.def.section)
1898
0
    && (!ELF_ST_IS_MIPS16 (h->root.other)
1899
0
        || (h->fn_stub && h->need_fn_stub))
1900
0
    && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1901
0
        || ELF_ST_IS_MIPS_PIC (h->root.other)));
1902
0
}
1903
1904
/* Set *SEC to the input section that contains the target of STUB.
1905
   Return the offset of the target from the start of that section.  */
1906
1907
static bfd_vma
1908
mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1909
        asection **sec)
1910
0
{
1911
0
  if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1912
0
    {
1913
0
      BFD_ASSERT (stub->h->need_fn_stub);
1914
0
      *sec = stub->h->fn_stub;
1915
0
      return 0;
1916
0
    }
1917
0
  else
1918
0
    {
1919
0
      *sec = stub->h->root.root.u.def.section;
1920
0
      return stub->h->root.root.u.def.value;
1921
0
    }
1922
0
}
1923
1924
/* STUB describes an la25 stub that we have decided to implement
1925
   by inserting an LUI/ADDIU pair before the target function.
1926
   Create the section and redirect the function symbol to it.  */
1927
1928
static bool
1929
mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1930
       struct bfd_link_info *info)
1931
0
{
1932
0
  struct mips_elf_link_hash_table *htab;
1933
0
  char *name;
1934
0
  asection *s, *input_section;
1935
0
  unsigned int align;
1936
1937
0
  htab = mips_elf_hash_table (info);
1938
0
  if (htab == NULL)
1939
0
    return false;
1940
1941
  /* Create a unique name for the new section.  */
1942
0
  name = bfd_malloc (11 + sizeof (".text.stub."));
1943
0
  if (name == NULL)
1944
0
    return false;
1945
0
  sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1946
1947
  /* Create the section.  */
1948
0
  mips_elf_get_la25_target (stub, &input_section);
1949
0
  s = htab->add_stub_section (name, input_section,
1950
0
            input_section->output_section);
1951
0
  if (s == NULL)
1952
0
    return false;
1953
1954
  /* Make sure that any padding goes before the stub.  */
1955
0
  align = input_section->alignment_power;
1956
0
  if (!bfd_set_section_alignment (s, align))
1957
0
    return false;
1958
0
  if (align > 3)
1959
0
    s->size = (1 << align) - 8;
1960
1961
  /* Create a symbol for the stub.  */
1962
0
  mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1963
0
  stub->stub_section = s;
1964
0
  stub->offset = s->size;
1965
1966
  /* Allocate room for it.  */
1967
0
  s->size += 8;
1968
0
  return true;
1969
0
}
1970
1971
/* STUB describes an la25 stub that we have decided to implement
1972
   with a separate trampoline.  Allocate room for it and redirect
1973
   the function symbol to it.  */
1974
1975
static bool
1976
mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1977
            struct bfd_link_info *info)
1978
0
{
1979
0
  struct mips_elf_link_hash_table *htab;
1980
0
  asection *s;
1981
1982
0
  htab = mips_elf_hash_table (info);
1983
0
  if (htab == NULL)
1984
0
    return false;
1985
1986
  /* Create a trampoline section, if we haven't already.  */
1987
0
  s = htab->strampoline;
1988
0
  if (s == NULL)
1989
0
    {
1990
0
      asection *input_section = stub->h->root.root.u.def.section;
1991
0
      s = htab->add_stub_section (".text", NULL,
1992
0
          input_section->output_section);
1993
0
      if (s == NULL || !bfd_set_section_alignment (s, 4))
1994
0
  return false;
1995
0
      htab->strampoline = s;
1996
0
    }
1997
1998
  /* Create a symbol for the stub.  */
1999
0
  mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
2000
0
  stub->stub_section = s;
2001
0
  stub->offset = s->size;
2002
2003
  /* Allocate room for it.  */
2004
0
  s->size += 16;
2005
0
  return true;
2006
0
}
2007
2008
/* H describes a symbol that needs an la25 stub.  Make sure that an
2009
   appropriate stub exists and point H at it.  */
2010
2011
static bool
2012
mips_elf_add_la25_stub (struct bfd_link_info *info,
2013
      struct mips_elf_link_hash_entry *h)
2014
0
{
2015
0
  struct mips_elf_link_hash_table *htab;
2016
0
  struct mips_elf_la25_stub search, *stub;
2017
0
  bool use_trampoline_p;
2018
0
  asection *s;
2019
0
  bfd_vma value;
2020
0
  void **slot;
2021
2022
  /* Describe the stub we want.  */
2023
0
  search.stub_section = NULL;
2024
0
  search.offset = 0;
2025
0
  search.h = h;
2026
2027
  /* See if we've already created an equivalent stub.  */
2028
0
  htab = mips_elf_hash_table (info);
2029
0
  if (htab == NULL)
2030
0
    return false;
2031
2032
0
  slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
2033
0
  if (slot == NULL)
2034
0
    return false;
2035
2036
0
  stub = (struct mips_elf_la25_stub *) *slot;
2037
0
  if (stub != NULL)
2038
0
    {
2039
      /* We can reuse the existing stub.  */
2040
0
      h->la25_stub = stub;
2041
0
      return true;
2042
0
    }
2043
2044
  /* Create a permanent copy of ENTRY and add it to the hash table.  */
2045
0
  stub = bfd_malloc (sizeof (search));
2046
0
  if (stub == NULL)
2047
0
    return false;
2048
0
  *stub = search;
2049
0
  *slot = stub;
2050
2051
  /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
2052
     of the section and if we would need no more than 2 nops.  */
2053
0
  value = mips_elf_get_la25_target (stub, &s);
2054
0
  if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
2055
0
    value &= ~1;
2056
0
  use_trampoline_p = (value != 0 || s->alignment_power > 4);
2057
2058
0
  h->la25_stub = stub;
2059
0
  return (use_trampoline_p
2060
0
    ? mips_elf_add_la25_trampoline (stub, info)
2061
0
    : mips_elf_add_la25_intro (stub, info));
2062
0
}
2063
2064
/* A mips_elf_link_hash_traverse callback that is called before sizing
2065
   sections.  DATA points to a mips_htab_traverse_info structure.  */
2066
2067
static bool
2068
mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
2069
0
{
2070
0
  struct mips_htab_traverse_info *hti;
2071
2072
0
  hti = (struct mips_htab_traverse_info *) data;
2073
0
  if (!bfd_link_relocatable (hti->info))
2074
0
    mips_elf_check_mips16_stubs (hti->info, h);
2075
2076
0
  if (mips_elf_local_pic_function_p (h))
2077
0
    {
2078
      /* PR 12845: If H is in a section that has been garbage
2079
   collected it will have its output section set to *ABS*.  */
2080
0
      if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
2081
0
  return true;
2082
2083
      /* H is a function that might need $25 to be valid on entry.
2084
   If we're creating a non-PIC relocatable object, mark H as
2085
   being PIC.  If we're creating a non-relocatable object with
2086
   non-PIC branches and jumps to H, make sure that H has an la25
2087
   stub.  */
2088
0
      if (bfd_link_relocatable (hti->info))
2089
0
  {
2090
0
    if (!PIC_OBJECT_P (hti->output_bfd))
2091
0
      h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
2092
0
  }
2093
0
      else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
2094
0
  {
2095
0
    hti->error = true;
2096
0
    return false;
2097
0
  }
2098
0
    }
2099
0
  return true;
2100
0
}
2101

2102
/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2103
   Most mips16 instructions are 16 bits, but these instructions
2104
   are 32 bits.
2105
2106
   The format of these instructions is:
2107
2108
   +--------------+--------------------------------+
2109
   |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
2110
   +--------------+--------------------------------+
2111
   |        Immediate  15:0      |
2112
   +-----------------------------------------------+
2113
2114
   JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
2115
   Note that the immediate value in the first word is swapped.
2116
2117
   When producing a relocatable object file, R_MIPS16_26 is
2118
   handled mostly like R_MIPS_26.  In particular, the addend is
2119
   stored as a straight 26-bit value in a 32-bit instruction.
2120
   (gas makes life simpler for itself by never adjusting a
2121
   R_MIPS16_26 reloc to be against a section, so the addend is
2122
   always zero).  However, the 32 bit instruction is stored as 2
2123
   16-bit values, rather than a single 32-bit value.  In a
2124
   big-endian file, the result is the same; in a little-endian
2125
   file, the two 16-bit halves of the 32 bit value are swapped.
2126
   This is so that a disassembler can recognize the jal
2127
   instruction.
2128
2129
   When doing a final link, R_MIPS16_26 is treated as a 32 bit
2130
   instruction stored as two 16-bit values.  The addend A is the
2131
   contents of the targ26 field.  The calculation is the same as
2132
   R_MIPS_26.  When storing the calculated value, reorder the
2133
   immediate value as shown above, and don't forget to store the
2134
   value as two 16-bit values.
2135
2136
   To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2137
   defined as
2138
2139
   big-endian:
2140
   +--------+----------------------+
2141
   |      |        |
2142
   |      |  targ26-16     |
2143
   |31    26|25       0|
2144
   +--------+----------------------+
2145
2146
   little-endian:
2147
   +----------+------+-------------+
2148
   |        |      |       |
2149
   |  sub1    |      |     sub2    |
2150
   |0      9|10  15|16   31|
2151
   +----------+--------------------+
2152
   where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2153
   ((sub1 << 16) | sub2)).
2154
2155
   When producing a relocatable object file, the calculation is
2156
   (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2157
   When producing a fully linked file, the calculation is
2158
   let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2159
   ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2160
2161
   The table below lists the other MIPS16 instruction relocations.
2162
   Each one is calculated in the same way as the non-MIPS16 relocation
2163
   given on the right, but using the extended MIPS16 layout of 16-bit
2164
   immediate fields:
2165
2166
  R_MIPS16_GPREL    R_MIPS_GPREL16
2167
  R_MIPS16_GOT16    R_MIPS_GOT16
2168
  R_MIPS16_CALL16   R_MIPS_CALL16
2169
  R_MIPS16_HI16   R_MIPS_HI16
2170
  R_MIPS16_LO16   R_MIPS_LO16
2171
2172
   A typical instruction will have a format like this:
2173
2174
   +--------------+--------------------------------+
2175
   |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
2176
   +--------------+--------------------------------+
2177
   |    Major     |   rx   |   ry   |   Imm  4:0   |
2178
   +--------------+--------------------------------+
2179
2180
   EXTEND is the five bit value 11110.  Major is the instruction
2181
   opcode.
2182
2183
   All we need to do here is shuffle the bits appropriately.
2184
   As above, the two 16-bit halves must be swapped on a
2185
   little-endian system.
2186
2187
   Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
2188
   relocatable field is shifted by 1 rather than 2 and the same bit
2189
   shuffling is done as with the relocations above.  */
2190
2191
static inline bool
2192
mips16_reloc_p (int r_type)
2193
6.93k
{
2194
6.93k
  switch (r_type)
2195
6.93k
    {
2196
8
    case R_MIPS16_26:
2197
26
    case R_MIPS16_GPREL:
2198
26
    case R_MIPS16_GOT16:
2199
32
    case R_MIPS16_CALL16:
2200
56
    case R_MIPS16_HI16:
2201
136
    case R_MIPS16_LO16:
2202
138
    case R_MIPS16_TLS_GD:
2203
144
    case R_MIPS16_TLS_LDM:
2204
144
    case R_MIPS16_TLS_DTPREL_HI16:
2205
146
    case R_MIPS16_TLS_DTPREL_LO16:
2206
180
    case R_MIPS16_TLS_GOTTPREL:
2207
210
    case R_MIPS16_TLS_TPREL_HI16:
2208
214
    case R_MIPS16_TLS_TPREL_LO16:
2209
214
    case R_MIPS16_PC16_S1:
2210
214
      return true;
2211
2212
6.72k
    default:
2213
6.72k
      return false;
2214
6.93k
    }
2215
6.93k
}
2216
2217
/* Check if a microMIPS reloc.  */
2218
2219
static inline bool
2220
micromips_reloc_p (unsigned int r_type)
2221
7.15k
{
2222
7.15k
  return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2223
7.15k
}
2224
2225
/* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2226
   on a little-endian system.  This does not apply to R_MICROMIPS_PC7_S1
2227
   and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.  */
2228
2229
static inline bool
2230
micromips_reloc_shuffle_p (unsigned int r_type)
2231
6.72k
{
2232
6.72k
  return (micromips_reloc_p (r_type)
2233
6.72k
    && r_type != R_MICROMIPS_PC7_S1
2234
6.72k
    && r_type != R_MICROMIPS_PC10_S1);
2235
6.72k
}
2236
2237
static inline bool
2238
got16_reloc_p (int r_type)
2239
0
{
2240
0
  return (r_type == R_MIPS_GOT16
2241
0
    || r_type == R_MIPS16_GOT16
2242
0
    || r_type == R_MICROMIPS_GOT16);
2243
0
}
2244
2245
static inline bool
2246
call16_reloc_p (int r_type)
2247
0
{
2248
0
  return (r_type == R_MIPS_CALL16
2249
0
    || r_type == R_MIPS16_CALL16
2250
0
    || r_type == R_MICROMIPS_CALL16);
2251
0
}
2252
2253
static inline bool
2254
got_disp_reloc_p (unsigned int r_type)
2255
0
{
2256
0
  return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2257
0
}
2258
2259
static inline bool
2260
got_page_reloc_p (unsigned int r_type)
2261
0
{
2262
0
  return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2263
0
}
2264
2265
static inline bool
2266
got_lo16_reloc_p (unsigned int r_type)
2267
0
{
2268
0
  return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2269
0
}
2270
2271
static inline bool
2272
call_hi16_reloc_p (unsigned int r_type)
2273
0
{
2274
0
  return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2275
0
}
2276
2277
static inline bool
2278
call_lo16_reloc_p (unsigned int r_type)
2279
0
{
2280
0
  return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2281
0
}
2282
2283
static inline bool
2284
hi16_reloc_p (int r_type)
2285
0
{
2286
0
  return (r_type == R_MIPS_HI16
2287
0
    || r_type == R_MIPS16_HI16
2288
0
    || r_type == R_MICROMIPS_HI16
2289
0
    || r_type == R_MIPS_PCHI16);
2290
0
}
2291
2292
static inline bool
2293
lo16_reloc_p (int r_type)
2294
0
{
2295
0
  return (r_type == R_MIPS_LO16
2296
0
    || r_type == R_MIPS16_LO16
2297
0
    || r_type == R_MICROMIPS_LO16
2298
0
    || r_type == R_MIPS_PCLO16);
2299
0
}
2300
2301
static inline bool
2302
mips16_call_reloc_p (int r_type)
2303
0
{
2304
0
  return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2305
0
}
2306
2307
static inline bool
2308
jal_reloc_p (int r_type)
2309
0
{
2310
0
  return (r_type == R_MIPS_26
2311
0
    || r_type == R_MIPS16_26
2312
0
    || r_type == R_MICROMIPS_26_S1);
2313
0
}
2314
2315
static inline bool
2316
b_reloc_p (int r_type)
2317
0
{
2318
0
  return (r_type == R_MIPS_PC26_S2
2319
0
    || r_type == R_MIPS_PC21_S2
2320
0
    || r_type == R_MIPS_PC16
2321
0
    || r_type == R_MIPS_GNU_REL16_S2
2322
0
    || r_type == R_MIPS16_PC16_S1
2323
0
    || r_type == R_MICROMIPS_PC16_S1
2324
0
    || r_type == R_MICROMIPS_PC10_S1
2325
0
    || r_type == R_MICROMIPS_PC7_S1);
2326
0
}
2327
2328
static inline bool
2329
aligned_pcrel_reloc_p (int r_type)
2330
0
{
2331
0
  return (r_type == R_MIPS_PC18_S3
2332
0
    || r_type == R_MIPS_PC19_S2);
2333
0
}
2334
2335
static inline bool
2336
branch_reloc_p (int r_type)
2337
0
{
2338
0
  return (r_type == R_MIPS_26
2339
0
    || r_type == R_MIPS_PC26_S2
2340
0
    || r_type == R_MIPS_PC21_S2
2341
0
    || r_type == R_MIPS_PC16
2342
0
    || r_type == R_MIPS_GNU_REL16_S2);
2343
0
}
2344
2345
static inline bool
2346
mips16_branch_reloc_p (int r_type)
2347
0
{
2348
0
  return (r_type == R_MIPS16_26
2349
0
    || r_type == R_MIPS16_PC16_S1);
2350
0
}
2351
2352
static inline bool
2353
micromips_branch_reloc_p (int r_type)
2354
0
{
2355
0
  return (r_type == R_MICROMIPS_26_S1
2356
0
    || r_type == R_MICROMIPS_PC16_S1
2357
0
    || r_type == R_MICROMIPS_PC10_S1
2358
0
    || r_type == R_MICROMIPS_PC7_S1);
2359
0
}
2360
2361
static inline bool
2362
tls_gd_reloc_p (unsigned int r_type)
2363
0
{
2364
0
  return (r_type == R_MIPS_TLS_GD
2365
0
    || r_type == R_MIPS16_TLS_GD
2366
0
    || r_type == R_MICROMIPS_TLS_GD);
2367
0
}
2368
2369
static inline bool
2370
tls_ldm_reloc_p (unsigned int r_type)
2371
0
{
2372
0
  return (r_type == R_MIPS_TLS_LDM
2373
0
    || r_type == R_MIPS16_TLS_LDM
2374
0
    || r_type == R_MICROMIPS_TLS_LDM);
2375
0
}
2376
2377
static inline bool
2378
tls_gottprel_reloc_p (unsigned int r_type)
2379
0
{
2380
0
  return (r_type == R_MIPS_TLS_GOTTPREL
2381
0
    || r_type == R_MIPS16_TLS_GOTTPREL
2382
0
    || r_type == R_MICROMIPS_TLS_GOTTPREL);
2383
0
}
2384
2385
static inline bool
2386
needs_shuffle (int r_type)
2387
6.93k
{
2388
6.93k
  return mips16_reloc_p (r_type) || micromips_reloc_shuffle_p (r_type);
2389
6.93k
}
2390
2391
void
2392
_bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2393
             bool jal_shuffle, bfd_byte *data)
2394
3.45k
{
2395
3.45k
  bfd_vma first, second, val;
2396
2397
3.45k
  if (!needs_shuffle (r_type))
2398
3.23k
    return;
2399
2400
  /* Pick up the first and second halfwords of the instruction.  */
2401
214
  first = bfd_get_16 (abfd, data);
2402
214
  second = bfd_get_16 (abfd, data + 2);
2403
214
  if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2404
115
    val = first << 16 | second;
2405
99
  else if (r_type != R_MIPS16_26)
2406
99
    val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2407
99
     | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2408
0
  else
2409
0
    val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2410
0
     | ((first & 0x1f) << 21) | second);
2411
214
  bfd_put_32 (abfd, val, data);
2412
214
}
2413
2414
void
2415
_bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2416
           bool jal_shuffle, bfd_byte *data)
2417
3.45k
{
2418
3.45k
  bfd_vma first, second, val;
2419
2420
3.45k
  if (!needs_shuffle (r_type))
2421
3.23k
    return;
2422
2423
214
  val = bfd_get_32 (abfd, data);
2424
214
  if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2425
115
    {
2426
115
      second = val & 0xffff;
2427
115
      first = val >> 16;
2428
115
    }
2429
99
  else if (r_type != R_MIPS16_26)
2430
99
    {
2431
99
      second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2432
99
      first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2433
99
    }
2434
0
  else
2435
0
    {
2436
0
      second = val & 0xffff;
2437
0
      first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2438
0
         | ((val >> 21) & 0x1f);
2439
0
    }
2440
214
  bfd_put_16 (abfd, second, data + 2);
2441
214
  bfd_put_16 (abfd, first, data);
2442
214
}
2443
2444
/* Perform reloc offset checking.
2445
   We can only use bfd_reloc_offset_in_range, which takes into account
2446
   the size of the field being relocated, when section contents will
2447
   be accessed because mips object files may use relocations that seem
2448
   to access beyond section limits.
2449
   gas/testsuite/gas/mips/dla-reloc.s is an example that puts
2450
   R_MIPS_SUB, a 64-bit relocation, on the last instruction in the
2451
   section.  The R_MIPS_SUB applies to the addend for the next reloc
2452
   rather than the section contents.
2453
2454
   CHECK is CHECK_STD for the standard bfd_reloc_offset_in_range check,
2455
   CHECK_INPLACE to only check partial_inplace relocs, and
2456
   CHECK_SHUFFLE to only check relocs that shuffle/unshuffle.  */
2457
2458
bool
2459
_bfd_mips_reloc_offset_in_range (bfd *abfd, asection *input_section,
2460
         arelent *reloc_entry, enum reloc_check check)
2461
3.52k
{
2462
3.52k
  if (check == check_inplace && !reloc_entry->howto->partial_inplace)
2463
22
    return true;
2464
3.50k
  if (check == check_shuffle && !needs_shuffle (reloc_entry->howto->type))
2465
0
    return true;
2466
3.50k
  return bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
2467
3.50k
            input_section, reloc_entry->address);
2468
3.50k
}
2469
2470
bfd_reloc_status_type
2471
_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2472
             arelent *reloc_entry, asection *input_section,
2473
             bool relocatable, void *data, bfd_vma gp)
2474
93
{
2475
93
  bfd_vma relocation;
2476
93
  bfd_signed_vma val;
2477
93
  bfd_reloc_status_type status;
2478
2479
93
  if (bfd_is_com_section (symbol->section))
2480
0
    relocation = 0;
2481
93
  else
2482
93
    relocation = symbol->value;
2483
2484
93
  if (symbol->section->output_section != NULL)
2485
93
    {
2486
93
      relocation += symbol->section->output_section->vma;
2487
93
      relocation += symbol->section->output_offset;
2488
93
    }
2489
2490
  /* Set val to the offset into the section or symbol.  */
2491
93
  val = reloc_entry->addend;
2492
2493
93
  _bfd_mips_elf_sign_extend (val, 16);
2494
2495
  /* Adjust val for the final section location and GP value.  If we
2496
     are producing relocatable output, we don't want to do this for
2497
     an external symbol.  */
2498
93
  if (! relocatable
2499
93
      || (symbol->flags & BSF_SECTION_SYM) != 0)
2500
93
    val += relocation - gp;
2501
2502
93
  if (reloc_entry->howto->partial_inplace)
2503
0
    {
2504
0
      if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2505
0
              reloc_entry->address))
2506
0
  return bfd_reloc_outofrange;
2507
2508
0
      status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2509
0
               (bfd_byte *) data
2510
0
               + reloc_entry->address);
2511
0
      if (status != bfd_reloc_ok)
2512
0
  return status;
2513
0
    }
2514
93
  else
2515
93
    reloc_entry->addend = val;
2516
2517
93
  if (relocatable)
2518
0
    reloc_entry->address += input_section->output_offset;
2519
2520
93
  return bfd_reloc_ok;
2521
93
}
2522
2523
/* A howto special_function for REL *HI16 relocations.  We can only
2524
   calculate the correct value once we've seen the partnering
2525
   *LO16 relocation, so just save the information for later.
2526
2527
   The ABI requires that the *LO16 immediately follow the *HI16.
2528
   However, as a GNU extension, we permit an arbitrary number of
2529
   *HI16s to be associated with a single *LO16.  This significantly
2530
   simplies the relocation handling in gcc.  */
2531
2532
bfd_reloc_status_type
2533
_bfd_mips_elf_hi16_reloc (bfd *abfd, arelent *reloc_entry,
2534
        asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2535
        asection *input_section, bfd *output_bfd,
2536
        char **error_message ATTRIBUTE_UNUSED)
2537
82
{
2538
82
  struct mips_hi16 *n;
2539
82
  struct mips_elf_obj_tdata *tdata;
2540
2541
82
  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2542
6
    return bfd_reloc_outofrange;
2543
2544
76
  n = bfd_malloc (sizeof *n);
2545
76
  if (n == NULL)
2546
0
    return bfd_reloc_outofrange;
2547
2548
76
  tdata = mips_elf_tdata (abfd);
2549
76
  n->next = tdata->mips_hi16_list;
2550
76
  n->data = data;
2551
76
  n->input_section = input_section;
2552
76
  n->rel = *reloc_entry;
2553
76
  tdata->mips_hi16_list = n;
2554
2555
76
  if (output_bfd != NULL)
2556
0
    reloc_entry->address += input_section->output_offset;
2557
2558
76
  return bfd_reloc_ok;
2559
76
}
2560
2561
/* A howto special_function for REL R_MIPS*_GOT16 relocations.  This is just
2562
   like any other 16-bit relocation when applied to global symbols, but is
2563
   treated in the same as R_MIPS_HI16 when applied to local symbols.  */
2564
2565
bfd_reloc_status_type
2566
_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2567
         void *data, asection *input_section,
2568
         bfd *output_bfd, char **error_message)
2569
60
{
2570
60
  if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2571
60
      || bfd_is_und_section (bfd_asymbol_section (symbol))
2572
60
      || bfd_is_com_section (bfd_asymbol_section (symbol)))
2573
    /* The relocation is against a global symbol.  */
2574
4
    return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2575
4
          input_section, output_bfd,
2576
4
          error_message);
2577
2578
56
  return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2579
56
           input_section, output_bfd, error_message);
2580
60
}
2581
2582
/* A howto special_function for REL *LO16 relocations.  The *LO16 itself
2583
   is a straightforward 16 bit inplace relocation, but we must deal with
2584
   any partnering high-part relocations as well.  */
2585
2586
bfd_reloc_status_type
2587
_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2588
        void *data, asection *input_section,
2589
        bfd *output_bfd, char **error_message)
2590
60
{
2591
60
  bfd_vma vallo;
2592
60
  bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2593
60
  struct mips_elf_obj_tdata *tdata;
2594
2595
60
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2596
60
          reloc_entry->address))
2597
2
    return bfd_reloc_outofrange;
2598
2599
58
  _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2600
58
         location);
2601
  /* The high 16 bits of the addend are stored in the high insn, the
2602
     low 16 bits in the low insn, but there is a catch:  You can't
2603
     just concatenate the high and low parts.  The high part of the
2604
     addend is adjusted for the fact that the low part is sign
2605
     extended.  For example, an addend of 0x38000 would have 0x0004 in
2606
     the high part and 0x8000 (=0xff..f8000) in the low part.
2607
     To extract the actual addend, calculate (a)
2608
     ((hi & 0xffff) << 16) + ((lo & 0xffff) ^ 0x8000) - 0x8000.
2609
     We will be applying (symbol + addend) & 0xffff to the low insn,
2610
     and we want to apply (b) (symbol + addend + 0x8000) >> 16 to the
2611
     high insn (the +0x8000 adjusting for when the applied low part is
2612
     negative).  Substituting (a) into (b) and recognising that
2613
     (hi & 0xffff) is already in the high insn gives a high part
2614
     addend adjustment of (lo & 0xffff) ^ 0x8000.  */
2615
58
  vallo = (bfd_get_32 (abfd, location) & 0xffff) ^ 0x8000;
2616
58
  _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2617
58
             location);
2618
2619
58
  tdata = mips_elf_tdata (abfd);
2620
76
  while (tdata->mips_hi16_list != NULL)
2621
18
    {
2622
18
      bfd_reloc_status_type ret;
2623
18
      struct mips_hi16 *hi;
2624
2625
18
      hi = tdata->mips_hi16_list;
2626
2627
      /* R_MIPS*_GOT16 relocations are something of a special case.  We
2628
   want to install the addend in the same way as for a R_MIPS*_HI16
2629
   relocation (with a rightshift of 16).  However, since GOT16
2630
   relocations can also be used with global symbols, their howto
2631
   has a rightshift of 0.  */
2632
18
      if (hi->rel.howto->type == R_MIPS_GOT16)
2633
0
  hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, false);
2634
18
      else if (hi->rel.howto->type == R_MIPS16_GOT16)
2635
3
  hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, false);
2636
15
      else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2637
6
  hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, false);
2638
2639
18
      hi->rel.addend += vallo;
2640
2641
18
      ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2642
18
           hi->input_section, output_bfd,
2643
18
           error_message);
2644
18
      if (ret != bfd_reloc_ok)
2645
0
  return ret;
2646
2647
18
      tdata->mips_hi16_list = hi->next;
2648
18
      free (hi);
2649
18
    }
2650
2651
58
  return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2652
58
              input_section, output_bfd,
2653
58
              error_message);
2654
58
}
2655
2656
/* A generic howto special_function.  This calculates and installs the
2657
   relocation itself, thus avoiding the oft-discussed problems in
2658
   bfd_perform_relocation and bfd_install_relocation.  */
2659
2660
bfd_reloc_status_type
2661
_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2662
           asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2663
           asection *input_section, bfd *output_bfd,
2664
           char **error_message ATTRIBUTE_UNUSED)
2665
3.47k
{
2666
3.47k
  bfd_signed_vma val;
2667
3.47k
  bfd_reloc_status_type status;
2668
3.47k
  bool relocatable;
2669
2670
3.47k
  relocatable = (output_bfd != NULL);
2671
2672
3.47k
  if (!_bfd_mips_reloc_offset_in_range (abfd, input_section, reloc_entry,
2673
3.47k
          (relocatable
2674
3.47k
           ? check_inplace : check_std)))
2675
99
    return bfd_reloc_outofrange;
2676
2677
  /* Build up the field adjustment in VAL.  */
2678
3.37k
  val = 0;
2679
3.37k
  if ((!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2680
3.37k
      && symbol->section->output_section != NULL)
2681
3.37k
    {
2682
      /* Either we're calculating the final field value or we have a
2683
   relocation against a section symbol.  Add in the section's
2684
   offset or address.  */
2685
3.37k
      val += symbol->section->output_section->vma;
2686
3.37k
      val += symbol->section->output_offset;
2687
3.37k
    }
2688
2689
3.37k
  if (!relocatable)
2690
3.37k
    {
2691
      /* We're calculating the final field value.  Add in the symbol's value
2692
   and, if pc-relative, subtract the address of the field itself.  */
2693
3.37k
      val += symbol->value;
2694
3.37k
      if (reloc_entry->howto->pc_relative)
2695
55
  {
2696
55
    val -= input_section->output_section->vma;
2697
55
    val -= input_section->output_offset;
2698
55
    val -= reloc_entry->address;
2699
55
  }
2700
3.37k
    }
2701
2702
  /* VAL is now the final adjustment.  If we're keeping this relocation
2703
     in the output file, and if the relocation uses a separate addend,
2704
     we just need to add VAL to that addend.  Otherwise we need to add
2705
     VAL to the relocation field itself.  */
2706
3.37k
  if (relocatable && !reloc_entry->howto->partial_inplace)
2707
0
    reloc_entry->addend += val;
2708
3.37k
  else
2709
3.37k
    {
2710
3.37k
      bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2711
2712
      /* Add in the separate addend, if any.  */
2713
3.37k
      val += reloc_entry->addend;
2714
2715
      /* Add VAL to the relocation field.  */
2716
3.37k
      _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2717
3.37k
             location);
2718
3.37k
      status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2719
3.37k
               location);
2720
3.37k
      _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2721
3.37k
           location);
2722
2723
3.37k
      if (status != bfd_reloc_ok)
2724
29
  return status;
2725
3.37k
    }
2726
2727
3.34k
  if (relocatable)
2728
0
    reloc_entry->address += input_section->output_offset;
2729
2730
3.34k
  return bfd_reloc_ok;
2731
3.37k
}
2732

2733
/* Swap an entry in a .gptab section.  Note that these routines rely
2734
   on the equivalence of the two elements of the union.  */
2735
2736
static void
2737
bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2738
            Elf32_gptab *in)
2739
0
{
2740
0
  in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2741
0
  in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2742
0
}
2743
2744
static void
2745
bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2746
             Elf32_External_gptab *ex)
2747
0
{
2748
0
  H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2749
0
  H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2750
0
}
2751
2752
static void
2753
bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2754
        Elf32_External_compact_rel *ex)
2755
0
{
2756
0
  H_PUT_32 (abfd, in->id1, ex->id1);
2757
0
  H_PUT_32 (abfd, in->num, ex->num);
2758
0
  H_PUT_32 (abfd, in->id2, ex->id2);
2759
0
  H_PUT_32 (abfd, in->offset, ex->offset);
2760
0
  H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2761
0
  H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2762
0
}
2763
2764
static void
2765
bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2766
         Elf32_External_crinfo *ex)
2767
0
{
2768
0
  unsigned long l;
2769
2770
0
  l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2771
0
       | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2772
0
       | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2773
0
       | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2774
0
  H_PUT_32 (abfd, l, ex->info);
2775
0
  H_PUT_32 (abfd, in->konst, ex->konst);
2776
0
  H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2777
0
}
2778

2779
/* A .reginfo section holds a single Elf32_RegInfo structure.  These
2780
   routines swap this structure in and out.  They are used outside of
2781
   BFD, so they are globally visible.  */
2782
2783
void
2784
bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2785
        Elf32_RegInfo *in)
2786
42
{
2787
42
  in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2788
42
  in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2789
42
  in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2790
42
  in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2791
42
  in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2792
42
  in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2793
42
}
2794
2795
void
2796
bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2797
         Elf32_External_RegInfo *ex)
2798
0
{
2799
0
  H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2800
0
  H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2801
0
  H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2802
0
  H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2803
0
  H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2804
0
  H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2805
0
}
2806
2807
/* In the 64 bit ABI, the .MIPS.options section holds register
2808
   information in an Elf64_Reginfo structure.  These routines swap
2809
   them in and out.  They are globally visible because they are used
2810
   outside of BFD.  These routines are here so that gas can call them
2811
   without worrying about whether the 64 bit ABI has been included.  */
2812
2813
void
2814
bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2815
        Elf64_Internal_RegInfo *in)
2816
689
{
2817
689
  in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2818
689
  in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2819
689
  in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2820
689
  in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2821
689
  in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2822
689
  in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2823
689
  in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2824
689
}
2825
2826
void
2827
bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2828
         Elf64_External_RegInfo *ex)
2829
0
{
2830
0
  H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2831
0
  H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2832
0
  H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2833
0
  H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2834
0
  H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2835
0
  H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2836
0
  H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2837
0
}
2838
2839
/* Swap in an options header.  */
2840
2841
void
2842
bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2843
            Elf_Internal_Options *in)
2844
3.47k
{
2845
3.47k
  in->kind = H_GET_8 (abfd, ex->kind);
2846
3.47k
  in->size = H_GET_8 (abfd, ex->size);
2847
3.47k
  in->section = H_GET_16 (abfd, ex->section);
2848
3.47k
  in->info = H_GET_32 (abfd, ex->info);
2849
3.47k
}
2850
2851
/* Swap out an options header.  */
2852
2853
void
2854
bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2855
             Elf_External_Options *ex)
2856
0
{
2857
0
  H_PUT_8 (abfd, in->kind, ex->kind);
2858
0
  H_PUT_8 (abfd, in->size, ex->size);
2859
0
  H_PUT_16 (abfd, in->section, ex->section);
2860
0
  H_PUT_32 (abfd, in->info, ex->info);
2861
0
}
2862
2863
/* Swap in an abiflags structure.  */
2864
2865
void
2866
bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2867
          const Elf_External_ABIFlags_v0 *ex,
2868
          Elf_Internal_ABIFlags_v0 *in)
2869
0
{
2870
0
  in->version = H_GET_16 (abfd, ex->version);
2871
0
  in->isa_level = H_GET_8 (abfd, ex->isa_level);
2872
0
  in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2873
0
  in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2874
0
  in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2875
0
  in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2876
0
  in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2877
0
  in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2878
0
  in->ases = H_GET_32 (abfd, ex->ases);
2879
0
  in->flags1 = H_GET_32 (abfd, ex->flags1);
2880
0
  in->flags2 = H_GET_32 (abfd, ex->flags2);
2881
0
}
2882
2883
/* Swap out an abiflags structure.  */
2884
2885
void
2886
bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2887
           const Elf_Internal_ABIFlags_v0 *in,
2888
           Elf_External_ABIFlags_v0 *ex)
2889
0
{
2890
0
  H_PUT_16 (abfd, in->version, ex->version);
2891
0
  H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2892
0
  H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2893
0
  H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2894
0
  H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2895
0
  H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2896
0
  H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2897
0
  H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2898
0
  H_PUT_32 (abfd, in->ases, ex->ases);
2899
0
  H_PUT_32 (abfd, in->flags1, ex->flags1);
2900
0
  H_PUT_32 (abfd, in->flags2, ex->flags2);
2901
0
}
2902

2903
/* This function is called via qsort() to sort the dynamic relocation
2904
   entries by increasing r_symndx value.  */
2905
2906
static int
2907
sort_dynamic_relocs (const void *arg1, const void *arg2)
2908
0
{
2909
0
  Elf_Internal_Rela int_reloc1;
2910
0
  Elf_Internal_Rela int_reloc2;
2911
0
  int diff;
2912
2913
0
  bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2914
0
  bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2915
2916
0
  diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2917
0
  if (diff != 0)
2918
0
    return diff;
2919
2920
0
  if (int_reloc1.r_offset < int_reloc2.r_offset)
2921
0
    return -1;
2922
0
  if (int_reloc1.r_offset > int_reloc2.r_offset)
2923
0
    return 1;
2924
0
  return 0;
2925
0
}
2926
2927
/* Like sort_dynamic_relocs, but used for elf64 relocations.  */
2928
2929
static int
2930
sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2931
      const void *arg2 ATTRIBUTE_UNUSED)
2932
0
{
2933
0
#ifdef BFD64
2934
0
  Elf_Internal_Rela int_reloc1[3];
2935
0
  Elf_Internal_Rela int_reloc2[3];
2936
2937
0
  (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2938
0
    (reldyn_sorting_bfd, arg1, int_reloc1);
2939
0
  (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2940
0
    (reldyn_sorting_bfd, arg2, int_reloc2);
2941
2942
0
  if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2943
0
    return -1;
2944
0
  if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2945
0
    return 1;
2946
2947
0
  if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2948
0
    return -1;
2949
0
  if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2950
0
    return 1;
2951
0
  return 0;
2952
#else
2953
  abort ();
2954
#endif
2955
0
}
2956
2957
2958
/* This routine is used to write out ECOFF debugging external symbol
2959
   information.  It is called via mips_elf_link_hash_traverse.  The
2960
   ECOFF external symbol information must match the ELF external
2961
   symbol information.  Unfortunately, at this point we don't know
2962
   whether a symbol is required by reloc information, so the two
2963
   tables may wind up being different.  We must sort out the external
2964
   symbol information before we can set the final size of the .mdebug
2965
   section, and we must set the size of the .mdebug section before we
2966
   can relocate any sections, and we can't know which symbols are
2967
   required by relocation until we relocate the sections.
2968
   Fortunately, it is relatively unlikely that any symbol will be
2969
   stripped but required by a reloc.  In particular, it can not happen
2970
   when generating a final executable.  */
2971
2972
static bool
2973
mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2974
0
{
2975
0
  struct extsym_info *einfo = data;
2976
0
  bool strip;
2977
0
  asection *sec, *output_section;
2978
2979
0
  if (h->root.indx == -2)
2980
0
    strip = false;
2981
0
  else if ((h->root.def_dynamic
2982
0
      || h->root.ref_dynamic
2983
0
      || h->root.type == bfd_link_hash_new)
2984
0
     && !h->root.def_regular
2985
0
     && !h->root.ref_regular)
2986
0
    strip = true;
2987
0
  else if (einfo->info->strip == strip_all
2988
0
     || (einfo->info->strip == strip_some
2989
0
         && bfd_hash_lookup (einfo->info->keep_hash,
2990
0
           h->root.root.root.string,
2991
0
           false, false) == NULL))
2992
0
    strip = true;
2993
0
  else
2994
0
    strip = false;
2995
2996
0
  if (strip)
2997
0
    return true;
2998
2999
0
  if (h->esym.ifd == -2)
3000
0
    {
3001
0
      h->esym.jmptbl = 0;
3002
0
      h->esym.cobol_main = 0;
3003
0
      h->esym.weakext = 0;
3004
0
      h->esym.reserved = 0;
3005
0
      h->esym.ifd = ifdNil;
3006
0
      h->esym.asym.value = 0;
3007
0
      h->esym.asym.st = stGlobal;
3008
3009
0
      if (h->root.root.type == bfd_link_hash_undefined
3010
0
    || h->root.root.type == bfd_link_hash_undefweak)
3011
0
  {
3012
0
    const char *name;
3013
3014
    /* Use undefined class.  Also, set class and type for some
3015
       special symbols.  */
3016
0
    name = h->root.root.root.string;
3017
0
    if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3018
0
        || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3019
0
      {
3020
0
        h->esym.asym.sc = scData;
3021
0
        h->esym.asym.st = stLabel;
3022
0
        h->esym.asym.value = 0;
3023
0
      }
3024
0
    else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3025
0
      {
3026
0
        h->esym.asym.sc = scAbs;
3027
0
        h->esym.asym.st = stLabel;
3028
0
        h->esym.asym.value =
3029
0
    mips_elf_hash_table (einfo->info)->procedure_count;
3030
0
      }
3031
0
    else
3032
0
      h->esym.asym.sc = scUndefined;
3033
0
  }
3034
0
      else if (h->root.root.type != bfd_link_hash_defined
3035
0
    && h->root.root.type != bfd_link_hash_defweak)
3036
0
  h->esym.asym.sc = scAbs;
3037
0
      else
3038
0
  {
3039
0
    const char *name;
3040
3041
0
    sec = h->root.root.u.def.section;
3042
0
    output_section = sec->output_section;
3043
3044
    /* When making a shared library and symbol h is the one from
3045
       the another shared library, OUTPUT_SECTION may be null.  */
3046
0
    if (output_section == NULL)
3047
0
      h->esym.asym.sc = scUndefined;
3048
0
    else
3049
0
      {
3050
0
        name = bfd_section_name (output_section);
3051
3052
0
        if (strcmp (name, ".text") == 0)
3053
0
    h->esym.asym.sc = scText;
3054
0
        else if (strcmp (name, ".data") == 0)
3055
0
    h->esym.asym.sc = scData;
3056
0
        else if (strcmp (name, ".sdata") == 0)
3057
0
    h->esym.asym.sc = scSData;
3058
0
        else if (strcmp (name, ".rodata") == 0
3059
0
           || strcmp (name, ".rdata") == 0)
3060
0
    h->esym.asym.sc = scRData;
3061
0
        else if (strcmp (name, ".bss") == 0)
3062
0
    h->esym.asym.sc = scBss;
3063
0
        else if (strcmp (name, ".sbss") == 0)
3064
0
    h->esym.asym.sc = scSBss;
3065
0
        else if (strcmp (name, ".init") == 0)
3066
0
    h->esym.asym.sc = scInit;
3067
0
        else if (strcmp (name, ".fini") == 0)
3068
0
    h->esym.asym.sc = scFini;
3069
0
        else
3070
0
    h->esym.asym.sc = scAbs;
3071
0
      }
3072
0
  }
3073
3074
0
      h->esym.asym.reserved = 0;
3075
0
      h->esym.asym.index = indexNil;
3076
0
    }
3077
3078
0
  if (h->root.root.type == bfd_link_hash_common)
3079
0
    h->esym.asym.value = h->root.root.u.c.size;
3080
0
  else if (h->root.root.type == bfd_link_hash_defined
3081
0
     || h->root.root.type == bfd_link_hash_defweak)
3082
0
    {
3083
0
      if (h->esym.asym.sc == scCommon)
3084
0
  h->esym.asym.sc = scBss;
3085
0
      else if (h->esym.asym.sc == scSCommon)
3086
0
  h->esym.asym.sc = scSBss;
3087
3088
0
      sec = h->root.root.u.def.section;
3089
0
      output_section = sec->output_section;
3090
0
      if (output_section != NULL)
3091
0
  h->esym.asym.value = (h->root.root.u.def.value
3092
0
            + sec->output_offset
3093
0
            + output_section->vma);
3094
0
      else
3095
0
  h->esym.asym.value = 0;
3096
0
    }
3097
0
  else
3098
0
    {
3099
0
      struct mips_elf_link_hash_entry *hd = h;
3100
3101
0
      while (hd->root.root.type == bfd_link_hash_indirect)
3102
0
  hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
3103
3104
0
      if (hd->needs_lazy_stub)
3105
0
  {
3106
0
    BFD_ASSERT (hd->root.plt.plist != NULL);
3107
0
    BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
3108
    /* Set type and value for a symbol with a function stub.  */
3109
0
    h->esym.asym.st = stProc;
3110
0
    sec = hd->root.root.u.def.section;
3111
0
    if (sec == NULL)
3112
0
      h->esym.asym.value = 0;
3113
0
    else
3114
0
      {
3115
0
        output_section = sec->output_section;
3116
0
        if (output_section != NULL)
3117
0
    h->esym.asym.value = (hd->root.plt.plist->stub_offset
3118
0
              + sec->output_offset
3119
0
              + output_section->vma);
3120
0
        else
3121
0
    h->esym.asym.value = 0;
3122
0
      }
3123
0
  }
3124
0
    }
3125
3126
0
  if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3127
0
              h->root.root.root.string,
3128
0
              &h->esym))
3129
0
    {
3130
0
      einfo->failed = true;
3131
0
      return false;
3132
0
    }
3133
3134
0
  return true;
3135
0
}
3136
3137
/* A comparison routine used to sort .gptab entries.  */
3138
3139
static int
3140
gptab_compare (const void *p1, const void *p2)
3141
0
{
3142
0
  const Elf32_gptab *a1 = p1;
3143
0
  const Elf32_gptab *a2 = p2;
3144
3145
0
  return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3146
0
}
3147

3148
/* Functions to manage the got entry hash table.  */
3149
3150
/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
3151
   hash number.  */
3152
3153
static inline hashval_t
3154
mips_elf_hash_bfd_vma (bfd_vma addr)
3155
0
{
3156
0
#ifdef BFD64
3157
0
  return addr + (addr >> 32);
3158
#else
3159
  return addr;
3160
#endif
3161
0
}
3162
3163
static hashval_t
3164
mips_elf_got_entry_hash (const void *entry_)
3165
0
{
3166
0
  const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
3167
3168
0
  return (entry->symndx
3169
0
    + ((entry->tls_type == GOT_TLS_LDM) << 18)
3170
0
    + (entry->tls_type == GOT_TLS_LDM ? 0
3171
0
       : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
3172
0
       : entry->symndx >= 0 ? (entry->abfd->id
3173
0
             + mips_elf_hash_bfd_vma (entry->d.addend))
3174
0
       : entry->d.h->root.root.root.hash));
3175
0
}
3176
3177
static int
3178
mips_elf_got_entry_eq (const void *entry1, const void *entry2)
3179
0
{
3180
0
  const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3181
0
  const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3182
3183
0
  return (e1->symndx == e2->symndx
3184
0
    && e1->tls_type == e2->tls_type
3185
0
    && (e1->tls_type == GOT_TLS_LDM ? true
3186
0
        : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3187
0
        : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3188
0
           && e1->d.addend == e2->d.addend)
3189
0
        : e2->abfd && e1->d.h == e2->d.h));
3190
0
}
3191
3192
static hashval_t
3193
mips_got_page_ref_hash (const void *ref_)
3194
0
{
3195
0
  const struct mips_got_page_ref *ref;
3196
3197
0
  ref = (const struct mips_got_page_ref *) ref_;
3198
0
  return ((ref->symndx >= 0
3199
0
     ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3200
0
     : ref->u.h->root.root.root.hash)
3201
0
    + mips_elf_hash_bfd_vma (ref->addend));
3202
0
}
3203
3204
static int
3205
mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3206
0
{
3207
0
  const struct mips_got_page_ref *ref1, *ref2;
3208
3209
0
  ref1 = (const struct mips_got_page_ref *) ref1_;
3210
0
  ref2 = (const struct mips_got_page_ref *) ref2_;
3211
0
  return (ref1->symndx == ref2->symndx
3212
0
    && (ref1->symndx < 0
3213
0
        ? ref1->u.h == ref2->u.h
3214
0
        : ref1->u.abfd == ref2->u.abfd)
3215
0
    && ref1->addend == ref2->addend);
3216
0
}
3217
3218
static hashval_t
3219
mips_got_page_entry_hash (const void *entry_)
3220
0
{
3221
0
  const struct mips_got_page_entry *entry;
3222
3223
0
  entry = (const struct mips_got_page_entry *) entry_;
3224
0
  return entry->sec->id;
3225
0
}
3226
3227
static int
3228
mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3229
0
{
3230
0
  const struct mips_got_page_entry *entry1, *entry2;
3231
3232
0
  entry1 = (const struct mips_got_page_entry *) entry1_;
3233
0
  entry2 = (const struct mips_got_page_entry *) entry2_;
3234
0
  return entry1->sec == entry2->sec;
3235
0
}
3236

3237
/* Create and return a new mips_got_info structure.  */
3238
3239
static struct mips_got_info *
3240
mips_elf_create_got_info (bfd *abfd)
3241
0
{
3242
0
  struct mips_got_info *g;
3243
3244
0
  g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3245
0
  if (g == NULL)
3246
0
    return NULL;
3247
3248
0
  g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3249
0
            mips_elf_got_entry_eq, NULL);
3250
0
  if (g->got_entries == NULL)
3251
0
    return NULL;
3252
3253
0
  g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3254
0
              mips_got_page_ref_eq, NULL);
3255
0
  if (g->got_page_refs == NULL)
3256
0
    return NULL;
3257
3258
0
  return g;
3259
0
}
3260
3261
/* Return the GOT info for input bfd ABFD, trying to create a new one if
3262
   CREATE_P and if ABFD doesn't already have a GOT.  */
3263
3264
static struct mips_got_info *
3265
mips_elf_bfd_got (bfd *abfd, bool create_p)
3266
0
{
3267
0
  struct mips_elf_obj_tdata *tdata;
3268
3269
0
  if (!is_mips_elf (abfd))
3270
0
    return NULL;
3271
3272
0
  tdata = mips_elf_tdata (abfd);
3273
0
  if (!tdata->got && create_p)
3274
0
    tdata->got = mips_elf_create_got_info (abfd);
3275
0
  return tdata->got;
3276
0
}
3277
3278
/* Record that ABFD should use output GOT G.  */
3279
3280
static void
3281
mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3282
0
{
3283
0
  struct mips_elf_obj_tdata *tdata;
3284
3285
0
  BFD_ASSERT (is_mips_elf (abfd));
3286
0
  tdata = mips_elf_tdata (abfd);
3287
0
  if (tdata->got)
3288
0
    {
3289
      /* The GOT structure itself and the hash table entries are
3290
   allocated to a bfd, but the hash tables aren't.  */
3291
0
      htab_delete (tdata->got->got_entries);
3292
0
      htab_delete (tdata->got->got_page_refs);
3293
0
      if (tdata->got->got_page_entries)
3294
0
  htab_delete (tdata->got->got_page_entries);
3295
0
    }
3296
0
  tdata->got = g;
3297
0
}
3298
3299
/* Return the dynamic relocation section.  If it doesn't exist, try to
3300
   create a new it if CREATE_P, otherwise return NULL.  Also return NULL
3301
   if creation fails.  */
3302
3303
static asection *
3304
mips_elf_rel_dyn_section (struct bfd_link_info *info, bool create_p)
3305
0
{
3306
0
  const char *dname;
3307
0
  asection *sreloc;
3308
0
  bfd *dynobj;
3309
3310
0
  dname = MIPS_ELF_REL_DYN_NAME (info);
3311
0
  dynobj = elf_hash_table (info)->dynobj;
3312
0
  sreloc = bfd_get_linker_section (dynobj, dname);
3313
0
  if (sreloc == NULL && create_p)
3314
0
    {
3315
0
      sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3316
0
               (SEC_ALLOC
3317
0
                | SEC_LOAD
3318
0
                | SEC_HAS_CONTENTS
3319
0
                | SEC_IN_MEMORY
3320
0
                | SEC_LINKER_CREATED
3321
0
                | SEC_READONLY));
3322
0
      if (sreloc == NULL
3323
0
    || !bfd_set_section_alignment (sreloc,
3324
0
           MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3325
0
  return NULL;
3326
0
    }
3327
0
  return sreloc;
3328
0
}
3329
3330
/* Return the GOT_TLS_* type required by relocation type R_TYPE.  */
3331
3332
static int
3333
mips_elf_reloc_tls_type (unsigned int r_type)
3334
0
{
3335
0
  if (tls_gd_reloc_p (r_type))
3336
0
    return GOT_TLS_GD;
3337
3338
0
  if (tls_ldm_reloc_p (r_type))
3339
0
    return GOT_TLS_LDM;
3340
3341
0
  if (tls_gottprel_reloc_p (r_type))
3342
0
    return GOT_TLS_IE;
3343
3344
0
  return GOT_TLS_NONE;
3345
0
}
3346
3347
/* Return the number of GOT slots needed for GOT TLS type TYPE.  */
3348
3349
static int
3350
mips_tls_got_entries (unsigned int type)
3351
0
{
3352
0
  switch (type)
3353
0
    {
3354
0
    case GOT_TLS_GD:
3355
0
    case GOT_TLS_LDM:
3356
0
      return 2;
3357
3358
0
    case GOT_TLS_IE:
3359
0
      return 1;
3360
3361
0
    case GOT_TLS_NONE:
3362
0
      return 0;
3363
0
    }
3364
0
  abort ();
3365
0
}
3366
3367
/* Count the number of relocations needed for a TLS GOT entry, with
3368
   access types from TLS_TYPE, and symbol H (or a local symbol if H
3369
   is NULL).  */
3370
3371
static int
3372
mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3373
         struct elf_link_hash_entry *h)
3374
0
{
3375
0
  int indx = 0;
3376
0
  bool need_relocs = false;
3377
0
  bool dyn = elf_hash_table (info)->dynamic_sections_created;
3378
3379
0
  if (h != NULL
3380
0
      && h->dynindx != -1
3381
0
      && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
3382
0
      && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
3383
0
    indx = h->dynindx;
3384
3385
0
  if ((bfd_link_dll (info) || indx != 0)
3386
0
      && (h == NULL
3387
0
    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3388
0
    || h->root.type != bfd_link_hash_undefweak))
3389
0
    need_relocs = true;
3390
3391
0
  if (!need_relocs)
3392
0
    return 0;
3393
3394
0
  switch (tls_type)
3395
0
    {
3396
0
    case GOT_TLS_GD:
3397
0
      return indx != 0 ? 2 : 1;
3398
3399
0
    case GOT_TLS_IE:
3400
0
      return 1;
3401
3402
0
    case GOT_TLS_LDM:
3403
0
      return bfd_link_dll (info) ? 1 : 0;
3404
3405
0
    default:
3406
0
      return 0;
3407
0
    }
3408
0
}
3409
3410
/* Add the number of GOT entries and TLS relocations required by ENTRY
3411
   to G.  */
3412
3413
static void
3414
mips_elf_count_got_entry (struct bfd_link_info *info,
3415
        struct mips_got_info *g,
3416
        struct mips_got_entry *entry)
3417
0
{
3418
0
  if (entry->tls_type)
3419
0
    {
3420
0
      g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3421
0
      g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3422
0
          entry->symndx < 0
3423
0
          ? &entry->d.h->root : NULL);
3424
0
    }
3425
0
  else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3426
0
    g->local_gotno += 1;
3427
0
  else
3428
0
    g->global_gotno += 1;
3429
0
}
3430
3431
/* Output a simple dynamic relocation into SRELOC.  */
3432
3433
static void
3434
mips_elf_output_dynamic_relocation (bfd *output_bfd,
3435
            asection *sreloc,
3436
            unsigned long reloc_index,
3437
            unsigned long indx,
3438
            int r_type,
3439
            bfd_vma offset)
3440
0
{
3441
0
  Elf_Internal_Rela rel[3];
3442
3443
0
  memset (rel, 0, sizeof (rel));
3444
3445
0
  rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3446
0
  rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3447
3448
0
  if (ABI_64_P (output_bfd))
3449
0
    {
3450
0
      (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3451
0
  (output_bfd, &rel[0],
3452
0
   (sreloc->contents
3453
0
    + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3454
0
    }
3455
0
  else
3456
0
    bfd_elf32_swap_reloc_out
3457
0
      (output_bfd, &rel[0],
3458
0
       (sreloc->contents
3459
0
  + reloc_index * sizeof (Elf32_External_Rel)));
3460
0
}
3461
3462
/* Initialize a set of TLS GOT entries for one symbol.  */
3463
3464
static void
3465
mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3466
             struct mips_got_entry *entry,
3467
             struct mips_elf_link_hash_entry *h,
3468
             bfd_vma value)
3469
0
{
3470
0
  bool dyn = elf_hash_table (info)->dynamic_sections_created;
3471
0
  struct mips_elf_link_hash_table *htab;
3472
0
  int indx;
3473
0
  asection *sreloc, *sgot;
3474
0
  bfd_vma got_offset, got_offset2;
3475
0
  bool need_relocs = false;
3476
3477
0
  htab = mips_elf_hash_table (info);
3478
0
  if (htab == NULL)
3479
0
    return;
3480
3481
0
  sgot = htab->root.sgot;
3482
3483
0
  indx = 0;
3484
0
  if (h != NULL
3485
0
      && h->root.dynindx != -1
3486
0
      && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
3487
0
      && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3488
0
    indx = h->root.dynindx;
3489
3490
0
  if (entry->tls_initialized)
3491
0
    return;
3492
3493
0
  if ((bfd_link_dll (info) || indx != 0)
3494
0
      && (h == NULL
3495
0
    || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3496
0
    || h->root.type != bfd_link_hash_undefweak))
3497
0
    need_relocs = true;
3498
3499
  /* MINUS_ONE means the symbol is not defined in this object.  It may not
3500
     be defined at all; assume that the value doesn't matter in that
3501
     case.  Otherwise complain if we would use the value.  */
3502
0
  BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3503
0
        || h->root.root.type == bfd_link_hash_undefweak);
3504
3505
  /* Emit necessary relocations.  */
3506
0
  sreloc = mips_elf_rel_dyn_section (info, false);
3507
0
  got_offset = entry->gotidx;
3508
3509
0
  switch (entry->tls_type)
3510
0
    {
3511
0
    case GOT_TLS_GD:
3512
      /* General Dynamic.  */
3513
0
      got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3514
3515
0
      if (need_relocs)
3516
0
  {
3517
0
    mips_elf_output_dynamic_relocation
3518
0
      (abfd, sreloc, sreloc->reloc_count++, indx,
3519
0
       ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3520
0
       sgot->output_offset + sgot->output_section->vma + got_offset);
3521
3522
0
    if (indx)
3523
0
      mips_elf_output_dynamic_relocation
3524
0
        (abfd, sreloc, sreloc->reloc_count++, indx,
3525
0
         ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3526
0
         sgot->output_offset + sgot->output_section->vma + got_offset2);
3527
0
    else
3528
0
      MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3529
0
             sgot->contents + got_offset2);
3530
0
  }
3531
0
      else
3532
0
  {
3533
0
    MIPS_ELF_PUT_WORD (abfd, 1,
3534
0
           sgot->contents + got_offset);
3535
0
    MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3536
0
           sgot->contents + got_offset2);
3537
0
  }
3538
0
      break;
3539
3540
0
    case GOT_TLS_IE:
3541
      /* Initial Exec model.  */
3542
0
      if (need_relocs)
3543
0
  {
3544
0
    if (indx == 0)
3545
0
      MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3546
0
             sgot->contents + got_offset);
3547
0
    else
3548
0
      MIPS_ELF_PUT_WORD (abfd, 0,
3549
0
             sgot->contents + got_offset);
3550
3551
0
    mips_elf_output_dynamic_relocation
3552
0
      (abfd, sreloc, sreloc->reloc_count++, indx,
3553
0
       ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3554
0
       sgot->output_offset + sgot->output_section->vma + got_offset);
3555
0
  }
3556
0
      else
3557
0
  MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3558
0
         sgot->contents + got_offset);
3559
0
      break;
3560
3561
0
    case GOT_TLS_LDM:
3562
      /* The initial offset is zero, and the LD offsets will include the
3563
   bias by DTP_OFFSET.  */
3564
0
      MIPS_ELF_PUT_WORD (abfd, 0,
3565
0
       sgot->contents + got_offset
3566
0
       + MIPS_ELF_GOT_SIZE (abfd));
3567
3568
0
      if (!bfd_link_dll (info))
3569
0
  MIPS_ELF_PUT_WORD (abfd, 1,
3570
0
         sgot->contents + got_offset);
3571
0
      else
3572
0
  mips_elf_output_dynamic_relocation
3573
0
    (abfd, sreloc, sreloc->reloc_count++, indx,
3574
0
     ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3575
0
     sgot->output_offset + sgot->output_section->vma + got_offset);
3576
0
      break;
3577
3578
0
    default:
3579
0
      abort ();
3580
0
    }
3581
3582
0
  entry->tls_initialized = true;
3583
0
}
3584
3585
/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3586
   for global symbol H.  .got.plt comes before the GOT, so the offset
3587
   will be negative.  */
3588
3589
static bfd_vma
3590
mips_elf_gotplt_index (struct bfd_link_info *info,
3591
           struct elf_link_hash_entry *h)
3592
0
{
3593
0
  bfd_vma got_address, got_value;
3594
0
  struct mips_elf_link_hash_table *htab;
3595
3596
0
  htab = mips_elf_hash_table (info);
3597
0
  BFD_ASSERT (htab != NULL);
3598
3599
0
  BFD_ASSERT (h->plt.plist != NULL);
3600
0
  BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3601
3602
  /* Calculate the address of the associated .got.plt entry.  */
3603
0
  got_address = (htab->root.sgotplt->output_section->vma
3604
0
     + htab->root.sgotplt->output_offset
3605
0
     + (h->plt.plist->gotplt_index
3606
0
        * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3607
3608
  /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
3609
0
  got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3610
0
         + htab->root.hgot->root.u.def.section->output_offset
3611
0
         + htab->root.hgot->root.u.def.value);
3612
3613
0
  return got_address - got_value;
3614
0
}
3615
3616
/* Return the GOT offset for address VALUE.   If there is not yet a GOT
3617
   entry for this value, create one.  If R_SYMNDX refers to a TLS symbol,
3618
   create a TLS GOT entry instead.  Return -1 if no satisfactory GOT
3619
   offset can be found.  */
3620
3621
static bfd_vma
3622
mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3623
        bfd_vma value, unsigned long r_symndx,
3624
        struct mips_elf_link_hash_entry *h, int r_type)
3625
0
{
3626
0
  struct mips_elf_link_hash_table *htab;
3627
0
  struct mips_got_entry *entry;
3628
3629
0
  htab = mips_elf_hash_table (info);
3630
0
  BFD_ASSERT (htab != NULL);
3631
3632
0
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3633
0
             r_symndx, h, r_type);
3634
0
  if (!entry)
3635
0
    return MINUS_ONE;
3636
3637
0
  if (entry->tls_type)
3638
0
    mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3639
0
  return entry->gotidx;
3640
0
}
3641
3642
/* Return the GOT index of global symbol H in the primary GOT.  */
3643
3644
static bfd_vma
3645
mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3646
           struct elf_link_hash_entry *h)
3647
0
{
3648
0
  struct mips_elf_link_hash_table *htab;
3649
0
  long global_got_dynindx;
3650
0
  struct mips_got_info *g;
3651
0
  bfd_vma got_index;
3652
3653
0
  htab = mips_elf_hash_table (info);
3654
0
  BFD_ASSERT (htab != NULL);
3655
3656
0
  global_got_dynindx = 0;
3657
0
  if (htab->global_gotsym != NULL)
3658
0
    global_got_dynindx = htab->global_gotsym->dynindx;
3659
3660
  /* Once we determine the global GOT entry with the lowest dynamic
3661
     symbol table index, we must put all dynamic symbols with greater
3662
     indices into the primary GOT.  That makes it easy to calculate the
3663
     GOT offset.  */
3664
0
  BFD_ASSERT (h->dynindx >= global_got_dynindx);
3665
0
  g = mips_elf_bfd_got (obfd, false);
3666
0
  got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3667
0
         * MIPS_ELF_GOT_SIZE (obfd));
3668
0
  BFD_ASSERT (got_index < htab->root.sgot->size);
3669
3670
0
  return got_index;
3671
0
}
3672
3673
/* Return the GOT index for the global symbol indicated by H, which is
3674
   referenced by a relocation of type R_TYPE in IBFD.  */
3675
3676
static bfd_vma
3677
mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3678
         struct elf_link_hash_entry *h, int r_type)
3679
0
{
3680
0
  struct mips_elf_link_hash_table *htab;
3681
0
  struct mips_got_info *g;
3682
0
  struct mips_got_entry lookup, *entry;
3683
0
  bfd_vma gotidx;
3684
3685
0
  htab = mips_elf_hash_table (info);
3686
0
  BFD_ASSERT (htab != NULL);
3687
3688
0
  g = mips_elf_bfd_got (ibfd, false);
3689
0
  BFD_ASSERT (g);
3690
3691
0
  lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3692
0
  if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, false))
3693
0
    return mips_elf_primary_global_got_index (obfd, info, h);
3694
3695
0
  lookup.abfd = ibfd;
3696
0
  lookup.symndx = -1;
3697
0
  lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3698
0
  entry = htab_find (g->got_entries, &lookup);
3699
0
  BFD_ASSERT (entry);
3700
3701
0
  gotidx = entry->gotidx;
3702
0
  BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3703
3704
0
  if (lookup.tls_type)
3705
0
    {
3706
0
      bfd_vma value = MINUS_ONE;
3707
3708
0
      if ((h->root.type == bfd_link_hash_defined
3709
0
     || h->root.type == bfd_link_hash_defweak)
3710
0
    && h->root.u.def.section->output_section)
3711
0
  value = (h->root.u.def.value
3712
0
     + h->root.u.def.section->output_offset
3713
0
     + h->root.u.def.section->output_section->vma);
3714
3715
0
      mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3716
0
    }
3717
0
  return gotidx;
3718
0
}
3719
3720
/* Find a GOT page entry that points to within 32KB of VALUE.  These
3721
   entries are supposed to be placed at small offsets in the GOT, i.e.,
3722
   within 32KB of GP.  Return the index of the GOT entry, or -1 if no
3723
   entry could be created.  If OFFSETP is nonnull, use it to return the
3724
   offset of the GOT entry from VALUE.  */
3725
3726
static bfd_vma
3727
mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3728
       bfd_vma value, bfd_vma *offsetp)
3729
0
{
3730
0
  bfd_vma page, got_index;
3731
0
  struct mips_got_entry *entry;
3732
3733
0
  page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3734
0
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3735
0
             NULL, R_MIPS_GOT_PAGE);
3736
3737
0
  if (!entry)
3738
0
    return MINUS_ONE;
3739
3740
0
  got_index = entry->gotidx;
3741
3742
0
  if (offsetp)
3743
0
    *offsetp = value - entry->d.address;
3744
3745
0
  return got_index;
3746
0
}
3747
3748
/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3749
   EXTERNAL is true if the relocation was originally against a global
3750
   symbol that binds locally.  */
3751
3752
static bfd_vma
3753
mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3754
          bfd_vma value, bool external)
3755
0
{
3756
0
  struct mips_got_entry *entry;
3757
3758
  /* GOT16 relocations against local symbols are followed by a LO16
3759
     relocation; those against global symbols are not.  Thus if the
3760
     symbol was originally local, the GOT16 relocation should load the
3761
     equivalent of %hi(VALUE), otherwise it should load VALUE itself.  */
3762
0
  if (! external)
3763
0
    value = mips_elf_high (value) << 16;
3764
3765
  /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3766
     R_MIPS16_GOT16, R_MIPS_CALL16, etc.  The format of the entry is the
3767
     same in all cases.  */
3768
0
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3769
0
             NULL, R_MIPS_GOT16);
3770
0
  if (entry)
3771
0
    return entry->gotidx;
3772
0
  else
3773
0
    return MINUS_ONE;
3774
0
}
3775
3776
/* Returns the offset for the entry at the INDEXth position
3777
   in the GOT.  */
3778
3779
static bfd_vma
3780
mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3781
        bfd *input_bfd, bfd_vma got_index)
3782
0
{
3783
0
  struct mips_elf_link_hash_table *htab;
3784
0
  asection *sgot;
3785
0
  bfd_vma gp;
3786
3787
0
  htab = mips_elf_hash_table (info);
3788
0
  BFD_ASSERT (htab != NULL);
3789
3790
0
  sgot = htab->root.sgot;
3791
0
  gp = _bfd_get_gp_value (output_bfd)
3792
0
    + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3793
3794
0
  return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3795
0
}
3796
3797
/* Create and return a local GOT entry for VALUE, which was calculated
3798
   from a symbol belonging to INPUT_SECTON.  Return NULL if it could not
3799
   be created.  If R_SYMNDX refers to a TLS symbol, create a TLS entry
3800
   instead.  */
3801
3802
static struct mips_got_entry *
3803
mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3804
         bfd *ibfd, bfd_vma value,
3805
         unsigned long r_symndx,
3806
         struct mips_elf_link_hash_entry *h,
3807
         int r_type)
3808
0
{
3809
0
  struct mips_got_entry lookup, *entry;
3810
0
  void **loc;
3811
0
  struct mips_got_info *g;
3812
0
  struct mips_elf_link_hash_table *htab;
3813
0
  bfd_vma gotidx;
3814
3815
0
  htab = mips_elf_hash_table (info);
3816
0
  BFD_ASSERT (htab != NULL);
3817
3818
0
  g = mips_elf_bfd_got (ibfd, false);
3819
0
  if (g == NULL)
3820
0
    {
3821
0
      g = mips_elf_bfd_got (abfd, false);
3822
0
      BFD_ASSERT (g != NULL);
3823
0
    }
3824
3825
  /* This function shouldn't be called for symbols that live in the global
3826
     area of the GOT.  */
3827
0
  BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3828
3829
0
  lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3830
0
  if (lookup.tls_type)
3831
0
    {
3832
0
      lookup.abfd = ibfd;
3833
0
      if (tls_ldm_reloc_p (r_type))
3834
0
  {
3835
0
    lookup.symndx = 0;
3836
0
    lookup.d.addend = 0;
3837
0
  }
3838
0
      else if (h == NULL)
3839
0
  {
3840
0
    lookup.symndx = r_symndx;
3841
0
    lookup.d.addend = 0;
3842
0
  }
3843
0
      else
3844
0
  {
3845
0
    lookup.symndx = -1;
3846
0
    lookup.d.h = h;
3847
0
  }
3848
3849
0
      entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3850
0
      BFD_ASSERT (entry);
3851
3852
0
      gotidx = entry->gotidx;
3853
0
      BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3854
3855
0
      return entry;
3856
0
    }
3857
3858
0
  lookup.abfd = NULL;
3859
0
  lookup.symndx = -1;
3860
0
  lookup.d.address = value;
3861
0
  loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3862
0
  if (!loc)
3863
0
    return NULL;
3864
3865
0
  entry = (struct mips_got_entry *) *loc;
3866
0
  if (entry)
3867
0
    return entry;
3868
3869
0
  if (g->assigned_low_gotno > g->assigned_high_gotno)
3870
0
    {
3871
      /* We didn't allocate enough space in the GOT.  */
3872
0
      _bfd_error_handler
3873
0
  (_("not enough GOT space for local GOT entries"));
3874
0
      bfd_set_error (bfd_error_bad_value);
3875
0
      return NULL;
3876
0
    }
3877
3878
0
  entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3879
0
  if (!entry)
3880
0
    return NULL;
3881
3882
0
  if (got16_reloc_p (r_type)
3883
0
      || call16_reloc_p (r_type)
3884
0
      || got_page_reloc_p (r_type)
3885
0
      || got_disp_reloc_p (r_type))
3886
0
    lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3887
0
  else
3888
0
    lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3889
3890
0
  *entry = lookup;
3891
0
  *loc = entry;
3892
3893
0
  MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
3894
3895
  /* These GOT entries need a dynamic relocation on VxWorks.  */
3896
0
  if (htab->root.target_os == is_vxworks)
3897
0
    {
3898
0
      Elf_Internal_Rela outrel;
3899
0
      asection *s;
3900
0
      bfd_byte *rloc;
3901
0
      bfd_vma got_address;
3902
3903
0
      s = mips_elf_rel_dyn_section (info, false);
3904
0
      got_address = (htab->root.sgot->output_section->vma
3905
0
         + htab->root.sgot->output_offset
3906
0
         + entry->gotidx);
3907
3908
0
      rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3909
0
      outrel.r_offset = got_address;
3910
0
      outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3911
0
      outrel.r_addend = value;
3912
0
      bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3913
0
    }
3914
3915
0
  return entry;
3916
0
}
3917
3918
/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3919
   The number might be exact or a worst-case estimate, depending on how
3920
   much information is available to elf_backend_omit_section_dynsym at
3921
   the current linking stage.  */
3922
3923
static bfd_size_type
3924
count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3925
0
{
3926
0
  bfd_size_type count;
3927
3928
0
  count = 0;
3929
0
  if (bfd_link_pic (info)
3930
0
      || elf_hash_table (info)->is_relocatable_executable)
3931
0
    {
3932
0
      asection *p;
3933
0
      const struct elf_backend_data *bed;
3934
3935
0
      bed = get_elf_backend_data (output_bfd);
3936
0
      for (p = output_bfd->sections; p ; p = p->next)
3937
0
  if ((p->flags & SEC_EXCLUDE) == 0
3938
0
      && (p->flags & SEC_ALLOC) != 0
3939
0
      && elf_hash_table (info)->dynamic_relocs
3940
0
      && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3941
0
    ++count;
3942
0
    }
3943
0
  return count;
3944
0
}
3945
3946
/* Sort the dynamic symbol table so that symbols that need GOT entries
3947
   appear towards the end.  */
3948
3949
static bool
3950
mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3951
0
{
3952
0
  struct mips_elf_link_hash_table *htab;
3953
0
  struct mips_elf_hash_sort_data hsd;
3954
0
  struct mips_got_info *g;
3955
3956
0
  htab = mips_elf_hash_table (info);
3957
0
  BFD_ASSERT (htab != NULL);
3958
3959
0
  if (htab->root.dynsymcount == 0)
3960
0
    return true;
3961
3962
0
  g = htab->got_info;
3963
0
  if (g == NULL)
3964
0
    return true;
3965
3966
0
  hsd.low = NULL;
3967
0
  hsd.max_unref_got_dynindx
3968
0
    = hsd.min_got_dynindx
3969
0
    = (htab->root.dynsymcount - g->reloc_only_gotno);
3970
  /* Add 1 to local symbol indices to account for the mandatory NULL entry
3971
     at the head of the table; see `_bfd_elf_link_renumber_dynsyms'.  */
3972
0
  hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
3973
0
  hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
3974
0
  hsd.output_bfd = abfd;
3975
0
  if (htab->root.dynobj != NULL
3976
0
      && htab->root.dynamic_sections_created
3977
0
      && info->emit_gnu_hash)
3978
0
    {
3979
0
      asection *s = bfd_get_linker_section (htab->root.dynobj, ".MIPS.xhash");
3980
0
      BFD_ASSERT (s != NULL);
3981
0
      hsd.mipsxhash = s->contents;
3982
0
      BFD_ASSERT (hsd.mipsxhash != NULL);
3983
0
    }
3984
0
  else
3985
0
    hsd.mipsxhash = NULL;
3986
0
  mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
3987
3988
  /* There should have been enough room in the symbol table to
3989
     accommodate both the GOT and non-GOT symbols.  */
3990
0
  BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
3991
0
  BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3992
0
  BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
3993
0
  BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
3994
3995
  /* Now we know which dynamic symbol has the lowest dynamic symbol
3996
     table index in the GOT.  */
3997
0
  htab->global_gotsym = hsd.low;
3998
3999
0
  return true;
4000
0
}
4001
4002
/* If H needs a GOT entry, assign it the highest available dynamic
4003
   index.  Otherwise, assign it the lowest available dynamic
4004
   index.  */
4005
4006
static bool
4007
mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
4008
0
{
4009
0
  struct mips_elf_hash_sort_data *hsd = data;
4010
4011
  /* Symbols without dynamic symbol table entries aren't interesting
4012
     at all.  */
4013
0
  if (h->root.dynindx == -1)
4014
0
    return true;
4015
4016
0
  switch (h->global_got_area)
4017
0
    {
4018
0
    case GGA_NONE:
4019
0
      if (h->root.forced_local)
4020
0
  h->root.dynindx = hsd->max_local_dynindx++;
4021
0
      else
4022
0
  h->root.dynindx = hsd->max_non_got_dynindx++;
4023
0
      break;
4024
4025
0
    case GGA_NORMAL:
4026
0
      h->root.dynindx = --hsd->min_got_dynindx;
4027
0
      hsd->low = (struct elf_link_hash_entry *) h;
4028
0
      break;
4029
4030
0
    case GGA_RELOC_ONLY:
4031
0
      if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
4032
0
  hsd->low = (struct elf_link_hash_entry *) h;
4033
0
      h->root.dynindx = hsd->max_unref_got_dynindx++;
4034
0
      break;
4035
0
    }
4036
4037
  /* Populate the .MIPS.xhash translation table entry with
4038
     the symbol dynindx.  */
4039
0
  if (h->mipsxhash_loc != 0 && hsd->mipsxhash != NULL)
4040
0
    bfd_put_32 (hsd->output_bfd, h->root.dynindx,
4041
0
    hsd->mipsxhash + h->mipsxhash_loc);
4042
4043
0
  return true;
4044
0
}
4045
4046
/* Record that input bfd ABFD requires a GOT entry like *LOOKUP
4047
   (which is owned by the caller and shouldn't be added to the
4048
   hash table directly).  */
4049
4050
static bool
4051
mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
4052
         struct mips_got_entry *lookup)
4053
0
{
4054
0
  struct mips_elf_link_hash_table *htab;
4055
0
  struct mips_got_entry *entry;
4056
0
  struct mips_got_info *g;
4057
0
  void **loc, **bfd_loc;
4058
4059
  /* Make sure there's a slot for this entry in the master GOT.  */
4060
0
  htab = mips_elf_hash_table (info);
4061
0
  g = htab->got_info;
4062
0
  loc = htab_find_slot (g->got_entries, lookup, INSERT);
4063
0
  if (!loc)
4064
0
    return false;
4065
4066
  /* Populate the entry if it isn't already.  */
4067
0
  entry = (struct mips_got_entry *) *loc;
4068
0
  if (!entry)
4069
0
    {
4070
0
      entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
4071
0
      if (!entry)
4072
0
  return false;
4073
4074
0
      lookup->tls_initialized = false;
4075
0
      lookup->gotidx = -1;
4076
0
      *entry = *lookup;
4077
0
      *loc = entry;
4078
0
    }
4079
4080
  /* Reuse the same GOT entry for the BFD's GOT.  */
4081
0
  g = mips_elf_bfd_got (abfd, true);
4082
0
  if (!g)
4083
0
    return false;
4084
4085
0
  bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
4086
0
  if (!bfd_loc)
4087
0
    return false;
4088
4089
0
  if (!*bfd_loc)
4090
0
    *bfd_loc = entry;
4091
0
  return true;
4092
0
}
4093
4094
/* ABFD has a GOT relocation of type R_TYPE against H.  Reserve a GOT
4095
   entry for it.  FOR_CALL is true if the caller is only interested in
4096
   using the GOT entry for calls.  */
4097
4098
static bool
4099
mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
4100
           bfd *abfd, struct bfd_link_info *info,
4101
           bool for_call, int r_type)
4102
0
{
4103
0
  struct mips_elf_link_hash_table *htab;
4104
0
  struct mips_elf_link_hash_entry *hmips;
4105
0
  struct mips_got_entry entry;
4106
0
  unsigned char tls_type;
4107
4108
0
  htab = mips_elf_hash_table (info);
4109
0
  BFD_ASSERT (htab != NULL);
4110
4111
0
  hmips = (struct mips_elf_link_hash_entry *) h;
4112
0
  if (!for_call)
4113
0
    hmips->got_only_for_calls = false;
4114
4115
  /* A global symbol in the GOT must also be in the dynamic symbol
4116
     table.  */
4117
0
  if (h->dynindx == -1)
4118
0
    {
4119
0
      switch (ELF_ST_VISIBILITY (h->other))
4120
0
  {
4121
0
  case STV_INTERNAL:
4122
0
  case STV_HIDDEN:
4123
0
    _bfd_mips_elf_hide_symbol (info, h, true);
4124
0
    break;
4125
0
  }
4126
0
      if (!bfd_elf_link_record_dynamic_symbol (info, h))
4127
0
  return false;
4128
0
    }
4129
4130
0
  tls_type = mips_elf_reloc_tls_type (r_type);
4131
0
  if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
4132
0
    hmips->global_got_area = GGA_NORMAL;
4133
4134
0
  entry.abfd = abfd;
4135
0
  entry.symndx = -1;
4136
0
  entry.d.h = (struct mips_elf_link_hash_entry *) h;
4137
0
  entry.tls_type = tls_type;
4138
0
  return mips_elf_record_got_entry (info, abfd, &entry);
4139
0
}
4140
4141
/* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
4142
   where SYMNDX is a local symbol.  Reserve a GOT entry for it.  */
4143
4144
static bool
4145
mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
4146
          struct bfd_link_info *info, int r_type)
4147
0
{
4148
0
  struct mips_elf_link_hash_table *htab;
4149
0
  struct mips_got_info *g;
4150
0
  struct mips_got_entry entry;
4151
4152
0
  htab = mips_elf_hash_table (info);
4153
0
  BFD_ASSERT (htab != NULL);
4154
4155
0
  g = htab->got_info;
4156
0
  BFD_ASSERT (g != NULL);
4157
4158
0
  entry.abfd = abfd;
4159
0
  entry.symndx = symndx;
4160
0
  entry.d.addend = addend;
4161
0
  entry.tls_type = mips_elf_reloc_tls_type (r_type);
4162
0
  return mips_elf_record_got_entry (info, abfd, &entry);
4163
0
}
4164
4165
/* Record that ABFD has a page relocation against SYMNDX + ADDEND.
4166
   H is the symbol's hash table entry, or null if SYMNDX is local
4167
   to ABFD.  */
4168
4169
static bool
4170
mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
4171
            long symndx, struct elf_link_hash_entry *h,
4172
            bfd_signed_vma addend)
4173
0
{
4174
0
  struct mips_elf_link_hash_table *htab;
4175
0
  struct mips_got_info *g1, *g2;
4176
0
  struct mips_got_page_ref lookup, *entry;
4177
0
  void **loc, **bfd_loc;
4178
4179
0
  htab = mips_elf_hash_table (info);
4180
0
  BFD_ASSERT (htab != NULL);
4181
4182
0
  g1 = htab->got_info;
4183
0
  BFD_ASSERT (g1 != NULL);
4184
4185
0
  if (h)
4186
0
    {
4187
0
      lookup.symndx = -1;
4188
0
      lookup.u.h = (struct mips_elf_link_hash_entry *) h;
4189
0
    }
4190
0
  else
4191
0
    {
4192
0
      lookup.symndx = symndx;
4193
0
      lookup.u.abfd = abfd;
4194
0
    }
4195
0
  lookup.addend = addend;
4196
0
  loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
4197
0
  if (loc == NULL)
4198
0
    return false;
4199
4200
0
  entry = (struct mips_got_page_ref *) *loc;
4201
0
  if (!entry)
4202
0
    {
4203
0
      entry = bfd_alloc (abfd, sizeof (*entry));
4204
0
      if (!entry)
4205
0
  return false;
4206
4207
0
      *entry = lookup;
4208
0
      *loc = entry;
4209
0
    }
4210
4211
  /* Add the same entry to the BFD's GOT.  */
4212
0
  g2 = mips_elf_bfd_got (abfd, true);
4213
0
  if (!g2)
4214
0
    return false;
4215
4216
0
  bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
4217
0
  if (!bfd_loc)
4218
0
    return false;
4219
4220
0
  if (!*bfd_loc)
4221
0
    *bfd_loc = entry;
4222
4223
0
  return true;
4224
0
}
4225
4226
/* Add room for N relocations to the .rel(a).dyn section in ABFD.  */
4227
4228
static void
4229
mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4230
               unsigned int n)
4231
0
{
4232
0
  asection *s;
4233
0
  struct mips_elf_link_hash_table *htab;
4234
4235
0
  htab = mips_elf_hash_table (info);
4236
0
  BFD_ASSERT (htab != NULL);
4237
4238
0
  s = mips_elf_rel_dyn_section (info, false);
4239
0
  BFD_ASSERT (s != NULL);
4240
4241
0
  if (htab->root.target_os == is_vxworks)
4242
0
    s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4243
0
  else
4244
0
    {
4245
0
      if (s->size == 0)
4246
0
  {
4247
    /* Make room for a null element.  */
4248
0
    s->size += MIPS_ELF_REL_SIZE (abfd);
4249
0
    ++s->reloc_count;
4250
0
  }
4251
0
      s->size += n * MIPS_ELF_REL_SIZE (abfd);
4252
0
    }
4253
0
}
4254

4255
/* A htab_traverse callback for GOT entries, with DATA pointing to a
4256
   mips_elf_traverse_got_arg structure.  Count the number of GOT
4257
   entries and TLS relocs.  Set DATA->value to true if we need
4258
   to resolve indirect or warning symbols and then recreate the GOT.  */
4259
4260
static int
4261
mips_elf_check_recreate_got (void **entryp, void *data)
4262
0
{
4263
0
  struct mips_got_entry *entry;
4264
0
  struct mips_elf_traverse_got_arg *arg;
4265
4266
0
  entry = (struct mips_got_entry *) *entryp;
4267
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4268
0
  if (entry->abfd != NULL && entry->symndx == -1)
4269
0
    {
4270
0
      struct mips_elf_link_hash_entry *h;
4271
4272
0
      h = entry->d.h;
4273
0
      if (h->root.root.type == bfd_link_hash_indirect
4274
0
    || h->root.root.type == bfd_link_hash_warning)
4275
0
  {
4276
0
    arg->value = true;
4277
0
    return 0;
4278
0
  }
4279
0
    }
4280
0
  mips_elf_count_got_entry (arg->info, arg->g, entry);
4281
0
  return 1;
4282
0
}
4283
4284
/* A htab_traverse callback for GOT entries, with DATA pointing to a
4285
   mips_elf_traverse_got_arg structure.  Add all entries to DATA->g,
4286
   converting entries for indirect and warning symbols into entries
4287
   for the target symbol.  Set DATA->g to null on error.  */
4288
4289
static int
4290
mips_elf_recreate_got (void **entryp, void *data)
4291
0
{
4292
0
  struct mips_got_entry new_entry, *entry;
4293
0
  struct mips_elf_traverse_got_arg *arg;
4294
0
  void **slot;
4295
4296
0
  entry = (struct mips_got_entry *) *entryp;
4297
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4298
0
  if (entry->abfd != NULL
4299
0
      && entry->symndx == -1
4300
0
      && (entry->d.h->root.root.type == bfd_link_hash_indirect
4301
0
    || entry->d.h->root.root.type == bfd_link_hash_warning))
4302
0
    {
4303
0
      struct mips_elf_link_hash_entry *h;
4304
4305
0
      new_entry = *entry;
4306
0
      entry = &new_entry;
4307
0
      h = entry->d.h;
4308
0
      do
4309
0
  {
4310
0
    BFD_ASSERT (h->global_got_area == GGA_NONE);
4311
0
    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4312
0
  }
4313
0
      while (h->root.root.type == bfd_link_hash_indirect
4314
0
       || h->root.root.type == bfd_link_hash_warning);
4315
0
      entry->d.h = h;
4316
0
    }
4317
0
  slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4318
0
  if (slot == NULL)
4319
0
    {
4320
0
      arg->g = NULL;
4321
0
      return 0;
4322
0
    }
4323
0
  if (*slot == NULL)
4324
0
    {
4325
0
      if (entry == &new_entry)
4326
0
  {
4327
0
    entry = bfd_alloc (entry->abfd, sizeof (*entry));
4328
0
    if (!entry)
4329
0
      {
4330
0
        arg->g = NULL;
4331
0
        return 0;
4332
0
      }
4333
0
    *entry = new_entry;
4334
0
  }
4335
0
      *slot = entry;
4336
0
      mips_elf_count_got_entry (arg->info, arg->g, entry);
4337
0
    }
4338
0
  return 1;
4339
0
}
4340
4341
/* Return the maximum number of GOT page entries required for RANGE.  */
4342
4343
static bfd_vma
4344
mips_elf_pages_for_range (const struct mips_got_page_range *range)
4345
0
{
4346
0
  return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4347
0
}
4348
4349
/* Record that G requires a page entry that can reach SEC + ADDEND.  */
4350
4351
static bool
4352
mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
4353
        asection *sec, bfd_signed_vma addend)
4354
0
{
4355
0
  struct mips_got_info *g = arg->g;
4356
0
  struct mips_got_page_entry lookup, *entry;
4357
0
  struct mips_got_page_range **range_ptr, *range;
4358
0
  bfd_vma old_pages, new_pages;
4359
0
  void **loc;
4360
4361
  /* Find the mips_got_page_entry hash table entry for this section.  */
4362
0
  lookup.sec = sec;
4363
0
  loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4364
0
  if (loc == NULL)
4365
0
    return false;
4366
4367
  /* Create a mips_got_page_entry if this is the first time we've
4368
     seen the section.  */
4369
0
  entry = (struct mips_got_page_entry *) *loc;
4370
0
  if (!entry)
4371
0
    {
4372
0
      entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
4373
0
      if (!entry)
4374
0
  return false;
4375
4376
0
      entry->sec = sec;
4377
0
      *loc = entry;
4378
0
    }
4379
4380
  /* Skip over ranges whose maximum extent cannot share a page entry
4381
     with ADDEND.  */
4382
0
  range_ptr = &entry->ranges;
4383
0
  while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4384
0
    range_ptr = &(*range_ptr)->next;
4385
4386
  /* If we scanned to the end of the list, or found a range whose
4387
     minimum extent cannot share a page entry with ADDEND, create
4388
     a new singleton range.  */
4389
0
  range = *range_ptr;
4390
0
  if (!range || addend < range->min_addend - 0xffff)
4391
0
    {
4392
0
      range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
4393
0
      if (!range)
4394
0
  return false;
4395
4396
0
      range->next = *range_ptr;
4397
0
      range->min_addend = addend;
4398
0
      range->max_addend = addend;
4399
4400
0
      *range_ptr = range;
4401
0
      entry->num_pages++;
4402
0
      g->page_gotno++;
4403
0
      return true;
4404
0
    }
4405
4406
  /* Remember how many pages the old range contributed.  */
4407
0
  old_pages = mips_elf_pages_for_range (range);
4408
4409
  /* Update the ranges.  */
4410
0
  if (addend < range->min_addend)
4411
0
    range->min_addend = addend;
4412
0
  else if (addend > range->max_addend)
4413
0
    {
4414
0
      if (range->next && addend >= range->next->min_addend - 0xffff)
4415
0
  {
4416
0
    old_pages += mips_elf_pages_for_range (range->next);
4417
0
    range->max_addend = range->next->max_addend;
4418
0
    range->next = range->next->next;
4419
0
  }
4420
0
      else
4421
0
  range->max_addend = addend;
4422
0
    }
4423
4424
  /* Record any change in the total estimate.  */
4425
0
  new_pages = mips_elf_pages_for_range (range);
4426
0
  if (old_pages != new_pages)
4427
0
    {
4428
0
      entry->num_pages += new_pages - old_pages;
4429
0
      g->page_gotno += new_pages - old_pages;
4430
0
    }
4431
4432
0
  return true;
4433
0
}
4434
4435
/* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4436
   and for which DATA points to a mips_elf_traverse_got_arg.  Work out
4437
   whether the page reference described by *REFP needs a GOT page entry,
4438
   and record that entry in DATA->g if so.  Set DATA->g to null on failure.  */
4439
4440
static int
4441
mips_elf_resolve_got_page_ref (void **refp, void *data)
4442
0
{
4443
0
  struct mips_got_page_ref *ref;
4444
0
  struct mips_elf_traverse_got_arg *arg;
4445
0
  struct mips_elf_link_hash_table *htab;
4446
0
  asection *sec;
4447
0
  bfd_vma addend;
4448
4449
0
  ref = (struct mips_got_page_ref *) *refp;
4450
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4451
0
  htab = mips_elf_hash_table (arg->info);
4452
4453
0
  if (ref->symndx < 0)
4454
0
    {
4455
0
      struct mips_elf_link_hash_entry *h;
4456
4457
      /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries.  */
4458
0
      h = ref->u.h;
4459
0
      if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4460
0
  return 1;
4461
4462
      /* Ignore undefined symbols; we'll issue an error later if
4463
   appropriate.  */
4464
0
      if (!((h->root.root.type == bfd_link_hash_defined
4465
0
       || h->root.root.type == bfd_link_hash_defweak)
4466
0
      && h->root.root.u.def.section))
4467
0
  return 1;
4468
4469
0
      sec = h->root.root.u.def.section;
4470
0
      addend = h->root.root.u.def.value + ref->addend;
4471
0
    }
4472
0
  else
4473
0
    {
4474
0
      Elf_Internal_Sym *isym;
4475
4476
      /* Read in the symbol.  */
4477
0
      isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ref->u.abfd,
4478
0
            ref->symndx);
4479
0
      if (isym == NULL)
4480
0
  {
4481
0
    arg->g = NULL;
4482
0
    return 0;
4483
0
  }
4484
4485
      /* Get the associated input section.  */
4486
0
      sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4487
0
      if (sec == NULL)
4488
0
  {
4489
0
    arg->g = NULL;
4490
0
    return 0;
4491
0
  }
4492
4493
      /* If this is a mergable section, work out the section and offset
4494
   of the merged data.  For section symbols, the addend specifies
4495
   of the offset _of_ the first byte in the data, otherwise it
4496
   specifies the offset _from_ the first byte.  */
4497
0
      if (sec->flags & SEC_MERGE)
4498
0
  {
4499
0
    void *secinfo;
4500
4501
0
    secinfo = elf_section_data (sec)->sec_info;
4502
0
    if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4503
0
      addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4504
0
             isym->st_value + ref->addend);
4505
0
    else
4506
0
      addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4507
0
             isym->st_value) + ref->addend;
4508
0
  }
4509
0
      else
4510
0
  addend = isym->st_value + ref->addend;
4511
0
    }
4512
0
  if (!mips_elf_record_got_page_entry (arg, sec, addend))
4513
0
    {
4514
0
      arg->g = NULL;
4515
0
      return 0;
4516
0
    }
4517
0
  return 1;
4518
0
}
4519
4520
/* If any entries in G->got_entries are for indirect or warning symbols,
4521
   replace them with entries for the target symbol.  Convert g->got_page_refs
4522
   into got_page_entry structures and estimate the number of page entries
4523
   that they require.  */
4524
4525
static bool
4526
mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4527
            struct mips_got_info *g)
4528
0
{
4529
0
  struct mips_elf_traverse_got_arg tga;
4530
0
  struct mips_got_info oldg;
4531
4532
0
  oldg = *g;
4533
4534
0
  tga.info = info;
4535
0
  tga.g = g;
4536
0
  tga.value = false;
4537
0
  htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4538
0
  if (tga.value)
4539
0
    {
4540
0
      *g = oldg;
4541
0
      g->got_entries = htab_create (htab_size (oldg.got_entries),
4542
0
            mips_elf_got_entry_hash,
4543
0
            mips_elf_got_entry_eq, NULL);
4544
0
      if (!g->got_entries)
4545
0
  return false;
4546
4547
0
      htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4548
0
      if (!tga.g)
4549
0
  return false;
4550
4551
0
      htab_delete (oldg.got_entries);
4552
0
    }
4553
4554
0
  g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4555
0
           mips_got_page_entry_eq, NULL);
4556
0
  if (g->got_page_entries == NULL)
4557
0
    return false;
4558
4559
0
  tga.info = info;
4560
0
  tga.g = g;
4561
0
  htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4562
4563
0
  return true;
4564
0
}
4565
4566
/* Return true if a GOT entry for H should live in the local rather than
4567
   global GOT area.  */
4568
4569
static bool
4570
mips_use_local_got_p (struct bfd_link_info *info,
4571
          struct mips_elf_link_hash_entry *h)
4572
0
{
4573
  /* Symbols that aren't in the dynamic symbol table must live in the
4574
     local GOT.  This includes symbols that are completely undefined
4575
     and which therefore don't bind locally.  We'll report undefined
4576
     symbols later if appropriate.  */
4577
0
  if (h->root.dynindx == -1)
4578
0
    return true;
4579
4580
  /* Absolute symbols, if ever they need a GOT entry, cannot ever go
4581
     to the local GOT, as they would be implicitly relocated by the
4582
     base address by the dynamic loader.  */
4583
0
  if (bfd_is_abs_symbol (&h->root.root))
4584
0
    return false;
4585
4586
  /* Symbols that bind locally can (and in the case of forced-local
4587
     symbols, must) live in the local GOT.  */
4588
0
  if (h->got_only_for_calls
4589
0
      ? SYMBOL_CALLS_LOCAL (info, &h->root)
4590
0
      : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4591
0
    return true;
4592
4593
  /* If this is an executable that must provide a definition of the symbol,
4594
     either though PLTs or copy relocations, then that address should go in
4595
     the local rather than global GOT.  */
4596
0
  if (bfd_link_executable (info) && h->has_static_relocs)
4597
0
    return true;
4598
4599
0
  return false;
4600
0
}
4601
4602
/* A mips_elf_link_hash_traverse callback for which DATA points to the
4603
   link_info structure.  Decide whether the hash entry needs an entry in
4604
   the global part of the primary GOT, setting global_got_area accordingly.
4605
   Count the number of global symbols that are in the primary GOT only
4606
   because they have relocations against them (reloc_only_gotno).  */
4607
4608
static bool
4609
mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4610
0
{
4611
0
  struct bfd_link_info *info;
4612
0
  struct mips_elf_link_hash_table *htab;
4613
0
  struct mips_got_info *g;
4614
4615
0
  info = (struct bfd_link_info *) data;
4616
0
  htab = mips_elf_hash_table (info);
4617
0
  g = htab->got_info;
4618
0
  if (h->global_got_area != GGA_NONE)
4619
0
    {
4620
      /* Make a final decision about whether the symbol belongs in the
4621
   local or global GOT.  */
4622
0
      if (mips_use_local_got_p (info, h))
4623
  /* The symbol belongs in the local GOT.  We no longer need this
4624
     entry if it was only used for relocations; those relocations
4625
     will be against the null or section symbol instead of H.  */
4626
0
  h->global_got_area = GGA_NONE;
4627
0
      else if (htab->root.target_os == is_vxworks
4628
0
         && h->got_only_for_calls
4629
0
         && h->root.plt.plist->mips_offset != MINUS_ONE)
4630
  /* On VxWorks, calls can refer directly to the .got.plt entry;
4631
     they don't need entries in the regular GOT.  .got.plt entries
4632
     will be allocated by _bfd_mips_elf_adjust_dynamic_symbol.  */
4633
0
  h->global_got_area = GGA_NONE;
4634
0
      else if (h->global_got_area == GGA_RELOC_ONLY)
4635
0
  {
4636
0
    g->reloc_only_gotno++;
4637
0
    g->global_gotno++;
4638
0
  }
4639
0
    }
4640
0
  return 1;
4641
0
}
4642

4643
/* A htab_traverse callback for GOT entries.  Add each one to the GOT
4644
   given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
4645
4646
static int
4647
mips_elf_add_got_entry (void **entryp, void *data)
4648
0
{
4649
0
  struct mips_got_entry *entry;
4650
0
  struct mips_elf_traverse_got_arg *arg;
4651
0
  void **slot;
4652
4653
0
  entry = (struct mips_got_entry *) *entryp;
4654
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4655
0
  slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4656
0
  if (!slot)
4657
0
    {
4658
0
      arg->g = NULL;
4659
0
      return 0;
4660
0
    }
4661
0
  if (!*slot)
4662
0
    {
4663
0
      *slot = entry;
4664
0
      mips_elf_count_got_entry (arg->info, arg->g, entry);
4665
0
    }
4666
0
  return 1;
4667
0
}
4668
4669
/* A htab_traverse callback for GOT page entries.  Add each one to the GOT
4670
   given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
4671
4672
static int
4673
mips_elf_add_got_page_entry (void **entryp, void *data)
4674
0
{
4675
0
  struct mips_got_page_entry *entry;
4676
0
  struct mips_elf_traverse_got_arg *arg;
4677
0
  void **slot;
4678
4679
0
  entry = (struct mips_got_page_entry *) *entryp;
4680
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4681
0
  slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4682
0
  if (!slot)
4683
0
    {
4684
0
      arg->g = NULL;
4685
0
      return 0;
4686
0
    }
4687
0
  if (!*slot)
4688
0
    {
4689
0
      *slot = entry;
4690
0
      arg->g->page_gotno += entry->num_pages;
4691
0
    }
4692
0
  return 1;
4693
0
}
4694
4695
/* Consider merging FROM, which is ABFD's GOT, into TO.  Return -1 if
4696
   this would lead to overflow, 1 if they were merged successfully,
4697
   and 0 if a merge failed due to lack of memory.  (These values are chosen
4698
   so that nonnegative return values can be returned by a htab_traverse
4699
   callback.)  */
4700
4701
static int
4702
mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4703
       struct mips_got_info *to,
4704
       struct mips_elf_got_per_bfd_arg *arg)
4705
0
{
4706
0
  struct mips_elf_traverse_got_arg tga;
4707
0
  unsigned int estimate;
4708
4709
  /* Work out how many page entries we would need for the combined GOT.  */
4710
0
  estimate = arg->max_pages;
4711
0
  if (estimate >= from->page_gotno + to->page_gotno)
4712
0
    estimate = from->page_gotno + to->page_gotno;
4713
4714
  /* And conservatively estimate how many local and TLS entries
4715
     would be needed.  */
4716
0
  estimate += from->local_gotno + to->local_gotno;
4717
0
  estimate += from->tls_gotno + to->tls_gotno;
4718
4719
  /* If we're merging with the primary got, any TLS relocations will
4720
     come after the full set of global entries.  Otherwise estimate those
4721
     conservatively as well.  */
4722
0
  if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4723
0
    estimate += arg->global_count;
4724
0
  else
4725
0
    estimate += from->global_gotno + to->global_gotno;
4726
4727
  /* Bail out if the combined GOT might be too big.  */
4728
0
  if (estimate > arg->max_count)
4729
0
    return -1;
4730
4731
  /* Transfer the bfd's got information from FROM to TO.  */
4732
0
  tga.info = arg->info;
4733
0
  tga.g = to;
4734
0
  htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4735
0
  if (!tga.g)
4736
0
    return 0;
4737
4738
0
  htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4739
0
  if (!tga.g)
4740
0
    return 0;
4741
4742
0
  mips_elf_replace_bfd_got (abfd, to);
4743
0
  return 1;
4744
0
}
4745
4746
/* Attempt to merge GOT G, which belongs to ABFD.  Try to use as much
4747
   as possible of the primary got, since it doesn't require explicit
4748
   dynamic relocations, but don't use bfds that would reference global
4749
   symbols out of the addressable range.  Failing the primary got,
4750
   attempt to merge with the current got, or finish the current got
4751
   and then make make the new got current.  */
4752
4753
static bool
4754
mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4755
        struct mips_elf_got_per_bfd_arg *arg)
4756
0
{
4757
0
  unsigned int estimate;
4758
0
  int result;
4759
4760
0
  if (!mips_elf_resolve_final_got_entries (arg->info, g))
4761
0
    return false;
4762
4763
  /* Work out the number of page, local and TLS entries.  */
4764
0
  estimate = arg->max_pages;
4765
0
  if (estimate > g->page_gotno)
4766
0
    estimate = g->page_gotno;
4767
0
  estimate += g->local_gotno + g->tls_gotno;
4768
4769
  /* We place TLS GOT entries after both locals and globals.  The globals
4770
     for the primary GOT may overflow the normal GOT size limit, so be
4771
     sure not to merge a GOT which requires TLS with the primary GOT in that
4772
     case.  This doesn't affect non-primary GOTs.  */
4773
0
  estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4774
4775
0
  if (estimate <= arg->max_count)
4776
0
    {
4777
      /* If we don't have a primary GOT, use it as
4778
   a starting point for the primary GOT.  */
4779
0
      if (!arg->primary)
4780
0
  {
4781
0
    arg->primary = g;
4782
0
    return true;
4783
0
  }
4784
4785
      /* Try merging with the primary GOT.  */
4786
0
      result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4787
0
      if (result >= 0)
4788
0
  return result;
4789
0
    }
4790
4791
  /* If we can merge with the last-created got, do it.  */
4792
0
  if (arg->current)
4793
0
    {
4794
0
      result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4795
0
      if (result >= 0)
4796
0
  return result;
4797
0
    }
4798
4799
  /* Well, we couldn't merge, so create a new GOT.  Don't check if it
4800
     fits; if it turns out that it doesn't, we'll get relocation
4801
     overflows anyway.  */
4802
0
  g->next = arg->current;
4803
0
  arg->current = g;
4804
4805
0
  return true;
4806
0
}
4807
4808
/* ENTRYP is a hash table entry for a mips_got_entry.  Set its gotidx
4809
   to GOTIDX, duplicating the entry if it has already been assigned
4810
   an index in a different GOT.  */
4811
4812
static bool
4813
mips_elf_set_gotidx (void **entryp, long gotidx)
4814
0
{
4815
0
  struct mips_got_entry *entry;
4816
4817
0
  entry = (struct mips_got_entry *) *entryp;
4818
0
  if (entry->gotidx > 0)
4819
0
    {
4820
0
      struct mips_got_entry *new_entry;
4821
4822
0
      new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4823
0
      if (!new_entry)
4824
0
  return false;
4825
4826
0
      *new_entry = *entry;
4827
0
      *entryp = new_entry;
4828
0
      entry = new_entry;
4829
0
    }
4830
0
  entry->gotidx = gotidx;
4831
0
  return true;
4832
0
}
4833
4834
/* Set the TLS GOT index for the GOT entry in ENTRYP.  DATA points to a
4835
   mips_elf_traverse_got_arg in which DATA->value is the size of one
4836
   GOT entry.  Set DATA->g to null on failure.  */
4837
4838
static int
4839
mips_elf_initialize_tls_index (void **entryp, void *data)
4840
0
{
4841
0
  struct mips_got_entry *entry;
4842
0
  struct mips_elf_traverse_got_arg *arg;
4843
4844
  /* We're only interested in TLS symbols.  */
4845
0
  entry = (struct mips_got_entry *) *entryp;
4846
0
  if (entry->tls_type == GOT_TLS_NONE)
4847
0
    return 1;
4848
4849
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4850
0
  if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4851
0
    {
4852
0
      arg->g = NULL;
4853
0
      return 0;
4854
0
    }
4855
4856
  /* Account for the entries we've just allocated.  */
4857
0
  arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4858
0
  return 1;
4859
0
}
4860
4861
/* A htab_traverse callback for GOT entries, where DATA points to a
4862
   mips_elf_traverse_got_arg.  Set the global_got_area of each global
4863
   symbol to DATA->value.  */
4864
4865
static int
4866
mips_elf_set_global_got_area (void **entryp, void *data)
4867
0
{
4868
0
  struct mips_got_entry *entry;
4869
0
  struct mips_elf_traverse_got_arg *arg;
4870
4871
0
  entry = (struct mips_got_entry *) *entryp;
4872
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4873
0
  if (entry->abfd != NULL
4874
0
      && entry->symndx == -1
4875
0
      && entry->d.h->global_got_area != GGA_NONE)
4876
0
    entry->d.h->global_got_area = arg->value;
4877
0
  return 1;
4878
0
}
4879
4880
/* A htab_traverse callback for secondary GOT entries, where DATA points
4881
   to a mips_elf_traverse_got_arg.  Assign GOT indices to global entries
4882
   and record the number of relocations they require.  DATA->value is
4883
   the size of one GOT entry.  Set DATA->g to null on failure.  */
4884
4885
static int
4886
mips_elf_set_global_gotidx (void **entryp, void *data)
4887
0
{
4888
0
  struct mips_got_entry *entry;
4889
0
  struct mips_elf_traverse_got_arg *arg;
4890
4891
0
  entry = (struct mips_got_entry *) *entryp;
4892
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4893
0
  if (entry->abfd != NULL
4894
0
      && entry->symndx == -1
4895
0
      && entry->d.h->global_got_area != GGA_NONE)
4896
0
    {
4897
0
      if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
4898
0
  {
4899
0
    arg->g = NULL;
4900
0
    return 0;
4901
0
  }
4902
0
      arg->g->assigned_low_gotno += 1;
4903
4904
0
      if (bfd_link_pic (arg->info)
4905
0
    || (elf_hash_table (arg->info)->dynamic_sections_created
4906
0
        && entry->d.h->root.def_dynamic
4907
0
        && !entry->d.h->root.def_regular))
4908
0
  arg->g->relocs += 1;
4909
0
    }
4910
4911
0
  return 1;
4912
0
}
4913
4914
/* A htab_traverse callback for GOT entries for which DATA is the
4915
   bfd_link_info.  Forbid any global symbols from having traditional
4916
   lazy-binding stubs.  */
4917
4918
static int
4919
mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4920
0
{
4921
0
  struct bfd_link_info *info;
4922
0
  struct mips_elf_link_hash_table *htab;
4923
0
  struct mips_got_entry *entry;
4924
4925
0
  entry = (struct mips_got_entry *) *entryp;
4926
0
  info = (struct bfd_link_info *) data;
4927
0
  htab = mips_elf_hash_table (info);
4928
0
  BFD_ASSERT (htab != NULL);
4929
4930
0
  if (entry->abfd != NULL
4931
0
      && entry->symndx == -1
4932
0
      && entry->d.h->needs_lazy_stub)
4933
0
    {
4934
0
      entry->d.h->needs_lazy_stub = false;
4935
0
      htab->lazy_stub_count--;
4936
0
    }
4937
4938
0
  return 1;
4939
0
}
4940
4941
/* Return the offset of an input bfd IBFD's GOT from the beginning of
4942
   the primary GOT.  */
4943
static bfd_vma
4944
mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4945
0
{
4946
0
  if (!g->next)
4947
0
    return 0;
4948
4949
0
  g = mips_elf_bfd_got (ibfd, false);
4950
0
  if (! g)
4951
0
    return 0;
4952
4953
0
  BFD_ASSERT (g->next);
4954
4955
0
  g = g->next;
4956
4957
0
  return (g->local_gotno + g->global_gotno + g->tls_gotno)
4958
0
    * MIPS_ELF_GOT_SIZE (abfd);
4959
0
}
4960
4961
/* Turn a single GOT that is too big for 16-bit addressing into
4962
   a sequence of GOTs, each one 16-bit addressable.  */
4963
4964
static bool
4965
mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4966
        asection *got, bfd_size_type pages)
4967
0
{
4968
0
  struct mips_elf_link_hash_table *htab;
4969
0
  struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4970
0
  struct mips_elf_traverse_got_arg tga;
4971
0
  struct mips_got_info *g, *gg;
4972
0
  unsigned int assign, needed_relocs;
4973
0
  bfd *dynobj, *ibfd;
4974
4975
0
  dynobj = elf_hash_table (info)->dynobj;
4976
0
  htab = mips_elf_hash_table (info);
4977
0
  BFD_ASSERT (htab != NULL);
4978
4979
0
  g = htab->got_info;
4980
4981
0
  got_per_bfd_arg.obfd = abfd;
4982
0
  got_per_bfd_arg.info = info;
4983
0
  got_per_bfd_arg.current = NULL;
4984
0
  got_per_bfd_arg.primary = NULL;
4985
0
  got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4986
0
        / MIPS_ELF_GOT_SIZE (abfd))
4987
0
             - htab->reserved_gotno);
4988
0
  got_per_bfd_arg.max_pages = pages;
4989
  /* The number of globals that will be included in the primary GOT.
4990
     See the calls to mips_elf_set_global_got_area below for more
4991
     information.  */
4992
0
  got_per_bfd_arg.global_count = g->global_gotno;
4993
4994
  /* Try to merge the GOTs of input bfds together, as long as they
4995
     don't seem to exceed the maximum GOT size, choosing one of them
4996
     to be the primary GOT.  */
4997
0
  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
4998
0
    {
4999
0
      gg = mips_elf_bfd_got (ibfd, false);
5000
0
      if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
5001
0
  return false;
5002
0
    }
5003
5004
  /* If we do not find any suitable primary GOT, create an empty one.  */
5005
0
  if (got_per_bfd_arg.primary == NULL)
5006
0
    g->next = mips_elf_create_got_info (abfd);
5007
0
  else
5008
0
    g->next = got_per_bfd_arg.primary;
5009
0
  g->next->next = got_per_bfd_arg.current;
5010
5011
  /* GG is now the master GOT, and G is the primary GOT.  */
5012
0
  gg = g;
5013
0
  g = g->next;
5014
5015
  /* Map the output bfd to the primary got.  That's what we're going
5016
     to use for bfds that use GOT16 or GOT_PAGE relocations that we
5017
     didn't mark in check_relocs, and we want a quick way to find it.
5018
     We can't just use gg->next because we're going to reverse the
5019
     list.  */
5020
0
  mips_elf_replace_bfd_got (abfd, g);
5021
5022
  /* Every symbol that is referenced in a dynamic relocation must be
5023
     present in the primary GOT, so arrange for them to appear after
5024
     those that are actually referenced.  */
5025
0
  gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
5026
0
  g->global_gotno = gg->global_gotno;
5027
5028
0
  tga.info = info;
5029
0
  tga.value = GGA_RELOC_ONLY;
5030
0
  htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
5031
0
  tga.value = GGA_NORMAL;
5032
0
  htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
5033
5034
  /* Now go through the GOTs assigning them offset ranges.
5035
     [assigned_low_gotno, local_gotno[ will be set to the range of local
5036
     entries in each GOT.  We can then compute the end of a GOT by
5037
     adding local_gotno to global_gotno.  We reverse the list and make
5038
     it circular since then we'll be able to quickly compute the
5039
     beginning of a GOT, by computing the end of its predecessor.  To
5040
     avoid special cases for the primary GOT, while still preserving
5041
     assertions that are valid for both single- and multi-got links,
5042
     we arrange for the main got struct to have the right number of
5043
     global entries, but set its local_gotno such that the initial
5044
     offset of the primary GOT is zero.  Remember that the primary GOT
5045
     will become the last item in the circular linked list, so it
5046
     points back to the master GOT.  */
5047
0
  gg->local_gotno = -g->global_gotno;
5048
0
  gg->global_gotno = g->global_gotno;
5049
0
  gg->tls_gotno = 0;
5050
0
  assign = 0;
5051
0
  gg->next = gg;
5052
5053
0
  do
5054
0
    {
5055
0
      struct mips_got_info *gn;
5056
5057
0
      assign += htab->reserved_gotno;
5058
0
      g->assigned_low_gotno = assign;
5059
0
      g->local_gotno += assign;
5060
0
      g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
5061
0
      g->assigned_high_gotno = g->local_gotno - 1;
5062
0
      assign = g->local_gotno + g->global_gotno + g->tls_gotno;
5063
5064
      /* Take g out of the direct list, and push it onto the reversed
5065
   list that gg points to.  g->next is guaranteed to be nonnull after
5066
   this operation, as required by mips_elf_initialize_tls_index. */
5067
0
      gn = g->next;
5068
0
      g->next = gg->next;
5069
0
      gg->next = g;
5070
5071
      /* Set up any TLS entries.  We always place the TLS entries after
5072
   all non-TLS entries.  */
5073
0
      g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
5074
0
      tga.g = g;
5075
0
      tga.value = MIPS_ELF_GOT_SIZE (abfd);
5076
0
      htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
5077
0
      if (!tga.g)
5078
0
  return false;
5079
0
      BFD_ASSERT (g->tls_assigned_gotno == assign);
5080
5081
      /* Move onto the next GOT.  It will be a secondary GOT if nonull.  */
5082
0
      g = gn;
5083
5084
      /* Forbid global symbols in every non-primary GOT from having
5085
   lazy-binding stubs.  */
5086
0
      if (g)
5087
0
  htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
5088
0
    }
5089
0
  while (g);
5090
5091
0
  got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
5092
5093
0
  needed_relocs = 0;
5094
0
  for (g = gg->next; g && g->next != gg; g = g->next)
5095
0
    {
5096
0
      unsigned int save_assign;
5097
5098
      /* Assign offsets to global GOT entries and count how many
5099
   relocations they need.  */
5100
0
      save_assign = g->assigned_low_gotno;
5101
0
      g->assigned_low_gotno = g->local_gotno;
5102
0
      tga.info = info;
5103
0
      tga.value = MIPS_ELF_GOT_SIZE (abfd);
5104
0
      tga.g = g;
5105
0
      htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
5106
0
      if (!tga.g)
5107
0
  return false;
5108
0
      BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
5109
0
      g->assigned_low_gotno = save_assign;
5110
5111
0
      if (bfd_link_pic (info))
5112
0
  {
5113
0
    g->relocs += g->local_gotno - g->assigned_low_gotno;
5114
0
    BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
5115
0
          + g->next->global_gotno
5116
0
          + g->next->tls_gotno
5117
0
          + htab->reserved_gotno);
5118
0
  }
5119
0
      needed_relocs += g->relocs;
5120
0
    }
5121
0
  needed_relocs += g->relocs;
5122
5123
0
  if (needed_relocs)
5124
0
    mips_elf_allocate_dynamic_relocations (dynobj, info,
5125
0
             needed_relocs);
5126
5127
0
  return true;
5128
0
}
5129
5130

5131
/* Returns the first relocation of type r_type found, beginning with
5132
   RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
5133
5134
static const Elf_Internal_Rela *
5135
mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
5136
        const Elf_Internal_Rela *relocation,
5137
        const Elf_Internal_Rela *relend)
5138
0
{
5139
0
  unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
5140
5141
0
  while (relocation < relend)
5142
0
    {
5143
0
      if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
5144
0
    && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
5145
0
  return relocation;
5146
5147
0
      ++relocation;
5148
0
    }
5149
5150
  /* We didn't find it.  */
5151
0
  return NULL;
5152
0
}
5153
5154
/* Return whether an input relocation is against a local symbol.  */
5155
5156
static bool
5157
mips_elf_local_relocation_p (bfd *input_bfd,
5158
           const Elf_Internal_Rela *relocation,
5159
           asection **local_sections)
5160
0
{
5161
0
  unsigned long r_symndx;
5162
0
  Elf_Internal_Shdr *symtab_hdr;
5163
0
  size_t extsymoff;
5164
5165
0
  r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5166
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5167
0
  extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5168
5169
0
  if (r_symndx < extsymoff)
5170
0
    return true;
5171
0
  if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
5172
0
    return true;
5173
5174
0
  return false;
5175
0
}
5176

5177
/* Sign-extend VALUE, which has the indicated number of BITS.  */
5178
5179
bfd_vma
5180
_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
5181
93
{
5182
93
  if (value & ((bfd_vma) 1 << (bits - 1)))
5183
    /* VALUE is negative.  */
5184
7
    value |= ((bfd_vma) - 1) << bits;
5185
5186
93
  return value;
5187
93
}
5188
5189
/* Return non-zero if the indicated VALUE has overflowed the maximum
5190
   range expressible by a signed number with the indicated number of
5191
   BITS.  */
5192
5193
static bool
5194
mips_elf_overflow_p (bfd_vma value, int bits)
5195
0
{
5196
0
  bfd_signed_vma svalue = (bfd_signed_vma) value;
5197
5198
0
  if (svalue > (1 << (bits - 1)) - 1)
5199
    /* The value is too big.  */
5200
0
    return true;
5201
0
  else if (svalue < -(1 << (bits - 1)))
5202
    /* The value is too small.  */
5203
0
    return true;
5204
5205
  /* All is well.  */
5206
0
  return false;
5207
0
}
5208
5209
/* Calculate the %high function.  */
5210
5211
static bfd_vma
5212
mips_elf_high (bfd_vma value)
5213
0
{
5214
0
  return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5215
0
}
5216
5217
/* Calculate the %higher function.  */
5218
5219
static bfd_vma
5220
mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
5221
0
{
5222
0
#ifdef BFD64
5223
0
  return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5224
#else
5225
  abort ();
5226
  return MINUS_ONE;
5227
#endif
5228
0
}
5229
5230
/* Calculate the %highest function.  */
5231
5232
static bfd_vma
5233
mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
5234
0
{
5235
0
#ifdef BFD64
5236
0
  return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
5237
#else
5238
  abort ();
5239
  return MINUS_ONE;
5240
#endif
5241
0
}
5242

5243
/* Create the .compact_rel section.  */
5244
5245
static bool
5246
mips_elf_create_compact_rel_section
5247
  (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
5248
0
{
5249
0
  flagword flags;
5250
0
  register asection *s;
5251
5252
0
  if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
5253
0
    {
5254
0
      flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5255
0
         | SEC_READONLY);
5256
5257
0
      s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
5258
0
      if (s == NULL
5259
0
    || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5260
0
  return false;
5261
5262
0
      s->size = sizeof (Elf32_External_compact_rel);
5263
0
    }
5264
5265
0
  return true;
5266
0
}
5267
5268
/* Create the .got section to hold the global offset table.  */
5269
5270
static bool
5271
mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5272
0
{
5273
0
  flagword flags;
5274
0
  register asection *s;
5275
0
  struct elf_link_hash_entry *h;
5276
0
  struct bfd_link_hash_entry *bh;
5277
0
  struct mips_elf_link_hash_table *htab;
5278
5279
0
  htab = mips_elf_hash_table (info);
5280
0
  BFD_ASSERT (htab != NULL);
5281
5282
  /* This function may be called more than once.  */
5283
0
  if (htab->root.sgot)
5284
0
    return true;
5285
5286
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5287
0
     | SEC_LINKER_CREATED);
5288
5289
  /* We have to use an alignment of 2**4 here because this is hardcoded
5290
     in the function stub generation and in the linker script.  */
5291
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5292
0
  if (s == NULL
5293
0
      || !bfd_set_section_alignment (s, 4))
5294
0
    return false;
5295
0
  htab->root.sgot = s;
5296
5297
  /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
5298
     linker script because we don't want to define the symbol if we
5299
     are not creating a global offset table.  */
5300
0
  bh = NULL;
5301
0
  if (! (_bfd_generic_link_add_one_symbol
5302
0
   (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5303
0
    0, NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
5304
0
    return false;
5305
5306
0
  h = (struct elf_link_hash_entry *) bh;
5307
0
  h->non_elf = 0;
5308
0
  h->def_regular = 1;
5309
0
  h->type = STT_OBJECT;
5310
0
  h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5311
0
  elf_hash_table (info)->hgot = h;
5312
5313
0
  if (bfd_link_pic (info)
5314
0
      && ! bfd_elf_link_record_dynamic_symbol (info, h))
5315
0
    return false;
5316
5317
0
  htab->got_info = mips_elf_create_got_info (abfd);
5318
0
  mips_elf_section_data (s)->elf.this_hdr.sh_flags
5319
0
    |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5320
5321
  /* We also need a .got.plt section when generating PLTs.  */
5322
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5323
0
            SEC_ALLOC | SEC_LOAD
5324
0
            | SEC_HAS_CONTENTS
5325
0
            | SEC_IN_MEMORY
5326
0
            | SEC_LINKER_CREATED);
5327
0
  if (s == NULL)
5328
0
    return false;
5329
0
  htab->root.sgotplt = s;
5330
5331
0
  return true;
5332
0
}
5333

5334
/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5335
   __GOTT_INDEX__ symbols.  These symbols are only special for
5336
   shared objects; they are not used in executables.  */
5337
5338
static bool
5339
is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5340
0
{
5341
0
  return (mips_elf_hash_table (info)->root.target_os == is_vxworks
5342
0
    && bfd_link_pic (info)
5343
0
    && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5344
0
        || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5345
0
}
5346
5347
/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5348
   require an la25 stub.  See also mips_elf_local_pic_function_p,
5349
   which determines whether the destination function ever requires a
5350
   stub.  */
5351
5352
static bool
5353
mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5354
             bool target_is_16_bit_code_p)
5355
0
{
5356
  /* We specifically ignore branches and jumps from EF_PIC objects,
5357
     where the onus is on the compiler or programmer to perform any
5358
     necessary initialization of $25.  Sometimes such initialization
5359
     is unnecessary; for example, -mno-shared functions do not use
5360
     the incoming value of $25, and may therefore be called directly.  */
5361
0
  if (PIC_OBJECT_P (input_bfd))
5362
0
    return false;
5363
5364
0
  switch (r_type)
5365
0
    {
5366
0
    case R_MIPS_26:
5367
0
    case R_MIPS_PC16:
5368
0
    case R_MIPS_PC21_S2:
5369
0
    case R_MIPS_PC26_S2:
5370
0
    case R_MICROMIPS_26_S1:
5371
0
    case R_MICROMIPS_PC7_S1:
5372
0
    case R_MICROMIPS_PC10_S1:
5373
0
    case R_MICROMIPS_PC16_S1:
5374
0
    case R_MICROMIPS_PC23_S2:
5375
0
      return true;
5376
5377
0
    case R_MIPS16_26:
5378
0
      return !target_is_16_bit_code_p;
5379
5380
0
    default:
5381
0
      return false;
5382
0
    }
5383
0
}
5384

5385
/* Obtain the field relocated by RELOCATION.  */
5386
5387
static bfd_vma
5388
mips_elf_obtain_contents (reloc_howto_type *howto,
5389
        const Elf_Internal_Rela *relocation,
5390
        bfd *input_bfd, bfd_byte *contents)
5391
0
{
5392
0
  bfd_vma x = 0;
5393
0
  bfd_byte *location = contents + relocation->r_offset;
5394
0
  unsigned int size = bfd_get_reloc_size (howto);
5395
5396
  /* Obtain the bytes.  */
5397
0
  if (size != 0)
5398
0
    x = bfd_get (8 * size, input_bfd, location);
5399
5400
0
  return x;
5401
0
}
5402
5403
/* Store the field relocated by RELOCATION.  */
5404
5405
static void
5406
mips_elf_store_contents (reloc_howto_type *howto,
5407
       const Elf_Internal_Rela *relocation,
5408
       bfd *input_bfd, bfd_byte *contents, bfd_vma x)
5409
0
{
5410
0
  bfd_byte *location = contents + relocation->r_offset;
5411
0
  unsigned int size = bfd_get_reloc_size (howto);
5412
5413
  /* Put the value into the output.  */
5414
0
  if (size != 0)
5415
0
    bfd_put (8 * size, input_bfd, x, location);
5416
0
}
5417
5418
/* Try to patch a load from GOT instruction in CONTENTS pointed to by
5419
   RELOCATION described by HOWTO, with a move of 0 to the load target
5420
   register, returning TRUE if that is successful and FALSE otherwise.
5421
   If DOIT is FALSE, then only determine it patching is possible and
5422
   return status without actually changing CONTENTS.
5423
*/
5424
5425
static bool
5426
mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
5427
         const Elf_Internal_Rela *relocation,
5428
         reloc_howto_type *howto, bool doit)
5429
0
{
5430
0
  int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5431
0
  bfd_byte *location = contents + relocation->r_offset;
5432
0
  bool nullified = true;
5433
0
  bfd_vma x;
5434
5435
0
  _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
5436
5437
  /* Obtain the current value.  */
5438
0
  x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5439
5440
  /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
5441
     while RY is at bits [18:16] of the combined 32-bit instruction word.  */
5442
0
  if (mips16_reloc_p (r_type)
5443
0
      && (((x >> 22) & 0x3ff) == 0x3d3        /* LW */
5444
0
    || ((x >> 22) & 0x3ff) == 0x3c7))      /* LD */
5445
0
    x = (0x3cdU << 22) | (x & (7 << 16)) << 3;     /* LI */
5446
0
  else if (micromips_reloc_p (r_type)
5447
0
     && ((x >> 26) & 0x37) == 0x37)     /* LW/LD */
5448
0
    x = (0xc << 26) | (x & (0x1f << 21));     /* ADDIU */
5449
0
  else if (((x >> 26) & 0x3f) == 0x23        /* LW */
5450
0
     || ((x >> 26) & 0x3f) == 0x37)     /* LD */
5451
0
    x = (0x9 << 26) | (x & (0x1f << 16));     /* ADDIU */
5452
0
  else
5453
0
    nullified = false;
5454
5455
  /* Put the value into the output.  */
5456
0
  if (doit && nullified)
5457
0
    mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
5458
5459
0
  _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, false, location);
5460
5461
0
  return nullified;
5462
0
}
5463
5464
/* Calculate the value produced by the RELOCATION (which comes from
5465
   the INPUT_BFD).  The ADDEND is the addend to use for this
5466
   RELOCATION; RELOCATION->R_ADDEND is ignored.
5467
5468
   The result of the relocation calculation is stored in VALUEP.
5469
   On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5470
   is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5471
5472
   This function returns bfd_reloc_continue if the caller need take no
5473
   further action regarding this relocation, bfd_reloc_notsupported if
5474
   something goes dramatically wrong, bfd_reloc_overflow if an
5475
   overflow occurs, and bfd_reloc_ok to indicate success.  */
5476
5477
static bfd_reloc_status_type
5478
mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5479
             asection *input_section, bfd_byte *contents,
5480
             struct bfd_link_info *info,
5481
             const Elf_Internal_Rela *relocation,
5482
             bfd_vma addend, reloc_howto_type *howto,
5483
             Elf_Internal_Sym *local_syms,
5484
             asection **local_sections, bfd_vma *valuep,
5485
             const char **namep,
5486
             bool *cross_mode_jump_p,
5487
             bool save_addend)
5488
0
{
5489
  /* The eventual value we will return.  */
5490
0
  bfd_vma value;
5491
  /* The address of the symbol against which the relocation is
5492
     occurring.  */
5493
0
  bfd_vma symbol = 0;
5494
  /* The final GP value to be used for the relocatable, executable, or
5495
     shared object file being produced.  */
5496
0
  bfd_vma gp;
5497
  /* The place (section offset or address) of the storage unit being
5498
     relocated.  */
5499
0
  bfd_vma p;
5500
  /* The value of GP used to create the relocatable object.  */
5501
0
  bfd_vma gp0;
5502
  /* The offset into the global offset table at which the address of
5503
     the relocation entry symbol, adjusted by the addend, resides
5504
     during execution.  */
5505
0
  bfd_vma g = MINUS_ONE;
5506
  /* The section in which the symbol referenced by the relocation is
5507
     located.  */
5508
0
  asection *sec = NULL;
5509
0
  struct mips_elf_link_hash_entry *h = NULL;
5510
  /* TRUE if the symbol referred to by this relocation is a local
5511
     symbol.  */
5512
0
  bool local_p, was_local_p;
5513
  /* TRUE if the symbol referred to by this relocation is a section
5514
     symbol.  */
5515
0
  bool section_p = false;
5516
  /* TRUE if the symbol referred to by this relocation is "_gp_disp".  */
5517
0
  bool gp_disp_p = false;
5518
  /* TRUE if the symbol referred to by this relocation is
5519
     "__gnu_local_gp".  */
5520
0
  bool gnu_local_gp_p = false;
5521
0
  Elf_Internal_Shdr *symtab_hdr;
5522
0
  size_t extsymoff;
5523
0
  unsigned long r_symndx;
5524
0
  int r_type;
5525
  /* TRUE if overflow occurred during the calculation of the
5526
     relocation value.  */
5527
0
  bool overflowed_p;
5528
  /* TRUE if this relocation refers to a MIPS16 function.  */
5529
0
  bool target_is_16_bit_code_p = false;
5530
0
  bool target_is_micromips_code_p = false;
5531
0
  struct mips_elf_link_hash_table *htab;
5532
0
  bfd *dynobj;
5533
0
  bool resolved_to_zero;
5534
5535
0
  dynobj = elf_hash_table (info)->dynobj;
5536
0
  htab = mips_elf_hash_table (info);
5537
0
  BFD_ASSERT (htab != NULL);
5538
5539
  /* Parse the relocation.  */
5540
0
  r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5541
0
  r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5542
0
  p = (input_section->output_section->vma
5543
0
       + input_section->output_offset
5544
0
       + relocation->r_offset);
5545
5546
  /* Assume that there will be no overflow.  */
5547
0
  overflowed_p = false;
5548
5549
  /* Figure out whether or not the symbol is local, and get the offset
5550
     used in the array of hash table entries.  */
5551
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5552
0
  local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5553
0
           local_sections);
5554
0
  was_local_p = local_p;
5555
0
  if (! elf_bad_symtab (input_bfd))
5556
0
    extsymoff = symtab_hdr->sh_info;
5557
0
  else
5558
0
    {
5559
      /* The symbol table does not follow the rule that local symbols
5560
   must come before globals.  */
5561
0
      extsymoff = 0;
5562
0
    }
5563
5564
  /* Figure out the value of the symbol.  */
5565
0
  if (local_p)
5566
0
    {
5567
0
      bool micromips_p = MICROMIPS_P (abfd);
5568
0
      Elf_Internal_Sym *sym;
5569
5570
0
      sym = local_syms + r_symndx;
5571
0
      sec = local_sections[r_symndx];
5572
5573
0
      section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
5574
5575
0
      symbol = sec->output_section->vma + sec->output_offset;
5576
0
      if (!section_p || (sec->flags & SEC_MERGE))
5577
0
  symbol += sym->st_value;
5578
0
      if ((sec->flags & SEC_MERGE) && section_p)
5579
0
  {
5580
0
    addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5581
0
    addend -= symbol;
5582
0
    addend += sec->output_section->vma + sec->output_offset;
5583
0
  }
5584
5585
      /* MIPS16/microMIPS text labels should be treated as odd.  */
5586
0
      if (ELF_ST_IS_COMPRESSED (sym->st_other))
5587
0
  ++symbol;
5588
5589
      /* Record the name of this symbol, for our caller.  */
5590
0
      *namep = bfd_elf_string_from_elf_section (input_bfd,
5591
0
            symtab_hdr->sh_link,
5592
0
            sym->st_name);
5593
0
      if (*namep == NULL || **namep == '\0')
5594
0
  *namep = bfd_section_name (sec);
5595
5596
      /* For relocations against a section symbol and ones against no
5597
   symbol (absolute relocations) infer the ISA mode from the addend.  */
5598
0
      if (section_p || r_symndx == STN_UNDEF)
5599
0
  {
5600
0
    target_is_16_bit_code_p = (addend & 1) && !micromips_p;
5601
0
    target_is_micromips_code_p = (addend & 1) && micromips_p;
5602
0
  }
5603
      /* For relocations against an absolute symbol infer the ISA mode
5604
   from the value of the symbol plus addend.  */
5605
0
      else if (bfd_is_abs_section (sec))
5606
0
  {
5607
0
    target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
5608
0
    target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
5609
0
  }
5610
      /* Otherwise just use the regular symbol annotation available.  */
5611
0
      else
5612
0
  {
5613
0
    target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5614
0
    target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5615
0
  }
5616
0
    }
5617
0
  else
5618
0
    {
5619
      /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ?  */
5620
5621
      /* For global symbols we look up the symbol in the hash-table.  */
5622
0
      h = ((struct mips_elf_link_hash_entry *)
5623
0
     elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5624
      /* Find the real hash-table entry for this symbol.  */
5625
0
      while (h->root.root.type == bfd_link_hash_indirect
5626
0
       || h->root.root.type == bfd_link_hash_warning)
5627
0
  h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5628
5629
      /* Record the name of this symbol, for our caller.  */
5630
0
      *namep = h->root.root.root.string;
5631
5632
      /* See if this is the special _gp_disp symbol.  Note that such a
5633
   symbol must always be a global symbol.  */
5634
0
      if (strcmp (*namep, "_gp_disp") == 0
5635
0
    && ! NEWABI_P (input_bfd))
5636
0
  {
5637
    /* Relocations against _gp_disp are permitted only with
5638
       R_MIPS_HI16 and R_MIPS_LO16 relocations.  */
5639
0
    if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5640
0
      return bfd_reloc_notsupported;
5641
5642
0
    gp_disp_p = true;
5643
0
  }
5644
      /* See if this is the special _gp symbol.  Note that such a
5645
   symbol must always be a global symbol.  */
5646
0
      else if (strcmp (*namep, "__gnu_local_gp") == 0)
5647
0
  gnu_local_gp_p = true;
5648
5649
5650
      /* If this symbol is defined, calculate its address.  Note that
5651
   _gp_disp is a magic symbol, always implicitly defined by the
5652
   linker, so it's inappropriate to check to see whether or not
5653
   its defined.  */
5654
0
      else if ((h->root.root.type == bfd_link_hash_defined
5655
0
    || h->root.root.type == bfd_link_hash_defweak)
5656
0
         && h->root.root.u.def.section)
5657
0
  {
5658
0
    sec = h->root.root.u.def.section;
5659
0
    if (sec->output_section)
5660
0
      symbol = (h->root.root.u.def.value
5661
0
          + sec->output_section->vma
5662
0
          + sec->output_offset);
5663
0
    else
5664
0
      symbol = h->root.root.u.def.value;
5665
0
  }
5666
0
      else if (h->root.root.type == bfd_link_hash_undefweak)
5667
  /* We allow relocations against undefined weak symbols, giving
5668
     it the value zero, so that you can undefined weak functions
5669
     and check to see if they exist by looking at their
5670
     addresses.  */
5671
0
  symbol = 0;
5672
0
      else if (info->unresolved_syms_in_objects == RM_IGNORE
5673
0
         && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5674
0
  symbol = 0;
5675
0
      else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5676
0
           ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5677
0
  {
5678
    /* If this is a dynamic link, we should have created a
5679
       _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5680
       in _bfd_mips_elf_create_dynamic_sections.
5681
       Otherwise, we should define the symbol with a value of 0.
5682
       FIXME: It should probably get into the symbol table
5683
       somehow as well.  */
5684
0
    BFD_ASSERT (! bfd_link_pic (info));
5685
0
    BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5686
0
    symbol = 0;
5687
0
  }
5688
0
      else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5689
0
  {
5690
    /* This is an optional symbol - an Irix specific extension to the
5691
       ELF spec.  Ignore it for now.
5692
       XXX - FIXME - there is more to the spec for OPTIONAL symbols
5693
       than simply ignoring them, but we do not handle this for now.
5694
       For information see the "64-bit ELF Object File Specification"
5695
       which is available from here:
5696
       http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf  */
5697
0
    symbol = 0;
5698
0
  }
5699
0
      else
5700
0
  {
5701
0
          bool reject_undefined
5702
0
      = ((info->unresolved_syms_in_objects == RM_DIAGNOSE
5703
0
    && !info->warn_unresolved_syms)
5704
0
         || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT);
5705
5706
0
    info->callbacks->undefined_symbol
5707
0
      (info, h->root.root.root.string, input_bfd,
5708
0
       input_section, relocation->r_offset, reject_undefined);
5709
5710
0
    if (reject_undefined)
5711
0
      return bfd_reloc_undefined;
5712
5713
0
    symbol = 0;
5714
0
  }
5715
5716
0
      target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5717
0
      target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5718
0
    }
5719
5720
  /* If this is a reference to a 16-bit function with a stub, we need
5721
     to redirect the relocation to the stub unless:
5722
5723
     (a) the relocation is for a MIPS16 JAL;
5724
5725
     (b) the relocation is for a MIPS16 PIC call, and there are no
5726
   non-MIPS16 uses of the GOT slot; or
5727
5728
     (c) the section allows direct references to MIPS16 functions.  */
5729
0
  if (r_type != R_MIPS16_26
5730
0
      && !bfd_link_relocatable (info)
5731
0
      && ((h != NULL
5732
0
     && h->fn_stub != NULL
5733
0
     && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5734
0
    || (local_p
5735
0
        && mips_elf_tdata (input_bfd)->local_stubs != NULL
5736
0
        && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5737
0
      && !section_allows_mips16_refs_p (input_section))
5738
0
    {
5739
      /* This is a 32- or 64-bit call to a 16-bit function.  We should
5740
   have already noticed that we were going to need the
5741
   stub.  */
5742
0
      if (local_p)
5743
0
  {
5744
0
    sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5745
0
    value = 0;
5746
0
  }
5747
0
      else
5748
0
  {
5749
0
    BFD_ASSERT (h->need_fn_stub);
5750
0
    if (h->la25_stub)
5751
0
      {
5752
        /* If a LA25 header for the stub itself exists, point to the
5753
     prepended LUI/ADDIU sequence.  */
5754
0
        sec = h->la25_stub->stub_section;
5755
0
        value = h->la25_stub->offset;
5756
0
      }
5757
0
    else
5758
0
      {
5759
0
        sec = h->fn_stub;
5760
0
        value = 0;
5761
0
      }
5762
0
  }
5763
5764
0
      symbol = sec->output_section->vma + sec->output_offset + value;
5765
      /* The target is 16-bit, but the stub isn't.  */
5766
0
      target_is_16_bit_code_p = false;
5767
0
    }
5768
  /* If this is a MIPS16 call with a stub, that is made through the PLT or
5769
     to a standard MIPS function, we need to redirect the call to the stub.
5770
     Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5771
     indirect calls should use an indirect stub instead.  */
5772
0
  else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
5773
0
     && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5774
0
         || (local_p
5775
0
       && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5776
0
       && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5777
0
     && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5778
0
    {
5779
0
      if (local_p)
5780
0
  sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5781
0
      else
5782
0
  {
5783
    /* If both call_stub and call_fp_stub are defined, we can figure
5784
       out which one to use by checking which one appears in the input
5785
       file.  */
5786
0
    if (h->call_stub != NULL && h->call_fp_stub != NULL)
5787
0
      {
5788
0
        asection *o;
5789
5790
0
        sec = NULL;
5791
0
        for (o = input_bfd->sections; o != NULL; o = o->next)
5792
0
    {
5793
0
      if (CALL_FP_STUB_P (bfd_section_name (o)))
5794
0
        {
5795
0
          sec = h->call_fp_stub;
5796
0
          break;
5797
0
        }
5798
0
    }
5799
0
        if (sec == NULL)
5800
0
    sec = h->call_stub;
5801
0
      }
5802
0
    else if (h->call_stub != NULL)
5803
0
      sec = h->call_stub;
5804
0
    else
5805
0
      sec = h->call_fp_stub;
5806
0
  }
5807
5808
0
      BFD_ASSERT (sec->size > 0);
5809
0
      symbol = sec->output_section->vma + sec->output_offset;
5810
0
    }
5811
  /* If this is a direct call to a PIC function, redirect to the
5812
     non-PIC stub.  */
5813
0
  else if (h != NULL && h->la25_stub
5814
0
     && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5815
0
               target_is_16_bit_code_p))
5816
0
    {
5817
0
  symbol = (h->la25_stub->stub_section->output_section->vma
5818
0
      + h->la25_stub->stub_section->output_offset
5819
0
      + h->la25_stub->offset);
5820
0
  if (ELF_ST_IS_MICROMIPS (h->root.other))
5821
0
    symbol |= 1;
5822
0
    }
5823
  /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5824
     entry is used if a standard PLT entry has also been made.  In this
5825
     case the symbol will have been set by mips_elf_set_plt_sym_value
5826
     to point to the standard PLT entry, so redirect to the compressed
5827
     one.  */
5828
0
  else if ((mips16_branch_reloc_p (r_type)
5829
0
      || micromips_branch_reloc_p (r_type))
5830
0
     && !bfd_link_relocatable (info)
5831
0
     && h != NULL
5832
0
     && h->use_plt_entry
5833
0
     && h->root.plt.plist->comp_offset != MINUS_ONE
5834
0
     && h->root.plt.plist->mips_offset != MINUS_ONE)
5835
0
    {
5836
0
      bool micromips_p = MICROMIPS_P (abfd);
5837
5838
0
      sec = htab->root.splt;
5839
0
      symbol = (sec->output_section->vma
5840
0
    + sec->output_offset
5841
0
    + htab->plt_header_size
5842
0
    + htab->plt_mips_offset
5843
0
    + h->root.plt.plist->comp_offset
5844
0
    + 1);
5845
5846
0
      target_is_16_bit_code_p = !micromips_p;
5847
0
      target_is_micromips_code_p = micromips_p;
5848
0
    }
5849
5850
  /* Make sure MIPS16 and microMIPS are not used together.  */
5851
0
  if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
5852
0
      || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5853
0
   {
5854
0
      _bfd_error_handler
5855
0
  (_("MIPS16 and microMIPS functions cannot call each other"));
5856
0
      return bfd_reloc_notsupported;
5857
0
   }
5858
5859
  /* Calls from 16-bit code to 32-bit code and vice versa require the
5860
     mode change.  However, we can ignore calls to undefined weak symbols,
5861
     which should never be executed at runtime.  This exception is important
5862
     because the assembly writer may have "known" that any definition of the
5863
     symbol would be 16-bit code, and that direct jumps were therefore
5864
     acceptable.  */
5865
0
  *cross_mode_jump_p = (!bfd_link_relocatable (info)
5866
0
      && !(h && h->root.root.type == bfd_link_hash_undefweak)
5867
0
      && ((mips16_branch_reloc_p (r_type)
5868
0
           && !target_is_16_bit_code_p)
5869
0
          || (micromips_branch_reloc_p (r_type)
5870
0
        && !target_is_micromips_code_p)
5871
0
          || ((branch_reloc_p (r_type)
5872
0
         || r_type == R_MIPS_JALR)
5873
0
        && (target_is_16_bit_code_p
5874
0
            || target_is_micromips_code_p))));
5875
5876
0
  resolved_to_zero = (h != NULL
5877
0
          && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
5878
5879
0
  switch (r_type)
5880
0
    {
5881
0
    case R_MIPS16_CALL16:
5882
0
    case R_MIPS16_GOT16:
5883
0
    case R_MIPS_CALL16:
5884
0
    case R_MIPS_GOT16:
5885
0
    case R_MIPS_GOT_PAGE:
5886
0
    case R_MIPS_GOT_DISP:
5887
0
    case R_MIPS_GOT_LO16:
5888
0
    case R_MIPS_CALL_LO16:
5889
0
    case R_MICROMIPS_CALL16:
5890
0
    case R_MICROMIPS_GOT16:
5891
0
    case R_MICROMIPS_GOT_PAGE:
5892
0
    case R_MICROMIPS_GOT_DISP:
5893
0
    case R_MICROMIPS_GOT_LO16:
5894
0
    case R_MICROMIPS_CALL_LO16:
5895
0
      if (resolved_to_zero
5896
0
    && !bfd_link_relocatable (info)
5897
0
    && bfd_reloc_offset_in_range (howto, input_bfd, input_section,
5898
0
          relocation->r_offset)
5899
0
    && mips_elf_nullify_got_load (input_bfd, contents,
5900
0
          relocation, howto, true))
5901
0
  return bfd_reloc_continue;
5902
5903
      /* Fall through.  */
5904
0
    case R_MIPS_GOT_HI16:
5905
0
    case R_MIPS_CALL_HI16:
5906
0
    case R_MICROMIPS_GOT_HI16:
5907
0
    case R_MICROMIPS_CALL_HI16:
5908
0
      if (resolved_to_zero
5909
0
    && htab->use_absolute_zero
5910
0
    && bfd_link_pic (info))
5911
0
  {
5912
    /* Redirect to the special `__gnu_absolute_zero' symbol.  */
5913
0
    h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
5914
0
           false, false, false);
5915
0
    BFD_ASSERT (h != NULL);
5916
0
  }
5917
0
      break;
5918
0
    }
5919
5920
0
  local_p = (h == NULL || mips_use_local_got_p (info, h));
5921
5922
0
  gp0 = _bfd_get_gp_value (input_bfd);
5923
0
  gp = _bfd_get_gp_value (abfd);
5924
0
  if (htab->got_info)
5925
0
    gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5926
5927
0
  if (gnu_local_gp_p)
5928
0
    symbol = gp;
5929
5930
  /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5931
     to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP.  The addend is applied by the
5932
     corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.  */
5933
0
  if (got_page_reloc_p (r_type) && !local_p)
5934
0
    {
5935
0
      r_type = (micromips_reloc_p (r_type)
5936
0
    ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5937
0
      addend = 0;
5938
0
    }
5939
5940
  /* If we haven't already determined the GOT offset, and we're going
5941
     to need it, get it now.  */
5942
0
  switch (r_type)
5943
0
    {
5944
0
    case R_MIPS16_CALL16:
5945
0
    case R_MIPS16_GOT16:
5946
0
    case R_MIPS_CALL16:
5947
0
    case R_MIPS_GOT16:
5948
0
    case R_MIPS_GOT_DISP:
5949
0
    case R_MIPS_GOT_HI16:
5950
0
    case R_MIPS_CALL_HI16:
5951
0
    case R_MIPS_GOT_LO16:
5952
0
    case R_MIPS_CALL_LO16:
5953
0
    case R_MICROMIPS_CALL16:
5954
0
    case R_MICROMIPS_GOT16:
5955
0
    case R_MICROMIPS_GOT_DISP:
5956
0
    case R_MICROMIPS_GOT_HI16:
5957
0
    case R_MICROMIPS_CALL_HI16:
5958
0
    case R_MICROMIPS_GOT_LO16:
5959
0
    case R_MICROMIPS_CALL_LO16:
5960
0
    case R_MIPS_TLS_GD:
5961
0
    case R_MIPS_TLS_GOTTPREL:
5962
0
    case R_MIPS_TLS_LDM:
5963
0
    case R_MIPS16_TLS_GD:
5964
0
    case R_MIPS16_TLS_GOTTPREL:
5965
0
    case R_MIPS16_TLS_LDM:
5966
0
    case R_MICROMIPS_TLS_GD:
5967
0
    case R_MICROMIPS_TLS_GOTTPREL:
5968
0
    case R_MICROMIPS_TLS_LDM:
5969
      /* Find the index into the GOT where this value is located.  */
5970
0
      if (tls_ldm_reloc_p (r_type))
5971
0
  {
5972
0
    g = mips_elf_local_got_index (abfd, input_bfd, info,
5973
0
          0, 0, NULL, r_type);
5974
0
    if (g == MINUS_ONE)
5975
0
      return bfd_reloc_outofrange;
5976
0
  }
5977
0
      else if (!local_p)
5978
0
  {
5979
    /* On VxWorks, CALL relocations should refer to the .got.plt
5980
       entry, which is initialized to point at the PLT stub.  */
5981
0
    if (htab->root.target_os == is_vxworks
5982
0
        && (call_hi16_reloc_p (r_type)
5983
0
      || call_lo16_reloc_p (r_type)
5984
0
      || call16_reloc_p (r_type)))
5985
0
      {
5986
0
        BFD_ASSERT (addend == 0);
5987
0
        BFD_ASSERT (h->root.needs_plt);
5988
0
        g = mips_elf_gotplt_index (info, &h->root);
5989
0
      }
5990
0
    else
5991
0
      {
5992
0
        BFD_ASSERT (addend == 0);
5993
0
        g = mips_elf_global_got_index (abfd, info, input_bfd,
5994
0
               &h->root, r_type);
5995
0
        if (!TLS_RELOC_P (r_type)
5996
0
      && !elf_hash_table (info)->dynamic_sections_created)
5997
    /* This is a static link.  We must initialize the GOT entry.  */
5998
0
    MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
5999
0
      }
6000
0
  }
6001
0
      else if (htab->root.target_os != is_vxworks
6002
0
         && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
6003
  /* The calculation below does not involve "g".  */
6004
0
  break;
6005
0
      else
6006
0
  {
6007
0
    g = mips_elf_local_got_index (abfd, input_bfd, info,
6008
0
          symbol + addend, r_symndx, h, r_type);
6009
0
    if (g == MINUS_ONE)
6010
0
      return bfd_reloc_outofrange;
6011
0
  }
6012
6013
      /* Convert GOT indices to actual offsets.  */
6014
0
      g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
6015
0
      break;
6016
0
    }
6017
6018
  /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
6019
     symbols are resolved by the loader.  Add them to .rela.dyn.  */
6020
0
  if (h != NULL && is_gott_symbol (info, &h->root))
6021
0
    {
6022
0
      Elf_Internal_Rela outrel;
6023
0
      bfd_byte *loc;
6024
0
      asection *s;
6025
6026
0
      s = mips_elf_rel_dyn_section (info, false);
6027
0
      loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6028
6029
0
      outrel.r_offset = (input_section->output_section->vma
6030
0
       + input_section->output_offset
6031
0
       + relocation->r_offset);
6032
0
      outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
6033
0
      outrel.r_addend = addend;
6034
0
      bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
6035
6036
      /* If we've written this relocation for a readonly section,
6037
   we need to set DF_TEXTREL again, so that we do not delete the
6038
   DT_TEXTREL tag.  */
6039
0
      if (MIPS_ELF_READONLY_SECTION (input_section))
6040
0
  info->flags |= DF_TEXTREL;
6041
6042
0
      *valuep = 0;
6043
0
      return bfd_reloc_ok;
6044
0
    }
6045
6046
  /* Figure out what kind of relocation is being performed.  */
6047
0
  switch (r_type)
6048
0
    {
6049
0
    case R_MIPS_NONE:
6050
0
      return bfd_reloc_continue;
6051
6052
0
    case R_MIPS_16:
6053
0
      if (howto->partial_inplace)
6054
0
  addend = _bfd_mips_elf_sign_extend (addend, 16);
6055
0
      value = symbol + addend;
6056
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6057
0
      break;
6058
6059
0
    case R_MIPS_32:
6060
0
    case R_MIPS_REL32:
6061
0
    case R_MIPS_64:
6062
0
      if ((bfd_link_pic (info)
6063
0
     || (htab->root.dynamic_sections_created
6064
0
         && h != NULL
6065
0
         && h->root.def_dynamic
6066
0
         && !h->root.def_regular
6067
0
         && !h->has_static_relocs))
6068
0
    && r_symndx != STN_UNDEF
6069
0
    && (h == NULL
6070
0
        || h->root.root.type != bfd_link_hash_undefweak
6071
0
        || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
6072
0
      && !resolved_to_zero))
6073
0
    && (input_section->flags & SEC_ALLOC) != 0)
6074
0
  {
6075
    /* If we're creating a shared library, then we can't know
6076
       where the symbol will end up.  So, we create a relocation
6077
       record in the output, and leave the job up to the dynamic
6078
       linker.  We must do the same for executable references to
6079
       shared library symbols, unless we've decided to use copy
6080
       relocs or PLTs instead.  */
6081
0
    value = addend;
6082
0
    if (!mips_elf_create_dynamic_relocation (abfd,
6083
0
               info,
6084
0
               relocation,
6085
0
               h,
6086
0
               sec,
6087
0
               symbol,
6088
0
               &value,
6089
0
               input_section))
6090
0
      return bfd_reloc_undefined;
6091
0
  }
6092
0
      else
6093
0
  {
6094
0
    if (r_type != R_MIPS_REL32)
6095
0
      value = symbol + addend;
6096
0
    else
6097
0
      value = addend;
6098
0
  }
6099
0
      value &= howto->dst_mask;
6100
0
      break;
6101
6102
0
    case R_MIPS_PC32:
6103
0
      value = symbol + addend - p;
6104
0
      value &= howto->dst_mask;
6105
0
      break;
6106
6107
0
    case R_MIPS16_26:
6108
      /* The calculation for R_MIPS16_26 is just the same as for an
6109
   R_MIPS_26.  It's only the storage of the relocated field into
6110
   the output file that's different.  That's handled in
6111
   mips_elf_perform_relocation.  So, we just fall through to the
6112
   R_MIPS_26 case here.  */
6113
0
    case R_MIPS_26:
6114
0
    case R_MICROMIPS_26_S1:
6115
0
      {
6116
0
  unsigned int shift;
6117
6118
  /* Shift is 2, unusually, for microMIPS JALX.  */
6119
0
  shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
6120
6121
0
  if (howto->partial_inplace && !section_p)
6122
0
    value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
6123
0
  else
6124
0
    value = addend;
6125
0
  value += symbol;
6126
6127
  /* Make sure the target of a jump is suitably aligned.  Bit 0 must
6128
     be the correct ISA mode selector except for weak undefined
6129
     symbols.  */
6130
0
  if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6131
0
      && (*cross_mode_jump_p
6132
0
    ? (value & 3) != (r_type == R_MIPS_26)
6133
0
    : (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
6134
0
    return bfd_reloc_outofrange;
6135
6136
0
  value >>= shift;
6137
0
  if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6138
0
    overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
6139
0
  value &= howto->dst_mask;
6140
0
      }
6141
0
      break;
6142
6143
0
    case R_MIPS_TLS_DTPREL_HI16:
6144
0
    case R_MIPS16_TLS_DTPREL_HI16:
6145
0
    case R_MICROMIPS_TLS_DTPREL_HI16:
6146
0
      value = (mips_elf_high (addend + symbol - dtprel_base (info))
6147
0
         & howto->dst_mask);
6148
0
      break;
6149
6150
0
    case R_MIPS_TLS_DTPREL_LO16:
6151
0
    case R_MIPS_TLS_DTPREL32:
6152
0
    case R_MIPS_TLS_DTPREL64:
6153
0
    case R_MIPS16_TLS_DTPREL_LO16:
6154
0
    case R_MICROMIPS_TLS_DTPREL_LO16:
6155
0
      value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
6156
0
      break;
6157
6158
0
    case R_MIPS_TLS_TPREL_HI16:
6159
0
    case R_MIPS16_TLS_TPREL_HI16:
6160
0
    case R_MICROMIPS_TLS_TPREL_HI16:
6161
0
      value = (mips_elf_high (addend + symbol - tprel_base (info))
6162
0
         & howto->dst_mask);
6163
0
      break;
6164
6165
0
    case R_MIPS_TLS_TPREL_LO16:
6166
0
    case R_MIPS_TLS_TPREL32:
6167
0
    case R_MIPS_TLS_TPREL64:
6168
0
    case R_MIPS16_TLS_TPREL_LO16:
6169
0
    case R_MICROMIPS_TLS_TPREL_LO16:
6170
0
      value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
6171
0
      break;
6172
6173
0
    case R_MIPS_HI16:
6174
0
    case R_MIPS16_HI16:
6175
0
    case R_MICROMIPS_HI16:
6176
0
      if (!gp_disp_p)
6177
0
  {
6178
0
    value = mips_elf_high (addend + symbol);
6179
0
    value &= howto->dst_mask;
6180
0
  }
6181
0
      else
6182
0
  {
6183
    /* For MIPS16 ABI code we generate this sequence
6184
    0: li      $v0,%hi(_gp_disp)
6185
    4: addiupc $v1,%lo(_gp_disp)
6186
    8: sll     $v0,16
6187
         12: addu    $v0,$v1
6188
         14: move    $gp,$v0
6189
       So the offsets of hi and lo relocs are the same, but the
6190
       base $pc is that used by the ADDIUPC instruction at $t9 + 4.
6191
       ADDIUPC clears the low two bits of the instruction address,
6192
       so the base is ($t9 + 4) & ~3.  */
6193
0
    if (r_type == R_MIPS16_HI16)
6194
0
      value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
6195
    /* The microMIPS .cpload sequence uses the same assembly
6196
       instructions as the traditional psABI version, but the
6197
       incoming $t9 has the low bit set.  */
6198
0
    else if (r_type == R_MICROMIPS_HI16)
6199
0
      value = mips_elf_high (addend + gp - p - 1);
6200
0
    else
6201
0
      value = mips_elf_high (addend + gp - p);
6202
0
  }
6203
0
      break;
6204
6205
0
    case R_MIPS_LO16:
6206
0
    case R_MIPS16_LO16:
6207
0
    case R_MICROMIPS_LO16:
6208
0
    case R_MICROMIPS_HI0_LO16:
6209
0
      if (!gp_disp_p)
6210
0
  value = (symbol + addend) & howto->dst_mask;
6211
0
      else
6212
0
  {
6213
    /* See the comment for R_MIPS16_HI16 above for the reason
6214
       for this conditional.  */
6215
0
    if (r_type == R_MIPS16_LO16)
6216
0
      value = addend + gp - (p & ~(bfd_vma) 0x3);
6217
0
    else if (r_type == R_MICROMIPS_LO16
6218
0
       || r_type == R_MICROMIPS_HI0_LO16)
6219
0
      value = addend + gp - p + 3;
6220
0
    else
6221
0
      value = addend + gp - p + 4;
6222
    /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6223
       for overflow.  But, on, say, IRIX5, relocations against
6224
       _gp_disp are normally generated from the .cpload
6225
       pseudo-op.  It generates code that normally looks like
6226
       this:
6227
6228
         lui    $gp,%hi(_gp_disp)
6229
         addiu  $gp,$gp,%lo(_gp_disp)
6230
         addu   $gp,$gp,$t9
6231
6232
       Here $t9 holds the address of the function being called,
6233
       as required by the MIPS ELF ABI.  The R_MIPS_LO16
6234
       relocation can easily overflow in this situation, but the
6235
       R_MIPS_HI16 relocation will handle the overflow.
6236
       Therefore, we consider this a bug in the MIPS ABI, and do
6237
       not check for overflow here.  */
6238
0
  }
6239
0
      break;
6240
6241
0
    case R_MIPS_LITERAL:
6242
0
    case R_MICROMIPS_LITERAL:
6243
      /* Because we don't merge literal sections, we can handle this
6244
   just like R_MIPS_GPREL16.  In the long run, we should merge
6245
   shared literals, and then we will need to additional work
6246
   here.  */
6247
6248
      /* Fall through.  */
6249
6250
0
    case R_MIPS16_GPREL:
6251
      /* The R_MIPS16_GPREL performs the same calculation as
6252
   R_MIPS_GPREL16, but stores the relocated bits in a different
6253
   order.  We don't need to do anything special here; the
6254
   differences are handled in mips_elf_perform_relocation.  */
6255
0
    case R_MIPS_GPREL16:
6256
0
    case R_MICROMIPS_GPREL7_S2:
6257
0
    case R_MICROMIPS_GPREL16:
6258
      /* Only sign-extend the addend if it was extracted from the
6259
   instruction.  If the addend was separate, leave it alone,
6260
   otherwise we may lose significant bits.  */
6261
0
      if (howto->partial_inplace)
6262
0
  addend = _bfd_mips_elf_sign_extend (addend, 16);
6263
0
      value = symbol + addend - gp;
6264
      /* If the symbol was local, any earlier relocatable links will
6265
   have adjusted its addend with the gp offset, so compensate
6266
   for that now.  Don't do it for symbols forced local in this
6267
   link, though, since they won't have had the gp offset applied
6268
   to them before.  */
6269
0
      if (was_local_p)
6270
0
  value += gp0;
6271
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6272
0
  overflowed_p = mips_elf_overflow_p (value, 16);
6273
0
      break;
6274
6275
0
    case R_MIPS16_GOT16:
6276
0
    case R_MIPS16_CALL16:
6277
0
    case R_MIPS_GOT16:
6278
0
    case R_MIPS_CALL16:
6279
0
    case R_MICROMIPS_GOT16:
6280
0
    case R_MICROMIPS_CALL16:
6281
      /* VxWorks does not have separate local and global semantics for
6282
   R_MIPS*_GOT16; every relocation evaluates to "G".  */
6283
0
      if (htab->root.target_os != is_vxworks && local_p)
6284
0
  {
6285
0
    value = mips_elf_got16_entry (abfd, input_bfd, info,
6286
0
          symbol + addend, !was_local_p);
6287
0
    if (value == MINUS_ONE)
6288
0
      return bfd_reloc_outofrange;
6289
0
    value
6290
0
      = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6291
0
    overflowed_p = mips_elf_overflow_p (value, 16);
6292
0
    break;
6293
0
  }
6294
6295
      /* Fall through.  */
6296
6297
0
    case R_MIPS_TLS_GD:
6298
0
    case R_MIPS_TLS_GOTTPREL:
6299
0
    case R_MIPS_TLS_LDM:
6300
0
    case R_MIPS_GOT_DISP:
6301
0
    case R_MIPS16_TLS_GD:
6302
0
    case R_MIPS16_TLS_GOTTPREL:
6303
0
    case R_MIPS16_TLS_LDM:
6304
0
    case R_MICROMIPS_TLS_GD:
6305
0
    case R_MICROMIPS_TLS_GOTTPREL:
6306
0
    case R_MICROMIPS_TLS_LDM:
6307
0
    case R_MICROMIPS_GOT_DISP:
6308
0
      value = g;
6309
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6310
0
      break;
6311
6312
0
    case R_MIPS_GPREL32:
6313
0
      value = (addend + symbol + gp0 - gp);
6314
0
      if (!save_addend)
6315
0
  value &= howto->dst_mask;
6316
0
      break;
6317
6318
0
    case R_MIPS_PC16:
6319
0
    case R_MIPS_GNU_REL16_S2:
6320
0
      if (howto->partial_inplace)
6321
0
  addend = _bfd_mips_elf_sign_extend (addend, 18);
6322
6323
      /* No need to exclude weak undefined symbols here as they resolve
6324
   to 0 and never set `*cross_mode_jump_p', so this alignment check
6325
   will never trigger for them.  */
6326
0
      if (*cross_mode_jump_p
6327
0
    ? ((symbol + addend) & 3) != 1
6328
0
    : ((symbol + addend) & 3) != 0)
6329
0
  return bfd_reloc_outofrange;
6330
6331
0
      value = symbol + addend - p;
6332
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6333
0
  overflowed_p = mips_elf_overflow_p (value, 18);
6334
0
      value >>= howto->rightshift;
6335
0
      value &= howto->dst_mask;
6336
0
      break;
6337
6338
0
    case R_MIPS16_PC16_S1:
6339
0
      if (howto->partial_inplace)
6340
0
  addend = _bfd_mips_elf_sign_extend (addend, 17);
6341
6342
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6343
0
    && (*cross_mode_jump_p
6344
0
        ? ((symbol + addend) & 3) != 0
6345
0
        : ((symbol + addend) & 1) == 0))
6346
0
  return bfd_reloc_outofrange;
6347
6348
0
      value = symbol + addend - p;
6349
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6350
0
  overflowed_p = mips_elf_overflow_p (value, 17);
6351
0
      value >>= howto->rightshift;
6352
0
      value &= howto->dst_mask;
6353
0
      break;
6354
6355
0
    case R_MIPS_PC21_S2:
6356
0
      if (howto->partial_inplace)
6357
0
  addend = _bfd_mips_elf_sign_extend (addend, 23);
6358
6359
0
      if ((symbol + addend) & 3)
6360
0
  return bfd_reloc_outofrange;
6361
6362
0
      value = symbol + addend - p;
6363
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6364
0
  overflowed_p = mips_elf_overflow_p (value, 23);
6365
0
      value >>= howto->rightshift;
6366
0
      value &= howto->dst_mask;
6367
0
      break;
6368
6369
0
    case R_MIPS_PC26_S2:
6370
0
      if (howto->partial_inplace)
6371
0
  addend = _bfd_mips_elf_sign_extend (addend, 28);
6372
6373
0
      if ((symbol + addend) & 3)
6374
0
  return bfd_reloc_outofrange;
6375
6376
0
      value = symbol + addend - p;
6377
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6378
0
  overflowed_p = mips_elf_overflow_p (value, 28);
6379
0
      value >>= howto->rightshift;
6380
0
      value &= howto->dst_mask;
6381
0
      break;
6382
6383
0
    case R_MIPS_PC18_S3:
6384
0
      if (howto->partial_inplace)
6385
0
  addend = _bfd_mips_elf_sign_extend (addend, 21);
6386
6387
0
      if ((symbol + addend) & 7)
6388
0
  return bfd_reloc_outofrange;
6389
6390
0
      value = symbol + addend - ((p | 7) ^ 7);
6391
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6392
0
  overflowed_p = mips_elf_overflow_p (value, 21);
6393
0
      value >>= howto->rightshift;
6394
0
      value &= howto->dst_mask;
6395
0
      break;
6396
6397
0
    case R_MIPS_PC19_S2:
6398
0
      if (howto->partial_inplace)
6399
0
  addend = _bfd_mips_elf_sign_extend (addend, 21);
6400
6401
0
      if ((symbol + addend) & 3)
6402
0
  return bfd_reloc_outofrange;
6403
6404
0
      value = symbol + addend - p;
6405
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6406
0
  overflowed_p = mips_elf_overflow_p (value, 21);
6407
0
      value >>= howto->rightshift;
6408
0
      value &= howto->dst_mask;
6409
0
      break;
6410
6411
0
    case R_MIPS_PCHI16:
6412
0
      value = mips_elf_high (symbol + addend - p);
6413
0
      value &= howto->dst_mask;
6414
0
      break;
6415
6416
0
    case R_MIPS_PCLO16:
6417
0
      if (howto->partial_inplace)
6418
0
  addend = _bfd_mips_elf_sign_extend (addend, 16);
6419
0
      value = symbol + addend - p;
6420
0
      value &= howto->dst_mask;
6421
0
      break;
6422
6423
0
    case R_MICROMIPS_PC7_S1:
6424
0
      if (howto->partial_inplace)
6425
0
  addend = _bfd_mips_elf_sign_extend (addend, 8);
6426
6427
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6428
0
    && (*cross_mode_jump_p
6429
0
        ? ((symbol + addend + 2) & 3) != 0
6430
0
        : ((symbol + addend + 2) & 1) == 0))
6431
0
  return bfd_reloc_outofrange;
6432
6433
0
      value = symbol + addend - p;
6434
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6435
0
  overflowed_p = mips_elf_overflow_p (value, 8);
6436
0
      value >>= howto->rightshift;
6437
0
      value &= howto->dst_mask;
6438
0
      break;
6439
6440
0
    case R_MICROMIPS_PC10_S1:
6441
0
      if (howto->partial_inplace)
6442
0
  addend = _bfd_mips_elf_sign_extend (addend, 11);
6443
6444
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6445
0
    && (*cross_mode_jump_p
6446
0
        ? ((symbol + addend + 2) & 3) != 0
6447
0
        : ((symbol + addend + 2) & 1) == 0))
6448
0
  return bfd_reloc_outofrange;
6449
6450
0
      value = symbol + addend - p;
6451
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6452
0
  overflowed_p = mips_elf_overflow_p (value, 11);
6453
0
      value >>= howto->rightshift;
6454
0
      value &= howto->dst_mask;
6455
0
      break;
6456
6457
0
    case R_MICROMIPS_PC16_S1:
6458
0
      if (howto->partial_inplace)
6459
0
  addend = _bfd_mips_elf_sign_extend (addend, 17);
6460
6461
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6462
0
    && (*cross_mode_jump_p
6463
0
        ? ((symbol + addend) & 3) != 0
6464
0
        : ((symbol + addend) & 1) == 0))
6465
0
  return bfd_reloc_outofrange;
6466
6467
0
      value = symbol + addend - p;
6468
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6469
0
  overflowed_p = mips_elf_overflow_p (value, 17);
6470
0
      value >>= howto->rightshift;
6471
0
      value &= howto->dst_mask;
6472
0
      break;
6473
6474
0
    case R_MICROMIPS_PC23_S2:
6475
0
      if (howto->partial_inplace)
6476
0
  addend = _bfd_mips_elf_sign_extend (addend, 25);
6477
0
      value = symbol + addend - ((p | 3) ^ 3);
6478
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6479
0
  overflowed_p = mips_elf_overflow_p (value, 25);
6480
0
      value >>= howto->rightshift;
6481
0
      value &= howto->dst_mask;
6482
0
      break;
6483
6484
0
    case R_MIPS_GOT_HI16:
6485
0
    case R_MIPS_CALL_HI16:
6486
0
    case R_MICROMIPS_GOT_HI16:
6487
0
    case R_MICROMIPS_CALL_HI16:
6488
      /* We're allowed to handle these two relocations identically.
6489
   The dynamic linker is allowed to handle the CALL relocations
6490
   differently by creating a lazy evaluation stub.  */
6491
0
      value = g;
6492
0
      value = mips_elf_high (value);
6493
0
      value &= howto->dst_mask;
6494
0
      break;
6495
6496
0
    case R_MIPS_GOT_LO16:
6497
0
    case R_MIPS_CALL_LO16:
6498
0
    case R_MICROMIPS_GOT_LO16:
6499
0
    case R_MICROMIPS_CALL_LO16:
6500
0
      value = g & howto->dst_mask;
6501
0
      break;
6502
6503
0
    case R_MIPS_GOT_PAGE:
6504
0
    case R_MICROMIPS_GOT_PAGE:
6505
0
      value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
6506
0
      if (value == MINUS_ONE)
6507
0
  return bfd_reloc_outofrange;
6508
0
      value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6509
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6510
0
      break;
6511
6512
0
    case R_MIPS_GOT_OFST:
6513
0
    case R_MICROMIPS_GOT_OFST:
6514
0
      if (local_p)
6515
0
  mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
6516
0
      else
6517
0
  value = addend;
6518
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6519
0
      break;
6520
6521
0
    case R_MIPS_SUB:
6522
0
    case R_MICROMIPS_SUB:
6523
0
      value = symbol - addend;
6524
0
      value &= howto->dst_mask;
6525
0
      break;
6526
6527
0
    case R_MIPS_HIGHER:
6528
0
    case R_MICROMIPS_HIGHER:
6529
0
      value = mips_elf_higher (addend + symbol);
6530
0
      value &= howto->dst_mask;
6531
0
      break;
6532
6533
0
    case R_MIPS_HIGHEST:
6534
0
    case R_MICROMIPS_HIGHEST:
6535
0
      value = mips_elf_highest (addend + symbol);
6536
0
      value &= howto->dst_mask;
6537
0
      break;
6538
6539
0
    case R_MIPS_SCN_DISP:
6540
0
    case R_MICROMIPS_SCN_DISP:
6541
0
      value = symbol + addend - sec->output_offset;
6542
0
      value &= howto->dst_mask;
6543
0
      break;
6544
6545
0
    case R_MIPS_JALR:
6546
0
    case R_MICROMIPS_JALR:
6547
      /* This relocation is only a hint.  In some cases, we optimize
6548
   it into a bal instruction.  But we don't try to optimize
6549
   when the symbol does not resolve locally.  */
6550
0
      if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
6551
0
  return bfd_reloc_continue;
6552
      /* We can't optimize cross-mode jumps either.  */
6553
0
      if (*cross_mode_jump_p)
6554
0
  return bfd_reloc_continue;
6555
0
      value = symbol + addend;
6556
      /* Neither we can non-instruction-aligned targets.  */
6557
0
      if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
6558
0
  return bfd_reloc_continue;
6559
0
      break;
6560
6561
0
    case R_MIPS_PJUMP:
6562
0
    case R_MIPS_GNU_VTINHERIT:
6563
0
    case R_MIPS_GNU_VTENTRY:
6564
      /* We don't do anything with these at present.  */
6565
0
      return bfd_reloc_continue;
6566
6567
0
    default:
6568
      /* An unrecognized relocation type.  */
6569
0
      return bfd_reloc_notsupported;
6570
0
    }
6571
6572
  /* Store the VALUE for our caller.  */
6573
0
  *valuep = value;
6574
0
  return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6575
0
}
6576
6577
/* It has been determined that the result of the RELOCATION is the
6578
   VALUE.  Use HOWTO to place VALUE into the output file at the
6579
   appropriate position.  The SECTION is the section to which the
6580
   relocation applies.
6581
   CROSS_MODE_JUMP_P is true if the relocation field
6582
   is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6583
6584
   Returns FALSE if anything goes wrong.  */
6585
6586
static bool
6587
mips_elf_perform_relocation (struct bfd_link_info *info,
6588
           reloc_howto_type *howto,
6589
           const Elf_Internal_Rela *relocation,
6590
           bfd_vma value, bfd *input_bfd,
6591
           asection *input_section, bfd_byte *contents,
6592
           bool cross_mode_jump_p)
6593
0
{
6594
0
  bfd_vma x;
6595
0
  bfd_byte *location;
6596
0
  int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6597
6598
  /* Figure out where the relocation is occurring.  */
6599
0
  location = contents + relocation->r_offset;
6600
6601
0
  _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
6602
6603
  /* Obtain the current value.  */
6604
0
  x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6605
6606
  /* Clear the field we are setting.  */
6607
0
  x &= ~howto->dst_mask;
6608
6609
  /* Set the field.  */
6610
0
  x |= (value & howto->dst_mask);
6611
6612
  /* Detect incorrect JALX usage.  If required, turn JAL or BAL into JALX.  */
6613
0
  if (!cross_mode_jump_p && jal_reloc_p (r_type))
6614
0
    {
6615
0
      bfd_vma opcode = x >> 26;
6616
6617
0
      if (r_type == R_MIPS16_26 ? opcode == 0x7
6618
0
    : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
6619
0
    : opcode == 0x1d)
6620
0
  {
6621
0
    info->callbacks->einfo
6622
0
      (_("%X%H: unsupported JALX to the same ISA mode\n"),
6623
0
       input_bfd, input_section, relocation->r_offset);
6624
0
    return true;
6625
0
  }
6626
0
    }
6627
0
  if (cross_mode_jump_p && jal_reloc_p (r_type))
6628
0
    {
6629
0
      bool ok;
6630
0
      bfd_vma opcode = x >> 26;
6631
0
      bfd_vma jalx_opcode;
6632
6633
      /* Check to see if the opcode is already JAL or JALX.  */
6634
0
      if (r_type == R_MIPS16_26)
6635
0
  {
6636
0
    ok = ((opcode == 0x6) || (opcode == 0x7));
6637
0
    jalx_opcode = 0x7;
6638
0
  }
6639
0
      else if (r_type == R_MICROMIPS_26_S1)
6640
0
  {
6641
0
    ok = ((opcode == 0x3d) || (opcode == 0x3c));
6642
0
    jalx_opcode = 0x3c;
6643
0
  }
6644
0
      else
6645
0
  {
6646
0
    ok = ((opcode == 0x3) || (opcode == 0x1d));
6647
0
    jalx_opcode = 0x1d;
6648
0
  }
6649
6650
      /* If the opcode is not JAL or JALX, there's a problem.  We cannot
6651
   convert J or JALS to JALX.  */
6652
0
      if (!ok)
6653
0
  {
6654
0
    info->callbacks->einfo
6655
0
      (_("%X%H: unsupported jump between ISA modes; "
6656
0
         "consider recompiling with interlinking enabled\n"),
6657
0
       input_bfd, input_section, relocation->r_offset);
6658
0
    return true;
6659
0
  }
6660
6661
      /* Make this the JALX opcode.  */
6662
0
      x = (x & ~(0x3fu << 26)) | (jalx_opcode << 26);
6663
0
    }
6664
0
  else if (cross_mode_jump_p && b_reloc_p (r_type))
6665
0
    {
6666
0
      bool ok = false;
6667
0
      bfd_vma opcode = x >> 16;
6668
0
      bfd_vma jalx_opcode = 0;
6669
0
      bfd_vma sign_bit = 0;
6670
0
      bfd_vma addr;
6671
0
      bfd_vma dest;
6672
6673
0
      if (r_type == R_MICROMIPS_PC16_S1)
6674
0
  {
6675
0
    ok = opcode == 0x4060;
6676
0
    jalx_opcode = 0x3c;
6677
0
    sign_bit = 0x10000;
6678
0
    value <<= 1;
6679
0
  }
6680
0
      else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
6681
0
  {
6682
0
    ok = opcode == 0x411;
6683
0
    jalx_opcode = 0x1d;
6684
0
    sign_bit = 0x20000;
6685
0
    value <<= 2;
6686
0
  }
6687
6688
0
      if (ok && !bfd_link_pic (info))
6689
0
  {
6690
0
    addr = (input_section->output_section->vma
6691
0
      + input_section->output_offset
6692
0
      + relocation->r_offset
6693
0
      + 4);
6694
0
    dest = (addr
6695
0
      + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
6696
6697
0
    if ((addr >> 28) << 28 != (dest >> 28) << 28)
6698
0
      {
6699
0
        info->callbacks->einfo
6700
0
    (_("%X%H: cannot convert branch between ISA modes "
6701
0
       "to JALX: relocation out of range\n"),
6702
0
     input_bfd, input_section, relocation->r_offset);
6703
0
        return true;
6704
0
      }
6705
6706
    /* Make this the JALX opcode.  */
6707
0
    x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
6708
0
  }
6709
0
      else if (!mips_elf_hash_table (info)->ignore_branch_isa)
6710
0
  {
6711
0
    info->callbacks->einfo
6712
0
      (_("%X%H: unsupported branch between ISA modes\n"),
6713
0
       input_bfd, input_section, relocation->r_offset);
6714
0
    return true;
6715
0
  }
6716
0
    }
6717
6718
  /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6719
     range.  */
6720
0
  if (!bfd_link_relocatable (info)
6721
0
      && !cross_mode_jump_p
6722
0
      && ((JAL_TO_BAL_P (input_bfd)
6723
0
     && r_type == R_MIPS_26
6724
0
     && (x >> 26) == 0x3)     /* jal addr */
6725
0
    || (JALR_TO_BAL_P (input_bfd)
6726
0
        && r_type == R_MIPS_JALR
6727
0
        && x == 0x0320f809)   /* jalr t9 */
6728
0
    || (JR_TO_B_P (input_bfd)
6729
0
        && r_type == R_MIPS_JALR
6730
0
        && (x & ~1) == 0x03200008))) /* jr t9 / jalr zero, t9 */
6731
0
    {
6732
0
      bfd_vma addr;
6733
0
      bfd_vma dest;
6734
0
      bfd_signed_vma off;
6735
6736
0
      addr = (input_section->output_section->vma
6737
0
        + input_section->output_offset
6738
0
        + relocation->r_offset
6739
0
        + 4);
6740
0
      if (r_type == R_MIPS_26)
6741
0
  dest = (value << 2) | ((addr >> 28) << 28);
6742
0
      else
6743
0
  dest = value;
6744
0
      off = dest - addr;
6745
0
      if (off <= 0x1ffff && off >= -0x20000)
6746
0
  {
6747
0
    if ((x & ~1) == 0x03200008)   /* jr t9 / jalr zero, t9 */
6748
0
      x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff);   /* b addr */
6749
0
    else
6750
0
      x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff);   /* bal addr */
6751
0
  }
6752
0
    }
6753
6754
  /* Put the value into the output.  */
6755
0
  mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
6756
6757
0
  _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
6758
0
             location);
6759
6760
0
  return true;
6761
0
}
6762

6763
/* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
6764
   is the original relocation, which is now being transformed into a
6765
   dynamic relocation.  The ADDENDP is adjusted if necessary; the
6766
   caller should store the result in place of the original addend.  */
6767
6768
static bool
6769
mips_elf_create_dynamic_relocation (bfd *output_bfd,
6770
            struct bfd_link_info *info,
6771
            const Elf_Internal_Rela *rel,
6772
            struct mips_elf_link_hash_entry *h,
6773
            asection *sec, bfd_vma symbol,
6774
            bfd_vma *addendp, asection *input_section)
6775
0
{
6776
0
  Elf_Internal_Rela outrel[3];
6777
0
  asection *sreloc;
6778
0
  bfd *dynobj;
6779
0
  int r_type;
6780
0
  long indx;
6781
0
  bool defined_p;
6782
0
  struct mips_elf_link_hash_table *htab;
6783
6784
0
  htab = mips_elf_hash_table (info);
6785
0
  BFD_ASSERT (htab != NULL);
6786
6787
0
  r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6788
0
  dynobj = elf_hash_table (info)->dynobj;
6789
0
  sreloc = mips_elf_rel_dyn_section (info, false);
6790
0
  BFD_ASSERT (sreloc != NULL);
6791
0
  BFD_ASSERT (sreloc->contents != NULL);
6792
0
  BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6793
0
        < sreloc->size);
6794
6795
0
  outrel[0].r_offset =
6796
0
    _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6797
0
  if (ABI_64_P (output_bfd))
6798
0
    {
6799
0
      outrel[1].r_offset =
6800
0
  _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6801
0
      outrel[2].r_offset =
6802
0
  _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6803
0
    }
6804
6805
0
  if (outrel[0].r_offset == MINUS_ONE)
6806
    /* The relocation field has been deleted.  */
6807
0
    return true;
6808
6809
0
  if (outrel[0].r_offset == MINUS_TWO)
6810
0
    {
6811
      /* The relocation field has been converted into a relative value of
6812
   some sort.  Functions like _bfd_elf_write_section_eh_frame expect
6813
   the field to be fully relocated, so add in the symbol's value.  */
6814
0
      *addendp += symbol;
6815
0
      return true;
6816
0
    }
6817
6818
  /* We must now calculate the dynamic symbol table index to use
6819
     in the relocation.  */
6820
0
  if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6821
0
    {
6822
0
      BFD_ASSERT (htab->root.target_os == is_vxworks
6823
0
      || h->global_got_area != GGA_NONE);
6824
0
      indx = h->root.dynindx;
6825
0
      if (SGI_COMPAT (output_bfd))
6826
0
  defined_p = h->root.def_regular;
6827
0
      else
6828
  /* ??? glibc's ld.so just adds the final GOT entry to the
6829
     relocation field.  It therefore treats relocs against
6830
     defined symbols in the same way as relocs against
6831
     undefined symbols.  */
6832
0
  defined_p = false;
6833
0
    }
6834
0
  else
6835
0
    {
6836
0
      if (sec != NULL && bfd_is_abs_section (sec))
6837
0
  indx = 0;
6838
0
      else if (sec == NULL || sec->owner == NULL)
6839
0
  {
6840
0
    bfd_set_error (bfd_error_bad_value);
6841
0
    return false;
6842
0
  }
6843
0
      else
6844
0
  {
6845
0
    indx = elf_section_data (sec->output_section)->dynindx;
6846
0
    if (indx == 0)
6847
0
      {
6848
0
        asection *osec = htab->root.text_index_section;
6849
0
        indx = elf_section_data (osec)->dynindx;
6850
0
      }
6851
0
    if (indx == 0)
6852
0
      abort ();
6853
0
  }
6854
6855
      /* Instead of generating a relocation using the section
6856
   symbol, we may as well make it a fully relative
6857
   relocation.  We want to avoid generating relocations to
6858
   local symbols because we used to generate them
6859
   incorrectly, without adding the original symbol value,
6860
   which is mandated by the ABI for section symbols.  In
6861
   order to give dynamic loaders and applications time to
6862
   phase out the incorrect use, we refrain from emitting
6863
   section-relative relocations.  It's not like they're
6864
   useful, after all.  This should be a bit more efficient
6865
   as well.  */
6866
      /* ??? Although this behavior is compatible with glibc's ld.so,
6867
   the ABI says that relocations against STN_UNDEF should have
6868
   a symbol value of 0.  Irix rld honors this, so relocations
6869
   against STN_UNDEF have no effect.  */
6870
0
      if (!SGI_COMPAT (output_bfd))
6871
0
  indx = 0;
6872
0
      defined_p = true;
6873
0
    }
6874
6875
  /* If the relocation was previously an absolute relocation and
6876
     this symbol will not be referred to by the relocation, we must
6877
     adjust it by the value we give it in the dynamic symbol table.
6878
     Otherwise leave the job up to the dynamic linker.  */
6879
0
  if (defined_p && r_type != R_MIPS_REL32)
6880
0
    *addendp += symbol;
6881
6882
0
  if (htab->root.target_os == is_vxworks)
6883
    /* VxWorks uses non-relative relocations for this.  */
6884
0
    outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6885
0
  else
6886
    /* The relocation is always an REL32 relocation because we don't
6887
       know where the shared library will wind up at load-time.  */
6888
0
    outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6889
0
           R_MIPS_REL32);
6890
6891
  /* For strict adherence to the ABI specification, we should
6892
     generate a R_MIPS_64 relocation record by itself before the
6893
     _REL32/_64 record as well, such that the addend is read in as
6894
     a 64-bit value (REL32 is a 32-bit relocation, after all).
6895
     However, since none of the existing ELF64 MIPS dynamic
6896
     loaders seems to care, we don't waste space with these
6897
     artificial relocations.  If this turns out to not be true,
6898
     mips_elf_allocate_dynamic_relocation() should be tweaked so
6899
     as to make room for a pair of dynamic relocations per
6900
     invocation if ABI_64_P, and here we should generate an
6901
     additional relocation record with R_MIPS_64 by itself for a
6902
     NULL symbol before this relocation record.  */
6903
0
  outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6904
0
         ABI_64_P (output_bfd)
6905
0
         ? R_MIPS_64
6906
0
         : R_MIPS_NONE);
6907
0
  outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6908
6909
  /* Adjust the output offset of the relocation to reference the
6910
     correct location in the output file.  */
6911
0
  outrel[0].r_offset += (input_section->output_section->vma
6912
0
       + input_section->output_offset);
6913
0
  outrel[1].r_offset += (input_section->output_section->vma
6914
0
       + input_section->output_offset);
6915
0
  outrel[2].r_offset += (input_section->output_section->vma
6916
0
       + input_section->output_offset);
6917
6918
  /* Put the relocation back out.  We have to use the special
6919
     relocation outputter in the 64-bit case since the 64-bit
6920
     relocation format is non-standard.  */
6921
0
  if (ABI_64_P (output_bfd))
6922
0
    {
6923
0
      (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6924
0
  (output_bfd, &outrel[0],
6925
0
   (sreloc->contents
6926
0
    + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6927
0
    }
6928
0
  else if (htab->root.target_os == is_vxworks)
6929
0
    {
6930
      /* VxWorks uses RELA rather than REL dynamic relocations.  */
6931
0
      outrel[0].r_addend = *addendp;
6932
0
      bfd_elf32_swap_reloca_out
6933
0
  (output_bfd, &outrel[0],
6934
0
   (sreloc->contents
6935
0
    + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6936
0
    }
6937
0
  else
6938
0
    bfd_elf32_swap_reloc_out
6939
0
      (output_bfd, &outrel[0],
6940
0
       (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6941
6942
  /* We've now added another relocation.  */
6943
0
  ++sreloc->reloc_count;
6944
6945
  /* Make sure the output section is writable.  The dynamic linker
6946
     will be writing to it.  */
6947
0
  elf_section_data (input_section->output_section)->this_hdr.sh_flags
6948
0
    |= SHF_WRITE;
6949
6950
  /* On IRIX5, make an entry of compact relocation info.  */
6951
0
  if (IRIX_COMPAT (output_bfd) == ict_irix5)
6952
0
    {
6953
0
      asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6954
0
      bfd_byte *cr;
6955
6956
0
      if (scpt)
6957
0
  {
6958
0
    Elf32_crinfo cptrel;
6959
6960
0
    mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6961
0
    cptrel.vaddr = (rel->r_offset
6962
0
        + input_section->output_section->vma
6963
0
        + input_section->output_offset);
6964
0
    if (r_type == R_MIPS_REL32)
6965
0
      mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6966
0
    else
6967
0
      mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6968
0
    mips_elf_set_cr_dist2to (cptrel, 0);
6969
0
    cptrel.konst = *addendp;
6970
6971
0
    cr = (scpt->contents
6972
0
    + sizeof (Elf32_External_compact_rel));
6973
0
    mips_elf_set_cr_relvaddr (cptrel, 0);
6974
0
    bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6975
0
             ((Elf32_External_crinfo *) cr
6976
0
              + scpt->reloc_count));
6977
0
    ++scpt->reloc_count;
6978
0
  }
6979
0
    }
6980
6981
  /* If we've written this relocation for a readonly section,
6982
     we need to set DF_TEXTREL again, so that we do not delete the
6983
     DT_TEXTREL tag.  */
6984
0
  if (MIPS_ELF_READONLY_SECTION (input_section))
6985
0
    info->flags |= DF_TEXTREL;
6986
6987
0
  return true;
6988
0
}
6989

6990
/* Return the MACH for a MIPS e_flags value.  */
6991
6992
unsigned long
6993
_bfd_elf_mips_mach (flagword flags)
6994
98.1k
{
6995
98.1k
  switch (flags & EF_MIPS_MACH)
6996
98.1k
    {
6997
23
    case E_MIPS_MACH_3900:
6998
23
      return bfd_mach_mips3900;
6999
7000
17
    case E_MIPS_MACH_4010:
7001
17
      return bfd_mach_mips4010;
7002
7003
9
    case E_MIPS_MACH_ALLEGREX:
7004
9
      return bfd_mach_mips_allegrex;
7005
7006
12
    case E_MIPS_MACH_4100:
7007
12
      return bfd_mach_mips4100;
7008
7009
12
    case E_MIPS_MACH_4111:
7010
12
      return bfd_mach_mips4111;
7011
7012
12
    case E_MIPS_MACH_4120:
7013
12
      return bfd_mach_mips4120;
7014
7015
163
    case E_MIPS_MACH_4650:
7016
163
      return bfd_mach_mips4650;
7017
7018
3
    case E_MIPS_MACH_5400:
7019
3
      return bfd_mach_mips5400;
7020
7021
26
    case E_MIPS_MACH_5500:
7022
26
      return bfd_mach_mips5500;
7023
7024
12
    case E_MIPS_MACH_5900:
7025
12
      return bfd_mach_mips5900;
7026
7027
3.68k
    case E_MIPS_MACH_9000:
7028
3.68k
      return bfd_mach_mips9000;
7029
7030
18
    case E_MIPS_MACH_SB1:
7031
18
      return bfd_mach_mips_sb1;
7032
7033
6
    case E_MIPS_MACH_LS2E:
7034
6
      return bfd_mach_mips_loongson_2e;
7035
7036
7
    case E_MIPS_MACH_LS2F:
7037
7
      return bfd_mach_mips_loongson_2f;
7038
7039
6
    case E_MIPS_MACH_GS464:
7040
6
      return bfd_mach_mips_gs464;
7041
7042
0
    case E_MIPS_MACH_GS464E:
7043
0
      return bfd_mach_mips_gs464e;
7044
7045
0
    case E_MIPS_MACH_GS264E:
7046
0
      return bfd_mach_mips_gs264e;
7047
7048
1.23k
    case E_MIPS_MACH_OCTEON3:
7049
1.23k
      return bfd_mach_mips_octeon3;
7050
7051
61
    case E_MIPS_MACH_OCTEON2:
7052
61
      return bfd_mach_mips_octeon2;
7053
7054
62
    case E_MIPS_MACH_OCTEON:
7055
62
      return bfd_mach_mips_octeon;
7056
7057
62
    case E_MIPS_MACH_XLR:
7058
62
      return bfd_mach_mips_xlr;
7059
7060
36
    case E_MIPS_MACH_IAMR2:
7061
36
      return bfd_mach_mips_interaptiv_mr2;
7062
7063
92.7k
    default:
7064
92.7k
      switch (flags & EF_MIPS_ARCH)
7065
92.7k
  {
7066
772
  default:
7067
82.3k
  case E_MIPS_ARCH_1:
7068
82.3k
    return bfd_mach_mips3000;
7069
7070
194
  case E_MIPS_ARCH_2:
7071
194
    return bfd_mach_mips6000;
7072
7073
1.17k
  case E_MIPS_ARCH_3:
7074
1.17k
    return bfd_mach_mips4000;
7075
7076
458
  case E_MIPS_ARCH_4:
7077
458
    return bfd_mach_mips8000;
7078
7079
39
  case E_MIPS_ARCH_5:
7080
39
    return bfd_mach_mips5;
7081
7082
1.19k
  case E_MIPS_ARCH_32:
7083
1.19k
    return bfd_mach_mipsisa32;
7084
7085
2.86k
  case E_MIPS_ARCH_64:
7086
2.86k
    return bfd_mach_mipsisa64;
7087
7088
2.39k
  case E_MIPS_ARCH_32R2:
7089
2.39k
    return bfd_mach_mipsisa32r2;
7090
7091
1.86k
  case E_MIPS_ARCH_64R2:
7092
1.86k
    return bfd_mach_mipsisa64r2;
7093
7094
172
  case E_MIPS_ARCH_32R6:
7095
172
    return bfd_mach_mipsisa32r6;
7096
7097
50
  case E_MIPS_ARCH_64R6:
7098
50
    return bfd_mach_mipsisa64r6;
7099
92.7k
  }
7100
98.1k
    }
7101
7102
0
  return 0;
7103
98.1k
}
7104
7105
/* Return printable name for ABI.  */
7106
7107
static inline char *
7108
elf_mips_abi_name (bfd *abfd)
7109
0
{
7110
0
  flagword flags;
7111
7112
0
  flags = elf_elfheader (abfd)->e_flags;
7113
0
  switch (flags & EF_MIPS_ABI)
7114
0
    {
7115
0
    case 0:
7116
0
      if (ABI_N32_P (abfd))
7117
0
  return "N32";
7118
0
      else if (ABI_64_P (abfd))
7119
0
  return "64";
7120
0
      else
7121
0
  return "none";
7122
0
    case E_MIPS_ABI_O32:
7123
0
      return "O32";
7124
0
    case E_MIPS_ABI_O64:
7125
0
      return "O64";
7126
0
    case E_MIPS_ABI_EABI32:
7127
0
      return "EABI32";
7128
0
    case E_MIPS_ABI_EABI64:
7129
0
      return "EABI64";
7130
0
    default:
7131
0
      return "unknown abi";
7132
0
    }
7133
0
}
7134

7135
/* MIPS ELF uses two common sections.  One is the usual one, and the
7136
   other is for small objects.  All the small objects are kept
7137
   together, and then referenced via the gp pointer, which yields
7138
   faster assembler code.  This is what we use for the small common
7139
   section.  This approach is copied from ecoff.c.  */
7140
static asection mips_elf_scom_section;
7141
static const asymbol mips_elf_scom_symbol =
7142
  GLOBAL_SYM_INIT (".scommon", &mips_elf_scom_section);
7143
static asection mips_elf_scom_section =
7144
  BFD_FAKE_SECTION (mips_elf_scom_section, &mips_elf_scom_symbol,
7145
        ".scommon", 0, SEC_IS_COMMON | SEC_SMALL_DATA);
7146
7147
/* MIPS ELF also uses an acommon section, which represents an
7148
   allocated common symbol which may be overridden by a
7149
   definition in a shared library.  */
7150
static asection mips_elf_acom_section;
7151
static const asymbol mips_elf_acom_symbol =
7152
  GLOBAL_SYM_INIT (".acommon", &mips_elf_acom_section);
7153
static asection mips_elf_acom_section =
7154
  BFD_FAKE_SECTION (mips_elf_acom_section, &mips_elf_acom_symbol,
7155
        ".acommon", 0, SEC_ALLOC);
7156
7157
/* This is used for both the 32-bit and the 64-bit ABI.  */
7158
7159
void
7160
_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
7161
43.2k
{
7162
43.2k
  elf_symbol_type *elfsym;
7163
7164
  /* Handle the special MIPS section numbers that a symbol may use.  */
7165
43.2k
  elfsym = (elf_symbol_type *) asym;
7166
43.2k
  switch (elfsym->internal_elf_sym.st_shndx)
7167
43.2k
    {
7168
104
    case SHN_MIPS_ACOMMON:
7169
      /* This section is used in a dynamically linked executable file.
7170
   It is an allocated common section.  The dynamic linker can
7171
   either resolve these symbols to something in a shared
7172
   library, or it can just leave them here.  For our purposes,
7173
   we can consider these symbols to be in a new section.  */
7174
104
      asym->section = &mips_elf_acom_section;
7175
104
      break;
7176
7177
81
    case SHN_COMMON:
7178
      /* Common symbols less than the GP size are automatically
7179
   treated as SHN_MIPS_SCOMMON symbols on IRIX5.  */
7180
81
      if (asym->value > elf_gp_size (abfd)
7181
81
    || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
7182
81
    || IRIX_COMPAT (abfd) == ict_irix6)
7183
78
  break;
7184
      /* Fall through.  */
7185
39
    case SHN_MIPS_SCOMMON:
7186
39
      asym->section = &mips_elf_scom_section;
7187
39
      asym->value = elfsym->internal_elf_sym.st_size;
7188
39
      break;
7189
7190
58
    case SHN_MIPS_SUNDEFINED:
7191
58
      asym->section = bfd_und_section_ptr;
7192
58
      break;
7193
7194
32
    case SHN_MIPS_TEXT:
7195
32
      {
7196
32
  asection *section = bfd_get_section_by_name (abfd, ".text");
7197
7198
32
  if (section != NULL)
7199
13
    {
7200
13
      asym->section = section;
7201
      /* MIPS_TEXT is a bit special, the address is not an offset
7202
         to the base of the .text section.  So subtract the section
7203
         base address to make it an offset.  */
7204
13
      asym->value -= section->vma;
7205
13
    }
7206
32
      }
7207
32
      break;
7208
7209
31
    case SHN_MIPS_DATA:
7210
31
      {
7211
31
  asection *section = bfd_get_section_by_name (abfd, ".data");
7212
7213
31
  if (section != NULL)
7214
0
    {
7215
0
      asym->section = section;
7216
      /* MIPS_DATA is a bit special, the address is not an offset
7217
         to the base of the .data section.  So subtract the section
7218
         base address to make it an offset.  */
7219
0
      asym->value -= section->vma;
7220
0
    }
7221
31
      }
7222
31
      break;
7223
43.2k
    }
7224
7225
  /* If this is an odd-valued function symbol, assume it's a MIPS16
7226
     or microMIPS one.  */
7227
43.2k
  if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
7228
43.2k
      && (asym->value & 1) != 0)
7229
666
    {
7230
666
      asym->value--;
7231
666
      if (MICROMIPS_P (abfd))
7232
139
  elfsym->internal_elf_sym.st_other
7233
139
    = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
7234
527
      else
7235
527
  elfsym->internal_elf_sym.st_other
7236
527
    = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
7237
666
    }
7238
43.2k
}
7239

7240
/* Implement elf_backend_eh_frame_address_size.  This differs from
7241
   the default in the way it handles EABI64.
7242
7243
   EABI64 was originally specified as an LP64 ABI, and that is what
7244
   -mabi=eabi normally gives on a 64-bit target.  However, gcc has
7245
   historically accepted the combination of -mabi=eabi and -mlong32,
7246
   and this ILP32 variation has become semi-official over time.
7247
   Both forms use elf32 and have pointer-sized FDE addresses.
7248
7249
   If an EABI object was generated by GCC 4.0 or above, it will have
7250
   an empty .gcc_compiled_longXX section, where XX is the size of longs
7251
   in bits.  Unfortunately, ILP32 objects generated by earlier compilers
7252
   have no special marking to distinguish them from LP64 objects.
7253
7254
   We don't want users of the official LP64 ABI to be punished for the
7255
   existence of the ILP32 variant, but at the same time, we don't want
7256
   to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
7257
   We therefore take the following approach:
7258
7259
      - If ABFD contains a .gcc_compiled_longXX section, use it to
7260
  determine the pointer size.
7261
7262
      - Otherwise check the type of the first relocation.  Assume that
7263
  the LP64 ABI is being used if the relocation is of type R_MIPS_64.
7264
7265
      - Otherwise punt.
7266
7267
   The second check is enough to detect LP64 objects generated by pre-4.0
7268
   compilers because, in the kind of output generated by those compilers,
7269
   the first relocation will be associated with either a CIE personality
7270
   routine or an FDE start address.  Furthermore, the compilers never
7271
   used a special (non-pointer) encoding for this ABI.
7272
7273
   Checking the relocation type should also be safe because there is no
7274
   reason to use R_MIPS_64 in an ILP32 object.  Pre-4.0 compilers never
7275
   did so.  */
7276
7277
unsigned int
7278
_bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
7279
0
{
7280
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
7281
0
    return 8;
7282
0
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
7283
0
    {
7284
0
      bool long32_p, long64_p;
7285
7286
0
      long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
7287
0
      long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
7288
0
      if (long32_p && long64_p)
7289
0
  return 0;
7290
0
      if (long32_p)
7291
0
  return 4;
7292
0
      if (long64_p)
7293
0
  return 8;
7294
7295
0
      if (sec->reloc_count > 0
7296
0
    && elf_section_data (sec)->relocs != NULL
7297
0
    && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
7298
0
        == R_MIPS_64))
7299
0
  return 8;
7300
7301
0
      return 0;
7302
0
    }
7303
0
  return 4;
7304
0
}
7305

7306
/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
7307
   relocations against two unnamed section symbols to resolve to the
7308
   same address.  For example, if we have code like:
7309
7310
  lw  $4,%got_disp(.data)($gp)
7311
  lw  $25,%got_disp(.text)($gp)
7312
  jalr  $25
7313
7314
   then the linker will resolve both relocations to .data and the program
7315
   will jump there rather than to .text.
7316
7317
   We can work around this problem by giving names to local section symbols.
7318
   This is also what the MIPSpro tools do.  */
7319
7320
bool
7321
_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
7322
11
{
7323
11
  return elf_elfheader (abfd)->e_type == ET_REL && SGI_COMPAT (abfd);
7324
11
}
7325

7326
/* Work over a section just before writing it out.  This routine is
7327
   used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
7328
   sections that need the SHF_MIPS_GPREL flag by name; there has to be
7329
   a better way.  */
7330
7331
bool
7332
_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
7333
429
{
7334
429
  if (hdr->sh_type == SHT_MIPS_REGINFO
7335
429
      && hdr->sh_size > 0)
7336
0
    {
7337
0
      bfd_byte buf[4];
7338
7339
0
      BFD_ASSERT (hdr->contents == NULL);
7340
7341
0
      if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
7342
0
  {
7343
0
    _bfd_error_handler
7344
0
      (_("%pB: incorrect `.reginfo' section size; "
7345
0
         "expected %" PRIu64 ", got %" PRIu64),
7346
0
       abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
7347
0
       (uint64_t) hdr->sh_size);
7348
0
    bfd_set_error (bfd_error_bad_value);
7349
0
    return false;
7350
0
  }
7351
7352
0
      if (bfd_seek (abfd,
7353
0
        hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
7354
0
        SEEK_SET) != 0)
7355
0
  return false;
7356
0
      H_PUT_32 (abfd, elf_gp (abfd), buf);
7357
0
      if (bfd_bwrite (buf, 4, abfd) != 4)
7358
0
  return false;
7359
0
    }
7360
7361
429
  if (hdr->sh_type == SHT_MIPS_OPTIONS
7362
429
      && hdr->bfd_section != NULL
7363
429
      && mips_elf_section_data (hdr->bfd_section) != NULL
7364
429
      && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
7365
0
    {
7366
0
      bfd_byte *contents, *l, *lend;
7367
7368
      /* We stored the section contents in the tdata field in the
7369
   set_section_contents routine.  We save the section contents
7370
   so that we don't have to read them again.
7371
   At this point we know that elf_gp is set, so we can look
7372
   through the section contents to see if there is an
7373
   ODK_REGINFO structure.  */
7374
7375
0
      contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
7376
0
      l = contents;
7377
0
      lend = contents + hdr->sh_size;
7378
0
      while (l + sizeof (Elf_External_Options) <= lend)
7379
0
  {
7380
0
    Elf_Internal_Options intopt;
7381
7382
0
    bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7383
0
          &intopt);
7384
0
    if (intopt.size < sizeof (Elf_External_Options))
7385
0
      {
7386
0
        _bfd_error_handler
7387
    /* xgettext:c-format */
7388
0
    (_("%pB: warning: bad `%s' option size %u smaller than"
7389
0
       " its header"),
7390
0
    abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7391
0
        break;
7392
0
      }
7393
0
    if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7394
0
      {
7395
0
        bfd_byte buf[8];
7396
7397
0
        if (bfd_seek (abfd,
7398
0
          (hdr->sh_offset
7399
0
           + (l - contents)
7400
0
           + sizeof (Elf_External_Options)
7401
0
           + (sizeof (Elf64_External_RegInfo) - 8)),
7402
0
           SEEK_SET) != 0)
7403
0
    return false;
7404
0
        H_PUT_64 (abfd, elf_gp (abfd), buf);
7405
0
        if (bfd_bwrite (buf, 8, abfd) != 8)
7406
0
    return false;
7407
0
      }
7408
0
    else if (intopt.kind == ODK_REGINFO)
7409
0
      {
7410
0
        bfd_byte buf[4];
7411
7412
0
        if (bfd_seek (abfd,
7413
0
          (hdr->sh_offset
7414
0
           + (l - contents)
7415
0
           + sizeof (Elf_External_Options)
7416
0
           + (sizeof (Elf32_External_RegInfo) - 4)),
7417
0
          SEEK_SET) != 0)
7418
0
    return false;
7419
0
        H_PUT_32 (abfd, elf_gp (abfd), buf);
7420
0
        if (bfd_bwrite (buf, 4, abfd) != 4)
7421
0
    return false;
7422
0
      }
7423
0
    l += intopt.size;
7424
0
  }
7425
0
    }
7426
7427
429
  if (hdr->bfd_section != NULL)
7428
381
    {
7429
381
      const char *name = bfd_section_name (hdr->bfd_section);
7430
7431
      /* .sbss is not handled specially here because the GNU/Linux
7432
   prelinker can convert .sbss from NOBITS to PROGBITS and
7433
   changing it back to NOBITS breaks the binary.  The entry in
7434
   _bfd_mips_elf_special_sections will ensure the correct flags
7435
   are set on .sbss if BFD creates it without reading it from an
7436
   input file, and without special handling here the flags set
7437
   on it in an input file will be followed.  */
7438
381
      if (strcmp (name, ".sdata") == 0
7439
381
    || strcmp (name, ".lit8") == 0
7440
381
    || strcmp (name, ".lit4") == 0)
7441
9
  hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7442
372
      else if (strcmp (name, ".srdata") == 0)
7443
0
  hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
7444
372
      else if (strcmp (name, ".compact_rel") == 0)
7445
9
  hdr->sh_flags = 0;
7446
363
      else if (strcmp (name, ".rtproc") == 0)
7447
0
  {
7448
0
    if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
7449
0
      {
7450
0
        unsigned int adjust;
7451
7452
0
        adjust = hdr->sh_size % hdr->sh_addralign;
7453
0
        if (adjust != 0)
7454
0
    hdr->sh_size += hdr->sh_addralign - adjust;
7455
0
      }
7456
0
  }
7457
381
    }
7458
7459
429
  return true;
7460
429
}
7461
7462
/* Handle a MIPS specific section when reading an object file.  This
7463
   is called when elfcode.h finds a section with an unknown type.
7464
   This routine supports both the 32-bit and 64-bit ELF ABI.  */
7465
7466
bool
7467
_bfd_mips_elf_section_from_shdr (bfd *abfd,
7468
         Elf_Internal_Shdr *hdr,
7469
         const char *name,
7470
         int shindex)
7471
174k
{
7472
174k
  flagword flags = 0;
7473
7474
  /* There ought to be a place to keep ELF backend specific flags, but
7475
     at the moment there isn't one.  We just keep track of the
7476
     sections by their name, instead.  Fortunately, the ABI gives
7477
     suggested names for all the MIPS specific sections, so we will
7478
     probably get away with this.  */
7479
174k
  switch (hdr->sh_type)
7480
174k
    {
7481
974
    case SHT_MIPS_LIBLIST:
7482
974
      if (strcmp (name, ".liblist") != 0)
7483
974
  return false;
7484
0
      break;
7485
20
    case SHT_MIPS_MSYM:
7486
20
      if (strcmp (name, ".msym") != 0)
7487
20
  return false;
7488
0
      break;
7489
3.24k
    case SHT_MIPS_CONFLICT:
7490
3.24k
      if (strcmp (name, ".conflict") != 0)
7491
3.24k
  return false;
7492
0
      break;
7493
942
    case SHT_MIPS_GPTAB:
7494
942
      if (! startswith (name, ".gptab."))
7495
942
  return false;
7496
0
      break;
7497
1.18k
    case SHT_MIPS_UCODE:
7498
1.18k
      if (strcmp (name, ".ucode") != 0)
7499
1.18k
  return false;
7500
0
      break;
7501
473
    case SHT_MIPS_DEBUG:
7502
473
      if (strcmp (name, ".mdebug") != 0)
7503
473
  return false;
7504
0
      flags = SEC_DEBUGGING;
7505
0
      break;
7506
48
    case SHT_MIPS_REGINFO:
7507
48
      if (strcmp (name, ".reginfo") != 0
7508
48
    || hdr->sh_size != sizeof (Elf32_External_RegInfo))
7509
6
  return false;
7510
42
      flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7511
42
      break;
7512
7
    case SHT_MIPS_IFACE:
7513
7
      if (strcmp (name, ".MIPS.interfaces") != 0)
7514
7
  return false;
7515
0
      break;
7516
481
    case SHT_MIPS_CONTENT:
7517
481
      if (! startswith (name, ".MIPS.content"))
7518
481
  return false;
7519
0
      break;
7520
2.05k
    case SHT_MIPS_OPTIONS:
7521
2.05k
      if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7522
249
  return false;
7523
1.80k
      break;
7524
1.80k
    case SHT_MIPS_ABIFLAGS:
7525
13
      if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
7526
13
  return false;
7527
0
      flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7528
0
      break;
7529
1.85k
    case SHT_MIPS_DWARF:
7530
1.85k
      if (! startswith (name, ".debug_")
7531
1.85k
         && ! startswith (name, ".gnu.debuglto_.debug_")
7532
1.85k
         && ! startswith (name, ".zdebug_")
7533
1.85k
         && ! startswith (name, ".gnu.debuglto_.zdebug_"))
7534
476
  return false;
7535
1.38k
      break;
7536
1.38k
    case SHT_MIPS_SYMBOL_LIB:
7537
9
      if (strcmp (name, ".MIPS.symlib") != 0)
7538
9
  return false;
7539
0
      break;
7540
7
    case SHT_MIPS_EVENTS:
7541
7
      if (! startswith (name, ".MIPS.events")
7542
7
    && ! startswith (name, ".MIPS.post_rel"))
7543
7
  return false;
7544
0
      break;
7545
5
    case SHT_MIPS_XHASH:
7546
5
      if (strcmp (name, ".MIPS.xhash") != 0)
7547
5
  return false;
7548
163k
    default:
7549
163k
      break;
7550
174k
    }
7551
7552
166k
  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
7553
2.38k
    return false;
7554
7555
164k
  if (hdr->sh_flags & SHF_MIPS_GPREL)
7556
24.8k
    flags |= SEC_SMALL_DATA;
7557
7558
164k
  if (flags)
7559
24.9k
    {
7560
24.9k
      if (!bfd_set_section_flags (hdr->bfd_section,
7561
24.9k
          (bfd_section_flags (hdr->bfd_section)
7562
24.9k
           | flags)))
7563
0
  return false;
7564
24.9k
    }
7565
7566
164k
  if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7567
0
    {
7568
0
      Elf_External_ABIFlags_v0 ext;
7569
7570
0
      if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7571
0
              &ext, 0, sizeof ext))
7572
0
  return false;
7573
0
      bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7574
0
          &mips_elf_tdata (abfd)->abiflags);
7575
0
      if (mips_elf_tdata (abfd)->abiflags.version != 0)
7576
0
  return false;
7577
0
      mips_elf_tdata (abfd)->abiflags_valid = true;
7578
0
    }
7579
7580
  /* FIXME: We should record sh_info for a .gptab section.  */
7581
7582
  /* For a .reginfo section, set the gp value in the tdata information
7583
     from the contents of this section.  We need the gp value while
7584
     processing relocs, so we just get it now.  The .reginfo section
7585
     is not used in the 64-bit MIPS ELF ABI.  */
7586
164k
  if (hdr->sh_type == SHT_MIPS_REGINFO)
7587
42
    {
7588
42
      Elf32_External_RegInfo ext;
7589
42
      Elf32_RegInfo s;
7590
7591
42
      if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7592
42
              &ext, 0, sizeof ext))
7593
0
  return false;
7594
42
      bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7595
42
      elf_gp (abfd) = s.ri_gp_value;
7596
42
    }
7597
7598
  /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7599
     set the gp value based on what we find.  We may see both
7600
     SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7601
     they should agree.  */
7602
164k
  if (hdr->sh_type == SHT_MIPS_OPTIONS)
7603
1.80k
    {
7604
1.80k
      bfd_byte *contents, *l, *lend;
7605
7606
1.80k
      if (!bfd_malloc_and_get_section (abfd, hdr->bfd_section, &contents))
7607
408
  {
7608
408
    free (contents);
7609
408
    return false;
7610
408
  }
7611
1.40k
      l = contents;
7612
1.40k
      lend = contents + hdr->sh_size;
7613
4.17k
      while (l + sizeof (Elf_External_Options) <= lend)
7614
3.47k
  {
7615
3.47k
    Elf_Internal_Options intopt;
7616
7617
3.47k
    bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7618
3.47k
          &intopt);
7619
3.47k
    if (intopt.size < sizeof (Elf_External_Options))
7620
2
      {
7621
705
      bad_opt:
7622
705
        _bfd_error_handler
7623
    /* xgettext:c-format */
7624
705
    (_("%pB: warning: truncated `%s' option"),
7625
705
     abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd));
7626
705
        break;
7627
2
      }
7628
3.47k
    if (intopt.kind == ODK_REGINFO)
7629
1.39k
      {
7630
1.39k
        if (ABI_64_P (abfd))
7631
1.39k
    {
7632
1.39k
      Elf64_Internal_RegInfo intreg;
7633
1.39k
      size_t needed = (sizeof (Elf_External_Options)
7634
1.39k
           + sizeof (Elf64_External_RegInfo));
7635
1.39k
      if (intopt.size < needed || (size_t) (lend - l) < needed)
7636
703
        goto bad_opt;
7637
689
      bfd_mips_elf64_swap_reginfo_in
7638
689
        (abfd,
7639
689
         ((Elf64_External_RegInfo *)
7640
689
          (l + sizeof (Elf_External_Options))),
7641
689
         &intreg);
7642
689
      elf_gp (abfd) = intreg.ri_gp_value;
7643
689
    }
7644
0
        else
7645
0
    {
7646
0
      Elf32_RegInfo intreg;
7647
0
      size_t needed = (sizeof (Elf_External_Options)
7648
0
           + sizeof (Elf32_External_RegInfo));
7649
0
      if (intopt.size < needed || (size_t) (lend - l) < needed)
7650
0
        goto bad_opt;
7651
0
      bfd_mips_elf32_swap_reginfo_in
7652
0
        (abfd,
7653
0
         ((Elf32_External_RegInfo *)
7654
0
          (l + sizeof (Elf_External_Options))),
7655
0
         &intreg);
7656
0
      elf_gp (abfd) = intreg.ri_gp_value;
7657
0
    }
7658
1.39k
      }
7659
2.77k
    l += intopt.size;
7660
2.77k
  }
7661
1.40k
      free (contents);
7662
1.40k
    }
7663
7664
163k
  return true;
7665
164k
}
7666
7667
/* Set the correct type for a MIPS ELF section.  We do this by the
7668
   section name, which is a hack, but ought to work.  This routine is
7669
   used by both the 32-bit and the 64-bit ABI.  */
7670
7671
bool
7672
_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7673
381
{
7674
381
  const char *name = bfd_section_name (sec);
7675
7676
381
  if (strcmp (name, ".liblist") == 0)
7677
0
    {
7678
0
      hdr->sh_type = SHT_MIPS_LIBLIST;
7679
0
      hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7680
      /* The sh_link field is set in final_write_processing.  */
7681
0
    }
7682
381
  else if (strcmp (name, ".conflict") == 0)
7683
0
    hdr->sh_type = SHT_MIPS_CONFLICT;
7684
381
  else if (startswith (name, ".gptab."))
7685
0
    {
7686
0
      hdr->sh_type = SHT_MIPS_GPTAB;
7687
0
      hdr->sh_entsize = sizeof (Elf32_External_gptab);
7688
      /* The sh_info field is set in final_write_processing.  */
7689
0
    }
7690
381
  else if (strcmp (name, ".ucode") == 0)
7691
0
    hdr->sh_type = SHT_MIPS_UCODE;
7692
381
  else if (strcmp (name, ".mdebug") == 0)
7693
0
    {
7694
0
      hdr->sh_type = SHT_MIPS_DEBUG;
7695
      /* In a shared object on IRIX 5.3, the .mdebug section has an
7696
   entsize of 0.  FIXME: Does this matter?  */
7697
0
      if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7698
0
  hdr->sh_entsize = 0;
7699
0
      else
7700
0
  hdr->sh_entsize = 1;
7701
0
    }
7702
381
  else if (strcmp (name, ".reginfo") == 0)
7703
0
    {
7704
0
      hdr->sh_type = SHT_MIPS_REGINFO;
7705
      /* In a shared object on IRIX 5.3, the .reginfo section has an
7706
   entsize of 0x18.  FIXME: Does this matter?  */
7707
0
      if (SGI_COMPAT (abfd))
7708
0
  {
7709
0
    if ((abfd->flags & DYNAMIC) != 0)
7710
0
      hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7711
0
    else
7712
0
      hdr->sh_entsize = 1;
7713
0
  }
7714
0
      else
7715
0
  hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7716
0
    }
7717
381
  else if (SGI_COMPAT (abfd)
7718
381
     && (strcmp (name, ".hash") == 0
7719
381
         || strcmp (name, ".dynamic") == 0
7720
381
         || strcmp (name, ".dynstr") == 0))
7721
27
    {
7722
27
      if (SGI_COMPAT (abfd))
7723
27
  hdr->sh_entsize = 0;
7724
#if 0
7725
      /* This isn't how the IRIX6 linker behaves.  */
7726
      hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7727
#endif
7728
27
    }
7729
354
  else if (strcmp (name, ".got") == 0
7730
354
     || strcmp (name, ".srdata") == 0
7731
354
     || strcmp (name, ".sdata") == 0
7732
354
     || strcmp (name, ".sbss") == 0
7733
354
     || strcmp (name, ".lit4") == 0
7734
354
     || strcmp (name, ".lit8") == 0)
7735
18
    hdr->sh_flags |= SHF_MIPS_GPREL;
7736
336
  else if (strcmp (name, ".MIPS.interfaces") == 0)
7737
0
    {
7738
0
      hdr->sh_type = SHT_MIPS_IFACE;
7739
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7740
0
    }
7741
336
  else if (startswith (name, ".MIPS.content"))
7742
0
    {
7743
0
      hdr->sh_type = SHT_MIPS_CONTENT;
7744
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7745
      /* The sh_info field is set in final_write_processing.  */
7746
0
    }
7747
336
  else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7748
0
    {
7749
0
      hdr->sh_type = SHT_MIPS_OPTIONS;
7750
0
      hdr->sh_entsize = 1;
7751
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7752
0
    }
7753
336
  else if (startswith (name, ".MIPS.abiflags"))
7754
0
    {
7755
0
      hdr->sh_type = SHT_MIPS_ABIFLAGS;
7756
0
      hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7757
0
    }
7758
336
  else if (startswith (name, ".debug_")
7759
336
     || startswith (name, ".gnu.debuglto_.debug_")
7760
336
     || startswith (name, ".zdebug_")
7761
336
     || startswith (name, ".gnu.debuglto_.zdebug_"))
7762
77
    {
7763
77
      hdr->sh_type = SHT_MIPS_DWARF;
7764
7765
      /* Irix facilities such as libexc expect a single .debug_frame
7766
   per executable, the system ones have NOSTRIP set and the linker
7767
   doesn't merge sections with different flags so ...  */
7768
77
      if (SGI_COMPAT (abfd) && startswith (name, ".debug_frame"))
7769
11
  hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7770
77
    }
7771
259
  else if (strcmp (name, ".MIPS.symlib") == 0)
7772
0
    {
7773
0
      hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7774
      /* The sh_link and sh_info fields are set in
7775
   final_write_processing.  */
7776
0
    }
7777
259
  else if (startswith (name, ".MIPS.events")
7778
259
     || startswith (name, ".MIPS.post_rel"))
7779
0
    {
7780
0
      hdr->sh_type = SHT_MIPS_EVENTS;
7781
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7782
      /* The sh_link field is set in final_write_processing.  */
7783
0
    }
7784
259
  else if (strcmp (name, ".msym") == 0)
7785
0
    {
7786
0
      hdr->sh_type = SHT_MIPS_MSYM;
7787
0
      hdr->sh_flags |= SHF_ALLOC;
7788
0
      hdr->sh_entsize = 8;
7789
0
    }
7790
259
  else if (strcmp (name, ".MIPS.xhash") == 0)
7791
0
    {
7792
0
      hdr->sh_type = SHT_MIPS_XHASH;
7793
0
      hdr->sh_flags |= SHF_ALLOC;
7794
0
      hdr->sh_entsize = get_elf_backend_data(abfd)->s->arch_size == 64 ? 0 : 4;
7795
0
    }
7796
7797
  /* The generic elf_fake_sections will set up REL_HDR using the default
7798
   kind of relocations.  We used to set up a second header for the
7799
   non-default kind of relocations here, but only NewABI would use
7800
   these, and the IRIX ld doesn't like resulting empty RELA sections.
7801
   Thus we create those header only on demand now.  */
7802
7803
381
  return true;
7804
381
}
7805
7806
/* Given a BFD section, try to locate the corresponding ELF section
7807
   index.  This is used by both the 32-bit and the 64-bit ABI.
7808
   Actually, it's not clear to me that the 64-bit ABI supports these,
7809
   but for non-PIC objects we will certainly want support for at least
7810
   the .scommon section.  */
7811
7812
bool
7813
_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7814
          asection *sec, int *retval)
7815
1.28k
{
7816
1.28k
  if (strcmp (bfd_section_name (sec), ".scommon") == 0)
7817
1
    {
7818
1
      *retval = SHN_MIPS_SCOMMON;
7819
1
      return true;
7820
1
    }
7821
1.28k
  if (strcmp (bfd_section_name (sec), ".acommon") == 0)
7822
1
    {
7823
1
      *retval = SHN_MIPS_ACOMMON;
7824
1
      return true;
7825
1
    }
7826
1.28k
  return false;
7827
1.28k
}
7828

7829
/* Hook called by the linker routine which adds symbols from an object
7830
   file.  We must handle the special MIPS section numbers here.  */
7831
7832
bool
7833
_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7834
             Elf_Internal_Sym *sym, const char **namep,
7835
             flagword *flagsp ATTRIBUTE_UNUSED,
7836
             asection **secp, bfd_vma *valp)
7837
0
{
7838
0
  if (SGI_COMPAT (abfd)
7839
0
      && (abfd->flags & DYNAMIC) != 0
7840
0
      && strcmp (*namep, "_rld_new_interface") == 0)
7841
0
    {
7842
      /* Skip IRIX5 rld entry name.  */
7843
0
      *namep = NULL;
7844
0
      return true;
7845
0
    }
7846
7847
  /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7848
     a SECTION *ABS*.  This causes ld to think it can resolve _gp_disp
7849
     by setting a DT_NEEDED for the shared object.  Since _gp_disp is
7850
     a magic symbol resolved by the linker, we ignore this bogus definition
7851
     of _gp_disp.  New ABI objects do not suffer from this problem so this
7852
     is not done for them. */
7853
0
  if (!NEWABI_P(abfd)
7854
0
      && (sym->st_shndx == SHN_ABS)
7855
0
      && (strcmp (*namep, "_gp_disp") == 0))
7856
0
    {
7857
0
      *namep = NULL;
7858
0
      return true;
7859
0
    }
7860
7861
0
  switch (sym->st_shndx)
7862
0
    {
7863
0
    case SHN_COMMON:
7864
      /* Common symbols less than the GP size are automatically
7865
   treated as SHN_MIPS_SCOMMON symbols.  */
7866
0
      if (sym->st_size > elf_gp_size (abfd)
7867
0
    || ELF_ST_TYPE (sym->st_info) == STT_TLS
7868
0
    || IRIX_COMPAT (abfd) == ict_irix6)
7869
0
  break;
7870
      /* Fall through.  */
7871
0
    case SHN_MIPS_SCOMMON:
7872
0
      *secp = bfd_make_section_old_way (abfd, ".scommon");
7873
0
      (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
7874
0
      *valp = sym->st_size;
7875
0
      break;
7876
7877
0
    case SHN_MIPS_TEXT:
7878
      /* This section is used in a shared object.  */
7879
0
      if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7880
0
  {
7881
0
    asymbol *elf_text_symbol;
7882
0
    asection *elf_text_section;
7883
0
    size_t amt = sizeof (asection);
7884
7885
0
    elf_text_section = bfd_zalloc (abfd, amt);
7886
0
    if (elf_text_section == NULL)
7887
0
      return false;
7888
7889
0
    amt = sizeof (asymbol);
7890
0
    elf_text_symbol = bfd_zalloc (abfd, amt);
7891
0
    if (elf_text_symbol == NULL)
7892
0
      return false;
7893
7894
    /* Initialize the section.  */
7895
7896
0
    mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7897
0
    mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
7898
7899
0
    elf_text_section->symbol = elf_text_symbol;
7900
0
    elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
7901
7902
0
    elf_text_section->name = ".text";
7903
0
    elf_text_section->flags = SEC_NO_FLAGS;
7904
0
    elf_text_section->output_section = NULL;
7905
0
    elf_text_section->owner = abfd;
7906
0
    elf_text_symbol->name = ".text";
7907
0
    elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7908
0
    elf_text_symbol->section = elf_text_section;
7909
0
  }
7910
      /* This code used to do *secp = bfd_und_section_ptr if
7911
   bfd_link_pic (info).  I don't know why, and that doesn't make sense,
7912
   so I took it out.  */
7913
0
      *secp = mips_elf_tdata (abfd)->elf_text_section;
7914
0
      break;
7915
7916
0
    case SHN_MIPS_ACOMMON:
7917
      /* Fall through. XXX Can we treat this as allocated data?  */
7918
0
    case SHN_MIPS_DATA:
7919
      /* This section is used in a shared object.  */
7920
0
      if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7921
0
  {
7922
0
    asymbol *elf_data_symbol;
7923
0
    asection *elf_data_section;
7924
0
    size_t amt = sizeof (asection);
7925
7926
0
    elf_data_section = bfd_zalloc (abfd, amt);
7927
0
    if (elf_data_section == NULL)
7928
0
      return false;
7929
7930
0
    amt = sizeof (asymbol);
7931
0
    elf_data_symbol = bfd_zalloc (abfd, amt);
7932
0
    if (elf_data_symbol == NULL)
7933
0
      return false;
7934
7935
    /* Initialize the section.  */
7936
7937
0
    mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7938
0
    mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7939
7940
0
    elf_data_section->symbol = elf_data_symbol;
7941
0
    elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
7942
7943
0
    elf_data_section->name = ".data";
7944
0
    elf_data_section->flags = SEC_NO_FLAGS;
7945
0
    elf_data_section->output_section = NULL;
7946
0
    elf_data_section->owner = abfd;
7947
0
    elf_data_symbol->name = ".data";
7948
0
    elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7949
0
    elf_data_symbol->section = elf_data_section;
7950
0
  }
7951
      /* This code used to do *secp = bfd_und_section_ptr if
7952
   bfd_link_pic (info).  I don't know why, and that doesn't make sense,
7953
   so I took it out.  */
7954
0
      *secp = mips_elf_tdata (abfd)->elf_data_section;
7955
0
      break;
7956
7957
0
    case SHN_MIPS_SUNDEFINED:
7958
0
      *secp = bfd_und_section_ptr;
7959
0
      break;
7960
0
    }
7961
7962
0
  if (SGI_COMPAT (abfd)
7963
0
      && ! bfd_link_pic (info)
7964
0
      && info->output_bfd->xvec == abfd->xvec
7965
0
      && strcmp (*namep, "__rld_obj_head") == 0)
7966
0
    {
7967
0
      struct elf_link_hash_entry *h;
7968
0
      struct bfd_link_hash_entry *bh;
7969
7970
      /* Mark __rld_obj_head as dynamic.  */
7971
0
      bh = NULL;
7972
0
      if (! (_bfd_generic_link_add_one_symbol
7973
0
       (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, false,
7974
0
        get_elf_backend_data (abfd)->collect, &bh)))
7975
0
  return false;
7976
7977
0
      h = (struct elf_link_hash_entry *) bh;
7978
0
      h->non_elf = 0;
7979
0
      h->def_regular = 1;
7980
0
      h->type = STT_OBJECT;
7981
7982
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
7983
0
  return false;
7984
7985
0
      mips_elf_hash_table (info)->use_rld_obj_head = true;
7986
0
      mips_elf_hash_table (info)->rld_symbol = h;
7987
0
    }
7988
7989
  /* If this is a mips16 text symbol, add 1 to the value to make it
7990
     odd.  This will cause something like .word SYM to come up with
7991
     the right value when it is loaded into the PC.  */
7992
0
  if (ELF_ST_IS_COMPRESSED (sym->st_other))
7993
0
    ++*valp;
7994
7995
0
  return true;
7996
0
}
7997
7998
/* This hook function is called before the linker writes out a global
7999
   symbol.  We mark symbols as small common if appropriate.  This is
8000
   also where we undo the increment of the value for a mips16 symbol.  */
8001
8002
int
8003
_bfd_mips_elf_link_output_symbol_hook
8004
  (struct bfd_link_info *info ATTRIBUTE_UNUSED,
8005
   const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
8006
   asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
8007
0
{
8008
  /* If we see a common symbol, which implies a relocatable link, then
8009
     if a symbol was small common in an input file, mark it as small
8010
     common in the output file.  */
8011
0
  if (sym->st_shndx == SHN_COMMON
8012
0
      && strcmp (input_sec->name, ".scommon") == 0)
8013
0
    sym->st_shndx = SHN_MIPS_SCOMMON;
8014
8015
0
  if (ELF_ST_IS_COMPRESSED (sym->st_other))
8016
0
    sym->st_value &= ~1;
8017
8018
0
  return 1;
8019
0
}
8020

8021
/* Functions for the dynamic linker.  */
8022
8023
/* Create dynamic sections when linking against a dynamic object.  */
8024
8025
bool
8026
_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
8027
0
{
8028
0
  struct elf_link_hash_entry *h;
8029
0
  struct bfd_link_hash_entry *bh;
8030
0
  flagword flags;
8031
0
  register asection *s;
8032
0
  const char * const *namep;
8033
0
  struct mips_elf_link_hash_table *htab;
8034
8035
0
  htab = mips_elf_hash_table (info);
8036
0
  BFD_ASSERT (htab != NULL);
8037
8038
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
8039
0
     | SEC_LINKER_CREATED | SEC_READONLY);
8040
8041
  /* The psABI requires a read-only .dynamic section, but the VxWorks
8042
     EABI doesn't.  */
8043
0
  if (htab->root.target_os != is_vxworks)
8044
0
    {
8045
0
      s = bfd_get_linker_section (abfd, ".dynamic");
8046
0
      if (s != NULL)
8047
0
  {
8048
0
    if (!bfd_set_section_flags (s, flags))
8049
0
      return false;
8050
0
  }
8051
0
    }
8052
8053
  /* We need to create .got section.  */
8054
0
  if (!mips_elf_create_got_section (abfd, info))
8055
0
    return false;
8056
8057
0
  if (! mips_elf_rel_dyn_section (info, true))
8058
0
    return false;
8059
8060
  /* Create .stub section.  */
8061
0
  s = bfd_make_section_anyway_with_flags (abfd,
8062
0
            MIPS_ELF_STUB_SECTION_NAME (abfd),
8063
0
            flags | SEC_CODE);
8064
0
  if (s == NULL
8065
0
      || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8066
0
    return false;
8067
0
  htab->sstubs = s;
8068
8069
0
  if (!mips_elf_hash_table (info)->use_rld_obj_head
8070
0
      && bfd_link_executable (info)
8071
0
      && bfd_get_linker_section (abfd, ".rld_map") == NULL)
8072
0
    {
8073
0
      s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
8074
0
                flags &~ (flagword) SEC_READONLY);
8075
0
      if (s == NULL
8076
0
    || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8077
0
  return false;
8078
0
    }
8079
8080
  /* Create .MIPS.xhash section.  */
8081
0
  if (info->emit_gnu_hash)
8082
0
    s = bfd_make_section_anyway_with_flags (abfd, ".MIPS.xhash",
8083
0
              flags | SEC_READONLY);
8084
8085
  /* On IRIX5, we adjust add some additional symbols and change the
8086
     alignments of several sections.  There is no ABI documentation
8087
     indicating that this is necessary on IRIX6, nor any evidence that
8088
     the linker takes such action.  */
8089
0
  if (IRIX_COMPAT (abfd) == ict_irix5)
8090
0
    {
8091
0
      for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
8092
0
  {
8093
0
    bh = NULL;
8094
0
    if (! (_bfd_generic_link_add_one_symbol
8095
0
     (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
8096
0
      NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8097
0
      return false;
8098
8099
0
    h = (struct elf_link_hash_entry *) bh;
8100
0
    h->mark = 1;
8101
0
    h->non_elf = 0;
8102
0
    h->def_regular = 1;
8103
0
    h->type = STT_SECTION;
8104
8105
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
8106
0
      return false;
8107
0
  }
8108
8109
      /* We need to create a .compact_rel section.  */
8110
0
      if (SGI_COMPAT (abfd))
8111
0
  {
8112
0
    if (!mips_elf_create_compact_rel_section (abfd, info))
8113
0
      return false;
8114
0
  }
8115
8116
      /* Change alignments of some sections.  */
8117
0
      s = bfd_get_linker_section (abfd, ".hash");
8118
0
      if (s != NULL)
8119
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8120
8121
0
      s = bfd_get_linker_section (abfd, ".dynsym");
8122
0
      if (s != NULL)
8123
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8124
8125
0
      s = bfd_get_linker_section (abfd, ".dynstr");
8126
0
      if (s != NULL)
8127
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8128
8129
      /* ??? */
8130
0
      s = bfd_get_section_by_name (abfd, ".reginfo");
8131
0
      if (s != NULL)
8132
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8133
8134
0
      s = bfd_get_linker_section (abfd, ".dynamic");
8135
0
      if (s != NULL)
8136
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8137
0
    }
8138
8139
0
  if (bfd_link_executable (info))
8140
0
    {
8141
0
      const char *name;
8142
8143
0
      name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
8144
0
      bh = NULL;
8145
0
      if (!(_bfd_generic_link_add_one_symbol
8146
0
      (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
8147
0
       NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8148
0
  return false;
8149
8150
0
      h = (struct elf_link_hash_entry *) bh;
8151
0
      h->non_elf = 0;
8152
0
      h->def_regular = 1;
8153
0
      h->type = STT_SECTION;
8154
8155
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
8156
0
  return false;
8157
8158
0
      if (! mips_elf_hash_table (info)->use_rld_obj_head)
8159
0
  {
8160
    /* __rld_map is a four byte word located in the .data section
8161
       and is filled in by the rtld to contain a pointer to
8162
       the _r_debug structure. Its symbol value will be set in
8163
       _bfd_mips_elf_finish_dynamic_symbol.  */
8164
0
    s = bfd_get_linker_section (abfd, ".rld_map");
8165
0
    BFD_ASSERT (s != NULL);
8166
8167
0
    name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
8168
0
    bh = NULL;
8169
0
    if (!(_bfd_generic_link_add_one_symbol
8170
0
    (info, abfd, name, BSF_GLOBAL, s, 0, NULL, false,
8171
0
     get_elf_backend_data (abfd)->collect, &bh)))
8172
0
      return false;
8173
8174
0
    h = (struct elf_link_hash_entry *) bh;
8175
0
    h->non_elf = 0;
8176
0
    h->def_regular = 1;
8177
0
    h->type = STT_OBJECT;
8178
8179
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
8180
0
      return false;
8181
0
    mips_elf_hash_table (info)->rld_symbol = h;
8182
0
  }
8183
0
    }
8184
8185
  /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
8186
     Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol.  */
8187
0
  if (!_bfd_elf_create_dynamic_sections (abfd, info))
8188
0
    return false;
8189
8190
  /* Do the usual VxWorks handling.  */
8191
0
  if (htab->root.target_os == is_vxworks
8192
0
      && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
8193
0
    return false;
8194
8195
0
  return true;
8196
0
}
8197

8198
/* Return true if relocation REL against section SEC is a REL rather than
8199
   RELA relocation.  RELOCS is the first relocation in the section and
8200
   ABFD is the bfd that contains SEC.  */
8201
8202
static bool
8203
mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
8204
         const Elf_Internal_Rela *relocs,
8205
         const Elf_Internal_Rela *rel)
8206
0
{
8207
0
  Elf_Internal_Shdr *rel_hdr;
8208
0
  const struct elf_backend_data *bed;
8209
8210
  /* To determine which flavor of relocation this is, we depend on the
8211
     fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR.  */
8212
0
  rel_hdr = elf_section_data (sec)->rel.hdr;
8213
0
  if (rel_hdr == NULL)
8214
0
    return false;
8215
0
  bed = get_elf_backend_data (abfd);
8216
0
  return ((size_t) (rel - relocs)
8217
0
    < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
8218
0
}
8219
8220
/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
8221
   HOWTO is the relocation's howto and CONTENTS points to the contents
8222
   of the section that REL is against.  */
8223
8224
static bfd_vma
8225
mips_elf_read_rel_addend (bfd *abfd, asection *sec,
8226
        const Elf_Internal_Rela *rel,
8227
        reloc_howto_type *howto, bfd_byte *contents)
8228
0
{
8229
0
  bfd_byte *location;
8230
0
  unsigned int r_type;
8231
0
  bfd_vma addend;
8232
0
  bfd_vma bytes;
8233
8234
0
  if (!bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8235
0
    return 0;
8236
8237
0
  r_type = ELF_R_TYPE (abfd, rel->r_info);
8238
0
  location = contents + rel->r_offset;
8239
8240
  /* Get the addend, which is stored in the input file.  */
8241
0
  _bfd_mips_elf_reloc_unshuffle (abfd, r_type, false, location);
8242
0
  bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
8243
0
  _bfd_mips_elf_reloc_shuffle (abfd, r_type, false, location);
8244
8245
0
  addend = bytes & howto->src_mask;
8246
8247
  /* Shift is 2, unusually, for microMIPS JALX.  Adjust the addend
8248
     accordingly.  */
8249
0
  if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
8250
0
    addend <<= 1;
8251
8252
0
  return addend;
8253
0
}
8254
8255
/* REL is a relocation in ABFD that needs a partnering LO16 relocation
8256
   and *ADDEND is the addend for REL itself.  Look for the LO16 relocation
8257
   and update *ADDEND with the final addend.  Return true on success
8258
   or false if the LO16 could not be found.  RELEND is the exclusive
8259
   upper bound on the relocations for REL's section.  */
8260
8261
static bool
8262
mips_elf_add_lo16_rel_addend (bfd *abfd,
8263
            asection *sec,
8264
            const Elf_Internal_Rela *rel,
8265
            const Elf_Internal_Rela *relend,
8266
            bfd_byte *contents, bfd_vma *addend)
8267
0
{
8268
0
  unsigned int r_type, lo16_type;
8269
0
  const Elf_Internal_Rela *lo16_relocation;
8270
0
  reloc_howto_type *lo16_howto;
8271
0
  bfd_vma l;
8272
8273
0
  r_type = ELF_R_TYPE (abfd, rel->r_info);
8274
0
  if (mips16_reloc_p (r_type))
8275
0
    lo16_type = R_MIPS16_LO16;
8276
0
  else if (micromips_reloc_p (r_type))
8277
0
    lo16_type = R_MICROMIPS_LO16;
8278
0
  else if (r_type == R_MIPS_PCHI16)
8279
0
    lo16_type = R_MIPS_PCLO16;
8280
0
  else
8281
0
    lo16_type = R_MIPS_LO16;
8282
8283
  /* The combined value is the sum of the HI16 addend, left-shifted by
8284
     sixteen bits, and the LO16 addend, sign extended.  (Usually, the
8285
     code does a `lui' of the HI16 value, and then an `addiu' of the
8286
     LO16 value.)
8287
8288
     Scan ahead to find a matching LO16 relocation.
8289
8290
     According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
8291
     be immediately following.  However, for the IRIX6 ABI, the next
8292
     relocation may be a composed relocation consisting of several
8293
     relocations for the same address.  In that case, the R_MIPS_LO16
8294
     relocation may occur as one of these.  We permit a similar
8295
     extension in general, as that is useful for GCC.
8296
8297
     In some cases GCC dead code elimination removes the LO16 but keeps
8298
     the corresponding HI16.  This is strictly speaking a violation of
8299
     the ABI but not immediately harmful.  */
8300
0
  lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
8301
0
  if (lo16_relocation == NULL)
8302
0
    return false;
8303
8304
  /* Obtain the addend kept there.  */
8305
0
  lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, false);
8306
0
  l = mips_elf_read_rel_addend (abfd, sec, lo16_relocation, lo16_howto,
8307
0
        contents);
8308
8309
0
  l <<= lo16_howto->rightshift;
8310
0
  l = _bfd_mips_elf_sign_extend (l, 16);
8311
8312
0
  *addend <<= 16;
8313
0
  *addend += l;
8314
0
  return true;
8315
0
}
8316
8317
/* Try to read the contents of section SEC in bfd ABFD.  Return true and
8318
   store the contents in *CONTENTS on success.  Assume that *CONTENTS
8319
   already holds the contents if it is nonull on entry.  */
8320
8321
static bool
8322
mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
8323
0
{
8324
0
  if (*contents)
8325
0
    return true;
8326
8327
  /* Get cached copy if it exists.  */
8328
0
  if (elf_section_data (sec)->this_hdr.contents != NULL)
8329
0
    {
8330
0
      *contents = elf_section_data (sec)->this_hdr.contents;
8331
0
      return true;
8332
0
    }
8333
8334
0
  return bfd_malloc_and_get_section (abfd, sec, contents);
8335
0
}
8336
8337
/* Make a new PLT record to keep internal data.  */
8338
8339
static struct plt_entry *
8340
mips_elf_make_plt_record (bfd *abfd)
8341
0
{
8342
0
  struct plt_entry *entry;
8343
8344
0
  entry = bfd_zalloc (abfd, sizeof (*entry));
8345
0
  if (entry == NULL)
8346
0
    return NULL;
8347
8348
0
  entry->stub_offset = MINUS_ONE;
8349
0
  entry->mips_offset = MINUS_ONE;
8350
0
  entry->comp_offset = MINUS_ONE;
8351
0
  entry->gotplt_index = MINUS_ONE;
8352
0
  return entry;
8353
0
}
8354
8355
/* Define the special `__gnu_absolute_zero' symbol.  We only need this
8356
   for PIC code, as otherwise there is no load-time relocation involved
8357
   and local GOT entries whose value is zero at static link time will
8358
   retain their value at load time.  */
8359
8360
static bool
8361
mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
8362
             struct mips_elf_link_hash_table *htab,
8363
             unsigned int r_type)
8364
0
{
8365
0
  union
8366
0
    {
8367
0
      struct elf_link_hash_entry *eh;
8368
0
      struct bfd_link_hash_entry *bh;
8369
0
    }
8370
0
  hzero;
8371
8372
0
  BFD_ASSERT (!htab->use_absolute_zero);
8373
0
  BFD_ASSERT (bfd_link_pic (info));
8374
8375
0
  hzero.bh = NULL;
8376
0
  if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
8377
0
           BSF_GLOBAL, bfd_abs_section_ptr, 0,
8378
0
           NULL, false, false, &hzero.bh))
8379
0
    return false;
8380
8381
0
  BFD_ASSERT (hzero.bh != NULL);
8382
0
  hzero.eh->size = 0;
8383
0
  hzero.eh->type = STT_NOTYPE;
8384
0
  hzero.eh->other = STV_PROTECTED;
8385
0
  hzero.eh->def_regular = 1;
8386
0
  hzero.eh->non_elf = 0;
8387
8388
0
  if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, true, r_type))
8389
0
    return false;
8390
8391
0
  htab->use_absolute_zero = true;
8392
8393
0
  return true;
8394
0
}
8395
8396
/* Look through the relocs for a section during the first phase, and
8397
   allocate space in the global offset table and record the need for
8398
   standard MIPS and compressed procedure linkage table entries.  */
8399
8400
bool
8401
_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
8402
          asection *sec, const Elf_Internal_Rela *relocs)
8403
0
{
8404
0
  const char *name;
8405
0
  bfd *dynobj;
8406
0
  Elf_Internal_Shdr *symtab_hdr;
8407
0
  struct elf_link_hash_entry **sym_hashes;
8408
0
  size_t extsymoff;
8409
0
  const Elf_Internal_Rela *rel;
8410
0
  const Elf_Internal_Rela *rel_end;
8411
0
  asection *sreloc;
8412
0
  const struct elf_backend_data *bed;
8413
0
  struct mips_elf_link_hash_table *htab;
8414
0
  bfd_byte *contents;
8415
0
  bfd_vma addend;
8416
0
  reloc_howto_type *howto;
8417
8418
0
  if (bfd_link_relocatable (info))
8419
0
    return true;
8420
8421
0
  htab = mips_elf_hash_table (info);
8422
0
  BFD_ASSERT (htab != NULL);
8423
8424
0
  dynobj = elf_hash_table (info)->dynobj;
8425
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8426
0
  sym_hashes = elf_sym_hashes (abfd);
8427
0
  extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8428
8429
0
  bed = get_elf_backend_data (abfd);
8430
0
  rel_end = relocs + sec->reloc_count;
8431
8432
  /* Check for the mips16 stub sections.  */
8433
8434
0
  name = bfd_section_name (sec);
8435
0
  if (FN_STUB_P (name))
8436
0
    {
8437
0
      unsigned long r_symndx;
8438
8439
      /* Look at the relocation information to figure out which symbol
8440
   this is for.  */
8441
8442
0
      r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8443
0
      if (r_symndx == 0)
8444
0
  {
8445
0
    _bfd_error_handler
8446
      /* xgettext:c-format */
8447
0
      (_("%pB: warning: cannot determine the target function for"
8448
0
         " stub section `%s'"),
8449
0
       abfd, name);
8450
0
    bfd_set_error (bfd_error_bad_value);
8451
0
    return false;
8452
0
  }
8453
8454
0
      if (r_symndx < extsymoff
8455
0
    || sym_hashes[r_symndx - extsymoff] == NULL)
8456
0
  {
8457
0
    asection *o;
8458
8459
    /* This stub is for a local symbol.  This stub will only be
8460
       needed if there is some relocation in this BFD, other
8461
       than a 16 bit function call, which refers to this symbol.  */
8462
0
    for (o = abfd->sections; o != NULL; o = o->next)
8463
0
      {
8464
0
        Elf_Internal_Rela *sec_relocs;
8465
0
        const Elf_Internal_Rela *r, *rend;
8466
8467
        /* We can ignore stub sections when looking for relocs.  */
8468
0
        if ((o->flags & SEC_RELOC) == 0
8469
0
      || o->reloc_count == 0
8470
0
      || section_allows_mips16_refs_p (o))
8471
0
    continue;
8472
8473
0
        sec_relocs
8474
0
    = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8475
0
               info->keep_memory);
8476
0
        if (sec_relocs == NULL)
8477
0
    return false;
8478
8479
0
        rend = sec_relocs + o->reloc_count;
8480
0
        for (r = sec_relocs; r < rend; r++)
8481
0
    if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8482
0
        && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
8483
0
      break;
8484
8485
0
        if (elf_section_data (o)->relocs != sec_relocs)
8486
0
    free (sec_relocs);
8487
8488
0
        if (r < rend)
8489
0
    break;
8490
0
      }
8491
8492
0
    if (o == NULL)
8493
0
      {
8494
        /* There is no non-call reloc for this stub, so we do
8495
     not need it.  Since this function is called before
8496
     the linker maps input sections to output sections, we
8497
     can easily discard it by setting the SEC_EXCLUDE
8498
     flag.  */
8499
0
        sec->flags |= SEC_EXCLUDE;
8500
0
        return true;
8501
0
      }
8502
8503
    /* Record this stub in an array of local symbol stubs for
8504
       this BFD.  */
8505
0
    if (mips_elf_tdata (abfd)->local_stubs == NULL)
8506
0
      {
8507
0
        unsigned long symcount;
8508
0
        asection **n;
8509
0
        bfd_size_type amt;
8510
8511
0
        if (elf_bad_symtab (abfd))
8512
0
    symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8513
0
        else
8514
0
    symcount = symtab_hdr->sh_info;
8515
0
        amt = symcount * sizeof (asection *);
8516
0
        n = bfd_zalloc (abfd, amt);
8517
0
        if (n == NULL)
8518
0
    return false;
8519
0
        mips_elf_tdata (abfd)->local_stubs = n;
8520
0
      }
8521
8522
0
    sec->flags |= SEC_KEEP;
8523
0
    mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
8524
8525
    /* We don't need to set mips16_stubs_seen in this case.
8526
       That flag is used to see whether we need to look through
8527
       the global symbol table for stubs.  We don't need to set
8528
       it here, because we just have a local stub.  */
8529
0
  }
8530
0
      else
8531
0
  {
8532
0
    struct mips_elf_link_hash_entry *h;
8533
8534
0
    h = ((struct mips_elf_link_hash_entry *)
8535
0
         sym_hashes[r_symndx - extsymoff]);
8536
8537
0
    while (h->root.root.type == bfd_link_hash_indirect
8538
0
     || h->root.root.type == bfd_link_hash_warning)
8539
0
      h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8540
8541
    /* H is the symbol this stub is for.  */
8542
8543
    /* If we already have an appropriate stub for this function, we
8544
       don't need another one, so we can discard this one.  Since
8545
       this function is called before the linker maps input sections
8546
       to output sections, we can easily discard it by setting the
8547
       SEC_EXCLUDE flag.  */
8548
0
    if (h->fn_stub != NULL)
8549
0
      {
8550
0
        sec->flags |= SEC_EXCLUDE;
8551
0
        return true;
8552
0
      }
8553
8554
0
    sec->flags |= SEC_KEEP;
8555
0
    h->fn_stub = sec;
8556
0
    mips_elf_hash_table (info)->mips16_stubs_seen = true;
8557
0
  }
8558
0
    }
8559
0
  else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
8560
0
    {
8561
0
      unsigned long r_symndx;
8562
0
      struct mips_elf_link_hash_entry *h;
8563
0
      asection **loc;
8564
8565
      /* Look at the relocation information to figure out which symbol
8566
   this is for.  */
8567
8568
0
      r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8569
0
      if (r_symndx == 0)
8570
0
  {
8571
0
    _bfd_error_handler
8572
      /* xgettext:c-format */
8573
0
      (_("%pB: warning: cannot determine the target function for"
8574
0
         " stub section `%s'"),
8575
0
       abfd, name);
8576
0
    bfd_set_error (bfd_error_bad_value);
8577
0
    return false;
8578
0
  }
8579
8580
0
      if (r_symndx < extsymoff
8581
0
    || sym_hashes[r_symndx - extsymoff] == NULL)
8582
0
  {
8583
0
    asection *o;
8584
8585
    /* This stub is for a local symbol.  This stub will only be
8586
       needed if there is some relocation (R_MIPS16_26) in this BFD
8587
       that refers to this symbol.  */
8588
0
    for (o = abfd->sections; o != NULL; o = o->next)
8589
0
      {
8590
0
        Elf_Internal_Rela *sec_relocs;
8591
0
        const Elf_Internal_Rela *r, *rend;
8592
8593
        /* We can ignore stub sections when looking for relocs.  */
8594
0
        if ((o->flags & SEC_RELOC) == 0
8595
0
      || o->reloc_count == 0
8596
0
      || section_allows_mips16_refs_p (o))
8597
0
    continue;
8598
8599
0
        sec_relocs
8600
0
    = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8601
0
               info->keep_memory);
8602
0
        if (sec_relocs == NULL)
8603
0
    return false;
8604
8605
0
        rend = sec_relocs + o->reloc_count;
8606
0
        for (r = sec_relocs; r < rend; r++)
8607
0
    if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8608
0
        && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
8609
0
        break;
8610
8611
0
        if (elf_section_data (o)->relocs != sec_relocs)
8612
0
    free (sec_relocs);
8613
8614
0
        if (r < rend)
8615
0
    break;
8616
0
      }
8617
8618
0
    if (o == NULL)
8619
0
      {
8620
        /* There is no non-call reloc for this stub, so we do
8621
     not need it.  Since this function is called before
8622
     the linker maps input sections to output sections, we
8623
     can easily discard it by setting the SEC_EXCLUDE
8624
     flag.  */
8625
0
        sec->flags |= SEC_EXCLUDE;
8626
0
        return true;
8627
0
      }
8628
8629
    /* Record this stub in an array of local symbol call_stubs for
8630
       this BFD.  */
8631
0
    if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
8632
0
      {
8633
0
        unsigned long symcount;
8634
0
        asection **n;
8635
0
        bfd_size_type amt;
8636
8637
0
        if (elf_bad_symtab (abfd))
8638
0
    symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8639
0
        else
8640
0
    symcount = symtab_hdr->sh_info;
8641
0
        amt = symcount * sizeof (asection *);
8642
0
        n = bfd_zalloc (abfd, amt);
8643
0
        if (n == NULL)
8644
0
    return false;
8645
0
        mips_elf_tdata (abfd)->local_call_stubs = n;
8646
0
      }
8647
8648
0
    sec->flags |= SEC_KEEP;
8649
0
    mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
8650
8651
    /* We don't need to set mips16_stubs_seen in this case.
8652
       That flag is used to see whether we need to look through
8653
       the global symbol table for stubs.  We don't need to set
8654
       it here, because we just have a local stub.  */
8655
0
  }
8656
0
      else
8657
0
  {
8658
0
    h = ((struct mips_elf_link_hash_entry *)
8659
0
         sym_hashes[r_symndx - extsymoff]);
8660
8661
    /* H is the symbol this stub is for.  */
8662
8663
0
    if (CALL_FP_STUB_P (name))
8664
0
      loc = &h->call_fp_stub;
8665
0
    else
8666
0
      loc = &h->call_stub;
8667
8668
    /* If we already have an appropriate stub for this function, we
8669
       don't need another one, so we can discard this one.  Since
8670
       this function is called before the linker maps input sections
8671
       to output sections, we can easily discard it by setting the
8672
       SEC_EXCLUDE flag.  */
8673
0
    if (*loc != NULL)
8674
0
      {
8675
0
        sec->flags |= SEC_EXCLUDE;
8676
0
        return true;
8677
0
      }
8678
8679
0
    sec->flags |= SEC_KEEP;
8680
0
    *loc = sec;
8681
0
    mips_elf_hash_table (info)->mips16_stubs_seen = true;
8682
0
  }
8683
0
    }
8684
8685
0
  sreloc = NULL;
8686
0
  contents = NULL;
8687
0
  for (rel = relocs; rel < rel_end; ++rel)
8688
0
    {
8689
0
      unsigned long r_symndx;
8690
0
      unsigned int r_type;
8691
0
      struct elf_link_hash_entry *h;
8692
0
      bool can_make_dynamic_p;
8693
0
      bool call_reloc_p;
8694
0
      bool constrain_symbol_p;
8695
8696
0
      r_symndx = ELF_R_SYM (abfd, rel->r_info);
8697
0
      r_type = ELF_R_TYPE (abfd, rel->r_info);
8698
8699
0
      if (r_symndx < extsymoff)
8700
0
  h = NULL;
8701
0
      else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
8702
0
  {
8703
0
    _bfd_error_handler
8704
      /* xgettext:c-format */
8705
0
      (_("%pB: malformed reloc detected for section %s"),
8706
0
       abfd, name);
8707
0
    bfd_set_error (bfd_error_bad_value);
8708
0
    return false;
8709
0
  }
8710
0
      else
8711
0
  {
8712
0
    h = sym_hashes[r_symndx - extsymoff];
8713
0
    if (h != NULL)
8714
0
      {
8715
0
        while (h->root.type == bfd_link_hash_indirect
8716
0
         || h->root.type == bfd_link_hash_warning)
8717
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
8718
0
      }
8719
0
  }
8720
8721
      /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8722
   relocation into a dynamic one.  */
8723
0
      can_make_dynamic_p = false;
8724
8725
      /* Set CALL_RELOC_P to true if the relocation is for a call,
8726
   and if pointer equality therefore doesn't matter.  */
8727
0
      call_reloc_p = false;
8728
8729
      /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8730
   into account when deciding how to define the symbol.  */
8731
0
      constrain_symbol_p = true;
8732
8733
0
      switch (r_type)
8734
0
  {
8735
0
  case R_MIPS_CALL16:
8736
0
  case R_MIPS_CALL_HI16:
8737
0
  case R_MIPS_CALL_LO16:
8738
0
  case R_MIPS16_CALL16:
8739
0
  case R_MICROMIPS_CALL16:
8740
0
  case R_MICROMIPS_CALL_HI16:
8741
0
  case R_MICROMIPS_CALL_LO16:
8742
0
    call_reloc_p = true;
8743
    /* Fall through.  */
8744
8745
0
  case R_MIPS_GOT16:
8746
0
  case R_MIPS_GOT_LO16:
8747
0
  case R_MIPS_GOT_PAGE:
8748
0
  case R_MIPS_GOT_DISP:
8749
0
  case R_MIPS16_GOT16:
8750
0
  case R_MICROMIPS_GOT16:
8751
0
  case R_MICROMIPS_GOT_LO16:
8752
0
  case R_MICROMIPS_GOT_PAGE:
8753
0
  case R_MICROMIPS_GOT_DISP:
8754
    /* If we have a symbol that will resolve to zero at static link
8755
       time and it is used by a GOT relocation applied to code we
8756
       cannot relax to an immediate zero load, then we will be using
8757
       the special `__gnu_absolute_zero' symbol whose value is zero
8758
       at dynamic load time.  We ignore HI16-type GOT relocations at
8759
       this stage, because their handling will depend entirely on
8760
       the corresponding LO16-type GOT relocation.  */
8761
0
    if (!call_hi16_reloc_p (r_type)
8762
0
        && h != NULL
8763
0
        && bfd_link_pic (info)
8764
0
        && !htab->use_absolute_zero
8765
0
        && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8766
0
      {
8767
0
        bool rel_reloc;
8768
8769
0
        if (!mips_elf_get_section_contents (abfd, sec, &contents))
8770
0
    return false;
8771
8772
0
        rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
8773
0
        howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
8774
0
        if (bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8775
0
    if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
8776
0
            false))
8777
0
      if (!mips_elf_define_absolute_zero (abfd, info, htab,
8778
0
                  r_type))
8779
0
        return false;
8780
0
      }
8781
8782
    /* Fall through.  */
8783
0
  case R_MIPS_GOT_HI16:
8784
0
  case R_MIPS_GOT_OFST:
8785
0
  case R_MIPS_TLS_GOTTPREL:
8786
0
  case R_MIPS_TLS_GD:
8787
0
  case R_MIPS_TLS_LDM:
8788
0
  case R_MIPS16_TLS_GOTTPREL:
8789
0
  case R_MIPS16_TLS_GD:
8790
0
  case R_MIPS16_TLS_LDM:
8791
0
  case R_MICROMIPS_GOT_HI16:
8792
0
  case R_MICROMIPS_GOT_OFST:
8793
0
  case R_MICROMIPS_TLS_GOTTPREL:
8794
0
  case R_MICROMIPS_TLS_GD:
8795
0
  case R_MICROMIPS_TLS_LDM:
8796
0
    if (dynobj == NULL)
8797
0
      elf_hash_table (info)->dynobj = dynobj = abfd;
8798
0
    if (!mips_elf_create_got_section (dynobj, info))
8799
0
      return false;
8800
0
    if (htab->root.target_os == is_vxworks
8801
0
        && !bfd_link_pic (info))
8802
0
      {
8803
0
        _bfd_error_handler
8804
    /* xgettext:c-format */
8805
0
    (_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
8806
0
     abfd, (uint64_t) rel->r_offset);
8807
0
        bfd_set_error (bfd_error_bad_value);
8808
0
        return false;
8809
0
      }
8810
0
    can_make_dynamic_p = true;
8811
0
    break;
8812
8813
0
  case R_MIPS_NONE:
8814
0
  case R_MIPS_JALR:
8815
0
  case R_MICROMIPS_JALR:
8816
    /* These relocations have empty fields and are purely there to
8817
       provide link information.  The symbol value doesn't matter.  */
8818
0
    constrain_symbol_p = false;
8819
0
    break;
8820
8821
0
  case R_MIPS_GPREL16:
8822
0
  case R_MIPS_GPREL32:
8823
0
  case R_MIPS16_GPREL:
8824
0
  case R_MICROMIPS_GPREL16:
8825
    /* GP-relative relocations always resolve to a definition in a
8826
       regular input file, ignoring the one-definition rule.  This is
8827
       important for the GP setup sequence in NewABI code, which
8828
       always resolves to a local function even if other relocations
8829
       against the symbol wouldn't.  */
8830
0
    constrain_symbol_p = false;
8831
0
    break;
8832
8833
0
  case R_MIPS_32:
8834
0
  case R_MIPS_REL32:
8835
0
  case R_MIPS_64:
8836
    /* In VxWorks executables, references to external symbols
8837
       must be handled using copy relocs or PLT entries; it is not
8838
       possible to convert this relocation into a dynamic one.
8839
8840
       For executables that use PLTs and copy-relocs, we have a
8841
       choice between converting the relocation into a dynamic
8842
       one or using copy relocations or PLT entries.  It is
8843
       usually better to do the former, unless the relocation is
8844
       against a read-only section.  */
8845
0
    if ((bfd_link_pic (info)
8846
0
         || (h != NULL
8847
0
       && htab->root.target_os != is_vxworks
8848
0
       && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8849
0
       && !(!info->nocopyreloc
8850
0
      && !PIC_OBJECT_P (abfd)
8851
0
      && MIPS_ELF_READONLY_SECTION (sec))))
8852
0
        && (sec->flags & SEC_ALLOC) != 0)
8853
0
      {
8854
0
        can_make_dynamic_p = true;
8855
0
        if (dynobj == NULL)
8856
0
    elf_hash_table (info)->dynobj = dynobj = abfd;
8857
0
      }
8858
0
    break;
8859
8860
0
  case R_MIPS_26:
8861
0
  case R_MIPS_PC16:
8862
0
  case R_MIPS_PC21_S2:
8863
0
  case R_MIPS_PC26_S2:
8864
0
  case R_MIPS16_26:
8865
0
  case R_MIPS16_PC16_S1:
8866
0
  case R_MICROMIPS_26_S1:
8867
0
  case R_MICROMIPS_PC7_S1:
8868
0
  case R_MICROMIPS_PC10_S1:
8869
0
  case R_MICROMIPS_PC16_S1:
8870
0
  case R_MICROMIPS_PC23_S2:
8871
0
    call_reloc_p = true;
8872
0
    break;
8873
0
  }
8874
8875
0
      if (h)
8876
0
  {
8877
0
    if (constrain_symbol_p)
8878
0
      {
8879
0
        if (!can_make_dynamic_p)
8880
0
    ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8881
8882
0
        if (!call_reloc_p)
8883
0
    h->pointer_equality_needed = 1;
8884
8885
        /* We must not create a stub for a symbol that has
8886
     relocations related to taking the function's address.
8887
     This doesn't apply to VxWorks, where CALL relocs refer
8888
     to a .got.plt entry instead of a normal .got entry.  */
8889
0
        if (htab->root.target_os != is_vxworks
8890
0
      && (!can_make_dynamic_p || !call_reloc_p))
8891
0
    ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = true;
8892
0
      }
8893
8894
    /* Relocations against the special VxWorks __GOTT_BASE__ and
8895
       __GOTT_INDEX__ symbols must be left to the loader.  Allocate
8896
       room for them in .rela.dyn.  */
8897
0
    if (is_gott_symbol (info, h))
8898
0
      {
8899
0
        if (sreloc == NULL)
8900
0
    {
8901
0
      sreloc = mips_elf_rel_dyn_section (info, true);
8902
0
      if (sreloc == NULL)
8903
0
        return false;
8904
0
    }
8905
0
        mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8906
0
        if (MIPS_ELF_READONLY_SECTION (sec))
8907
    /* We tell the dynamic linker that there are
8908
       relocations against the text segment.  */
8909
0
    info->flags |= DF_TEXTREL;
8910
0
      }
8911
0
  }
8912
0
      else if (call_lo16_reloc_p (r_type)
8913
0
         || got_lo16_reloc_p (r_type)
8914
0
         || got_disp_reloc_p (r_type)
8915
0
         || (got16_reloc_p (r_type)
8916
0
       && htab->root.target_os == is_vxworks))
8917
0
  {
8918
    /* We may need a local GOT entry for this relocation.  We
8919
       don't count R_MIPS_GOT_PAGE because we can estimate the
8920
       maximum number of pages needed by looking at the size of
8921
       the segment.  Similar comments apply to R_MIPS*_GOT16 and
8922
       R_MIPS*_CALL16, except on VxWorks, where GOT relocations
8923
       always evaluate to "G".  We don't count R_MIPS_GOT_HI16, or
8924
       R_MIPS_CALL_HI16 because these are always followed by an
8925
       R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.  */
8926
0
    if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8927
0
             rel->r_addend, info, r_type))
8928
0
      return false;
8929
0
  }
8930
8931
0
      if (h != NULL
8932
0
    && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8933
0
              ELF_ST_IS_MIPS16 (h->other)))
8934
0
  ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = true;
8935
8936
0
      switch (r_type)
8937
0
  {
8938
0
  case R_MIPS_CALL16:
8939
0
  case R_MIPS16_CALL16:
8940
0
  case R_MICROMIPS_CALL16:
8941
0
    if (h == NULL)
8942
0
      {
8943
0
        _bfd_error_handler
8944
    /* xgettext:c-format */
8945
0
    (_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
8946
0
     abfd, (uint64_t) rel->r_offset);
8947
0
        bfd_set_error (bfd_error_bad_value);
8948
0
        return false;
8949
0
      }
8950
    /* Fall through.  */
8951
8952
0
  case R_MIPS_CALL_HI16:
8953
0
  case R_MIPS_CALL_LO16:
8954
0
  case R_MICROMIPS_CALL_HI16:
8955
0
  case R_MICROMIPS_CALL_LO16:
8956
0
    if (h != NULL)
8957
0
      {
8958
        /* Make sure there is room in the regular GOT to hold the
8959
     function's address.  We may eliminate it in favour of
8960
     a .got.plt entry later; see mips_elf_count_got_symbols.  */
8961
0
        if (!mips_elf_record_global_got_symbol (h, abfd, info, true,
8962
0
                  r_type))
8963
0
    return false;
8964
8965
        /* We need a stub, not a plt entry for the undefined
8966
     function.  But we record it as if it needs plt.  See
8967
     _bfd_elf_adjust_dynamic_symbol.  */
8968
0
        h->needs_plt = 1;
8969
0
        h->type = STT_FUNC;
8970
0
      }
8971
0
    break;
8972
8973
0
  case R_MIPS_GOT_PAGE:
8974
0
  case R_MICROMIPS_GOT_PAGE:
8975
0
  case R_MIPS16_GOT16:
8976
0
  case R_MIPS_GOT16:
8977
0
  case R_MIPS_GOT_HI16:
8978
0
  case R_MIPS_GOT_LO16:
8979
0
  case R_MICROMIPS_GOT16:
8980
0
  case R_MICROMIPS_GOT_HI16:
8981
0
  case R_MICROMIPS_GOT_LO16:
8982
0
    if (!h || got_page_reloc_p (r_type))
8983
0
      {
8984
        /* This relocation needs (or may need, if h != NULL) a
8985
     page entry in the GOT.  For R_MIPS_GOT_PAGE we do not
8986
     know for sure until we know whether the symbol is
8987
     preemptible.  */
8988
0
        if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8989
0
    {
8990
0
      if (!mips_elf_get_section_contents (abfd, sec, &contents))
8991
0
        return false;
8992
0
      howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, false);
8993
0
      addend = mips_elf_read_rel_addend (abfd, sec, rel,
8994
0
                 howto, contents);
8995
0
      if (got16_reloc_p (r_type))
8996
0
        mips_elf_add_lo16_rel_addend (abfd, sec, rel, rel_end,
8997
0
              contents, &addend);
8998
0
      else
8999
0
        addend <<= howto->rightshift;
9000
0
    }
9001
0
        else
9002
0
    addend = rel->r_addend;
9003
0
        if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
9004
0
             h, addend))
9005
0
    return false;
9006
9007
0
        if (h)
9008
0
    {
9009
0
      struct mips_elf_link_hash_entry *hmips =
9010
0
        (struct mips_elf_link_hash_entry *) h;
9011
9012
      /* This symbol is definitely not overridable.  */
9013
0
      if (hmips->root.def_regular
9014
0
          && ! (bfd_link_pic (info) && ! info->symbolic
9015
0
          && ! hmips->root.forced_local))
9016
0
        h = NULL;
9017
0
    }
9018
0
      }
9019
    /* If this is a global, overridable symbol, GOT_PAGE will
9020
       decay to GOT_DISP, so we'll need a GOT entry for it.  */
9021
    /* Fall through.  */
9022
9023
0
  case R_MIPS_GOT_DISP:
9024
0
  case R_MICROMIPS_GOT_DISP:
9025
0
    if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
9026
0
                   false, r_type))
9027
0
      return false;
9028
0
    break;
9029
9030
0
  case R_MIPS_TLS_GOTTPREL:
9031
0
  case R_MIPS16_TLS_GOTTPREL:
9032
0
  case R_MICROMIPS_TLS_GOTTPREL:
9033
0
    if (bfd_link_pic (info))
9034
0
      info->flags |= DF_STATIC_TLS;
9035
    /* Fall through */
9036
9037
0
  case R_MIPS_TLS_LDM:
9038
0
  case R_MIPS16_TLS_LDM:
9039
0
  case R_MICROMIPS_TLS_LDM:
9040
0
    if (tls_ldm_reloc_p (r_type))
9041
0
      {
9042
0
        r_symndx = STN_UNDEF;
9043
0
        h = NULL;
9044
0
      }
9045
    /* Fall through */
9046
9047
0
  case R_MIPS_TLS_GD:
9048
0
  case R_MIPS16_TLS_GD:
9049
0
  case R_MICROMIPS_TLS_GD:
9050
    /* This symbol requires a global offset table entry, or two
9051
       for TLS GD relocations.  */
9052
0
    if (h != NULL)
9053
0
      {
9054
0
        if (!mips_elf_record_global_got_symbol (h, abfd, info,
9055
0
                  false, r_type))
9056
0
    return false;
9057
0
      }
9058
0
    else
9059
0
      {
9060
0
        if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
9061
0
                 rel->r_addend,
9062
0
                 info, r_type))
9063
0
    return false;
9064
0
      }
9065
0
    break;
9066
9067
0
  case R_MIPS_32:
9068
0
  case R_MIPS_REL32:
9069
0
  case R_MIPS_64:
9070
    /* In VxWorks executables, references to external symbols
9071
       are handled using copy relocs or PLT stubs, so there's
9072
       no need to add a .rela.dyn entry for this relocation.  */
9073
0
    if (can_make_dynamic_p)
9074
0
      {
9075
0
        if (sreloc == NULL)
9076
0
    {
9077
0
      sreloc = mips_elf_rel_dyn_section (info, true);
9078
0
      if (sreloc == NULL)
9079
0
        return false;
9080
0
    }
9081
0
        if (bfd_link_pic (info) && h == NULL)
9082
0
    {
9083
      /* When creating a shared object, we must copy these
9084
         reloc types into the output file as R_MIPS_REL32
9085
         relocs.  Make room for this reloc in .rel(a).dyn.  */
9086
0
      mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9087
0
      if (MIPS_ELF_READONLY_SECTION (sec))
9088
        /* We tell the dynamic linker that there are
9089
           relocations against the text segment.  */
9090
0
        info->flags |= DF_TEXTREL;
9091
0
    }
9092
0
        else
9093
0
    {
9094
0
      struct mips_elf_link_hash_entry *hmips;
9095
9096
      /* For a shared object, we must copy this relocation
9097
         unless the symbol turns out to be undefined and
9098
         weak with non-default visibility, in which case
9099
         it will be left as zero.
9100
9101
         We could elide R_MIPS_REL32 for locally binding symbols
9102
         in shared libraries, but do not yet do so.
9103
9104
         For an executable, we only need to copy this
9105
         reloc if the symbol is defined in a dynamic
9106
         object.  */
9107
0
      hmips = (struct mips_elf_link_hash_entry *) h;
9108
0
      ++hmips->possibly_dynamic_relocs;
9109
0
      if (MIPS_ELF_READONLY_SECTION (sec))
9110
        /* We need it to tell the dynamic linker if there
9111
           are relocations against the text segment.  */
9112
0
        hmips->readonly_reloc = true;
9113
0
    }
9114
0
      }
9115
9116
0
    if (SGI_COMPAT (abfd))
9117
0
      mips_elf_hash_table (info)->compact_rel_size +=
9118
0
        sizeof (Elf32_External_crinfo);
9119
0
    break;
9120
9121
0
  case R_MIPS_26:
9122
0
  case R_MIPS_GPREL16:
9123
0
  case R_MIPS_LITERAL:
9124
0
  case R_MIPS_GPREL32:
9125
0
  case R_MICROMIPS_26_S1:
9126
0
  case R_MICROMIPS_GPREL16:
9127
0
  case R_MICROMIPS_LITERAL:
9128
0
  case R_MICROMIPS_GPREL7_S2:
9129
0
    if (SGI_COMPAT (abfd))
9130
0
      mips_elf_hash_table (info)->compact_rel_size +=
9131
0
        sizeof (Elf32_External_crinfo);
9132
0
    break;
9133
9134
    /* This relocation describes the C++ object vtable hierarchy.
9135
       Reconstruct it for later use during GC.  */
9136
0
  case R_MIPS_GNU_VTINHERIT:
9137
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
9138
0
      return false;
9139
0
    break;
9140
9141
    /* This relocation describes which C++ vtable entries are actually
9142
       used.  Record for later use during GC.  */
9143
0
  case R_MIPS_GNU_VTENTRY:
9144
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
9145
0
      return false;
9146
0
    break;
9147
9148
0
  default:
9149
0
    break;
9150
0
  }
9151
9152
      /* Record the need for a PLT entry.  At this point we don't know
9153
   yet if we are going to create a PLT in the first place, but
9154
   we only record whether the relocation requires a standard MIPS
9155
   or a compressed code entry anyway.  If we don't make a PLT after
9156
   all, then we'll just ignore these arrangements.  Likewise if
9157
   a PLT entry is not created because the symbol is satisfied
9158
   locally.  */
9159
0
      if (h != NULL
9160
0
    && (branch_reloc_p (r_type)
9161
0
        || mips16_branch_reloc_p (r_type)
9162
0
        || micromips_branch_reloc_p (r_type))
9163
0
    && !SYMBOL_CALLS_LOCAL (info, h))
9164
0
  {
9165
0
    if (h->plt.plist == NULL)
9166
0
      h->plt.plist = mips_elf_make_plt_record (abfd);
9167
0
    if (h->plt.plist == NULL)
9168
0
      return false;
9169
9170
0
    if (branch_reloc_p (r_type))
9171
0
      h->plt.plist->need_mips = true;
9172
0
    else
9173
0
      h->plt.plist->need_comp = true;
9174
0
  }
9175
9176
      /* See if this reloc would need to refer to a MIPS16 hard-float stub,
9177
   if there is one.  We only need to handle global symbols here;
9178
   we decide whether to keep or delete stubs for local symbols
9179
   when processing the stub's relocations.  */
9180
0
      if (h != NULL
9181
0
    && !mips16_call_reloc_p (r_type)
9182
0
    && !section_allows_mips16_refs_p (sec))
9183
0
  {
9184
0
    struct mips_elf_link_hash_entry *mh;
9185
9186
0
    mh = (struct mips_elf_link_hash_entry *) h;
9187
0
    mh->need_fn_stub = true;
9188
0
  }
9189
9190
      /* Refuse some position-dependent relocations when creating a
9191
   shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
9192
   not PIC, but we can create dynamic relocations and the result
9193
   will be fine.  Also do not refuse R_MIPS_LO16, which can be
9194
   combined with R_MIPS_GOT16.  */
9195
0
      if (bfd_link_pic (info))
9196
0
  {
9197
0
    switch (r_type)
9198
0
      {
9199
0
      case R_MIPS_TLS_TPREL_HI16:
9200
0
      case R_MIPS16_TLS_TPREL_HI16:
9201
0
      case R_MICROMIPS_TLS_TPREL_HI16:
9202
0
      case R_MIPS_TLS_TPREL_LO16:
9203
0
      case R_MIPS16_TLS_TPREL_LO16:
9204
0
      case R_MICROMIPS_TLS_TPREL_LO16:
9205
        /* These are okay in PIE, but not in a shared library.  */
9206
0
        if (bfd_link_executable (info))
9207
0
    break;
9208
9209
        /* FALLTHROUGH */
9210
9211
0
      case R_MIPS16_HI16:
9212
0
      case R_MIPS_HI16:
9213
0
      case R_MIPS_HIGHER:
9214
0
      case R_MIPS_HIGHEST:
9215
0
      case R_MICROMIPS_HI16:
9216
0
      case R_MICROMIPS_HIGHER:
9217
0
      case R_MICROMIPS_HIGHEST:
9218
        /* Don't refuse a high part relocation if it's against
9219
     no symbol (e.g. part of a compound relocation).  */
9220
0
        if (r_symndx == STN_UNDEF)
9221
0
    break;
9222
9223
        /* Likewise an absolute symbol.  */
9224
0
        if (h != NULL && bfd_is_abs_symbol (&h->root))
9225
0
    break;
9226
9227
        /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
9228
     and has a special meaning.  */
9229
0
        if (!NEWABI_P (abfd) && h != NULL
9230
0
      && strcmp (h->root.root.string, "_gp_disp") == 0)
9231
0
    break;
9232
9233
        /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks.  */
9234
0
        if (is_gott_symbol (info, h))
9235
0
    break;
9236
9237
        /* FALLTHROUGH */
9238
9239
0
      case R_MIPS16_26:
9240
0
      case R_MIPS_26:
9241
0
      case R_MICROMIPS_26_S1:
9242
0
        howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
9243
        /* An error for unsupported relocations is raised as part
9244
     of the above search, so we can skip the following.  */
9245
0
        if (howto != NULL)
9246
0
    info->callbacks->einfo
9247
      /* xgettext:c-format */
9248
0
      (_("%X%H: relocation %s against `%s' cannot be used"
9249
0
         " when making a shared object; recompile with -fPIC\n"),
9250
0
       abfd, sec, rel->r_offset, howto->name,
9251
0
       (h) ? h->root.root.string : "a local symbol");
9252
0
        break;
9253
0
      default:
9254
0
        break;
9255
0
      }
9256
0
  }
9257
0
    }
9258
9259
0
  return true;
9260
0
}
9261

9262
/* Allocate space for global sym dynamic relocs.  */
9263
9264
static bool
9265
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9266
0
{
9267
0
  struct bfd_link_info *info = inf;
9268
0
  bfd *dynobj;
9269
0
  struct mips_elf_link_hash_entry *hmips;
9270
0
  struct mips_elf_link_hash_table *htab;
9271
9272
0
  htab = mips_elf_hash_table (info);
9273
0
  BFD_ASSERT (htab != NULL);
9274
9275
0
  dynobj = elf_hash_table (info)->dynobj;
9276
0
  hmips = (struct mips_elf_link_hash_entry *) h;
9277
9278
  /* VxWorks executables are handled elsewhere; we only need to
9279
     allocate relocations in shared objects.  */
9280
0
  if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9281
0
    return true;
9282
9283
  /* Ignore indirect symbols.  All relocations against such symbols
9284
     will be redirected to the target symbol.  */
9285
0
  if (h->root.type == bfd_link_hash_indirect)
9286
0
    return true;
9287
9288
  /* If this symbol is defined in a dynamic object, or we are creating
9289
     a shared library, we will need to copy any R_MIPS_32 or
9290
     R_MIPS_REL32 relocs against it into the output file.  */
9291
0
  if (! bfd_link_relocatable (info)
9292
0
      && hmips->possibly_dynamic_relocs != 0
9293
0
      && (h->root.type == bfd_link_hash_defweak
9294
0
    || (!h->def_regular && !ELF_COMMON_DEF_P (h))
9295
0
    || bfd_link_pic (info)))
9296
0
    {
9297
0
      bool do_copy = true;
9298
9299
0
      if (h->root.type == bfd_link_hash_undefweak)
9300
0
  {
9301
    /* Do not copy relocations for undefined weak symbols that
9302
       we are not going to export.  */
9303
0
    if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9304
0
      do_copy = false;
9305
9306
    /* Make sure undefined weak symbols are output as a dynamic
9307
       symbol in PIEs.  */
9308
0
    else if (h->dynindx == -1 && !h->forced_local)
9309
0
      {
9310
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
9311
0
    return false;
9312
0
      }
9313
0
  }
9314
9315
0
      if (do_copy)
9316
0
  {
9317
    /* Even though we don't directly need a GOT entry for this symbol,
9318
       the SVR4 psABI requires it to have a dynamic symbol table
9319
       index greater that DT_MIPS_GOTSYM if there are dynamic
9320
       relocations against it.
9321
9322
       VxWorks does not enforce the same mapping between the GOT
9323
       and the symbol table, so the same requirement does not
9324
       apply there.  */
9325
0
    if (htab->root.target_os != is_vxworks)
9326
0
      {
9327
0
        if (hmips->global_got_area > GGA_RELOC_ONLY)
9328
0
    hmips->global_got_area = GGA_RELOC_ONLY;
9329
0
        hmips->got_only_for_calls = false;
9330
0
      }
9331
9332
0
    mips_elf_allocate_dynamic_relocations
9333
0
      (dynobj, info, hmips->possibly_dynamic_relocs);
9334
0
    if (hmips->readonly_reloc)
9335
      /* We tell the dynamic linker that there are relocations
9336
         against the text segment.  */
9337
0
      info->flags |= DF_TEXTREL;
9338
0
  }
9339
0
    }
9340
9341
0
  return true;
9342
0
}
9343
9344
/* Adjust a symbol defined by a dynamic object and referenced by a
9345
   regular object.  The current definition is in some section of the
9346
   dynamic object, but we're not including those sections.  We have to
9347
   change the definition to something the rest of the link can
9348
   understand.  */
9349
9350
bool
9351
_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
9352
             struct elf_link_hash_entry *h)
9353
0
{
9354
0
  bfd *dynobj;
9355
0
  struct mips_elf_link_hash_entry *hmips;
9356
0
  struct mips_elf_link_hash_table *htab;
9357
0
  asection *s, *srel;
9358
9359
0
  htab = mips_elf_hash_table (info);
9360
0
  BFD_ASSERT (htab != NULL);
9361
9362
0
  dynobj = elf_hash_table (info)->dynobj;
9363
0
  hmips = (struct mips_elf_link_hash_entry *) h;
9364
9365
  /* Make sure we know what is going on here.  */
9366
0
  if (dynobj == NULL
9367
0
      || (! h->needs_plt
9368
0
    && ! h->is_weakalias
9369
0
    && (! h->def_dynamic
9370
0
        || ! h->ref_regular
9371
0
        || h->def_regular)))
9372
0
    {
9373
0
      if (h->type == STT_GNU_IFUNC)
9374
0
  _bfd_error_handler (_("IFUNC symbol %s in dynamic symbol table - IFUNCS are not supported"),
9375
0
          h->root.root.string);
9376
0
      else
9377
0
  _bfd_error_handler (_("non-dynamic symbol %s in dynamic symbol table"),
9378
0
          h->root.root.string);
9379
0
      return true;
9380
0
    }
9381
9382
0
  hmips = (struct mips_elf_link_hash_entry *) h;
9383
9384
  /* If there are call relocations against an externally-defined symbol,
9385
     see whether we can create a MIPS lazy-binding stub for it.  We can
9386
     only do this if all references to the function are through call
9387
     relocations, and in that case, the traditional lazy-binding stubs
9388
     are much more efficient than PLT entries.
9389
9390
     Traditional stubs are only available on SVR4 psABI-based systems;
9391
     VxWorks always uses PLTs instead.  */
9392
0
  if (htab->root.target_os != is_vxworks
9393
0
      && h->needs_plt
9394
0
      && !hmips->no_fn_stub)
9395
0
    {
9396
0
      if (! elf_hash_table (info)->dynamic_sections_created)
9397
0
  return true;
9398
9399
      /* If this symbol is not defined in a regular file, then set
9400
   the symbol to the stub location.  This is required to make
9401
   function pointers compare as equal between the normal
9402
   executable and the shared library.  */
9403
0
      if (!h->def_regular
9404
0
    && !bfd_is_abs_section (htab->sstubs->output_section))
9405
0
  {
9406
0
    hmips->needs_lazy_stub = true;
9407
0
    htab->lazy_stub_count++;
9408
0
    return true;
9409
0
  }
9410
0
    }
9411
  /* As above, VxWorks requires PLT entries for externally-defined
9412
     functions that are only accessed through call relocations.
9413
9414
     Both VxWorks and non-VxWorks targets also need PLT entries if there
9415
     are static-only relocations against an externally-defined function.
9416
     This can technically occur for shared libraries if there are
9417
     branches to the symbol, although it is unlikely that this will be
9418
     used in practice due to the short ranges involved.  It can occur
9419
     for any relative or absolute relocation in executables; in that
9420
     case, the PLT entry becomes the function's canonical address.  */
9421
0
  else if (((h->needs_plt && !hmips->no_fn_stub)
9422
0
      || (h->type == STT_FUNC && hmips->has_static_relocs))
9423
0
     && htab->use_plts_and_copy_relocs
9424
0
     && !SYMBOL_CALLS_LOCAL (info, h)
9425
0
     && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9426
0
    && h->root.type == bfd_link_hash_undefweak))
9427
0
    {
9428
0
      bool micromips_p = MICROMIPS_P (info->output_bfd);
9429
0
      bool newabi_p = NEWABI_P (info->output_bfd);
9430
9431
      /* If this is the first symbol to need a PLT entry, then make some
9432
   basic setup.  Also work out PLT entry sizes.  We'll need them
9433
   for PLT offset calculations.  */
9434
0
      if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
9435
0
  {
9436
0
    BFD_ASSERT (htab->root.sgotplt->size == 0);
9437
0
    BFD_ASSERT (htab->plt_got_index == 0);
9438
9439
    /* If we're using the PLT additions to the psABI, each PLT
9440
       entry is 16 bytes and the PLT0 entry is 32 bytes.
9441
       Encourage better cache usage by aligning.  We do this
9442
       lazily to avoid pessimizing traditional objects.  */
9443
0
    if (htab->root.target_os != is_vxworks
9444
0
        && !bfd_set_section_alignment (htab->root.splt, 5))
9445
0
      return false;
9446
9447
    /* Make sure that .got.plt is word-aligned.  We do this lazily
9448
       for the same reason as above.  */
9449
0
    if (!bfd_set_section_alignment (htab->root.sgotplt,
9450
0
            MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
9451
0
      return false;
9452
9453
    /* On non-VxWorks targets, the first two entries in .got.plt
9454
       are reserved.  */
9455
0
    if (htab->root.target_os != is_vxworks)
9456
0
      htab->plt_got_index
9457
0
        += (get_elf_backend_data (dynobj)->got_header_size
9458
0
      / MIPS_ELF_GOT_SIZE (dynobj));
9459
9460
    /* On VxWorks, also allocate room for the header's
9461
       .rela.plt.unloaded entries.  */
9462
0
    if (htab->root.target_os == is_vxworks
9463
0
        && !bfd_link_pic (info))
9464
0
      htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
9465
9466
    /* Now work out the sizes of individual PLT entries.  */
9467
0
    if (htab->root.target_os == is_vxworks
9468
0
        && bfd_link_pic (info))
9469
0
      htab->plt_mips_entry_size
9470
0
        = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
9471
0
    else if (htab->root.target_os == is_vxworks)
9472
0
      htab->plt_mips_entry_size
9473
0
        = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
9474
0
    else if (newabi_p)
9475
0
      htab->plt_mips_entry_size
9476
0
        = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9477
0
    else if (!micromips_p)
9478
0
      {
9479
0
        htab->plt_mips_entry_size
9480
0
    = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9481
0
        htab->plt_comp_entry_size
9482
0
    = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
9483
0
      }
9484
0
    else if (htab->insn32)
9485
0
      {
9486
0
        htab->plt_mips_entry_size
9487
0
    = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9488
0
        htab->plt_comp_entry_size
9489
0
    = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
9490
0
      }
9491
0
    else
9492
0
      {
9493
0
        htab->plt_mips_entry_size
9494
0
    = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9495
0
        htab->plt_comp_entry_size
9496
0
    = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
9497
0
      }
9498
0
  }
9499
9500
0
      if (h->plt.plist == NULL)
9501
0
  h->plt.plist = mips_elf_make_plt_record (dynobj);
9502
0
      if (h->plt.plist == NULL)
9503
0
  return false;
9504
9505
      /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
9506
   n32 or n64, so always use a standard entry there.
9507
9508
   If the symbol has a MIPS16 call stub and gets a PLT entry, then
9509
   all MIPS16 calls will go via that stub, and there is no benefit
9510
   to having a MIPS16 entry.  And in the case of call_stub a
9511
   standard entry actually has to be used as the stub ends with a J
9512
   instruction.  */
9513
0
      if (newabi_p
9514
0
    || htab->root.target_os == is_vxworks
9515
0
    || hmips->call_stub
9516
0
    || hmips->call_fp_stub)
9517
0
  {
9518
0
    h->plt.plist->need_mips = true;
9519
0
    h->plt.plist->need_comp = false;
9520
0
  }
9521
9522
      /* Otherwise, if there are no direct calls to the function, we
9523
   have a free choice of whether to use standard or compressed
9524
   entries.  Prefer microMIPS entries if the object is known to
9525
   contain microMIPS code, so that it becomes possible to create
9526
   pure microMIPS binaries.  Prefer standard entries otherwise,
9527
   because MIPS16 ones are no smaller and are usually slower.  */
9528
0
      if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
9529
0
  {
9530
0
    if (micromips_p)
9531
0
      h->plt.plist->need_comp = true;
9532
0
    else
9533
0
      h->plt.plist->need_mips = true;
9534
0
  }
9535
9536
0
      if (h->plt.plist->need_mips)
9537
0
  {
9538
0
    h->plt.plist->mips_offset = htab->plt_mips_offset;
9539
0
    htab->plt_mips_offset += htab->plt_mips_entry_size;
9540
0
  }
9541
0
      if (h->plt.plist->need_comp)
9542
0
  {
9543
0
    h->plt.plist->comp_offset = htab->plt_comp_offset;
9544
0
    htab->plt_comp_offset += htab->plt_comp_entry_size;
9545
0
  }
9546
9547
      /* Reserve the corresponding .got.plt entry now too.  */
9548
0
      h->plt.plist->gotplt_index = htab->plt_got_index++;
9549
9550
      /* If the output file has no definition of the symbol, set the
9551
   symbol's value to the address of the stub.  */
9552
0
      if (!bfd_link_pic (info) && !h->def_regular)
9553
0
  hmips->use_plt_entry = true;
9554
9555
      /* Make room for the R_MIPS_JUMP_SLOT relocation.  */
9556
0
      htab->root.srelplt->size += (htab->root.target_os == is_vxworks
9557
0
           ? MIPS_ELF_RELA_SIZE (dynobj)
9558
0
           : MIPS_ELF_REL_SIZE (dynobj));
9559
9560
      /* Make room for the .rela.plt.unloaded relocations.  */
9561
0
      if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9562
0
  htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9563
9564
      /* All relocations against this symbol that could have been made
9565
   dynamic will now refer to the PLT entry instead.  */
9566
0
      hmips->possibly_dynamic_relocs = 0;
9567
9568
0
      return true;
9569
0
    }
9570
9571
  /* If this is a weak symbol, and there is a real definition, the
9572
     processor independent code will have arranged for us to see the
9573
     real definition first, and we can just use the same value.  */
9574
0
  if (h->is_weakalias)
9575
0
    {
9576
0
      struct elf_link_hash_entry *def = weakdef (h);
9577
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
9578
0
      h->root.u.def.section = def->root.u.def.section;
9579
0
      h->root.u.def.value = def->root.u.def.value;
9580
0
      return true;
9581
0
    }
9582
9583
  /* Otherwise, there is nothing further to do for symbols defined
9584
     in regular objects.  */
9585
0
  if (h->def_regular)
9586
0
    return true;
9587
9588
  /* There's also nothing more to do if we'll convert all relocations
9589
     against this symbol into dynamic relocations.  */
9590
0
  if (!hmips->has_static_relocs)
9591
0
    return true;
9592
9593
  /* We're now relying on copy relocations.  Complain if we have
9594
     some that we can't convert.  */
9595
0
  if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
9596
0
    {
9597
0
      _bfd_error_handler (_("non-dynamic relocations refer to "
9598
0
          "dynamic symbol %s"),
9599
0
        h->root.root.string);
9600
0
      bfd_set_error (bfd_error_bad_value);
9601
0
      return false;
9602
0
    }
9603
9604
  /* We must allocate the symbol in our .dynbss section, which will
9605
     become part of the .bss section of the executable.  There will be
9606
     an entry for this symbol in the .dynsym section.  The dynamic
9607
     object will contain position independent code, so all references
9608
     from the dynamic object to this symbol will go through the global
9609
     offset table.  The dynamic linker will use the .dynsym entry to
9610
     determine the address it must put in the global offset table, so
9611
     both the dynamic object and the regular object will refer to the
9612
     same memory location for the variable.  */
9613
9614
0
  if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9615
0
    {
9616
0
      s = htab->root.sdynrelro;
9617
0
      srel = htab->root.sreldynrelro;
9618
0
    }
9619
0
  else
9620
0
    {
9621
0
      s = htab->root.sdynbss;
9622
0
      srel = htab->root.srelbss;
9623
0
    }
9624
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9625
0
    {
9626
0
      if (htab->root.target_os == is_vxworks)
9627
0
  srel->size += sizeof (Elf32_External_Rela);
9628
0
      else
9629
0
  mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9630
0
      h->needs_copy = 1;
9631
0
    }
9632
9633
  /* All relocations against this symbol that could have been made
9634
     dynamic will now refer to the local copy instead.  */
9635
0
  hmips->possibly_dynamic_relocs = 0;
9636
9637
0
  return _bfd_elf_adjust_dynamic_copy (info, h, s);
9638
0
}
9639

9640
/* This function is called after all the input files have been read,
9641
   and the input sections have been assigned to output sections.  We
9642
   check for any mips16 stub sections that we can discard.  */
9643
9644
bool
9645
_bfd_mips_elf_always_size_sections (bfd *output_bfd,
9646
            struct bfd_link_info *info)
9647
0
{
9648
0
  asection *sect;
9649
0
  struct mips_elf_link_hash_table *htab;
9650
0
  struct mips_htab_traverse_info hti;
9651
9652
0
  htab = mips_elf_hash_table (info);
9653
0
  BFD_ASSERT (htab != NULL);
9654
9655
  /* The .reginfo section has a fixed size.  */
9656
0
  sect = bfd_get_section_by_name (output_bfd, ".reginfo");
9657
0
  if (sect != NULL)
9658
0
    {
9659
0
      bfd_set_section_size (sect, sizeof (Elf32_External_RegInfo));
9660
0
      sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9661
0
    }
9662
9663
  /* The .MIPS.abiflags section has a fixed size.  */
9664
0
  sect = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
9665
0
  if (sect != NULL)
9666
0
    {
9667
0
      bfd_set_section_size (sect, sizeof (Elf_External_ABIFlags_v0));
9668
0
      sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9669
0
    }
9670
9671
0
  hti.info = info;
9672
0
  hti.output_bfd = output_bfd;
9673
0
  hti.error = false;
9674
0
  mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9675
0
             mips_elf_check_symbols, &hti);
9676
0
  if (hti.error)
9677
0
    return false;
9678
9679
0
  return true;
9680
0
}
9681
9682
/* If the link uses a GOT, lay it out and work out its size.  */
9683
9684
static bool
9685
mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9686
0
{
9687
0
  bfd *dynobj;
9688
0
  asection *s;
9689
0
  struct mips_got_info *g;
9690
0
  bfd_size_type loadable_size = 0;
9691
0
  bfd_size_type page_gotno;
9692
0
  bfd *ibfd;
9693
0
  struct mips_elf_traverse_got_arg tga;
9694
0
  struct mips_elf_link_hash_table *htab;
9695
9696
0
  htab = mips_elf_hash_table (info);
9697
0
  BFD_ASSERT (htab != NULL);
9698
9699
0
  s = htab->root.sgot;
9700
0
  if (s == NULL)
9701
0
    return true;
9702
9703
0
  dynobj = elf_hash_table (info)->dynobj;
9704
0
  g = htab->got_info;
9705
9706
  /* Allocate room for the reserved entries.  VxWorks always reserves
9707
     3 entries; other objects only reserve 2 entries.  */
9708
0
  BFD_ASSERT (g->assigned_low_gotno == 0);
9709
0
  if (htab->root.target_os == is_vxworks)
9710
0
    htab->reserved_gotno = 3;
9711
0
  else
9712
0
    htab->reserved_gotno = 2;
9713
0
  g->local_gotno += htab->reserved_gotno;
9714
0
  g->assigned_low_gotno = htab->reserved_gotno;
9715
9716
  /* Decide which symbols need to go in the global part of the GOT and
9717
     count the number of reloc-only GOT symbols.  */
9718
0
  mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9719
9720
0
  if (!mips_elf_resolve_final_got_entries (info, g))
9721
0
    return false;
9722
9723
  /* Calculate the total loadable size of the output.  That
9724
     will give us the maximum number of GOT_PAGE entries
9725
     required.  */
9726
0
  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9727
0
    {
9728
0
      asection *subsection;
9729
9730
0
      for (subsection = ibfd->sections;
9731
0
     subsection;
9732
0
     subsection = subsection->next)
9733
0
  {
9734
0
    if ((subsection->flags & SEC_ALLOC) == 0)
9735
0
      continue;
9736
0
    loadable_size += ((subsection->size + 0xf)
9737
0
          &~ (bfd_size_type) 0xf);
9738
0
  }
9739
0
    }
9740
9741
0
  if (htab->root.target_os == is_vxworks)
9742
    /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9743
       relocations against local symbols evaluate to "G", and the EABI does
9744
       not include R_MIPS_GOT_PAGE.  */
9745
0
    page_gotno = 0;
9746
0
  else
9747
    /* Assume there are two loadable segments consisting of contiguous
9748
       sections.  Is 5 enough?  */
9749
0
    page_gotno = (loadable_size >> 16) + 5;
9750
9751
  /* Choose the smaller of the two page estimates; both are intended to be
9752
     conservative.  */
9753
0
  if (page_gotno > g->page_gotno)
9754
0
    page_gotno = g->page_gotno;
9755
9756
0
  g->local_gotno += page_gotno;
9757
0
  g->assigned_high_gotno = g->local_gotno - 1;
9758
9759
0
  s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9760
0
  s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9761
0
  s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9762
9763
  /* VxWorks does not support multiple GOTs.  It initializes $gp to
9764
     __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9765
     dynamic loader.  */
9766
0
  if (htab->root.target_os != is_vxworks
9767
0
      && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9768
0
    {
9769
0
      if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9770
0
  return false;
9771
0
    }
9772
0
  else
9773
0
    {
9774
      /* Record that all bfds use G.  This also has the effect of freeing
9775
   the per-bfd GOTs, which we no longer need.  */
9776
0
      for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9777
0
  if (mips_elf_bfd_got (ibfd, false))
9778
0
    mips_elf_replace_bfd_got (ibfd, g);
9779
0
      mips_elf_replace_bfd_got (output_bfd, g);
9780
9781
      /* Set up TLS entries.  */
9782
0
      g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9783
0
      tga.info = info;
9784
0
      tga.g = g;
9785
0
      tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9786
0
      htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9787
0
      if (!tga.g)
9788
0
  return false;
9789
0
      BFD_ASSERT (g->tls_assigned_gotno
9790
0
      == g->global_gotno + g->local_gotno + g->tls_gotno);
9791
9792
      /* Each VxWorks GOT entry needs an explicit relocation.  */
9793
0
      if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
9794
0
  g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9795
9796
      /* Allocate room for the TLS relocations.  */
9797
0
      if (g->relocs)
9798
0
  mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9799
0
    }
9800
9801
0
  return true;
9802
0
}
9803
9804
/* Estimate the size of the .MIPS.stubs section.  */
9805
9806
static void
9807
mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9808
0
{
9809
0
  struct mips_elf_link_hash_table *htab;
9810
0
  bfd_size_type dynsymcount;
9811
9812
0
  htab = mips_elf_hash_table (info);
9813
0
  BFD_ASSERT (htab != NULL);
9814
9815
0
  if (htab->lazy_stub_count == 0)
9816
0
    return;
9817
9818
  /* IRIX rld assumes that a function stub isn't at the end of the .text
9819
     section, so add a dummy entry to the end.  */
9820
0
  htab->lazy_stub_count++;
9821
9822
  /* Get a worst-case estimate of the number of dynamic symbols needed.
9823
     At this point, dynsymcount does not account for section symbols
9824
     and count_section_dynsyms may overestimate the number that will
9825
     be needed.  */
9826
0
  dynsymcount = (elf_hash_table (info)->dynsymcount
9827
0
     + count_section_dynsyms (output_bfd, info));
9828
9829
  /* Determine the size of one stub entry.  There's no disadvantage
9830
     from using microMIPS code here, so for the sake of pure-microMIPS
9831
     binaries we prefer it whenever there's any microMIPS code in
9832
     output produced at all.  This has a benefit of stubs being
9833
     shorter by 4 bytes each too, unless in the insn32 mode.  */
9834
0
  if (!MICROMIPS_P (output_bfd))
9835
0
    htab->function_stub_size = (dynsymcount > 0x10000
9836
0
        ? MIPS_FUNCTION_STUB_BIG_SIZE
9837
0
        : MIPS_FUNCTION_STUB_NORMAL_SIZE);
9838
0
  else if (htab->insn32)
9839
0
    htab->function_stub_size = (dynsymcount > 0x10000
9840
0
        ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9841
0
        : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9842
0
  else
9843
0
    htab->function_stub_size = (dynsymcount > 0x10000
9844
0
        ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9845
0
        : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9846
9847
0
  htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9848
0
}
9849
9850
/* A mips_elf_link_hash_traverse callback for which DATA points to a
9851
   mips_htab_traverse_info.  If H needs a traditional MIPS lazy-binding
9852
   stub, allocate an entry in the stubs section.  */
9853
9854
static bool
9855
mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9856
0
{
9857
0
  struct mips_htab_traverse_info *hti = data;
9858
0
  struct mips_elf_link_hash_table *htab;
9859
0
  struct bfd_link_info *info;
9860
0
  bfd *output_bfd;
9861
9862
0
  info = hti->info;
9863
0
  output_bfd = hti->output_bfd;
9864
0
  htab = mips_elf_hash_table (info);
9865
0
  BFD_ASSERT (htab != NULL);
9866
9867
0
  if (h->needs_lazy_stub)
9868
0
    {
9869
0
      bool micromips_p = MICROMIPS_P (output_bfd);
9870
0
      unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9871
0
      bfd_vma isa_bit = micromips_p;
9872
9873
0
      BFD_ASSERT (htab->root.dynobj != NULL);
9874
0
      if (h->root.plt.plist == NULL)
9875
0
  h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9876
0
      if (h->root.plt.plist == NULL)
9877
0
  {
9878
0
    hti->error = true;
9879
0
    return false;
9880
0
  }
9881
0
      h->root.root.u.def.section = htab->sstubs;
9882
0
      h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9883
0
      h->root.plt.plist->stub_offset = htab->sstubs->size;
9884
0
      h->root.other = other;
9885
0
      htab->sstubs->size += htab->function_stub_size;
9886
0
    }
9887
0
  return true;
9888
0
}
9889
9890
/* Allocate offsets in the stubs section to each symbol that needs one.
9891
   Set the final size of the .MIPS.stub section.  */
9892
9893
static bool
9894
mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9895
0
{
9896
0
  bfd *output_bfd = info->output_bfd;
9897
0
  bool micromips_p = MICROMIPS_P (output_bfd);
9898
0
  unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9899
0
  bfd_vma isa_bit = micromips_p;
9900
0
  struct mips_elf_link_hash_table *htab;
9901
0
  struct mips_htab_traverse_info hti;
9902
0
  struct elf_link_hash_entry *h;
9903
0
  bfd *dynobj;
9904
9905
0
  htab = mips_elf_hash_table (info);
9906
0
  BFD_ASSERT (htab != NULL);
9907
9908
0
  if (htab->lazy_stub_count == 0)
9909
0
    return true;
9910
9911
0
  htab->sstubs->size = 0;
9912
0
  hti.info = info;
9913
0
  hti.output_bfd = output_bfd;
9914
0
  hti.error = false;
9915
0
  mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9916
0
  if (hti.error)
9917
0
    return false;
9918
0
  htab->sstubs->size += htab->function_stub_size;
9919
0
  BFD_ASSERT (htab->sstubs->size
9920
0
        == htab->lazy_stub_count * htab->function_stub_size);
9921
9922
0
  dynobj = elf_hash_table (info)->dynobj;
9923
0
  BFD_ASSERT (dynobj != NULL);
9924
0
  h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9925
0
  if (h == NULL)
9926
0
    return false;
9927
0
  h->root.u.def.value = isa_bit;
9928
0
  h->other = other;
9929
0
  h->type = STT_FUNC;
9930
9931
0
  return true;
9932
0
}
9933
9934
/* A mips_elf_link_hash_traverse callback for which DATA points to a
9935
   bfd_link_info.  If H uses the address of a PLT entry as the value
9936
   of the symbol, then set the entry in the symbol table now.  Prefer
9937
   a standard MIPS PLT entry.  */
9938
9939
static bool
9940
mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9941
0
{
9942
0
  struct bfd_link_info *info = data;
9943
0
  bool micromips_p = MICROMIPS_P (info->output_bfd);
9944
0
  struct mips_elf_link_hash_table *htab;
9945
0
  unsigned int other;
9946
0
  bfd_vma isa_bit;
9947
0
  bfd_vma val;
9948
9949
0
  htab = mips_elf_hash_table (info);
9950
0
  BFD_ASSERT (htab != NULL);
9951
9952
0
  if (h->use_plt_entry)
9953
0
    {
9954
0
      BFD_ASSERT (h->root.plt.plist != NULL);
9955
0
      BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9956
0
      || h->root.plt.plist->comp_offset != MINUS_ONE);
9957
9958
0
      val = htab->plt_header_size;
9959
0
      if (h->root.plt.plist->mips_offset != MINUS_ONE)
9960
0
  {
9961
0
    isa_bit = 0;
9962
0
    val += h->root.plt.plist->mips_offset;
9963
0
    other = 0;
9964
0
  }
9965
0
      else
9966
0
  {
9967
0
    isa_bit = 1;
9968
0
    val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9969
0
    other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9970
0
  }
9971
0
      val += isa_bit;
9972
      /* For VxWorks, point at the PLT load stub rather than the lazy
9973
   resolution stub; this stub will become the canonical function
9974
   address.  */
9975
0
      if (htab->root.target_os == is_vxworks)
9976
0
  val += 8;
9977
9978
0
      h->root.root.u.def.section = htab->root.splt;
9979
0
      h->root.root.u.def.value = val;
9980
0
      h->root.other = other;
9981
0
    }
9982
9983
0
  return true;
9984
0
}
9985
9986
/* Set the sizes of the dynamic sections.  */
9987
9988
bool
9989
_bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9990
             struct bfd_link_info *info)
9991
0
{
9992
0
  bfd *dynobj;
9993
0
  asection *s, *sreldyn;
9994
0
  bool reltext;
9995
0
  struct mips_elf_link_hash_table *htab;
9996
9997
0
  htab = mips_elf_hash_table (info);
9998
0
  BFD_ASSERT (htab != NULL);
9999
0
  dynobj = elf_hash_table (info)->dynobj;
10000
0
  BFD_ASSERT (dynobj != NULL);
10001
10002
0
  if (elf_hash_table (info)->dynamic_sections_created)
10003
0
    {
10004
      /* Set the contents of the .interp section to the interpreter.  */
10005
0
      if (bfd_link_executable (info) && !info->nointerp)
10006
0
  {
10007
0
    s = bfd_get_linker_section (dynobj, ".interp");
10008
0
    BFD_ASSERT (s != NULL);
10009
0
    s->size
10010
0
      = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
10011
0
    s->contents
10012
0
      = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
10013
0
  }
10014
10015
      /* Figure out the size of the PLT header if we know that we
10016
   are using it.  For the sake of cache alignment always use
10017
   a standard header whenever any standard entries are present
10018
   even if microMIPS entries are present as well.  This also
10019
   lets the microMIPS header rely on the value of $v0 only set
10020
   by microMIPS entries, for a small size reduction.
10021
10022
   Set symbol table entry values for symbols that use the
10023
   address of their PLT entry now that we can calculate it.
10024
10025
   Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
10026
   haven't already in _bfd_elf_create_dynamic_sections.  */
10027
0
      if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
10028
0
  {
10029
0
    bool micromips_p = (MICROMIPS_P (output_bfd)
10030
0
             && !htab->plt_mips_offset);
10031
0
    unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10032
0
    bfd_vma isa_bit = micromips_p;
10033
0
    struct elf_link_hash_entry *h;
10034
0
    bfd_vma size;
10035
10036
0
    BFD_ASSERT (htab->use_plts_and_copy_relocs);
10037
0
    BFD_ASSERT (htab->root.sgotplt->size == 0);
10038
0
    BFD_ASSERT (htab->root.splt->size == 0);
10039
10040
0
    if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
10041
0
      size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
10042
0
    else if (htab->root.target_os == is_vxworks)
10043
0
      size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
10044
0
    else if (ABI_64_P (output_bfd))
10045
0
      size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
10046
0
    else if (ABI_N32_P (output_bfd))
10047
0
      size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
10048
0
    else if (!micromips_p)
10049
0
      size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
10050
0
    else if (htab->insn32)
10051
0
      size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
10052
0
    else
10053
0
      size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
10054
10055
0
    htab->plt_header_is_comp = micromips_p;
10056
0
    htab->plt_header_size = size;
10057
0
    htab->root.splt->size = (size
10058
0
           + htab->plt_mips_offset
10059
0
           + htab->plt_comp_offset);
10060
0
    htab->root.sgotplt->size = (htab->plt_got_index
10061
0
              * MIPS_ELF_GOT_SIZE (dynobj));
10062
10063
0
    mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
10064
10065
0
    if (htab->root.hplt == NULL)
10066
0
      {
10067
0
        h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
10068
0
                 "_PROCEDURE_LINKAGE_TABLE_");
10069
0
        htab->root.hplt = h;
10070
0
        if (h == NULL)
10071
0
    return false;
10072
0
      }
10073
10074
0
    h = htab->root.hplt;
10075
0
    h->root.u.def.value = isa_bit;
10076
0
    h->other = other;
10077
0
    h->type = STT_FUNC;
10078
0
  }
10079
0
    }
10080
10081
  /* Allocate space for global sym dynamic relocs.  */
10082
0
  elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
10083
10084
0
  mips_elf_estimate_stub_size (output_bfd, info);
10085
10086
0
  if (!mips_elf_lay_out_got (output_bfd, info))
10087
0
    return false;
10088
10089
0
  mips_elf_lay_out_lazy_stubs (info);
10090
10091
  /* The check_relocs and adjust_dynamic_symbol entry points have
10092
     determined the sizes of the various dynamic sections.  Allocate
10093
     memory for them.  */
10094
0
  reltext = false;
10095
0
  for (s = dynobj->sections; s != NULL; s = s->next)
10096
0
    {
10097
0
      const char *name;
10098
10099
      /* It's OK to base decisions on the section name, because none
10100
   of the dynobj section names depend upon the input files.  */
10101
0
      name = bfd_section_name (s);
10102
10103
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
10104
0
  continue;
10105
10106
0
      if (startswith (name, ".rel"))
10107
0
  {
10108
0
    if (s->size != 0)
10109
0
      {
10110
0
        const char *outname;
10111
0
        asection *target;
10112
10113
        /* If this relocation section applies to a read only
10114
     section, then we probably need a DT_TEXTREL entry.
10115
     If the relocation section is .rel(a).dyn, we always
10116
     assert a DT_TEXTREL entry rather than testing whether
10117
     there exists a relocation to a read only section or
10118
     not.  */
10119
0
        outname = bfd_section_name (s->output_section);
10120
0
        target = bfd_get_section_by_name (output_bfd, outname + 4);
10121
0
        if ((target != NULL
10122
0
       && (target->flags & SEC_READONLY) != 0
10123
0
       && (target->flags & SEC_ALLOC) != 0)
10124
0
      || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
10125
0
    reltext = true;
10126
10127
        /* We use the reloc_count field as a counter if we need
10128
     to copy relocs into the output file.  */
10129
0
        if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
10130
0
    s->reloc_count = 0;
10131
10132
        /* If combreloc is enabled, elf_link_sort_relocs() will
10133
     sort relocations, but in a different way than we do,
10134
     and before we're done creating relocations.  Also, it
10135
     will move them around between input sections'
10136
     relocation's contents, so our sorting would be
10137
     broken, so don't let it run.  */
10138
0
        info->combreloc = 0;
10139
0
      }
10140
0
  }
10141
0
      else if (bfd_link_executable (info)
10142
0
         && ! mips_elf_hash_table (info)->use_rld_obj_head
10143
0
         && startswith (name, ".rld_map"))
10144
0
  {
10145
    /* We add a room for __rld_map.  It will be filled in by the
10146
       rtld to contain a pointer to the _r_debug structure.  */
10147
0
    s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
10148
0
  }
10149
0
      else if (SGI_COMPAT (output_bfd)
10150
0
         && startswith (name, ".compact_rel"))
10151
0
  s->size += mips_elf_hash_table (info)->compact_rel_size;
10152
0
      else if (s == htab->root.splt)
10153
0
  {
10154
    /* If the last PLT entry has a branch delay slot, allocate
10155
       room for an extra nop to fill the delay slot.  This is
10156
       for CPUs without load interlocking.  */
10157
0
    if (! LOAD_INTERLOCKS_P (output_bfd)
10158
0
        && htab->root.target_os != is_vxworks
10159
0
        && s->size > 0)
10160
0
      s->size += 4;
10161
0
  }
10162
0
      else if (! startswith (name, ".init")
10163
0
         && s != htab->root.sgot
10164
0
         && s != htab->root.sgotplt
10165
0
         && s != htab->sstubs
10166
0
         && s != htab->root.sdynbss
10167
0
         && s != htab->root.sdynrelro)
10168
0
  {
10169
    /* It's not one of our sections, so don't allocate space.  */
10170
0
    continue;
10171
0
  }
10172
10173
0
      if (s->size == 0)
10174
0
  {
10175
0
    s->flags |= SEC_EXCLUDE;
10176
0
    continue;
10177
0
  }
10178
10179
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
10180
0
  continue;
10181
10182
      /* Allocate memory for the section contents.  */
10183
0
      s->contents = bfd_zalloc (dynobj, s->size);
10184
0
      if (s->contents == NULL)
10185
0
  {
10186
0
    bfd_set_error (bfd_error_no_memory);
10187
0
    return false;
10188
0
  }
10189
0
    }
10190
10191
0
  if (elf_hash_table (info)->dynamic_sections_created)
10192
0
    {
10193
      /* Add some entries to the .dynamic section.  We fill in the
10194
   values later, in _bfd_mips_elf_finish_dynamic_sections, but we
10195
   must add the entries now so that we get the correct size for
10196
   the .dynamic section.  */
10197
10198
      /* SGI object has the equivalence of DT_DEBUG in the
10199
   DT_MIPS_RLD_MAP entry.  This must come first because glibc
10200
   only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
10201
   may only look at the first one they see.  */
10202
0
      if (!bfd_link_pic (info)
10203
0
    && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
10204
0
  return false;
10205
10206
0
      if (bfd_link_executable (info)
10207
0
    && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
10208
0
  return false;
10209
10210
      /* The DT_DEBUG entry may be filled in by the dynamic linker and
10211
   used by the debugger.  */
10212
0
      if (bfd_link_executable (info)
10213
0
    && !SGI_COMPAT (output_bfd)
10214
0
    && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
10215
0
  return false;
10216
10217
0
      if (reltext
10218
0
    && (SGI_COMPAT (output_bfd)
10219
0
        || htab->root.target_os == is_vxworks))
10220
0
  info->flags |= DF_TEXTREL;
10221
10222
0
      if ((info->flags & DF_TEXTREL) != 0)
10223
0
  {
10224
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
10225
0
      return false;
10226
10227
    /* Clear the DF_TEXTREL flag.  It will be set again if we
10228
       write out an actual text relocation; we may not, because
10229
       at this point we do not know whether e.g. any .eh_frame
10230
       absolute relocations have been converted to PC-relative.  */
10231
0
    info->flags &= ~DF_TEXTREL;
10232
0
  }
10233
10234
0
      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
10235
0
  return false;
10236
10237
0
      sreldyn = mips_elf_rel_dyn_section (info, false);
10238
0
      if (htab->root.target_os == is_vxworks)
10239
0
  {
10240
    /* VxWorks uses .rela.dyn instead of .rel.dyn.  It does not
10241
       use any of the DT_MIPS_* tags.  */
10242
0
    if (sreldyn && sreldyn->size > 0)
10243
0
      {
10244
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
10245
0
    return false;
10246
10247
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
10248
0
    return false;
10249
10250
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
10251
0
    return false;
10252
0
      }
10253
0
  }
10254
0
      else
10255
0
  {
10256
0
    if (sreldyn && sreldyn->size > 0
10257
0
        && !bfd_is_abs_section (sreldyn->output_section))
10258
0
      {
10259
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
10260
0
    return false;
10261
10262
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
10263
0
    return false;
10264
10265
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
10266
0
    return false;
10267
0
      }
10268
10269
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
10270
0
      return false;
10271
10272
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
10273
0
      return false;
10274
10275
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
10276
0
      return false;
10277
10278
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
10279
0
      return false;
10280
10281
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
10282
0
      return false;
10283
10284
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
10285
0
      return false;
10286
10287
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
10288
0
      return false;
10289
10290
0
    if (info->emit_gnu_hash
10291
0
        && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_XHASH, 0))
10292
0
      return false;
10293
10294
0
    if (IRIX_COMPAT (dynobj) == ict_irix5
10295
0
        && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
10296
0
      return false;
10297
10298
0
    if (IRIX_COMPAT (dynobj) == ict_irix6
10299
0
        && (bfd_get_section_by_name
10300
0
      (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
10301
0
        && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
10302
0
      return false;
10303
0
  }
10304
0
      if (htab->root.splt->size > 0)
10305
0
  {
10306
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
10307
0
      return false;
10308
10309
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
10310
0
      return false;
10311
10312
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
10313
0
      return false;
10314
10315
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
10316
0
      return false;
10317
0
  }
10318
0
      if (htab->root.target_os == is_vxworks
10319
0
    && !elf_vxworks_add_dynamic_entries (output_bfd, info))
10320
0
  return false;
10321
0
    }
10322
10323
0
  return true;
10324
0
}
10325

10326
/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
10327
   Adjust its R_ADDEND field so that it is correct for the output file.
10328
   LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
10329
   and sections respectively; both use symbol indexes.  */
10330
10331
static void
10332
mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
10333
      bfd *input_bfd, Elf_Internal_Sym *local_syms,
10334
      asection **local_sections, Elf_Internal_Rela *rel)
10335
0
{
10336
0
  unsigned int r_type, r_symndx;
10337
0
  Elf_Internal_Sym *sym;
10338
0
  asection *sec;
10339
10340
0
  if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10341
0
    {
10342
0
      r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10343
0
      if (gprel16_reloc_p (r_type)
10344
0
    || r_type == R_MIPS_GPREL32
10345
0
    || literal_reloc_p (r_type))
10346
0
  {
10347
0
    rel->r_addend += _bfd_get_gp_value (input_bfd);
10348
0
    rel->r_addend -= _bfd_get_gp_value (output_bfd);
10349
0
  }
10350
10351
0
      r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
10352
0
      sym = local_syms + r_symndx;
10353
10354
      /* Adjust REL's addend to account for section merging.  */
10355
0
      if (!bfd_link_relocatable (info))
10356
0
  {
10357
0
    sec = local_sections[r_symndx];
10358
0
    _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
10359
0
  }
10360
10361
      /* This would normally be done by the rela_normal code in elflink.c.  */
10362
0
      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10363
0
  rel->r_addend += local_sections[r_symndx]->output_offset;
10364
0
    }
10365
0
}
10366
10367
/* Handle relocations against symbols from removed linkonce sections,
10368
   or sections discarded by a linker script.  We use this wrapper around
10369
   RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
10370
   on 64-bit ELF targets.  In this case for any relocation handled, which
10371
   always be the first in a triplet, the remaining two have to be processed
10372
   together with the first, even if they are R_MIPS_NONE.  It is the symbol
10373
   index referred by the first reloc that applies to all the three and the
10374
   remaining two never refer to an object symbol.  And it is the final
10375
   relocation (the last non-null one) that determines the output field of
10376
   the whole relocation so retrieve the corresponding howto structure for
10377
   the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
10378
10379
   Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
10380
   and therefore requires to be pasted in a loop.  It also defines a block
10381
   and does not protect any of its arguments, hence the extra brackets.  */
10382
10383
static void
10384
mips_reloc_against_discarded_section (bfd *output_bfd,
10385
              struct bfd_link_info *info,
10386
              bfd *input_bfd, asection *input_section,
10387
              Elf_Internal_Rela **rel,
10388
              const Elf_Internal_Rela **relend,
10389
              bool rel_reloc,
10390
              reloc_howto_type *howto,
10391
              bfd_byte *contents)
10392
0
{
10393
0
  const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10394
0
  int count = bed->s->int_rels_per_ext_rel;
10395
0
  unsigned int r_type;
10396
0
  int i;
10397
10398
0
  for (i = count - 1; i > 0; i--)
10399
0
    {
10400
0
      r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
10401
0
      if (r_type != R_MIPS_NONE)
10402
0
  {
10403
0
    howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10404
0
    break;
10405
0
  }
10406
0
    }
10407
0
  do
10408
0
    {
10409
0
       RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
10410
0
          (*rel), count, (*relend),
10411
0
          howto, i, contents);
10412
0
    }
10413
0
  while (0);
10414
0
}
10415
10416
/* Relocate a MIPS ELF section.  */
10417
10418
int
10419
_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
10420
        bfd *input_bfd, asection *input_section,
10421
        bfd_byte *contents, Elf_Internal_Rela *relocs,
10422
        Elf_Internal_Sym *local_syms,
10423
        asection **local_sections)
10424
0
{
10425
0
  Elf_Internal_Rela *rel;
10426
0
  const Elf_Internal_Rela *relend;
10427
0
  bfd_vma addend = 0;
10428
0
  bool use_saved_addend_p = false;
10429
10430
0
  relend = relocs + input_section->reloc_count;
10431
0
  for (rel = relocs; rel < relend; ++rel)
10432
0
    {
10433
0
      const char *name;
10434
0
      bfd_vma value = 0;
10435
0
      reloc_howto_type *howto;
10436
0
      bool cross_mode_jump_p = false;
10437
      /* TRUE if the relocation is a RELA relocation, rather than a
10438
   REL relocation.  */
10439
0
      bool rela_relocation_p = true;
10440
0
      unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10441
0
      const char *msg;
10442
0
      unsigned long r_symndx;
10443
0
      asection *sec;
10444
0
      Elf_Internal_Shdr *symtab_hdr;
10445
0
      struct elf_link_hash_entry *h;
10446
0
      bool rel_reloc;
10447
10448
0
      rel_reloc = (NEWABI_P (input_bfd)
10449
0
       && mips_elf_rel_relocation_p (input_bfd, input_section,
10450
0
             relocs, rel));
10451
      /* Find the relocation howto for this relocation.  */
10452
0
      howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10453
10454
0
      r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
10455
0
      symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10456
0
      if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10457
0
  {
10458
0
    sec = local_sections[r_symndx];
10459
0
    h = NULL;
10460
0
  }
10461
0
      else
10462
0
  {
10463
0
    unsigned long extsymoff;
10464
10465
0
    extsymoff = 0;
10466
0
    if (!elf_bad_symtab (input_bfd))
10467
0
      extsymoff = symtab_hdr->sh_info;
10468
0
    h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
10469
0
    while (h->root.type == bfd_link_hash_indirect
10470
0
     || h->root.type == bfd_link_hash_warning)
10471
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
10472
10473
0
    sec = NULL;
10474
0
    if (h->root.type == bfd_link_hash_defined
10475
0
        || h->root.type == bfd_link_hash_defweak)
10476
0
      sec = h->root.u.def.section;
10477
0
  }
10478
10479
0
      if (sec != NULL && discarded_section (sec))
10480
0
  {
10481
0
    mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
10482
0
            input_section, &rel, &relend,
10483
0
            rel_reloc, howto, contents);
10484
0
    continue;
10485
0
  }
10486
10487
0
      if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
10488
0
  {
10489
    /* Some 32-bit code uses R_MIPS_64.  In particular, people use
10490
       64-bit code, but make sure all their addresses are in the
10491
       lowermost or uppermost 32-bit section of the 64-bit address
10492
       space.  Thus, when they use an R_MIPS_64 they mean what is
10493
       usually meant by R_MIPS_32, with the exception that the
10494
       stored value is sign-extended to 64 bits.  */
10495
0
    howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
10496
10497
    /* On big-endian systems, we need to lie about the position
10498
       of the reloc.  */
10499
0
    if (bfd_big_endian (input_bfd))
10500
0
      rel->r_offset += 4;
10501
0
  }
10502
10503
0
      if (!use_saved_addend_p)
10504
0
  {
10505
    /* If these relocations were originally of the REL variety,
10506
       we must pull the addend out of the field that will be
10507
       relocated.  Otherwise, we simply use the contents of the
10508
       RELA relocation.  */
10509
0
    if (mips_elf_rel_relocation_p (input_bfd, input_section,
10510
0
           relocs, rel))
10511
0
      {
10512
0
        rela_relocation_p = false;
10513
0
        addend = mips_elf_read_rel_addend (input_bfd, input_section,
10514
0
             rel, howto, contents);
10515
0
        if (hi16_reloc_p (r_type)
10516
0
      || (got16_reloc_p (r_type)
10517
0
          && mips_elf_local_relocation_p (input_bfd, rel,
10518
0
                  local_sections)))
10519
0
    {
10520
0
      if (!mips_elf_add_lo16_rel_addend (input_bfd, input_section,
10521
0
                 rel, relend,
10522
0
                 contents, &addend))
10523
0
        {
10524
0
          if (h)
10525
0
      name = h->root.root.string;
10526
0
          else
10527
0
      name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10528
0
             local_syms + r_symndx,
10529
0
             sec);
10530
0
          _bfd_error_handler
10531
      /* xgettext:c-format */
10532
0
      (_("%pB: can't find matching LO16 reloc against `%s'"
10533
0
         " for %s at %#" PRIx64 " in section `%pA'"),
10534
0
       input_bfd, name,
10535
0
       howto->name, (uint64_t) rel->r_offset, input_section);
10536
0
        }
10537
0
    }
10538
0
        else
10539
0
    addend <<= howto->rightshift;
10540
0
      }
10541
0
    else
10542
0
      addend = rel->r_addend;
10543
0
    mips_elf_adjust_addend (output_bfd, info, input_bfd,
10544
0
          local_syms, local_sections, rel);
10545
0
  }
10546
10547
0
      if (bfd_link_relocatable (info))
10548
0
  {
10549
0
    if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
10550
0
        && bfd_big_endian (input_bfd))
10551
0
      rel->r_offset -= 4;
10552
10553
0
    if (!rela_relocation_p && rel->r_addend)
10554
0
      {
10555
0
        addend += rel->r_addend;
10556
0
        if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
10557
0
    addend = mips_elf_high (addend);
10558
0
        else if (r_type == R_MIPS_HIGHER)
10559
0
    addend = mips_elf_higher (addend);
10560
0
        else if (r_type == R_MIPS_HIGHEST)
10561
0
    addend = mips_elf_highest (addend);
10562
0
        else
10563
0
    addend >>= howto->rightshift;
10564
10565
        /* We use the source mask, rather than the destination
10566
     mask because the place to which we are writing will be
10567
     source of the addend in the final link.  */
10568
0
        addend &= howto->src_mask;
10569
10570
0
        if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10571
    /* See the comment above about using R_MIPS_64 in the 32-bit
10572
       ABI.  Here, we need to update the addend.  It would be
10573
       possible to get away with just using the R_MIPS_32 reloc
10574
       but for endianness.  */
10575
0
    {
10576
0
      bfd_vma sign_bits;
10577
0
      bfd_vma low_bits;
10578
0
      bfd_vma high_bits;
10579
10580
0
      if (addend & ((bfd_vma) 1 << 31))
10581
0
#ifdef BFD64
10582
0
        sign_bits = ((bfd_vma) 1 << 32) - 1;
10583
#else
10584
        sign_bits = -1;
10585
#endif
10586
0
      else
10587
0
        sign_bits = 0;
10588
10589
      /* If we don't know that we have a 64-bit type,
10590
         do two separate stores.  */
10591
0
      if (bfd_big_endian (input_bfd))
10592
0
        {
10593
          /* Store the sign-bits (which are most significant)
10594
       first.  */
10595
0
          low_bits = sign_bits;
10596
0
          high_bits = addend;
10597
0
        }
10598
0
      else
10599
0
        {
10600
0
          low_bits = addend;
10601
0
          high_bits = sign_bits;
10602
0
        }
10603
0
      bfd_put_32 (input_bfd, low_bits,
10604
0
            contents + rel->r_offset);
10605
0
      bfd_put_32 (input_bfd, high_bits,
10606
0
            contents + rel->r_offset + 4);
10607
0
      continue;
10608
0
    }
10609
10610
0
        if (! mips_elf_perform_relocation (info, howto, rel, addend,
10611
0
             input_bfd, input_section,
10612
0
             contents, false))
10613
0
    return false;
10614
0
      }
10615
10616
    /* Go on to the next relocation.  */
10617
0
    continue;
10618
0
  }
10619
10620
      /* In the N32 and 64-bit ABIs there may be multiple consecutive
10621
   relocations for the same offset.  In that case we are
10622
   supposed to treat the output of each relocation as the addend
10623
   for the next.  */
10624
0
      if (rel + 1 < relend
10625
0
    && rel->r_offset == rel[1].r_offset
10626
0
    && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
10627
0
  use_saved_addend_p = true;
10628
0
      else
10629
0
  use_saved_addend_p = false;
10630
10631
      /* Figure out what value we are supposed to relocate.  */
10632
0
      switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
10633
0
               input_section, contents,
10634
0
               info, rel, addend, howto,
10635
0
               local_syms, local_sections,
10636
0
               &value, &name, &cross_mode_jump_p,
10637
0
               use_saved_addend_p))
10638
0
  {
10639
0
  case bfd_reloc_continue:
10640
    /* There's nothing to do.  */
10641
0
    continue;
10642
10643
0
  case bfd_reloc_undefined:
10644
    /* mips_elf_calculate_relocation already called the
10645
       undefined_symbol callback.  There's no real point in
10646
       trying to perform the relocation at this point, so we
10647
       just skip ahead to the next relocation.  */
10648
0
    continue;
10649
10650
0
  case bfd_reloc_notsupported:
10651
0
    msg = _("internal error: unsupported relocation error");
10652
0
    info->callbacks->warning
10653
0
      (info, msg, name, input_bfd, input_section, rel->r_offset);
10654
0
    return false;
10655
10656
0
  case bfd_reloc_overflow:
10657
0
    if (use_saved_addend_p)
10658
      /* Ignore overflow until we reach the last relocation for
10659
         a given location.  */
10660
0
      ;
10661
0
    else
10662
0
      {
10663
0
        struct mips_elf_link_hash_table *htab;
10664
10665
0
        htab = mips_elf_hash_table (info);
10666
0
        BFD_ASSERT (htab != NULL);
10667
0
        BFD_ASSERT (name != NULL);
10668
0
        if (!htab->small_data_overflow_reported
10669
0
      && (gprel16_reloc_p (howto->type)
10670
0
          || literal_reloc_p (howto->type)))
10671
0
    {
10672
0
      msg = _("small-data section exceeds 64KB;"
10673
0
        " lower small-data size limit (see option -G)");
10674
10675
0
      htab->small_data_overflow_reported = true;
10676
0
      (*info->callbacks->einfo) ("%P: %s\n", msg);
10677
0
    }
10678
0
        (*info->callbacks->reloc_overflow)
10679
0
    (info, NULL, name, howto->name, (bfd_vma) 0,
10680
0
     input_bfd, input_section, rel->r_offset);
10681
0
      }
10682
0
    break;
10683
10684
0
  case bfd_reloc_ok:
10685
0
    break;
10686
10687
0
  case bfd_reloc_outofrange:
10688
0
    msg = NULL;
10689
0
    if (jal_reloc_p (howto->type))
10690
0
      msg = (cross_mode_jump_p
10691
0
       ? _("cannot convert a jump to JALX "
10692
0
           "for a non-word-aligned address")
10693
0
       : (howto->type == R_MIPS16_26
10694
0
          ? _("jump to a non-word-aligned address")
10695
0
          : _("jump to a non-instruction-aligned address")));
10696
0
    else if (b_reloc_p (howto->type))
10697
0
      msg = (cross_mode_jump_p
10698
0
       ? _("cannot convert a branch to JALX "
10699
0
           "for a non-word-aligned address")
10700
0
       : _("branch to a non-instruction-aligned address"));
10701
0
    else if (aligned_pcrel_reloc_p (howto->type))
10702
0
      msg = _("PC-relative load from unaligned address");
10703
0
    if (msg)
10704
0
      {
10705
0
        info->callbacks->einfo
10706
0
    ("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
10707
0
        break;
10708
0
      }
10709
    /* Fall through.  */
10710
10711
0
  default:
10712
0
    abort ();
10713
0
    break;
10714
0
  }
10715
10716
      /* If we've got another relocation for the address, keep going
10717
   until we reach the last one.  */
10718
0
      if (use_saved_addend_p)
10719
0
  {
10720
0
    addend = value;
10721
0
    continue;
10722
0
  }
10723
10724
0
      if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10725
  /* See the comment above about using R_MIPS_64 in the 32-bit
10726
     ABI.  Until now, we've been using the HOWTO for R_MIPS_32;
10727
     that calculated the right value.  Now, however, we
10728
     sign-extend the 32-bit result to 64-bits, and store it as a
10729
     64-bit value.  We are especially generous here in that we
10730
     go to extreme lengths to support this usage on systems with
10731
     only a 32-bit VMA.  */
10732
0
  {
10733
0
    bfd_vma sign_bits;
10734
0
    bfd_vma low_bits;
10735
0
    bfd_vma high_bits;
10736
10737
0
    if (value & ((bfd_vma) 1 << 31))
10738
0
#ifdef BFD64
10739
0
      sign_bits = ((bfd_vma) 1 << 32) - 1;
10740
#else
10741
      sign_bits = -1;
10742
#endif
10743
0
    else
10744
0
      sign_bits = 0;
10745
10746
    /* If we don't know that we have a 64-bit type,
10747
       do two separate stores.  */
10748
0
    if (bfd_big_endian (input_bfd))
10749
0
      {
10750
        /* Undo what we did above.  */
10751
0
        rel->r_offset -= 4;
10752
        /* Store the sign-bits (which are most significant)
10753
     first.  */
10754
0
        low_bits = sign_bits;
10755
0
        high_bits = value;
10756
0
      }
10757
0
    else
10758
0
      {
10759
0
        low_bits = value;
10760
0
        high_bits = sign_bits;
10761
0
      }
10762
0
    bfd_put_32 (input_bfd, low_bits,
10763
0
          contents + rel->r_offset);
10764
0
    bfd_put_32 (input_bfd, high_bits,
10765
0
          contents + rel->r_offset + 4);
10766
0
    continue;
10767
0
  }
10768
10769
      /* Actually perform the relocation.  */
10770
0
      if (! mips_elf_perform_relocation (info, howto, rel, value,
10771
0
           input_bfd, input_section,
10772
0
           contents, cross_mode_jump_p))
10773
0
  return false;
10774
0
    }
10775
10776
0
  return true;
10777
0
}
10778

10779
/* A function that iterates over each entry in la25_stubs and fills
10780
   in the code for each one.  DATA points to a mips_htab_traverse_info.  */
10781
10782
static int
10783
mips_elf_create_la25_stub (void **slot, void *data)
10784
0
{
10785
0
  struct mips_htab_traverse_info *hti;
10786
0
  struct mips_elf_link_hash_table *htab;
10787
0
  struct mips_elf_la25_stub *stub;
10788
0
  asection *s;
10789
0
  bfd_byte *loc;
10790
0
  bfd_vma offset, target, target_high, target_low;
10791
0
  bfd_vma branch_pc;
10792
0
  bfd_signed_vma pcrel_offset = 0;
10793
10794
0
  stub = (struct mips_elf_la25_stub *) *slot;
10795
0
  hti = (struct mips_htab_traverse_info *) data;
10796
0
  htab = mips_elf_hash_table (hti->info);
10797
0
  BFD_ASSERT (htab != NULL);
10798
10799
  /* Create the section contents, if we haven't already.  */
10800
0
  s = stub->stub_section;
10801
0
  loc = s->contents;
10802
0
  if (loc == NULL)
10803
0
    {
10804
0
      loc = bfd_malloc (s->size);
10805
0
      if (loc == NULL)
10806
0
  {
10807
0
    hti->error = true;
10808
0
    return false;
10809
0
  }
10810
0
      s->contents = loc;
10811
0
    }
10812
10813
  /* Work out where in the section this stub should go.  */
10814
0
  offset = stub->offset;
10815
10816
  /* We add 8 here to account for the LUI/ADDIU instructions
10817
     before the branch instruction.  This cannot be moved down to
10818
     where pcrel_offset is calculated as 's' is updated in
10819
     mips_elf_get_la25_target.  */
10820
0
  branch_pc = s->output_section->vma + s->output_offset + offset + 8;
10821
10822
  /* Work out the target address.  */
10823
0
  target = mips_elf_get_la25_target (stub, &s);
10824
0
  target += s->output_section->vma + s->output_offset;
10825
10826
0
  target_high = ((target + 0x8000) >> 16) & 0xffff;
10827
0
  target_low = (target & 0xffff);
10828
10829
  /* Calculate the PC of the compact branch instruction (for the case where
10830
     compact branches are used for either microMIPSR6 or MIPSR6 with
10831
     compact branches.  Add 4-bytes to account for BC using the PC of the
10832
     next instruction as the base.  */
10833
0
  pcrel_offset = target - (branch_pc + 4);
10834
10835
0
  if (stub->stub_section != htab->strampoline)
10836
0
    {
10837
      /* This is a simple LUI/ADDIU stub.  Zero out the beginning
10838
   of the section and write the two instructions at the end.  */
10839
0
      memset (loc, 0, offset);
10840
0
      loc += offset;
10841
0
      if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10842
0
  {
10843
0
    bfd_put_micromips_32 (hti->output_bfd,
10844
0
        LA25_LUI_MICROMIPS (target_high),
10845
0
        loc);
10846
0
    bfd_put_micromips_32 (hti->output_bfd,
10847
0
        LA25_ADDIU_MICROMIPS (target_low),
10848
0
        loc + 4);
10849
0
  }
10850
0
      else
10851
0
  {
10852
0
    bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10853
0
    bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10854
0
  }
10855
0
    }
10856
0
  else
10857
0
    {
10858
      /* This is trampoline.  */
10859
0
      loc += offset;
10860
0
      if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10861
0
  {
10862
0
    bfd_put_micromips_32 (hti->output_bfd,
10863
0
        LA25_LUI_MICROMIPS (target_high), loc);
10864
0
    bfd_put_micromips_32 (hti->output_bfd,
10865
0
        LA25_J_MICROMIPS (target), loc + 4);
10866
0
    bfd_put_micromips_32 (hti->output_bfd,
10867
0
        LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10868
0
    bfd_put_32 (hti->output_bfd, 0, loc + 12);
10869
0
  }
10870
0
      else
10871
0
  {
10872
0
    bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10873
0
    if (MIPSR6_P (hti->output_bfd) && htab->compact_branches)
10874
0
      {
10875
0
        bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10876
0
        bfd_put_32 (hti->output_bfd, LA25_BC (pcrel_offset), loc + 8);
10877
0
      }
10878
0
    else
10879
0
      {
10880
0
        bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10881
0
        bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10882
0
      }
10883
0
    bfd_put_32 (hti->output_bfd, 0, loc + 12);
10884
0
  }
10885
0
    }
10886
0
  return true;
10887
0
}
10888
10889
/* If NAME is one of the special IRIX6 symbols defined by the linker,
10890
   adjust it appropriately now.  */
10891
10892
static void
10893
mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10894
              const char *name, Elf_Internal_Sym *sym)
10895
0
{
10896
  /* The linker script takes care of providing names and values for
10897
     these, but we must place them into the right sections.  */
10898
0
  static const char* const text_section_symbols[] = {
10899
0
    "_ftext",
10900
0
    "_etext",
10901
0
    "__dso_displacement",
10902
0
    "__elf_header",
10903
0
    "__program_header_table",
10904
0
    NULL
10905
0
  };
10906
10907
0
  static const char* const data_section_symbols[] = {
10908
0
    "_fdata",
10909
0
    "_edata",
10910
0
    "_end",
10911
0
    "_fbss",
10912
0
    NULL
10913
0
  };
10914
10915
0
  const char* const *p;
10916
0
  int i;
10917
10918
0
  for (i = 0; i < 2; ++i)
10919
0
    for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10920
0
   *p;
10921
0
   ++p)
10922
0
      if (strcmp (*p, name) == 0)
10923
0
  {
10924
    /* All of these symbols are given type STT_SECTION by the
10925
       IRIX6 linker.  */
10926
0
    sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10927
0
    sym->st_other = STO_PROTECTED;
10928
10929
    /* The IRIX linker puts these symbols in special sections.  */
10930
0
    if (i == 0)
10931
0
      sym->st_shndx = SHN_MIPS_TEXT;
10932
0
    else
10933
0
      sym->st_shndx = SHN_MIPS_DATA;
10934
10935
0
    break;
10936
0
  }
10937
0
}
10938
10939
/* Finish up dynamic symbol handling.  We set the contents of various
10940
   dynamic sections here.  */
10941
10942
bool
10943
_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10944
             struct bfd_link_info *info,
10945
             struct elf_link_hash_entry *h,
10946
             Elf_Internal_Sym *sym)
10947
0
{
10948
0
  bfd *dynobj;
10949
0
  asection *sgot;
10950
0
  struct mips_got_info *g, *gg;
10951
0
  const char *name;
10952
0
  int idx;
10953
0
  struct mips_elf_link_hash_table *htab;
10954
0
  struct mips_elf_link_hash_entry *hmips;
10955
10956
0
  htab = mips_elf_hash_table (info);
10957
0
  BFD_ASSERT (htab != NULL);
10958
0
  dynobj = elf_hash_table (info)->dynobj;
10959
0
  hmips = (struct mips_elf_link_hash_entry *) h;
10960
10961
0
  BFD_ASSERT (htab->root.target_os != is_vxworks);
10962
10963
0
  if (h->plt.plist != NULL
10964
0
      && (h->plt.plist->mips_offset != MINUS_ONE
10965
0
    || h->plt.plist->comp_offset != MINUS_ONE))
10966
0
    {
10967
      /* We've decided to create a PLT entry for this symbol.  */
10968
0
      bfd_byte *loc;
10969
0
      bfd_vma header_address, got_address;
10970
0
      bfd_vma got_address_high, got_address_low, load;
10971
0
      bfd_vma got_index;
10972
0
      bfd_vma isa_bit;
10973
10974
0
      got_index = h->plt.plist->gotplt_index;
10975
10976
0
      BFD_ASSERT (htab->use_plts_and_copy_relocs);
10977
0
      BFD_ASSERT (h->dynindx != -1);
10978
0
      BFD_ASSERT (htab->root.splt != NULL);
10979
0
      BFD_ASSERT (got_index != MINUS_ONE);
10980
0
      BFD_ASSERT (!h->def_regular);
10981
10982
      /* Calculate the address of the PLT header.  */
10983
0
      isa_bit = htab->plt_header_is_comp;
10984
0
      header_address = (htab->root.splt->output_section->vma
10985
0
      + htab->root.splt->output_offset + isa_bit);
10986
10987
      /* Calculate the address of the .got.plt entry.  */
10988
0
      got_address = (htab->root.sgotplt->output_section->vma
10989
0
         + htab->root.sgotplt->output_offset
10990
0
         + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10991
10992
0
      got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10993
0
      got_address_low = got_address & 0xffff;
10994
10995
      /* The PLT sequence is not safe for N64 if .got.plt entry's address
10996
   cannot be loaded in two instructions.  */
10997
0
      if (ABI_64_P (output_bfd)
10998
0
    && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
10999
0
  {
11000
0
    _bfd_error_handler
11001
      /* xgettext:c-format */
11002
0
      (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
11003
0
         "supported; consider using `-Ttext-segment=...'"),
11004
0
       output_bfd,
11005
0
       htab->root.sgotplt->output_section,
11006
0
       (int64_t) got_address);
11007
0
    bfd_set_error (bfd_error_no_error);
11008
0
    return false;
11009
0
  }
11010
11011
      /* Initially point the .got.plt entry at the PLT header.  */
11012
0
      loc = (htab->root.sgotplt->contents
11013
0
       + got_index * MIPS_ELF_GOT_SIZE (dynobj));
11014
0
      if (ABI_64_P (output_bfd))
11015
0
  bfd_put_64 (output_bfd, header_address, loc);
11016
0
      else
11017
0
  bfd_put_32 (output_bfd, header_address, loc);
11018
11019
      /* Now handle the PLT itself.  First the standard entry (the order
11020
   does not matter, we just have to pick one).  */
11021
0
      if (h->plt.plist->mips_offset != MINUS_ONE)
11022
0
  {
11023
0
    const bfd_vma *plt_entry;
11024
0
    bfd_vma plt_offset;
11025
11026
0
    plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11027
11028
0
    BFD_ASSERT (plt_offset <= htab->root.splt->size);
11029
11030
    /* Find out where the .plt entry should go.  */
11031
0
    loc = htab->root.splt->contents + plt_offset;
11032
11033
    /* Pick the load opcode.  */
11034
0
    load = MIPS_ELF_LOAD_WORD (output_bfd);
11035
11036
    /* Fill in the PLT entry itself.  */
11037
11038
0
    if (MIPSR6_P (output_bfd))
11039
0
      plt_entry = htab->compact_branches ? mipsr6_exec_plt_entry_compact
11040
0
                 : mipsr6_exec_plt_entry;
11041
0
    else
11042
0
      plt_entry = mips_exec_plt_entry;
11043
0
    bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
11044
0
    bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
11045
0
          loc + 4);
11046
11047
0
    if (! LOAD_INTERLOCKS_P (output_bfd)
11048
0
        || (MIPSR6_P (output_bfd) && htab->compact_branches))
11049
0
      {
11050
0
        bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
11051
0
        bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11052
0
      }
11053
0
    else
11054
0
      {
11055
0
        bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
11056
0
        bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
11057
0
        loc + 12);
11058
0
      }
11059
0
  }
11060
11061
      /* Now the compressed entry.  They come after any standard ones.  */
11062
0
      if (h->plt.plist->comp_offset != MINUS_ONE)
11063
0
  {
11064
0
    bfd_vma plt_offset;
11065
11066
0
    plt_offset = (htab->plt_header_size + htab->plt_mips_offset
11067
0
      + h->plt.plist->comp_offset);
11068
11069
0
    BFD_ASSERT (plt_offset <= htab->root.splt->size);
11070
11071
    /* Find out where the .plt entry should go.  */
11072
0
    loc = htab->root.splt->contents + plt_offset;
11073
11074
    /* Fill in the PLT entry itself.  */
11075
0
    if (!MICROMIPS_P (output_bfd))
11076
0
      {
11077
0
        const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
11078
11079
0
        bfd_put_16 (output_bfd, plt_entry[0], loc);
11080
0
        bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
11081
0
        bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11082
0
        bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11083
0
        bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11084
0
        bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11085
0
        bfd_put_32 (output_bfd, got_address, loc + 12);
11086
0
      }
11087
0
    else if (htab->insn32)
11088
0
      {
11089
0
        const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
11090
11091
0
        bfd_put_16 (output_bfd, plt_entry[0], loc);
11092
0
        bfd_put_16 (output_bfd, got_address_high, loc + 2);
11093
0
        bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11094
0
        bfd_put_16 (output_bfd, got_address_low, loc + 6);
11095
0
        bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11096
0
        bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11097
0
        bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
11098
0
        bfd_put_16 (output_bfd, got_address_low, loc + 14);
11099
0
      }
11100
0
    else
11101
0
      {
11102
0
        const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
11103
0
        bfd_signed_vma gotpc_offset;
11104
0
        bfd_vma loc_address;
11105
11106
0
        BFD_ASSERT (got_address % 4 == 0);
11107
11108
0
        loc_address = (htab->root.splt->output_section->vma
11109
0
           + htab->root.splt->output_offset + plt_offset);
11110
0
        gotpc_offset = got_address - ((loc_address | 3) ^ 3);
11111
11112
        /* ADDIUPC has a span of +/-16MB, check we're in range.  */
11113
0
        if (gotpc_offset + 0x1000000 >= 0x2000000)
11114
0
    {
11115
0
      _bfd_error_handler
11116
        /* xgettext:c-format */
11117
0
        (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11118
0
           "beyond the range of ADDIUPC"),
11119
0
         output_bfd,
11120
0
         htab->root.sgotplt->output_section,
11121
0
         (int64_t) gotpc_offset,
11122
0
         htab->root.splt->output_section);
11123
0
      bfd_set_error (bfd_error_no_error);
11124
0
      return false;
11125
0
    }
11126
0
        bfd_put_16 (output_bfd,
11127
0
        plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11128
0
        bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11129
0
        bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11130
0
        bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11131
0
        bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11132
0
        bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11133
0
      }
11134
0
  }
11135
11136
      /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
11137
0
      mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
11138
0
            got_index - 2, h->dynindx,
11139
0
            R_MIPS_JUMP_SLOT, got_address);
11140
11141
      /* We distinguish between PLT entries and lazy-binding stubs by
11142
   giving the former an st_other value of STO_MIPS_PLT.  Set the
11143
   flag and leave the value if there are any relocations in the
11144
   binary where pointer equality matters.  */
11145
0
      sym->st_shndx = SHN_UNDEF;
11146
0
      if (h->pointer_equality_needed)
11147
0
  sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
11148
0
      else
11149
0
  {
11150
0
    sym->st_value = 0;
11151
0
    sym->st_other = 0;
11152
0
  }
11153
0
    }
11154
11155
0
  if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
11156
0
    {
11157
      /* We've decided to create a lazy-binding stub.  */
11158
0
      bool micromips_p = MICROMIPS_P (output_bfd);
11159
0
      unsigned int other = micromips_p ? STO_MICROMIPS : 0;
11160
0
      bfd_vma stub_size = htab->function_stub_size;
11161
0
      bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
11162
0
      bfd_vma isa_bit = micromips_p;
11163
0
      bfd_vma stub_big_size;
11164
11165
0
      if (!micromips_p)
11166
0
  stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
11167
0
      else if (htab->insn32)
11168
0
  stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
11169
0
      else
11170
0
  stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
11171
11172
      /* This symbol has a stub.  Set it up.  */
11173
11174
0
      BFD_ASSERT (h->dynindx != -1);
11175
11176
0
      BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
11177
11178
      /* Values up to 2^31 - 1 are allowed.  Larger values would cause
11179
   sign extension at runtime in the stub, resulting in a negative
11180
   index value.  */
11181
0
      if (h->dynindx & ~0x7fffffff)
11182
0
  return false;
11183
11184
      /* Fill the stub.  */
11185
0
      if (micromips_p)
11186
0
  {
11187
0
    idx = 0;
11188
0
    bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
11189
0
        stub + idx);
11190
0
    idx += 4;
11191
0
    if (htab->insn32)
11192
0
      {
11193
0
        bfd_put_micromips_32 (output_bfd,
11194
0
            STUB_MOVE32_MICROMIPS, stub + idx);
11195
0
        idx += 4;
11196
0
      }
11197
0
    else
11198
0
      {
11199
0
        bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
11200
0
        idx += 2;
11201
0
      }
11202
0
    if (stub_size == stub_big_size)
11203
0
      {
11204
0
        long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
11205
11206
0
        bfd_put_micromips_32 (output_bfd,
11207
0
            STUB_LUI_MICROMIPS (dynindx_hi),
11208
0
            stub + idx);
11209
0
        idx += 4;
11210
0
      }
11211
0
    if (htab->insn32)
11212
0
      {
11213
0
        bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
11214
0
            stub + idx);
11215
0
        idx += 4;
11216
0
      }
11217
0
    else
11218
0
      {
11219
0
        bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
11220
0
        idx += 2;
11221
0
      }
11222
11223
    /* If a large stub is not required and sign extension is not a
11224
       problem, then use legacy code in the stub.  */
11225
0
    if (stub_size == stub_big_size)
11226
0
      bfd_put_micromips_32 (output_bfd,
11227
0
          STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
11228
0
          stub + idx);
11229
0
    else if (h->dynindx & ~0x7fff)
11230
0
      bfd_put_micromips_32 (output_bfd,
11231
0
          STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
11232
0
          stub + idx);
11233
0
    else
11234
0
      bfd_put_micromips_32 (output_bfd,
11235
0
          STUB_LI16S_MICROMIPS (output_bfd,
11236
0
              h->dynindx),
11237
0
          stub + idx);
11238
0
  }
11239
0
      else
11240
0
  {
11241
0
    idx = 0;
11242
0
    bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
11243
0
    idx += 4;
11244
0
    bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
11245
0
    idx += 4;
11246
0
    if (stub_size == stub_big_size)
11247
0
      {
11248
0
        bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
11249
0
        stub + idx);
11250
0
        idx += 4;
11251
0
      }
11252
11253
0
    if (!(MIPSR6_P (output_bfd) && htab->compact_branches))
11254
0
      {
11255
0
        bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
11256
0
        idx += 4;
11257
0
      }
11258
11259
    /* If a large stub is not required and sign extension is not a
11260
       problem, then use legacy code in the stub.  */
11261
0
    if (stub_size == stub_big_size)
11262
0
      bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
11263
0
      stub + idx);
11264
0
    else if (h->dynindx & ~0x7fff)
11265
0
      bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
11266
0
      stub + idx);
11267
0
    else
11268
0
      bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
11269
0
      stub + idx);
11270
0
    idx += 4;
11271
11272
0
    if (MIPSR6_P (output_bfd) && htab->compact_branches)
11273
0
      bfd_put_32 (output_bfd, STUB_JALRC, stub + idx);
11274
0
  }
11275
11276
0
      BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
11277
0
      memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
11278
0
        stub, stub_size);
11279
11280
      /* Mark the symbol as undefined.  stub_offset != -1 occurs
11281
   only for the referenced symbol.  */
11282
0
      sym->st_shndx = SHN_UNDEF;
11283
11284
      /* The run-time linker uses the st_value field of the symbol
11285
   to reset the global offset table entry for this external
11286
   to its stub address when unlinking a shared object.  */
11287
0
      sym->st_value = (htab->sstubs->output_section->vma
11288
0
           + htab->sstubs->output_offset
11289
0
           + h->plt.plist->stub_offset
11290
0
           + isa_bit);
11291
0
      sym->st_other = other;
11292
0
    }
11293
11294
  /* If we have a MIPS16 function with a stub, the dynamic symbol must
11295
     refer to the stub, since only the stub uses the standard calling
11296
     conventions.  */
11297
0
  if (h->dynindx != -1 && hmips->fn_stub != NULL)
11298
0
    {
11299
0
      BFD_ASSERT (hmips->need_fn_stub);
11300
0
      sym->st_value = (hmips->fn_stub->output_section->vma
11301
0
           + hmips->fn_stub->output_offset);
11302
0
      sym->st_size = hmips->fn_stub->size;
11303
0
      sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
11304
0
    }
11305
11306
0
  BFD_ASSERT (h->dynindx != -1
11307
0
        || h->forced_local);
11308
11309
0
  sgot = htab->root.sgot;
11310
0
  g = htab->got_info;
11311
0
  BFD_ASSERT (g != NULL);
11312
11313
  /* Run through the global symbol table, creating GOT entries for all
11314
     the symbols that need them.  */
11315
0
  if (hmips->global_got_area != GGA_NONE)
11316
0
    {
11317
0
      bfd_vma offset;
11318
0
      bfd_vma value;
11319
11320
0
      value = sym->st_value;
11321
0
      offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11322
0
      MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
11323
0
    }
11324
11325
0
  if (hmips->global_got_area != GGA_NONE && g->next)
11326
0
    {
11327
0
      struct mips_got_entry e, *p;
11328
0
      bfd_vma entry;
11329
0
      bfd_vma offset;
11330
11331
0
      gg = g;
11332
11333
0
      e.abfd = output_bfd;
11334
0
      e.symndx = -1;
11335
0
      e.d.h = hmips;
11336
0
      e.tls_type = GOT_TLS_NONE;
11337
11338
0
      for (g = g->next; g->next != gg; g = g->next)
11339
0
  {
11340
0
    if (g->got_entries
11341
0
        && (p = (struct mips_got_entry *) htab_find (g->got_entries,
11342
0
                 &e)))
11343
0
      {
11344
0
        offset = p->gotidx;
11345
0
        BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
11346
0
        if (bfd_link_pic (info)
11347
0
      || (elf_hash_table (info)->dynamic_sections_created
11348
0
          && p->d.h != NULL
11349
0
          && p->d.h->root.def_dynamic
11350
0
          && !p->d.h->root.def_regular))
11351
0
    {
11352
      /* Create an R_MIPS_REL32 relocation for this entry.  Due to
11353
         the various compatibility problems, it's easier to mock
11354
         up an R_MIPS_32 or R_MIPS_64 relocation and leave
11355
         mips_elf_create_dynamic_relocation to calculate the
11356
         appropriate addend.  */
11357
0
      Elf_Internal_Rela rel[3];
11358
11359
0
      memset (rel, 0, sizeof (rel));
11360
0
      if (ABI_64_P (output_bfd))
11361
0
        rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
11362
0
      else
11363
0
        rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
11364
0
      rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
11365
11366
0
      entry = 0;
11367
0
      if (! (mips_elf_create_dynamic_relocation
11368
0
       (output_bfd, info, rel,
11369
0
        e.d.h, NULL, sym->st_value, &entry, sgot)))
11370
0
        return false;
11371
0
    }
11372
0
        else
11373
0
    entry = sym->st_value;
11374
0
        MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
11375
0
      }
11376
0
  }
11377
0
    }
11378
11379
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
11380
0
  name = h->root.root.string;
11381
0
  if (h == elf_hash_table (info)->hdynamic
11382
0
      || h == elf_hash_table (info)->hgot)
11383
0
    sym->st_shndx = SHN_ABS;
11384
0
  else if (strcmp (name, "_DYNAMIC_LINK") == 0
11385
0
     || strcmp (name, "_DYNAMIC_LINKING") == 0)
11386
0
    {
11387
0
      sym->st_shndx = SHN_ABS;
11388
0
      sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11389
0
      sym->st_value = 1;
11390
0
    }
11391
0
  else if (SGI_COMPAT (output_bfd))
11392
0
    {
11393
0
      if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
11394
0
    || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
11395
0
  {
11396
0
    sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11397
0
    sym->st_other = STO_PROTECTED;
11398
0
    sym->st_value = 0;
11399
0
    sym->st_shndx = SHN_MIPS_DATA;
11400
0
  }
11401
0
      else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
11402
0
  {
11403
0
    sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11404
0
    sym->st_other = STO_PROTECTED;
11405
0
    sym->st_value = mips_elf_hash_table (info)->procedure_count;
11406
0
    sym->st_shndx = SHN_ABS;
11407
0
  }
11408
0
      else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
11409
0
  {
11410
0
    if (h->type == STT_FUNC)
11411
0
      sym->st_shndx = SHN_MIPS_TEXT;
11412
0
    else if (h->type == STT_OBJECT)
11413
0
      sym->st_shndx = SHN_MIPS_DATA;
11414
0
  }
11415
0
    }
11416
11417
  /* Emit a copy reloc, if needed.  */
11418
0
  if (h->needs_copy)
11419
0
    {
11420
0
      asection *s;
11421
0
      bfd_vma symval;
11422
11423
0
      BFD_ASSERT (h->dynindx != -1);
11424
0
      BFD_ASSERT (htab->use_plts_and_copy_relocs);
11425
11426
0
      s = mips_elf_rel_dyn_section (info, false);
11427
0
      symval = (h->root.u.def.section->output_section->vma
11428
0
    + h->root.u.def.section->output_offset
11429
0
    + h->root.u.def.value);
11430
0
      mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
11431
0
            h->dynindx, R_MIPS_COPY, symval);
11432
0
    }
11433
11434
  /* Handle the IRIX6-specific symbols.  */
11435
0
  if (IRIX_COMPAT (output_bfd) == ict_irix6)
11436
0
    mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
11437
11438
  /* Keep dynamic compressed symbols odd.  This allows the dynamic linker
11439
     to treat compressed symbols like any other.  */
11440
0
  if (ELF_ST_IS_MIPS16 (sym->st_other))
11441
0
    {
11442
0
      BFD_ASSERT (sym->st_value & 1);
11443
0
      sym->st_other -= STO_MIPS16;
11444
0
    }
11445
0
  else if (ELF_ST_IS_MICROMIPS (sym->st_other))
11446
0
    {
11447
0
      BFD_ASSERT (sym->st_value & 1);
11448
0
      sym->st_other -= STO_MICROMIPS;
11449
0
    }
11450
11451
0
  return true;
11452
0
}
11453
11454
/* Likewise, for VxWorks.  */
11455
11456
bool
11457
_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
11458
           struct bfd_link_info *info,
11459
           struct elf_link_hash_entry *h,
11460
           Elf_Internal_Sym *sym)
11461
0
{
11462
0
  bfd *dynobj;
11463
0
  asection *sgot;
11464
0
  struct mips_got_info *g;
11465
0
  struct mips_elf_link_hash_table *htab;
11466
0
  struct mips_elf_link_hash_entry *hmips;
11467
11468
0
  htab = mips_elf_hash_table (info);
11469
0
  BFD_ASSERT (htab != NULL);
11470
0
  dynobj = elf_hash_table (info)->dynobj;
11471
0
  hmips = (struct mips_elf_link_hash_entry *) h;
11472
11473
0
  if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
11474
0
    {
11475
0
      bfd_byte *loc;
11476
0
      bfd_vma plt_address, got_address, got_offset, branch_offset;
11477
0
      Elf_Internal_Rela rel;
11478
0
      static const bfd_vma *plt_entry;
11479
0
      bfd_vma gotplt_index;
11480
0
      bfd_vma plt_offset;
11481
11482
0
      plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11483
0
      gotplt_index = h->plt.plist->gotplt_index;
11484
11485
0
      BFD_ASSERT (h->dynindx != -1);
11486
0
      BFD_ASSERT (htab->root.splt != NULL);
11487
0
      BFD_ASSERT (gotplt_index != MINUS_ONE);
11488
0
      BFD_ASSERT (plt_offset <= htab->root.splt->size);
11489
11490
      /* Calculate the address of the .plt entry.  */
11491
0
      plt_address = (htab->root.splt->output_section->vma
11492
0
         + htab->root.splt->output_offset
11493
0
         + plt_offset);
11494
11495
      /* Calculate the address of the .got.plt entry.  */
11496
0
      got_address = (htab->root.sgotplt->output_section->vma
11497
0
         + htab->root.sgotplt->output_offset
11498
0
         + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
11499
11500
      /* Calculate the offset of the .got.plt entry from
11501
   _GLOBAL_OFFSET_TABLE_.  */
11502
0
      got_offset = mips_elf_gotplt_index (info, h);
11503
11504
      /* Calculate the offset for the branch at the start of the PLT
11505
   entry.  The branch jumps to the beginning of .plt.  */
11506
0
      branch_offset = -(plt_offset / 4 + 1) & 0xffff;
11507
11508
      /* Fill in the initial value of the .got.plt entry.  */
11509
0
      bfd_put_32 (output_bfd, plt_address,
11510
0
      (htab->root.sgotplt->contents
11511
0
       + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
11512
11513
      /* Find out where the .plt entry should go.  */
11514
0
      loc = htab->root.splt->contents + plt_offset;
11515
11516
0
      if (bfd_link_pic (info))
11517
0
  {
11518
0
    plt_entry = mips_vxworks_shared_plt_entry;
11519
0
    bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11520
0
    bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11521
0
  }
11522
0
      else
11523
0
  {
11524
0
    bfd_vma got_address_high, got_address_low;
11525
11526
0
    plt_entry = mips_vxworks_exec_plt_entry;
11527
0
    got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11528
0
    got_address_low = got_address & 0xffff;
11529
11530
0
    bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11531
0
    bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11532
0
    bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
11533
0
    bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
11534
0
    bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11535
0
    bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11536
0
    bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11537
0
    bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11538
11539
0
    loc = (htab->srelplt2->contents
11540
0
     + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
11541
11542
    /* Emit a relocation for the .got.plt entry.  */
11543
0
    rel.r_offset = got_address;
11544
0
    rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11545
0
    rel.r_addend = plt_offset;
11546
0
    bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11547
11548
    /* Emit a relocation for the lui of %hi(<.got.plt slot>).  */
11549
0
    loc += sizeof (Elf32_External_Rela);
11550
0
    rel.r_offset = plt_address + 8;
11551
0
    rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11552
0
    rel.r_addend = got_offset;
11553
0
    bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11554
11555
    /* Emit a relocation for the addiu of %lo(<.got.plt slot>).  */
11556
0
    loc += sizeof (Elf32_External_Rela);
11557
0
    rel.r_offset += 4;
11558
0
    rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11559
0
    bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11560
0
  }
11561
11562
      /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
11563
0
      loc = (htab->root.srelplt->contents
11564
0
       + gotplt_index * sizeof (Elf32_External_Rela));
11565
0
      rel.r_offset = got_address;
11566
0
      rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
11567
0
      rel.r_addend = 0;
11568
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11569
11570
0
      if (!h->def_regular)
11571
0
  sym->st_shndx = SHN_UNDEF;
11572
0
    }
11573
11574
0
  BFD_ASSERT (h->dynindx != -1 || h->forced_local);
11575
11576
0
  sgot = htab->root.sgot;
11577
0
  g = htab->got_info;
11578
0
  BFD_ASSERT (g != NULL);
11579
11580
  /* See if this symbol has an entry in the GOT.  */
11581
0
  if (hmips->global_got_area != GGA_NONE)
11582
0
    {
11583
0
      bfd_vma offset;
11584
0
      Elf_Internal_Rela outrel;
11585
0
      bfd_byte *loc;
11586
0
      asection *s;
11587
11588
      /* Install the symbol value in the GOT.   */
11589
0
      offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11590
0
      MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
11591
11592
      /* Add a dynamic relocation for it.  */
11593
0
      s = mips_elf_rel_dyn_section (info, false);
11594
0
      loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
11595
0
      outrel.r_offset = (sgot->output_section->vma
11596
0
       + sgot->output_offset
11597
0
       + offset);
11598
0
      outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
11599
0
      outrel.r_addend = 0;
11600
0
      bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
11601
0
    }
11602
11603
  /* Emit a copy reloc, if needed.  */
11604
0
  if (h->needs_copy)
11605
0
    {
11606
0
      Elf_Internal_Rela rel;
11607
0
      asection *srel;
11608
0
      bfd_byte *loc;
11609
11610
0
      BFD_ASSERT (h->dynindx != -1);
11611
11612
0
      rel.r_offset = (h->root.u.def.section->output_section->vma
11613
0
          + h->root.u.def.section->output_offset
11614
0
          + h->root.u.def.value);
11615
0
      rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
11616
0
      rel.r_addend = 0;
11617
0
      if (h->root.u.def.section == htab->root.sdynrelro)
11618
0
  srel = htab->root.sreldynrelro;
11619
0
      else
11620
0
  srel = htab->root.srelbss;
11621
0
      loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
11622
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11623
0
      ++srel->reloc_count;
11624
0
    }
11625
11626
  /* If this is a mips16/microMIPS symbol, force the value to be even.  */
11627
0
  if (ELF_ST_IS_COMPRESSED (sym->st_other))
11628
0
    sym->st_value &= ~1;
11629
11630
0
  return true;
11631
0
}
11632
11633
/* Write out a plt0 entry to the beginning of .plt.  */
11634
11635
static bool
11636
mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11637
0
{
11638
0
  bfd_byte *loc;
11639
0
  bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11640
0
  static const bfd_vma *plt_entry;
11641
0
  struct mips_elf_link_hash_table *htab;
11642
11643
0
  htab = mips_elf_hash_table (info);
11644
0
  BFD_ASSERT (htab != NULL);
11645
11646
0
  if (ABI_64_P (output_bfd))
11647
0
    plt_entry = (htab->compact_branches
11648
0
     ? mipsr6_n64_exec_plt0_entry_compact
11649
0
     : mips_n64_exec_plt0_entry);
11650
0
  else if (ABI_N32_P (output_bfd))
11651
0
    plt_entry = (htab->compact_branches
11652
0
     ? mipsr6_n32_exec_plt0_entry_compact
11653
0
     : mips_n32_exec_plt0_entry);
11654
0
  else if (!htab->plt_header_is_comp)
11655
0
    plt_entry = (htab->compact_branches
11656
0
     ? mipsr6_o32_exec_plt0_entry_compact
11657
0
     : mips_o32_exec_plt0_entry);
11658
0
  else if (htab->insn32)
11659
0
    plt_entry = micromips_insn32_o32_exec_plt0_entry;
11660
0
  else
11661
0
    plt_entry = micromips_o32_exec_plt0_entry;
11662
11663
  /* Calculate the value of .got.plt.  */
11664
0
  gotplt_value = (htab->root.sgotplt->output_section->vma
11665
0
      + htab->root.sgotplt->output_offset);
11666
0
  gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11667
0
  gotplt_value_low = gotplt_value & 0xffff;
11668
11669
  /* The PLT sequence is not safe for N64 if .got.plt's address can
11670
     not be loaded in two instructions.  */
11671
0
  if (ABI_64_P (output_bfd)
11672
0
      && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11673
0
    {
11674
0
      _bfd_error_handler
11675
  /* xgettext:c-format */
11676
0
  (_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
11677
0
     "supported; consider using `-Ttext-segment=...'"),
11678
0
   output_bfd,
11679
0
   htab->root.sgotplt->output_section,
11680
0
   (int64_t) gotplt_value);
11681
0
      bfd_set_error (bfd_error_no_error);
11682
0
      return false;
11683
0
    }
11684
11685
  /* Install the PLT header.  */
11686
0
  loc = htab->root.splt->contents;
11687
0
  if (plt_entry == micromips_o32_exec_plt0_entry)
11688
0
    {
11689
0
      bfd_vma gotpc_offset;
11690
0
      bfd_vma loc_address;
11691
0
      size_t i;
11692
11693
0
      BFD_ASSERT (gotplt_value % 4 == 0);
11694
11695
0
      loc_address = (htab->root.splt->output_section->vma
11696
0
         + htab->root.splt->output_offset);
11697
0
      gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11698
11699
      /* ADDIUPC has a span of +/-16MB, check we're in range.  */
11700
0
      if (gotpc_offset + 0x1000000 >= 0x2000000)
11701
0
  {
11702
0
    _bfd_error_handler
11703
      /* xgettext:c-format */
11704
0
      (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11705
0
         "beyond the range of ADDIUPC"),
11706
0
       output_bfd,
11707
0
       htab->root.sgotplt->output_section,
11708
0
       (int64_t) gotpc_offset,
11709
0
       htab->root.splt->output_section);
11710
0
    bfd_set_error (bfd_error_no_error);
11711
0
    return false;
11712
0
  }
11713
0
      bfd_put_16 (output_bfd,
11714
0
      plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11715
0
      bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11716
0
      for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11717
0
  bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11718
0
    }
11719
0
  else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11720
0
    {
11721
0
      size_t i;
11722
11723
0
      bfd_put_16 (output_bfd, plt_entry[0], loc);
11724
0
      bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11725
0
      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11726
0
      bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11727
0
      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11728
0
      bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11729
0
      for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11730
0
  bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11731
0
    }
11732
0
  else
11733
0
    {
11734
0
      bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11735
0
      bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11736
0
      bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11737
0
      bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11738
0
      bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11739
0
      bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11740
0
      bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11741
0
      bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11742
0
    }
11743
11744
0
  return true;
11745
0
}
11746
11747
/* Install the PLT header for a VxWorks executable and finalize the
11748
   contents of .rela.plt.unloaded.  */
11749
11750
static void
11751
mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11752
0
{
11753
0
  Elf_Internal_Rela rela;
11754
0
  bfd_byte *loc;
11755
0
  bfd_vma got_value, got_value_high, got_value_low, plt_address;
11756
0
  static const bfd_vma *plt_entry;
11757
0
  struct mips_elf_link_hash_table *htab;
11758
11759
0
  htab = mips_elf_hash_table (info);
11760
0
  BFD_ASSERT (htab != NULL);
11761
11762
0
  plt_entry = mips_vxworks_exec_plt0_entry;
11763
11764
  /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
11765
0
  got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11766
0
         + htab->root.hgot->root.u.def.section->output_offset
11767
0
         + htab->root.hgot->root.u.def.value);
11768
11769
0
  got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11770
0
  got_value_low = got_value & 0xffff;
11771
11772
  /* Calculate the address of the PLT header.  */
11773
0
  plt_address = (htab->root.splt->output_section->vma
11774
0
     + htab->root.splt->output_offset);
11775
11776
  /* Install the PLT header.  */
11777
0
  loc = htab->root.splt->contents;
11778
0
  bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11779
0
  bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11780
0
  bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11781
0
  bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11782
0
  bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11783
0
  bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11784
11785
  /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_).  */
11786
0
  loc = htab->srelplt2->contents;
11787
0
  rela.r_offset = plt_address;
11788
0
  rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11789
0
  rela.r_addend = 0;
11790
0
  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11791
0
  loc += sizeof (Elf32_External_Rela);
11792
11793
  /* Output the relocation for the following addiu of
11794
     %lo(_GLOBAL_OFFSET_TABLE_).  */
11795
0
  rela.r_offset += 4;
11796
0
  rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11797
0
  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11798
0
  loc += sizeof (Elf32_External_Rela);
11799
11800
  /* Fix up the remaining relocations.  They may have the wrong
11801
     symbol index for _G_O_T_ or _P_L_T_ depending on the order
11802
     in which symbols were output.  */
11803
0
  while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11804
0
    {
11805
0
      Elf_Internal_Rela rel;
11806
11807
0
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11808
0
      rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11809
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11810
0
      loc += sizeof (Elf32_External_Rela);
11811
11812
0
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11813
0
      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11814
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11815
0
      loc += sizeof (Elf32_External_Rela);
11816
11817
0
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11818
0
      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11819
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11820
0
      loc += sizeof (Elf32_External_Rela);
11821
0
    }
11822
0
}
11823
11824
/* Install the PLT header for a VxWorks shared library.  */
11825
11826
static void
11827
mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11828
0
{
11829
0
  unsigned int i;
11830
0
  struct mips_elf_link_hash_table *htab;
11831
11832
0
  htab = mips_elf_hash_table (info);
11833
0
  BFD_ASSERT (htab != NULL);
11834
11835
  /* We just need to copy the entry byte-by-byte.  */
11836
0
  for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11837
0
    bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11838
0
    htab->root.splt->contents + i * 4);
11839
0
}
11840
11841
/* Finish up the dynamic sections.  */
11842
11843
bool
11844
_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11845
               struct bfd_link_info *info)
11846
0
{
11847
0
  bfd *dynobj;
11848
0
  asection *sdyn;
11849
0
  asection *sgot;
11850
0
  struct mips_got_info *gg, *g;
11851
0
  struct mips_elf_link_hash_table *htab;
11852
11853
0
  htab = mips_elf_hash_table (info);
11854
0
  BFD_ASSERT (htab != NULL);
11855
11856
0
  dynobj = elf_hash_table (info)->dynobj;
11857
11858
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11859
11860
0
  sgot = htab->root.sgot;
11861
0
  gg = htab->got_info;
11862
11863
0
  if (elf_hash_table (info)->dynamic_sections_created)
11864
0
    {
11865
0
      bfd_byte *b;
11866
0
      int dyn_to_skip = 0, dyn_skipped = 0;
11867
11868
0
      BFD_ASSERT (sdyn != NULL);
11869
0
      BFD_ASSERT (gg != NULL);
11870
11871
0
      g = mips_elf_bfd_got (output_bfd, false);
11872
0
      BFD_ASSERT (g != NULL);
11873
11874
0
      for (b = sdyn->contents;
11875
0
     b < sdyn->contents + sdyn->size;
11876
0
     b += MIPS_ELF_DYN_SIZE (dynobj))
11877
0
  {
11878
0
    Elf_Internal_Dyn dyn;
11879
0
    const char *name;
11880
0
    size_t elemsize;
11881
0
    asection *s;
11882
0
    bool swap_out_p;
11883
11884
    /* Read in the current dynamic entry.  */
11885
0
    (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11886
11887
    /* Assume that we're going to modify it and write it out.  */
11888
0
    swap_out_p = true;
11889
11890
0
    switch (dyn.d_tag)
11891
0
      {
11892
0
      case DT_RELENT:
11893
0
        dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11894
0
        break;
11895
11896
0
      case DT_RELAENT:
11897
0
        BFD_ASSERT (htab->root.target_os == is_vxworks);
11898
0
        dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11899
0
        break;
11900
11901
0
      case DT_STRSZ:
11902
        /* Rewrite DT_STRSZ.  */
11903
0
        dyn.d_un.d_val =
11904
0
    _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11905
0
        break;
11906
11907
0
      case DT_PLTGOT:
11908
0
        s = htab->root.sgot;
11909
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11910
0
        break;
11911
11912
0
      case DT_MIPS_PLTGOT:
11913
0
        s = htab->root.sgotplt;
11914
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11915
0
        break;
11916
11917
0
      case DT_MIPS_RLD_VERSION:
11918
0
        dyn.d_un.d_val = 1; /* XXX */
11919
0
        break;
11920
11921
0
      case DT_MIPS_FLAGS:
11922
0
        dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11923
0
        break;
11924
11925
0
      case DT_MIPS_TIME_STAMP:
11926
0
        {
11927
0
    time_t t;
11928
0
    time (&t);
11929
0
    dyn.d_un.d_val = t;
11930
0
        }
11931
0
        break;
11932
11933
0
      case DT_MIPS_ICHECKSUM:
11934
        /* XXX FIXME: */
11935
0
        swap_out_p = false;
11936
0
        break;
11937
11938
0
      case DT_MIPS_IVERSION:
11939
        /* XXX FIXME: */
11940
0
        swap_out_p = false;
11941
0
        break;
11942
11943
0
      case DT_MIPS_BASE_ADDRESS:
11944
0
        s = output_bfd->sections;
11945
0
        BFD_ASSERT (s != NULL);
11946
0
        dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11947
0
        break;
11948
11949
0
      case DT_MIPS_LOCAL_GOTNO:
11950
0
        dyn.d_un.d_val = g->local_gotno;
11951
0
        break;
11952
11953
0
      case DT_MIPS_UNREFEXTNO:
11954
        /* The index into the dynamic symbol table which is the
11955
     entry of the first external symbol that is not
11956
     referenced within the same object.  */
11957
0
        dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11958
0
        break;
11959
11960
0
      case DT_MIPS_GOTSYM:
11961
0
        if (htab->global_gotsym)
11962
0
    {
11963
0
      dyn.d_un.d_val = htab->global_gotsym->dynindx;
11964
0
      break;
11965
0
    }
11966
        /* In case if we don't have global got symbols we default
11967
     to setting DT_MIPS_GOTSYM to the same value as
11968
     DT_MIPS_SYMTABNO.  */
11969
        /* Fall through.  */
11970
11971
0
      case DT_MIPS_SYMTABNO:
11972
0
        name = ".dynsym";
11973
0
        elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11974
0
        s = bfd_get_linker_section (dynobj, name);
11975
11976
0
        if (s != NULL)
11977
0
    dyn.d_un.d_val = s->size / elemsize;
11978
0
        else
11979
0
    dyn.d_un.d_val = 0;
11980
0
        break;
11981
11982
0
      case DT_MIPS_HIPAGENO:
11983
0
        dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
11984
0
        break;
11985
11986
0
      case DT_MIPS_RLD_MAP:
11987
0
        {
11988
0
    struct elf_link_hash_entry *h;
11989
0
    h = mips_elf_hash_table (info)->rld_symbol;
11990
0
    if (!h)
11991
0
      {
11992
0
        dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11993
0
        swap_out_p = false;
11994
0
        break;
11995
0
      }
11996
0
    s = h->root.u.def.section;
11997
11998
    /* The MIPS_RLD_MAP tag stores the absolute address of the
11999
       debug pointer.  */
12000
0
    dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
12001
0
          + h->root.u.def.value);
12002
0
        }
12003
0
        break;
12004
12005
0
      case DT_MIPS_RLD_MAP_REL:
12006
0
        {
12007
0
    struct elf_link_hash_entry *h;
12008
0
    bfd_vma dt_addr, rld_addr;
12009
0
    h = mips_elf_hash_table (info)->rld_symbol;
12010
0
    if (!h)
12011
0
      {
12012
0
        dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12013
0
        swap_out_p = false;
12014
0
        break;
12015
0
      }
12016
0
    s = h->root.u.def.section;
12017
12018
    /* The MIPS_RLD_MAP_REL tag stores the offset to the debug
12019
       pointer, relative to the address of the tag.  */
12020
0
    dt_addr = (sdyn->output_section->vma + sdyn->output_offset
12021
0
         + (b - sdyn->contents));
12022
0
    rld_addr = (s->output_section->vma + s->output_offset
12023
0
          + h->root.u.def.value);
12024
0
    dyn.d_un.d_ptr = rld_addr - dt_addr;
12025
0
        }
12026
0
        break;
12027
12028
0
      case DT_MIPS_OPTIONS:
12029
0
        s = (bfd_get_section_by_name
12030
0
       (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
12031
0
        dyn.d_un.d_ptr = s->vma;
12032
0
        break;
12033
12034
0
      case DT_PLTREL:
12035
0
        BFD_ASSERT (htab->use_plts_and_copy_relocs);
12036
0
        if (htab->root.target_os == is_vxworks)
12037
0
    dyn.d_un.d_val = DT_RELA;
12038
0
        else
12039
0
    dyn.d_un.d_val = DT_REL;
12040
0
        break;
12041
12042
0
      case DT_PLTRELSZ:
12043
0
        BFD_ASSERT (htab->use_plts_and_copy_relocs);
12044
0
        dyn.d_un.d_val = htab->root.srelplt->size;
12045
0
        break;
12046
12047
0
      case DT_JMPREL:
12048
0
        BFD_ASSERT (htab->use_plts_and_copy_relocs);
12049
0
        dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
12050
0
        + htab->root.srelplt->output_offset);
12051
0
        break;
12052
12053
0
      case DT_TEXTREL:
12054
        /* If we didn't need any text relocations after all, delete
12055
     the dynamic tag.  */
12056
0
        if (!(info->flags & DF_TEXTREL))
12057
0
    {
12058
0
      dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12059
0
      swap_out_p = false;
12060
0
    }
12061
0
        break;
12062
12063
0
      case DT_FLAGS:
12064
        /* If we didn't need any text relocations after all, clear
12065
     DF_TEXTREL from DT_FLAGS.  */
12066
0
        if (!(info->flags & DF_TEXTREL))
12067
0
    dyn.d_un.d_val &= ~DF_TEXTREL;
12068
0
        else
12069
0
    swap_out_p = false;
12070
0
        break;
12071
12072
0
      case DT_MIPS_XHASH:
12073
0
        name = ".MIPS.xhash";
12074
0
        s = bfd_get_linker_section (dynobj, name);
12075
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
12076
0
        break;
12077
12078
0
      default:
12079
0
        swap_out_p = false;
12080
0
        if (htab->root.target_os == is_vxworks
12081
0
      && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
12082
0
    swap_out_p = true;
12083
0
        break;
12084
0
      }
12085
12086
0
    if (swap_out_p || dyn_skipped)
12087
0
      (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12088
0
        (dynobj, &dyn, b - dyn_skipped);
12089
12090
0
    if (dyn_to_skip)
12091
0
      {
12092
0
        dyn_skipped += dyn_to_skip;
12093
0
        dyn_to_skip = 0;
12094
0
      }
12095
0
  }
12096
12097
      /* Wipe out any trailing entries if we shifted down a dynamic tag.  */
12098
0
      if (dyn_skipped > 0)
12099
0
  memset (b - dyn_skipped, 0, dyn_skipped);
12100
0
    }
12101
12102
0
  if (sgot != NULL && sgot->size > 0
12103
0
      && !bfd_is_abs_section (sgot->output_section))
12104
0
    {
12105
0
      if (htab->root.target_os == is_vxworks)
12106
0
  {
12107
    /* The first entry of the global offset table points to the
12108
       ".dynamic" section.  The second is initialized by the
12109
       loader and contains the shared library identifier.
12110
       The third is also initialized by the loader and points
12111
       to the lazy resolution stub.  */
12112
0
    MIPS_ELF_PUT_WORD (output_bfd,
12113
0
           sdyn->output_offset + sdyn->output_section->vma,
12114
0
           sgot->contents);
12115
0
    MIPS_ELF_PUT_WORD (output_bfd, 0,
12116
0
           sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12117
0
    MIPS_ELF_PUT_WORD (output_bfd, 0,
12118
0
           sgot->contents
12119
0
           + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
12120
0
  }
12121
0
      else
12122
0
  {
12123
    /* The first entry of the global offset table will be filled at
12124
       runtime. The second entry will be used by some runtime loaders.
12125
       This isn't the case of IRIX rld.  */
12126
0
    MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
12127
0
    MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12128
0
           sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12129
0
  }
12130
12131
0
      elf_section_data (sgot->output_section)->this_hdr.sh_entsize
12132
0
   = MIPS_ELF_GOT_SIZE (output_bfd);
12133
0
    }
12134
12135
  /* Generate dynamic relocations for the non-primary gots.  */
12136
0
  if (gg != NULL && gg->next)
12137
0
    {
12138
0
      Elf_Internal_Rela rel[3];
12139
0
      bfd_vma addend = 0;
12140
12141
0
      memset (rel, 0, sizeof (rel));
12142
0
      rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
12143
12144
0
      for (g = gg->next; g->next != gg; g = g->next)
12145
0
  {
12146
0
    bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
12147
0
      + g->next->tls_gotno;
12148
12149
0
    MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
12150
0
           + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12151
0
    MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12152
0
           sgot->contents
12153
0
           + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12154
12155
0
    if (! bfd_link_pic (info))
12156
0
      continue;
12157
12158
0
    for (; got_index < g->local_gotno; got_index++)
12159
0
      {
12160
0
        if (got_index >= g->assigned_low_gotno
12161
0
      && got_index <= g->assigned_high_gotno)
12162
0
    continue;
12163
12164
0
        rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
12165
0
    = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
12166
0
        if (!(mips_elf_create_dynamic_relocation
12167
0
        (output_bfd, info, rel, NULL,
12168
0
         bfd_abs_section_ptr,
12169
0
         0, &addend, sgot)))
12170
0
    return false;
12171
0
        BFD_ASSERT (addend == 0);
12172
0
      }
12173
0
  }
12174
0
    }
12175
12176
  /* The generation of dynamic relocations for the non-primary gots
12177
     adds more dynamic relocations.  We cannot count them until
12178
     here.  */
12179
12180
0
  if (elf_hash_table (info)->dynamic_sections_created)
12181
0
    {
12182
0
      bfd_byte *b;
12183
0
      bool swap_out_p;
12184
12185
0
      BFD_ASSERT (sdyn != NULL);
12186
12187
0
      for (b = sdyn->contents;
12188
0
     b < sdyn->contents + sdyn->size;
12189
0
     b += MIPS_ELF_DYN_SIZE (dynobj))
12190
0
  {
12191
0
    Elf_Internal_Dyn dyn;
12192
0
    asection *s;
12193
12194
    /* Read in the current dynamic entry.  */
12195
0
    (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
12196
12197
    /* Assume that we're going to modify it and write it out.  */
12198
0
    swap_out_p = true;
12199
12200
0
    switch (dyn.d_tag)
12201
0
      {
12202
0
      case DT_RELSZ:
12203
        /* Reduce DT_RELSZ to account for any relocations we
12204
     decided not to make.  This is for the n64 irix rld,
12205
     which doesn't seem to apply any relocations if there
12206
     are trailing null entries.  */
12207
0
        s = mips_elf_rel_dyn_section (info, false);
12208
0
        dyn.d_un.d_val = (s->reloc_count
12209
0
        * (ABI_64_P (output_bfd)
12210
0
           ? sizeof (Elf64_Mips_External_Rel)
12211
0
           : sizeof (Elf32_External_Rel)));
12212
        /* Adjust the section size too.  Tools like the prelinker
12213
     can reasonably expect the values to the same.  */
12214
0
        BFD_ASSERT (!bfd_is_abs_section (s->output_section));
12215
0
        elf_section_data (s->output_section)->this_hdr.sh_size
12216
0
    = dyn.d_un.d_val;
12217
0
        break;
12218
12219
0
      default:
12220
0
        swap_out_p = false;
12221
0
        break;
12222
0
      }
12223
12224
0
    if (swap_out_p)
12225
0
      (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12226
0
        (dynobj, &dyn, b);
12227
0
  }
12228
0
    }
12229
12230
0
  {
12231
0
    asection *s;
12232
0
    Elf32_compact_rel cpt;
12233
12234
0
    if (SGI_COMPAT (output_bfd))
12235
0
      {
12236
  /* Write .compact_rel section out.  */
12237
0
  s = bfd_get_linker_section (dynobj, ".compact_rel");
12238
0
  if (s != NULL)
12239
0
    {
12240
0
      cpt.id1 = 1;
12241
0
      cpt.num = s->reloc_count;
12242
0
      cpt.id2 = 2;
12243
0
      cpt.offset = (s->output_section->filepos
12244
0
        + sizeof (Elf32_External_compact_rel));
12245
0
      cpt.reserved0 = 0;
12246
0
      cpt.reserved1 = 0;
12247
0
      bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
12248
0
              ((Elf32_External_compact_rel *)
12249
0
               s->contents));
12250
12251
      /* Clean up a dummy stub function entry in .text.  */
12252
0
      if (htab->sstubs != NULL
12253
0
    && htab->sstubs->contents != NULL)
12254
0
        {
12255
0
    file_ptr dummy_offset;
12256
12257
0
    BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
12258
0
    dummy_offset = htab->sstubs->size - htab->function_stub_size;
12259
0
    memset (htab->sstubs->contents + dummy_offset, 0,
12260
0
      htab->function_stub_size);
12261
0
        }
12262
0
    }
12263
0
      }
12264
12265
    /* The psABI says that the dynamic relocations must be sorted in
12266
       increasing order of r_symndx.  The VxWorks EABI doesn't require
12267
       this, and because the code below handles REL rather than RELA
12268
       relocations, using it for VxWorks would be outright harmful.  */
12269
0
    if (htab->root.target_os != is_vxworks)
12270
0
      {
12271
0
  s = mips_elf_rel_dyn_section (info, false);
12272
0
  if (s != NULL
12273
0
      && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
12274
0
    {
12275
0
      reldyn_sorting_bfd = output_bfd;
12276
12277
0
      if (ABI_64_P (output_bfd))
12278
0
        qsort ((Elf64_External_Rel *) s->contents + 1,
12279
0
         s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
12280
0
         sort_dynamic_relocs_64);
12281
0
      else
12282
0
        qsort ((Elf32_External_Rel *) s->contents + 1,
12283
0
         s->reloc_count - 1, sizeof (Elf32_External_Rel),
12284
0
         sort_dynamic_relocs);
12285
0
    }
12286
0
      }
12287
0
  }
12288
12289
0
  if (htab->root.splt && htab->root.splt->size > 0)
12290
0
    {
12291
0
      if (htab->root.target_os == is_vxworks)
12292
0
  {
12293
0
    if (bfd_link_pic (info))
12294
0
      mips_vxworks_finish_shared_plt (output_bfd, info);
12295
0
    else
12296
0
      mips_vxworks_finish_exec_plt (output_bfd, info);
12297
0
  }
12298
0
      else
12299
0
  {
12300
0
    BFD_ASSERT (!bfd_link_pic (info));
12301
0
    if (!mips_finish_exec_plt (output_bfd, info))
12302
0
      return false;
12303
0
  }
12304
0
    }
12305
0
  return true;
12306
0
}
12307
12308
12309
/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags.  */
12310
12311
static void
12312
mips_set_isa_flags (bfd *abfd)
12313
24
{
12314
24
  flagword val;
12315
12316
24
  switch (bfd_get_mach (abfd))
12317
24
    {
12318
0
    default:
12319
0
      if (ABI_N32_P (abfd) || ABI_64_P (abfd))
12320
0
        val = MIPS_DEFAULT_R6 ? E_MIPS_ARCH_64R6 : E_MIPS_ARCH_3;
12321
0
      else
12322
0
        val = MIPS_DEFAULT_R6 ? E_MIPS_ARCH_32R6 : E_MIPS_ARCH_1;
12323
0
      break;
12324
12325
23
    case bfd_mach_mips3000:
12326
23
      val = E_MIPS_ARCH_1;
12327
23
      break;
12328
12329
0
    case bfd_mach_mips3900:
12330
0
      val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
12331
0
      break;
12332
12333
0
    case bfd_mach_mips6000:
12334
0
      val = E_MIPS_ARCH_2;
12335
0
      break;
12336
12337
0
    case bfd_mach_mips4010:
12338
0
      val = E_MIPS_ARCH_2 | E_MIPS_MACH_4010;
12339
0
      break;
12340
12341
0
    case bfd_mach_mips_allegrex:
12342
0
      val = E_MIPS_ARCH_2 | E_MIPS_MACH_ALLEGREX;
12343
0
      break;
12344
12345
0
    case bfd_mach_mips4000:
12346
0
    case bfd_mach_mips4300:
12347
0
    case bfd_mach_mips4400:
12348
0
    case bfd_mach_mips4600:
12349
0
      val = E_MIPS_ARCH_3;
12350
0
      break;
12351
12352
0
    case bfd_mach_mips4100:
12353
0
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
12354
0
      break;
12355
12356
0
    case bfd_mach_mips4111:
12357
0
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
12358
0
      break;
12359
12360
0
    case bfd_mach_mips4120:
12361
0
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
12362
0
      break;
12363
12364
0
    case bfd_mach_mips4650:
12365
0
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
12366
0
      break;
12367
12368
0
    case bfd_mach_mips5400:
12369
0
      val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
12370
0
      break;
12371
12372
0
    case bfd_mach_mips5500:
12373
0
      val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
12374
0
      break;
12375
12376
0
    case bfd_mach_mips5900:
12377
0
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
12378
0
      break;
12379
12380
0
    case bfd_mach_mips9000:
12381
0
      val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
12382
0
      break;
12383
12384
0
    case bfd_mach_mips5000:
12385
0
    case bfd_mach_mips7000:
12386
1
    case bfd_mach_mips8000:
12387
1
    case bfd_mach_mips10000:
12388
1
    case bfd_mach_mips12000:
12389
1
    case bfd_mach_mips14000:
12390
1
    case bfd_mach_mips16000:
12391
1
      val = E_MIPS_ARCH_4;
12392
1
      break;
12393
12394
0
    case bfd_mach_mips5:
12395
0
      val = E_MIPS_ARCH_5;
12396
0
      break;
12397
12398
0
    case bfd_mach_mips_loongson_2e:
12399
0
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
12400
0
      break;
12401
12402
0
    case bfd_mach_mips_loongson_2f:
12403
0
      val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
12404
0
      break;
12405
12406
0
    case bfd_mach_mips_sb1:
12407
0
      val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
12408
0
      break;
12409
12410
0
    case bfd_mach_mips_gs464:
12411
0
      val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464;
12412
0
      break;
12413
12414
0
    case bfd_mach_mips_gs464e:
12415
0
      val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464E;
12416
0
      break;
12417
12418
0
    case bfd_mach_mips_gs264e:
12419
0
      val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS264E;
12420
0
      break;
12421
12422
0
    case bfd_mach_mips_octeon:
12423
0
    case bfd_mach_mips_octeonp:
12424
0
      val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
12425
0
      break;
12426
12427
0
    case bfd_mach_mips_octeon3:
12428
0
      val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON3;
12429
0
      break;
12430
12431
0
    case bfd_mach_mips_xlr:
12432
0
      val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
12433
0
      break;
12434
12435
0
    case bfd_mach_mips_octeon2:
12436
0
      val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
12437
0
      break;
12438
12439
0
    case bfd_mach_mipsisa32:
12440
0
      val = E_MIPS_ARCH_32;
12441
0
      break;
12442
12443
0
    case bfd_mach_mipsisa64:
12444
0
      val = E_MIPS_ARCH_64;
12445
0
      break;
12446
12447
0
    case bfd_mach_mipsisa32r2:
12448
0
    case bfd_mach_mipsisa32r3:
12449
0
    case bfd_mach_mipsisa32r5:
12450
0
      val = E_MIPS_ARCH_32R2;
12451
0
      break;
12452
12453
0
    case bfd_mach_mips_interaptiv_mr2:
12454
0
      val = E_MIPS_ARCH_32R2 | E_MIPS_MACH_IAMR2;
12455
0
      break;
12456
12457
0
    case bfd_mach_mipsisa64r2:
12458
0
    case bfd_mach_mipsisa64r3:
12459
0
    case bfd_mach_mipsisa64r5:
12460
0
      val = E_MIPS_ARCH_64R2;
12461
0
      break;
12462
12463
0
    case bfd_mach_mipsisa32r6:
12464
0
      val = E_MIPS_ARCH_32R6;
12465
0
      break;
12466
12467
0
    case bfd_mach_mipsisa64r6:
12468
0
      val = E_MIPS_ARCH_64R6;
12469
0
      break;
12470
24
    }
12471
24
  elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12472
24
  elf_elfheader (abfd)->e_flags |= val;
12473
12474
24
}
12475
12476
12477
/* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
12478
   Don't do so for code sections.  We want to keep ordering of HI16/LO16
12479
   as is.  On the other hand, elf-eh-frame.c processing requires .eh_frame
12480
   relocs to be sorted.  */
12481
12482
bool
12483
_bfd_mips_elf_sort_relocs_p (asection *sec)
12484
0
{
12485
0
  return (sec->flags & SEC_CODE) == 0;
12486
0
}
12487
12488
12489
/* The final processing done just before writing out a MIPS ELF object
12490
   file.  This gets the MIPS architecture right based on the machine
12491
   number.  This is used by both the 32-bit and the 64-bit ABI.  */
12492
12493
void
12494
_bfd_mips_final_write_processing (bfd *abfd)
12495
26
{
12496
26
  unsigned int i;
12497
26
  Elf_Internal_Shdr **hdrpp;
12498
26
  const char *name;
12499
26
  asection *sec;
12500
12501
  /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
12502
     is nonzero.  This is for compatibility with old objects, which used
12503
     a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH.  */
12504
26
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
12505
24
    mips_set_isa_flags (abfd);
12506
12507
  /* Set the sh_info field for .gptab sections and other appropriate
12508
     info for each special section.  */
12509
26
  for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
12510
455
       i < elf_numsections (abfd);
12511
429
       i++, hdrpp++)
12512
429
    {
12513
429
      switch ((*hdrpp)->sh_type)
12514
429
  {
12515
0
  case SHT_MIPS_MSYM:
12516
0
  case SHT_MIPS_LIBLIST:
12517
0
    sec = bfd_get_section_by_name (abfd, ".dynstr");
12518
0
    if (sec != NULL)
12519
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12520
0
    break;
12521
12522
0
  case SHT_MIPS_GPTAB:
12523
0
    BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12524
0
    name = bfd_section_name ((*hdrpp)->bfd_section);
12525
0
    BFD_ASSERT (name != NULL
12526
0
          && startswith (name, ".gptab."));
12527
0
    sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
12528
0
    BFD_ASSERT (sec != NULL);
12529
0
    (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12530
0
    break;
12531
12532
0
  case SHT_MIPS_CONTENT:
12533
0
    BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12534
0
    name = bfd_section_name ((*hdrpp)->bfd_section);
12535
0
    BFD_ASSERT (name != NULL
12536
0
          && startswith (name, ".MIPS.content"));
12537
0
    sec = bfd_get_section_by_name (abfd,
12538
0
           name + sizeof ".MIPS.content" - 1);
12539
0
    BFD_ASSERT (sec != NULL);
12540
0
    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12541
0
    break;
12542
12543
0
  case SHT_MIPS_SYMBOL_LIB:
12544
0
    sec = bfd_get_section_by_name (abfd, ".dynsym");
12545
0
    if (sec != NULL)
12546
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12547
0
    sec = bfd_get_section_by_name (abfd, ".liblist");
12548
0
    if (sec != NULL)
12549
0
      (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12550
0
    break;
12551
12552
0
  case SHT_MIPS_EVENTS:
12553
0
    BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12554
0
    name = bfd_section_name ((*hdrpp)->bfd_section);
12555
0
    BFD_ASSERT (name != NULL);
12556
0
    if (startswith (name, ".MIPS.events"))
12557
0
      sec = bfd_get_section_by_name (abfd,
12558
0
             name + sizeof ".MIPS.events" - 1);
12559
0
    else
12560
0
      {
12561
0
        BFD_ASSERT (startswith (name, ".MIPS.post_rel"));
12562
0
        sec = bfd_get_section_by_name (abfd,
12563
0
               (name
12564
0
                + sizeof ".MIPS.post_rel" - 1));
12565
0
      }
12566
0
    BFD_ASSERT (sec != NULL);
12567
0
    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12568
0
    break;
12569
12570
0
  case SHT_MIPS_XHASH:
12571
0
    sec = bfd_get_section_by_name (abfd, ".dynsym");
12572
0
    if (sec != NULL)
12573
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12574
429
  }
12575
429
    }
12576
26
}
12577
12578
bool
12579
_bfd_mips_elf_final_write_processing (bfd *abfd)
12580
26
{
12581
26
  _bfd_mips_final_write_processing (abfd);
12582
26
  return _bfd_elf_final_write_processing (abfd);
12583
26
}
12584

12585
/* When creating an IRIX5 executable, we need REGINFO and RTPROC
12586
   segments.  */
12587
12588
int
12589
_bfd_mips_elf_additional_program_headers (bfd *abfd,
12590
            struct bfd_link_info *info ATTRIBUTE_UNUSED)
12591
0
{
12592
0
  asection *s;
12593
0
  int ret = 0;
12594
12595
  /* See if we need a PT_MIPS_REGINFO segment.  */
12596
0
  s = bfd_get_section_by_name (abfd, ".reginfo");
12597
0
  if (s && (s->flags & SEC_LOAD))
12598
0
    ++ret;
12599
12600
  /* See if we need a PT_MIPS_ABIFLAGS segment.  */
12601
0
  if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
12602
0
    ++ret;
12603
12604
  /* See if we need a PT_MIPS_OPTIONS segment.  */
12605
0
  if (IRIX_COMPAT (abfd) == ict_irix6
12606
0
      && bfd_get_section_by_name (abfd,
12607
0
          MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
12608
0
    ++ret;
12609
12610
  /* See if we need a PT_MIPS_RTPROC segment.  */
12611
0
  if (IRIX_COMPAT (abfd) == ict_irix5
12612
0
      && bfd_get_section_by_name (abfd, ".dynamic")
12613
0
      && bfd_get_section_by_name (abfd, ".mdebug"))
12614
0
    ++ret;
12615
12616
  /* Allocate a PT_NULL header in dynamic objects.  See
12617
     _bfd_mips_elf_modify_segment_map for details.  */
12618
0
  if (!SGI_COMPAT (abfd)
12619
0
      && bfd_get_section_by_name (abfd, ".dynamic"))
12620
0
    ++ret;
12621
12622
0
  return ret;
12623
0
}
12624
12625
/* Modify the segment map for an IRIX5 executable.  */
12626
12627
bool
12628
_bfd_mips_elf_modify_segment_map (bfd *abfd,
12629
          struct bfd_link_info *info)
12630
3
{
12631
3
  asection *s;
12632
3
  struct elf_segment_map *m, **pm;
12633
3
  size_t amt;
12634
12635
  /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
12636
     segment.  */
12637
3
  s = bfd_get_section_by_name (abfd, ".reginfo");
12638
3
  if (s != NULL && (s->flags & SEC_LOAD) != 0)
12639
0
    {
12640
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12641
0
  if (m->p_type == PT_MIPS_REGINFO)
12642
0
    break;
12643
0
      if (m == NULL)
12644
0
  {
12645
0
    amt = sizeof *m;
12646
0
    m = bfd_zalloc (abfd, amt);
12647
0
    if (m == NULL)
12648
0
      return false;
12649
12650
0
    m->p_type = PT_MIPS_REGINFO;
12651
0
    m->count = 1;
12652
0
    m->sections[0] = s;
12653
12654
    /* We want to put it after the PHDR and INTERP segments.  */
12655
0
    pm = &elf_seg_map (abfd);
12656
0
    while (*pm != NULL
12657
0
     && ((*pm)->p_type == PT_PHDR
12658
0
         || (*pm)->p_type == PT_INTERP))
12659
0
      pm = &(*pm)->next;
12660
12661
0
    m->next = *pm;
12662
0
    *pm = m;
12663
0
  }
12664
0
    }
12665
12666
  /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
12667
     segment.  */
12668
3
  s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
12669
3
  if (s != NULL && (s->flags & SEC_LOAD) != 0)
12670
0
    {
12671
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12672
0
  if (m->p_type == PT_MIPS_ABIFLAGS)
12673
0
    break;
12674
0
      if (m == NULL)
12675
0
  {
12676
0
    amt = sizeof *m;
12677
0
    m = bfd_zalloc (abfd, amt);
12678
0
    if (m == NULL)
12679
0
      return false;
12680
12681
0
    m->p_type = PT_MIPS_ABIFLAGS;
12682
0
    m->count = 1;
12683
0
    m->sections[0] = s;
12684
12685
    /* We want to put it after the PHDR and INTERP segments.  */
12686
0
    pm = &elf_seg_map (abfd);
12687
0
    while (*pm != NULL
12688
0
     && ((*pm)->p_type == PT_PHDR
12689
0
         || (*pm)->p_type == PT_INTERP))
12690
0
      pm = &(*pm)->next;
12691
12692
0
    m->next = *pm;
12693
0
    *pm = m;
12694
0
  }
12695
0
    }
12696
12697
  /* For IRIX 6, we don't have .mdebug sections, nor does anything but
12698
     .dynamic end up in PT_DYNAMIC.  However, we do have to insert a
12699
     PT_MIPS_OPTIONS segment immediately following the program header
12700
     table.  */
12701
3
  if (NEWABI_P (abfd)
12702
      /* On non-IRIX6 new abi, we'll have already created a segment
12703
   for this section, so don't create another.  I'm not sure this
12704
   is not also the case for IRIX 6, but I can't test it right
12705
   now.  */
12706
3
      && IRIX_COMPAT (abfd) == ict_irix6)
12707
0
    {
12708
0
      for (s = abfd->sections; s; s = s->next)
12709
0
  if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
12710
0
    break;
12711
12712
0
      if (s)
12713
0
  {
12714
0
    struct elf_segment_map *options_segment;
12715
12716
0
    pm = &elf_seg_map (abfd);
12717
0
    while (*pm != NULL
12718
0
     && ((*pm)->p_type == PT_PHDR
12719
0
         || (*pm)->p_type == PT_INTERP))
12720
0
      pm = &(*pm)->next;
12721
12722
0
    if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
12723
0
      {
12724
0
        amt = sizeof (struct elf_segment_map);
12725
0
        options_segment = bfd_zalloc (abfd, amt);
12726
0
        options_segment->next = *pm;
12727
0
        options_segment->p_type = PT_MIPS_OPTIONS;
12728
0
        options_segment->p_flags = PF_R;
12729
0
        options_segment->p_flags_valid = true;
12730
0
        options_segment->count = 1;
12731
0
        options_segment->sections[0] = s;
12732
0
        *pm = options_segment;
12733
0
      }
12734
0
  }
12735
0
    }
12736
3
  else
12737
3
    {
12738
3
      if (IRIX_COMPAT (abfd) == ict_irix5)
12739
3
  {
12740
    /* If there are .dynamic and .mdebug sections, we make a room
12741
       for the RTPROC header.  FIXME: Rewrite without section names.  */
12742
3
    if (bfd_get_section_by_name (abfd, ".interp") == NULL
12743
3
        && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12744
3
        && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12745
0
      {
12746
0
        for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12747
0
    if (m->p_type == PT_MIPS_RTPROC)
12748
0
      break;
12749
0
        if (m == NULL)
12750
0
    {
12751
0
      amt = sizeof *m;
12752
0
      m = bfd_zalloc (abfd, amt);
12753
0
      if (m == NULL)
12754
0
        return false;
12755
12756
0
      m->p_type = PT_MIPS_RTPROC;
12757
12758
0
      s = bfd_get_section_by_name (abfd, ".rtproc");
12759
0
      if (s == NULL)
12760
0
        {
12761
0
          m->count = 0;
12762
0
          m->p_flags = 0;
12763
0
          m->p_flags_valid = 1;
12764
0
        }
12765
0
      else
12766
0
        {
12767
0
          m->count = 1;
12768
0
          m->sections[0] = s;
12769
0
        }
12770
12771
      /* We want to put it after the DYNAMIC segment.  */
12772
0
      pm = &elf_seg_map (abfd);
12773
0
      while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12774
0
        pm = &(*pm)->next;
12775
0
      if (*pm != NULL)
12776
0
        pm = &(*pm)->next;
12777
12778
0
      m->next = *pm;
12779
0
      *pm = m;
12780
0
    }
12781
0
      }
12782
3
  }
12783
      /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
12784
   .dynstr, .dynsym, and .hash sections, and everything in
12785
   between.  */
12786
8
      for (pm = &elf_seg_map (abfd); *pm != NULL;
12787
5
     pm = &(*pm)->next)
12788
5
  if ((*pm)->p_type == PT_DYNAMIC)
12789
0
    break;
12790
3
      m = *pm;
12791
      /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12792
   glibc's dynamic linker has traditionally derived the number of
12793
   tags from the p_filesz field, and sometimes allocates stack
12794
   arrays of that size.  An overly-big PT_DYNAMIC segment can
12795
   be actively harmful in such cases.  Making PT_DYNAMIC contain
12796
   other sections can also make life hard for the prelinker,
12797
   which might move one of the other sections to a different
12798
   PT_LOAD segment.  */
12799
3
      if (SGI_COMPAT (abfd)
12800
3
    && m != NULL
12801
3
    && m->count == 1
12802
3
    && strcmp (m->sections[0]->name, ".dynamic") == 0)
12803
0
  {
12804
0
    static const char *sec_names[] =
12805
0
    {
12806
0
      ".dynamic", ".dynstr", ".dynsym", ".hash"
12807
0
    };
12808
0
    bfd_vma low, high;
12809
0
    unsigned int i, c;
12810
0
    struct elf_segment_map *n;
12811
12812
0
    low = ~(bfd_vma) 0;
12813
0
    high = 0;
12814
0
    for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12815
0
      {
12816
0
        s = bfd_get_section_by_name (abfd, sec_names[i]);
12817
0
        if (s != NULL && (s->flags & SEC_LOAD) != 0)
12818
0
    {
12819
0
      bfd_size_type sz;
12820
12821
0
      if (low > s->vma)
12822
0
        low = s->vma;
12823
0
      sz = s->size;
12824
0
      if (high < s->vma + sz)
12825
0
        high = s->vma + sz;
12826
0
    }
12827
0
      }
12828
12829
0
    c = 0;
12830
0
    for (s = abfd->sections; s != NULL; s = s->next)
12831
0
      if ((s->flags & SEC_LOAD) != 0
12832
0
    && s->vma >= low
12833
0
    && s->vma + s->size <= high)
12834
0
        ++c;
12835
12836
0
    amt = sizeof *n - sizeof (asection *) + c * sizeof (asection *);
12837
0
    n = bfd_zalloc (abfd, amt);
12838
0
    if (n == NULL)
12839
0
      return false;
12840
0
    *n = *m;
12841
0
    n->count = c;
12842
12843
0
    i = 0;
12844
0
    for (s = abfd->sections; s != NULL; s = s->next)
12845
0
      {
12846
0
        if ((s->flags & SEC_LOAD) != 0
12847
0
      && s->vma >= low
12848
0
      && s->vma + s->size <= high)
12849
0
    {
12850
0
      n->sections[i] = s;
12851
0
      ++i;
12852
0
    }
12853
0
      }
12854
12855
0
    *pm = n;
12856
0
  }
12857
3
    }
12858
12859
  /* Allocate a spare program header in dynamic objects so that tools
12860
     like the prelinker can add an extra PT_LOAD entry.
12861
12862
     If the prelinker needs to make room for a new PT_LOAD entry, its
12863
     standard procedure is to move the first (read-only) sections into
12864
     the new (writable) segment.  However, the MIPS ABI requires
12865
     .dynamic to be in a read-only segment, and the section will often
12866
     start within sizeof (ElfNN_Phdr) bytes of the last program header.
12867
12868
     Although the prelinker could in principle move .dynamic to a
12869
     writable segment, it seems better to allocate a spare program
12870
     header instead, and avoid the need to move any sections.
12871
     There is a long tradition of allocating spare dynamic tags,
12872
     so allocating a spare program header seems like a natural
12873
     extension.
12874
12875
     If INFO is NULL, we may be copying an already prelinked binary
12876
     with objcopy or strip, so do not add this header.  */
12877
3
  if (info != NULL
12878
3
      && !SGI_COMPAT (abfd)
12879
3
      && bfd_get_section_by_name (abfd, ".dynamic"))
12880
0
    {
12881
0
      for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12882
0
  if ((*pm)->p_type == PT_NULL)
12883
0
    break;
12884
0
      if (*pm == NULL)
12885
0
  {
12886
0
    m = bfd_zalloc (abfd, sizeof (*m));
12887
0
    if (m == NULL)
12888
0
      return false;
12889
12890
0
    m->p_type = PT_NULL;
12891
0
    *pm = m;
12892
0
  }
12893
0
    }
12894
12895
3
  return true;
12896
3
}
12897

12898
/* Return the section that should be marked against GC for a given
12899
   relocation.  */
12900
12901
asection *
12902
_bfd_mips_elf_gc_mark_hook (asection *sec,
12903
          struct bfd_link_info *info,
12904
          Elf_Internal_Rela *rel,
12905
          struct elf_link_hash_entry *h,
12906
          Elf_Internal_Sym *sym)
12907
0
{
12908
  /* ??? Do mips16 stub sections need to be handled special?  */
12909
12910
0
  if (h != NULL)
12911
0
    switch (ELF_R_TYPE (sec->owner, rel->r_info))
12912
0
      {
12913
0
      case R_MIPS_GNU_VTINHERIT:
12914
0
      case R_MIPS_GNU_VTENTRY:
12915
0
  return NULL;
12916
0
      }
12917
12918
0
  return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12919
0
}
12920
12921
/* Prevent .MIPS.abiflags from being discarded with --gc-sections.  */
12922
12923
bool
12924
_bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12925
              elf_gc_mark_hook_fn gc_mark_hook)
12926
0
{
12927
0
  bfd *sub;
12928
12929
0
  _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12930
12931
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12932
0
    {
12933
0
      asection *o;
12934
12935
0
      if (! is_mips_elf (sub))
12936
0
  continue;
12937
12938
0
      for (o = sub->sections; o != NULL; o = o->next)
12939
0
  if (!o->gc_mark
12940
0
      && MIPS_ELF_ABIFLAGS_SECTION_NAME_P (bfd_section_name (o)))
12941
0
    {
12942
0
      if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12943
0
        return false;
12944
0
    }
12945
0
    }
12946
12947
0
  return true;
12948
0
}
12949

12950
/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12951
   hiding the old indirect symbol.  Process additional relocation
12952
   information.  Also called for weakdefs, in which case we just let
12953
   _bfd_elf_link_hash_copy_indirect copy the flags for us.  */
12954
12955
void
12956
_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
12957
            struct elf_link_hash_entry *dir,
12958
            struct elf_link_hash_entry *ind)
12959
0
{
12960
0
  struct mips_elf_link_hash_entry *dirmips, *indmips;
12961
12962
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
12963
12964
0
  dirmips = (struct mips_elf_link_hash_entry *) dir;
12965
0
  indmips = (struct mips_elf_link_hash_entry *) ind;
12966
  /* Any absolute non-dynamic relocations against an indirect or weak
12967
     definition will be against the target symbol.  */
12968
0
  if (indmips->has_static_relocs)
12969
0
    dirmips->has_static_relocs = true;
12970
12971
0
  if (ind->root.type != bfd_link_hash_indirect)
12972
0
    return;
12973
12974
0
  dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12975
0
  if (indmips->readonly_reloc)
12976
0
    dirmips->readonly_reloc = true;
12977
0
  if (indmips->no_fn_stub)
12978
0
    dirmips->no_fn_stub = true;
12979
0
  if (indmips->fn_stub)
12980
0
    {
12981
0
      dirmips->fn_stub = indmips->fn_stub;
12982
0
      indmips->fn_stub = NULL;
12983
0
    }
12984
0
  if (indmips->need_fn_stub)
12985
0
    {
12986
0
      dirmips->need_fn_stub = true;
12987
0
      indmips->need_fn_stub = false;
12988
0
    }
12989
0
  if (indmips->call_stub)
12990
0
    {
12991
0
      dirmips->call_stub = indmips->call_stub;
12992
0
      indmips->call_stub = NULL;
12993
0
    }
12994
0
  if (indmips->call_fp_stub)
12995
0
    {
12996
0
      dirmips->call_fp_stub = indmips->call_fp_stub;
12997
0
      indmips->call_fp_stub = NULL;
12998
0
    }
12999
0
  if (indmips->global_got_area < dirmips->global_got_area)
13000
0
    dirmips->global_got_area = indmips->global_got_area;
13001
0
  if (indmips->global_got_area < GGA_NONE)
13002
0
    indmips->global_got_area = GGA_NONE;
13003
0
  if (indmips->has_nonpic_branches)
13004
0
    dirmips->has_nonpic_branches = true;
13005
0
}
13006
13007
/* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
13008
   to hide it.  It has to remain global (it will also be protected) so as to
13009
   be assigned a global GOT entry, which will then remain unchanged at load
13010
   time.  */
13011
13012
void
13013
_bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
13014
         struct elf_link_hash_entry *entry,
13015
         bool force_local)
13016
0
{
13017
0
  struct mips_elf_link_hash_table *htab;
13018
13019
0
  htab = mips_elf_hash_table (info);
13020
0
  BFD_ASSERT (htab != NULL);
13021
0
  if (htab->use_absolute_zero
13022
0
      && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
13023
0
    return;
13024
13025
0
  _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
13026
0
}
13027

13028
0
#define PDR_SIZE 32
13029
13030
bool
13031
_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
13032
          struct bfd_link_info *info)
13033
0
{
13034
0
  asection *o;
13035
0
  bool ret = false;
13036
0
  unsigned char *tdata;
13037
0
  size_t i, skip;
13038
13039
0
  o = bfd_get_section_by_name (abfd, ".pdr");
13040
0
  if (! o)
13041
0
    return false;
13042
0
  if (o->size == 0)
13043
0
    return false;
13044
0
  if (o->size % PDR_SIZE != 0)
13045
0
    return false;
13046
0
  if (o->output_section != NULL
13047
0
      && bfd_is_abs_section (o->output_section))
13048
0
    return false;
13049
13050
0
  tdata = bfd_zmalloc (o->size / PDR_SIZE);
13051
0
  if (! tdata)
13052
0
    return false;
13053
13054
0
  cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
13055
0
              info->keep_memory);
13056
0
  if (!cookie->rels)
13057
0
    {
13058
0
      free (tdata);
13059
0
      return false;
13060
0
    }
13061
13062
0
  cookie->rel = cookie->rels;
13063
0
  cookie->relend = cookie->rels + o->reloc_count;
13064
13065
0
  for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
13066
0
    {
13067
0
      if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
13068
0
  {
13069
0
    tdata[i] = 1;
13070
0
    skip ++;
13071
0
  }
13072
0
    }
13073
13074
0
  if (skip != 0)
13075
0
    {
13076
0
      mips_elf_section_data (o)->u.tdata = tdata;
13077
0
      if (o->rawsize == 0)
13078
0
  o->rawsize = o->size;
13079
0
      o->size -= skip * PDR_SIZE;
13080
0
      ret = true;
13081
0
    }
13082
0
  else
13083
0
    free (tdata);
13084
13085
0
  if (! info->keep_memory)
13086
0
    free (cookie->rels);
13087
13088
0
  return ret;
13089
0
}
13090
13091
bool
13092
_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
13093
0
{
13094
0
  if (strcmp (sec->name, ".pdr") == 0)
13095
0
    return true;
13096
0
  return false;
13097
0
}
13098
13099
bool
13100
_bfd_mips_elf_write_section (bfd *output_bfd,
13101
           struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
13102
           asection *sec, bfd_byte *contents)
13103
0
{
13104
0
  bfd_byte *to, *from, *end;
13105
0
  int i;
13106
13107
0
  if (strcmp (sec->name, ".pdr") != 0)
13108
0
    return false;
13109
13110
0
  if (mips_elf_section_data (sec)->u.tdata == NULL)
13111
0
    return false;
13112
13113
0
  to = contents;
13114
0
  end = contents + sec->size;
13115
0
  for (from = contents, i = 0;
13116
0
       from < end;
13117
0
       from += PDR_SIZE, i++)
13118
0
    {
13119
0
      if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
13120
0
  continue;
13121
0
      if (to != from)
13122
0
  memcpy (to, from, PDR_SIZE);
13123
0
      to += PDR_SIZE;
13124
0
    }
13125
0
  bfd_set_section_contents (output_bfd, sec->output_section, contents,
13126
0
          sec->output_offset, sec->size);
13127
0
  return true;
13128
0
}
13129

13130
/* microMIPS code retains local labels for linker relaxation.  Omit them
13131
   from output by default for clarity.  */
13132
13133
bool
13134
_bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
13135
4.63k
{
13136
4.63k
  return _bfd_elf_is_local_label_name (abfd, sym->name);
13137
4.63k
}
13138
13139
bool
13140
_bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
13141
         asection *section, bfd_vma offset,
13142
         const char **filename_ptr,
13143
         const char **functionname_ptr,
13144
         unsigned int *line_ptr,
13145
         unsigned int *discriminator_ptr)
13146
6.64k
{
13147
6.64k
  asection *msec;
13148
13149
6.64k
  if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
13150
6.64k
             filename_ptr, functionname_ptr,
13151
6.64k
             line_ptr, discriminator_ptr,
13152
6.64k
             dwarf_debug_sections,
13153
6.64k
             &elf_tdata (abfd)->dwarf2_find_line_info)
13154
6.64k
      == 1)
13155
10
    return true;
13156
13157
6.63k
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
13158
6.63k
             filename_ptr, functionname_ptr,
13159
6.63k
             line_ptr))
13160
0
    {
13161
0
      if (!*functionname_ptr)
13162
0
  _bfd_elf_find_function (abfd, symbols, section, offset,
13163
0
        *filename_ptr ? NULL : filename_ptr,
13164
0
        functionname_ptr);
13165
0
      return true;
13166
0
    }
13167
13168
6.63k
  msec = bfd_get_section_by_name (abfd, ".mdebug");
13169
6.63k
  if (msec != NULL)
13170
5.04k
    {
13171
5.04k
      flagword origflags;
13172
5.04k
      struct mips_elf_find_line *fi;
13173
5.04k
      const struct ecoff_debug_swap * const swap =
13174
5.04k
  get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
13175
13176
      /* If we are called during a link, mips_elf_final_link may have
13177
   cleared the SEC_HAS_CONTENTS field.  We force it back on here
13178
   if appropriate (which it normally will be).  */
13179
5.04k
      origflags = msec->flags;
13180
5.04k
      if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
13181
5.04k
  msec->flags |= SEC_HAS_CONTENTS;
13182
13183
5.04k
      fi = mips_elf_tdata (abfd)->find_line_info;
13184
5.04k
      if (fi == NULL)
13185
3.74k
  {
13186
3.74k
    bfd_size_type external_fdr_size;
13187
3.74k
    char *fraw_src;
13188
3.74k
    char *fraw_end;
13189
3.74k
    struct fdr *fdr_ptr;
13190
3.74k
    bfd_size_type amt = sizeof (struct mips_elf_find_line);
13191
13192
3.74k
    fi = bfd_zalloc (abfd, amt);
13193
3.74k
    if (fi == NULL)
13194
0
      {
13195
0
        msec->flags = origflags;
13196
0
        return false;
13197
0
      }
13198
13199
3.74k
    if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
13200
3.46k
      {
13201
3.46k
        msec->flags = origflags;
13202
3.46k
        return false;
13203
3.46k
      }
13204
13205
    /* Swap in the FDR information.  */
13206
272
    amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
13207
272
    fi->d.fdr = bfd_alloc (abfd, amt);
13208
272
    if (fi->d.fdr == NULL)
13209
0
      {
13210
0
        _bfd_ecoff_free_ecoff_debug_info (&fi->d);
13211
0
        msec->flags = origflags;
13212
0
        return false;
13213
0
      }
13214
272
    external_fdr_size = swap->external_fdr_size;
13215
272
    fdr_ptr = fi->d.fdr;
13216
272
    fraw_src = (char *) fi->d.external_fdr;
13217
272
    fraw_end = (fraw_src
13218
272
          + fi->d.symbolic_header.ifdMax * external_fdr_size);
13219
80.1k
    for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
13220
79.8k
      (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
13221
13222
272
    mips_elf_tdata (abfd)->find_line_info = fi;
13223
272
  }
13224
13225
1.57k
      if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
13226
1.57k
          &fi->i, filename_ptr, functionname_ptr,
13227
1.57k
          line_ptr))
13228
936
  {
13229
936
    msec->flags = origflags;
13230
936
    return true;
13231
936
  }
13232
13233
641
      msec->flags = origflags;
13234
641
    }
13235
13236
  /* Fall back on the generic ELF find_nearest_line routine.  */
13237
13238
2.23k
  return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
13239
2.23k
             filename_ptr, functionname_ptr,
13240
2.23k
             line_ptr, discriminator_ptr);
13241
6.63k
}
13242
13243
bool
13244
_bfd_mips_elf_find_inliner_info (bfd *abfd,
13245
         const char **filename_ptr,
13246
         const char **functionname_ptr,
13247
         unsigned int *line_ptr)
13248
0
{
13249
0
  bool found;
13250
0
  found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13251
0
           functionname_ptr, line_ptr,
13252
0
           & elf_tdata (abfd)->dwarf2_find_line_info);
13253
0
  return found;
13254
0
}
13255
13256

13257
/* When are writing out the .options or .MIPS.options section,
13258
   remember the bytes we are writing out, so that we can install the
13259
   GP value in the section_processing routine.  */
13260
13261
bool
13262
_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
13263
            const void *location,
13264
            file_ptr offset, bfd_size_type count)
13265
365
{
13266
365
  if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
13267
0
    {
13268
0
      bfd_byte *c;
13269
13270
0
      if (elf_section_data (section) == NULL)
13271
0
  {
13272
0
    size_t amt = sizeof (struct bfd_elf_section_data);
13273
0
    section->used_by_bfd = bfd_zalloc (abfd, amt);
13274
0
    if (elf_section_data (section) == NULL)
13275
0
      return false;
13276
0
  }
13277
0
      c = mips_elf_section_data (section)->u.tdata;
13278
0
      if (c == NULL)
13279
0
  {
13280
0
    c = bfd_zalloc (abfd, section->size);
13281
0
    if (c == NULL)
13282
0
      return false;
13283
0
    mips_elf_section_data (section)->u.tdata = c;
13284
0
  }
13285
13286
0
      memcpy (c + offset, location, count);
13287
0
    }
13288
13289
365
  return _bfd_elf_set_section_contents (abfd, section, location, offset,
13290
365
          count);
13291
365
}
13292
13293
/* This is almost identical to bfd_generic_get_... except that some
13294
   MIPS relocations need to be handled specially.  Sigh.  */
13295
13296
bfd_byte *
13297
_bfd_elf_mips_get_relocated_section_contents
13298
  (bfd *abfd,
13299
   struct bfd_link_info *link_info,
13300
   struct bfd_link_order *link_order,
13301
   bfd_byte *data,
13302
   bool relocatable,
13303
   asymbol **symbols)
13304
330
{
13305
330
  bfd *input_bfd = link_order->u.indirect.section->owner;
13306
330
  asection *input_section = link_order->u.indirect.section;
13307
330
  long reloc_size;
13308
330
  arelent **reloc_vector;
13309
330
  long reloc_count;
13310
13311
330
  reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13312
330
  if (reloc_size < 0)
13313
13
    return NULL;
13314
13315
  /* Read in the section.  */
13316
317
  bfd_byte *orig_data = data;
13317
317
  if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
13318
14
    return NULL;
13319
13320
303
  if (data == NULL)
13321
44
    return NULL;
13322
13323
259
  if (reloc_size == 0)
13324
0
    return data;
13325
13326
259
  reloc_vector = (arelent **) bfd_malloc (reloc_size);
13327
259
  if (reloc_vector == NULL)
13328
0
    {
13329
0
      struct mips_elf_obj_tdata *tdata;
13330
0
      struct mips_hi16 **hip, *hi;
13331
201
    error_return:
13332
      /* If we are going to return an error, remove entries on
13333
   mips_hi16_list that point into this section's data.  Data
13334
   will typically be freed on return from this function.  */
13335
201
      tdata = mips_elf_tdata (abfd);
13336
201
      hip = &tdata->mips_hi16_list;
13337
258
      while ((hi = *hip) != NULL)
13338
57
  {
13339
57
    if (hi->input_section == input_section)
13340
40
      {
13341
40
        *hip = hi->next;
13342
40
        free (hi);
13343
40
      }
13344
17
    else
13345
17
      hip = &hi->next;
13346
57
  }
13347
201
      if (orig_data == NULL)
13348
41
  free (data);
13349
201
      data = NULL;
13350
201
      goto out;
13351
0
    }
13352
13353
259
  reloc_count = bfd_canonicalize_reloc (input_bfd,
13354
259
          input_section,
13355
259
          reloc_vector,
13356
259
          symbols);
13357
259
  if (reloc_count < 0)
13358
82
    goto error_return;
13359
13360
177
  if (reloc_count > 0)
13361
171
    {
13362
171
      arelent **parent;
13363
      /* for mips */
13364
171
      int gp_found;
13365
171
      bfd_vma gp = 0x12345678;  /* initialize just to shut gcc up */
13366
13367
171
      {
13368
171
  struct bfd_hash_entry *h;
13369
171
  struct bfd_link_hash_entry *lh;
13370
  /* Skip all this stuff if we aren't mixing formats.  */
13371
171
  if (abfd && input_bfd
13372
171
      && abfd->xvec == input_bfd->xvec)
13373
171
    lh = 0;
13374
0
  else
13375
0
    {
13376
0
      h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
13377
0
      lh = (struct bfd_link_hash_entry *) h;
13378
0
    }
13379
171
      lookup:
13380
171
  if (lh)
13381
0
    {
13382
0
      switch (lh->type)
13383
0
        {
13384
0
        case bfd_link_hash_undefined:
13385
0
        case bfd_link_hash_undefweak:
13386
0
        case bfd_link_hash_common:
13387
0
    gp_found = 0;
13388
0
    break;
13389
0
        case bfd_link_hash_defined:
13390
0
        case bfd_link_hash_defweak:
13391
0
    gp_found = 1;
13392
0
    gp = lh->u.def.value;
13393
0
    break;
13394
0
        case bfd_link_hash_indirect:
13395
0
        case bfd_link_hash_warning:
13396
0
    lh = lh->u.i.link;
13397
    /* @@FIXME  ignoring warning for now */
13398
0
    goto lookup;
13399
0
        case bfd_link_hash_new:
13400
0
        default:
13401
0
    abort ();
13402
0
        }
13403
0
    }
13404
171
  else
13405
171
    gp_found = 0;
13406
171
      }
13407
      /* end mips */
13408
13409
3.90k
      for (parent = reloc_vector; *parent != NULL; parent++)
13410
3.85k
  {
13411
3.85k
    char *error_message = NULL;
13412
3.85k
    asymbol *symbol;
13413
3.85k
    bfd_reloc_status_type r;
13414
13415
3.85k
    symbol = *(*parent)->sym_ptr_ptr;
13416
    /* PR ld/19628: A specially crafted input file
13417
       can result in a NULL symbol pointer here.  */
13418
3.85k
    if (symbol == NULL)
13419
0
      {
13420
0
        link_info->callbacks->einfo
13421
    /* xgettext:c-format */
13422
0
    (_("%X%P: %pB(%pA): error: relocation for offset %V has no value\n"),
13423
0
     abfd, input_section, (* parent)->address);
13424
0
        goto error_return;
13425
0
      }
13426
13427
    /* Zap reloc field when the symbol is from a discarded
13428
       section, ignoring any addend.  Do the same when called
13429
       from bfd_simple_get_relocated_section_contents for
13430
       undefined symbols in debug sections.  This is to keep
13431
       debug info reasonably sane, in particular so that
13432
       DW_FORM_ref_addr to another file's .debug_info isn't
13433
       confused with an offset into the current file's
13434
       .debug_info.  */
13435
3.85k
    if ((symbol->section != NULL && discarded_section (symbol->section))
13436
3.85k
        || (symbol->section == bfd_und_section_ptr
13437
3.85k
      && (input_section->flags & SEC_DEBUGGING) != 0
13438
3.85k
      && link_info->input_bfds == link_info->output_bfd))
13439
57
      {
13440
57
        bfd_vma off;
13441
57
        static reloc_howto_type none_howto
13442
57
    = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
13443
57
       "unused", false, 0, 0, false);
13444
13445
57
        off = ((*parent)->address
13446
57
         * bfd_octets_per_byte (input_bfd, input_section));
13447
57
        _bfd_clear_contents ((*parent)->howto, input_bfd,
13448
57
           input_section, data, off);
13449
57
        (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13450
57
        (*parent)->addend = 0;
13451
57
        (*parent)->howto = &none_howto;
13452
57
        r = bfd_reloc_ok;
13453
57
      }
13454
13455
    /* Specific to MIPS: Deal with relocation types that require
13456
       knowing the gp of the output bfd.  */
13457
13458
    /* If we've managed to find the gp and have a special
13459
       function for the relocation then go ahead, else default
13460
       to the generic handling.  */
13461
3.79k
    else if (gp_found
13462
3.79k
       && ((*parent)->howto->special_function
13463
0
           == _bfd_mips_elf32_gprel16_reloc))
13464
0
      r = _bfd_mips_elf_gprel16_with_gp (input_bfd, symbol, *parent,
13465
0
                 input_section, relocatable,
13466
0
                 data, gp);
13467
3.79k
    else
13468
3.79k
      r = bfd_perform_relocation (input_bfd,
13469
3.79k
          *parent,
13470
3.79k
          data,
13471
3.79k
          input_section,
13472
3.79k
          relocatable ? abfd : NULL,
13473
3.79k
          &error_message);
13474
13475
3.85k
    if (relocatable)
13476
0
      {
13477
0
        asection *os = input_section->output_section;
13478
13479
        /* A partial link, so keep the relocs.  */
13480
0
        os->orelocation[os->reloc_count] = *parent;
13481
0
        os->reloc_count++;
13482
0
      }
13483
13484
3.85k
    if (r != bfd_reloc_ok)
13485
209
      {
13486
209
        switch (r)
13487
209
    {
13488
2
    case bfd_reloc_undefined:
13489
2
      (*link_info->callbacks->undefined_symbol)
13490
2
        (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13491
2
         input_bfd, input_section, (*parent)->address, true);
13492
2
      break;
13493
59
    case bfd_reloc_dangerous:
13494
59
      BFD_ASSERT (error_message != NULL);
13495
59
      (*link_info->callbacks->reloc_dangerous)
13496
59
        (link_info, error_message,
13497
59
         input_bfd, input_section, (*parent)->address);
13498
59
      break;
13499
29
    case bfd_reloc_overflow:
13500
29
      (*link_info->callbacks->reloc_overflow)
13501
29
        (link_info, NULL,
13502
29
         bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13503
29
         (*parent)->howto->name, (*parent)->addend,
13504
29
         input_bfd, input_section, (*parent)->address);
13505
29
      break;
13506
119
    case bfd_reloc_outofrange:
13507
      /* PR ld/13730:
13508
         This error can result when processing some partially
13509
         complete binaries.  Do not abort, but issue an error
13510
         message instead.  */
13511
119
      link_info->callbacks->einfo
13512
        /* xgettext:c-format */
13513
119
        (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13514
119
         abfd, input_section, * parent);
13515
119
      goto error_return;
13516
13517
0
    case bfd_reloc_notsupported:
13518
      /* PR ld/17512
13519
         This error can result when processing a corrupt binary.
13520
         Do not abort.  Issue an error message instead.  */
13521
0
      link_info->callbacks->einfo
13522
        /* xgettext:c-format */
13523
0
        (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
13524
0
         abfd, input_section, * parent);
13525
0
      goto error_return;
13526
13527
0
    default:
13528
      /* PR 17512; file: 90c2a92e.
13529
         Report unexpected results, without aborting.  */
13530
0
      link_info->callbacks->einfo
13531
        /* xgettext:c-format */
13532
0
        (_("%X%P: %pB(%pA): relocation \"%pR\" returns an unrecognized value %x\n"),
13533
0
         abfd, input_section, * parent, r);
13534
0
      break;
13535
209
    }
13536
13537
209
      }
13538
3.85k
  }
13539
171
    }
13540
13541
259
 out:
13542
259
  free (reloc_vector);
13543
259
  return data;
13544
177
}
13545

13546
static bool
13547
mips_elf_relax_delete_bytes (bfd *abfd,
13548
           asection *sec, bfd_vma addr, int count)
13549
0
{
13550
0
  Elf_Internal_Shdr *symtab_hdr;
13551
0
  unsigned int sec_shndx;
13552
0
  bfd_byte *contents;
13553
0
  Elf_Internal_Rela *irel, *irelend;
13554
0
  Elf_Internal_Sym *isym;
13555
0
  Elf_Internal_Sym *isymend;
13556
0
  struct elf_link_hash_entry **sym_hashes;
13557
0
  struct elf_link_hash_entry **end_hashes;
13558
0
  struct elf_link_hash_entry **start_hashes;
13559
0
  unsigned int symcount;
13560
13561
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
13562
0
  contents = elf_section_data (sec)->this_hdr.contents;
13563
13564
0
  irel = elf_section_data (sec)->relocs;
13565
0
  irelend = irel + sec->reloc_count;
13566
13567
  /* Actually delete the bytes.  */
13568
0
  memmove (contents + addr, contents + addr + count,
13569
0
     (size_t) (sec->size - addr - count));
13570
0
  sec->size -= count;
13571
13572
  /* Adjust all the relocs.  */
13573
0
  for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
13574
0
    {
13575
      /* Get the new reloc address.  */
13576
0
      if (irel->r_offset > addr)
13577
0
  irel->r_offset -= count;
13578
0
    }
13579
13580
0
  BFD_ASSERT (addr % 2 == 0);
13581
0
  BFD_ASSERT (count % 2 == 0);
13582
13583
  /* Adjust the local symbols defined in this section.  */
13584
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13585
0
  isym = (Elf_Internal_Sym *) symtab_hdr->contents;
13586
0
  for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
13587
0
    if (isym->st_shndx == sec_shndx && isym->st_value > addr)
13588
0
      isym->st_value -= count;
13589
13590
  /* Now adjust the global symbols defined in this section.  */
13591
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
13592
0
        - symtab_hdr->sh_info);
13593
0
  sym_hashes = start_hashes = elf_sym_hashes (abfd);
13594
0
  end_hashes = sym_hashes + symcount;
13595
13596
0
  for (; sym_hashes < end_hashes; sym_hashes++)
13597
0
    {
13598
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
13599
13600
0
      if ((sym_hash->root.type == bfd_link_hash_defined
13601
0
     || sym_hash->root.type == bfd_link_hash_defweak)
13602
0
    && sym_hash->root.u.def.section == sec)
13603
0
  {
13604
0
    bfd_vma value = sym_hash->root.u.def.value;
13605
13606
0
    if (ELF_ST_IS_MICROMIPS (sym_hash->other))
13607
0
      value &= MINUS_TWO;
13608
0
    if (value > addr)
13609
0
      sym_hash->root.u.def.value -= count;
13610
0
  }
13611
0
    }
13612
13613
0
  return true;
13614
0
}
13615
13616
13617
/* Opcodes needed for microMIPS relaxation as found in
13618
   opcodes/micromips-opc.c.  */
13619
13620
struct opcode_descriptor {
13621
  unsigned long match;
13622
  unsigned long mask;
13623
};
13624
13625
/* The $ra register aka $31.  */
13626
13627
0
#define RA 31
13628
13629
/* 32-bit instruction format register fields.  */
13630
13631
0
#define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
13632
0
#define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
13633
13634
/* Check if a 5-bit register index can be abbreviated to 3 bits.  */
13635
13636
#define OP16_VALID_REG(r) \
13637
0
  ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
13638
13639
13640
/* 32-bit and 16-bit branches.  */
13641
13642
static const struct opcode_descriptor b_insns_32[] = {
13643
  { /* "b", "p",    */ 0x40400000, 0xffff0000 }, /* bgez 0 */
13644
  { /* "b", "p",    */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
13645
  { 0, 0 }  /* End marker for find_match().  */
13646
};
13647
13648
static const struct opcode_descriptor bc_insn_32 =
13649
  { /* "bc(1|2)(ft)", "N,p",  */ 0x42800000, 0xfec30000 };
13650
13651
static const struct opcode_descriptor bz_insn_32 =
13652
  { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
13653
13654
static const struct opcode_descriptor bzal_insn_32 =
13655
  { /* "b(ge|lt)zal", "s,p",  */ 0x40200000, 0xffa00000 };
13656
13657
static const struct opcode_descriptor beq_insn_32 =
13658
  { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
13659
13660
static const struct opcode_descriptor b_insn_16 =
13661
  { /* "b", "mD",   */ 0xcc00,     0xfc00 };
13662
13663
static const struct opcode_descriptor bz_insn_16 =
13664
  { /* "b(eq|ne)z", "md,mE",  */ 0x8c00,     0xdc00 };
13665
13666
13667
/* 32-bit and 16-bit branch EQ and NE zero.  */
13668
13669
/* NOTE: All opcode tables have BEQ/BNE in the same order: first the
13670
   eq and second the ne.  This convention is used when replacing a
13671
   32-bit BEQ/BNE with the 16-bit version.  */
13672
13673
0
#define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
13674
13675
static const struct opcode_descriptor bz_rs_insns_32[] = {
13676
  { /* "beqz",  "s,p",    */ 0x94000000, 0xffe00000 },
13677
  { /* "bnez",  "s,p",    */ 0xb4000000, 0xffe00000 },
13678
  { 0, 0 }  /* End marker for find_match().  */
13679
};
13680
13681
static const struct opcode_descriptor bz_rt_insns_32[] = {
13682
  { /* "beqz",  "t,p",    */ 0x94000000, 0xfc01f000 },
13683
  { /* "bnez",  "t,p",    */ 0xb4000000, 0xfc01f000 },
13684
  { 0, 0 }  /* End marker for find_match().  */
13685
};
13686
13687
static const struct opcode_descriptor bzc_insns_32[] = {
13688
  { /* "beqzc", "s,p",    */ 0x40e00000, 0xffe00000 },
13689
  { /* "bnezc", "s,p",    */ 0x40a00000, 0xffe00000 },
13690
  { 0, 0 }  /* End marker for find_match().  */
13691
};
13692
13693
static const struct opcode_descriptor bz_insns_16[] = {
13694
  { /* "beqz",  "md,mE",  */ 0x8c00,     0xfc00 },
13695
  { /* "bnez",  "md,mE",  */ 0xac00,     0xfc00 },
13696
  { 0, 0 }  /* End marker for find_match().  */
13697
};
13698
13699
/* Switch between a 5-bit register index and its 3-bit shorthand.  */
13700
13701
0
#define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
13702
#define BZ16_REG_FIELD(r) (((r) & 7) << 7)
13703
13704
13705
/* 32-bit instructions with a delay slot.  */
13706
13707
static const struct opcode_descriptor jal_insn_32_bd16 =
13708
  { /* "jals",  "a",    */ 0x74000000, 0xfc000000 };
13709
13710
static const struct opcode_descriptor jal_insn_32_bd32 =
13711
  { /* "jal", "a",    */ 0xf4000000, 0xfc000000 };
13712
13713
static const struct opcode_descriptor jal_x_insn_32_bd32 =
13714
  { /* "jal[x]", "a",   */ 0xf0000000, 0xf8000000 };
13715
13716
static const struct opcode_descriptor j_insn_32 =
13717
  { /* "j", "a",    */ 0xd4000000, 0xfc000000 };
13718
13719
static const struct opcode_descriptor jalr_insn_32 =
13720
  { /* "jalr[.hb]", "t,s",  */ 0x00000f3c, 0xfc00efff };
13721
13722
/* This table can be compacted, because no opcode replacement is made.  */
13723
13724
static const struct opcode_descriptor ds_insns_32_bd16[] = {
13725
  { /* "jals",  "a",    */ 0x74000000, 0xfc000000 },
13726
13727
  { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
13728
  { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
13729
13730
  { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
13731
  { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
13732
  { /* "j", "a",    */ 0xd4000000, 0xfc000000 },
13733
  { 0, 0 }  /* End marker for find_match().  */
13734
};
13735
13736
/* This table can be compacted, because no opcode replacement is made.  */
13737
13738
static const struct opcode_descriptor ds_insns_32_bd32[] = {
13739
  { /* "jal[x]", "a",   */ 0xf0000000, 0xf8000000 },
13740
13741
  { /* "jalr[.hb]", "t,s",  */ 0x00000f3c, 0xfc00efff },
13742
  { /* "b(ge|lt)zal", "s,p",  */ 0x40200000, 0xffa00000 },
13743
  { 0, 0 }  /* End marker for find_match().  */
13744
};
13745
13746
13747
/* 16-bit instructions with a delay slot.  */
13748
13749
static const struct opcode_descriptor jalr_insn_16_bd16 =
13750
  { /* "jalrs", "my,mj",  */ 0x45e0,     0xffe0 };
13751
13752
static const struct opcode_descriptor jalr_insn_16_bd32 =
13753
  { /* "jalr",  "my,mj",  */ 0x45c0,     0xffe0 };
13754
13755
static const struct opcode_descriptor jr_insn_16 =
13756
  { /* "jr",  "mj",   */ 0x4580,     0xffe0 };
13757
13758
0
#define JR16_REG(opcode) ((opcode) & 0x1f)
13759
13760
/* This table can be compacted, because no opcode replacement is made.  */
13761
13762
static const struct opcode_descriptor ds_insns_16_bd16[] = {
13763
  { /* "jalrs", "my,mj",  */ 0x45e0,     0xffe0 },
13764
13765
  { /* "b", "mD",   */ 0xcc00,     0xfc00 },
13766
  { /* "b(eq|ne)z", "md,mE",  */ 0x8c00,     0xdc00 },
13767
  { /* "jr",  "mj",   */ 0x4580,     0xffe0 },
13768
  { 0, 0 }  /* End marker for find_match().  */
13769
};
13770
13771
13772
/* LUI instruction.  */
13773
13774
static const struct opcode_descriptor lui_insn =
13775
 { /* "lui",  "s,u",    */ 0x41a00000, 0xffe00000 };
13776
13777
13778
/* ADDIU instruction.  */
13779
13780
static const struct opcode_descriptor addiu_insn =
13781
  { /* "addiu", "t,r,j",  */ 0x30000000, 0xfc000000 };
13782
13783
static const struct opcode_descriptor addiupc_insn =
13784
  { /* "addiu", "mb,$pc,mQ",  */ 0x78000000, 0xfc000000 };
13785
13786
#define ADDIUPC_REG_FIELD(r) \
13787
0
  (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13788
13789
13790
/* Relaxable instructions in a JAL delay slot: MOVE.  */
13791
13792
/* The 16-bit move has rd in 9:5 and rs in 4:0.  The 32-bit moves
13793
   (ADDU, OR) have rd in 15:11 and rs in 10:16.  */
13794
#define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13795
#define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13796
13797
#define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13798
#define MOVE16_RS_FIELD(r) (((r) & 0x1f)     )
13799
13800
static const struct opcode_descriptor move_insns_32[] = {
13801
  { /* "move",  "d,s",    */ 0x00000290, 0xffe007ff }, /* or   d,s,$0 */
13802
  { /* "move",  "d,s",    */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
13803
  { 0, 0 }  /* End marker for find_match().  */
13804
};
13805
13806
static const struct opcode_descriptor move_insn_16 =
13807
  { /* "move",  "mp,mj",  */ 0x0c00,     0xfc00 };
13808
13809
13810
/* NOP instructions.  */
13811
13812
static const struct opcode_descriptor nop_insn_32 =
13813
  { /* "nop", "",   */ 0x00000000, 0xffffffff };
13814
13815
static const struct opcode_descriptor nop_insn_16 =
13816
  { /* "nop", "",   */ 0x0c00,     0xffff };
13817
13818
13819
/* Instruction match support.  */
13820
13821
0
#define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13822
13823
static int
13824
find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13825
0
{
13826
0
  unsigned long indx;
13827
13828
0
  for (indx = 0; insn[indx].mask != 0; indx++)
13829
0
    if (MATCH (opcode, insn[indx]))
13830
0
      return indx;
13831
13832
0
  return -1;
13833
0
}
13834
13835
13836
/* Branch and delay slot decoding support.  */
13837
13838
/* If PTR points to what *might* be a 16-bit branch or jump, then
13839
   return the minimum length of its delay slot, otherwise return 0.
13840
   Non-zero results are not definitive as we might be checking against
13841
   the second half of another instruction.  */
13842
13843
static int
13844
check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13845
0
{
13846
0
  unsigned long opcode;
13847
0
  int bdsize;
13848
13849
0
  opcode = bfd_get_16 (abfd, ptr);
13850
0
  if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13851
    /* 16-bit branch/jump with a 32-bit delay slot.  */
13852
0
    bdsize = 4;
13853
0
  else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13854
0
     || find_match (opcode, ds_insns_16_bd16) >= 0)
13855
    /* 16-bit branch/jump with a 16-bit delay slot.  */
13856
0
    bdsize = 2;
13857
0
  else
13858
    /* No delay slot.  */
13859
0
    bdsize = 0;
13860
13861
0
  return bdsize;
13862
0
}
13863
13864
/* If PTR points to what *might* be a 32-bit branch or jump, then
13865
   return the minimum length of its delay slot, otherwise return 0.
13866
   Non-zero results are not definitive as we might be checking against
13867
   the second half of another instruction.  */
13868
13869
static int
13870
check_br32_dslot (bfd *abfd, bfd_byte *ptr)
13871
0
{
13872
0
  unsigned long opcode;
13873
0
  int bdsize;
13874
13875
0
  opcode = bfd_get_micromips_32 (abfd, ptr);
13876
0
  if (find_match (opcode, ds_insns_32_bd32) >= 0)
13877
    /* 32-bit branch/jump with a 32-bit delay slot.  */
13878
0
    bdsize = 4;
13879
0
  else if (find_match (opcode, ds_insns_32_bd16) >= 0)
13880
    /* 32-bit branch/jump with a 16-bit delay slot.  */
13881
0
    bdsize = 2;
13882
0
  else
13883
    /* No delay slot.  */
13884
0
    bdsize = 0;
13885
13886
0
  return bdsize;
13887
0
}
13888
13889
/* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
13890
   that doesn't fiddle with REG, then return TRUE, otherwise FALSE.  */
13891
13892
static bool
13893
check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13894
0
{
13895
0
  unsigned long opcode;
13896
13897
0
  opcode = bfd_get_16 (abfd, ptr);
13898
0
  if (MATCH (opcode, b_insn_16)
13899
            /* B16  */
13900
0
      || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
13901
            /* JR16  */
13902
0
      || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
13903
            /* BEQZ16, BNEZ16  */
13904
0
      || (MATCH (opcode, jalr_insn_16_bd32)
13905
            /* JALR16  */
13906
0
    && reg != JR16_REG (opcode) && reg != RA))
13907
0
    return true;
13908
13909
0
  return false;
13910
0
}
13911
13912
/* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
13913
   then return TRUE, otherwise FALSE.  */
13914
13915
static bool
13916
check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13917
0
{
13918
0
  unsigned long opcode;
13919
13920
0
  opcode = bfd_get_micromips_32 (abfd, ptr);
13921
0
  if (MATCH (opcode, j_insn_32)
13922
            /* J  */
13923
0
      || MATCH (opcode, bc_insn_32)
13924
            /* BC1F, BC1T, BC2F, BC2T  */
13925
0
      || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13926
            /* JAL, JALX  */
13927
0
      || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13928
            /* BGEZ, BGTZ, BLEZ, BLTZ  */
13929
0
      || (MATCH (opcode, bzal_insn_32)
13930
            /* BGEZAL, BLTZAL  */
13931
0
    && reg != OP32_SREG (opcode) && reg != RA)
13932
0
      || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13933
            /* JALR, JALR.HB, BEQ, BNE  */
13934
0
    && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13935
0
    return true;
13936
13937
0
  return false;
13938
0
}
13939
13940
/* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13941
   IRELEND) at OFFSET indicate that there must be a compact branch there,
13942
   then return TRUE, otherwise FALSE.  */
13943
13944
static bool
13945
check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13946
         const Elf_Internal_Rela *internal_relocs,
13947
         const Elf_Internal_Rela *irelend)
13948
0
{
13949
0
  const Elf_Internal_Rela *irel;
13950
0
  unsigned long opcode;
13951
13952
0
  opcode = bfd_get_micromips_32 (abfd, ptr);
13953
0
  if (find_match (opcode, bzc_insns_32) < 0)
13954
0
    return false;
13955
13956
0
  for (irel = internal_relocs; irel < irelend; irel++)
13957
0
    if (irel->r_offset == offset
13958
0
  && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13959
0
      return true;
13960
13961
0
  return false;
13962
0
}
13963
13964
/* Bitsize checking.  */
13965
#define IS_BITSIZE(val, N)            \
13966
0
  (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1)))   \
13967
0
    - (1ULL << ((N) - 1))) == (val))
13968
13969

13970
bool
13971
_bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13972
           struct bfd_link_info *link_info,
13973
           bool *again)
13974
0
{
13975
0
  bool insn32 = mips_elf_hash_table (link_info)->insn32;
13976
0
  Elf_Internal_Shdr *symtab_hdr;
13977
0
  Elf_Internal_Rela *internal_relocs;
13978
0
  Elf_Internal_Rela *irel, *irelend;
13979
0
  bfd_byte *contents = NULL;
13980
0
  Elf_Internal_Sym *isymbuf = NULL;
13981
13982
  /* Assume nothing changes.  */
13983
0
  *again = false;
13984
13985
  /* We don't have to do anything for a relocatable link, if
13986
     this section does not have relocs, or if this is not a
13987
     code section.  */
13988
13989
0
  if (bfd_link_relocatable (link_info)
13990
0
      || sec->reloc_count == 0
13991
0
      || (sec->flags & SEC_RELOC) == 0
13992
0
      || (sec->flags & SEC_HAS_CONTENTS) == 0
13993
0
      || (sec->flags & SEC_CODE) == 0)
13994
0
    return true;
13995
13996
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13997
13998
  /* Get a copy of the native relocations.  */
13999
0
  internal_relocs = (_bfd_elf_link_read_relocs
14000
0
         (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
14001
0
          link_info->keep_memory));
14002
0
  if (internal_relocs == NULL)
14003
0
    goto error_return;
14004
14005
  /* Walk through them looking for relaxing opportunities.  */
14006
0
  irelend = internal_relocs + sec->reloc_count;
14007
0
  for (irel = internal_relocs; irel < irelend; irel++)
14008
0
    {
14009
0
      unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
14010
0
      unsigned int r_type = ELF32_R_TYPE (irel->r_info);
14011
0
      bool target_is_micromips_code_p;
14012
0
      unsigned long opcode;
14013
0
      bfd_vma symval;
14014
0
      bfd_vma pcrval;
14015
0
      bfd_byte *ptr;
14016
0
      int fndopc;
14017
14018
      /* The number of bytes to delete for relaxation and from where
14019
   to delete these bytes starting at irel->r_offset.  */
14020
0
      int delcnt = 0;
14021
0
      int deloff = 0;
14022
14023
      /* If this isn't something that can be relaxed, then ignore
14024
   this reloc.  */
14025
0
      if (r_type != R_MICROMIPS_HI16
14026
0
    && r_type != R_MICROMIPS_PC16_S1
14027
0
    && r_type != R_MICROMIPS_26_S1)
14028
0
  continue;
14029
14030
      /* Get the section contents if we haven't done so already.  */
14031
0
      if (contents == NULL)
14032
0
  {
14033
    /* Get cached copy if it exists.  */
14034
0
    if (elf_section_data (sec)->this_hdr.contents != NULL)
14035
0
      contents = elf_section_data (sec)->this_hdr.contents;
14036
    /* Go get them off disk.  */
14037
0
    else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
14038
0
      goto error_return;
14039
0
  }
14040
0
      ptr = contents + irel->r_offset;
14041
14042
      /* Read this BFD's local symbols if we haven't done so already.  */
14043
0
      if (isymbuf == NULL && symtab_hdr->sh_info != 0)
14044
0
  {
14045
0
    isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
14046
0
    if (isymbuf == NULL)
14047
0
      isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
14048
0
              symtab_hdr->sh_info, 0,
14049
0
              NULL, NULL, NULL);
14050
0
    if (isymbuf == NULL)
14051
0
      goto error_return;
14052
0
  }
14053
14054
      /* Get the value of the symbol referred to by the reloc.  */
14055
0
      if (r_symndx < symtab_hdr->sh_info)
14056
0
  {
14057
    /* A local symbol.  */
14058
0
    Elf_Internal_Sym *isym;
14059
0
    asection *sym_sec;
14060
14061
0
    isym = isymbuf + r_symndx;
14062
0
    if (isym->st_shndx == SHN_UNDEF)
14063
0
      sym_sec = bfd_und_section_ptr;
14064
0
    else if (isym->st_shndx == SHN_ABS)
14065
0
      sym_sec = bfd_abs_section_ptr;
14066
0
    else if (isym->st_shndx == SHN_COMMON)
14067
0
      sym_sec = bfd_com_section_ptr;
14068
0
    else
14069
0
      sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
14070
0
    symval = (isym->st_value
14071
0
        + sym_sec->output_section->vma
14072
0
        + sym_sec->output_offset);
14073
0
    target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
14074
0
  }
14075
0
      else
14076
0
  {
14077
0
    unsigned long indx;
14078
0
    struct elf_link_hash_entry *h;
14079
14080
    /* An external symbol.  */
14081
0
    indx = r_symndx - symtab_hdr->sh_info;
14082
0
    h = elf_sym_hashes (abfd)[indx];
14083
0
    BFD_ASSERT (h != NULL);
14084
14085
0
    if (h->root.type != bfd_link_hash_defined
14086
0
        && h->root.type != bfd_link_hash_defweak)
14087
      /* This appears to be a reference to an undefined
14088
         symbol.  Just ignore it -- it will be caught by the
14089
         regular reloc processing.  */
14090
0
      continue;
14091
14092
0
    symval = (h->root.u.def.value
14093
0
        + h->root.u.def.section->output_section->vma
14094
0
        + h->root.u.def.section->output_offset);
14095
0
    target_is_micromips_code_p = (!h->needs_plt
14096
0
          && ELF_ST_IS_MICROMIPS (h->other));
14097
0
  }
14098
14099
14100
      /* For simplicity of coding, we are going to modify the
14101
   section contents, the section relocs, and the BFD symbol
14102
   table.  We must tell the rest of the code not to free up this
14103
   information.  It would be possible to instead create a table
14104
   of changes which have to be made, as is done in coff-mips.c;
14105
   that would be more work, but would require less memory when
14106
   the linker is run.  */
14107
14108
      /* Only 32-bit instructions relaxed.  */
14109
0
      if (irel->r_offset + 4 > sec->size)
14110
0
  continue;
14111
14112
0
      opcode = bfd_get_micromips_32 (abfd, ptr);
14113
14114
      /* This is the pc-relative distance from the instruction the
14115
   relocation is applied to, to the symbol referred.  */
14116
0
      pcrval = (symval
14117
0
    - (sec->output_section->vma + sec->output_offset)
14118
0
    - irel->r_offset);
14119
14120
      /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
14121
   of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
14122
   R_MICROMIPS_PC23_S2.  The R_MICROMIPS_PC23_S2 condition is
14123
14124
     (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
14125
14126
   where pcrval has first to be adjusted to apply against the LO16
14127
   location (we make the adjustment later on, when we have figured
14128
   out the offset).  */
14129
0
      if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
14130
0
  {
14131
0
    bool bzc = false;
14132
0
    unsigned long nextopc;
14133
0
    unsigned long reg;
14134
0
    bfd_vma offset;
14135
14136
    /* Give up if the previous reloc was a HI16 against this symbol
14137
       too.  */
14138
0
    if (irel > internal_relocs
14139
0
        && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
14140
0
        && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
14141
0
      continue;
14142
14143
    /* Or if the next reloc is not a LO16 against this symbol.  */
14144
0
    if (irel + 1 >= irelend
14145
0
        || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
14146
0
        || ELF32_R_SYM (irel[1].r_info) != r_symndx)
14147
0
      continue;
14148
14149
    /* Or if the second next reloc is a LO16 against this symbol too.  */
14150
0
    if (irel + 2 >= irelend
14151
0
        && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
14152
0
        && ELF32_R_SYM (irel[2].r_info) == r_symndx)
14153
0
      continue;
14154
14155
    /* See if the LUI instruction *might* be in a branch delay slot.
14156
       We check whether what looks like a 16-bit branch or jump is
14157
       actually an immediate argument to a compact branch, and let
14158
       it through if so.  */
14159
0
    if (irel->r_offset >= 2
14160
0
        && check_br16_dslot (abfd, ptr - 2)
14161
0
        && !(irel->r_offset >= 4
14162
0
       && (bzc = check_relocated_bzc (abfd,
14163
0
              ptr - 4, irel->r_offset - 4,
14164
0
              internal_relocs, irelend))))
14165
0
      continue;
14166
0
    if (irel->r_offset >= 4
14167
0
        && !bzc
14168
0
        && check_br32_dslot (abfd, ptr - 4))
14169
0
      continue;
14170
14171
0
    reg = OP32_SREG (opcode);
14172
14173
    /* We only relax adjacent instructions or ones separated with
14174
       a branch or jump that has a delay slot.  The branch or jump
14175
       must not fiddle with the register used to hold the address.
14176
       Subtract 4 for the LUI itself.  */
14177
0
    offset = irel[1].r_offset - irel[0].r_offset;
14178
0
    switch (offset - 4)
14179
0
      {
14180
0
      case 0:
14181
0
        break;
14182
0
      case 2:
14183
0
        if (check_br16 (abfd, ptr + 4, reg))
14184
0
    break;
14185
0
        continue;
14186
0
      case 4:
14187
0
        if (check_br32 (abfd, ptr + 4, reg))
14188
0
    break;
14189
0
        continue;
14190
0
      default:
14191
0
        continue;
14192
0
      }
14193
14194
0
    nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
14195
14196
    /* Give up unless the same register is used with both
14197
       relocations.  */
14198
0
    if (OP32_SREG (nextopc) != reg)
14199
0
      continue;
14200
14201
    /* Now adjust pcrval, subtracting the offset to the LO16 reloc
14202
       and rounding up to take masking of the two LSBs into account.  */
14203
0
    pcrval = ((pcrval - offset + 3) | 3) ^ 3;
14204
14205
    /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16.  */
14206
0
    if (IS_BITSIZE (symval, 16))
14207
0
      {
14208
        /* Fix the relocation's type.  */
14209
0
        irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
14210
14211
        /* Instructions using R_MICROMIPS_LO16 have the base or
14212
     source register in bits 20:16.  This register becomes $0
14213
     (zero) as the result of the R_MICROMIPS_HI16 being 0.  */
14214
0
        nextopc &= ~0x001f0000;
14215
0
        bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
14216
0
        contents + irel[1].r_offset);
14217
0
      }
14218
14219
    /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
14220
       We add 4 to take LUI deletion into account while checking
14221
       the PC-relative distance.  */
14222
0
    else if (symval % 4 == 0
14223
0
       && IS_BITSIZE (pcrval + 4, 25)
14224
0
       && MATCH (nextopc, addiu_insn)
14225
0
       && OP32_TREG (nextopc) == OP32_SREG (nextopc)
14226
0
       && OP16_VALID_REG (OP32_TREG (nextopc)))
14227
0
      {
14228
        /* Fix the relocation's type.  */
14229
0
        irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
14230
14231
        /* Replace ADDIU with the ADDIUPC version.  */
14232
0
        nextopc = (addiupc_insn.match
14233
0
       | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
14234
14235
0
        bfd_put_micromips_32 (abfd, nextopc,
14236
0
            contents + irel[1].r_offset);
14237
0
      }
14238
14239
    /* Can't do anything, give up, sigh...  */
14240
0
    else
14241
0
      continue;
14242
14243
    /* Fix the relocation's type.  */
14244
0
    irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
14245
14246
    /* Delete the LUI instruction: 4 bytes at irel->r_offset.  */
14247
0
    delcnt = 4;
14248
0
    deloff = 0;
14249
0
  }
14250
14251
      /* Compact branch relaxation -- due to the multitude of macros
14252
   employed by the compiler/assembler, compact branches are not
14253
   always generated.  Obviously, this can/will be fixed elsewhere,
14254
   but there is no drawback in double checking it here.  */
14255
0
      else if (r_type == R_MICROMIPS_PC16_S1
14256
0
         && irel->r_offset + 5 < sec->size
14257
0
         && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14258
0
       || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
14259
0
         && ((!insn32
14260
0
        && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
14261
0
          nop_insn_16) ? 2 : 0))
14262
0
       || (irel->r_offset + 7 < sec->size
14263
0
           && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
14264
0
                 ptr + 4),
14265
0
             nop_insn_32) ? 4 : 0))))
14266
0
  {
14267
0
    unsigned long reg;
14268
14269
0
    reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14270
14271
    /* Replace BEQZ/BNEZ with the compact version.  */
14272
0
    opcode = (bzc_insns_32[fndopc].match
14273
0
        | BZC32_REG_FIELD (reg)
14274
0
        | (opcode & 0xffff));   /* Addend value.  */
14275
14276
0
    bfd_put_micromips_32 (abfd, opcode, ptr);
14277
14278
    /* Delete the delay slot NOP: two or four bytes from
14279
       irel->offset + 4; delcnt has already been set above.  */
14280
0
    deloff = 4;
14281
0
  }
14282
14283
      /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1.  We need
14284
   to check the distance from the next instruction, so subtract 2.  */
14285
0
      else if (!insn32
14286
0
         && r_type == R_MICROMIPS_PC16_S1
14287
0
         && IS_BITSIZE (pcrval - 2, 11)
14288
0
         && find_match (opcode, b_insns_32) >= 0)
14289
0
  {
14290
    /* Fix the relocation's type.  */
14291
0
    irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
14292
14293
    /* Replace the 32-bit opcode with a 16-bit opcode.  */
14294
0
    bfd_put_16 (abfd,
14295
0
          (b_insn_16.match
14296
0
           | (opcode & 0x3ff)),   /* Addend value.  */
14297
0
          ptr);
14298
14299
    /* Delete 2 bytes from irel->r_offset + 2.  */
14300
0
    delcnt = 2;
14301
0
    deloff = 2;
14302
0
  }
14303
14304
      /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1.  We need
14305
   to check the distance from the next instruction, so subtract 2.  */
14306
0
      else if (!insn32
14307
0
         && r_type == R_MICROMIPS_PC16_S1
14308
0
         && IS_BITSIZE (pcrval - 2, 8)
14309
0
         && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14310
0
        && OP16_VALID_REG (OP32_SREG (opcode)))
14311
0
       || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
14312
0
           && OP16_VALID_REG (OP32_TREG (opcode)))))
14313
0
  {
14314
0
    unsigned long reg;
14315
14316
0
    reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14317
14318
    /* Fix the relocation's type.  */
14319
0
    irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
14320
14321
    /* Replace the 32-bit opcode with a 16-bit opcode.  */
14322
0
    bfd_put_16 (abfd,
14323
0
          (bz_insns_16[fndopc].match
14324
0
           | BZ16_REG_FIELD (reg)
14325
0
           | (opcode & 0x7f)),    /* Addend value.  */
14326
0
          ptr);
14327
14328
    /* Delete 2 bytes from irel->r_offset + 2.  */
14329
0
    delcnt = 2;
14330
0
    deloff = 2;
14331
0
  }
14332
14333
      /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets.  */
14334
0
      else if (!insn32
14335
0
         && r_type == R_MICROMIPS_26_S1
14336
0
         && target_is_micromips_code_p
14337
0
         && irel->r_offset + 7 < sec->size
14338
0
         && MATCH (opcode, jal_insn_32_bd32))
14339
0
  {
14340
0
    unsigned long n32opc;
14341
0
    bool relaxed = false;
14342
14343
0
    n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
14344
14345
0
    if (MATCH (n32opc, nop_insn_32))
14346
0
      {
14347
        /* Replace delay slot 32-bit NOP with a 16-bit NOP.  */
14348
0
        bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
14349
14350
0
        relaxed = true;
14351
0
      }
14352
0
    else if (find_match (n32opc, move_insns_32) >= 0)
14353
0
      {
14354
        /* Replace delay slot 32-bit MOVE with 16-bit MOVE.  */
14355
0
        bfd_put_16 (abfd,
14356
0
        (move_insn_16.match
14357
0
         | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
14358
0
         | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
14359
0
        ptr + 4);
14360
14361
0
        relaxed = true;
14362
0
      }
14363
    /* Other 32-bit instructions relaxable to 16-bit
14364
       instructions will be handled here later.  */
14365
14366
0
    if (relaxed)
14367
0
      {
14368
        /* JAL with 32-bit delay slot that is changed to a JALS
14369
     with 16-bit delay slot.  */
14370
0
        bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
14371
14372
        /* Delete 2 bytes from irel->r_offset + 6.  */
14373
0
        delcnt = 2;
14374
0
        deloff = 6;
14375
0
      }
14376
0
  }
14377
14378
0
      if (delcnt != 0)
14379
0
  {
14380
    /* Note that we've changed the relocs, section contents, etc.  */
14381
0
    elf_section_data (sec)->relocs = internal_relocs;
14382
0
    elf_section_data (sec)->this_hdr.contents = contents;
14383
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
14384
14385
    /* Delete bytes depending on the delcnt and deloff.  */
14386
0
    if (!mips_elf_relax_delete_bytes (abfd, sec,
14387
0
              irel->r_offset + deloff, delcnt))
14388
0
      goto error_return;
14389
14390
    /* That will change things, so we should relax again.
14391
       Note that this is not required, and it may be slow.  */
14392
0
    *again = true;
14393
0
  }
14394
0
    }
14395
14396
0
  if (isymbuf != NULL
14397
0
      && symtab_hdr->contents != (unsigned char *) isymbuf)
14398
0
    {
14399
0
      if (! link_info->keep_memory)
14400
0
  free (isymbuf);
14401
0
      else
14402
0
  {
14403
    /* Cache the symbols for elf_link_input_bfd.  */
14404
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
14405
0
  }
14406
0
    }
14407
14408
0
  if (contents != NULL
14409
0
      && elf_section_data (sec)->this_hdr.contents != contents)
14410
0
    {
14411
0
      if (! link_info->keep_memory)
14412
0
  free (contents);
14413
0
      else
14414
0
  {
14415
    /* Cache the section contents for elf_link_input_bfd.  */
14416
0
    elf_section_data (sec)->this_hdr.contents = contents;
14417
0
  }
14418
0
    }
14419
14420
0
  if (elf_section_data (sec)->relocs != internal_relocs)
14421
0
    free (internal_relocs);
14422
14423
0
  return true;
14424
14425
0
 error_return:
14426
0
  if (symtab_hdr->contents != (unsigned char *) isymbuf)
14427
0
    free (isymbuf);
14428
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
14429
0
    free (contents);
14430
0
  if (elf_section_data (sec)->relocs != internal_relocs)
14431
0
    free (internal_relocs);
14432
14433
0
  return false;
14434
0
}
14435

14436
/* Create a MIPS ELF linker hash table.  */
14437
14438
struct bfd_link_hash_table *
14439
_bfd_mips_elf_link_hash_table_create (bfd *abfd)
14440
0
{
14441
0
  struct mips_elf_link_hash_table *ret;
14442
0
  size_t amt = sizeof (struct mips_elf_link_hash_table);
14443
14444
0
  ret = bfd_zmalloc (amt);
14445
0
  if (ret == NULL)
14446
0
    return NULL;
14447
14448
0
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
14449
0
              mips_elf_link_hash_newfunc,
14450
0
              sizeof (struct mips_elf_link_hash_entry),
14451
0
              MIPS_ELF_DATA))
14452
0
    {
14453
0
      free (ret);
14454
0
      return NULL;
14455
0
    }
14456
0
  ret->root.init_plt_refcount.plist = NULL;
14457
0
  ret->root.init_plt_offset.plist = NULL;
14458
14459
0
  return &ret->root.root;
14460
0
}
14461
14462
/* Likewise, but indicate that the target is VxWorks.  */
14463
14464
struct bfd_link_hash_table *
14465
_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
14466
0
{
14467
0
  struct bfd_link_hash_table *ret;
14468
14469
0
  ret = _bfd_mips_elf_link_hash_table_create (abfd);
14470
0
  if (ret)
14471
0
    {
14472
0
      struct mips_elf_link_hash_table *htab;
14473
14474
0
      htab = (struct mips_elf_link_hash_table *) ret;
14475
0
      htab->use_plts_and_copy_relocs = true;
14476
0
    }
14477
0
  return ret;
14478
0
}
14479
14480
/* A function that the linker calls if we are allowed to use PLTs
14481
   and copy relocs.  */
14482
14483
void
14484
_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
14485
0
{
14486
0
  mips_elf_hash_table (info)->use_plts_and_copy_relocs = true;
14487
0
}
14488
14489
/* A function that the linker calls to select between all or only
14490
   32-bit microMIPS instructions, and between making or ignoring
14491
   branch relocation checks for invalid transitions between ISA modes.
14492
   Also record whether we have been configured for a GNU target.  */
14493
14494
void
14495
_bfd_mips_elf_linker_flags (struct bfd_link_info *info, bool insn32,
14496
          bool ignore_branch_isa,
14497
          bool gnu_target)
14498
0
{
14499
0
  mips_elf_hash_table (info)->insn32 = insn32;
14500
0
  mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
14501
0
  mips_elf_hash_table (info)->gnu_target = gnu_target;
14502
0
}
14503
14504
/* A function that the linker calls to enable use of compact branches in
14505
   linker generated code for MIPSR6.  */
14506
14507
void
14508
_bfd_mips_elf_compact_branches (struct bfd_link_info *info, bool on)
14509
0
{
14510
0
  mips_elf_hash_table (info)->compact_branches = on;
14511
0
}
14512
14513

14514
/* Structure for saying that BFD machine EXTENSION extends BASE.  */
14515
14516
struct mips_mach_extension
14517
{
14518
  unsigned long extension, base;
14519
};
14520
14521
/* An array that maps 64-bit architectures to the corresponding 32-bit
14522
   architectures.  */
14523
static const struct mips_mach_extension mips_mach_32_64[] =
14524
{
14525
  { bfd_mach_mipsisa64r6, bfd_mach_mipsisa32r6 },
14526
  { bfd_mach_mipsisa64r5, bfd_mach_mipsisa32r5 },
14527
  { bfd_mach_mipsisa64r3, bfd_mach_mipsisa32r3 },
14528
  { bfd_mach_mipsisa64r2, bfd_mach_mipsisa32r2 },
14529
  { bfd_mach_mipsisa64,   bfd_mach_mipsisa32 }
14530
};
14531
14532
/* An array describing how BFD machines relate to one another.  The entries
14533
   are ordered topologically with MIPS I extensions listed last.  */
14534
14535
static const struct mips_mach_extension mips_mach_extensions[] =
14536
{
14537
  /* MIPS64r2 extensions.  */
14538
  { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
14539
  { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14540
  { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14541
  { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14542
  { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
14543
  { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
14544
  { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
14545
14546
  /* MIPS64 extensions.  */
14547
  { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14548
  { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14549
  { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14550
14551
  /* MIPS V extensions.  */
14552
  { bfd_mach_mipsisa64, bfd_mach_mips5 },
14553
14554
  /* R10000 extensions.  */
14555
  { bfd_mach_mips12000, bfd_mach_mips10000 },
14556
  { bfd_mach_mips14000, bfd_mach_mips10000 },
14557
  { bfd_mach_mips16000, bfd_mach_mips10000 },
14558
14559
  /* R5000 extensions.  Note: the vr5500 ISA is an extension of the core
14560
     vr5400 ISA, but doesn't include the multimedia stuff.  It seems
14561
     better to allow vr5400 and vr5500 code to be merged anyway, since
14562
     many libraries will just use the core ISA.  Perhaps we could add
14563
     some sort of ASE flag if this ever proves a problem.  */
14564
  { bfd_mach_mips5500, bfd_mach_mips5400 },
14565
  { bfd_mach_mips5400, bfd_mach_mips5000 },
14566
14567
  /* MIPS IV extensions.  */
14568
  { bfd_mach_mips5, bfd_mach_mips8000 },
14569
  { bfd_mach_mips10000, bfd_mach_mips8000 },
14570
  { bfd_mach_mips5000, bfd_mach_mips8000 },
14571
  { bfd_mach_mips7000, bfd_mach_mips8000 },
14572
  { bfd_mach_mips9000, bfd_mach_mips8000 },
14573
14574
  /* VR4100 extensions.  */
14575
  { bfd_mach_mips4120, bfd_mach_mips4100 },
14576
  { bfd_mach_mips4111, bfd_mach_mips4100 },
14577
14578
  /* MIPS III extensions.  */
14579
  { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14580
  { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14581
  { bfd_mach_mips8000, bfd_mach_mips4000 },
14582
  { bfd_mach_mips4650, bfd_mach_mips4000 },
14583
  { bfd_mach_mips4600, bfd_mach_mips4000 },
14584
  { bfd_mach_mips4400, bfd_mach_mips4000 },
14585
  { bfd_mach_mips4300, bfd_mach_mips4000 },
14586
  { bfd_mach_mips4100, bfd_mach_mips4000 },
14587
  { bfd_mach_mips5900, bfd_mach_mips4000 },
14588
14589
  /* MIPS32r3 extensions.  */
14590
  { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
14591
14592
  /* MIPS32r2 extensions.  */
14593
  { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
14594
14595
  /* MIPS32 extensions.  */
14596
  { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14597
14598
  /* MIPS II extensions.  */
14599
  { bfd_mach_mips4000, bfd_mach_mips6000 },
14600
  { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14601
  { bfd_mach_mips4010, bfd_mach_mips6000 },
14602
  { bfd_mach_mips_allegrex, bfd_mach_mips6000 },
14603
14604
  /* MIPS I extensions.  */
14605
  { bfd_mach_mips6000, bfd_mach_mips3000 },
14606
  { bfd_mach_mips3900, bfd_mach_mips3000 }
14607
};
14608
14609
/* Return true if bfd machine EXTENSION is the same as BASE, or if
14610
   EXTENSION is the 64-bit equivalent of a 32-bit BASE.  */
14611
14612
static bool
14613
mips_mach_extends_32_64 (unsigned long base, unsigned long extension)
14614
0
{
14615
0
  size_t i;
14616
14617
0
  if (extension == base)
14618
0
    return true;
14619
14620
0
  for (i = 0; i < ARRAY_SIZE (mips_mach_32_64); i++)
14621
0
    if (extension == mips_mach_32_64[i].extension)
14622
0
      return base == mips_mach_32_64[i].base;
14623
14624
0
  return false;
14625
0
}
14626
14627
static bool
14628
mips_mach_extends_p (unsigned long base, unsigned long extension)
14629
0
{
14630
0
  size_t i;
14631
14632
0
  if (mips_mach_extends_32_64 (base, extension))
14633
0
    return true;
14634
14635
0
  for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14636
0
    if (extension == mips_mach_extensions[i].extension)
14637
0
      {
14638
0
  extension = mips_mach_extensions[i].base;
14639
0
  if (mips_mach_extends_32_64 (base, extension))
14640
0
    return true;
14641
0
      }
14642
14643
0
  return false;
14644
0
}
14645
14646
/* Return the BFD mach for each .MIPS.abiflags ISA Extension.  */
14647
14648
static unsigned long
14649
bfd_mips_isa_ext_mach (unsigned int isa_ext)
14650
0
{
14651
0
  switch (isa_ext)
14652
0
    {
14653
0
    case AFL_EXT_3900:       return bfd_mach_mips3900;
14654
0
    case AFL_EXT_4010:       return bfd_mach_mips4010;
14655
0
    case AFL_EXT_4100:       return bfd_mach_mips4100;
14656
0
    case AFL_EXT_4111:       return bfd_mach_mips4111;
14657
0
    case AFL_EXT_4120:       return bfd_mach_mips4120;
14658
0
    case AFL_EXT_4650:       return bfd_mach_mips4650;
14659
0
    case AFL_EXT_5400:       return bfd_mach_mips5400;
14660
0
    case AFL_EXT_5500:       return bfd_mach_mips5500;
14661
0
    case AFL_EXT_5900:       return bfd_mach_mips5900;
14662
0
    case AFL_EXT_10000:       return bfd_mach_mips10000;
14663
0
    case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
14664
0
    case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
14665
0
    case AFL_EXT_SB1:       return bfd_mach_mips_sb1;
14666
0
    case AFL_EXT_OCTEON:      return bfd_mach_mips_octeon;
14667
0
    case AFL_EXT_OCTEONP:     return bfd_mach_mips_octeonp;
14668
0
    case AFL_EXT_OCTEON2:     return bfd_mach_mips_octeon2;
14669
0
    case AFL_EXT_XLR:       return bfd_mach_mips_xlr;
14670
0
    default:          return bfd_mach_mips3000;
14671
0
    }
14672
0
}
14673
14674
/* Return the .MIPS.abiflags value representing each ISA Extension.  */
14675
14676
unsigned int
14677
bfd_mips_isa_ext (bfd *abfd)
14678
0
{
14679
0
  switch (bfd_get_mach (abfd))
14680
0
    {
14681
0
    case bfd_mach_mips3900:     return AFL_EXT_3900;
14682
0
    case bfd_mach_mips4010:     return AFL_EXT_4010;
14683
0
    case bfd_mach_mips4100:     return AFL_EXT_4100;
14684
0
    case bfd_mach_mips4111:     return AFL_EXT_4111;
14685
0
    case bfd_mach_mips4120:     return AFL_EXT_4120;
14686
0
    case bfd_mach_mips4650:     return AFL_EXT_4650;
14687
0
    case bfd_mach_mips5400:     return AFL_EXT_5400;
14688
0
    case bfd_mach_mips5500:     return AFL_EXT_5500;
14689
0
    case bfd_mach_mips5900:     return AFL_EXT_5900;
14690
0
    case bfd_mach_mips10000:     return AFL_EXT_10000;
14691
0
    case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
14692
0
    case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
14693
0
    case bfd_mach_mips_sb1:     return AFL_EXT_SB1;
14694
0
    case bfd_mach_mips_octeon:     return AFL_EXT_OCTEON;
14695
0
    case bfd_mach_mips_octeonp:     return AFL_EXT_OCTEONP;
14696
0
    case bfd_mach_mips_octeon3:     return AFL_EXT_OCTEON3;
14697
0
    case bfd_mach_mips_octeon2:     return AFL_EXT_OCTEON2;
14698
0
    case bfd_mach_mips_xlr:     return AFL_EXT_XLR;
14699
0
    case bfd_mach_mips_interaptiv_mr2:
14700
0
      return AFL_EXT_INTERAPTIV_MR2;
14701
0
    default:          return 0;
14702
0
    }
14703
0
}
14704
14705
/* Encode ISA level and revision as a single value.  */
14706
0
#define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
14707
14708
/* Decode a single value into level and revision.  */
14709
0
#define ISA_LEVEL(LEVREV)  ((LEVREV) >> 3)
14710
0
#define ISA_REV(LEVREV)    ((LEVREV) & 0x7)
14711
14712
/* Update the isa_level, isa_rev, isa_ext fields of abiflags.  */
14713
14714
static void
14715
update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
14716
0
{
14717
0
  int new_isa = 0;
14718
0
  switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
14719
0
    {
14720
0
    case E_MIPS_ARCH_1:    new_isa = LEVEL_REV (1, 0); break;
14721
0
    case E_MIPS_ARCH_2:    new_isa = LEVEL_REV (2, 0); break;
14722
0
    case E_MIPS_ARCH_3:    new_isa = LEVEL_REV (3, 0); break;
14723
0
    case E_MIPS_ARCH_4:    new_isa = LEVEL_REV (4, 0); break;
14724
0
    case E_MIPS_ARCH_5:    new_isa = LEVEL_REV (5, 0); break;
14725
0
    case E_MIPS_ARCH_32:   new_isa = LEVEL_REV (32, 1); break;
14726
0
    case E_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
14727
0
    case E_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
14728
0
    case E_MIPS_ARCH_64:   new_isa = LEVEL_REV (64, 1); break;
14729
0
    case E_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
14730
0
    case E_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
14731
0
    default:
14732
0
      _bfd_error_handler
14733
  /* xgettext:c-format */
14734
0
  (_("%pB: unknown architecture %s"),
14735
0
   abfd, bfd_printable_name (abfd));
14736
0
    }
14737
14738
0
  if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
14739
0
    {
14740
0
      abiflags->isa_level = ISA_LEVEL (new_isa);
14741
0
      abiflags->isa_rev = ISA_REV (new_isa);
14742
0
    }
14743
14744
  /* Update the isa_ext if ABFD describes a further extension.  */
14745
0
  if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
14746
0
         bfd_get_mach (abfd)))
14747
0
    abiflags->isa_ext = bfd_mips_isa_ext (abfd);
14748
0
}
14749
14750
/* Return true if the given ELF header flags describe a 32-bit binary.  */
14751
14752
static bool
14753
mips_32bit_flags_p (flagword flags)
14754
0
{
14755
0
  return ((flags & EF_MIPS_32BITMODE) != 0
14756
0
    || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
14757
0
    || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
14758
0
    || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
14759
0
    || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
14760
0
    || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
14761
0
    || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2
14762
0
    || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6);
14763
0
}
14764
14765
/* Infer the content of the ABI flags based on the elf header.  */
14766
14767
static void
14768
infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
14769
0
{
14770
0
  obj_attribute *in_attr;
14771
14772
0
  memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
14773
0
  update_mips_abiflags_isa (abfd, abiflags);
14774
14775
0
  if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
14776
0
    abiflags->gpr_size = AFL_REG_32;
14777
0
  else
14778
0
    abiflags->gpr_size = AFL_REG_64;
14779
14780
0
  abiflags->cpr1_size = AFL_REG_NONE;
14781
14782
0
  in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
14783
0
  abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14784
14785
0
  if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
14786
0
      || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
14787
0
      || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14788
0
    && abiflags->gpr_size == AFL_REG_32))
14789
0
    abiflags->cpr1_size = AFL_REG_32;
14790
0
  else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14791
0
     || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
14792
0
     || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
14793
0
    abiflags->cpr1_size = AFL_REG_64;
14794
14795
0
  abiflags->cpr2_size = AFL_REG_NONE;
14796
14797
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14798
0
    abiflags->ases |= AFL_ASE_MDMX;
14799
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14800
0
    abiflags->ases |= AFL_ASE_MIPS16;
14801
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14802
0
    abiflags->ases |= AFL_ASE_MICROMIPS;
14803
14804
0
  if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14805
0
      && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
14806
0
      && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
14807
0
      && abiflags->isa_level >= 32
14808
0
      && abiflags->ases != AFL_ASE_LOONGSON_EXT)
14809
0
    abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
14810
0
}
14811
14812
/* We need to use a special link routine to handle the .reginfo and
14813
   the .mdebug sections.  We need to merge all instances of these
14814
   sections together, not write them all out sequentially.  */
14815
14816
bool
14817
_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
14818
0
{
14819
0
  asection *o;
14820
0
  struct bfd_link_order *p;
14821
0
  asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
14822
0
  asection *rtproc_sec, *abiflags_sec;
14823
0
  Elf32_RegInfo reginfo;
14824
0
  struct ecoff_debug_info debug;
14825
0
  struct mips_htab_traverse_info hti;
14826
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14827
0
  const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
14828
0
  HDRR *symhdr = &debug.symbolic_header;
14829
0
  void *mdebug_handle = NULL;
14830
0
  asection *s;
14831
0
  EXTR esym;
14832
0
  unsigned int i;
14833
0
  bfd_size_type amt;
14834
0
  struct mips_elf_link_hash_table *htab;
14835
14836
0
  static const char * const secname[] =
14837
0
  {
14838
0
    ".text", ".init", ".fini", ".data",
14839
0
    ".rodata", ".sdata", ".sbss", ".bss"
14840
0
  };
14841
0
  static const int sc[] =
14842
0
  {
14843
0
    scText, scInit, scFini, scData,
14844
0
    scRData, scSData, scSBss, scBss
14845
0
  };
14846
14847
0
  htab = mips_elf_hash_table (info);
14848
0
  BFD_ASSERT (htab != NULL);
14849
14850
  /* Sort the dynamic symbols so that those with GOT entries come after
14851
     those without.  */
14852
0
  if (!mips_elf_sort_hash_table (abfd, info))
14853
0
    return false;
14854
14855
  /* Create any scheduled LA25 stubs.  */
14856
0
  hti.info = info;
14857
0
  hti.output_bfd = abfd;
14858
0
  hti.error = false;
14859
0
  htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
14860
0
  if (hti.error)
14861
0
    return false;
14862
14863
  /* Get a value for the GP register.  */
14864
0
  if (elf_gp (abfd) == 0)
14865
0
    {
14866
0
      struct bfd_link_hash_entry *h;
14867
14868
0
      h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
14869
0
      if (h != NULL && h->type == bfd_link_hash_defined)
14870
0
  elf_gp (abfd) = (h->u.def.value
14871
0
       + h->u.def.section->output_section->vma
14872
0
       + h->u.def.section->output_offset);
14873
0
      else if (htab->root.target_os == is_vxworks
14874
0
         && (h = bfd_link_hash_lookup (info->hash,
14875
0
               "_GLOBAL_OFFSET_TABLE_",
14876
0
               false, false, true))
14877
0
         && h->type == bfd_link_hash_defined)
14878
0
  elf_gp (abfd) = (h->u.def.section->output_section->vma
14879
0
       + h->u.def.section->output_offset
14880
0
       + h->u.def.value);
14881
0
      else if (bfd_link_relocatable (info))
14882
0
  {
14883
0
    bfd_vma lo = MINUS_ONE;
14884
14885
    /* Find the GP-relative section with the lowest offset.  */
14886
0
    for (o = abfd->sections; o != NULL; o = o->next)
14887
0
      if (o->vma < lo
14888
0
    && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
14889
0
        lo = o->vma;
14890
14891
    /* And calculate GP relative to that.  */
14892
0
    elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
14893
0
  }
14894
0
      else
14895
0
  {
14896
    /* If the relocate_section function needs to do a reloc
14897
       involving the GP value, it should make a reloc_dangerous
14898
       callback to warn that GP is not defined.  */
14899
0
  }
14900
0
    }
14901
14902
  /* Go through the sections and collect the .reginfo and .mdebug
14903
     information.  */
14904
0
  abiflags_sec = NULL;
14905
0
  reginfo_sec = NULL;
14906
0
  mdebug_sec = NULL;
14907
0
  gptab_data_sec = NULL;
14908
0
  gptab_bss_sec = NULL;
14909
0
  for (o = abfd->sections; o != NULL; o = o->next)
14910
0
    {
14911
0
      if (strcmp (o->name, ".MIPS.abiflags") == 0)
14912
0
  {
14913
    /* We have found the .MIPS.abiflags section in the output file.
14914
       Look through all the link_orders comprising it and remove them.
14915
       The data is merged in _bfd_mips_elf_merge_private_bfd_data.  */
14916
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
14917
0
      {
14918
0
        asection *input_section;
14919
14920
0
        if (p->type != bfd_indirect_link_order)
14921
0
    {
14922
0
      if (p->type == bfd_data_link_order)
14923
0
        continue;
14924
0
      abort ();
14925
0
    }
14926
14927
0
        input_section = p->u.indirect.section;
14928
14929
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
14930
     elf_link_input_bfd ignores this section.  */
14931
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
14932
0
      }
14933
14934
    /* Size has been set in _bfd_mips_elf_always_size_sections.  */
14935
0
    BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
14936
14937
    /* Skip this section later on (I don't think this currently
14938
       matters, but someday it might).  */
14939
0
    o->map_head.link_order = NULL;
14940
14941
0
    abiflags_sec = o;
14942
0
  }
14943
14944
0
      if (strcmp (o->name, ".reginfo") == 0)
14945
0
  {
14946
0
    memset (&reginfo, 0, sizeof reginfo);
14947
14948
    /* We have found the .reginfo section in the output file.
14949
       Look through all the link_orders comprising it and merge
14950
       the information together.  */
14951
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
14952
0
      {
14953
0
        asection *input_section;
14954
0
        bfd *input_bfd;
14955
0
        Elf32_External_RegInfo ext;
14956
0
        Elf32_RegInfo sub;
14957
0
        bfd_size_type sz;
14958
14959
0
        if (p->type != bfd_indirect_link_order)
14960
0
    {
14961
0
      if (p->type == bfd_data_link_order)
14962
0
        continue;
14963
0
      abort ();
14964
0
    }
14965
14966
0
        input_section = p->u.indirect.section;
14967
0
        input_bfd = input_section->owner;
14968
14969
0
        sz = (input_section->size < sizeof (ext)
14970
0
        ? input_section->size : sizeof (ext));
14971
0
        memset (&ext, 0, sizeof (ext));
14972
0
        if (! bfd_get_section_contents (input_bfd, input_section,
14973
0
                &ext, 0, sz))
14974
0
    return false;
14975
14976
0
        bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
14977
14978
0
        reginfo.ri_gprmask |= sub.ri_gprmask;
14979
0
        reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
14980
0
        reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
14981
0
        reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
14982
0
        reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
14983
14984
        /* ri_gp_value is set by the function
14985
     `_bfd_mips_elf_section_processing' when the section is
14986
     finally written out.  */
14987
14988
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
14989
     elf_link_input_bfd ignores this section.  */
14990
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
14991
0
      }
14992
14993
    /* Size has been set in _bfd_mips_elf_always_size_sections.  */
14994
0
    BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
14995
14996
    /* Skip this section later on (I don't think this currently
14997
       matters, but someday it might).  */
14998
0
    o->map_head.link_order = NULL;
14999
15000
0
    reginfo_sec = o;
15001
0
  }
15002
15003
0
      if (strcmp (o->name, ".mdebug") == 0)
15004
0
  {
15005
0
    struct extsym_info einfo;
15006
0
    bfd_vma last;
15007
15008
    /* We have found the .mdebug section in the output file.
15009
       Look through all the link_orders comprising it and merge
15010
       the information together.  */
15011
0
    symhdr->magic = swap->sym_magic;
15012
    /* FIXME: What should the version stamp be?  */
15013
0
    symhdr->vstamp = 0;
15014
0
    symhdr->ilineMax = 0;
15015
0
    symhdr->cbLine = 0;
15016
0
    symhdr->idnMax = 0;
15017
0
    symhdr->ipdMax = 0;
15018
0
    symhdr->isymMax = 0;
15019
0
    symhdr->ioptMax = 0;
15020
0
    symhdr->iauxMax = 0;
15021
0
    symhdr->issMax = 0;
15022
0
    symhdr->issExtMax = 0;
15023
0
    symhdr->ifdMax = 0;
15024
0
    symhdr->crfd = 0;
15025
0
    symhdr->iextMax = 0;
15026
15027
    /* We accumulate the debugging information itself in the
15028
       debug_info structure.  */
15029
0
    debug.alloc_syments = false;
15030
0
    debug.line = NULL;
15031
0
    debug.external_dnr = NULL;
15032
0
    debug.external_pdr = NULL;
15033
0
    debug.external_sym = NULL;
15034
0
    debug.external_opt = NULL;
15035
0
    debug.external_aux = NULL;
15036
0
    debug.ss = NULL;
15037
0
    debug.ssext = debug.ssext_end = NULL;
15038
0
    debug.external_fdr = NULL;
15039
0
    debug.external_rfd = NULL;
15040
0
    debug.external_ext = debug.external_ext_end = NULL;
15041
15042
0
    mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
15043
0
    if (mdebug_handle == NULL)
15044
0
      return false;
15045
15046
0
    esym.jmptbl = 0;
15047
0
    esym.cobol_main = 0;
15048
0
    esym.weakext = 0;
15049
0
    esym.reserved = 0;
15050
0
    esym.ifd = ifdNil;
15051
0
    esym.asym.iss = issNil;
15052
0
    esym.asym.st = stLocal;
15053
0
    esym.asym.reserved = 0;
15054
0
    esym.asym.index = indexNil;
15055
0
    last = 0;
15056
0
    for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
15057
0
      {
15058
0
        esym.asym.sc = sc[i];
15059
0
        s = bfd_get_section_by_name (abfd, secname[i]);
15060
0
        if (s != NULL)
15061
0
    {
15062
0
      esym.asym.value = s->vma;
15063
0
      last = s->vma + s->size;
15064
0
    }
15065
0
        else
15066
0
    esym.asym.value = last;
15067
0
        if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
15068
0
             secname[i], &esym))
15069
0
    return false;
15070
0
      }
15071
15072
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
15073
0
      {
15074
0
        asection *input_section;
15075
0
        bfd *input_bfd;
15076
0
        const struct ecoff_debug_swap *input_swap;
15077
0
        struct ecoff_debug_info input_debug;
15078
0
        char *eraw_src;
15079
0
        char *eraw_end;
15080
15081
0
        if (p->type != bfd_indirect_link_order)
15082
0
    {
15083
0
      if (p->type == bfd_data_link_order)
15084
0
        continue;
15085
0
      abort ();
15086
0
    }
15087
15088
0
        input_section = p->u.indirect.section;
15089
0
        input_bfd = input_section->owner;
15090
15091
0
        if (!is_mips_elf (input_bfd))
15092
0
    {
15093
      /* I don't know what a non MIPS ELF bfd would be
15094
         doing with a .mdebug section, but I don't really
15095
         want to deal with it.  */
15096
0
      continue;
15097
0
    }
15098
15099
0
        input_swap = (get_elf_backend_data (input_bfd)
15100
0
          ->elf_backend_ecoff_debug_swap);
15101
15102
0
        BFD_ASSERT (p->size == input_section->size);
15103
15104
        /* The ECOFF linking code expects that we have already
15105
     read in the debugging information and set up an
15106
     ecoff_debug_info structure, so we do that now.  */
15107
0
        if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
15108
0
               &input_debug))
15109
0
    return false;
15110
15111
0
        if (! (bfd_ecoff_debug_accumulate
15112
0
         (mdebug_handle, abfd, &debug, swap, input_bfd,
15113
0
          &input_debug, input_swap, info)))
15114
0
    {
15115
0
      _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15116
0
      return false;
15117
0
    }
15118
15119
        /* Loop through the external symbols.  For each one with
15120
     interesting information, try to find the symbol in
15121
     the linker global hash table and save the information
15122
     for the output external symbols.  */
15123
0
        eraw_src = input_debug.external_ext;
15124
0
        eraw_end = (eraw_src
15125
0
        + (input_debug.symbolic_header.iextMax
15126
0
           * input_swap->external_ext_size));
15127
0
        for (;
15128
0
       eraw_src < eraw_end;
15129
0
       eraw_src += input_swap->external_ext_size)
15130
0
    {
15131
0
      EXTR ext;
15132
0
      const char *name;
15133
0
      struct mips_elf_link_hash_entry *h;
15134
15135
0
      (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
15136
0
      if (ext.asym.sc == scNil
15137
0
          || ext.asym.sc == scUndefined
15138
0
          || ext.asym.sc == scSUndefined)
15139
0
        continue;
15140
15141
0
      name = input_debug.ssext + ext.asym.iss;
15142
0
      h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
15143
0
             name, false, false, true);
15144
0
      if (h == NULL || h->esym.ifd != -2)
15145
0
        continue;
15146
15147
0
      if (ext.ifd != -1)
15148
0
        {
15149
0
          BFD_ASSERT (ext.ifd
15150
0
          < input_debug.symbolic_header.ifdMax);
15151
0
          ext.ifd = input_debug.ifdmap[ext.ifd];
15152
0
        }
15153
15154
0
      h->esym = ext;
15155
0
    }
15156
15157
        /* Free up the information we just read.  */
15158
0
        _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15159
15160
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
15161
     elf_link_input_bfd ignores this section.  */
15162
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
15163
0
      }
15164
15165
0
    if (SGI_COMPAT (abfd) && bfd_link_pic (info))
15166
0
      {
15167
        /* Create .rtproc section.  */
15168
0
        rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
15169
0
        if (rtproc_sec == NULL)
15170
0
    {
15171
0
      flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
15172
0
            | SEC_LINKER_CREATED | SEC_READONLY);
15173
15174
0
      rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
15175
0
                   ".rtproc",
15176
0
                   flags);
15177
0
      if (rtproc_sec == NULL
15178
0
          || !bfd_set_section_alignment (rtproc_sec, 4))
15179
0
        return false;
15180
0
    }
15181
15182
0
        if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
15183
0
                 info, rtproc_sec,
15184
0
                 &debug))
15185
0
    return false;
15186
0
      }
15187
15188
    /* Build the external symbol information.  */
15189
0
    einfo.abfd = abfd;
15190
0
    einfo.info = info;
15191
0
    einfo.debug = &debug;
15192
0
    einfo.swap = swap;
15193
0
    einfo.failed = false;
15194
0
    mips_elf_link_hash_traverse (mips_elf_hash_table (info),
15195
0
               mips_elf_output_extsym, &einfo);
15196
0
    if (einfo.failed)
15197
0
      return false;
15198
15199
    /* Set the size of the .mdebug section.  */
15200
0
    o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
15201
15202
    /* Skip this section later on (I don't think this currently
15203
       matters, but someday it might).  */
15204
0
    o->map_head.link_order = NULL;
15205
15206
0
    mdebug_sec = o;
15207
0
  }
15208
15209
0
      if (startswith (o->name, ".gptab."))
15210
0
  {
15211
0
    const char *subname;
15212
0
    unsigned int c;
15213
0
    Elf32_gptab *tab;
15214
0
    Elf32_External_gptab *ext_tab;
15215
0
    unsigned int j;
15216
15217
    /* The .gptab.sdata and .gptab.sbss sections hold
15218
       information describing how the small data area would
15219
       change depending upon the -G switch.  These sections
15220
       not used in executables files.  */
15221
0
    if (! bfd_link_relocatable (info))
15222
0
      {
15223
0
        for (p = o->map_head.link_order; p != NULL; p = p->next)
15224
0
    {
15225
0
      asection *input_section;
15226
15227
0
      if (p->type != bfd_indirect_link_order)
15228
0
        {
15229
0
          if (p->type == bfd_data_link_order)
15230
0
      continue;
15231
0
          abort ();
15232
0
        }
15233
15234
0
      input_section = p->u.indirect.section;
15235
15236
      /* Hack: reset the SEC_HAS_CONTENTS flag so that
15237
         elf_link_input_bfd ignores this section.  */
15238
0
      input_section->flags &= ~SEC_HAS_CONTENTS;
15239
0
    }
15240
15241
        /* Skip this section later on (I don't think this
15242
     currently matters, but someday it might).  */
15243
0
        o->map_head.link_order = NULL;
15244
15245
        /* Really remove the section.  */
15246
0
        bfd_section_list_remove (abfd, o);
15247
0
        --abfd->section_count;
15248
15249
0
        continue;
15250
0
      }
15251
15252
    /* There is one gptab for initialized data, and one for
15253
       uninitialized data.  */
15254
0
    if (strcmp (o->name, ".gptab.sdata") == 0)
15255
0
      gptab_data_sec = o;
15256
0
    else if (strcmp (o->name, ".gptab.sbss") == 0)
15257
0
      gptab_bss_sec = o;
15258
0
    else
15259
0
      {
15260
0
        _bfd_error_handler
15261
    /* xgettext:c-format */
15262
0
    (_("%pB: illegal section name `%pA'"), abfd, o);
15263
0
        bfd_set_error (bfd_error_nonrepresentable_section);
15264
0
        return false;
15265
0
      }
15266
15267
    /* The linker script always combines .gptab.data and
15268
       .gptab.sdata into .gptab.sdata, and likewise for
15269
       .gptab.bss and .gptab.sbss.  It is possible that there is
15270
       no .sdata or .sbss section in the output file, in which
15271
       case we must change the name of the output section.  */
15272
0
    subname = o->name + sizeof ".gptab" - 1;
15273
0
    if (bfd_get_section_by_name (abfd, subname) == NULL)
15274
0
      {
15275
0
        if (o == gptab_data_sec)
15276
0
    o->name = ".gptab.data";
15277
0
        else
15278
0
    o->name = ".gptab.bss";
15279
0
        subname = o->name + sizeof ".gptab" - 1;
15280
0
        BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
15281
0
      }
15282
15283
    /* Set up the first entry.  */
15284
0
    c = 1;
15285
0
    amt = c * sizeof (Elf32_gptab);
15286
0
    tab = bfd_malloc (amt);
15287
0
    if (tab == NULL)
15288
0
      return false;
15289
0
    tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
15290
0
    tab[0].gt_header.gt_unused = 0;
15291
15292
    /* Combine the input sections.  */
15293
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
15294
0
      {
15295
0
        asection *input_section;
15296
0
        bfd *input_bfd;
15297
0
        bfd_size_type size;
15298
0
        unsigned long last;
15299
0
        bfd_size_type gpentry;
15300
15301
0
        if (p->type != bfd_indirect_link_order)
15302
0
    {
15303
0
      if (p->type == bfd_data_link_order)
15304
0
        continue;
15305
0
      abort ();
15306
0
    }
15307
15308
0
        input_section = p->u.indirect.section;
15309
0
        input_bfd = input_section->owner;
15310
15311
        /* Combine the gptab entries for this input section one
15312
     by one.  We know that the input gptab entries are
15313
     sorted by ascending -G value.  */
15314
0
        size = input_section->size;
15315
0
        last = 0;
15316
0
        for (gpentry = sizeof (Elf32_External_gptab);
15317
0
       gpentry < size;
15318
0
       gpentry += sizeof (Elf32_External_gptab))
15319
0
    {
15320
0
      Elf32_External_gptab ext_gptab;
15321
0
      Elf32_gptab int_gptab;
15322
0
      unsigned long val;
15323
0
      unsigned long add;
15324
0
      bool exact;
15325
0
      unsigned int look;
15326
15327
0
      if (! (bfd_get_section_contents
15328
0
       (input_bfd, input_section, &ext_gptab, gpentry,
15329
0
        sizeof (Elf32_External_gptab))))
15330
0
        {
15331
0
          free (tab);
15332
0
          return false;
15333
0
        }
15334
15335
0
      bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
15336
0
            &int_gptab);
15337
0
      val = int_gptab.gt_entry.gt_g_value;
15338
0
      add = int_gptab.gt_entry.gt_bytes - last;
15339
15340
0
      exact = false;
15341
0
      for (look = 1; look < c; look++)
15342
0
        {
15343
0
          if (tab[look].gt_entry.gt_g_value >= val)
15344
0
      tab[look].gt_entry.gt_bytes += add;
15345
15346
0
          if (tab[look].gt_entry.gt_g_value == val)
15347
0
      exact = true;
15348
0
        }
15349
15350
0
      if (! exact)
15351
0
        {
15352
0
          Elf32_gptab *new_tab;
15353
0
          unsigned int max;
15354
15355
          /* We need a new table entry.  */
15356
0
          amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
15357
0
          new_tab = bfd_realloc (tab, amt);
15358
0
          if (new_tab == NULL)
15359
0
      {
15360
0
        free (tab);
15361
0
        return false;
15362
0
      }
15363
0
          tab = new_tab;
15364
0
          tab[c].gt_entry.gt_g_value = val;
15365
0
          tab[c].gt_entry.gt_bytes = add;
15366
15367
          /* Merge in the size for the next smallest -G
15368
       value, since that will be implied by this new
15369
       value.  */
15370
0
          max = 0;
15371
0
          for (look = 1; look < c; look++)
15372
0
      {
15373
0
        if (tab[look].gt_entry.gt_g_value < val
15374
0
            && (max == 0
15375
0
          || (tab[look].gt_entry.gt_g_value
15376
0
              > tab[max].gt_entry.gt_g_value)))
15377
0
          max = look;
15378
0
      }
15379
0
          if (max != 0)
15380
0
      tab[c].gt_entry.gt_bytes +=
15381
0
        tab[max].gt_entry.gt_bytes;
15382
15383
0
          ++c;
15384
0
        }
15385
15386
0
      last = int_gptab.gt_entry.gt_bytes;
15387
0
    }
15388
15389
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
15390
     elf_link_input_bfd ignores this section.  */
15391
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
15392
0
      }
15393
15394
    /* The table must be sorted by -G value.  */
15395
0
    if (c > 2)
15396
0
      qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
15397
15398
    /* Swap out the table.  */
15399
0
    amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
15400
0
    ext_tab = bfd_alloc (abfd, amt);
15401
0
    if (ext_tab == NULL)
15402
0
      {
15403
0
        free (tab);
15404
0
        return false;
15405
0
      }
15406
15407
0
    for (j = 0; j < c; j++)
15408
0
      bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
15409
0
    free (tab);
15410
15411
0
    o->size = c * sizeof (Elf32_External_gptab);
15412
0
    o->contents = (bfd_byte *) ext_tab;
15413
15414
    /* Skip this section later on (I don't think this currently
15415
       matters, but someday it might).  */
15416
0
    o->map_head.link_order = NULL;
15417
0
  }
15418
0
    }
15419
15420
  /* Invoke the regular ELF backend linker to do all the work.  */
15421
0
  if (!bfd_elf_final_link (abfd, info))
15422
0
    return false;
15423
15424
  /* Now write out the computed sections.  */
15425
15426
0
  if (abiflags_sec != NULL)
15427
0
    {
15428
0
      Elf_External_ABIFlags_v0 ext;
15429
0
      Elf_Internal_ABIFlags_v0 *abiflags;
15430
15431
0
      abiflags = &mips_elf_tdata (abfd)->abiflags;
15432
15433
      /* Set up the abiflags if no valid input sections were found.  */
15434
0
      if (!mips_elf_tdata (abfd)->abiflags_valid)
15435
0
  {
15436
0
    infer_mips_abiflags (abfd, abiflags);
15437
0
    mips_elf_tdata (abfd)->abiflags_valid = true;
15438
0
  }
15439
0
      bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
15440
0
      if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
15441
0
  return false;
15442
0
    }
15443
15444
0
  if (reginfo_sec != NULL)
15445
0
    {
15446
0
      Elf32_External_RegInfo ext;
15447
15448
0
      bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
15449
0
      if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
15450
0
  return false;
15451
0
    }
15452
15453
0
  if (mdebug_sec != NULL)
15454
0
    {
15455
0
      BFD_ASSERT (abfd->output_has_begun);
15456
0
      if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
15457
0
                 swap, info,
15458
0
                 mdebug_sec->filepos))
15459
0
  return false;
15460
15461
0
      bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
15462
0
    }
15463
15464
0
  if (gptab_data_sec != NULL)
15465
0
    {
15466
0
      if (! bfd_set_section_contents (abfd, gptab_data_sec,
15467
0
              gptab_data_sec->contents,
15468
0
              0, gptab_data_sec->size))
15469
0
  return false;
15470
0
    }
15471
15472
0
  if (gptab_bss_sec != NULL)
15473
0
    {
15474
0
      if (! bfd_set_section_contents (abfd, gptab_bss_sec,
15475
0
              gptab_bss_sec->contents,
15476
0
              0, gptab_bss_sec->size))
15477
0
  return false;
15478
0
    }
15479
15480
0
  if (SGI_COMPAT (abfd))
15481
0
    {
15482
0
      rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
15483
0
      if (rtproc_sec != NULL)
15484
0
  {
15485
0
    if (! bfd_set_section_contents (abfd, rtproc_sec,
15486
0
            rtproc_sec->contents,
15487
0
            0, rtproc_sec->size))
15488
0
      return false;
15489
0
  }
15490
0
    }
15491
15492
0
  return true;
15493
0
}
15494

15495
/* Merge object file header flags from IBFD into OBFD.  Raise an error
15496
   if there are conflicting settings.  */
15497
15498
static bool
15499
mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
15500
0
{
15501
0
  bfd *obfd = info->output_bfd;
15502
0
  struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15503
0
  flagword old_flags;
15504
0
  flagword new_flags;
15505
0
  bool ok;
15506
15507
0
  new_flags = elf_elfheader (ibfd)->e_flags;
15508
0
  elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15509
0
  old_flags = elf_elfheader (obfd)->e_flags;
15510
15511
  /* Check flag compatibility.  */
15512
15513
0
  new_flags &= ~EF_MIPS_NOREORDER;
15514
0
  old_flags &= ~EF_MIPS_NOREORDER;
15515
15516
  /* Some IRIX 6 BSD-compatibility objects have this bit set.  It
15517
     doesn't seem to matter.  */
15518
0
  new_flags &= ~EF_MIPS_XGOT;
15519
0
  old_flags &= ~EF_MIPS_XGOT;
15520
15521
  /* MIPSpro generates ucode info in n64 objects.  Again, we should
15522
     just be able to ignore this.  */
15523
0
  new_flags &= ~EF_MIPS_UCODE;
15524
0
  old_flags &= ~EF_MIPS_UCODE;
15525
15526
  /* DSOs should only be linked with CPIC code.  */
15527
0
  if ((ibfd->flags & DYNAMIC) != 0)
15528
0
    new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15529
15530
0
  if (new_flags == old_flags)
15531
0
    return true;
15532
15533
0
  ok = true;
15534
15535
0
  if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15536
0
      != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15537
0
    {
15538
0
      _bfd_error_handler
15539
0
  (_("%pB: warning: linking abicalls files with non-abicalls files"),
15540
0
   ibfd);
15541
0
      ok = true;
15542
0
    }
15543
15544
0
  if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15545
0
    elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15546
0
  if (! (new_flags & EF_MIPS_PIC))
15547
0
    elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15548
15549
0
  new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15550
0
  old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15551
15552
  /* Compare the ISAs.  */
15553
0
  if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15554
0
    {
15555
0
      _bfd_error_handler
15556
0
  (_("%pB: linking 32-bit code with 64-bit code"),
15557
0
   ibfd);
15558
0
      ok = false;
15559
0
    }
15560
0
  else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15561
0
    {
15562
      /* OBFD's ISA isn't the same as, or an extension of, IBFD's.  */
15563
0
      if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15564
0
  {
15565
    /* Copy the architecture info from IBFD to OBFD.  Also copy
15566
       the 32-bit flag (if set) so that we continue to recognise
15567
       OBFD as a 32-bit binary.  */
15568
0
    bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15569
0
    elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15570
0
    elf_elfheader (obfd)->e_flags
15571
0
      |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15572
15573
    /* Update the ABI flags isa_level, isa_rev, isa_ext fields.  */
15574
0
    update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15575
15576
    /* Copy across the ABI flags if OBFD doesn't use them
15577
       and if that was what caused us to treat IBFD as 32-bit.  */
15578
0
    if ((old_flags & EF_MIPS_ABI) == 0
15579
0
        && mips_32bit_flags_p (new_flags)
15580
0
        && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15581
0
      elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15582
0
  }
15583
0
      else
15584
0
  {
15585
    /* The ISAs aren't compatible.  */
15586
0
    _bfd_error_handler
15587
      /* xgettext:c-format */
15588
0
      (_("%pB: linking %s module with previous %s modules"),
15589
0
       ibfd,
15590
0
       bfd_printable_name (ibfd),
15591
0
       bfd_printable_name (obfd));
15592
0
    ok = false;
15593
0
  }
15594
0
    }
15595
15596
0
  new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15597
0
  old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15598
15599
  /* Compare ABIs.  The 64-bit ABI does not use EF_MIPS_ABI.  But, it
15600
     does set EI_CLASS differently from any 32-bit ABI.  */
15601
0
  if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15602
0
      || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15603
0
    != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15604
0
    {
15605
      /* Only error if both are set (to different values).  */
15606
0
      if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15607
0
    || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15608
0
        != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15609
0
  {
15610
0
    _bfd_error_handler
15611
      /* xgettext:c-format */
15612
0
      (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
15613
0
       ibfd,
15614
0
       elf_mips_abi_name (ibfd),
15615
0
       elf_mips_abi_name (obfd));
15616
0
    ok = false;
15617
0
  }
15618
0
      new_flags &= ~EF_MIPS_ABI;
15619
0
      old_flags &= ~EF_MIPS_ABI;
15620
0
    }
15621
15622
  /* Compare ASEs.  Forbid linking MIPS16 and microMIPS ASE modules together
15623
     and allow arbitrary mixing of the remaining ASEs (retain the union).  */
15624
0
  if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15625
0
    {
15626
0
      int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15627
0
      int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15628
0
      int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15629
0
      int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15630
0
      int micro_mis = old_m16 && new_micro;
15631
0
      int m16_mis = old_micro && new_m16;
15632
15633
0
      if (m16_mis || micro_mis)
15634
0
  {
15635
0
    _bfd_error_handler
15636
      /* xgettext:c-format */
15637
0
      (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
15638
0
       ibfd,
15639
0
       m16_mis ? "MIPS16" : "microMIPS",
15640
0
       m16_mis ? "microMIPS" : "MIPS16");
15641
0
    ok = false;
15642
0
  }
15643
15644
0
      elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15645
15646
0
      new_flags &= ~ EF_MIPS_ARCH_ASE;
15647
0
      old_flags &= ~ EF_MIPS_ARCH_ASE;
15648
0
    }
15649
15650
  /* Compare NaN encodings.  */
15651
0
  if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15652
0
    {
15653
      /* xgettext:c-format */
15654
0
      _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15655
0
        ibfd,
15656
0
        (new_flags & EF_MIPS_NAN2008
15657
0
         ? "-mnan=2008" : "-mnan=legacy"),
15658
0
        (old_flags & EF_MIPS_NAN2008
15659
0
         ? "-mnan=2008" : "-mnan=legacy"));
15660
0
      ok = false;
15661
0
      new_flags &= ~EF_MIPS_NAN2008;
15662
0
      old_flags &= ~EF_MIPS_NAN2008;
15663
0
    }
15664
15665
  /* Compare FP64 state.  */
15666
0
  if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15667
0
    {
15668
      /* xgettext:c-format */
15669
0
      _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15670
0
        ibfd,
15671
0
        (new_flags & EF_MIPS_FP64
15672
0
         ? "-mfp64" : "-mfp32"),
15673
0
        (old_flags & EF_MIPS_FP64
15674
0
         ? "-mfp64" : "-mfp32"));
15675
0
      ok = false;
15676
0
      new_flags &= ~EF_MIPS_FP64;
15677
0
      old_flags &= ~EF_MIPS_FP64;
15678
0
    }
15679
15680
  /* Warn about any other mismatches */
15681
0
  if (new_flags != old_flags)
15682
0
    {
15683
      /* xgettext:c-format */
15684
0
      _bfd_error_handler
15685
0
  (_("%pB: uses different e_flags (%#x) fields than previous modules "
15686
0
     "(%#x)"),
15687
0
   ibfd, new_flags, old_flags);
15688
0
      ok = false;
15689
0
    }
15690
15691
0
  return ok;
15692
0
}
15693
15694
/* Merge object attributes from IBFD into OBFD.  Raise an error if
15695
   there are conflicting attributes.  */
15696
static bool
15697
mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
15698
0
{
15699
0
  bfd *obfd = info->output_bfd;
15700
0
  obj_attribute *in_attr;
15701
0
  obj_attribute *out_attr;
15702
0
  bfd *abi_fp_bfd;
15703
0
  bfd *abi_msa_bfd;
15704
15705
0
  abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
15706
0
  in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15707
0
  if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
15708
0
    mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15709
15710
0
  abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
15711
0
  if (!abi_msa_bfd
15712
0
      && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15713
0
    mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
15714
15715
0
  if (!elf_known_obj_attributes_proc (obfd)[0].i)
15716
0
    {
15717
      /* This is the first object.  Copy the attributes.  */
15718
0
      _bfd_elf_copy_obj_attributes (ibfd, obfd);
15719
15720
      /* Use the Tag_null value to indicate the attributes have been
15721
   initialized.  */
15722
0
      elf_known_obj_attributes_proc (obfd)[0].i = 1;
15723
15724
0
      return true;
15725
0
    }
15726
15727
  /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
15728
     non-conflicting ones.  */
15729
0
  out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15730
0
  if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
15731
0
    {
15732
0
      int out_fp, in_fp;
15733
15734
0
      out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15735
0
      in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15736
0
      out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
15737
0
      if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
15738
0
  out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
15739
0
      else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
15740
0
         && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15741
0
       || in_fp == Val_GNU_MIPS_ABI_FP_64
15742
0
       || in_fp == Val_GNU_MIPS_ABI_FP_64A))
15743
0
  {
15744
0
    mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15745
0
    out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15746
0
  }
15747
0
      else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
15748
0
         && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15749
0
       || out_fp == Val_GNU_MIPS_ABI_FP_64
15750
0
       || out_fp == Val_GNU_MIPS_ABI_FP_64A))
15751
0
  /* Keep the current setting.  */;
15752
0
      else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
15753
0
         && in_fp == Val_GNU_MIPS_ABI_FP_64)
15754
0
  {
15755
0
    mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15756
0
    out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15757
0
  }
15758
0
      else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
15759
0
         && out_fp == Val_GNU_MIPS_ABI_FP_64)
15760
0
  /* Keep the current setting.  */;
15761
0
      else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
15762
0
  {
15763
0
    const char *out_string, *in_string;
15764
15765
0
    out_string = _bfd_mips_fp_abi_string (out_fp);
15766
0
    in_string = _bfd_mips_fp_abi_string (in_fp);
15767
    /* First warn about cases involving unrecognised ABIs.  */
15768
0
    if (!out_string && !in_string)
15769
      /* xgettext:c-format */
15770
0
      _bfd_error_handler
15771
0
        (_("warning: %pB uses unknown floating point ABI %d "
15772
0
     "(set by %pB), %pB uses unknown floating point ABI %d"),
15773
0
         obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
15774
0
    else if (!out_string)
15775
0
      _bfd_error_handler
15776
        /* xgettext:c-format */
15777
0
        (_("warning: %pB uses unknown floating point ABI %d "
15778
0
     "(set by %pB), %pB uses %s"),
15779
0
         obfd, out_fp, abi_fp_bfd, ibfd, in_string);
15780
0
    else if (!in_string)
15781
0
      _bfd_error_handler
15782
        /* xgettext:c-format */
15783
0
        (_("warning: %pB uses %s (set by %pB), "
15784
0
     "%pB uses unknown floating point ABI %d"),
15785
0
         obfd, out_string, abi_fp_bfd, ibfd, in_fp);
15786
0
    else
15787
0
      {
15788
        /* If one of the bfds is soft-float, the other must be
15789
     hard-float.  The exact choice of hard-float ABI isn't
15790
     really relevant to the error message.  */
15791
0
        if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15792
0
    out_string = "-mhard-float";
15793
0
        else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15794
0
    in_string = "-mhard-float";
15795
0
        _bfd_error_handler
15796
    /* xgettext:c-format */
15797
0
    (_("warning: %pB uses %s (set by %pB), %pB uses %s"),
15798
0
     obfd, out_string, abi_fp_bfd, ibfd, in_string);
15799
0
      }
15800
0
  }
15801
0
    }
15802
15803
  /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
15804
     non-conflicting ones.  */
15805
0
  if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15806
0
    {
15807
0
      out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
15808
0
      if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
15809
0
  out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
15810
0
      else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15811
0
  switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15812
0
    {
15813
0
    case Val_GNU_MIPS_ABI_MSA_128:
15814
0
      _bfd_error_handler
15815
        /* xgettext:c-format */
15816
0
        (_("warning: %pB uses %s (set by %pB), "
15817
0
     "%pB uses unknown MSA ABI %d"),
15818
0
         obfd, "-mmsa", abi_msa_bfd,
15819
0
         ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15820
0
      break;
15821
15822
0
    default:
15823
0
      switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
15824
0
        {
15825
0
        case Val_GNU_MIPS_ABI_MSA_128:
15826
0
    _bfd_error_handler
15827
      /* xgettext:c-format */
15828
0
      (_("warning: %pB uses unknown MSA ABI %d "
15829
0
         "(set by %pB), %pB uses %s"),
15830
0
         obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15831
0
       abi_msa_bfd, ibfd, "-mmsa");
15832
0
      break;
15833
15834
0
        default:
15835
0
    _bfd_error_handler
15836
      /* xgettext:c-format */
15837
0
      (_("warning: %pB uses unknown MSA ABI %d "
15838
0
         "(set by %pB), %pB uses unknown MSA ABI %d"),
15839
0
       obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15840
0
       abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15841
0
    break;
15842
0
        }
15843
0
    }
15844
0
    }
15845
15846
  /* Merge Tag_compatibility attributes and any common GNU ones.  */
15847
0
  return _bfd_elf_merge_object_attributes (ibfd, info);
15848
0
}
15849
15850
/* Merge object ABI flags from IBFD into OBFD.  Raise an error if
15851
   there are conflicting settings.  */
15852
15853
static bool
15854
mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
15855
0
{
15856
0
  obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15857
0
  struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15858
0
  struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
15859
15860
  /* Update the output abiflags fp_abi using the computed fp_abi.  */
15861
0
  out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15862
15863
0
#define max(a, b) ((a) > (b) ? (a) : (b))
15864
  /* Merge abiflags.  */
15865
0
  out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
15866
0
               in_tdata->abiflags.isa_level);
15867
0
  out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
15868
0
             in_tdata->abiflags.isa_rev);
15869
0
  out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
15870
0
              in_tdata->abiflags.gpr_size);
15871
0
  out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
15872
0
               in_tdata->abiflags.cpr1_size);
15873
0
  out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
15874
0
               in_tdata->abiflags.cpr2_size);
15875
0
#undef max
15876
0
  out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
15877
0
  out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
15878
15879
0
  return true;
15880
0
}
15881
15882
/* Merge backend specific data from an object file to the output
15883
   object file when linking.  */
15884
15885
bool
15886
_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
15887
0
{
15888
0
  bfd *obfd = info->output_bfd;
15889
0
  struct mips_elf_obj_tdata *out_tdata;
15890
0
  struct mips_elf_obj_tdata *in_tdata;
15891
0
  bool null_input_bfd = true;
15892
0
  asection *sec;
15893
0
  bool ok;
15894
15895
  /* Check if we have the same endianness.  */
15896
0
  if (! _bfd_generic_verify_endian_match (ibfd, info))
15897
0
    {
15898
0
      _bfd_error_handler
15899
0
  (_("%pB: endianness incompatible with that of the selected emulation"),
15900
0
   ibfd);
15901
0
      return false;
15902
0
    }
15903
15904
0
  if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
15905
0
    return true;
15906
15907
0
  in_tdata = mips_elf_tdata (ibfd);
15908
0
  out_tdata = mips_elf_tdata (obfd);
15909
15910
0
  if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
15911
0
    {
15912
0
      _bfd_error_handler
15913
0
  (_("%pB: ABI is incompatible with that of the selected emulation"),
15914
0
   ibfd);
15915
0
      return false;
15916
0
    }
15917
15918
  /* Check to see if the input BFD actually contains any sections.  If not,
15919
     then it has no attributes, and its flags may not have been initialized
15920
     either, but it cannot actually cause any incompatibility.  */
15921
  /* FIXME: This excludes any input shared library from consideration.  */
15922
0
  for (sec = ibfd->sections; sec != NULL; sec = sec->next)
15923
0
    {
15924
      /* Ignore synthetic sections and empty .text, .data and .bss sections
15925
   which are automatically generated by gas.  Also ignore fake
15926
   (s)common sections, since merely defining a common symbol does
15927
   not affect compatibility.  */
15928
0
      if ((sec->flags & SEC_IS_COMMON) == 0
15929
0
    && strcmp (sec->name, ".reginfo")
15930
0
    && strcmp (sec->name, ".mdebug")
15931
0
    && (sec->size != 0
15932
0
        || (strcmp (sec->name, ".text")
15933
0
      && strcmp (sec->name, ".data")
15934
0
      && strcmp (sec->name, ".bss"))))
15935
0
  {
15936
0
    null_input_bfd = false;
15937
0
    break;
15938
0
  }
15939
0
    }
15940
0
  if (null_input_bfd)
15941
0
    return true;
15942
15943
  /* Populate abiflags using existing information.  */
15944
0
  if (in_tdata->abiflags_valid)
15945
0
    {
15946
0
      obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15947
0
      Elf_Internal_ABIFlags_v0 in_abiflags;
15948
0
      Elf_Internal_ABIFlags_v0 abiflags;
15949
15950
      /* Set up the FP ABI attribute from the abiflags if it is not already
15951
   set.  */
15952
0
      if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
15953
0
  in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
15954
15955
0
      infer_mips_abiflags (ibfd, &abiflags);
15956
0
      in_abiflags = in_tdata->abiflags;
15957
15958
      /* It is not possible to infer the correct ISA revision
15959
   for R3 or R5 so drop down to R2 for the checks.  */
15960
0
      if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
15961
0
  in_abiflags.isa_rev = 2;
15962
15963
0
      if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
15964
0
    < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
15965
0
  _bfd_error_handler
15966
0
    (_("%pB: warning: inconsistent ISA between e_flags and "
15967
0
       ".MIPS.abiflags"), ibfd);
15968
0
      if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
15969
0
    && in_abiflags.fp_abi != abiflags.fp_abi)
15970
0
  _bfd_error_handler
15971
0
    (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
15972
0
       ".MIPS.abiflags"), ibfd);
15973
0
      if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
15974
0
  _bfd_error_handler
15975
0
    (_("%pB: warning: inconsistent ASEs between e_flags and "
15976
0
       ".MIPS.abiflags"), ibfd);
15977
      /* The isa_ext is allowed to be an extension of what can be inferred
15978
   from e_flags.  */
15979
0
      if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
15980
0
        bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
15981
0
  _bfd_error_handler
15982
0
    (_("%pB: warning: inconsistent ISA extensions between e_flags and "
15983
0
       ".MIPS.abiflags"), ibfd);
15984
0
      if (in_abiflags.flags2 != 0)
15985
0
  _bfd_error_handler
15986
0
    (_("%pB: warning: unexpected flag in the flags2 field of "
15987
0
       ".MIPS.abiflags (0x%lx)"), ibfd,
15988
0
     in_abiflags.flags2);
15989
0
    }
15990
0
  else
15991
0
    {
15992
0
      infer_mips_abiflags (ibfd, &in_tdata->abiflags);
15993
0
      in_tdata->abiflags_valid = true;
15994
0
    }
15995
15996
0
  if (!out_tdata->abiflags_valid)
15997
0
    {
15998
      /* Copy input abiflags if output abiflags are not already valid.  */
15999
0
      out_tdata->abiflags = in_tdata->abiflags;
16000
0
      out_tdata->abiflags_valid = true;
16001
0
    }
16002
16003
0
  if (! elf_flags_init (obfd))
16004
0
    {
16005
0
      elf_flags_init (obfd) = true;
16006
0
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
16007
0
      elf_elfheader (obfd)->e_ident[EI_CLASS]
16008
0
  = elf_elfheader (ibfd)->e_ident[EI_CLASS];
16009
16010
0
      if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
16011
0
    && (bfd_get_arch_info (obfd)->the_default
16012
0
        || mips_mach_extends_p (bfd_get_mach (obfd),
16013
0
              bfd_get_mach (ibfd))))
16014
0
  {
16015
0
    if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
16016
0
           bfd_get_mach (ibfd)))
16017
0
      return false;
16018
16019
    /* Update the ABI flags isa_level, isa_rev and isa_ext fields.  */
16020
0
    update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
16021
0
  }
16022
16023
0
      ok = true;
16024
0
    }
16025
0
  else
16026
0
    ok = mips_elf_merge_obj_e_flags (ibfd, info);
16027
16028
0
  ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
16029
16030
0
  ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
16031
16032
0
  if (!ok)
16033
0
    {
16034
0
      bfd_set_error (bfd_error_bad_value);
16035
0
      return false;
16036
0
    }
16037
16038
0
  return true;
16039
0
}
16040
16041
/* Function to keep MIPS specific file flags like as EF_MIPS_PIC.  */
16042
16043
bool
16044
_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
16045
0
{
16046
0
  BFD_ASSERT (!elf_flags_init (abfd)
16047
0
        || elf_elfheader (abfd)->e_flags == flags);
16048
16049
0
  elf_elfheader (abfd)->e_flags = flags;
16050
0
  elf_flags_init (abfd) = true;
16051
0
  return true;
16052
0
}
16053
16054
char *
16055
_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
16056
66
{
16057
66
  switch (dtag)
16058
66
    {
16059
50
    default: return "";
16060
2
    case DT_MIPS_RLD_VERSION:
16061
2
      return "MIPS_RLD_VERSION";
16062
0
    case DT_MIPS_TIME_STAMP:
16063
0
      return "MIPS_TIME_STAMP";
16064
0
    case DT_MIPS_ICHECKSUM:
16065
0
      return "MIPS_ICHECKSUM";
16066
0
    case DT_MIPS_IVERSION:
16067
0
      return "MIPS_IVERSION";
16068
2
    case DT_MIPS_FLAGS:
16069
2
      return "MIPS_FLAGS";
16070
2
    case DT_MIPS_BASE_ADDRESS:
16071
2
      return "MIPS_BASE_ADDRESS";
16072
0
    case DT_MIPS_MSYM:
16073
0
      return "MIPS_MSYM";
16074
0
    case DT_MIPS_CONFLICT:
16075
0
      return "MIPS_CONFLICT";
16076
0
    case DT_MIPS_LIBLIST:
16077
0
      return "MIPS_LIBLIST";
16078
2
    case DT_MIPS_LOCAL_GOTNO:
16079
2
      return "MIPS_LOCAL_GOTNO";
16080
0
    case DT_MIPS_CONFLICTNO:
16081
0
      return "MIPS_CONFLICTNO";
16082
0
    case DT_MIPS_LIBLISTNO:
16083
0
      return "MIPS_LIBLISTNO";
16084
2
    case DT_MIPS_SYMTABNO:
16085
2
      return "MIPS_SYMTABNO";
16086
2
    case DT_MIPS_UNREFEXTNO:
16087
2
      return "MIPS_UNREFEXTNO";
16088
2
    case DT_MIPS_GOTSYM:
16089
2
      return "MIPS_GOTSYM";
16090
0
    case DT_MIPS_HIPAGENO:
16091
0
      return "MIPS_HIPAGENO";
16092
2
    case DT_MIPS_RLD_MAP:
16093
2
      return "MIPS_RLD_MAP";
16094
0
    case DT_MIPS_RLD_MAP_REL:
16095
0
      return "MIPS_RLD_MAP_REL";
16096
0
    case DT_MIPS_DELTA_CLASS:
16097
0
      return "MIPS_DELTA_CLASS";
16098
0
    case DT_MIPS_DELTA_CLASS_NO:
16099
0
      return "MIPS_DELTA_CLASS_NO";
16100
0
    case DT_MIPS_DELTA_INSTANCE:
16101
0
      return "MIPS_DELTA_INSTANCE";
16102
0
    case DT_MIPS_DELTA_INSTANCE_NO:
16103
0
      return "MIPS_DELTA_INSTANCE_NO";
16104
0
    case DT_MIPS_DELTA_RELOC:
16105
0
      return "MIPS_DELTA_RELOC";
16106
0
    case DT_MIPS_DELTA_RELOC_NO:
16107
0
      return "MIPS_DELTA_RELOC_NO";
16108
0
    case DT_MIPS_DELTA_SYM:
16109
0
      return "MIPS_DELTA_SYM";
16110
0
    case DT_MIPS_DELTA_SYM_NO:
16111
0
      return "MIPS_DELTA_SYM_NO";
16112
0
    case DT_MIPS_DELTA_CLASSSYM:
16113
0
      return "MIPS_DELTA_CLASSSYM";
16114
0
    case DT_MIPS_DELTA_CLASSSYM_NO:
16115
0
      return "MIPS_DELTA_CLASSSYM_NO";
16116
0
    case DT_MIPS_CXX_FLAGS:
16117
0
      return "MIPS_CXX_FLAGS";
16118
0
    case DT_MIPS_PIXIE_INIT:
16119
0
      return "MIPS_PIXIE_INIT";
16120
0
    case DT_MIPS_SYMBOL_LIB:
16121
0
      return "MIPS_SYMBOL_LIB";
16122
0
    case DT_MIPS_LOCALPAGE_GOTIDX:
16123
0
      return "MIPS_LOCALPAGE_GOTIDX";
16124
0
    case DT_MIPS_LOCAL_GOTIDX:
16125
0
      return "MIPS_LOCAL_GOTIDX";
16126
0
    case DT_MIPS_HIDDEN_GOTIDX:
16127
0
      return "MIPS_HIDDEN_GOTIDX";
16128
0
    case DT_MIPS_PROTECTED_GOTIDX:
16129
0
      return "MIPS_PROTECTED_GOT_IDX";
16130
0
    case DT_MIPS_OPTIONS:
16131
0
      return "MIPS_OPTIONS";
16132
0
    case DT_MIPS_INTERFACE:
16133
0
      return "MIPS_INTERFACE";
16134
0
    case DT_MIPS_DYNSTR_ALIGN:
16135
0
      return "DT_MIPS_DYNSTR_ALIGN";
16136
0
    case DT_MIPS_INTERFACE_SIZE:
16137
0
      return "DT_MIPS_INTERFACE_SIZE";
16138
0
    case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
16139
0
      return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
16140
0
    case DT_MIPS_PERF_SUFFIX:
16141
0
      return "DT_MIPS_PERF_SUFFIX";
16142
0
    case DT_MIPS_COMPACT_SIZE:
16143
0
      return "DT_MIPS_COMPACT_SIZE";
16144
0
    case DT_MIPS_GP_VALUE:
16145
0
      return "DT_MIPS_GP_VALUE";
16146
0
    case DT_MIPS_AUX_DYNAMIC:
16147
0
      return "DT_MIPS_AUX_DYNAMIC";
16148
0
    case DT_MIPS_PLTGOT:
16149
0
      return "DT_MIPS_PLTGOT";
16150
0
    case DT_MIPS_RWPLT:
16151
0
      return "DT_MIPS_RWPLT";
16152
0
    case DT_MIPS_XHASH:
16153
0
      return "DT_MIPS_XHASH";
16154
66
    }
16155
66
}
16156
16157
/* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
16158
   not known.  */
16159
16160
const char *
16161
_bfd_mips_fp_abi_string (int fp)
16162
0
{
16163
0
  switch (fp)
16164
0
    {
16165
      /* These strings aren't translated because they're simply
16166
   option lists.  */
16167
0
    case Val_GNU_MIPS_ABI_FP_DOUBLE:
16168
0
      return "-mdouble-float";
16169
16170
0
    case Val_GNU_MIPS_ABI_FP_SINGLE:
16171
0
      return "-msingle-float";
16172
16173
0
    case Val_GNU_MIPS_ABI_FP_SOFT:
16174
0
      return "-msoft-float";
16175
16176
0
    case Val_GNU_MIPS_ABI_FP_OLD_64:
16177
0
      return _("-mips32r2 -mfp64 (12 callee-saved)");
16178
16179
0
    case Val_GNU_MIPS_ABI_FP_XX:
16180
0
      return "-mfpxx";
16181
16182
0
    case Val_GNU_MIPS_ABI_FP_64:
16183
0
      return "-mgp32 -mfp64";
16184
16185
0
    case Val_GNU_MIPS_ABI_FP_64A:
16186
0
      return "-mgp32 -mfp64 -mno-odd-spreg";
16187
16188
0
    default:
16189
0
      return 0;
16190
0
    }
16191
0
}
16192
16193
static void
16194
print_mips_ases (FILE *file, unsigned int mask)
16195
0
{
16196
0
  if (mask & AFL_ASE_DSP)
16197
0
    fputs ("\n\tDSP ASE", file);
16198
0
  if (mask & AFL_ASE_DSPR2)
16199
0
    fputs ("\n\tDSP R2 ASE", file);
16200
0
  if (mask & AFL_ASE_DSPR3)
16201
0
    fputs ("\n\tDSP R3 ASE", file);
16202
0
  if (mask & AFL_ASE_EVA)
16203
0
    fputs ("\n\tEnhanced VA Scheme", file);
16204
0
  if (mask & AFL_ASE_MCU)
16205
0
    fputs ("\n\tMCU (MicroController) ASE", file);
16206
0
  if (mask & AFL_ASE_MDMX)
16207
0
    fputs ("\n\tMDMX ASE", file);
16208
0
  if (mask & AFL_ASE_MIPS3D)
16209
0
    fputs ("\n\tMIPS-3D ASE", file);
16210
0
  if (mask & AFL_ASE_MT)
16211
0
    fputs ("\n\tMT ASE", file);
16212
0
  if (mask & AFL_ASE_SMARTMIPS)
16213
0
    fputs ("\n\tSmartMIPS ASE", file);
16214
0
  if (mask & AFL_ASE_VIRT)
16215
0
    fputs ("\n\tVZ ASE", file);
16216
0
  if (mask & AFL_ASE_MSA)
16217
0
    fputs ("\n\tMSA ASE", file);
16218
0
  if (mask & AFL_ASE_MIPS16)
16219
0
    fputs ("\n\tMIPS16 ASE", file);
16220
0
  if (mask & AFL_ASE_MICROMIPS)
16221
0
    fputs ("\n\tMICROMIPS ASE", file);
16222
0
  if (mask & AFL_ASE_XPA)
16223
0
    fputs ("\n\tXPA ASE", file);
16224
0
  if (mask & AFL_ASE_MIPS16E2)
16225
0
    fputs ("\n\tMIPS16e2 ASE", file);
16226
0
  if (mask & AFL_ASE_CRC)
16227
0
    fputs ("\n\tCRC ASE", file);
16228
0
  if (mask & AFL_ASE_GINV)
16229
0
    fputs ("\n\tGINV ASE", file);
16230
0
  if (mask & AFL_ASE_LOONGSON_MMI)
16231
0
    fputs ("\n\tLoongson MMI ASE", file);
16232
0
  if (mask & AFL_ASE_LOONGSON_CAM)
16233
0
    fputs ("\n\tLoongson CAM ASE", file);
16234
0
  if (mask & AFL_ASE_LOONGSON_EXT)
16235
0
    fputs ("\n\tLoongson EXT ASE", file);
16236
0
  if (mask & AFL_ASE_LOONGSON_EXT2)
16237
0
    fputs ("\n\tLoongson EXT2 ASE", file);
16238
0
  if (mask == 0)
16239
0
    fprintf (file, "\n\t%s", _("None"));
16240
0
  else if ((mask & ~AFL_ASE_MASK) != 0)
16241
0
    fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
16242
0
}
16243
16244
static void
16245
print_mips_isa_ext (FILE *file, unsigned int isa_ext)
16246
0
{
16247
0
  switch (isa_ext)
16248
0
    {
16249
0
    case 0:
16250
0
      fputs (_("None"), file);
16251
0
      break;
16252
0
    case AFL_EXT_XLR:
16253
0
      fputs ("RMI XLR", file);
16254
0
      break;
16255
0
    case AFL_EXT_OCTEON3:
16256
0
      fputs ("Cavium Networks Octeon3", file);
16257
0
      break;
16258
0
    case AFL_EXT_OCTEON2:
16259
0
      fputs ("Cavium Networks Octeon2", file);
16260
0
      break;
16261
0
    case AFL_EXT_OCTEONP:
16262
0
      fputs ("Cavium Networks OcteonP", file);
16263
0
      break;
16264
0
    case AFL_EXT_OCTEON:
16265
0
      fputs ("Cavium Networks Octeon", file);
16266
0
      break;
16267
0
    case AFL_EXT_5900:
16268
0
      fputs ("Toshiba R5900", file);
16269
0
      break;
16270
0
    case AFL_EXT_4650:
16271
0
      fputs ("MIPS R4650", file);
16272
0
      break;
16273
0
    case AFL_EXT_4010:
16274
0
      fputs ("LSI R4010", file);
16275
0
      break;
16276
0
    case AFL_EXT_4100:
16277
0
      fputs ("NEC VR4100", file);
16278
0
      break;
16279
0
    case AFL_EXT_3900:
16280
0
      fputs ("Toshiba R3900", file);
16281
0
      break;
16282
0
    case AFL_EXT_10000:
16283
0
      fputs ("MIPS R10000", file);
16284
0
      break;
16285
0
    case AFL_EXT_SB1:
16286
0
      fputs ("Broadcom SB-1", file);
16287
0
      break;
16288
0
    case AFL_EXT_4111:
16289
0
      fputs ("NEC VR4111/VR4181", file);
16290
0
      break;
16291
0
    case AFL_EXT_4120:
16292
0
      fputs ("NEC VR4120", file);
16293
0
      break;
16294
0
    case AFL_EXT_5400:
16295
0
      fputs ("NEC VR5400", file);
16296
0
      break;
16297
0
    case AFL_EXT_5500:
16298
0
      fputs ("NEC VR5500", file);
16299
0
      break;
16300
0
    case AFL_EXT_LOONGSON_2E:
16301
0
      fputs ("ST Microelectronics Loongson 2E", file);
16302
0
      break;
16303
0
    case AFL_EXT_LOONGSON_2F:
16304
0
      fputs ("ST Microelectronics Loongson 2F", file);
16305
0
      break;
16306
0
    case AFL_EXT_INTERAPTIV_MR2:
16307
0
      fputs ("Imagination interAptiv MR2", file);
16308
0
      break;
16309
0
    default:
16310
0
      fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
16311
0
      break;
16312
0
    }
16313
0
}
16314
16315
static void
16316
print_mips_fp_abi_value (FILE *file, int val)
16317
0
{
16318
0
  switch (val)
16319
0
    {
16320
0
    case Val_GNU_MIPS_ABI_FP_ANY:
16321
0
      fprintf (file, _("Hard or soft float\n"));
16322
0
      break;
16323
0
    case Val_GNU_MIPS_ABI_FP_DOUBLE:
16324
0
      fprintf (file, _("Hard float (double precision)\n"));
16325
0
      break;
16326
0
    case Val_GNU_MIPS_ABI_FP_SINGLE:
16327
0
      fprintf (file, _("Hard float (single precision)\n"));
16328
0
      break;
16329
0
    case Val_GNU_MIPS_ABI_FP_SOFT:
16330
0
      fprintf (file, _("Soft float\n"));
16331
0
      break;
16332
0
    case Val_GNU_MIPS_ABI_FP_OLD_64:
16333
0
      fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16334
0
      break;
16335
0
    case Val_GNU_MIPS_ABI_FP_XX:
16336
0
      fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
16337
0
      break;
16338
0
    case Val_GNU_MIPS_ABI_FP_64:
16339
0
      fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
16340
0
      break;
16341
0
    case Val_GNU_MIPS_ABI_FP_64A:
16342
0
      fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16343
0
      break;
16344
0
    default:
16345
0
      fprintf (file, "??? (%d)\n", val);
16346
0
      break;
16347
0
    }
16348
0
}
16349
16350
static int
16351
get_mips_reg_size (int reg_size)
16352
0
{
16353
0
  return (reg_size == AFL_REG_NONE) ? 0
16354
0
   : (reg_size == AFL_REG_32) ? 32
16355
0
   : (reg_size == AFL_REG_64) ? 64
16356
0
   : (reg_size == AFL_REG_128) ? 128
16357
0
   : -1;
16358
0
}
16359
16360
bool
16361
_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
16362
466
{
16363
466
  FILE *file = ptr;
16364
16365
466
  BFD_ASSERT (abfd != NULL && ptr != NULL);
16366
16367
  /* Print normal ELF private data.  */
16368
466
  _bfd_elf_print_private_bfd_data (abfd, ptr);
16369
16370
  /* xgettext:c-format */
16371
466
  fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
16372
16373
466
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
16374
4
    fprintf (file, _(" [abi=O32]"));
16375
462
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
16376
8
    fprintf (file, _(" [abi=O64]"));
16377
454
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
16378
18
    fprintf (file, _(" [abi=EABI32]"));
16379
436
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
16380
18
    fprintf (file, _(" [abi=EABI64]"));
16381
418
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
16382
213
    fprintf (file, _(" [abi unknown]"));
16383
205
  else if (ABI_N32_P (abfd))
16384
17
    fprintf (file, _(" [abi=N32]"));
16385
188
  else if (ABI_64_P (abfd))
16386
146
    fprintf (file, _(" [abi=64]"));
16387
42
  else
16388
42
    fprintf (file, _(" [no abi set]"));
16389
16390
466
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
16391
175
    fprintf (file, " [mips1]");
16392
291
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
16393
58
    fprintf (file, " [mips2]");
16394
233
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
16395
8
    fprintf (file, " [mips3]");
16396
225
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
16397
5
    fprintf (file, " [mips4]");
16398
220
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
16399
11
    fprintf (file, " [mips5]");
16400
209
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
16401
42
    fprintf (file, " [mips32]");
16402
167
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
16403
33
    fprintf (file, " [mips64]");
16404
134
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
16405
19
    fprintf (file, " [mips32r2]");
16406
115
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
16407
9
    fprintf (file, " [mips64r2]");
16408
106
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6)
16409
77
    fprintf (file, " [mips32r6]");
16410
29
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
16411
13
    fprintf (file, " [mips64r6]");
16412
16
  else
16413
16
    fprintf (file, _(" [unknown ISA]"));
16414
16415
466
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
16416
166
    fprintf (file, " [mdmx]");
16417
16418
466
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
16419
139
    fprintf (file, " [mips16]");
16420
16421
466
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
16422
158
    fprintf (file, " [micromips]");
16423
16424
466
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
16425
236
    fprintf (file, " [nan2008]");
16426
16427
466
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
16428
223
    fprintf (file, " [old fp64]");
16429
16430
466
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
16431
141
    fprintf (file, " [32bitmode]");
16432
325
  else
16433
325
    fprintf (file, _(" [not 32bitmode]"));
16434
16435
466
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
16436
183
    fprintf (file, " [noreorder]");
16437
16438
466
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
16439
45
    fprintf (file, " [PIC]");
16440
16441
466
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
16442
182
    fprintf (file, " [CPIC]");
16443
16444
466
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
16445
49
    fprintf (file, " [XGOT]");
16446
16447
466
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
16448
177
    fprintf (file, " [UCODE]");
16449
16450
466
  fputc ('\n', file);
16451
16452
466
  if (mips_elf_tdata (abfd)->abiflags_valid)
16453
0
    {
16454
0
      Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
16455
0
      fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
16456
0
      fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
16457
0
      if (abiflags->isa_rev > 1)
16458
0
  fprintf (file, "r%d", abiflags->isa_rev);
16459
0
      fprintf (file, "\nGPR size: %d",
16460
0
         get_mips_reg_size (abiflags->gpr_size));
16461
0
      fprintf (file, "\nCPR1 size: %d",
16462
0
         get_mips_reg_size (abiflags->cpr1_size));
16463
0
      fprintf (file, "\nCPR2 size: %d",
16464
0
         get_mips_reg_size (abiflags->cpr2_size));
16465
0
      fputs ("\nFP ABI: ", file);
16466
0
      print_mips_fp_abi_value (file, abiflags->fp_abi);
16467
0
      fputs ("ISA Extension: ", file);
16468
0
      print_mips_isa_ext (file, abiflags->isa_ext);
16469
0
      fputs ("\nASEs:", file);
16470
0
      print_mips_ases (file, abiflags->ases);
16471
0
      fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
16472
0
      fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
16473
0
      fputc ('\n', file);
16474
0
    }
16475
16476
466
  return true;
16477
466
}
16478
16479
const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
16480
{
16481
  { STRING_COMMA_LEN (".lit4"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16482
  { STRING_COMMA_LEN (".lit8"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16483
  { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
16484
  { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,     SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16485
  { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16486
  { STRING_COMMA_LEN (".ucode"),  0, SHT_MIPS_UCODE, 0 },
16487
  { STRING_COMMA_LEN (".MIPS.xhash"),  0, SHT_MIPS_XHASH,   SHF_ALLOC },
16488
  { NULL,         0,  0, 0,        0 }
16489
};
16490
16491
/* Merge non visibility st_other attributes.  Ensure that the
16492
   STO_OPTIONAL flag is copied into h->other, even if this is not a
16493
   definiton of the symbol.  */
16494
void
16495
_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
16496
              unsigned int st_other,
16497
              bool definition,
16498
              bool dynamic ATTRIBUTE_UNUSED)
16499
0
{
16500
0
  if ((st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
16501
0
    {
16502
0
      unsigned char other;
16503
16504
0
      other = (definition ? st_other : h->other);
16505
0
      other &= ~ELF_ST_VISIBILITY (-1);
16506
0
      h->other = other | ELF_ST_VISIBILITY (h->other);
16507
0
    }
16508
16509
0
  if (!definition
16510
0
      && ELF_MIPS_IS_OPTIONAL (st_other))
16511
0
    h->other |= STO_OPTIONAL;
16512
0
}
16513
16514
/* Decide whether an undefined symbol is special and can be ignored.
16515
   This is the case for OPTIONAL symbols on IRIX.  */
16516
bool
16517
_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
16518
0
{
16519
0
  return ELF_MIPS_IS_OPTIONAL (h->other) != 0;
16520
0
}
16521
16522
bool
16523
_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
16524
0
{
16525
0
  return (sym->st_shndx == SHN_COMMON
16526
0
    || sym->st_shndx == SHN_MIPS_ACOMMON
16527
0
    || sym->st_shndx == SHN_MIPS_SCOMMON);
16528
0
}
16529
16530
/* Return address for Ith PLT stub in section PLT, for relocation REL
16531
   or (bfd_vma) -1 if it should not be included.  */
16532
16533
bfd_vma
16534
_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
16535
         const arelent *rel ATTRIBUTE_UNUSED)
16536
0
{
16537
0
  return (plt->vma
16538
0
    + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
16539
0
    + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
16540
0
}
16541
16542
/* Build a table of synthetic symbols to represent the PLT.  As with MIPS16
16543
   and microMIPS PLT slots we may have a many-to-one mapping between .plt
16544
   and .got.plt and also the slots may be of a different size each we walk
16545
   the PLT manually fetching instructions and matching them against known
16546
   patterns.  To make things easier standard MIPS slots, if any, always come
16547
   first.  As we don't create proper ELF symbols we use the UDATA.I member
16548
   of ASYMBOL to carry ISA annotation.  The encoding used is the same as
16549
   with the ST_OTHER member of the ELF symbol.  */
16550
16551
long
16552
_bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
16553
            long symcount ATTRIBUTE_UNUSED,
16554
            asymbol **syms ATTRIBUTE_UNUSED,
16555
            long dynsymcount, asymbol **dynsyms,
16556
            asymbol **ret)
16557
27
{
16558
27
  static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
16559
27
  static const char microsuffix[] = "@micromipsplt";
16560
27
  static const char m16suffix[] = "@mips16plt";
16561
27
  static const char mipssuffix[] = "@plt";
16562
16563
27
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
16564
27
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
16565
27
  bool micromips_p = MICROMIPS_P (abfd);
16566
27
  Elf_Internal_Shdr *hdr;
16567
27
  bfd_byte *plt_data;
16568
27
  bfd_vma plt_offset;
16569
27
  unsigned int other;
16570
27
  bfd_vma entry_size;
16571
27
  bfd_vma plt0_size;
16572
27
  asection *relplt;
16573
27
  bfd_vma opcode;
16574
27
  asection *plt;
16575
27
  asymbol *send;
16576
27
  size_t size;
16577
27
  char *names;
16578
27
  long counti;
16579
27
  arelent *p;
16580
27
  asymbol *s;
16581
27
  char *nend;
16582
27
  long count;
16583
27
  long pi;
16584
27
  long i;
16585
27
  long n;
16586
16587
27
  *ret = NULL;
16588
16589
27
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
16590
26
    return 0;
16591
16592
1
  relplt = bfd_get_section_by_name (abfd, ".rel.plt");
16593
1
  if (relplt == NULL)
16594
1
    return 0;
16595
16596
0
  hdr = &elf_section_data (relplt)->this_hdr;
16597
0
  if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
16598
0
    return 0;
16599
16600
0
  plt = bfd_get_section_by_name (abfd, ".plt");
16601
0
  if (plt == NULL || (plt->flags & SEC_HAS_CONTENTS) == 0)
16602
0
    return 0;
16603
16604
0
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
16605
0
  if (!(*slurp_relocs) (abfd, relplt, dynsyms, true))
16606
0
    return -1;
16607
0
  p = relplt->relocation;
16608
16609
  /* Calculating the exact amount of space required for symbols would
16610
     require two passes over the PLT, so just pessimise assuming two
16611
     PLT slots per relocation.  */
16612
0
  count = NUM_SHDR_ENTRIES (hdr);
16613
0
  counti = count * bed->s->int_rels_per_ext_rel;
16614
0
  size = 2 * count * sizeof (asymbol);
16615
0
  size += count * (sizeof (mipssuffix) +
16616
0
       (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
16617
0
  for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
16618
0
    size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
16619
16620
  /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too.  */
16621
0
  size += sizeof (asymbol) + sizeof (pltname);
16622
16623
0
  if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
16624
0
    return -1;
16625
16626
0
  if (plt->size < 16)
16627
0
    return -1;
16628
16629
0
  s = *ret = bfd_malloc (size);
16630
0
  if (s == NULL)
16631
0
    return -1;
16632
0
  send = s + 2 * count + 1;
16633
16634
0
  names = (char *) send;
16635
0
  nend = (char *) s + size;
16636
0
  n = 0;
16637
16638
0
  opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
16639
0
  if (opcode == 0x3302fffe)
16640
0
    {
16641
0
      if (!micromips_p)
16642
0
  return -1;
16643
0
      plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
16644
0
      other = STO_MICROMIPS;
16645
0
    }
16646
0
  else if (opcode == 0x0398c1d0)
16647
0
    {
16648
0
      if (!micromips_p)
16649
0
  return -1;
16650
0
      plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
16651
0
      other = STO_MICROMIPS;
16652
0
    }
16653
0
  else
16654
0
    {
16655
0
      plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
16656
0
      other = 0;
16657
0
    }
16658
16659
0
  s->the_bfd = abfd;
16660
0
  s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
16661
0
  s->section = plt;
16662
0
  s->value = 0;
16663
0
  s->name = names;
16664
0
  s->udata.i = other;
16665
0
  memcpy (names, pltname, sizeof (pltname));
16666
0
  names += sizeof (pltname);
16667
0
  ++s, ++n;
16668
16669
0
  pi = 0;
16670
0
  for (plt_offset = plt0_size;
16671
0
       plt_offset + 8 <= plt->size && s < send;
16672
0
       plt_offset += entry_size)
16673
0
    {
16674
0
      bfd_vma gotplt_addr;
16675
0
      const char *suffix;
16676
0
      bfd_vma gotplt_hi;
16677
0
      bfd_vma gotplt_lo;
16678
0
      size_t suffixlen;
16679
16680
0
      opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
16681
16682
      /* Check if the second word matches the expected MIPS16 instruction.  */
16683
0
      if (opcode == 0x651aeb00)
16684
0
  {
16685
0
    if (micromips_p)
16686
0
      return -1;
16687
    /* Truncated table???  */
16688
0
    if (plt_offset + 16 > plt->size)
16689
0
      break;
16690
0
    gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
16691
0
    entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
16692
0
    suffixlen = sizeof (m16suffix);
16693
0
    suffix = m16suffix;
16694
0
    other = STO_MIPS16;
16695
0
  }
16696
      /* Likewise the expected microMIPS instruction (no insn32 mode).  */
16697
0
      else if (opcode == 0xff220000)
16698
0
  {
16699
0
    if (!micromips_p)
16700
0
      return -1;
16701
0
    gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
16702
0
    gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16703
0
    gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
16704
0
    gotplt_lo <<= 2;
16705
0
    gotplt_addr = gotplt_hi + gotplt_lo;
16706
0
    gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
16707
0
    entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
16708
0
    suffixlen = sizeof (microsuffix);
16709
0
    suffix = microsuffix;
16710
0
    other = STO_MICROMIPS;
16711
0
  }
16712
      /* Likewise the expected microMIPS instruction (insn32 mode).  */
16713
0
      else if ((opcode & 0xffff0000) == 0xff2f0000)
16714
0
  {
16715
0
    gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16716
0
    gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
16717
0
    gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16718
0
    gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16719
0
    gotplt_addr = gotplt_hi + gotplt_lo;
16720
0
    entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
16721
0
    suffixlen = sizeof (microsuffix);
16722
0
    suffix = microsuffix;
16723
0
    other = STO_MICROMIPS;
16724
0
  }
16725
      /* Otherwise assume standard MIPS code.  */
16726
0
      else
16727
0
  {
16728
0
    gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
16729
0
    gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
16730
0
    gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16731
0
    gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16732
0
    gotplt_addr = gotplt_hi + gotplt_lo;
16733
0
    entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
16734
0
    suffixlen = sizeof (mipssuffix);
16735
0
    suffix = mipssuffix;
16736
0
    other = 0;
16737
0
  }
16738
      /* Truncated table???  */
16739
0
      if (plt_offset + entry_size > plt->size)
16740
0
  break;
16741
16742
0
      for (i = 0;
16743
0
     i < count && p[pi].address != gotplt_addr;
16744
0
     i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
16745
16746
0
      if (i < count)
16747
0
  {
16748
0
    size_t namelen;
16749
0
    size_t len;
16750
16751
0
    *s = **p[pi].sym_ptr_ptr;
16752
    /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
16753
       we are defining a symbol, ensure one of them is set.  */
16754
0
    if ((s->flags & BSF_LOCAL) == 0)
16755
0
      s->flags |= BSF_GLOBAL;
16756
0
    s->flags |= BSF_SYNTHETIC;
16757
0
    s->section = plt;
16758
0
    s->value = plt_offset;
16759
0
    s->name = names;
16760
0
    s->udata.i = other;
16761
16762
0
    len = strlen ((*p[pi].sym_ptr_ptr)->name);
16763
0
    namelen = len + suffixlen;
16764
0
    if (names + namelen > nend)
16765
0
      break;
16766
16767
0
    memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
16768
0
    names += len;
16769
0
    memcpy (names, suffix, suffixlen);
16770
0
    names += suffixlen;
16771
16772
0
    ++s, ++n;
16773
0
    pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
16774
0
  }
16775
0
    }
16776
16777
0
  free (plt_data);
16778
16779
0
  return n;
16780
0
}
16781
16782
/* Return the ABI flags associated with ABFD if available.  */
16783
16784
Elf_Internal_ABIFlags_v0 *
16785
bfd_mips_elf_get_abiflags (bfd *abfd)
16786
706k
{
16787
706k
  struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
16788
16789
706k
  return tdata->abiflags_valid ? &tdata->abiflags : NULL;
16790
706k
}
16791
16792
/* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
16793
   field.  Taken from `libc-abis.h' generated at GNU libc build time.
16794
   Using a MIPS_ prefix as other libc targets use different values.  */
16795
enum
16796
{
16797
  MIPS_LIBC_ABI_DEFAULT = 0,
16798
  MIPS_LIBC_ABI_MIPS_PLT,
16799
  MIPS_LIBC_ABI_UNIQUE,
16800
  MIPS_LIBC_ABI_MIPS_O32_FP64,
16801
  MIPS_LIBC_ABI_ABSOLUTE,
16802
  MIPS_LIBC_ABI_XHASH,
16803
  MIPS_LIBC_ABI_MAX
16804
};
16805
16806
bool
16807
_bfd_mips_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
16808
26
{
16809
26
  struct mips_elf_link_hash_table *htab = NULL;
16810
26
  Elf_Internal_Ehdr *i_ehdrp;
16811
16812
26
  if (!_bfd_elf_init_file_header (abfd, link_info))
16813
0
    return false;
16814
16815
26
  i_ehdrp = elf_elfheader (abfd);
16816
26
  if (link_info)
16817
0
    {
16818
0
      htab = mips_elf_hash_table (link_info);
16819
0
      BFD_ASSERT (htab != NULL);
16820
0
    }
16821
16822
26
  if (htab != NULL
16823
26
      && htab->use_plts_and_copy_relocs
16824
26
      && htab->root.target_os != is_vxworks)
16825
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
16826
16827
26
  if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
16828
26
      || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
16829
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
16830
16831
  /* Mark that we need support for absolute symbols in the dynamic loader.  */
16832
26
  if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
16833
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
16834
16835
  /* Mark that we need support for .MIPS.xhash in the dynamic linker,
16836
     if it is the only hash section that will be created.  */
16837
26
  if (link_info && link_info->emit_gnu_hash && !link_info->emit_hash)
16838
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_XHASH;
16839
26
  return true;
16840
26
}
16841
16842
int
16843
_bfd_mips_elf_compact_eh_encoding
16844
  (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16845
0
{
16846
0
  return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
16847
0
}
16848
16849
/* Return the opcode for can't unwind.  */
16850
16851
int
16852
_bfd_mips_elf_cant_unwind_opcode
16853
  (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16854
0
{
16855
0
  return COMPACT_EH_CANT_UNWIND_OPCODE;
16856
0
}
16857
16858
/* Record a position XLAT_LOC in the xlat translation table, associated with
16859
   the hash entry H.  The entry in the translation table will later be
16860
   populated with the real symbol dynindx.  */
16861
16862
void
16863
_bfd_mips_elf_record_xhash_symbol (struct elf_link_hash_entry *h,
16864
           bfd_vma xlat_loc)
16865
0
{
16866
0
  struct mips_elf_link_hash_entry *hmips;
16867
16868
0
  hmips = (struct mips_elf_link_hash_entry *) h;
16869
0
  hmips->mipsxhash_loc = xlat_loc;
16870
0
}