Coverage Report

Created: 2025-07-08 11:15

/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-2025 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
  asection *elf_data_section;
597
  asection *elf_text_section;
598
599
  struct mips_hi16 *mips_hi16_list;
600
};
601
602
/* Get MIPS ELF private object data from BFD's tdata.  */
603
604
#define mips_elf_tdata(bfd) \
605
2.18M
  ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
606
607
#define TLS_RELOC_P(r_type) \
608
0
  (r_type == R_MIPS_TLS_DTPMOD32    \
609
0
   || r_type == R_MIPS_TLS_DTPMOD64    \
610
0
   || r_type == R_MIPS_TLS_DTPREL32    \
611
0
   || r_type == R_MIPS_TLS_DTPREL64    \
612
0
   || r_type == R_MIPS_TLS_GD      \
613
0
   || r_type == R_MIPS_TLS_LDM      \
614
0
   || r_type == R_MIPS_TLS_DTPREL_HI16    \
615
0
   || r_type == R_MIPS_TLS_DTPREL_LO16    \
616
0
   || r_type == R_MIPS_TLS_GOTTPREL    \
617
0
   || r_type == R_MIPS_TLS_TPREL32    \
618
0
   || r_type == R_MIPS_TLS_TPREL64    \
619
0
   || r_type == R_MIPS_TLS_TPREL_HI16    \
620
0
   || r_type == R_MIPS_TLS_TPREL_LO16    \
621
0
   || r_type == R_MIPS16_TLS_GD      \
622
0
   || r_type == R_MIPS16_TLS_LDM    \
623
0
   || r_type == R_MIPS16_TLS_DTPREL_HI16  \
624
0
   || r_type == R_MIPS16_TLS_DTPREL_LO16  \
625
0
   || r_type == R_MIPS16_TLS_GOTTPREL    \
626
0
   || r_type == R_MIPS16_TLS_TPREL_HI16    \
627
0
   || r_type == R_MIPS16_TLS_TPREL_LO16    \
628
0
   || r_type == R_MICROMIPS_TLS_GD    \
629
0
   || r_type == R_MICROMIPS_TLS_LDM    \
630
0
   || r_type == R_MICROMIPS_TLS_DTPREL_HI16  \
631
0
   || r_type == R_MICROMIPS_TLS_DTPREL_LO16  \
632
0
   || r_type == R_MICROMIPS_TLS_GOTTPREL  \
633
0
   || r_type == R_MICROMIPS_TLS_TPREL_HI16  \
634
0
   || r_type == R_MICROMIPS_TLS_TPREL_LO16)
635
636
/* Structure used to pass information to mips_elf_output_extsym.  */
637
638
struct extsym_info
639
{
640
  bfd *abfd;
641
  struct bfd_link_info *info;
642
  struct ecoff_debug_info *debug;
643
  const struct ecoff_debug_swap *swap;
644
  bool failed;
645
};
646
647
/* The names of the runtime procedure table symbols used on IRIX5.  */
648
649
static const char * const mips_elf_dynsym_rtproc_names[] =
650
{
651
  "_procedure_table",
652
  "_procedure_string_table",
653
  "_procedure_table_size",
654
  NULL
655
};
656
657
/* These structures are used to generate the .compact_rel section on
658
   IRIX5.  */
659
660
typedef struct
661
{
662
  unsigned long id1;    /* Always one?  */
663
  unsigned long num;    /* Number of compact relocation entries.  */
664
  unsigned long id2;    /* Always two?  */
665
  unsigned long offset;   /* The file offset of the first relocation.  */
666
  unsigned long reserved0;  /* Zero?  */
667
  unsigned long reserved1;  /* Zero?  */
668
} Elf32_compact_rel;
669
670
typedef struct
671
{
672
  bfd_byte id1[4];
673
  bfd_byte num[4];
674
  bfd_byte id2[4];
675
  bfd_byte offset[4];
676
  bfd_byte reserved0[4];
677
  bfd_byte reserved1[4];
678
} Elf32_External_compact_rel;
679
680
typedef struct
681
{
682
  unsigned int ctype : 1; /* 1: long 0: short format. See below.  */
683
  unsigned int rtype : 4; /* Relocation types. See below.  */
684
  unsigned int dist2to : 8;
685
  unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
686
  unsigned long konst;    /* KONST field. See below.  */
687
  unsigned long vaddr;    /* VADDR to be relocated.  */
688
} Elf32_crinfo;
689
690
typedef struct
691
{
692
  unsigned int ctype : 1; /* 1: long 0: short format. See below.  */
693
  unsigned int rtype : 4; /* Relocation types. See below.  */
694
  unsigned int dist2to : 8;
695
  unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
696
  unsigned long konst;    /* KONST field. See below.  */
697
} Elf32_crinfo2;
698
699
typedef struct
700
{
701
  bfd_byte info[4];
702
  bfd_byte konst[4];
703
  bfd_byte vaddr[4];
704
} Elf32_External_crinfo;
705
706
typedef struct
707
{
708
  bfd_byte info[4];
709
  bfd_byte konst[4];
710
} Elf32_External_crinfo2;
711
712
/* These are the constants used to swap the bitfields in a crinfo.  */
713
714
0
#define CRINFO_CTYPE (0x1U)
715
0
#define CRINFO_CTYPE_SH (31)
716
0
#define CRINFO_RTYPE (0xfU)
717
0
#define CRINFO_RTYPE_SH (27)
718
0
#define CRINFO_DIST2TO (0xffU)
719
0
#define CRINFO_DIST2TO_SH (19)
720
0
#define CRINFO_RELVADDR (0x7ffffU)
721
0
#define CRINFO_RELVADDR_SH (0)
722
723
/* A compact relocation info has long (3 words) or short (2 words)
724
   formats.  A short format doesn't have VADDR field and relvaddr
725
   fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
726
#define CRF_MIPS_LONG     1
727
#define CRF_MIPS_SHORT      0
728
729
/* There are 4 types of compact relocation at least. The value KONST
730
   has different meaning for each type:
731
732
   (type)   (konst)
733
   CT_MIPS_REL32  Address in data
734
   CT_MIPS_WORD   Address in word (XXX)
735
   CT_MIPS_GPHI_LO  GP - vaddr
736
   CT_MIPS_JMPAD  Address to jump
737
   */
738
739
#define CRT_MIPS_REL32      0xa
740
#define CRT_MIPS_WORD     0xb
741
#define CRT_MIPS_GPHI_LO    0xc
742
#define CRT_MIPS_JMPAD      0xd
743
744
0
#define mips_elf_set_cr_format(x,format)  ((x).ctype = (format))
745
0
#define mips_elf_set_cr_type(x,type)    ((x).rtype = (type))
746
0
#define mips_elf_set_cr_dist2to(x,v)    ((x).dist2to = (v))
747
0
#define mips_elf_set_cr_relvaddr(x,d)   ((x).relvaddr = (d)<<2)
748

749
/* The structure of the runtime procedure descriptor created by the
750
   loader for use by the static exception system.  */
751
752
typedef struct runtime_pdr {
753
  bfd_vma adr;    /* Memory address of start of procedure.  */
754
  long  regmask;  /* Save register mask.  */
755
  long  regoffset;  /* Save register offset.  */
756
  long  fregmask; /* Save floating point register mask.  */
757
  long  fregoffset; /* Save floating point register offset.  */
758
  long  frameoffset;  /* Frame size.  */
759
  short framereg; /* Frame pointer register.  */
760
  short pcreg;    /* Offset or reg of return pc.  */
761
  long  irpss;    /* Index into the runtime string table.  */
762
  long  reserved;
763
  struct exception_info *exception_info;/* Pointer to exception array.  */
764
} RPDR, *pRPDR;
765
#define cbRPDR sizeof (RPDR)
766
#define rpdNil ((pRPDR) 0)
767

768
static struct mips_got_entry *mips_elf_create_local_got_entry
769
  (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
770
   struct mips_elf_link_hash_entry *, int);
771
static bool mips_elf_sort_hash_table_f
772
  (struct mips_elf_link_hash_entry *, void *);
773
static bfd_vma mips_elf_high
774
  (bfd_vma);
775
static bool mips_elf_create_dynamic_relocation
776
  (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
777
   struct mips_elf_link_hash_entry *, asection *, bfd_vma,
778
   bfd_vma *, asection *);
779
static bfd_vma mips_elf_adjust_gp
780
  (bfd *, struct mips_got_info *, bfd *);
781
782
/* This will be used when we sort the dynamic relocation records.  */
783
static bfd *reldyn_sorting_bfd;
784
785
/* True if ABFD is for CPUs with load interlocking that include
786
   non-MIPS1 CPUs and R3900.  */
787
#define LOAD_INTERLOCKS_P(abfd) \
788
0
  (   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != EF_MIPS_ARCH_1) \
789
0
   || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_3900))
790
791
/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
792
   This should be safe for all architectures.  We enable this predicate
793
   for RM9000 for now.  */
794
#define JAL_TO_BAL_P(abfd) \
795
0
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_9000)
796
797
/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
798
   This should be safe for all architectures.  We enable this predicate for
799
   all CPUs.  */
800
0
#define JALR_TO_BAL_P(abfd) 1
801
802
/* True if ABFD is for CPUs that are faster if JR is converted to B.
803
   This should be safe for all architectures.  We enable this predicate for
804
   all CPUs.  */
805
0
#define JR_TO_B_P(abfd) 1
806
807
/* True if ABFD is a PIC object.  */
808
#define PIC_OBJECT_P(abfd) \
809
0
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
810
811
/* Nonzero if ABFD is using the O32 ABI.  */
812
#define ABI_O32_P(abfd) \
813
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32)
814
815
/* Nonzero if ABFD is using the N32 ABI.  */
816
#define ABI_N32_P(abfd) \
817
5.75k
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
818
819
/* Nonzero if ABFD is using the N64 ABI.  */
820
#define ABI_64_P(abfd) \
821
4.27k
  (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
822
823
/* Nonzero if ABFD is using NewABI conventions.  */
824
2.81k
#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
825
826
/* Nonzero if ABFD has microMIPS code.  */
827
#define MICROMIPS_P(abfd) \
828
38
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
829
830
/* Nonzero if ABFD is MIPS R6.  */
831
#define MIPSR6_P(abfd) \
832
0
  ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6 \
833
0
    || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6)
834
835
/* The IRIX compatibility level we are striving for.  */
836
#define IRIX_COMPAT(abfd) \
837
6
  (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
838
839
/* Whether we are trying to be compatible with IRIX at all.  */
840
#define SGI_COMPAT(abfd) \
841
3
  (IRIX_COMPAT (abfd) != ict_none)
842
843
/* The name of the options section.  */
844
#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
845
2.81k
  (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
846
847
/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
848
   Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME.  */
849
#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
850
9.84k
  (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
851
852
/* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section.  */
853
#define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
854
482
  (strcmp (NAME, ".MIPS.abiflags") == 0)
855
856
/* Whether the section is readonly.  */
857
#define MIPS_ELF_READONLY_SECTION(sec) \
858
0
  ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))    \
859
0
   == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
860
861
/* The name of the stub section.  */
862
0
#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
863
864
/* The size of an external REL relocation.  */
865
#define MIPS_ELF_REL_SIZE(abfd) \
866
0
  (get_elf_backend_data (abfd)->s->sizeof_rel)
867
868
/* The size of an external RELA relocation.  */
869
#define MIPS_ELF_RELA_SIZE(abfd) \
870
0
  (get_elf_backend_data (abfd)->s->sizeof_rela)
871
872
/* The size of an external dynamic table entry.  */
873
#define MIPS_ELF_DYN_SIZE(abfd) \
874
0
  (get_elf_backend_data (abfd)->s->sizeof_dyn)
875
876
/* The size of a GOT entry.  */
877
#define MIPS_ELF_GOT_SIZE(abfd) \
878
0
  (get_elf_backend_data (abfd)->s->arch_size / 8)
879
880
/* The size of the .rld_map section. */
881
#define MIPS_ELF_RLD_MAP_SIZE(abfd) \
882
0
  (get_elf_backend_data (abfd)->s->arch_size / 8)
883
884
/* The size of a symbol-table entry.  */
885
#define MIPS_ELF_SYM_SIZE(abfd) \
886
0
  (get_elf_backend_data (abfd)->s->sizeof_sym)
887
888
/* The default alignment for sections, as a power of two.  */
889
#define MIPS_ELF_LOG_FILE_ALIGN(abfd)       \
890
0
  (get_elf_backend_data (abfd)->s->log_file_align)
891
892
/* Get word-sized data.  */
893
#define MIPS_ELF_GET_WORD(abfd, ptr) \
894
  (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
895
896
/* Put out word-sized data.  */
897
#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
898
0
  (ABI_64_P (abfd)       \
899
0
   ? bfd_put_64 (abfd, val, ptr)   \
900
0
   : bfd_put_32 (abfd, val, ptr))
901
902
/* The opcode for word-sized loads (LW or LD).  */
903
#define MIPS_ELF_LOAD_WORD(abfd) \
904
0
  (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
905
906
/* Add a dynamic symbol table-entry.  */
907
#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val)  \
908
0
  _bfd_elf_add_dynamic_entry (info, tag, val)
909
910
#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela)      \
911
0
  (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
912
913
/* The name of the dynamic relocation section.  */
914
#define MIPS_ELF_REL_DYN_NAME(INFO) \
915
0
  (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
916
0
   ? ".rela.dyn" : ".rel.dyn")
917
918
/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
919
   from smaller values.  Start with zero, widen, *then* decrement.  */
920
0
#define MINUS_ONE (((bfd_vma)0) - 1)
921
0
#define MINUS_TWO (((bfd_vma)0) - 2)
922
923
/* The value to write into got[1] for SVR4 targets, to identify it is
924
   a GNU object.  The dynamic linker can then use got[1] to store the
925
   module pointer.  */
926
#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
927
  ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
928
929
/* The offset of $gp from the beginning of the .got section.  */
930
#define ELF_MIPS_GP_OFFSET(INFO) \
931
0
  (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
932
0
   ? 0x0 : 0x7ff0)
933
934
/* The maximum size of the GOT for it to be addressable using 16-bit
935
   offsets from $gp.  */
936
0
#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
937
938
/* Instructions which appear in a stub.  */
939
#define STUB_LW(abfd)             \
940
  ((ABI_64_P (abfd)             \
941
    ? 0xdf998010        /* ld t9,0x8010(gp) */  \
942
    : 0x8f998010))        /* lw t9,0x8010(gp) */
943
#define STUB_MOVE 0x03e07825      /* or t7,ra,zero */
944
#define STUB_LUI(VAL) (0x3c180000 + (VAL))  /* lui t8,VAL */
945
#define STUB_JALR 0x0320f809      /* jalr ra,t9 */
946
#define STUB_JALRC 0xf8190000     /* jalrc ra,t9 */
947
#define STUB_ORI(VAL) (0x37180000 + (VAL))  /* ori t8,t8,VAL */
948
#define STUB_LI16U(VAL) (0x34180000 + (VAL))  /* ori t8,zero,VAL unsigned */
949
#define STUB_LI16S(abfd, VAL)           \
950
   ((ABI_64_P (abfd)              \
951
    ? (0x64180000 + (VAL))  /* daddiu t8,zero,VAL sign extended */  \
952
    : (0x24180000 + (VAL))))  /* addiu t8,zero,VAL sign extended */
953
954
/* Likewise for the microMIPS ASE.  */
955
#define STUB_LW_MICROMIPS(abfd)           \
956
0
  (ABI_64_P (abfd)             \
957
0
   ? 0xdf3c8010          /* ld t9,0x8010(gp) */  \
958
0
   : 0xff3c8010)        /* lw t9,0x8010(gp) */
959
#define STUB_MOVE_MICROMIPS 0x0dff    /* move t7,ra */
960
0
#define STUB_MOVE32_MICROMIPS 0x001f7a90  /* or t7,ra,zero */
961
#define STUB_LUI_MICROMIPS(VAL)           \
962
0
   (0x41b80000 + (VAL))        /* lui t8,VAL */
963
#define STUB_JALR_MICROMIPS 0x45d9    /* jalr t9 */
964
0
#define STUB_JALR32_MICROMIPS 0x03f90f3c  /* jalr ra,t9 */
965
#define STUB_ORI_MICROMIPS(VAL)           \
966
0
  (0x53180000 + (VAL))        /* ori t8,t8,VAL */
967
#define STUB_LI16U_MICROMIPS(VAL)         \
968
0
  (0x53000000 + (VAL))        /* ori t8,zero,VAL unsigned */
969
#define STUB_LI16S_MICROMIPS(abfd, VAL)         \
970
0
   (ABI_64_P (abfd)             \
971
0
    ? 0x5f000000 + (VAL)  /* daddiu t8,zero,VAL sign extended */  \
972
0
    : 0x33000000 + (VAL))  /* addiu t8,zero,VAL sign extended */
973
974
0
#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
975
0
#define MIPS_FUNCTION_STUB_BIG_SIZE 20
976
0
#define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
977
0
#define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
978
0
#define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
979
0
#define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
980
981
/* The name of the dynamic interpreter.  This is put in the .interp
982
   section.  */
983
984
#define ELF_DYNAMIC_INTERPRETER(abfd)   \
985
0
   (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1"  \
986
0
    : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1"  \
987
0
    : "/usr/lib/libc.so.1")
988
989
#ifdef BFD64
990
#define MNAME(bfd,pre,pos) \
991
  (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
992
#define ELF_R_SYM(bfd, i)         \
993
0
  (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
994
#define ELF_R_TYPE(bfd, i)          \
995
0
  (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
996
#define ELF_R_INFO(bfd, s, t)         \
997
0
  (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
998
#else
999
#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
1000
#define ELF_R_SYM(bfd, i)         \
1001
  (ELF32_R_SYM (i))
1002
#define ELF_R_TYPE(bfd, i)          \
1003
  (ELF32_R_TYPE (i))
1004
#define ELF_R_INFO(bfd, s, t)         \
1005
  (ELF32_R_INFO (s, t))
1006
#endif
1007

1008
  /* The mips16 compiler uses a couple of special sections to handle
1009
     floating point arguments.
1010
1011
     Section names that look like .mips16.fn.FNNAME contain stubs that
1012
     copy floating point arguments from the fp regs to the gp regs and
1013
     then jump to FNNAME.  If any 32 bit function calls FNNAME, the
1014
     call should be redirected to the stub instead.  If no 32 bit
1015
     function calls FNNAME, the stub should be discarded.  We need to
1016
     consider any reference to the function, not just a call, because
1017
     if the address of the function is taken we will need the stub,
1018
     since the address might be passed to a 32 bit function.
1019
1020
     Section names that look like .mips16.call.FNNAME contain stubs
1021
     that copy floating point arguments from the gp regs to the fp
1022
     regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
1023
     then any 16 bit function that calls FNNAME should be redirected
1024
     to the stub instead.  If FNNAME is not a 32 bit function, the
1025
     stub should be discarded.
1026
1027
     .mips16.call.fp.FNNAME sections are similar, but contain stubs
1028
     which call FNNAME and then copy the return value from the fp regs
1029
     to the gp regs.  These stubs store the return value in $18 while
1030
     calling FNNAME; any function which might call one of these stubs
1031
     must arrange to save $18 around the call.  (This case is not
1032
     needed for 32 bit functions that call 16 bit functions, because
1033
     16 bit functions always return floating point values in both
1034
     $f0/$f1 and $2/$3.)
1035
1036
     Note that in all cases FNNAME might be defined statically.
1037
     Therefore, FNNAME is not used literally.  Instead, the relocation
1038
     information will indicate which symbol the section is for.
1039
1040
     We record any stubs that we find in the symbol table.  */
1041
1042
0
#define FN_STUB ".mips16.fn."
1043
0
#define CALL_STUB ".mips16.call."
1044
0
#define CALL_FP_STUB ".mips16.call.fp."
1045
1046
0
#define FN_STUB_P(name) startswith (name, FN_STUB)
1047
0
#define CALL_STUB_P(name) startswith (name, CALL_STUB)
1048
0
#define CALL_FP_STUB_P(name) startswith (name, CALL_FP_STUB)
1049

1050
/* The format of the first PLT entry in an O32 executable.  */
1051
static const bfd_vma mips_o32_exec_plt0_entry[] =
1052
{
1053
  0x3c1c0000, /* lui $28, %hi(&GOTPLT[0])       */
1054
  0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28)       */
1055
  0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0])      */
1056
  0x031cc023, /* subu $24, $24, $28         */
1057
  0x03e07825, /* or t7, ra, zero          */
1058
  0x0018c082, /* srl $24, $24, 2          */
1059
  0x0320f809, /* jalr $25           */
1060
  0x2718fffe  /* subu $24, $24, 2         */
1061
};
1062
1063
/* The format of the first PLT entry in an O32 executable using compact
1064
   jumps.  */
1065
static const bfd_vma mipsr6_o32_exec_plt0_entry_compact[] =
1066
{
1067
  0x3c1c0000, /* lui $28, %hi(&GOTPLT[0])       */
1068
  0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28)       */
1069
  0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0])      */
1070
  0x031cc023, /* subu $24, $24, $28         */
1071
  0x03e07821, /* move $15, $31  # 32-bit move (addu)    */
1072
  0x0018c082, /* srl $24, $24, 2          */
1073
  0x2718fffe, /* subu $24, $24, 2         */
1074
  0xf8190000  /* jalrc $25            */
1075
};
1076
1077
/* The format of the first PLT entry in an N32 executable.  Different
1078
   because gp ($28) is not available; we use t2 ($14) instead.  */
1079
static const bfd_vma mips_n32_exec_plt0_entry[] =
1080
{
1081
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1082
  0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14)       */
1083
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1084
  0x030ec023, /* subu $24, $24, $14         */
1085
  0x03e07825, /* or t7, ra, zero          */
1086
  0x0018c082, /* srl $24, $24, 2          */
1087
  0x0320f809, /* jalr $25           */
1088
  0x2718fffe  /* subu $24, $24, 2         */
1089
};
1090
1091
/* The format of the first PLT entry in an N32 executable using compact
1092
   jumps.  Different because gp ($28) is not available; we use t2 ($14)
1093
   instead.  */
1094
static const bfd_vma mipsr6_n32_exec_plt0_entry_compact[] =
1095
{
1096
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1097
  0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14)       */
1098
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1099
  0x030ec023, /* subu $24, $24, $14         */
1100
  0x03e07821, /* move $15, $31  # 32-bit move (addu)    */
1101
  0x0018c082, /* srl $24, $24, 2          */
1102
  0x2718fffe, /* subu $24, $24, 2         */
1103
  0xf8190000  /* jalrc $25            */
1104
};
1105
1106
/* The format of the first PLT entry in an N64 executable.  Different
1107
   from N32 because of the increased size of GOT entries.  */
1108
static const bfd_vma mips_n64_exec_plt0_entry[] =
1109
{
1110
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1111
  0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14)       */
1112
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1113
  0x030ec023, /* subu $24, $24, $14         */
1114
  0x03e07825, /* or t7, ra, zero          */
1115
  0x0018c0c2, /* srl $24, $24, 3          */
1116
  0x0320f809, /* jalr $25           */
1117
  0x2718fffe  /* subu $24, $24, 2         */
1118
};
1119
1120
/* The format of the first PLT entry in an N64 executable using compact
1121
   jumps.  Different from N32 because of the increased size of GOT
1122
   entries.  */
1123
static const bfd_vma mipsr6_n64_exec_plt0_entry_compact[] =
1124
{
1125
  0x3c0e0000, /* lui $14, %hi(&GOTPLT[0])       */
1126
  0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14)       */
1127
  0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0])      */
1128
  0x030ec023, /* subu $24, $24, $14         */
1129
  0x03e0782d, /* move $15, $31  # 64-bit move (daddu)   */
1130
  0x0018c0c2, /* srl $24, $24, 3          */
1131
  0x2718fffe, /* subu $24, $24, 2         */
1132
  0xf8190000  /* jalrc $25            */
1133
};
1134
1135
1136
/* The format of the microMIPS first PLT entry in an O32 executable.
1137
   We rely on v0 ($2) rather than t8 ($24) to contain the address
1138
   of the GOTPLT entry handled, so this stub may only be used when
1139
   all the subsequent PLT entries are microMIPS code too.
1140
1141
   The trailing NOP is for alignment and correct disassembly only.  */
1142
static const bfd_vma micromips_o32_exec_plt0_entry[] =
1143
{
1144
  0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - .     */
1145
  0xff23, 0x0000, /* lw $25, 0($3)        */
1146
  0x0535,   /* subu $2, $2, $3        */
1147
  0x2525,   /* srl $2, $2, 2        */
1148
  0x3302, 0xfffe, /* subu $24, $2, 2        */
1149
  0x0dff,   /* move $15, $31        */
1150
  0x45f9,   /* jalrs $25          */
1151
  0x0f83,   /* move $28, $3         */
1152
  0x0c00    /* nop            */
1153
};
1154
1155
/* The format of the microMIPS first PLT entry in an O32 executable
1156
   in the insn32 mode.  */
1157
static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1158
{
1159
  0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0])     */
1160
  0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28)     */
1161
  0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0])    */
1162
  0x0398, 0xc1d0, /* subu $24, $24, $28       */
1163
  0x001f, 0x7a90, /* or $15, $31, zero        */
1164
  0x0318, 0x1040, /* srl $24, $24, 2        */
1165
  0x03f9, 0x0f3c, /* jalr $25         */
1166
  0x3318, 0xfffe  /* subu $24, $24, 2       */
1167
};
1168
1169
/* The format of subsequent standard PLT entries.  */
1170
static const bfd_vma mips_exec_plt_entry[] =
1171
{
1172
  0x3c0f0000, /* lui $15, %hi(.got.plt entry)     */
1173
  0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15)    */
1174
  0x25f80000, /* addiu $24, $15, %lo(.got.plt entry)    */
1175
  0x03200008  /* jr $25         */
1176
};
1177
1178
static const bfd_vma mipsr6_exec_plt_entry[] =
1179
{
1180
  0x3c0f0000, /* lui $15, %hi(.got.plt entry)     */
1181
  0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15)    */
1182
  0x25f80000, /* addiu $24, $15, %lo(.got.plt entry)    */
1183
  0x03200009  /* jr $25         */
1184
};
1185
1186
static const bfd_vma mipsr6_exec_plt_entry_compact[] =
1187
{
1188
  0x3c0f0000, /* lui $15, %hi(.got.plt entry)     */
1189
  0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15)    */
1190
  0x25f80000, /* addiu $24, $15, %lo(.got.plt entry)    */
1191
  0xd8190000  /* jic $25, 0         */
1192
};
1193
1194
/* The format of subsequent MIPS16 o32 PLT entries.  We use v0 ($2)
1195
   and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1196
   directly addressable.  */
1197
static const bfd_vma mips16_o32_exec_plt_entry[] =
1198
{
1199
  0xb203,   /* lw $2, 12($pc)     */
1200
  0x9a60,   /* lw $3, 0($2)       */
1201
  0x651a,   /* move $24, $2       */
1202
  0xeb00,   /* jr $3        */
1203
  0x653b,   /* move $25, $3       */
1204
  0x6500,   /* nop          */
1205
  0x0000, 0x0000  /* .word (.got.plt entry)   */
1206
};
1207
1208
/* The format of subsequent microMIPS o32 PLT entries.  We use v0 ($2)
1209
   as a temporary because t8 ($24) is not addressable with ADDIUPC.  */
1210
static const bfd_vma micromips_o32_exec_plt_entry[] =
1211
{
1212
  0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1213
  0xff22, 0x0000, /* lw $25, 0($2)      */
1214
  0x4599,   /* jr $25       */
1215
  0x0f02    /* move $24, $2       */
1216
};
1217
1218
/* The format of subsequent microMIPS o32 PLT entries in the insn32 mode.  */
1219
static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1220
{
1221
  0x41af, 0x0000, /* lui $15, %hi(.got.plt entry)   */
1222
  0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1223
  0x0019, 0x0f3c, /* jr $25       */
1224
  0x330f, 0x0000  /* addiu $24, $15, %lo(.got.plt entry)  */
1225
};
1226
1227
/* The format of the first PLT entry in a VxWorks executable.  */
1228
static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1229
{
1230
  0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_)   */
1231
  0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1232
  0x8f390008, /* lw t9, 8(t9)         */
1233
  0x00000000, /* nop            */
1234
  0x03200008, /* jr t9          */
1235
  0x00000000  /* nop            */
1236
};
1237
1238
/* The format of subsequent PLT entries.  */
1239
static const bfd_vma mips_vxworks_exec_plt_entry[] =
1240
{
1241
  0x10000000, /* b .PLT_resolver      */
1242
  0x24180000, /* li t8, <pltindex>      */
1243
  0x3c190000, /* lui t9, %hi(<.got.plt slot>)   */
1244
  0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1245
  0x8f390000, /* lw t9, 0(t9)       */
1246
  0x00000000, /* nop          */
1247
  0x03200008, /* jr t9        */
1248
  0x00000000  /* nop          */
1249
};
1250
1251
/* The format of the first PLT entry in a VxWorks shared object.  */
1252
static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1253
{
1254
  0x8f990008, /* lw t9, 8(gp)   */
1255
  0x00000000, /* nop      */
1256
  0x03200008, /* jr t9    */
1257
  0x00000000, /* nop      */
1258
  0x00000000, /* nop      */
1259
  0x00000000  /* nop      */
1260
};
1261
1262
/* The format of subsequent PLT entries.  */
1263
static const bfd_vma mips_vxworks_shared_plt_entry[] =
1264
{
1265
  0x10000000, /* b .PLT_resolver  */
1266
  0x24180000  /* li t8, <pltindex>  */
1267
};
1268

1269
/* microMIPS 32-bit opcode helper installer.  */
1270
1271
static void
1272
bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1273
0
{
1274
0
  bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1275
0
  bfd_put_16 (abfd,  opcode    & 0xffff, ptr + 2);
1276
0
}
1277
1278
/* microMIPS 32-bit opcode helper retriever.  */
1279
1280
static bfd_vma
1281
bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1282
0
{
1283
0
  return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1284
0
}
1285

1286
/* Look up an entry in a MIPS ELF linker hash table.  */
1287
1288
#define mips_elf_link_hash_lookup(table, string, create, copy, follow)  \
1289
0
  ((struct mips_elf_link_hash_entry *)          \
1290
0
   elf_link_hash_lookup (&(table)->root, (string), (create),    \
1291
0
       (copy), (follow)))
1292
1293
/* Traverse a MIPS ELF linker hash table.  */
1294
1295
#define mips_elf_link_hash_traverse(table, func, info)      \
1296
0
  (elf_link_hash_traverse           \
1297
0
   (&(table)->root,              \
1298
0
    (bool (*) (struct elf_link_hash_entry *, void *)) (func),   \
1299
0
    (info)))
1300
1301
/* Find the base offsets for thread-local storage in this object,
1302
   for GD/LD and IE/LE respectively.  */
1303
1304
0
#define TP_OFFSET 0x7000
1305
0
#define DTP_OFFSET 0x8000
1306
1307
static bfd_vma
1308
dtprel_base (struct bfd_link_info *info)
1309
0
{
1310
  /* If tls_sec is NULL, we should have signalled an error already.  */
1311
0
  if (elf_hash_table (info)->tls_sec == NULL)
1312
0
    return 0;
1313
0
  return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1314
0
}
1315
1316
static bfd_vma
1317
tprel_base (struct bfd_link_info *info)
1318
0
{
1319
  /* If tls_sec is NULL, we should have signalled an error already.  */
1320
0
  if (elf_hash_table (info)->tls_sec == NULL)
1321
0
    return 0;
1322
0
  return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1323
0
}
1324
1325
/* Create an entry in a MIPS ELF linker hash table.  */
1326
1327
static struct bfd_hash_entry *
1328
mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1329
          struct bfd_hash_table *table, const char *string)
1330
0
{
1331
0
  struct mips_elf_link_hash_entry *ret =
1332
0
    (struct mips_elf_link_hash_entry *) entry;
1333
1334
  /* Allocate the structure if it has not already been allocated by a
1335
     subclass.  */
1336
0
  if (ret == NULL)
1337
0
    ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1338
0
  if (ret == NULL)
1339
0
    return (struct bfd_hash_entry *) ret;
1340
1341
  /* Call the allocation method of the superclass.  */
1342
0
  ret = ((struct mips_elf_link_hash_entry *)
1343
0
   _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1344
0
             table, string));
1345
0
  if (ret != NULL)
1346
0
    {
1347
      /* Set local fields.  */
1348
0
      memset (&ret->esym, 0, sizeof (EXTR));
1349
      /* We use -2 as a marker to indicate that the information has
1350
   not been set.  -1 means there is no associated ifd.  */
1351
0
      ret->esym.ifd = -2;
1352
0
      ret->la25_stub = 0;
1353
0
      ret->possibly_dynamic_relocs = 0;
1354
0
      ret->fn_stub = NULL;
1355
0
      ret->call_stub = NULL;
1356
0
      ret->call_fp_stub = NULL;
1357
0
      ret->mipsxhash_loc = 0;
1358
0
      ret->global_got_area = GGA_NONE;
1359
0
      ret->got_only_for_calls = true;
1360
0
      ret->readonly_reloc = false;
1361
0
      ret->has_static_relocs = false;
1362
0
      ret->no_fn_stub = false;
1363
0
      ret->need_fn_stub = false;
1364
0
      ret->has_nonpic_branches = false;
1365
0
      ret->needs_lazy_stub = false;
1366
0
      ret->use_plt_entry = false;
1367
0
    }
1368
1369
0
  return (struct bfd_hash_entry *) ret;
1370
0
}
1371
1372
/* Allocate MIPS ELF private object data.  */
1373
1374
bool
1375
_bfd_mips_elf_mkobject (bfd *abfd)
1376
2.56M
{
1377
2.56M
  return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata));
1378
2.56M
}
1379
1380
/* MIPS ELF uses a special find_nearest_line routine in order the
1381
   handle the ECOFF debugging information.  */
1382
1383
struct mips_elf_find_line
1384
{
1385
  struct ecoff_debug_info d;
1386
  struct ecoff_find_line i;
1387
};
1388
1389
bool
1390
_bfd_mips_elf_free_cached_info (bfd *abfd)
1391
97.6k
{
1392
97.6k
  struct mips_elf_obj_tdata *tdata;
1393
1394
97.6k
  if ((bfd_get_format (abfd) == bfd_object
1395
97.6k
       || bfd_get_format (abfd) == bfd_core)
1396
97.6k
      && (tdata = mips_elf_tdata (abfd)) != NULL)
1397
40.5k
    {
1398
40.5k
      BFD_ASSERT (tdata->root.object_id == MIPS_ELF_DATA);
1399
40.5k
      while (tdata->mips_hi16_list != NULL)
1400
0
  {
1401
0
    struct mips_hi16 *hi = tdata->mips_hi16_list;
1402
0
    tdata->mips_hi16_list = hi->next;
1403
0
    free (hi);
1404
0
  }
1405
40.5k
      if (tdata->find_line_info != NULL)
1406
104
  _bfd_ecoff_free_ecoff_debug_info (&tdata->find_line_info->d);
1407
40.5k
    }
1408
97.6k
  return _bfd_elf_free_cached_info (abfd);
1409
97.6k
}
1410
1411
bool
1412
_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1413
783k
{
1414
783k
  struct _mips_elf_section_data *sdata;
1415
1416
783k
  sdata = bfd_zalloc (abfd, sizeof (*sdata));
1417
783k
  if (sdata == NULL)
1418
0
    return false;
1419
783k
  sec->used_by_bfd = sdata;
1420
1421
783k
  return _bfd_elf_new_section_hook (abfd, sec);
1422
783k
}
1423

1424
/* Read ECOFF debugging information from a .mdebug section into a
1425
   ecoff_debug_info structure.  */
1426
1427
bool
1428
_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1429
             struct ecoff_debug_info *debug)
1430
1.37k
{
1431
1.37k
  HDRR *symhdr;
1432
1.37k
  const struct ecoff_debug_swap *swap;
1433
1.37k
  char *ext_hdr;
1434
1435
1.37k
  swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1436
1.37k
  memset (debug, 0, sizeof (*debug));
1437
1438
1.37k
  ext_hdr = bfd_malloc (swap->external_hdr_size);
1439
1.37k
  if (ext_hdr == NULL && swap->external_hdr_size != 0)
1440
0
    goto error_return;
1441
1442
1.37k
  if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1443
1.37k
          swap->external_hdr_size))
1444
78
    goto error_return;
1445
1446
1.29k
  symhdr = &debug->symbolic_header;
1447
1.29k
  (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1448
1.29k
  free (ext_hdr);
1449
1.29k
  ext_hdr = NULL;
1450
1451
  /* The symbolic header contains absolute file offsets and sizes to
1452
     read.  */
1453
1.29k
#define READ(ptr, offset, count, size)          \
1454
7.65k
  do                  \
1455
7.65k
    {                 \
1456
7.65k
      size_t amt;             \
1457
7.65k
      debug->ptr = NULL;            \
1458
7.65k
      if (symhdr->count == 0)           \
1459
7.65k
  break;               \
1460
7.65k
      if (_bfd_mul_overflow (size, symhdr->count, &amt))   \
1461
2.91k
  {               \
1462
0
    bfd_set_error (bfd_error_file_too_big);     \
1463
0
    goto error_return;            \
1464
0
  }                \
1465
2.91k
      if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0)   \
1466
2.91k
  goto error_return;           \
1467
2.91k
      debug->ptr = _bfd_malloc_and_read (abfd, amt + 1, amt);   \
1468
2.38k
      if (debug->ptr == NULL)           \
1469
2.38k
  goto error_return;           \
1470
2.38k
      ((char *) debug->ptr)[amt] = 0;         \
1471
1.72k
    } while (0)
1472
1473
1.29k
  READ (line, cbLineOffset, cbLine, sizeof (unsigned char));
1474
1.01k
  READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size);
1475
874
  READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size);
1476
802
  READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size);
1477
785
  READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size);
1478
702
  READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext));
1479
587
  READ (ss, cbSsOffset, issMax, sizeof (char));
1480
500
  READ (ssext, cbSsExtOffset, issExtMax, sizeof (char));
1481
442
  READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size);
1482
369
  READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size);
1483
281
  READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size);
1484
104
#undef READ
1485
1486
104
  return true;
1487
1488
1.27k
 error_return:
1489
1.27k
  free (ext_hdr);
1490
1.27k
  _bfd_ecoff_free_ecoff_debug_info (debug);
1491
1.27k
  return false;
1492
281
}
1493

1494
/* Swap RPDR (runtime procedure table entry) for output.  */
1495
1496
static void
1497
ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1498
0
{
1499
0
  H_PUT_S32 (abfd, in->adr, ex->p_adr);
1500
0
  H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1501
0
  H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1502
0
  H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1503
0
  H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1504
0
  H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1505
1506
0
  H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1507
0
  H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1508
1509
0
  H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1510
0
}
1511
1512
/* Create a runtime procedure table from the .mdebug section.  */
1513
1514
static bool
1515
mips_elf_create_procedure_table (void *handle, bfd *abfd,
1516
         struct bfd_link_info *info, asection *s,
1517
         struct ecoff_debug_info *debug)
1518
0
{
1519
0
  const struct ecoff_debug_swap *swap;
1520
0
  HDRR *hdr = &debug->symbolic_header;
1521
0
  RPDR *rpdr, *rp;
1522
0
  struct rpdr_ext *erp;
1523
0
  void *rtproc;
1524
0
  struct pdr_ext *epdr;
1525
0
  struct sym_ext *esym;
1526
0
  char *ss, **sv;
1527
0
  char *str;
1528
0
  bfd_size_type size;
1529
0
  bfd_size_type count;
1530
0
  unsigned long sindex;
1531
0
  unsigned long i;
1532
0
  PDR pdr;
1533
0
  SYMR sym;
1534
0
  const char *no_name_func = _("static procedure (no name)");
1535
1536
0
  epdr = NULL;
1537
0
  rpdr = NULL;
1538
0
  esym = NULL;
1539
0
  ss = NULL;
1540
0
  sv = NULL;
1541
1542
0
  swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1543
1544
0
  sindex = strlen (no_name_func) + 1;
1545
0
  count = hdr->ipdMax;
1546
0
  if (count > 0)
1547
0
    {
1548
0
      size = swap->external_pdr_size;
1549
1550
0
      epdr = bfd_malloc (size * count);
1551
0
      if (epdr == NULL)
1552
0
  goto error_return;
1553
1554
0
      if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1555
0
  goto error_return;
1556
1557
0
      size = sizeof (RPDR);
1558
0
      rp = rpdr = bfd_malloc (size * count);
1559
0
      if (rpdr == NULL)
1560
0
  goto error_return;
1561
1562
0
      size = sizeof (char *);
1563
0
      sv = bfd_malloc (size * count);
1564
0
      if (sv == NULL)
1565
0
  goto error_return;
1566
1567
0
      count = hdr->isymMax;
1568
0
      size = swap->external_sym_size;
1569
0
      esym = bfd_malloc (size * count);
1570
0
      if (esym == NULL)
1571
0
  goto error_return;
1572
1573
0
      if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1574
0
  goto error_return;
1575
1576
0
      count = hdr->issMax;
1577
0
      ss = bfd_malloc (count);
1578
0
      if (ss == NULL)
1579
0
  goto error_return;
1580
0
      if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1581
0
  goto error_return;
1582
1583
0
      count = hdr->ipdMax;
1584
0
      for (i = 0; i < (unsigned long) count; i++, rp++)
1585
0
  {
1586
0
    (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1587
0
    (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1588
0
    rp->adr = sym.value;
1589
0
    rp->regmask = pdr.regmask;
1590
0
    rp->regoffset = pdr.regoffset;
1591
0
    rp->fregmask = pdr.fregmask;
1592
0
    rp->fregoffset = pdr.fregoffset;
1593
0
    rp->frameoffset = pdr.frameoffset;
1594
0
    rp->framereg = pdr.framereg;
1595
0
    rp->pcreg = pdr.pcreg;
1596
0
    rp->irpss = sindex;
1597
0
    sv[i] = ss + sym.iss;
1598
0
    sindex += strlen (sv[i]) + 1;
1599
0
  }
1600
0
    }
1601
1602
0
  size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1603
0
  size = BFD_ALIGN (size, 16);
1604
0
  rtproc = bfd_alloc (abfd, size);
1605
0
  if (rtproc == NULL)
1606
0
    {
1607
0
      mips_elf_hash_table (info)->procedure_count = 0;
1608
0
      goto error_return;
1609
0
    }
1610
1611
0
  mips_elf_hash_table (info)->procedure_count = count + 2;
1612
1613
0
  erp = rtproc;
1614
0
  memset (erp, 0, sizeof (struct rpdr_ext));
1615
0
  erp++;
1616
0
  str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1617
0
  strcpy (str, no_name_func);
1618
0
  str += strlen (no_name_func) + 1;
1619
0
  for (i = 0; i < count; i++)
1620
0
    {
1621
0
      ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1622
0
      strcpy (str, sv[i]);
1623
0
      str += strlen (sv[i]) + 1;
1624
0
    }
1625
0
  H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1626
1627
  /* Set the size and contents of .rtproc section.  */
1628
0
  s->size = size;
1629
0
  s->contents = rtproc;
1630
1631
  /* Skip this section later on (I don't think this currently
1632
     matters, but someday it might).  */
1633
0
  s->map_head.link_order = NULL;
1634
1635
0
  free (epdr);
1636
0
  free (rpdr);
1637
0
  free (esym);
1638
0
  free (ss);
1639
0
  free (sv);
1640
0
  return true;
1641
1642
0
 error_return:
1643
0
  free (epdr);
1644
0
  free (rpdr);
1645
0
  free (esym);
1646
0
  free (ss);
1647
0
  free (sv);
1648
0
  return false;
1649
0
}
1650

1651
/* We're going to create a stub for H.  Create a symbol for the stub's
1652
   value and size, to help make the disassembly easier to read.  */
1653
1654
static bool
1655
mips_elf_create_stub_symbol (struct bfd_link_info *info,
1656
           struct mips_elf_link_hash_entry *h,
1657
           const char *prefix, asection *s, bfd_vma value,
1658
           bfd_vma size)
1659
0
{
1660
0
  bool micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
1661
0
  struct bfd_link_hash_entry *bh;
1662
0
  struct elf_link_hash_entry *elfh;
1663
0
  char *name;
1664
0
  bool res;
1665
1666
0
  if (micromips_p)
1667
0
    value |= 1;
1668
1669
  /* Create a new symbol.  */
1670
0
  name = concat (prefix, h->root.root.root.string, NULL);
1671
0
  bh = NULL;
1672
0
  res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1673
0
            BSF_LOCAL, s, value, NULL,
1674
0
            true, false, &bh);
1675
0
  free (name);
1676
0
  if (! res)
1677
0
    return false;
1678
1679
  /* Make it a local function.  */
1680
0
  elfh = (struct elf_link_hash_entry *) bh;
1681
0
  elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1682
0
  elfh->size = size;
1683
0
  elfh->forced_local = 1;
1684
0
  if (micromips_p)
1685
0
    elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
1686
0
  return true;
1687
0
}
1688
1689
/* We're about to redefine H.  Create a symbol to represent H's
1690
   current value and size, to help make the disassembly easier
1691
   to read.  */
1692
1693
static bool
1694
mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1695
             struct mips_elf_link_hash_entry *h,
1696
             const char *prefix)
1697
0
{
1698
0
  struct bfd_link_hash_entry *bh;
1699
0
  struct elf_link_hash_entry *elfh;
1700
0
  char *name;
1701
0
  asection *s;
1702
0
  bfd_vma value;
1703
0
  bool res;
1704
1705
  /* Read the symbol's value.  */
1706
0
  BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1707
0
        || h->root.root.type == bfd_link_hash_defweak);
1708
0
  s = h->root.root.u.def.section;
1709
0
  value = h->root.root.u.def.value;
1710
1711
  /* Create a new symbol.  */
1712
0
  name = concat (prefix, h->root.root.root.string, NULL);
1713
0
  bh = NULL;
1714
0
  res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1715
0
            BSF_LOCAL, s, value, NULL,
1716
0
            true, false, &bh);
1717
0
  free (name);
1718
0
  if (! res)
1719
0
    return false;
1720
1721
  /* Make it local and copy the other attributes from H.  */
1722
0
  elfh = (struct elf_link_hash_entry *) bh;
1723
0
  elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1724
0
  elfh->other = h->root.other;
1725
0
  elfh->size = h->root.size;
1726
0
  elfh->forced_local = 1;
1727
0
  return true;
1728
0
}
1729
1730
/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1731
   function rather than to a hard-float stub.  */
1732
1733
static bool
1734
section_allows_mips16_refs_p (asection *section)
1735
0
{
1736
0
  const char *name;
1737
1738
0
  name = bfd_section_name (section);
1739
0
  return (FN_STUB_P (name)
1740
0
    || CALL_STUB_P (name)
1741
0
    || CALL_FP_STUB_P (name)
1742
0
    || strcmp (name, ".pdr") == 0);
1743
0
}
1744
1745
/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1746
   stub section of some kind.  Return the R_SYMNDX of the target
1747
   function, or 0 if we can't decide which function that is.  */
1748
1749
static unsigned long
1750
mips16_stub_symndx (const struct elf_backend_data *bed,
1751
        asection *sec ATTRIBUTE_UNUSED,
1752
        const Elf_Internal_Rela *relocs,
1753
        const Elf_Internal_Rela *relend)
1754
0
{
1755
0
  int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1756
0
  const Elf_Internal_Rela *rel;
1757
1758
  /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1759
     one in a compound relocation.  */
1760
0
  for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1761
0
    if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1762
0
      return ELF_R_SYM (sec->owner, rel->r_info);
1763
1764
  /* Otherwise trust the first relocation, whatever its kind.  This is
1765
     the traditional behavior.  */
1766
0
  if (relocs < relend)
1767
0
    return ELF_R_SYM (sec->owner, relocs->r_info);
1768
1769
0
  return 0;
1770
0
}
1771
1772
/* Check the mips16 stubs for a particular symbol, and see if we can
1773
   discard them.  */
1774
1775
static void
1776
mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1777
           struct mips_elf_link_hash_entry *h)
1778
0
{
1779
  /* Dynamic symbols must use the standard call interface, in case other
1780
     objects try to call them.  */
1781
0
  if (h->fn_stub != NULL
1782
0
      && h->root.dynindx != -1)
1783
0
    {
1784
0
      mips_elf_create_shadow_symbol (info, h, ".mips16.");
1785
0
      h->need_fn_stub = true;
1786
0
    }
1787
1788
0
  if (h->fn_stub != NULL
1789
0
      && ! h->need_fn_stub)
1790
0
    {
1791
      /* We don't need the fn_stub; the only references to this symbol
1792
   are 16 bit calls.  Clobber the size to 0 to prevent it from
1793
   being included in the link.  */
1794
0
      h->fn_stub->size = 0;
1795
0
      h->fn_stub->flags &= ~SEC_RELOC;
1796
0
      h->fn_stub->reloc_count = 0;
1797
0
      h->fn_stub->flags |= SEC_EXCLUDE;
1798
0
      h->fn_stub->output_section = bfd_abs_section_ptr;
1799
0
    }
1800
1801
0
  if (h->call_stub != NULL
1802
0
      && ELF_ST_IS_MIPS16 (h->root.other))
1803
0
    {
1804
      /* We don't need the call_stub; this is a 16 bit function, so
1805
   calls from other 16 bit functions are OK.  Clobber the size
1806
   to 0 to prevent it from being included in the link.  */
1807
0
      h->call_stub->size = 0;
1808
0
      h->call_stub->flags &= ~SEC_RELOC;
1809
0
      h->call_stub->reloc_count = 0;
1810
0
      h->call_stub->flags |= SEC_EXCLUDE;
1811
0
      h->call_stub->output_section = bfd_abs_section_ptr;
1812
0
    }
1813
1814
0
  if (h->call_fp_stub != NULL
1815
0
      && ELF_ST_IS_MIPS16 (h->root.other))
1816
0
    {
1817
      /* We don't need the call_stub; this is a 16 bit function, so
1818
   calls from other 16 bit functions are OK.  Clobber the size
1819
   to 0 to prevent it from being included in the link.  */
1820
0
      h->call_fp_stub->size = 0;
1821
0
      h->call_fp_stub->flags &= ~SEC_RELOC;
1822
0
      h->call_fp_stub->reloc_count = 0;
1823
0
      h->call_fp_stub->flags |= SEC_EXCLUDE;
1824
0
      h->call_fp_stub->output_section = bfd_abs_section_ptr;
1825
0
    }
1826
0
}
1827
1828
/* Hashtable callbacks for mips_elf_la25_stubs.  */
1829
1830
static hashval_t
1831
mips_elf_la25_stub_hash (const void *entry_)
1832
0
{
1833
0
  const struct mips_elf_la25_stub *entry;
1834
1835
0
  entry = (struct mips_elf_la25_stub *) entry_;
1836
0
  return entry->h->root.root.u.def.section->id
1837
0
    + entry->h->root.root.u.def.value;
1838
0
}
1839
1840
static int
1841
mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1842
0
{
1843
0
  const struct mips_elf_la25_stub *entry1, *entry2;
1844
1845
0
  entry1 = (struct mips_elf_la25_stub *) entry1_;
1846
0
  entry2 = (struct mips_elf_la25_stub *) entry2_;
1847
0
  return ((entry1->h->root.root.u.def.section
1848
0
     == entry2->h->root.root.u.def.section)
1849
0
    && (entry1->h->root.root.u.def.value
1850
0
        == entry2->h->root.root.u.def.value));
1851
0
}
1852
1853
/* Called by the linker to set up the la25 stub-creation code.  FN is
1854
   the linker's implementation of add_stub_function.  Return true on
1855
   success.  */
1856
1857
bool
1858
_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1859
        asection *(*fn) (const char *, asection *,
1860
             asection *))
1861
0
{
1862
0
  struct mips_elf_link_hash_table *htab;
1863
1864
0
  htab = mips_elf_hash_table (info);
1865
0
  if (htab == NULL)
1866
0
    return false;
1867
1868
0
  htab->add_stub_section = fn;
1869
0
  htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1870
0
              mips_elf_la25_stub_eq, NULL);
1871
0
  if (htab->la25_stubs == NULL)
1872
0
    return false;
1873
1874
0
  return true;
1875
0
}
1876
1877
/* Return true if H is a locally-defined PIC function, in the sense
1878
   that it or its fn_stub might need $25 to be valid on entry.
1879
   Note that MIPS16 functions set up $gp using PC-relative instructions,
1880
   so they themselves never need $25 to be valid.  Only non-MIPS16
1881
   entry points are of interest here.  */
1882
1883
static bool
1884
mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1885
0
{
1886
0
  return ((h->root.root.type == bfd_link_hash_defined
1887
0
     || h->root.root.type == bfd_link_hash_defweak)
1888
0
    && h->root.def_regular
1889
0
    && !bfd_is_abs_section (h->root.root.u.def.section)
1890
0
    && !bfd_is_und_section (h->root.root.u.def.section)
1891
0
    && (!ELF_ST_IS_MIPS16 (h->root.other)
1892
0
        || (h->fn_stub && h->need_fn_stub))
1893
0
    && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1894
0
        || ELF_ST_IS_MIPS_PIC (h->root.other)));
1895
0
}
1896
1897
/* Set *SEC to the input section that contains the target of STUB.
1898
   Return the offset of the target from the start of that section.  */
1899
1900
static bfd_vma
1901
mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1902
        asection **sec)
1903
0
{
1904
0
  if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1905
0
    {
1906
0
      BFD_ASSERT (stub->h->need_fn_stub);
1907
0
      *sec = stub->h->fn_stub;
1908
0
      return 0;
1909
0
    }
1910
0
  else
1911
0
    {
1912
0
      *sec = stub->h->root.root.u.def.section;
1913
0
      return stub->h->root.root.u.def.value;
1914
0
    }
1915
0
}
1916
1917
/* STUB describes an la25 stub that we have decided to implement
1918
   by inserting an LUI/ADDIU pair before the target function.
1919
   Create the section and redirect the function symbol to it.  */
1920
1921
static bool
1922
mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1923
       struct bfd_link_info *info)
1924
0
{
1925
0
  struct mips_elf_link_hash_table *htab;
1926
0
  char *name;
1927
0
  asection *s, *input_section;
1928
0
  unsigned int align;
1929
1930
0
  htab = mips_elf_hash_table (info);
1931
0
  if (htab == NULL)
1932
0
    return false;
1933
1934
  /* Create a unique name for the new section.  */
1935
0
  name = bfd_malloc (11 + sizeof (".text.stub."));
1936
0
  if (name == NULL)
1937
0
    return false;
1938
0
  sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1939
1940
  /* Create the section.  */
1941
0
  mips_elf_get_la25_target (stub, &input_section);
1942
0
  s = htab->add_stub_section (name, input_section,
1943
0
            input_section->output_section);
1944
0
  if (s == NULL)
1945
0
    return false;
1946
1947
  /* Make sure that any padding goes before the stub.  */
1948
0
  align = input_section->alignment_power;
1949
0
  if (!bfd_link_align_section (s, align))
1950
0
    return false;
1951
0
  if (align > 3)
1952
0
    s->size = (1 << align) - 8;
1953
1954
  /* Create a symbol for the stub.  */
1955
0
  mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1956
0
  stub->stub_section = s;
1957
0
  stub->offset = s->size;
1958
1959
  /* Allocate room for it.  */
1960
0
  s->size += 8;
1961
0
  return true;
1962
0
}
1963
1964
/* STUB describes an la25 stub that we have decided to implement
1965
   with a separate trampoline.  Allocate room for it and redirect
1966
   the function symbol to it.  */
1967
1968
static bool
1969
mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1970
            struct bfd_link_info *info)
1971
0
{
1972
0
  struct mips_elf_link_hash_table *htab;
1973
0
  asection *s;
1974
1975
0
  htab = mips_elf_hash_table (info);
1976
0
  if (htab == NULL)
1977
0
    return false;
1978
1979
  /* Create a trampoline section, if we haven't already.  */
1980
0
  s = htab->strampoline;
1981
0
  if (s == NULL)
1982
0
    {
1983
0
      asection *input_section = stub->h->root.root.u.def.section;
1984
0
      s = htab->add_stub_section (".text", NULL,
1985
0
          input_section->output_section);
1986
0
      if (s == NULL || !bfd_link_align_section (s, 4))
1987
0
  return false;
1988
0
      htab->strampoline = s;
1989
0
    }
1990
1991
  /* Create a symbol for the stub.  */
1992
0
  mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1993
0
  stub->stub_section = s;
1994
0
  stub->offset = s->size;
1995
1996
  /* Allocate room for it.  */
1997
0
  s->size += 16;
1998
0
  return true;
1999
0
}
2000
2001
/* H describes a symbol that needs an la25 stub.  Make sure that an
2002
   appropriate stub exists and point H at it.  */
2003
2004
static bool
2005
mips_elf_add_la25_stub (struct bfd_link_info *info,
2006
      struct mips_elf_link_hash_entry *h)
2007
0
{
2008
0
  struct mips_elf_link_hash_table *htab;
2009
0
  struct mips_elf_la25_stub search, *stub;
2010
0
  bool use_trampoline_p;
2011
0
  asection *s;
2012
0
  bfd_vma value;
2013
0
  void **slot;
2014
2015
  /* Describe the stub we want.  */
2016
0
  search.stub_section = NULL;
2017
0
  search.offset = 0;
2018
0
  search.h = h;
2019
2020
  /* See if we've already created an equivalent stub.  */
2021
0
  htab = mips_elf_hash_table (info);
2022
0
  if (htab == NULL)
2023
0
    return false;
2024
2025
0
  slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
2026
0
  if (slot == NULL)
2027
0
    return false;
2028
2029
0
  stub = (struct mips_elf_la25_stub *) *slot;
2030
0
  if (stub != NULL)
2031
0
    {
2032
      /* We can reuse the existing stub.  */
2033
0
      h->la25_stub = stub;
2034
0
      return true;
2035
0
    }
2036
2037
  /* Create a permanent copy of ENTRY and add it to the hash table.  */
2038
0
  stub = bfd_malloc (sizeof (search));
2039
0
  if (stub == NULL)
2040
0
    return false;
2041
0
  *stub = search;
2042
0
  *slot = stub;
2043
2044
  /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
2045
     of the section and if we would need no more than 2 nops.  */
2046
0
  value = mips_elf_get_la25_target (stub, &s);
2047
0
  if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
2048
0
    value &= ~1;
2049
0
  use_trampoline_p = (value != 0 || s->alignment_power > 4);
2050
2051
0
  h->la25_stub = stub;
2052
0
  return (use_trampoline_p
2053
0
    ? mips_elf_add_la25_trampoline (stub, info)
2054
0
    : mips_elf_add_la25_intro (stub, info));
2055
0
}
2056
2057
/* A mips_elf_link_hash_traverse callback that is called before sizing
2058
   sections.  DATA points to a mips_htab_traverse_info structure.  */
2059
2060
static bool
2061
mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
2062
0
{
2063
0
  struct mips_htab_traverse_info *hti;
2064
2065
0
  hti = (struct mips_htab_traverse_info *) data;
2066
0
  if (!bfd_link_relocatable (hti->info))
2067
0
    mips_elf_check_mips16_stubs (hti->info, h);
2068
2069
0
  if (mips_elf_local_pic_function_p (h))
2070
0
    {
2071
      /* PR 12845: If H is in a section that has been garbage
2072
   collected it will have its output section set to *ABS*.  */
2073
0
      if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
2074
0
  return true;
2075
2076
      /* H is a function that might need $25 to be valid on entry.
2077
   If we're creating a non-PIC relocatable object, mark H as
2078
   being PIC.  If we're creating a non-relocatable object with
2079
   non-PIC branches and jumps to H, make sure that H has an la25
2080
   stub.  */
2081
0
      if (bfd_link_relocatable (hti->info))
2082
0
  {
2083
0
    if (!PIC_OBJECT_P (hti->output_bfd))
2084
0
      h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
2085
0
  }
2086
0
      else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
2087
0
  {
2088
0
    hti->error = true;
2089
0
    return false;
2090
0
  }
2091
0
    }
2092
0
  return true;
2093
0
}
2094

2095
/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2096
   Most mips16 instructions are 16 bits, but these instructions
2097
   are 32 bits.
2098
2099
   The format of these instructions is:
2100
2101
   +--------------+--------------------------------+
2102
   |     JALX     | X|   Imm 20:16  |   Imm 25:21  |
2103
   +--------------+--------------------------------+
2104
   |        Immediate  15:0      |
2105
   +-----------------------------------------------+
2106
2107
   JALX is the 5-bit value 00011.  X is 0 for jal, 1 for jalx.
2108
   Note that the immediate value in the first word is swapped.
2109
2110
   When producing a relocatable object file, R_MIPS16_26 is
2111
   handled mostly like R_MIPS_26.  In particular, the addend is
2112
   stored as a straight 26-bit value in a 32-bit instruction.
2113
   (gas makes life simpler for itself by never adjusting a
2114
   R_MIPS16_26 reloc to be against a section, so the addend is
2115
   always zero).  However, the 32 bit instruction is stored as 2
2116
   16-bit values, rather than a single 32-bit value.  In a
2117
   big-endian file, the result is the same; in a little-endian
2118
   file, the two 16-bit halves of the 32 bit value are swapped.
2119
   This is so that a disassembler can recognize the jal
2120
   instruction.
2121
2122
   When doing a final link, R_MIPS16_26 is treated as a 32 bit
2123
   instruction stored as two 16-bit values.  The addend A is the
2124
   contents of the targ26 field.  The calculation is the same as
2125
   R_MIPS_26.  When storing the calculated value, reorder the
2126
   immediate value as shown above, and don't forget to store the
2127
   value as two 16-bit values.
2128
2129
   To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2130
   defined as
2131
2132
   big-endian:
2133
   +--------+----------------------+
2134
   |      |        |
2135
   |      |  targ26-16     |
2136
   |31    26|25       0|
2137
   +--------+----------------------+
2138
2139
   little-endian:
2140
   +----------+------+-------------+
2141
   |        |      |       |
2142
   |  sub1    |      |     sub2    |
2143
   |0      9|10  15|16   31|
2144
   +----------+--------------------+
2145
   where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2146
   ((sub1 << 16) | sub2)).
2147
2148
   When producing a relocatable object file, the calculation is
2149
   (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2150
   When producing a fully linked file, the calculation is
2151
   let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2152
   ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2153
2154
   The table below lists the other MIPS16 instruction relocations.
2155
   Each one is calculated in the same way as the non-MIPS16 relocation
2156
   given on the right, but using the extended MIPS16 layout of 16-bit
2157
   immediate fields:
2158
2159
  R_MIPS16_GPREL    R_MIPS_GPREL16
2160
  R_MIPS16_GOT16    R_MIPS_GOT16
2161
  R_MIPS16_CALL16   R_MIPS_CALL16
2162
  R_MIPS16_HI16   R_MIPS_HI16
2163
  R_MIPS16_LO16   R_MIPS_LO16
2164
2165
   A typical instruction will have a format like this:
2166
2167
   +--------------+--------------------------------+
2168
   |    EXTEND    |     Imm 10:5    |   Imm 15:11  |
2169
   +--------------+--------------------------------+
2170
   |    Major     |   rx   |   ry   |   Imm  4:0   |
2171
   +--------------+--------------------------------+
2172
2173
   EXTEND is the five bit value 11110.  Major is the instruction
2174
   opcode.
2175
2176
   All we need to do here is shuffle the bits appropriately.
2177
   As above, the two 16-bit halves must be swapped on a
2178
   little-endian system.
2179
2180
   Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
2181
   relocatable field is shifted by 1 rather than 2 and the same bit
2182
   shuffling is done as with the relocations above.  */
2183
2184
static inline bool
2185
mips16_reloc_p (int r_type)
2186
808
{
2187
808
  switch (r_type)
2188
808
    {
2189
26
    case R_MIPS16_26:
2190
26
    case R_MIPS16_GPREL:
2191
54
    case R_MIPS16_GOT16:
2192
56
    case R_MIPS16_CALL16:
2193
56
    case R_MIPS16_HI16:
2194
72
    case R_MIPS16_LO16:
2195
72
    case R_MIPS16_TLS_GD:
2196
72
    case R_MIPS16_TLS_LDM:
2197
72
    case R_MIPS16_TLS_DTPREL_HI16:
2198
72
    case R_MIPS16_TLS_DTPREL_LO16:
2199
74
    case R_MIPS16_TLS_GOTTPREL:
2200
74
    case R_MIPS16_TLS_TPREL_HI16:
2201
74
    case R_MIPS16_TLS_TPREL_LO16:
2202
74
    case R_MIPS16_PC16_S1:
2203
74
      return true;
2204
2205
734
    default:
2206
734
      return false;
2207
808
    }
2208
808
}
2209
2210
/* Check if a microMIPS reloc.  */
2211
2212
static inline bool
2213
micromips_reloc_p (unsigned int r_type)
2214
854
{
2215
854
  return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2216
854
}
2217
2218
/* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2219
   on a little-endian system.  This does not apply to R_MICROMIPS_PC7_S1,
2220
   R_MICROMIPS_PC10_S1 and R_MICROMIPS_GPREL7_S2 relocs that apply to
2221
   16-bit instructions.  */
2222
2223
static inline bool
2224
micromips_reloc_shuffle_p (unsigned int r_type)
2225
734
{
2226
734
  return (micromips_reloc_p (r_type)
2227
734
    && r_type != R_MICROMIPS_PC7_S1
2228
734
    && r_type != R_MICROMIPS_PC10_S1
2229
734
    && r_type != R_MICROMIPS_GPREL7_S2);
2230
734
}
2231
2232
static inline bool
2233
got16_reloc_p (int r_type)
2234
0
{
2235
0
  return (r_type == R_MIPS_GOT16
2236
0
    || r_type == R_MIPS16_GOT16
2237
0
    || r_type == R_MICROMIPS_GOT16);
2238
0
}
2239
2240
static inline bool
2241
call16_reloc_p (int r_type)
2242
0
{
2243
0
  return (r_type == R_MIPS_CALL16
2244
0
    || r_type == R_MIPS16_CALL16
2245
0
    || r_type == R_MICROMIPS_CALL16);
2246
0
}
2247
2248
static inline bool
2249
got_disp_reloc_p (unsigned int r_type)
2250
0
{
2251
0
  return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2252
0
}
2253
2254
static inline bool
2255
got_page_reloc_p (unsigned int r_type)
2256
0
{
2257
0
  return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2258
0
}
2259
2260
static inline bool
2261
got_lo16_reloc_p (unsigned int r_type)
2262
0
{
2263
0
  return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2264
0
}
2265
2266
static inline bool
2267
call_hi16_reloc_p (unsigned int r_type)
2268
0
{
2269
0
  return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2270
0
}
2271
2272
static inline bool
2273
call_lo16_reloc_p (unsigned int r_type)
2274
0
{
2275
0
  return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2276
0
}
2277
2278
static inline bool
2279
hi16_reloc_p (int r_type)
2280
0
{
2281
0
  return (r_type == R_MIPS_HI16
2282
0
    || r_type == R_MIPS16_HI16
2283
0
    || r_type == R_MICROMIPS_HI16
2284
0
    || r_type == R_MIPS_PCHI16);
2285
0
}
2286
2287
static inline bool
2288
lo16_reloc_p (int r_type)
2289
0
{
2290
0
  return (r_type == R_MIPS_LO16
2291
0
    || r_type == R_MIPS16_LO16
2292
0
    || r_type == R_MICROMIPS_LO16
2293
0
    || r_type == R_MIPS_PCLO16);
2294
0
}
2295
2296
static inline bool
2297
mips16_call_reloc_p (int r_type)
2298
0
{
2299
0
  return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2300
0
}
2301
2302
static inline bool
2303
jal_reloc_p (int r_type)
2304
0
{
2305
0
  return (r_type == R_MIPS_26
2306
0
    || r_type == R_MIPS16_26
2307
0
    || r_type == R_MICROMIPS_26_S1);
2308
0
}
2309
2310
static inline bool
2311
b_reloc_p (int r_type)
2312
0
{
2313
0
  return (r_type == R_MIPS_PC26_S2
2314
0
    || r_type == R_MIPS_PC21_S2
2315
0
    || r_type == R_MIPS_PC16
2316
0
    || r_type == R_MIPS_GNU_REL16_S2
2317
0
    || r_type == R_MIPS16_PC16_S1
2318
0
    || r_type == R_MICROMIPS_PC16_S1
2319
0
    || r_type == R_MICROMIPS_PC10_S1
2320
0
    || r_type == R_MICROMIPS_PC7_S1);
2321
0
}
2322
2323
static inline bool
2324
aligned_pcrel_reloc_p (int r_type)
2325
0
{
2326
0
  return (r_type == R_MIPS_PC18_S3
2327
0
    || r_type == R_MIPS_PC19_S2);
2328
0
}
2329
2330
static inline bool
2331
branch_reloc_p (int r_type)
2332
0
{
2333
0
  return (r_type == R_MIPS_26
2334
0
    || r_type == R_MIPS_PC26_S2
2335
0
    || r_type == R_MIPS_PC21_S2
2336
0
    || r_type == R_MIPS_PC16
2337
0
    || r_type == R_MIPS_GNU_REL16_S2);
2338
0
}
2339
2340
static inline bool
2341
mips16_branch_reloc_p (int r_type)
2342
0
{
2343
0
  return (r_type == R_MIPS16_26
2344
0
    || r_type == R_MIPS16_PC16_S1);
2345
0
}
2346
2347
static inline bool
2348
micromips_branch_reloc_p (int r_type)
2349
0
{
2350
0
  return (r_type == R_MICROMIPS_26_S1
2351
0
    || r_type == R_MICROMIPS_PC16_S1
2352
0
    || r_type == R_MICROMIPS_PC10_S1
2353
0
    || r_type == R_MICROMIPS_PC7_S1);
2354
0
}
2355
2356
static inline bool
2357
tls_gd_reloc_p (unsigned int r_type)
2358
0
{
2359
0
  return (r_type == R_MIPS_TLS_GD
2360
0
    || r_type == R_MIPS16_TLS_GD
2361
0
    || r_type == R_MICROMIPS_TLS_GD);
2362
0
}
2363
2364
static inline bool
2365
tls_ldm_reloc_p (unsigned int r_type)
2366
0
{
2367
0
  return (r_type == R_MIPS_TLS_LDM
2368
0
    || r_type == R_MIPS16_TLS_LDM
2369
0
    || r_type == R_MICROMIPS_TLS_LDM);
2370
0
}
2371
2372
static inline bool
2373
tls_gottprel_reloc_p (unsigned int r_type)
2374
0
{
2375
0
  return (r_type == R_MIPS_TLS_GOTTPREL
2376
0
    || r_type == R_MIPS16_TLS_GOTTPREL
2377
0
    || r_type == R_MICROMIPS_TLS_GOTTPREL);
2378
0
}
2379
2380
static inline bool
2381
needs_shuffle (int r_type)
2382
808
{
2383
808
  return mips16_reloc_p (r_type) || micromips_reloc_shuffle_p (r_type);
2384
808
}
2385
2386
void
2387
_bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2388
             bool jal_shuffle, bfd_byte *data)
2389
403
{
2390
403
  bfd_vma first, second, val;
2391
2392
403
  if (!needs_shuffle (r_type))
2393
343
    return;
2394
2395
  /* Pick up the first and second halfwords of the instruction.  */
2396
60
  first = bfd_get_16 (abfd, data);
2397
60
  second = bfd_get_16 (abfd, data + 2);
2398
60
  if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2399
36
    val = first << 16 | second;
2400
24
  else if (r_type != R_MIPS16_26)
2401
24
    val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2402
24
     | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2403
0
  else
2404
0
    val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2405
0
     | ((first & 0x1f) << 21) | second);
2406
60
  bfd_put_32 (abfd, val, data);
2407
60
}
2408
2409
void
2410
_bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2411
           bool jal_shuffle, bfd_byte *data)
2412
403
{
2413
403
  bfd_vma first, second, val;
2414
2415
403
  if (!needs_shuffle (r_type))
2416
343
    return;
2417
2418
60
  val = bfd_get_32 (abfd, data);
2419
60
  if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2420
36
    {
2421
36
      second = val & 0xffff;
2422
36
      first = val >> 16;
2423
36
    }
2424
24
  else if (r_type != R_MIPS16_26)
2425
24
    {
2426
24
      second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2427
24
      first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2428
24
    }
2429
0
  else
2430
0
    {
2431
0
      second = val & 0xffff;
2432
0
      first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2433
0
         | ((val >> 21) & 0x1f);
2434
0
    }
2435
60
  bfd_put_16 (abfd, second, data + 2);
2436
60
  bfd_put_16 (abfd, first, data);
2437
60
}
2438
2439
/* Perform reloc offset checking.
2440
   We can only use bfd_reloc_offset_in_range, which takes into account
2441
   the size of the field being relocated, when section contents will
2442
   be accessed because mips object files may use relocations that seem
2443
   to access beyond section limits.
2444
   gas/testsuite/gas/mips/dla-reloc.s is an example that puts
2445
   R_MIPS_SUB, a 64-bit relocation, on the last instruction in the
2446
   section.  The R_MIPS_SUB applies to the addend for the next reloc
2447
   rather than the section contents.
2448
2449
   CHECK is CHECK_STD for the standard bfd_reloc_offset_in_range check,
2450
   CHECK_INPLACE to only check partial_inplace relocs, and
2451
   CHECK_SHUFFLE to only check relocs that shuffle/unshuffle.  */
2452
2453
bool
2454
_bfd_mips_reloc_offset_in_range (bfd *abfd, asection *input_section,
2455
         arelent *reloc_entry, enum reloc_check check)
2456
442
{
2457
442
  if (check == check_inplace && !reloc_entry->howto->partial_inplace)
2458
0
    return true;
2459
442
  if (check == check_shuffle && !needs_shuffle (reloc_entry->howto->type))
2460
0
    return true;
2461
442
  return bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
2462
442
            input_section, reloc_entry->address);
2463
442
}
2464
2465
bfd_reloc_status_type
2466
_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2467
             arelent *reloc_entry, asection *input_section,
2468
             bool relocatable, void *data, bfd_vma gp)
2469
7
{
2470
7
  bfd_vma relocation;
2471
7
  bfd_signed_vma val;
2472
7
  bfd_reloc_status_type status;
2473
2474
7
  if (bfd_is_com_section (symbol->section))
2475
0
    relocation = 0;
2476
7
  else
2477
7
    relocation = symbol->value;
2478
2479
7
  if (symbol->section->output_section != NULL)
2480
7
    {
2481
7
      relocation += symbol->section->output_section->vma;
2482
7
      relocation += symbol->section->output_offset;
2483
7
    }
2484
2485
  /* Set val to the offset into the section or symbol.  */
2486
7
  val = reloc_entry->addend;
2487
2488
7
  _bfd_mips_elf_sign_extend (val, 16);
2489
2490
  /* Adjust val for the final section location and GP value.  If we
2491
     are producing relocatable output, we don't want to do this for
2492
     an external symbol.  */
2493
7
  if (! relocatable
2494
7
      || (symbol->flags & BSF_SECTION_SYM) != 0)
2495
7
    val += relocation - gp;
2496
2497
7
  if (reloc_entry->howto->partial_inplace)
2498
0
    {
2499
0
      if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2500
0
              reloc_entry->address))
2501
0
  return bfd_reloc_outofrange;
2502
2503
0
      status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2504
0
               (bfd_byte *) data
2505
0
               + reloc_entry->address);
2506
0
      if (status != bfd_reloc_ok)
2507
0
  return status;
2508
0
    }
2509
7
  else
2510
7
    reloc_entry->addend = val;
2511
2512
7
  if (relocatable)
2513
0
    reloc_entry->address += input_section->output_offset;
2514
2515
7
  return bfd_reloc_ok;
2516
7
}
2517
2518
/* A howto special_function for REL *HI16 relocations.  We can only
2519
   calculate the correct value once we've seen the partnering
2520
   *LO16 relocation, so just save the information for later.
2521
2522
   The ABI requires that the *LO16 immediately follow the *HI16.
2523
   However, as a GNU extension, we permit an arbitrary number of
2524
   *HI16s to be associated with a single *LO16.  This significantly
2525
   simplies the relocation handling in gcc.  */
2526
2527
bfd_reloc_status_type
2528
_bfd_mips_elf_hi16_reloc (bfd *abfd, arelent *reloc_entry,
2529
        asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2530
        asection *input_section, bfd *output_bfd,
2531
        char **error_message ATTRIBUTE_UNUSED)
2532
0
{
2533
0
  struct mips_hi16 *n;
2534
0
  struct mips_elf_obj_tdata *tdata;
2535
2536
0
  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2537
0
    return bfd_reloc_outofrange;
2538
2539
0
  n = bfd_malloc (sizeof *n);
2540
0
  if (n == NULL)
2541
0
    return bfd_reloc_outofrange;
2542
2543
0
  tdata = mips_elf_tdata (abfd);
2544
0
  n->next = tdata->mips_hi16_list;
2545
0
  n->data = data;
2546
0
  n->input_section = input_section;
2547
0
  n->rel = *reloc_entry;
2548
0
  tdata->mips_hi16_list = n;
2549
2550
0
  if (output_bfd != NULL)
2551
0
    reloc_entry->address += input_section->output_offset;
2552
2553
0
  return bfd_reloc_ok;
2554
0
}
2555
2556
/* A howto special_function for REL R_MIPS*_GOT16 relocations.  This is just
2557
   like any other 16-bit relocation when applied to global symbols, but is
2558
   treated in the same as R_MIPS_HI16 when applied to local symbols.  */
2559
2560
bfd_reloc_status_type
2561
_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2562
         void *data, asection *input_section,
2563
         bfd *output_bfd, char **error_message)
2564
0
{
2565
0
  if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2566
0
      || bfd_is_und_section (bfd_asymbol_section (symbol))
2567
0
      || bfd_is_com_section (bfd_asymbol_section (symbol)))
2568
    /* The relocation is against a global symbol.  */
2569
0
    return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2570
0
          input_section, output_bfd,
2571
0
          error_message);
2572
2573
0
  return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2574
0
           input_section, output_bfd, error_message);
2575
0
}
2576
2577
/* A howto special_function for REL *LO16 relocations.  The *LO16 itself
2578
   is a straightforward 16 bit inplace relocation, but we must deal with
2579
   any partnering high-part relocations as well.  */
2580
2581
bfd_reloc_status_type
2582
_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2583
        void *data, asection *input_section,
2584
        bfd *output_bfd, char **error_message)
2585
0
{
2586
0
  bfd_vma vallo;
2587
0
  bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2588
0
  struct mips_elf_obj_tdata *tdata;
2589
2590
0
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2591
0
          reloc_entry->address))
2592
0
    return bfd_reloc_outofrange;
2593
2594
0
  _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2595
0
         location);
2596
  /* The high 16 bits of the addend are stored in the high insn, the
2597
     low 16 bits in the low insn, but there is a catch:  You can't
2598
     just concatenate the high and low parts.  The high part of the
2599
     addend is adjusted for the fact that the low part is sign
2600
     extended.  For example, an addend of 0x38000 would have 0x0004 in
2601
     the high part and 0x8000 (=0xff..f8000) in the low part.
2602
     To extract the actual addend, calculate
2603
     ((hi & 0xffff) << 16) + ((lo & 0xffff) ^ 0x8000) - 0x8000.
2604
     We will be applying (symbol + addend) & 0xffff to the low insn,
2605
     and we want to apply (symbol + addend + 0x8000) >> 16 to the
2606
     high insn (the +0x8000 adjusting for when the applied low part is
2607
     negative).  */
2608
0
  vallo = ((bfd_get_32 (abfd, location) & 0xffff) ^ 0x8000) - 0x8000;
2609
0
  _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2610
0
             location);
2611
2612
0
  tdata = mips_elf_tdata (abfd);
2613
0
  while (tdata->mips_hi16_list != NULL)
2614
0
    {
2615
0
      bfd_reloc_status_type ret;
2616
0
      struct mips_hi16 *hi;
2617
2618
0
      hi = tdata->mips_hi16_list;
2619
2620
      /* R_MIPS*_GOT16 relocations are something of a special case.  We
2621
   want to install the addend in the same way as for a R_MIPS*_HI16
2622
   relocation (with a rightshift of 16).  However, since GOT16
2623
   relocations can also be used with global symbols, their howto
2624
   has a rightshift of 0.  */
2625
0
      if (hi->rel.howto->type == R_MIPS_GOT16)
2626
0
  hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, false);
2627
0
      else if (hi->rel.howto->type == R_MIPS16_GOT16)
2628
0
  hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, false);
2629
0
      else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2630
0
  hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, false);
2631
2632
0
      hi->rel.addend += vallo;
2633
2634
0
      ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2635
0
           hi->input_section, output_bfd,
2636
0
           error_message);
2637
0
      if (ret != bfd_reloc_ok)
2638
0
  return ret;
2639
2640
0
      tdata->mips_hi16_list = hi->next;
2641
0
      free (hi);
2642
0
    }
2643
2644
0
  return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2645
0
              input_section, output_bfd,
2646
0
              error_message);
2647
0
}
2648
2649
/* A generic howto special_function.  This calculates and installs the
2650
   relocation itself, thus avoiding the oft-discussed problems in
2651
   bfd_perform_relocation and bfd_install_relocation.  */
2652
2653
bfd_reloc_status_type
2654
_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2655
           asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2656
           asection *input_section, bfd *output_bfd,
2657
           char **error_message ATTRIBUTE_UNUSED)
2658
440
{
2659
440
  bfd_signed_vma val;
2660
440
  bfd_reloc_status_type status;
2661
440
  bool relocatable;
2662
2663
440
  relocatable = (output_bfd != NULL);
2664
2665
440
  if (!_bfd_mips_reloc_offset_in_range (abfd, input_section, reloc_entry,
2666
440
          (relocatable
2667
440
           ? check_inplace : check_std)))
2668
39
    return bfd_reloc_outofrange;
2669
2670
  /* Build up the field adjustment in VAL.  */
2671
401
  val = 0;
2672
401
  if ((!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2673
401
      && symbol->section->output_section != NULL)
2674
401
    {
2675
      /* Either we're calculating the final field value or we have a
2676
   relocation against a section symbol.  Add in the section's
2677
   offset or address.  */
2678
401
      val += symbol->section->output_section->vma;
2679
401
      val += symbol->section->output_offset;
2680
401
    }
2681
2682
401
  if (!relocatable)
2683
401
    {
2684
      /* We're calculating the final field value.  Add in the symbol's value
2685
   and, if pc-relative, subtract the address of the field itself.  */
2686
401
      val += symbol->value;
2687
401
      if (reloc_entry->howto->pc_relative)
2688
18
  {
2689
18
    val -= input_section->output_section->vma;
2690
18
    val -= input_section->output_offset;
2691
18
    val -= reloc_entry->address;
2692
18
  }
2693
401
    }
2694
2695
  /* VAL is now the final adjustment.  If we're keeping this relocation
2696
     in the output file, and if the relocation uses a separate addend,
2697
     we just need to add VAL to that addend.  Otherwise we need to add
2698
     VAL to the relocation field itself.  */
2699
401
  if (relocatable && !reloc_entry->howto->partial_inplace)
2700
0
    reloc_entry->addend += val;
2701
401
  else
2702
401
    {
2703
401
      bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2704
2705
      /* Add in the separate addend, if any.  */
2706
401
      val += reloc_entry->addend;
2707
2708
      /* The high 16 bits of the addend are stored in the high insn, the
2709
   low 16 bits in the low insn, but there is a catch:  You can't
2710
   just concatenate the high and low parts.  The high part of the
2711
   addend is adjusted for the fact that the low part is sign
2712
   extended.  For example, an addend of 0x38000 would have 0x0004 in
2713
   the high part and 0x8000 (=0xff..f8000) in the low part.
2714
   We will be applying (symbol + addend) & 0xffff to the low insn,
2715
   and we want to apply (symbol + addend + 0x8000) >> 16 to the
2716
   high insn (the +0x8000 adjusting for when the applied low part is
2717
   negative).  Analogously for the higher parts of a 64-bit addend.  */
2718
401
      if (reloc_entry->howto->bitsize == 16
2719
401
    && reloc_entry->howto->rightshift % 16 == 0)
2720
58
#ifdef BFD64
2721
58
  val += 0x800080008000ULL >> (48 - reloc_entry->howto->rightshift);
2722
#else
2723
  {
2724
    if (reloc_entry->howto->rightshift <= 16)
2725
      val += 0x8000 >> (16 - reloc_entry->howto->rightshift);
2726
    else
2727
      abort ();
2728
  }
2729
#endif
2730
2731
      /* Add VAL to the relocation field.  */
2732
401
      _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2733
401
             location);
2734
401
      status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2735
401
               location);
2736
401
      _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2737
401
           location);
2738
2739
401
      if (status != bfd_reloc_ok)
2740
23
  return status;
2741
401
    }
2742
2743
378
  if (relocatable)
2744
0
    reloc_entry->address += input_section->output_offset;
2745
2746
378
  return bfd_reloc_ok;
2747
401
}
2748

2749
/* Swap an entry in a .gptab section.  Note that these routines rely
2750
   on the equivalence of the two elements of the union.  */
2751
2752
static void
2753
bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2754
            Elf32_gptab *in)
2755
0
{
2756
0
  in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2757
0
  in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2758
0
}
2759
2760
static void
2761
bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2762
             Elf32_External_gptab *ex)
2763
0
{
2764
0
  H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2765
0
  H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2766
0
}
2767
2768
static void
2769
bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2770
        Elf32_External_compact_rel *ex)
2771
0
{
2772
0
  H_PUT_32 (abfd, in->id1, ex->id1);
2773
0
  H_PUT_32 (abfd, in->num, ex->num);
2774
0
  H_PUT_32 (abfd, in->id2, ex->id2);
2775
0
  H_PUT_32 (abfd, in->offset, ex->offset);
2776
0
  H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2777
0
  H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2778
0
}
2779
2780
static void
2781
bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2782
         Elf32_External_crinfo *ex)
2783
0
{
2784
0
  unsigned long l;
2785
2786
0
  l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2787
0
       | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2788
0
       | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2789
0
       | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2790
0
  H_PUT_32 (abfd, l, ex->info);
2791
0
  H_PUT_32 (abfd, in->konst, ex->konst);
2792
0
  H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2793
0
}
2794

2795
/* A .reginfo section holds a single Elf32_RegInfo structure.  These
2796
   routines swap this structure in and out.  They are used outside of
2797
   BFD, so they are globally visible.  */
2798
2799
void
2800
bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2801
        Elf32_RegInfo *in)
2802
66
{
2803
66
  in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2804
66
  in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2805
66
  in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2806
66
  in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2807
66
  in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2808
66
  in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2809
66
}
2810
2811
void
2812
bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2813
         Elf32_External_RegInfo *ex)
2814
0
{
2815
0
  H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2816
0
  H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2817
0
  H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2818
0
  H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2819
0
  H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2820
0
  H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2821
0
}
2822
2823
/* In the 64 bit ABI, the .MIPS.options section holds register
2824
   information in an Elf64_Reginfo structure.  These routines swap
2825
   them in and out.  They are globally visible because they are used
2826
   outside of BFD.  These routines are here so that gas can call them
2827
   without worrying about whether the 64 bit ABI has been included.  */
2828
2829
void
2830
bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2831
        Elf64_Internal_RegInfo *in)
2832
2.77k
{
2833
2.77k
  in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2834
2.77k
  in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2835
2.77k
  in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2836
2.77k
  in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2837
2.77k
  in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2838
2.77k
  in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2839
2.77k
  in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2840
2.77k
}
2841
2842
void
2843
bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2844
         Elf64_External_RegInfo *ex)
2845
0
{
2846
0
  H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2847
0
  H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2848
0
  H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2849
0
  H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2850
0
  H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2851
0
  H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2852
0
  H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2853
0
}
2854
2855
/* Swap in an options header.  */
2856
2857
void
2858
bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2859
            Elf_Internal_Options *in)
2860
11.1k
{
2861
11.1k
  in->kind = H_GET_8 (abfd, ex->kind);
2862
11.1k
  in->size = H_GET_8 (abfd, ex->size);
2863
11.1k
  in->section = H_GET_16 (abfd, ex->section);
2864
11.1k
  in->info = H_GET_32 (abfd, ex->info);
2865
11.1k
}
2866
2867
/* Swap out an options header.  */
2868
2869
void
2870
bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2871
             Elf_External_Options *ex)
2872
0
{
2873
0
  H_PUT_8 (abfd, in->kind, ex->kind);
2874
0
  H_PUT_8 (abfd, in->size, ex->size);
2875
0
  H_PUT_16 (abfd, in->section, ex->section);
2876
0
  H_PUT_32 (abfd, in->info, ex->info);
2877
0
}
2878
2879
/* Swap in an abiflags structure.  */
2880
2881
void
2882
bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2883
          const Elf_External_ABIFlags_v0 *ex,
2884
          Elf_Internal_ABIFlags_v0 *in)
2885
0
{
2886
0
  in->version = H_GET_16 (abfd, ex->version);
2887
0
  in->isa_level = H_GET_8 (abfd, ex->isa_level);
2888
0
  in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2889
0
  in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2890
0
  in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2891
0
  in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2892
0
  in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2893
0
  in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2894
0
  in->ases = H_GET_32 (abfd, ex->ases);
2895
0
  in->flags1 = H_GET_32 (abfd, ex->flags1);
2896
0
  in->flags2 = H_GET_32 (abfd, ex->flags2);
2897
0
}
2898
2899
/* Swap out an abiflags structure.  */
2900
2901
void
2902
bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2903
           const Elf_Internal_ABIFlags_v0 *in,
2904
           Elf_External_ABIFlags_v0 *ex)
2905
0
{
2906
0
  H_PUT_16 (abfd, in->version, ex->version);
2907
0
  H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2908
0
  H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2909
0
  H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2910
0
  H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2911
0
  H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2912
0
  H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2913
0
  H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2914
0
  H_PUT_32 (abfd, in->ases, ex->ases);
2915
0
  H_PUT_32 (abfd, in->flags1, ex->flags1);
2916
0
  H_PUT_32 (abfd, in->flags2, ex->flags2);
2917
0
}
2918

2919
/* This function is called via qsort() to sort the dynamic relocation
2920
   entries by increasing r_symndx value.  */
2921
2922
static int
2923
sort_dynamic_relocs (const void *arg1, const void *arg2)
2924
0
{
2925
0
  Elf_Internal_Rela int_reloc1;
2926
0
  Elf_Internal_Rela int_reloc2;
2927
0
  int diff;
2928
2929
0
  bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2930
0
  bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2931
2932
0
  diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2933
0
  if (diff != 0)
2934
0
    return diff;
2935
2936
0
  if (int_reloc1.r_offset < int_reloc2.r_offset)
2937
0
    return -1;
2938
0
  if (int_reloc1.r_offset > int_reloc2.r_offset)
2939
0
    return 1;
2940
0
  return 0;
2941
0
}
2942
2943
/* Like sort_dynamic_relocs, but used for elf64 relocations.  */
2944
2945
static int
2946
sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2947
      const void *arg2 ATTRIBUTE_UNUSED)
2948
0
{
2949
0
#ifdef BFD64
2950
0
  Elf_Internal_Rela int_reloc1[3];
2951
0
  Elf_Internal_Rela int_reloc2[3];
2952
2953
0
  (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2954
0
    (reldyn_sorting_bfd, arg1, int_reloc1);
2955
0
  (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2956
0
    (reldyn_sorting_bfd, arg2, int_reloc2);
2957
2958
0
  if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2959
0
    return -1;
2960
0
  if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2961
0
    return 1;
2962
2963
0
  if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2964
0
    return -1;
2965
0
  if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2966
0
    return 1;
2967
0
  return 0;
2968
#else
2969
  abort ();
2970
#endif
2971
0
}
2972
2973
2974
/* This routine is used to write out ECOFF debugging external symbol
2975
   information.  It is called via mips_elf_link_hash_traverse.  The
2976
   ECOFF external symbol information must match the ELF external
2977
   symbol information.  Unfortunately, at this point we don't know
2978
   whether a symbol is required by reloc information, so the two
2979
   tables may wind up being different.  We must sort out the external
2980
   symbol information before we can set the final size of the .mdebug
2981
   section, and we must set the size of the .mdebug section before we
2982
   can relocate any sections, and we can't know which symbols are
2983
   required by relocation until we relocate the sections.
2984
   Fortunately, it is relatively unlikely that any symbol will be
2985
   stripped but required by a reloc.  In particular, it can not happen
2986
   when generating a final executable.  */
2987
2988
static bool
2989
mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2990
0
{
2991
0
  struct extsym_info *einfo = data;
2992
0
  bool strip;
2993
0
  asection *sec, *output_section;
2994
2995
0
  if (h->root.indx == -2)
2996
0
    strip = false;
2997
0
  else if ((h->root.def_dynamic
2998
0
      || h->root.ref_dynamic
2999
0
      || h->root.type == bfd_link_hash_new)
3000
0
     && !h->root.def_regular
3001
0
     && !h->root.ref_regular)
3002
0
    strip = true;
3003
0
  else if (einfo->info->strip == strip_all
3004
0
     || (einfo->info->strip == strip_some
3005
0
         && bfd_hash_lookup (einfo->info->keep_hash,
3006
0
           h->root.root.root.string,
3007
0
           false, false) == NULL))
3008
0
    strip = true;
3009
0
  else
3010
0
    strip = false;
3011
3012
0
  if (strip)
3013
0
    return true;
3014
3015
0
  if (h->esym.ifd == -2)
3016
0
    {
3017
0
      h->esym.jmptbl = 0;
3018
0
      h->esym.cobol_main = 0;
3019
0
      h->esym.weakext = 0;
3020
0
      h->esym.reserved = 0;
3021
0
      h->esym.ifd = ifdNil;
3022
0
      h->esym.asym.value = 0;
3023
0
      h->esym.asym.st = stGlobal;
3024
3025
0
      if (h->root.root.type == bfd_link_hash_undefined
3026
0
    || h->root.root.type == bfd_link_hash_undefweak)
3027
0
  {
3028
0
    const char *name;
3029
3030
    /* Use undefined class.  Also, set class and type for some
3031
       special symbols.  */
3032
0
    name = h->root.root.root.string;
3033
0
    if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3034
0
        || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3035
0
      {
3036
0
        h->esym.asym.sc = scData;
3037
0
        h->esym.asym.st = stLabel;
3038
0
        h->esym.asym.value = 0;
3039
0
      }
3040
0
    else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3041
0
      {
3042
0
        h->esym.asym.sc = scAbs;
3043
0
        h->esym.asym.st = stLabel;
3044
0
        h->esym.asym.value =
3045
0
    mips_elf_hash_table (einfo->info)->procedure_count;
3046
0
      }
3047
0
    else
3048
0
      h->esym.asym.sc = scUndefined;
3049
0
  }
3050
0
      else if (h->root.root.type != bfd_link_hash_defined
3051
0
    && h->root.root.type != bfd_link_hash_defweak)
3052
0
  h->esym.asym.sc = scAbs;
3053
0
      else
3054
0
  {
3055
0
    const char *name;
3056
3057
0
    sec = h->root.root.u.def.section;
3058
0
    output_section = sec->output_section;
3059
3060
    /* When making a shared library and symbol h is the one from
3061
       the another shared library, OUTPUT_SECTION may be null.  */
3062
0
    if (output_section == NULL)
3063
0
      h->esym.asym.sc = scUndefined;
3064
0
    else
3065
0
      {
3066
0
        name = bfd_section_name (output_section);
3067
3068
0
        if (strcmp (name, ".text") == 0)
3069
0
    h->esym.asym.sc = scText;
3070
0
        else if (strcmp (name, ".data") == 0)
3071
0
    h->esym.asym.sc = scData;
3072
0
        else if (strcmp (name, ".sdata") == 0)
3073
0
    h->esym.asym.sc = scSData;
3074
0
        else if (strcmp (name, ".rodata") == 0
3075
0
           || strcmp (name, ".rdata") == 0)
3076
0
    h->esym.asym.sc = scRData;
3077
0
        else if (strcmp (name, ".bss") == 0)
3078
0
    h->esym.asym.sc = scBss;
3079
0
        else if (strcmp (name, ".sbss") == 0)
3080
0
    h->esym.asym.sc = scSBss;
3081
0
        else if (strcmp (name, ".init") == 0)
3082
0
    h->esym.asym.sc = scInit;
3083
0
        else if (strcmp (name, ".fini") == 0)
3084
0
    h->esym.asym.sc = scFini;
3085
0
        else
3086
0
    h->esym.asym.sc = scAbs;
3087
0
      }
3088
0
  }
3089
3090
0
      h->esym.asym.reserved = 0;
3091
0
      h->esym.asym.index = indexNil;
3092
0
    }
3093
3094
0
  if (h->root.root.type == bfd_link_hash_common)
3095
0
    h->esym.asym.value = h->root.root.u.c.size;
3096
0
  else if (h->root.root.type == bfd_link_hash_defined
3097
0
     || h->root.root.type == bfd_link_hash_defweak)
3098
0
    {
3099
0
      if (h->esym.asym.sc == scCommon)
3100
0
  h->esym.asym.sc = scBss;
3101
0
      else if (h->esym.asym.sc == scSCommon)
3102
0
  h->esym.asym.sc = scSBss;
3103
3104
0
      sec = h->root.root.u.def.section;
3105
0
      output_section = sec->output_section;
3106
0
      if (output_section != NULL)
3107
0
  h->esym.asym.value = (h->root.root.u.def.value
3108
0
            + sec->output_offset
3109
0
            + output_section->vma);
3110
0
      else
3111
0
  h->esym.asym.value = 0;
3112
0
    }
3113
0
  else
3114
0
    {
3115
0
      struct mips_elf_link_hash_entry *hd = h;
3116
3117
0
      while (hd->root.root.type == bfd_link_hash_indirect)
3118
0
  hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
3119
3120
0
      if (hd->needs_lazy_stub)
3121
0
  {
3122
0
    BFD_ASSERT (hd->root.plt.plist != NULL);
3123
0
    BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
3124
    /* Set type and value for a symbol with a function stub.  */
3125
0
    h->esym.asym.st = stProc;
3126
0
    sec = hd->root.root.u.def.section;
3127
0
    if (sec == NULL)
3128
0
      h->esym.asym.value = 0;
3129
0
    else
3130
0
      {
3131
0
        output_section = sec->output_section;
3132
0
        if (output_section != NULL)
3133
0
    h->esym.asym.value = (hd->root.plt.plist->stub_offset
3134
0
              + sec->output_offset
3135
0
              + output_section->vma);
3136
0
        else
3137
0
    h->esym.asym.value = 0;
3138
0
      }
3139
0
  }
3140
0
    }
3141
3142
0
  if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3143
0
              h->root.root.root.string,
3144
0
              &h->esym))
3145
0
    {
3146
0
      einfo->failed = true;
3147
0
      return false;
3148
0
    }
3149
3150
0
  return true;
3151
0
}
3152
3153
/* A comparison routine used to sort .gptab entries.  */
3154
3155
static int
3156
gptab_compare (const void *p1, const void *p2)
3157
0
{
3158
0
  const Elf32_gptab *a1 = p1;
3159
0
  const Elf32_gptab *a2 = p2;
3160
3161
0
  return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3162
0
}
3163

3164
/* Functions to manage the got entry hash table.  */
3165
3166
/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
3167
   hash number.  */
3168
3169
static inline hashval_t
3170
mips_elf_hash_bfd_vma (bfd_vma addr)
3171
0
{
3172
0
#ifdef BFD64
3173
0
  return addr + (addr >> 32);
3174
#else
3175
  return addr;
3176
#endif
3177
0
}
3178
3179
static hashval_t
3180
mips_elf_got_entry_hash (const void *entry_)
3181
0
{
3182
0
  const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
3183
3184
0
  return (entry->symndx
3185
0
    + ((entry->tls_type == GOT_TLS_LDM) << 18)
3186
0
    + (entry->tls_type == GOT_TLS_LDM ? 0
3187
0
       : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
3188
0
       : entry->symndx >= 0 ? (entry->abfd->id
3189
0
             + mips_elf_hash_bfd_vma (entry->d.addend))
3190
0
       : entry->d.h->root.root.root.hash));
3191
0
}
3192
3193
static int
3194
mips_elf_got_entry_eq (const void *entry1, const void *entry2)
3195
0
{
3196
0
  const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3197
0
  const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3198
3199
0
  return (e1->symndx == e2->symndx
3200
0
    && e1->tls_type == e2->tls_type
3201
0
    && (e1->tls_type == GOT_TLS_LDM ? true
3202
0
        : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3203
0
        : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3204
0
           && e1->d.addend == e2->d.addend)
3205
0
        : e2->abfd && e1->d.h == e2->d.h));
3206
0
}
3207
3208
static hashval_t
3209
mips_got_page_ref_hash (const void *ref_)
3210
0
{
3211
0
  const struct mips_got_page_ref *ref;
3212
3213
0
  ref = (const struct mips_got_page_ref *) ref_;
3214
0
  return ((ref->symndx >= 0
3215
0
     ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3216
0
     : ref->u.h->root.root.root.hash)
3217
0
    + mips_elf_hash_bfd_vma (ref->addend));
3218
0
}
3219
3220
static int
3221
mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3222
0
{
3223
0
  const struct mips_got_page_ref *ref1, *ref2;
3224
3225
0
  ref1 = (const struct mips_got_page_ref *) ref1_;
3226
0
  ref2 = (const struct mips_got_page_ref *) ref2_;
3227
0
  return (ref1->symndx == ref2->symndx
3228
0
    && (ref1->symndx < 0
3229
0
        ? ref1->u.h == ref2->u.h
3230
0
        : ref1->u.abfd == ref2->u.abfd)
3231
0
    && ref1->addend == ref2->addend);
3232
0
}
3233
3234
static hashval_t
3235
mips_got_page_entry_hash (const void *entry_)
3236
0
{
3237
0
  const struct mips_got_page_entry *entry;
3238
3239
0
  entry = (const struct mips_got_page_entry *) entry_;
3240
0
  return entry->sec->id;
3241
0
}
3242
3243
static int
3244
mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3245
0
{
3246
0
  const struct mips_got_page_entry *entry1, *entry2;
3247
3248
0
  entry1 = (const struct mips_got_page_entry *) entry1_;
3249
0
  entry2 = (const struct mips_got_page_entry *) entry2_;
3250
0
  return entry1->sec == entry2->sec;
3251
0
}
3252

3253
/* Create and return a new mips_got_info structure.  */
3254
3255
static struct mips_got_info *
3256
mips_elf_create_got_info (bfd *abfd)
3257
0
{
3258
0
  struct mips_got_info *g;
3259
3260
0
  g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3261
0
  if (g == NULL)
3262
0
    return NULL;
3263
3264
0
  g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3265
0
            mips_elf_got_entry_eq, NULL);
3266
0
  if (g->got_entries == NULL)
3267
0
    return NULL;
3268
3269
0
  g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3270
0
              mips_got_page_ref_eq, NULL);
3271
0
  if (g->got_page_refs == NULL)
3272
0
    return NULL;
3273
3274
0
  return g;
3275
0
}
3276
3277
/* Return the GOT info for input bfd ABFD, trying to create a new one if
3278
   CREATE_P and if ABFD doesn't already have a GOT.  */
3279
3280
static struct mips_got_info *
3281
mips_elf_bfd_got (bfd *abfd, bool create_p)
3282
0
{
3283
0
  struct mips_elf_obj_tdata *tdata;
3284
3285
0
  if (!is_mips_elf (abfd))
3286
0
    return NULL;
3287
3288
0
  tdata = mips_elf_tdata (abfd);
3289
0
  if (!tdata->got && create_p)
3290
0
    tdata->got = mips_elf_create_got_info (abfd);
3291
0
  return tdata->got;
3292
0
}
3293
3294
/* Record that ABFD should use output GOT G.  */
3295
3296
static void
3297
mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3298
0
{
3299
0
  struct mips_elf_obj_tdata *tdata;
3300
3301
0
  BFD_ASSERT (is_mips_elf (abfd));
3302
0
  tdata = mips_elf_tdata (abfd);
3303
0
  if (tdata->got)
3304
0
    {
3305
      /* The GOT structure itself and the hash table entries are
3306
   allocated to a bfd, but the hash tables aren't.  */
3307
0
      htab_delete (tdata->got->got_entries);
3308
0
      htab_delete (tdata->got->got_page_refs);
3309
0
      if (tdata->got->got_page_entries)
3310
0
  htab_delete (tdata->got->got_page_entries);
3311
0
    }
3312
0
  tdata->got = g;
3313
0
}
3314
3315
/* Return the dynamic relocation section.  If it doesn't exist, try to
3316
   create a new it if CREATE_P, otherwise return NULL.  Also return NULL
3317
   if creation fails.  */
3318
3319
static asection *
3320
mips_elf_rel_dyn_section (struct bfd_link_info *info, bool create_p)
3321
0
{
3322
0
  const char *dname;
3323
0
  asection *sreloc;
3324
0
  bfd *dynobj;
3325
3326
0
  dname = MIPS_ELF_REL_DYN_NAME (info);
3327
0
  dynobj = elf_hash_table (info)->dynobj;
3328
0
  sreloc = bfd_get_linker_section (dynobj, dname);
3329
0
  if (sreloc == NULL && create_p)
3330
0
    {
3331
0
      sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3332
0
               (SEC_ALLOC
3333
0
                | SEC_LOAD
3334
0
                | SEC_HAS_CONTENTS
3335
0
                | SEC_IN_MEMORY
3336
0
                | SEC_LINKER_CREATED
3337
0
                | SEC_READONLY));
3338
0
      if (sreloc == NULL
3339
0
    || !bfd_set_section_alignment (sreloc,
3340
0
           MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3341
0
  return NULL;
3342
0
    }
3343
0
  return sreloc;
3344
0
}
3345
3346
/* Return the GOT_TLS_* type required by relocation type R_TYPE.  */
3347
3348
static int
3349
mips_elf_reloc_tls_type (unsigned int r_type)
3350
0
{
3351
0
  if (tls_gd_reloc_p (r_type))
3352
0
    return GOT_TLS_GD;
3353
3354
0
  if (tls_ldm_reloc_p (r_type))
3355
0
    return GOT_TLS_LDM;
3356
3357
0
  if (tls_gottprel_reloc_p (r_type))
3358
0
    return GOT_TLS_IE;
3359
3360
0
  return GOT_TLS_NONE;
3361
0
}
3362
3363
/* Return the number of GOT slots needed for GOT TLS type TYPE.  */
3364
3365
static int
3366
mips_tls_got_entries (unsigned int type)
3367
0
{
3368
0
  switch (type)
3369
0
    {
3370
0
    case GOT_TLS_GD:
3371
0
    case GOT_TLS_LDM:
3372
0
      return 2;
3373
3374
0
    case GOT_TLS_IE:
3375
0
      return 1;
3376
3377
0
    case GOT_TLS_NONE:
3378
0
      return 0;
3379
0
    }
3380
0
  abort ();
3381
0
}
3382
3383
/* Count the number of relocations needed for a TLS GOT entry, with
3384
   access types from TLS_TYPE, and symbol H (or a local symbol if H
3385
   is NULL).  */
3386
3387
static int
3388
mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3389
         struct elf_link_hash_entry *h)
3390
0
{
3391
0
  int indx = 0;
3392
0
  bool need_relocs = false;
3393
0
  bool dyn = elf_hash_table (info)->dynamic_sections_created;
3394
3395
0
  if (h != NULL
3396
0
      && h->dynindx != -1
3397
0
      && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
3398
0
      && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
3399
0
    indx = h->dynindx;
3400
3401
0
  if ((bfd_link_dll (info) || indx != 0)
3402
0
      && (h == NULL
3403
0
    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3404
0
    || h->root.type != bfd_link_hash_undefweak))
3405
0
    need_relocs = true;
3406
3407
0
  if (!need_relocs)
3408
0
    return 0;
3409
3410
0
  switch (tls_type)
3411
0
    {
3412
0
    case GOT_TLS_GD:
3413
0
      return indx != 0 ? 2 : 1;
3414
3415
0
    case GOT_TLS_IE:
3416
0
      return 1;
3417
3418
0
    case GOT_TLS_LDM:
3419
0
      return bfd_link_dll (info) ? 1 : 0;
3420
3421
0
    default:
3422
0
      return 0;
3423
0
    }
3424
0
}
3425
3426
/* Add the number of GOT entries and TLS relocations required by ENTRY
3427
   to G.  */
3428
3429
static void
3430
mips_elf_count_got_entry (struct bfd_link_info *info,
3431
        struct mips_got_info *g,
3432
        struct mips_got_entry *entry)
3433
0
{
3434
0
  if (entry->tls_type)
3435
0
    {
3436
0
      g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3437
0
      g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3438
0
          entry->symndx < 0
3439
0
          ? &entry->d.h->root : NULL);
3440
0
    }
3441
0
  else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3442
0
    g->local_gotno += 1;
3443
0
  else
3444
0
    g->global_gotno += 1;
3445
0
}
3446
3447
/* Output a simple dynamic relocation into SRELOC.  */
3448
3449
static void
3450
mips_elf_output_dynamic_relocation (bfd *output_bfd,
3451
            asection *sreloc,
3452
            unsigned long reloc_index,
3453
            unsigned long indx,
3454
            int r_type,
3455
            bfd_vma offset)
3456
0
{
3457
0
  Elf_Internal_Rela rel[3];
3458
3459
0
  memset (rel, 0, sizeof (rel));
3460
3461
0
  rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3462
0
  rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3463
3464
0
  if (ABI_64_P (output_bfd))
3465
0
    {
3466
0
      (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3467
0
  (output_bfd, &rel[0],
3468
0
   (sreloc->contents
3469
0
    + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3470
0
    }
3471
0
  else
3472
0
    bfd_elf32_swap_reloc_out
3473
0
      (output_bfd, &rel[0],
3474
0
       (sreloc->contents
3475
0
  + reloc_index * sizeof (Elf32_External_Rel)));
3476
0
}
3477
3478
/* Initialize a set of TLS GOT entries for one symbol.  */
3479
3480
static void
3481
mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3482
             struct mips_got_entry *entry,
3483
             struct mips_elf_link_hash_entry *h,
3484
             bfd_vma value)
3485
0
{
3486
0
  bool dyn = elf_hash_table (info)->dynamic_sections_created;
3487
0
  struct mips_elf_link_hash_table *htab;
3488
0
  int indx;
3489
0
  asection *sreloc, *sgot;
3490
0
  bfd_vma got_offset, got_offset2;
3491
0
  bool need_relocs = false;
3492
3493
0
  htab = mips_elf_hash_table (info);
3494
0
  if (htab == NULL)
3495
0
    return;
3496
3497
0
  sgot = htab->root.sgot;
3498
3499
0
  indx = 0;
3500
0
  if (h != NULL
3501
0
      && h->root.dynindx != -1
3502
0
      && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
3503
0
      && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3504
0
    indx = h->root.dynindx;
3505
3506
0
  if (entry->tls_initialized)
3507
0
    return;
3508
3509
0
  if ((bfd_link_dll (info) || indx != 0)
3510
0
      && (h == NULL
3511
0
    || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3512
0
    || h->root.type != bfd_link_hash_undefweak))
3513
0
    need_relocs = true;
3514
3515
  /* MINUS_ONE means the symbol is not defined in this object.  It may not
3516
     be defined at all; assume that the value doesn't matter in that
3517
     case.  Otherwise complain if we would use the value.  */
3518
0
  BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3519
0
        || h->root.root.type == bfd_link_hash_undefweak);
3520
3521
  /* Emit necessary relocations.  */
3522
0
  sreloc = mips_elf_rel_dyn_section (info, false);
3523
0
  got_offset = entry->gotidx;
3524
3525
0
  switch (entry->tls_type)
3526
0
    {
3527
0
    case GOT_TLS_GD:
3528
      /* General Dynamic.  */
3529
0
      got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3530
3531
0
      if (need_relocs)
3532
0
  {
3533
0
    mips_elf_output_dynamic_relocation
3534
0
      (abfd, sreloc, sreloc->reloc_count++, indx,
3535
0
       ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3536
0
       sgot->output_offset + sgot->output_section->vma + got_offset);
3537
3538
0
    if (indx)
3539
0
      mips_elf_output_dynamic_relocation
3540
0
        (abfd, sreloc, sreloc->reloc_count++, indx,
3541
0
         ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3542
0
         sgot->output_offset + sgot->output_section->vma + got_offset2);
3543
0
    else
3544
0
      MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3545
0
             sgot->contents + got_offset2);
3546
0
  }
3547
0
      else
3548
0
  {
3549
0
    MIPS_ELF_PUT_WORD (abfd, 1,
3550
0
           sgot->contents + got_offset);
3551
0
    MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3552
0
           sgot->contents + got_offset2);
3553
0
  }
3554
0
      break;
3555
3556
0
    case GOT_TLS_IE:
3557
      /* Initial Exec model.  */
3558
0
      if (need_relocs)
3559
0
  {
3560
0
    if (indx == 0)
3561
0
      MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3562
0
             sgot->contents + got_offset);
3563
0
    else
3564
0
      MIPS_ELF_PUT_WORD (abfd, 0,
3565
0
             sgot->contents + got_offset);
3566
3567
0
    mips_elf_output_dynamic_relocation
3568
0
      (abfd, sreloc, sreloc->reloc_count++, indx,
3569
0
       ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3570
0
       sgot->output_offset + sgot->output_section->vma + got_offset);
3571
0
  }
3572
0
      else
3573
0
  MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3574
0
         sgot->contents + got_offset);
3575
0
      break;
3576
3577
0
    case GOT_TLS_LDM:
3578
      /* The initial offset is zero, and the LD offsets will include the
3579
   bias by DTP_OFFSET.  */
3580
0
      MIPS_ELF_PUT_WORD (abfd, 0,
3581
0
       sgot->contents + got_offset
3582
0
       + MIPS_ELF_GOT_SIZE (abfd));
3583
3584
0
      if (!bfd_link_dll (info))
3585
0
  MIPS_ELF_PUT_WORD (abfd, 1,
3586
0
         sgot->contents + got_offset);
3587
0
      else
3588
0
  mips_elf_output_dynamic_relocation
3589
0
    (abfd, sreloc, sreloc->reloc_count++, indx,
3590
0
     ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3591
0
     sgot->output_offset + sgot->output_section->vma + got_offset);
3592
0
      break;
3593
3594
0
    default:
3595
0
      abort ();
3596
0
    }
3597
3598
0
  entry->tls_initialized = true;
3599
0
}
3600
3601
/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3602
   for global symbol H.  .got.plt comes before the GOT, so the offset
3603
   will be negative.  */
3604
3605
static bfd_vma
3606
mips_elf_gotplt_index (struct bfd_link_info *info,
3607
           struct elf_link_hash_entry *h)
3608
0
{
3609
0
  bfd_vma got_address, got_value;
3610
0
  struct mips_elf_link_hash_table *htab;
3611
3612
0
  htab = mips_elf_hash_table (info);
3613
0
  BFD_ASSERT (htab != NULL);
3614
3615
0
  BFD_ASSERT (h->plt.plist != NULL);
3616
0
  BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3617
3618
  /* Calculate the address of the associated .got.plt entry.  */
3619
0
  got_address = (htab->root.sgotplt->output_section->vma
3620
0
     + htab->root.sgotplt->output_offset
3621
0
     + (h->plt.plist->gotplt_index
3622
0
        * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3623
3624
  /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
3625
0
  got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3626
0
         + htab->root.hgot->root.u.def.section->output_offset
3627
0
         + htab->root.hgot->root.u.def.value);
3628
3629
0
  return got_address - got_value;
3630
0
}
3631
3632
/* Return the GOT offset for address VALUE.   If there is not yet a GOT
3633
   entry for this value, create one.  If R_SYMNDX refers to a TLS symbol,
3634
   create a TLS GOT entry instead.  Return -1 if no satisfactory GOT
3635
   offset can be found.  */
3636
3637
static bfd_vma
3638
mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3639
        bfd_vma value, unsigned long r_symndx,
3640
        struct mips_elf_link_hash_entry *h, int r_type)
3641
0
{
3642
0
  struct mips_elf_link_hash_table *htab;
3643
0
  struct mips_got_entry *entry;
3644
3645
0
  htab = mips_elf_hash_table (info);
3646
0
  BFD_ASSERT (htab != NULL);
3647
3648
0
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3649
0
             r_symndx, h, r_type);
3650
0
  if (!entry)
3651
0
    return MINUS_ONE;
3652
3653
0
  if (entry->tls_type)
3654
0
    mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3655
0
  return entry->gotidx;
3656
0
}
3657
3658
/* Return the GOT index of global symbol H in the primary GOT.  */
3659
3660
static bfd_vma
3661
mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3662
           struct elf_link_hash_entry *h)
3663
0
{
3664
0
  struct mips_elf_link_hash_table *htab;
3665
0
  long global_got_dynindx;
3666
0
  struct mips_got_info *g;
3667
0
  bfd_vma got_index;
3668
3669
0
  htab = mips_elf_hash_table (info);
3670
0
  BFD_ASSERT (htab != NULL);
3671
3672
0
  global_got_dynindx = 0;
3673
0
  if (htab->global_gotsym != NULL)
3674
0
    global_got_dynindx = htab->global_gotsym->dynindx;
3675
3676
  /* Once we determine the global GOT entry with the lowest dynamic
3677
     symbol table index, we must put all dynamic symbols with greater
3678
     indices into the primary GOT.  That makes it easy to calculate the
3679
     GOT offset.  */
3680
0
  BFD_ASSERT (h->dynindx >= global_got_dynindx);
3681
0
  g = mips_elf_bfd_got (obfd, false);
3682
0
  got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3683
0
         * MIPS_ELF_GOT_SIZE (obfd));
3684
0
  BFD_ASSERT (got_index < htab->root.sgot->size);
3685
3686
0
  return got_index;
3687
0
}
3688
3689
/* Return the GOT index for the global symbol indicated by H, which is
3690
   referenced by a relocation of type R_TYPE in IBFD.  */
3691
3692
static bfd_vma
3693
mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3694
         struct elf_link_hash_entry *h, int r_type)
3695
0
{
3696
0
  struct mips_elf_link_hash_table *htab;
3697
0
  struct mips_got_info *g;
3698
0
  struct mips_got_entry lookup, *entry;
3699
0
  bfd_vma gotidx;
3700
3701
0
  htab = mips_elf_hash_table (info);
3702
0
  BFD_ASSERT (htab != NULL);
3703
3704
0
  g = mips_elf_bfd_got (ibfd, false);
3705
0
  BFD_ASSERT (g);
3706
3707
0
  lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3708
0
  if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, false))
3709
0
    return mips_elf_primary_global_got_index (obfd, info, h);
3710
3711
0
  lookup.abfd = ibfd;
3712
0
  lookup.symndx = -1;
3713
0
  lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3714
0
  entry = htab_find (g->got_entries, &lookup);
3715
0
  BFD_ASSERT (entry);
3716
3717
0
  gotidx = entry->gotidx;
3718
0
  BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3719
3720
0
  if (lookup.tls_type)
3721
0
    {
3722
0
      bfd_vma value = MINUS_ONE;
3723
3724
0
      if ((h->root.type == bfd_link_hash_defined
3725
0
     || h->root.type == bfd_link_hash_defweak)
3726
0
    && h->root.u.def.section->output_section)
3727
0
  value = (h->root.u.def.value
3728
0
     + h->root.u.def.section->output_offset
3729
0
     + h->root.u.def.section->output_section->vma);
3730
3731
0
      mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3732
0
    }
3733
0
  return gotidx;
3734
0
}
3735
3736
/* Find a GOT page entry that points to within 32KB of VALUE.  These
3737
   entries are supposed to be placed at small offsets in the GOT, i.e.,
3738
   within 32KB of GP.  Return the index of the GOT entry, or -1 if no
3739
   entry could be created.  If OFFSETP is nonnull, use it to return the
3740
   offset of the GOT entry from VALUE.  */
3741
3742
static bfd_vma
3743
mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3744
       bfd_vma value, bfd_vma *offsetp)
3745
0
{
3746
0
  bfd_vma page, got_index;
3747
0
  struct mips_got_entry *entry;
3748
3749
0
  page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3750
0
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3751
0
             NULL, R_MIPS_GOT_PAGE);
3752
3753
0
  if (!entry)
3754
0
    return MINUS_ONE;
3755
3756
0
  got_index = entry->gotidx;
3757
3758
0
  if (offsetp)
3759
0
    *offsetp = value - entry->d.address;
3760
3761
0
  return got_index;
3762
0
}
3763
3764
/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3765
   EXTERNAL is true if the relocation was originally against a global
3766
   symbol that binds locally.  */
3767
3768
static bfd_vma
3769
mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3770
          bfd_vma value, bool external)
3771
0
{
3772
0
  struct mips_got_entry *entry;
3773
3774
  /* GOT16 relocations against local symbols are followed by a LO16
3775
     relocation; those against global symbols are not.  Thus if the
3776
     symbol was originally local, the GOT16 relocation should load the
3777
     equivalent of %hi(VALUE), otherwise it should load VALUE itself.  */
3778
0
  if (! external)
3779
0
    value = mips_elf_high (value) << 16;
3780
3781
  /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3782
     R_MIPS16_GOT16, R_MIPS_CALL16, etc.  The format of the entry is the
3783
     same in all cases.  */
3784
0
  entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3785
0
             NULL, R_MIPS_GOT16);
3786
0
  if (entry)
3787
0
    return entry->gotidx;
3788
0
  else
3789
0
    return MINUS_ONE;
3790
0
}
3791
3792
/* Returns the offset for the entry at the INDEXth position
3793
   in the GOT.  */
3794
3795
static bfd_vma
3796
mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3797
        bfd *input_bfd, bfd_vma got_index)
3798
0
{
3799
0
  struct mips_elf_link_hash_table *htab;
3800
0
  asection *sgot;
3801
0
  bfd_vma gp;
3802
3803
0
  htab = mips_elf_hash_table (info);
3804
0
  BFD_ASSERT (htab != NULL);
3805
3806
0
  sgot = htab->root.sgot;
3807
0
  gp = _bfd_get_gp_value (output_bfd)
3808
0
    + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3809
3810
0
  return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3811
0
}
3812
3813
/* Create and return a local GOT entry for VALUE, which was calculated
3814
   from a symbol belonging to INPUT_SECTON.  Return NULL if it could not
3815
   be created.  If R_SYMNDX refers to a TLS symbol, create a TLS entry
3816
   instead.  */
3817
3818
static struct mips_got_entry *
3819
mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3820
         bfd *ibfd, bfd_vma value,
3821
         unsigned long r_symndx,
3822
         struct mips_elf_link_hash_entry *h,
3823
         int r_type)
3824
0
{
3825
0
  struct mips_got_entry lookup, *entry;
3826
0
  void **loc;
3827
0
  struct mips_got_info *g;
3828
0
  struct mips_elf_link_hash_table *htab;
3829
0
  bfd_vma gotidx;
3830
3831
0
  htab = mips_elf_hash_table (info);
3832
0
  BFD_ASSERT (htab != NULL);
3833
3834
0
  g = mips_elf_bfd_got (ibfd, false);
3835
0
  if (g == NULL)
3836
0
    {
3837
0
      g = mips_elf_bfd_got (abfd, false);
3838
0
      BFD_ASSERT (g != NULL);
3839
0
    }
3840
3841
  /* This function shouldn't be called for symbols that live in the global
3842
     area of the GOT.  */
3843
0
  BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3844
3845
0
  lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3846
0
  if (lookup.tls_type)
3847
0
    {
3848
0
      lookup.abfd = ibfd;
3849
0
      if (tls_ldm_reloc_p (r_type))
3850
0
  {
3851
0
    lookup.symndx = 0;
3852
0
    lookup.d.addend = 0;
3853
0
  }
3854
0
      else if (h == NULL)
3855
0
  {
3856
0
    lookup.symndx = r_symndx;
3857
0
    lookup.d.addend = 0;
3858
0
  }
3859
0
      else
3860
0
  {
3861
0
    lookup.symndx = -1;
3862
0
    lookup.d.h = h;
3863
0
  }
3864
3865
0
      entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3866
0
      BFD_ASSERT (entry);
3867
3868
0
      gotidx = entry->gotidx;
3869
0
      BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3870
3871
0
      return entry;
3872
0
    }
3873
3874
0
  lookup.abfd = NULL;
3875
0
  lookup.symndx = -1;
3876
0
  lookup.d.address = value;
3877
0
  loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3878
0
  if (!loc)
3879
0
    return NULL;
3880
3881
0
  entry = (struct mips_got_entry *) *loc;
3882
0
  if (entry)
3883
0
    return entry;
3884
3885
0
  if (g->assigned_low_gotno > g->assigned_high_gotno)
3886
0
    {
3887
      /* We didn't allocate enough space in the GOT.  */
3888
0
      _bfd_error_handler
3889
0
  (_("not enough GOT space for local GOT entries"));
3890
0
      bfd_set_error (bfd_error_bad_value);
3891
0
      return NULL;
3892
0
    }
3893
3894
0
  entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3895
0
  if (!entry)
3896
0
    return NULL;
3897
3898
0
  if (got16_reloc_p (r_type)
3899
0
      || call16_reloc_p (r_type)
3900
0
      || got_page_reloc_p (r_type)
3901
0
      || got_disp_reloc_p (r_type))
3902
0
    lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3903
0
  else
3904
0
    lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3905
3906
0
  *entry = lookup;
3907
0
  *loc = entry;
3908
3909
0
  MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
3910
3911
  /* These GOT entries need a dynamic relocation on VxWorks.  */
3912
0
  if (htab->root.target_os == is_vxworks)
3913
0
    {
3914
0
      Elf_Internal_Rela outrel;
3915
0
      asection *s;
3916
0
      bfd_byte *rloc;
3917
0
      bfd_vma got_address;
3918
3919
0
      s = mips_elf_rel_dyn_section (info, false);
3920
0
      got_address = (htab->root.sgot->output_section->vma
3921
0
         + htab->root.sgot->output_offset
3922
0
         + entry->gotidx);
3923
3924
0
      rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3925
0
      outrel.r_offset = got_address;
3926
0
      outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3927
0
      outrel.r_addend = value;
3928
0
      bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3929
0
    }
3930
3931
0
  return entry;
3932
0
}
3933
3934
/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3935
   The number might be exact or a worst-case estimate, depending on how
3936
   much information is available to elf_backend_omit_section_dynsym at
3937
   the current linking stage.  */
3938
3939
static bfd_size_type
3940
count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3941
0
{
3942
0
  bfd_size_type count;
3943
3944
0
  count = 0;
3945
0
  if (bfd_link_pic (info))
3946
0
    {
3947
0
      asection *p;
3948
0
      const struct elf_backend_data *bed;
3949
3950
0
      bed = get_elf_backend_data (output_bfd);
3951
0
      for (p = output_bfd->sections; p ; p = p->next)
3952
0
  if ((p->flags & SEC_EXCLUDE) == 0
3953
0
      && (p->flags & SEC_ALLOC) != 0
3954
0
      && elf_hash_table (info)->dynamic_relocs
3955
0
      && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3956
0
    ++count;
3957
0
    }
3958
0
  return count;
3959
0
}
3960
3961
/* Sort the dynamic symbol table so that symbols that need GOT entries
3962
   appear towards the end.  */
3963
3964
static bool
3965
mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3966
0
{
3967
0
  struct mips_elf_link_hash_table *htab;
3968
0
  struct mips_elf_hash_sort_data hsd;
3969
0
  struct mips_got_info *g;
3970
3971
0
  htab = mips_elf_hash_table (info);
3972
0
  BFD_ASSERT (htab != NULL);
3973
3974
0
  if (htab->root.dynsymcount == 0)
3975
0
    return true;
3976
3977
0
  g = htab->got_info;
3978
0
  if (g == NULL)
3979
0
    return true;
3980
3981
0
  hsd.low = NULL;
3982
0
  hsd.max_unref_got_dynindx
3983
0
    = hsd.min_got_dynindx
3984
0
    = (htab->root.dynsymcount - g->reloc_only_gotno);
3985
  /* Add 1 to local symbol indices to account for the mandatory NULL entry
3986
     at the head of the table; see `_bfd_elf_link_renumber_dynsyms'.  */
3987
0
  hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
3988
0
  hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
3989
0
  hsd.output_bfd = abfd;
3990
0
  if (htab->root.dynobj != NULL
3991
0
      && htab->root.dynamic_sections_created
3992
0
      && info->emit_gnu_hash)
3993
0
    {
3994
0
      asection *s = bfd_get_linker_section (htab->root.dynobj, ".MIPS.xhash");
3995
0
      BFD_ASSERT (s != NULL);
3996
0
      hsd.mipsxhash = s->contents;
3997
0
      BFD_ASSERT (hsd.mipsxhash != NULL);
3998
0
    }
3999
0
  else
4000
0
    hsd.mipsxhash = NULL;
4001
0
  mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
4002
4003
  /* There should have been enough room in the symbol table to
4004
     accommodate both the GOT and non-GOT symbols.  */
4005
0
  BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
4006
0
  BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
4007
0
  BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
4008
0
  BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
4009
4010
  /* Now we know which dynamic symbol has the lowest dynamic symbol
4011
     table index in the GOT.  */
4012
0
  htab->global_gotsym = hsd.low;
4013
4014
0
  return true;
4015
0
}
4016
4017
/* If H needs a GOT entry, assign it the highest available dynamic
4018
   index.  Otherwise, assign it the lowest available dynamic
4019
   index.  */
4020
4021
static bool
4022
mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
4023
0
{
4024
0
  struct mips_elf_hash_sort_data *hsd = data;
4025
4026
  /* Symbols without dynamic symbol table entries aren't interesting
4027
     at all.  */
4028
0
  if (h->root.dynindx == -1)
4029
0
    return true;
4030
4031
0
  switch (h->global_got_area)
4032
0
    {
4033
0
    case GGA_NONE:
4034
0
      if (h->root.forced_local)
4035
0
  h->root.dynindx = hsd->max_local_dynindx++;
4036
0
      else
4037
0
  h->root.dynindx = hsd->max_non_got_dynindx++;
4038
0
      break;
4039
4040
0
    case GGA_NORMAL:
4041
0
      h->root.dynindx = --hsd->min_got_dynindx;
4042
0
      hsd->low = (struct elf_link_hash_entry *) h;
4043
0
      break;
4044
4045
0
    case GGA_RELOC_ONLY:
4046
0
      if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
4047
0
  hsd->low = (struct elf_link_hash_entry *) h;
4048
0
      h->root.dynindx = hsd->max_unref_got_dynindx++;
4049
0
      break;
4050
0
    }
4051
4052
  /* Populate the .MIPS.xhash translation table entry with
4053
     the symbol dynindx.  */
4054
0
  if (h->mipsxhash_loc != 0 && hsd->mipsxhash != NULL)
4055
0
    bfd_put_32 (hsd->output_bfd, h->root.dynindx,
4056
0
    hsd->mipsxhash + h->mipsxhash_loc);
4057
4058
0
  return true;
4059
0
}
4060
4061
/* Record that input bfd ABFD requires a GOT entry like *LOOKUP
4062
   (which is owned by the caller and shouldn't be added to the
4063
   hash table directly).  */
4064
4065
static bool
4066
mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
4067
         struct mips_got_entry *lookup)
4068
0
{
4069
0
  struct mips_elf_link_hash_table *htab;
4070
0
  struct mips_got_entry *entry;
4071
0
  struct mips_got_info *g;
4072
0
  void **loc, **bfd_loc;
4073
4074
  /* Make sure there's a slot for this entry in the master GOT.  */
4075
0
  htab = mips_elf_hash_table (info);
4076
0
  g = htab->got_info;
4077
0
  loc = htab_find_slot (g->got_entries, lookup, INSERT);
4078
0
  if (!loc)
4079
0
    return false;
4080
4081
  /* Populate the entry if it isn't already.  */
4082
0
  entry = (struct mips_got_entry *) *loc;
4083
0
  if (!entry)
4084
0
    {
4085
0
      entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
4086
0
      if (!entry)
4087
0
  return false;
4088
4089
0
      lookup->tls_initialized = false;
4090
0
      lookup->gotidx = -1;
4091
0
      *entry = *lookup;
4092
0
      *loc = entry;
4093
0
    }
4094
4095
  /* Reuse the same GOT entry for the BFD's GOT.  */
4096
0
  g = mips_elf_bfd_got (abfd, true);
4097
0
  if (!g)
4098
0
    return false;
4099
4100
0
  bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
4101
0
  if (!bfd_loc)
4102
0
    return false;
4103
4104
0
  if (!*bfd_loc)
4105
0
    *bfd_loc = entry;
4106
0
  return true;
4107
0
}
4108
4109
/* ABFD has a GOT relocation of type R_TYPE against H.  Reserve a GOT
4110
   entry for it.  FOR_CALL is true if the caller is only interested in
4111
   using the GOT entry for calls.  */
4112
4113
static bool
4114
mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
4115
           bfd *abfd, struct bfd_link_info *info,
4116
           bool for_call, int r_type)
4117
0
{
4118
0
  struct mips_elf_link_hash_table *htab;
4119
0
  struct mips_elf_link_hash_entry *hmips;
4120
0
  struct mips_got_entry entry;
4121
0
  unsigned char tls_type;
4122
4123
0
  htab = mips_elf_hash_table (info);
4124
0
  BFD_ASSERT (htab != NULL);
4125
4126
0
  hmips = (struct mips_elf_link_hash_entry *) h;
4127
0
  if (!for_call)
4128
0
    hmips->got_only_for_calls = false;
4129
4130
  /* A global symbol in the GOT must also be in the dynamic symbol
4131
     table.  */
4132
0
  if (h->dynindx == -1)
4133
0
    {
4134
0
      switch (ELF_ST_VISIBILITY (h->other))
4135
0
  {
4136
0
  case STV_INTERNAL:
4137
0
  case STV_HIDDEN:
4138
0
    _bfd_mips_elf_hide_symbol (info, h, true);
4139
0
    break;
4140
0
  }
4141
0
      if (!bfd_elf_link_record_dynamic_symbol (info, h))
4142
0
  return false;
4143
0
    }
4144
4145
0
  tls_type = mips_elf_reloc_tls_type (r_type);
4146
0
  if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
4147
0
    hmips->global_got_area = GGA_NORMAL;
4148
4149
0
  entry.abfd = abfd;
4150
0
  entry.symndx = -1;
4151
0
  entry.d.h = (struct mips_elf_link_hash_entry *) h;
4152
0
  entry.tls_type = tls_type;
4153
0
  return mips_elf_record_got_entry (info, abfd, &entry);
4154
0
}
4155
4156
/* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
4157
   where SYMNDX is a local symbol.  Reserve a GOT entry for it.  */
4158
4159
static bool
4160
mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
4161
          struct bfd_link_info *info, int r_type)
4162
0
{
4163
0
  struct mips_elf_link_hash_table *htab;
4164
0
  struct mips_got_info *g;
4165
0
  struct mips_got_entry entry;
4166
4167
0
  htab = mips_elf_hash_table (info);
4168
0
  BFD_ASSERT (htab != NULL);
4169
4170
0
  g = htab->got_info;
4171
0
  BFD_ASSERT (g != NULL);
4172
4173
0
  entry.abfd = abfd;
4174
0
  entry.symndx = symndx;
4175
0
  entry.d.addend = addend;
4176
0
  entry.tls_type = mips_elf_reloc_tls_type (r_type);
4177
0
  return mips_elf_record_got_entry (info, abfd, &entry);
4178
0
}
4179
4180
/* Record that ABFD has a page relocation against SYMNDX + ADDEND.
4181
   H is the symbol's hash table entry, or null if SYMNDX is local
4182
   to ABFD.  */
4183
4184
static bool
4185
mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
4186
            long symndx, struct elf_link_hash_entry *h,
4187
            bfd_signed_vma addend)
4188
0
{
4189
0
  struct mips_elf_link_hash_table *htab;
4190
0
  struct mips_got_info *g1, *g2;
4191
0
  struct mips_got_page_ref lookup, *entry;
4192
0
  void **loc, **bfd_loc;
4193
4194
0
  htab = mips_elf_hash_table (info);
4195
0
  BFD_ASSERT (htab != NULL);
4196
4197
0
  g1 = htab->got_info;
4198
0
  BFD_ASSERT (g1 != NULL);
4199
4200
0
  if (h)
4201
0
    {
4202
0
      lookup.symndx = -1;
4203
0
      lookup.u.h = (struct mips_elf_link_hash_entry *) h;
4204
0
    }
4205
0
  else
4206
0
    {
4207
0
      lookup.symndx = symndx;
4208
0
      lookup.u.abfd = abfd;
4209
0
    }
4210
0
  lookup.addend = addend;
4211
0
  loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
4212
0
  if (loc == NULL)
4213
0
    return false;
4214
4215
0
  entry = (struct mips_got_page_ref *) *loc;
4216
0
  if (!entry)
4217
0
    {
4218
0
      entry = bfd_alloc (abfd, sizeof (*entry));
4219
0
      if (!entry)
4220
0
  return false;
4221
4222
0
      *entry = lookup;
4223
0
      *loc = entry;
4224
0
    }
4225
4226
  /* Add the same entry to the BFD's GOT.  */
4227
0
  g2 = mips_elf_bfd_got (abfd, true);
4228
0
  if (!g2)
4229
0
    return false;
4230
4231
0
  bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
4232
0
  if (!bfd_loc)
4233
0
    return false;
4234
4235
0
  if (!*bfd_loc)
4236
0
    *bfd_loc = entry;
4237
4238
0
  return true;
4239
0
}
4240
4241
/* Add room for N relocations to the .rel(a).dyn section in ABFD.  */
4242
4243
static void
4244
mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4245
               unsigned int n)
4246
0
{
4247
0
  asection *s;
4248
0
  struct mips_elf_link_hash_table *htab;
4249
4250
0
  htab = mips_elf_hash_table (info);
4251
0
  BFD_ASSERT (htab != NULL);
4252
4253
0
  s = mips_elf_rel_dyn_section (info, false);
4254
0
  BFD_ASSERT (s != NULL);
4255
4256
0
  if (htab->root.target_os == is_vxworks)
4257
0
    s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4258
0
  else
4259
0
    {
4260
0
      if (s->size == 0)
4261
0
  {
4262
    /* Make room for a null element.  */
4263
0
    s->size += MIPS_ELF_REL_SIZE (abfd);
4264
0
    ++s->reloc_count;
4265
0
  }
4266
0
      s->size += n * MIPS_ELF_REL_SIZE (abfd);
4267
0
    }
4268
0
}
4269

4270
/* A htab_traverse callback for GOT entries, with DATA pointing to a
4271
   mips_elf_traverse_got_arg structure.  Count the number of GOT
4272
   entries and TLS relocs.  Set DATA->value to true if we need
4273
   to resolve indirect or warning symbols and then recreate the GOT.  */
4274
4275
static int
4276
mips_elf_check_recreate_got (void **entryp, void *data)
4277
0
{
4278
0
  struct mips_got_entry *entry;
4279
0
  struct mips_elf_traverse_got_arg *arg;
4280
4281
0
  entry = (struct mips_got_entry *) *entryp;
4282
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4283
0
  if (entry->abfd != NULL && entry->symndx == -1)
4284
0
    {
4285
0
      struct mips_elf_link_hash_entry *h;
4286
4287
0
      h = entry->d.h;
4288
0
      if (h->root.root.type == bfd_link_hash_indirect
4289
0
    || h->root.root.type == bfd_link_hash_warning)
4290
0
  {
4291
0
    arg->value = true;
4292
0
    return 0;
4293
0
  }
4294
0
    }
4295
0
  mips_elf_count_got_entry (arg->info, arg->g, entry);
4296
0
  return 1;
4297
0
}
4298
4299
/* A htab_traverse callback for GOT entries, with DATA pointing to a
4300
   mips_elf_traverse_got_arg structure.  Add all entries to DATA->g,
4301
   converting entries for indirect and warning symbols into entries
4302
   for the target symbol.  Set DATA->g to null on error.  */
4303
4304
static int
4305
mips_elf_recreate_got (void **entryp, void *data)
4306
0
{
4307
0
  struct mips_got_entry new_entry, *entry;
4308
0
  struct mips_elf_traverse_got_arg *arg;
4309
0
  void **slot;
4310
4311
0
  entry = (struct mips_got_entry *) *entryp;
4312
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4313
0
  if (entry->abfd != NULL
4314
0
      && entry->symndx == -1
4315
0
      && (entry->d.h->root.root.type == bfd_link_hash_indirect
4316
0
    || entry->d.h->root.root.type == bfd_link_hash_warning))
4317
0
    {
4318
0
      struct mips_elf_link_hash_entry *h;
4319
4320
0
      new_entry = *entry;
4321
0
      entry = &new_entry;
4322
0
      h = entry->d.h;
4323
0
      do
4324
0
  {
4325
0
    BFD_ASSERT (h->global_got_area == GGA_NONE);
4326
0
    h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4327
0
  }
4328
0
      while (h->root.root.type == bfd_link_hash_indirect
4329
0
       || h->root.root.type == bfd_link_hash_warning);
4330
0
      entry->d.h = h;
4331
0
    }
4332
0
  slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4333
0
  if (slot == NULL)
4334
0
    {
4335
0
      arg->g = NULL;
4336
0
      return 0;
4337
0
    }
4338
0
  if (*slot == NULL)
4339
0
    {
4340
0
      if (entry == &new_entry)
4341
0
  {
4342
0
    entry = bfd_alloc (entry->abfd, sizeof (*entry));
4343
0
    if (!entry)
4344
0
      {
4345
0
        arg->g = NULL;
4346
0
        return 0;
4347
0
      }
4348
0
    *entry = new_entry;
4349
0
  }
4350
0
      *slot = entry;
4351
0
      mips_elf_count_got_entry (arg->info, arg->g, entry);
4352
0
    }
4353
0
  return 1;
4354
0
}
4355
4356
/* Return the maximum number of GOT page entries required for RANGE.  */
4357
4358
static bfd_vma
4359
mips_elf_pages_for_range (const struct mips_got_page_range *range)
4360
0
{
4361
0
  return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4362
0
}
4363
4364
/* Record that G requires a page entry that can reach SEC + ADDEND.  */
4365
4366
static bool
4367
mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
4368
        asection *sec, bfd_signed_vma addend)
4369
0
{
4370
0
  struct mips_got_info *g = arg->g;
4371
0
  struct mips_got_page_entry lookup, *entry;
4372
0
  struct mips_got_page_range **range_ptr, *range;
4373
0
  bfd_vma old_pages, new_pages;
4374
0
  void **loc;
4375
4376
  /* Find the mips_got_page_entry hash table entry for this section.  */
4377
0
  lookup.sec = sec;
4378
0
  loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4379
0
  if (loc == NULL)
4380
0
    return false;
4381
4382
  /* Create a mips_got_page_entry if this is the first time we've
4383
     seen the section.  */
4384
0
  entry = (struct mips_got_page_entry *) *loc;
4385
0
  if (!entry)
4386
0
    {
4387
0
      entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
4388
0
      if (!entry)
4389
0
  return false;
4390
4391
0
      entry->sec = sec;
4392
0
      *loc = entry;
4393
0
    }
4394
4395
  /* Skip over ranges whose maximum extent cannot share a page entry
4396
     with ADDEND.  */
4397
0
  range_ptr = &entry->ranges;
4398
0
  while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4399
0
    range_ptr = &(*range_ptr)->next;
4400
4401
  /* If we scanned to the end of the list, or found a range whose
4402
     minimum extent cannot share a page entry with ADDEND, create
4403
     a new singleton range.  */
4404
0
  range = *range_ptr;
4405
0
  if (!range || addend < range->min_addend - 0xffff)
4406
0
    {
4407
0
      range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
4408
0
      if (!range)
4409
0
  return false;
4410
4411
0
      range->next = *range_ptr;
4412
0
      range->min_addend = addend;
4413
0
      range->max_addend = addend;
4414
4415
0
      *range_ptr = range;
4416
0
      entry->num_pages++;
4417
0
      g->page_gotno++;
4418
0
      return true;
4419
0
    }
4420
4421
  /* Remember how many pages the old range contributed.  */
4422
0
  old_pages = mips_elf_pages_for_range (range);
4423
4424
  /* Update the ranges.  */
4425
0
  if (addend < range->min_addend)
4426
0
    range->min_addend = addend;
4427
0
  else if (addend > range->max_addend)
4428
0
    {
4429
0
      if (range->next && addend >= range->next->min_addend - 0xffff)
4430
0
  {
4431
0
    old_pages += mips_elf_pages_for_range (range->next);
4432
0
    range->max_addend = range->next->max_addend;
4433
0
    range->next = range->next->next;
4434
0
  }
4435
0
      else
4436
0
  range->max_addend = addend;
4437
0
    }
4438
4439
  /* Record any change in the total estimate.  */
4440
0
  new_pages = mips_elf_pages_for_range (range);
4441
0
  if (old_pages != new_pages)
4442
0
    {
4443
0
      entry->num_pages += new_pages - old_pages;
4444
0
      g->page_gotno += new_pages - old_pages;
4445
0
    }
4446
4447
0
  return true;
4448
0
}
4449
4450
/* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4451
   and for which DATA points to a mips_elf_traverse_got_arg.  Work out
4452
   whether the page reference described by *REFP needs a GOT page entry,
4453
   and record that entry in DATA->g if so.  Set DATA->g to null on failure.  */
4454
4455
static int
4456
mips_elf_resolve_got_page_ref (void **refp, void *data)
4457
0
{
4458
0
  struct mips_got_page_ref *ref;
4459
0
  struct mips_elf_traverse_got_arg *arg;
4460
0
  struct mips_elf_link_hash_table *htab;
4461
0
  asection *sec;
4462
0
  bfd_vma addend;
4463
4464
0
  ref = (struct mips_got_page_ref *) *refp;
4465
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4466
0
  htab = mips_elf_hash_table (arg->info);
4467
4468
0
  if (ref->symndx < 0)
4469
0
    {
4470
0
      struct mips_elf_link_hash_entry *h;
4471
4472
      /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries.  */
4473
0
      h = ref->u.h;
4474
0
      if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4475
0
  return 1;
4476
4477
      /* Ignore undefined symbols; we'll issue an error later if
4478
   appropriate.  */
4479
0
      if (!((h->root.root.type == bfd_link_hash_defined
4480
0
       || h->root.root.type == bfd_link_hash_defweak)
4481
0
      && h->root.root.u.def.section))
4482
0
  return 1;
4483
4484
0
      sec = h->root.root.u.def.section;
4485
0
      addend = h->root.root.u.def.value + ref->addend;
4486
0
    }
4487
0
  else
4488
0
    {
4489
0
      Elf_Internal_Sym *isym;
4490
4491
      /* Read in the symbol.  */
4492
0
      isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ref->u.abfd,
4493
0
            ref->symndx);
4494
0
      if (isym == NULL)
4495
0
  {
4496
0
    arg->g = NULL;
4497
0
    return 0;
4498
0
  }
4499
4500
      /* Get the associated input section.  */
4501
0
      sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4502
0
      if (sec == NULL)
4503
0
  {
4504
0
    arg->g = NULL;
4505
0
    return 0;
4506
0
  }
4507
4508
      /* If this is a mergable section, work out the section and offset
4509
   of the merged data.  For section symbols, the addend specifies
4510
   of the offset _of_ the first byte in the data, otherwise it
4511
   specifies the offset _from_ the first byte.  */
4512
0
      if (sec->flags & SEC_MERGE)
4513
0
  {
4514
0
    void *secinfo;
4515
4516
0
    secinfo = elf_section_data (sec)->sec_info;
4517
0
    if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4518
0
      addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4519
0
             isym->st_value + ref->addend);
4520
0
    else
4521
0
      addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4522
0
             isym->st_value) + ref->addend;
4523
0
  }
4524
0
      else
4525
0
  addend = isym->st_value + ref->addend;
4526
0
    }
4527
0
  if (!mips_elf_record_got_page_entry (arg, sec, addend))
4528
0
    {
4529
0
      arg->g = NULL;
4530
0
      return 0;
4531
0
    }
4532
0
  return 1;
4533
0
}
4534
4535
/* If any entries in G->got_entries are for indirect or warning symbols,
4536
   replace them with entries for the target symbol.  Convert g->got_page_refs
4537
   into got_page_entry structures and estimate the number of page entries
4538
   that they require.  */
4539
4540
static bool
4541
mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4542
            struct mips_got_info *g)
4543
0
{
4544
0
  struct mips_elf_traverse_got_arg tga;
4545
0
  struct mips_got_info oldg;
4546
4547
0
  oldg = *g;
4548
4549
0
  tga.info = info;
4550
0
  tga.g = g;
4551
0
  tga.value = false;
4552
0
  htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4553
0
  if (tga.value)
4554
0
    {
4555
0
      *g = oldg;
4556
0
      g->got_entries = htab_create (htab_size (oldg.got_entries),
4557
0
            mips_elf_got_entry_hash,
4558
0
            mips_elf_got_entry_eq, NULL);
4559
0
      if (!g->got_entries)
4560
0
  return false;
4561
4562
0
      htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4563
0
      if (!tga.g)
4564
0
  return false;
4565
4566
0
      htab_delete (oldg.got_entries);
4567
0
    }
4568
4569
0
  g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4570
0
           mips_got_page_entry_eq, NULL);
4571
0
  if (g->got_page_entries == NULL)
4572
0
    return false;
4573
4574
0
  tga.info = info;
4575
0
  tga.g = g;
4576
0
  htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4577
4578
0
  return true;
4579
0
}
4580
4581
/* Return true if a GOT entry for H should live in the local rather than
4582
   global GOT area.  */
4583
4584
static bool
4585
mips_use_local_got_p (struct bfd_link_info *info,
4586
          struct mips_elf_link_hash_entry *h)
4587
0
{
4588
  /* Symbols that aren't in the dynamic symbol table must live in the
4589
     local GOT.  This includes symbols that are completely undefined
4590
     and which therefore don't bind locally.  We'll report undefined
4591
     symbols later if appropriate.  */
4592
0
  if (h->root.dynindx == -1)
4593
0
    return true;
4594
4595
  /* Absolute symbols, if ever they need a GOT entry, cannot ever go
4596
     to the local GOT, as they would be implicitly relocated by the
4597
     base address by the dynamic loader.  */
4598
0
  if (bfd_is_abs_symbol (&h->root.root))
4599
0
    return false;
4600
4601
  /* Symbols that bind locally can (and in the case of forced-local
4602
     symbols, must) live in the local GOT.  */
4603
0
  if (h->got_only_for_calls
4604
0
      ? SYMBOL_CALLS_LOCAL (info, &h->root)
4605
0
      : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4606
0
    return true;
4607
4608
  /* If this is an executable that must provide a definition of the symbol,
4609
     either though PLTs or copy relocations, then that address should go in
4610
     the local rather than global GOT.  */
4611
0
  if (bfd_link_executable (info) && h->has_static_relocs)
4612
0
    return true;
4613
4614
0
  return false;
4615
0
}
4616
4617
/* A mips_elf_link_hash_traverse callback for which DATA points to the
4618
   link_info structure.  Decide whether the hash entry needs an entry in
4619
   the global part of the primary GOT, setting global_got_area accordingly.
4620
   Count the number of global symbols that are in the primary GOT only
4621
   because they have relocations against them (reloc_only_gotno).  */
4622
4623
static bool
4624
mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4625
0
{
4626
0
  struct bfd_link_info *info;
4627
0
  struct mips_elf_link_hash_table *htab;
4628
0
  struct mips_got_info *g;
4629
4630
0
  info = (struct bfd_link_info *) data;
4631
0
  htab = mips_elf_hash_table (info);
4632
0
  g = htab->got_info;
4633
0
  if (h->global_got_area != GGA_NONE)
4634
0
    {
4635
      /* Make a final decision about whether the symbol belongs in the
4636
   local or global GOT.  */
4637
0
      if (mips_use_local_got_p (info, h))
4638
  /* The symbol belongs in the local GOT.  We no longer need this
4639
     entry if it was only used for relocations; those relocations
4640
     will be against the null or section symbol instead of H.  */
4641
0
  h->global_got_area = GGA_NONE;
4642
0
      else if (htab->root.target_os == is_vxworks
4643
0
         && h->got_only_for_calls
4644
0
         && h->root.plt.plist->mips_offset != MINUS_ONE)
4645
  /* On VxWorks, calls can refer directly to the .got.plt entry;
4646
     they don't need entries in the regular GOT.  .got.plt entries
4647
     will be allocated by _bfd_mips_elf_adjust_dynamic_symbol.  */
4648
0
  h->global_got_area = GGA_NONE;
4649
0
      else if (h->global_got_area == GGA_RELOC_ONLY)
4650
0
  {
4651
0
    g->reloc_only_gotno++;
4652
0
    g->global_gotno++;
4653
0
  }
4654
0
    }
4655
0
  return 1;
4656
0
}
4657

4658
/* A htab_traverse callback for GOT entries.  Add each one to the GOT
4659
   given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
4660
4661
static int
4662
mips_elf_add_got_entry (void **entryp, void *data)
4663
0
{
4664
0
  struct mips_got_entry *entry;
4665
0
  struct mips_elf_traverse_got_arg *arg;
4666
0
  void **slot;
4667
4668
0
  entry = (struct mips_got_entry *) *entryp;
4669
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4670
0
  slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4671
0
  if (!slot)
4672
0
    {
4673
0
      arg->g = NULL;
4674
0
      return 0;
4675
0
    }
4676
0
  if (!*slot)
4677
0
    {
4678
0
      *slot = entry;
4679
0
      mips_elf_count_got_entry (arg->info, arg->g, entry);
4680
0
    }
4681
0
  return 1;
4682
0
}
4683
4684
/* A htab_traverse callback for GOT page entries.  Add each one to the GOT
4685
   given in mips_elf_traverse_got_arg DATA.  Clear DATA->G on error.  */
4686
4687
static int
4688
mips_elf_add_got_page_entry (void **entryp, void *data)
4689
0
{
4690
0
  struct mips_got_page_entry *entry;
4691
0
  struct mips_elf_traverse_got_arg *arg;
4692
0
  void **slot;
4693
4694
0
  entry = (struct mips_got_page_entry *) *entryp;
4695
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4696
0
  slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4697
0
  if (!slot)
4698
0
    {
4699
0
      arg->g = NULL;
4700
0
      return 0;
4701
0
    }
4702
0
  if (!*slot)
4703
0
    {
4704
0
      *slot = entry;
4705
0
      arg->g->page_gotno += entry->num_pages;
4706
0
    }
4707
0
  return 1;
4708
0
}
4709
4710
/* Consider merging FROM, which is ABFD's GOT, into TO.  Return -1 if
4711
   this would lead to overflow, 1 if they were merged successfully,
4712
   and 0 if a merge failed due to lack of memory.  (These values are chosen
4713
   so that nonnegative return values can be returned by a htab_traverse
4714
   callback.)  */
4715
4716
static int
4717
mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4718
       struct mips_got_info *to,
4719
       struct mips_elf_got_per_bfd_arg *arg)
4720
0
{
4721
0
  struct mips_elf_traverse_got_arg tga;
4722
0
  unsigned int estimate;
4723
4724
  /* Work out how many page entries we would need for the combined GOT.  */
4725
0
  estimate = arg->max_pages;
4726
0
  if (estimate >= from->page_gotno + to->page_gotno)
4727
0
    estimate = from->page_gotno + to->page_gotno;
4728
4729
  /* And conservatively estimate how many local and TLS entries
4730
     would be needed.  */
4731
0
  estimate += from->local_gotno + to->local_gotno;
4732
0
  estimate += from->tls_gotno + to->tls_gotno;
4733
4734
  /* If we're merging with the primary got, any TLS relocations will
4735
     come after the full set of global entries.  Otherwise estimate those
4736
     conservatively as well.  */
4737
0
  if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4738
0
    estimate += arg->global_count;
4739
0
  else
4740
0
    estimate += from->global_gotno + to->global_gotno;
4741
4742
  /* Bail out if the combined GOT might be too big.  */
4743
0
  if (estimate > arg->max_count)
4744
0
    return -1;
4745
4746
  /* Transfer the bfd's got information from FROM to TO.  */
4747
0
  tga.info = arg->info;
4748
0
  tga.g = to;
4749
0
  htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4750
0
  if (!tga.g)
4751
0
    return 0;
4752
4753
0
  htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4754
0
  if (!tga.g)
4755
0
    return 0;
4756
4757
0
  mips_elf_replace_bfd_got (abfd, to);
4758
0
  return 1;
4759
0
}
4760
4761
/* Attempt to merge GOT G, which belongs to ABFD.  Try to use as much
4762
   as possible of the primary got, since it doesn't require explicit
4763
   dynamic relocations, but don't use bfds that would reference global
4764
   symbols out of the addressable range.  Failing the primary got,
4765
   attempt to merge with the current got, or finish the current got
4766
   and then make make the new got current.  */
4767
4768
static bool
4769
mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4770
        struct mips_elf_got_per_bfd_arg *arg)
4771
0
{
4772
0
  unsigned int estimate;
4773
0
  int result;
4774
4775
0
  if (!mips_elf_resolve_final_got_entries (arg->info, g))
4776
0
    return false;
4777
4778
  /* Work out the number of page, local and TLS entries.  */
4779
0
  estimate = arg->max_pages;
4780
0
  if (estimate > g->page_gotno)
4781
0
    estimate = g->page_gotno;
4782
0
  estimate += g->local_gotno + g->tls_gotno;
4783
4784
  /* We place TLS GOT entries after both locals and globals.  The globals
4785
     for the primary GOT may overflow the normal GOT size limit, so be
4786
     sure not to merge a GOT which requires TLS with the primary GOT in that
4787
     case.  This doesn't affect non-primary GOTs.  */
4788
0
  estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4789
4790
0
  if (estimate <= arg->max_count)
4791
0
    {
4792
      /* If we don't have a primary GOT, use it as
4793
   a starting point for the primary GOT.  */
4794
0
      if (!arg->primary)
4795
0
  {
4796
0
    arg->primary = g;
4797
0
    return true;
4798
0
  }
4799
4800
      /* Try merging with the primary GOT.  */
4801
0
      result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4802
0
      if (result >= 0)
4803
0
  return result;
4804
0
    }
4805
4806
  /* If we can merge with the last-created got, do it.  */
4807
0
  if (arg->current)
4808
0
    {
4809
0
      result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4810
0
      if (result >= 0)
4811
0
  return result;
4812
0
    }
4813
4814
  /* Well, we couldn't merge, so create a new GOT.  Don't check if it
4815
     fits; if it turns out that it doesn't, we'll get relocation
4816
     overflows anyway.  */
4817
0
  g->next = arg->current;
4818
0
  arg->current = g;
4819
4820
0
  return true;
4821
0
}
4822
4823
/* ENTRYP is a hash table entry for a mips_got_entry.  Set its gotidx
4824
   to GOTIDX, duplicating the entry if it has already been assigned
4825
   an index in a different GOT.  */
4826
4827
static bool
4828
mips_elf_set_gotidx (void **entryp, long gotidx)
4829
0
{
4830
0
  struct mips_got_entry *entry;
4831
4832
0
  entry = (struct mips_got_entry *) *entryp;
4833
0
  if (entry->gotidx > 0)
4834
0
    {
4835
0
      struct mips_got_entry *new_entry;
4836
4837
0
      new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4838
0
      if (!new_entry)
4839
0
  return false;
4840
4841
0
      *new_entry = *entry;
4842
0
      *entryp = new_entry;
4843
0
      entry = new_entry;
4844
0
    }
4845
0
  entry->gotidx = gotidx;
4846
0
  return true;
4847
0
}
4848
4849
/* Set the TLS GOT index for the GOT entry in ENTRYP.  DATA points to a
4850
   mips_elf_traverse_got_arg in which DATA->value is the size of one
4851
   GOT entry.  Set DATA->g to null on failure.  */
4852
4853
static int
4854
mips_elf_initialize_tls_index (void **entryp, void *data)
4855
0
{
4856
0
  struct mips_got_entry *entry;
4857
0
  struct mips_elf_traverse_got_arg *arg;
4858
4859
  /* We're only interested in TLS symbols.  */
4860
0
  entry = (struct mips_got_entry *) *entryp;
4861
0
  if (entry->tls_type == GOT_TLS_NONE)
4862
0
    return 1;
4863
4864
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4865
0
  if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4866
0
    {
4867
0
      arg->g = NULL;
4868
0
      return 0;
4869
0
    }
4870
4871
  /* Account for the entries we've just allocated.  */
4872
0
  arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4873
0
  return 1;
4874
0
}
4875
4876
/* A htab_traverse callback for GOT entries, where DATA points to a
4877
   mips_elf_traverse_got_arg.  Set the global_got_area of each global
4878
   symbol to DATA->value.  */
4879
4880
static int
4881
mips_elf_set_global_got_area (void **entryp, void *data)
4882
0
{
4883
0
  struct mips_got_entry *entry;
4884
0
  struct mips_elf_traverse_got_arg *arg;
4885
4886
0
  entry = (struct mips_got_entry *) *entryp;
4887
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4888
0
  if (entry->abfd != NULL
4889
0
      && entry->symndx == -1
4890
0
      && entry->d.h->global_got_area != GGA_NONE)
4891
0
    entry->d.h->global_got_area = arg->value;
4892
0
  return 1;
4893
0
}
4894
4895
/* A htab_traverse callback for secondary GOT entries, where DATA points
4896
   to a mips_elf_traverse_got_arg.  Assign GOT indices to global entries
4897
   and record the number of relocations they require.  DATA->value is
4898
   the size of one GOT entry.  Set DATA->g to null on failure.  */
4899
4900
static int
4901
mips_elf_set_global_gotidx (void **entryp, void *data)
4902
0
{
4903
0
  struct mips_got_entry *entry;
4904
0
  struct mips_elf_traverse_got_arg *arg;
4905
4906
0
  entry = (struct mips_got_entry *) *entryp;
4907
0
  arg = (struct mips_elf_traverse_got_arg *) data;
4908
0
  if (entry->abfd != NULL
4909
0
      && entry->symndx == -1
4910
0
      && entry->d.h->global_got_area != GGA_NONE)
4911
0
    {
4912
0
      if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
4913
0
  {
4914
0
    arg->g = NULL;
4915
0
    return 0;
4916
0
  }
4917
0
      arg->g->assigned_low_gotno += 1;
4918
4919
0
      if (bfd_link_pic (arg->info)
4920
0
    || (elf_hash_table (arg->info)->dynamic_sections_created
4921
0
        && entry->d.h->root.def_dynamic
4922
0
        && !entry->d.h->root.def_regular))
4923
0
  arg->g->relocs += 1;
4924
0
    }
4925
4926
0
  return 1;
4927
0
}
4928
4929
/* A htab_traverse callback for GOT entries for which DATA is the
4930
   bfd_link_info.  Forbid any global symbols from having traditional
4931
   lazy-binding stubs.  */
4932
4933
static int
4934
mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4935
0
{
4936
0
  struct bfd_link_info *info;
4937
0
  struct mips_elf_link_hash_table *htab;
4938
0
  struct mips_got_entry *entry;
4939
4940
0
  entry = (struct mips_got_entry *) *entryp;
4941
0
  info = (struct bfd_link_info *) data;
4942
0
  htab = mips_elf_hash_table (info);
4943
0
  BFD_ASSERT (htab != NULL);
4944
4945
0
  if (entry->abfd != NULL
4946
0
      && entry->symndx == -1
4947
0
      && entry->d.h->needs_lazy_stub)
4948
0
    {
4949
0
      entry->d.h->needs_lazy_stub = false;
4950
0
      htab->lazy_stub_count--;
4951
0
    }
4952
4953
0
  return 1;
4954
0
}
4955
4956
/* Return the offset of an input bfd IBFD's GOT from the beginning of
4957
   the primary GOT.  */
4958
static bfd_vma
4959
mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4960
0
{
4961
0
  if (!g->next)
4962
0
    return 0;
4963
4964
0
  g = mips_elf_bfd_got (ibfd, false);
4965
0
  if (! g)
4966
0
    return 0;
4967
4968
0
  BFD_ASSERT (g->next);
4969
4970
0
  g = g->next;
4971
4972
0
  return (g->local_gotno + g->global_gotno + g->tls_gotno)
4973
0
    * MIPS_ELF_GOT_SIZE (abfd);
4974
0
}
4975
4976
/* Turn a single GOT that is too big for 16-bit addressing into
4977
   a sequence of GOTs, each one 16-bit addressable.  */
4978
4979
static bool
4980
mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4981
        asection *got, bfd_size_type pages)
4982
0
{
4983
0
  struct mips_elf_link_hash_table *htab;
4984
0
  struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4985
0
  struct mips_elf_traverse_got_arg tga;
4986
0
  struct mips_got_info *g, *gg;
4987
0
  unsigned int assign, needed_relocs;
4988
0
  bfd *dynobj, *ibfd;
4989
4990
0
  dynobj = elf_hash_table (info)->dynobj;
4991
0
  htab = mips_elf_hash_table (info);
4992
0
  BFD_ASSERT (htab != NULL);
4993
4994
0
  g = htab->got_info;
4995
4996
0
  got_per_bfd_arg.obfd = abfd;
4997
0
  got_per_bfd_arg.info = info;
4998
0
  got_per_bfd_arg.current = NULL;
4999
0
  got_per_bfd_arg.primary = NULL;
5000
0
  got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
5001
0
        / MIPS_ELF_GOT_SIZE (abfd))
5002
0
             - htab->reserved_gotno);
5003
0
  got_per_bfd_arg.max_pages = pages;
5004
  /* The number of globals that will be included in the primary GOT.
5005
     See the calls to mips_elf_set_global_got_area below for more
5006
     information.  */
5007
0
  got_per_bfd_arg.global_count = g->global_gotno;
5008
5009
  /* Try to merge the GOTs of input bfds together, as long as they
5010
     don't seem to exceed the maximum GOT size, choosing one of them
5011
     to be the primary GOT.  */
5012
0
  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
5013
0
    {
5014
0
      gg = mips_elf_bfd_got (ibfd, false);
5015
0
      if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
5016
0
  return false;
5017
0
    }
5018
5019
  /* If we do not find any suitable primary GOT, create an empty one.  */
5020
0
  if (got_per_bfd_arg.primary == NULL)
5021
0
    g->next = mips_elf_create_got_info (abfd);
5022
0
  else
5023
0
    g->next = got_per_bfd_arg.primary;
5024
0
  g->next->next = got_per_bfd_arg.current;
5025
5026
  /* GG is now the master GOT, and G is the primary GOT.  */
5027
0
  gg = g;
5028
0
  g = g->next;
5029
5030
  /* Map the output bfd to the primary got.  That's what we're going
5031
     to use for bfds that use GOT16 or GOT_PAGE relocations that we
5032
     didn't mark in check_relocs, and we want a quick way to find it.
5033
     We can't just use gg->next because we're going to reverse the
5034
     list.  */
5035
0
  mips_elf_replace_bfd_got (abfd, g);
5036
5037
  /* Every symbol that is referenced in a dynamic relocation must be
5038
     present in the primary GOT, so arrange for them to appear after
5039
     those that are actually referenced.  */
5040
0
  gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
5041
0
  g->global_gotno = gg->global_gotno;
5042
5043
0
  tga.info = info;
5044
0
  tga.value = GGA_RELOC_ONLY;
5045
0
  htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
5046
0
  tga.value = GGA_NORMAL;
5047
0
  htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
5048
5049
  /* Now go through the GOTs assigning them offset ranges.
5050
     [assigned_low_gotno, local_gotno[ will be set to the range of local
5051
     entries in each GOT.  We can then compute the end of a GOT by
5052
     adding local_gotno to global_gotno.  We reverse the list and make
5053
     it circular since then we'll be able to quickly compute the
5054
     beginning of a GOT, by computing the end of its predecessor.  To
5055
     avoid special cases for the primary GOT, while still preserving
5056
     assertions that are valid for both single- and multi-got links,
5057
     we arrange for the main got struct to have the right number of
5058
     global entries, but set its local_gotno such that the initial
5059
     offset of the primary GOT is zero.  Remember that the primary GOT
5060
     will become the last item in the circular linked list, so it
5061
     points back to the master GOT.  */
5062
0
  gg->local_gotno = -g->global_gotno;
5063
0
  gg->global_gotno = g->global_gotno;
5064
0
  gg->tls_gotno = 0;
5065
0
  assign = 0;
5066
0
  gg->next = gg;
5067
5068
0
  do
5069
0
    {
5070
0
      struct mips_got_info *gn;
5071
5072
0
      assign += htab->reserved_gotno;
5073
0
      g->assigned_low_gotno = assign;
5074
0
      g->local_gotno += assign;
5075
0
      g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
5076
0
      g->assigned_high_gotno = g->local_gotno - 1;
5077
0
      assign = g->local_gotno + g->global_gotno + g->tls_gotno;
5078
5079
      /* Take g out of the direct list, and push it onto the reversed
5080
   list that gg points to.  g->next is guaranteed to be nonnull after
5081
   this operation, as required by mips_elf_initialize_tls_index. */
5082
0
      gn = g->next;
5083
0
      g->next = gg->next;
5084
0
      gg->next = g;
5085
5086
      /* Set up any TLS entries.  We always place the TLS entries after
5087
   all non-TLS entries.  */
5088
0
      g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
5089
0
      tga.g = g;
5090
0
      tga.value = MIPS_ELF_GOT_SIZE (abfd);
5091
0
      htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
5092
0
      if (!tga.g)
5093
0
  return false;
5094
0
      BFD_ASSERT (g->tls_assigned_gotno == assign);
5095
5096
      /* Move onto the next GOT.  It will be a secondary GOT if nonull.  */
5097
0
      g = gn;
5098
5099
      /* Forbid global symbols in every non-primary GOT from having
5100
   lazy-binding stubs.  */
5101
0
      if (g)
5102
0
  htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
5103
0
    }
5104
0
  while (g);
5105
5106
0
  got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
5107
5108
0
  needed_relocs = 0;
5109
0
  for (g = gg->next; g && g->next != gg; g = g->next)
5110
0
    {
5111
0
      unsigned int save_assign;
5112
5113
      /* Assign offsets to global GOT entries and count how many
5114
   relocations they need.  */
5115
0
      save_assign = g->assigned_low_gotno;
5116
0
      g->assigned_low_gotno = g->local_gotno;
5117
0
      tga.info = info;
5118
0
      tga.value = MIPS_ELF_GOT_SIZE (abfd);
5119
0
      tga.g = g;
5120
0
      htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
5121
0
      if (!tga.g)
5122
0
  return false;
5123
0
      BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
5124
0
      g->assigned_low_gotno = save_assign;
5125
5126
0
      if (bfd_link_pic (info))
5127
0
  {
5128
0
    g->relocs += g->local_gotno - g->assigned_low_gotno;
5129
0
    BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
5130
0
          + g->next->global_gotno
5131
0
          + g->next->tls_gotno
5132
0
          + htab->reserved_gotno);
5133
0
  }
5134
0
      needed_relocs += g->relocs;
5135
0
    }
5136
0
  needed_relocs += g->relocs;
5137
5138
0
  if (needed_relocs)
5139
0
    mips_elf_allocate_dynamic_relocations (dynobj, info,
5140
0
             needed_relocs);
5141
5142
0
  return true;
5143
0
}
5144
5145

5146
/* Returns the first relocation of type r_type found, beginning with
5147
   RELOCATION.  RELEND is one-past-the-end of the relocation table.  */
5148
5149
static const Elf_Internal_Rela *
5150
mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
5151
        const Elf_Internal_Rela *relocation,
5152
        const Elf_Internal_Rela *relend)
5153
0
{
5154
0
  unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
5155
5156
0
  while (relocation < relend)
5157
0
    {
5158
0
      if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
5159
0
    && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
5160
0
  return relocation;
5161
5162
0
      ++relocation;
5163
0
    }
5164
5165
  /* We didn't find it.  */
5166
0
  return NULL;
5167
0
}
5168
5169
/* Return whether an input relocation is against a local symbol.  */
5170
5171
static bool
5172
mips_elf_local_relocation_p (bfd *input_bfd,
5173
           const Elf_Internal_Rela *relocation,
5174
           asection **local_sections)
5175
0
{
5176
0
  unsigned long r_symndx;
5177
0
  Elf_Internal_Shdr *symtab_hdr;
5178
0
  size_t extsymoff;
5179
5180
0
  r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5181
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5182
0
  extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5183
5184
0
  if (r_symndx < extsymoff)
5185
0
    return true;
5186
0
  if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
5187
0
    return true;
5188
5189
0
  return false;
5190
0
}
5191

5192
/* Sign-extend VALUE, which has the indicated number of BITS.  */
5193
5194
bfd_vma
5195
_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
5196
7
{
5197
7
  if (value & ((bfd_vma) 1 << (bits - 1)))
5198
    /* VALUE is negative.  */
5199
4
    value |= ((bfd_vma) - 1) << bits;
5200
5201
7
  return value;
5202
7
}
5203
5204
/* Return non-zero if the indicated VALUE has overflowed the maximum
5205
   range expressible by a signed number with the indicated number of
5206
   BITS.  */
5207
5208
static bool
5209
mips_elf_overflow_p (bfd_vma value, int bits)
5210
0
{
5211
0
  bfd_signed_vma svalue = (bfd_signed_vma) value;
5212
5213
0
  if (svalue > (1 << (bits - 1)) - 1)
5214
    /* The value is too big.  */
5215
0
    return true;
5216
0
  else if (svalue < -(1 << (bits - 1)))
5217
    /* The value is too small.  */
5218
0
    return true;
5219
5220
  /* All is well.  */
5221
0
  return false;
5222
0
}
5223
5224
/* Calculate the %high function.  */
5225
5226
static bfd_vma
5227
mips_elf_high (bfd_vma value)
5228
0
{
5229
0
  return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5230
0
}
5231
5232
/* Calculate the %higher function.  */
5233
5234
static bfd_vma
5235
mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
5236
0
{
5237
0
#ifdef BFD64
5238
0
  return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5239
#else
5240
  abort ();
5241
  return MINUS_ONE;
5242
#endif
5243
0
}
5244
5245
/* Calculate the %highest function.  */
5246
5247
static bfd_vma
5248
mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
5249
0
{
5250
0
#ifdef BFD64
5251
0
  return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
5252
#else
5253
  abort ();
5254
  return MINUS_ONE;
5255
#endif
5256
0
}
5257

5258
/* Create the .compact_rel section.  */
5259
5260
static bool
5261
mips_elf_create_compact_rel_section
5262
  (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
5263
0
{
5264
0
  flagword flags;
5265
0
  register asection *s;
5266
5267
0
  if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
5268
0
    {
5269
0
      flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5270
0
         | SEC_READONLY);
5271
5272
0
      s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
5273
0
      if (s == NULL
5274
0
    || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5275
0
  return false;
5276
5277
0
      s->size = sizeof (Elf32_External_compact_rel);
5278
0
    }
5279
5280
0
  return true;
5281
0
}
5282
5283
/* Create the .got section to hold the global offset table.  */
5284
5285
static bool
5286
mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5287
0
{
5288
0
  flagword flags;
5289
0
  register asection *s;
5290
0
  struct elf_link_hash_entry *h;
5291
0
  struct bfd_link_hash_entry *bh;
5292
0
  struct mips_elf_link_hash_table *htab;
5293
5294
0
  htab = mips_elf_hash_table (info);
5295
0
  BFD_ASSERT (htab != NULL);
5296
5297
  /* This function may be called more than once.  */
5298
0
  if (htab->root.sgot)
5299
0
    return true;
5300
5301
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5302
0
     | SEC_LINKER_CREATED);
5303
5304
  /* We have to use an alignment of 2**4 here because this is hardcoded
5305
     in the function stub generation and in the linker script.  */
5306
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5307
0
  if (s == NULL
5308
0
      || !bfd_set_section_alignment (s, 4))
5309
0
    return false;
5310
0
  htab->root.sgot = s;
5311
5312
  /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
5313
     linker script because we don't want to define the symbol if we
5314
     are not creating a global offset table.  */
5315
0
  bh = NULL;
5316
0
  if (! (_bfd_generic_link_add_one_symbol
5317
0
   (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5318
0
    0, NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
5319
0
    return false;
5320
5321
0
  h = (struct elf_link_hash_entry *) bh;
5322
0
  h->non_elf = 0;
5323
0
  h->def_regular = 1;
5324
0
  h->type = STT_OBJECT;
5325
0
  h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5326
0
  elf_hash_table (info)->hgot = h;
5327
5328
0
  if (bfd_link_pic (info)
5329
0
      && ! bfd_elf_link_record_dynamic_symbol (info, h))
5330
0
    return false;
5331
5332
0
  htab->got_info = mips_elf_create_got_info (abfd);
5333
0
  mips_elf_section_data (s)->elf.this_hdr.sh_flags
5334
0
    |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5335
5336
  /* We also need a .got.plt section when generating PLTs.  */
5337
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5338
0
            SEC_ALLOC | SEC_LOAD
5339
0
            | SEC_HAS_CONTENTS
5340
0
            | SEC_IN_MEMORY
5341
0
            | SEC_LINKER_CREATED);
5342
0
  if (s == NULL)
5343
0
    return false;
5344
0
  htab->root.sgotplt = s;
5345
5346
0
  return true;
5347
0
}
5348

5349
/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5350
   __GOTT_INDEX__ symbols.  These symbols are only special for
5351
   shared objects; they are not used in executables.  */
5352
5353
static bool
5354
is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5355
0
{
5356
0
  return (mips_elf_hash_table (info)->root.target_os == is_vxworks
5357
0
    && bfd_link_pic (info)
5358
0
    && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5359
0
        || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5360
0
}
5361
5362
/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5363
   require an la25 stub.  See also mips_elf_local_pic_function_p,
5364
   which determines whether the destination function ever requires a
5365
   stub.  */
5366
5367
static bool
5368
mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5369
             bool target_is_16_bit_code_p)
5370
0
{
5371
  /* We specifically ignore branches and jumps from EF_PIC objects,
5372
     where the onus is on the compiler or programmer to perform any
5373
     necessary initialization of $25.  Sometimes such initialization
5374
     is unnecessary; for example, -mno-shared functions do not use
5375
     the incoming value of $25, and may therefore be called directly.  */
5376
0
  if (PIC_OBJECT_P (input_bfd))
5377
0
    return false;
5378
5379
0
  switch (r_type)
5380
0
    {
5381
0
    case R_MIPS_26:
5382
0
    case R_MIPS_PC16:
5383
0
    case R_MIPS_PC21_S2:
5384
0
    case R_MIPS_PC26_S2:
5385
0
    case R_MICROMIPS_26_S1:
5386
0
    case R_MICROMIPS_PC7_S1:
5387
0
    case R_MICROMIPS_PC10_S1:
5388
0
    case R_MICROMIPS_PC16_S1:
5389
0
    case R_MICROMIPS_PC23_S2:
5390
0
      return true;
5391
5392
0
    case R_MIPS16_26:
5393
0
      return !target_is_16_bit_code_p;
5394
5395
0
    default:
5396
0
      return false;
5397
0
    }
5398
0
}
5399

5400
/* Obtain the field relocated by RELOCATION.  */
5401
5402
static bfd_vma
5403
mips_elf_obtain_contents (reloc_howto_type *howto,
5404
        const Elf_Internal_Rela *relocation,
5405
        bfd *input_bfd, bfd_byte *contents)
5406
0
{
5407
0
  bfd_vma x = 0;
5408
0
  bfd_byte *location = contents + relocation->r_offset;
5409
0
  unsigned int size = bfd_get_reloc_size (howto);
5410
5411
  /* Obtain the bytes.  */
5412
0
  if (size != 0)
5413
0
    x = bfd_get (8 * size, input_bfd, location);
5414
5415
0
  return x;
5416
0
}
5417
5418
/* Store the field relocated by RELOCATION.  */
5419
5420
static void
5421
mips_elf_store_contents (reloc_howto_type *howto,
5422
       const Elf_Internal_Rela *relocation,
5423
       bfd *input_bfd, bfd_byte *contents, bfd_vma x)
5424
0
{
5425
0
  bfd_byte *location = contents + relocation->r_offset;
5426
0
  unsigned int size = bfd_get_reloc_size (howto);
5427
5428
  /* Put the value into the output.  */
5429
0
  if (size != 0)
5430
0
    bfd_put (8 * size, input_bfd, x, location);
5431
0
}
5432
5433
/* Try to patch a load from GOT instruction in CONTENTS pointed to by
5434
   RELOCATION described by HOWTO, with a move of 0 to the load target
5435
   register, returning TRUE if that is successful and FALSE otherwise.
5436
   If DOIT is FALSE, then only determine it patching is possible and
5437
   return status without actually changing CONTENTS.
5438
*/
5439
5440
static bool
5441
mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
5442
         const Elf_Internal_Rela *relocation,
5443
         reloc_howto_type *howto, bool doit)
5444
0
{
5445
0
  int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5446
0
  bfd_byte *location = contents + relocation->r_offset;
5447
0
  bool nullified = true;
5448
0
  bfd_vma x;
5449
5450
0
  _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
5451
5452
  /* Obtain the current value.  */
5453
0
  x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5454
5455
  /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
5456
     while RY is at bits [18:16] of the combined 32-bit instruction word.  */
5457
0
  if (mips16_reloc_p (r_type)
5458
0
      && (((x >> 22) & 0x3ff) == 0x3d3        /* LW */
5459
0
    || ((x >> 22) & 0x3ff) == 0x3c7))      /* LD */
5460
0
    x = (0x3cdU << 22) | (x & (7 << 16)) << 3;     /* LI */
5461
0
  else if (micromips_reloc_p (r_type)
5462
0
     && ((x >> 26) & 0x37) == 0x37)     /* LW/LD */
5463
0
    x = (0xc << 26) | (x & (0x1f << 21));     /* ADDIU */
5464
0
  else if (((x >> 26) & 0x3f) == 0x23        /* LW */
5465
0
     || ((x >> 26) & 0x3f) == 0x37)     /* LD */
5466
0
    x = (0x9 << 26) | (x & (0x1f << 16));     /* ADDIU */
5467
0
  else
5468
0
    nullified = false;
5469
5470
  /* Put the value into the output.  */
5471
0
  if (doit && nullified)
5472
0
    mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
5473
5474
0
  _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, false, location);
5475
5476
0
  return nullified;
5477
0
}
5478
5479
/* Calculate the value produced by the RELOCATION (which comes from
5480
   the INPUT_BFD).  The ADDEND is the addend to use for this
5481
   RELOCATION; RELOCATION->R_ADDEND is ignored.
5482
5483
   The result of the relocation calculation is stored in VALUEP.
5484
   On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5485
   is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5486
5487
   This function returns bfd_reloc_continue if the caller need take no
5488
   further action regarding this relocation, bfd_reloc_notsupported if
5489
   something goes dramatically wrong, bfd_reloc_overflow if an
5490
   overflow occurs, and bfd_reloc_ok to indicate success.  */
5491
5492
static bfd_reloc_status_type
5493
mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5494
             asection *input_section, bfd_byte *contents,
5495
             struct bfd_link_info *info,
5496
             const Elf_Internal_Rela *relocation,
5497
             bfd_vma addend, reloc_howto_type *howto,
5498
             Elf_Internal_Sym *local_syms,
5499
             asection **local_sections, bfd_vma *valuep,
5500
             const char **namep,
5501
             bool *cross_mode_jump_p,
5502
             bool save_addend)
5503
0
{
5504
  /* The eventual value we will return.  */
5505
0
  bfd_vma value;
5506
  /* The address of the symbol against which the relocation is
5507
     occurring.  */
5508
0
  bfd_vma symbol = 0;
5509
  /* The final GP value to be used for the relocatable, executable, or
5510
     shared object file being produced.  */
5511
0
  bfd_vma gp;
5512
  /* The place (section offset or address) of the storage unit being
5513
     relocated.  */
5514
0
  bfd_vma p;
5515
  /* The value of GP used to create the relocatable object.  */
5516
0
  bfd_vma gp0;
5517
  /* The offset into the global offset table at which the address of
5518
     the relocation entry symbol, adjusted by the addend, resides
5519
     during execution.  */
5520
0
  bfd_vma g = MINUS_ONE;
5521
  /* The section in which the symbol referenced by the relocation is
5522
     located.  */
5523
0
  asection *sec = NULL;
5524
0
  struct mips_elf_link_hash_entry *h = NULL;
5525
  /* TRUE if the symbol referred to by this relocation is a local
5526
     symbol.  */
5527
0
  bool local_p, was_local_p;
5528
  /* TRUE if the symbol referred to by this relocation is a section
5529
     symbol.  */
5530
0
  bool section_p = false;
5531
  /* TRUE if the symbol referred to by this relocation is "_gp_disp".  */
5532
0
  bool gp_disp_p = false;
5533
  /* TRUE if the symbol referred to by this relocation is
5534
     "__gnu_local_gp".  */
5535
0
  bool gnu_local_gp_p = false;
5536
0
  Elf_Internal_Shdr *symtab_hdr;
5537
0
  size_t extsymoff;
5538
0
  unsigned long r_symndx;
5539
0
  int r_type;
5540
  /* TRUE if overflow occurred during the calculation of the
5541
     relocation value.  */
5542
0
  bool overflowed_p;
5543
  /* TRUE if this relocation refers to a MIPS16 function.  */
5544
0
  bool target_is_16_bit_code_p = false;
5545
0
  bool target_is_micromips_code_p = false;
5546
0
  struct mips_elf_link_hash_table *htab;
5547
0
  bfd *dynobj;
5548
0
  bool resolved_to_zero;
5549
5550
0
  dynobj = elf_hash_table (info)->dynobj;
5551
0
  htab = mips_elf_hash_table (info);
5552
0
  BFD_ASSERT (htab != NULL);
5553
5554
  /* Parse the relocation.  */
5555
0
  r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5556
0
  r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5557
0
  p = (input_section->output_section->vma
5558
0
       + input_section->output_offset
5559
0
       + relocation->r_offset);
5560
5561
  /* Assume that there will be no overflow.  */
5562
0
  overflowed_p = false;
5563
5564
  /* Figure out whether or not the symbol is local, and get the offset
5565
     used in the array of hash table entries.  */
5566
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5567
0
  local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5568
0
           local_sections);
5569
0
  was_local_p = local_p;
5570
0
  if (! elf_bad_symtab (input_bfd))
5571
0
    extsymoff = symtab_hdr->sh_info;
5572
0
  else
5573
0
    {
5574
      /* The symbol table does not follow the rule that local symbols
5575
   must come before globals.  */
5576
0
      extsymoff = 0;
5577
0
    }
5578
5579
  /* Figure out the value of the symbol.  */
5580
0
  if (local_p)
5581
0
    {
5582
0
      bool micromips_p = MICROMIPS_P (abfd);
5583
0
      Elf_Internal_Sym *sym;
5584
5585
0
      sym = local_syms + r_symndx;
5586
0
      sec = local_sections[r_symndx];
5587
5588
0
      section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
5589
5590
0
      symbol = sec->output_section->vma + sec->output_offset;
5591
0
      if (!section_p || (sec->flags & SEC_MERGE))
5592
0
  symbol += sym->st_value;
5593
0
      if ((sec->flags & SEC_MERGE) && section_p)
5594
0
  {
5595
0
    addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5596
0
    addend -= symbol;
5597
0
    addend += sec->output_section->vma + sec->output_offset;
5598
0
  }
5599
5600
      /* MIPS16/microMIPS text labels should be treated as odd.  */
5601
0
      if (ELF_ST_IS_COMPRESSED (sym->st_other))
5602
0
  ++symbol;
5603
5604
      /* Record the name of this symbol, for our caller.  */
5605
0
      *namep = bfd_elf_string_from_elf_section (input_bfd,
5606
0
            symtab_hdr->sh_link,
5607
0
            sym->st_name);
5608
0
      if (*namep == NULL || **namep == '\0')
5609
0
  *namep = bfd_section_name (sec);
5610
5611
      /* For relocations against a section symbol and ones against no
5612
   symbol (absolute relocations) infer the ISA mode from the addend.  */
5613
0
      if (section_p || r_symndx == STN_UNDEF)
5614
0
  {
5615
0
    target_is_16_bit_code_p = (addend & 1) && !micromips_p;
5616
0
    target_is_micromips_code_p = (addend & 1) && micromips_p;
5617
0
  }
5618
      /* For relocations against an absolute symbol infer the ISA mode
5619
   from the value of the symbol plus addend.  */
5620
0
      else if (bfd_is_abs_section (sec))
5621
0
  {
5622
0
    target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
5623
0
    target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
5624
0
  }
5625
      /* Otherwise just use the regular symbol annotation available.  */
5626
0
      else
5627
0
  {
5628
0
    target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5629
0
    target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5630
0
  }
5631
0
    }
5632
0
  else
5633
0
    {
5634
      /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ?  */
5635
5636
      /* For global symbols we look up the symbol in the hash-table.  */
5637
0
      h = ((struct mips_elf_link_hash_entry *)
5638
0
     elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5639
      /* Find the real hash-table entry for this symbol.  */
5640
0
      while (h->root.root.type == bfd_link_hash_indirect
5641
0
       || h->root.root.type == bfd_link_hash_warning)
5642
0
  h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5643
5644
      /* Record the name of this symbol, for our caller.  */
5645
0
      *namep = h->root.root.root.string;
5646
5647
      /* See if this is the special _gp_disp symbol.  Note that such a
5648
   symbol must always be a global symbol.  */
5649
0
      if (strcmp (*namep, "_gp_disp") == 0
5650
0
    && ! NEWABI_P (input_bfd))
5651
0
  {
5652
    /* Relocations against _gp_disp are permitted only with
5653
       R_MIPS_HI16 and R_MIPS_LO16 relocations.  */
5654
0
    if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5655
0
      return bfd_reloc_notsupported;
5656
5657
0
    gp_disp_p = true;
5658
0
  }
5659
      /* See if this is the special _gp symbol.  Note that such a
5660
   symbol must always be a global symbol.  */
5661
0
      else if (strcmp (*namep, "__gnu_local_gp") == 0)
5662
0
  gnu_local_gp_p = true;
5663
5664
5665
      /* If this symbol is defined, calculate its address.  Note that
5666
   _gp_disp is a magic symbol, always implicitly defined by the
5667
   linker, so it's inappropriate to check to see whether or not
5668
   its defined.  */
5669
0
      else if ((h->root.root.type == bfd_link_hash_defined
5670
0
    || h->root.root.type == bfd_link_hash_defweak)
5671
0
         && h->root.root.u.def.section)
5672
0
  {
5673
0
    sec = h->root.root.u.def.section;
5674
0
    if (sec->output_section)
5675
0
      symbol = (h->root.root.u.def.value
5676
0
          + sec->output_section->vma
5677
0
          + sec->output_offset);
5678
0
    else
5679
0
      symbol = h->root.root.u.def.value;
5680
0
  }
5681
0
      else if (h->root.root.type == bfd_link_hash_undefweak)
5682
  /* We allow relocations against undefined weak symbols, giving
5683
     it the value zero, so that you can undefined weak functions
5684
     and check to see if they exist by looking at their
5685
     addresses.  */
5686
0
  symbol = 0;
5687
0
      else if (info->unresolved_syms_in_objects == RM_IGNORE
5688
0
         && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5689
0
  symbol = 0;
5690
0
      else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5691
0
           ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5692
0
  {
5693
    /* If this is a dynamic link, we should have created a
5694
       _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5695
       in _bfd_mips_elf_create_dynamic_sections.
5696
       Otherwise, we should define the symbol with a value of 0.
5697
       FIXME: It should probably get into the symbol table
5698
       somehow as well.  */
5699
0
    BFD_ASSERT (! bfd_link_pic (info));
5700
0
    BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5701
0
    symbol = 0;
5702
0
  }
5703
0
      else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5704
0
  {
5705
    /* This is an optional symbol - an Irix specific extension to the
5706
       ELF spec.  Ignore it for now.
5707
       XXX - FIXME - there is more to the spec for OPTIONAL symbols
5708
       than simply ignoring them, but we do not handle this for now.
5709
       For information see the "64-bit ELF Object File Specification"
5710
       which is available from here:
5711
       http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf  */
5712
0
    symbol = 0;
5713
0
  }
5714
0
      else
5715
0
  {
5716
0
          bool reject_undefined
5717
0
      = ((info->unresolved_syms_in_objects == RM_DIAGNOSE
5718
0
    && !info->warn_unresolved_syms)
5719
0
         || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT);
5720
5721
0
    info->callbacks->undefined_symbol
5722
0
      (info, h->root.root.root.string, input_bfd,
5723
0
       input_section, relocation->r_offset, reject_undefined);
5724
5725
0
    if (reject_undefined)
5726
0
      return bfd_reloc_undefined;
5727
5728
0
    symbol = 0;
5729
0
  }
5730
5731
0
      target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5732
0
      target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5733
0
    }
5734
5735
  /* If this is a reference to a 16-bit function with a stub, we need
5736
     to redirect the relocation to the stub unless:
5737
5738
     (a) the relocation is for a MIPS16 JAL;
5739
5740
     (b) the relocation is for a MIPS16 PIC call, and there are no
5741
   non-MIPS16 uses of the GOT slot; or
5742
5743
     (c) the section allows direct references to MIPS16 functions.  */
5744
0
  if (r_type != R_MIPS16_26
5745
0
      && !bfd_link_relocatable (info)
5746
0
      && ((h != NULL
5747
0
     && h->fn_stub != NULL
5748
0
     && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5749
0
    || (local_p
5750
0
        && mips_elf_tdata (input_bfd)->local_stubs != NULL
5751
0
        && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5752
0
      && !section_allows_mips16_refs_p (input_section))
5753
0
    {
5754
      /* This is a 32- or 64-bit call to a 16-bit function.  We should
5755
   have already noticed that we were going to need the
5756
   stub.  */
5757
0
      if (local_p)
5758
0
  {
5759
0
    sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5760
0
    value = 0;
5761
0
  }
5762
0
      else
5763
0
  {
5764
0
    BFD_ASSERT (h->need_fn_stub);
5765
0
    if (h->la25_stub)
5766
0
      {
5767
        /* If a LA25 header for the stub itself exists, point to the
5768
     prepended LUI/ADDIU sequence.  */
5769
0
        sec = h->la25_stub->stub_section;
5770
0
        value = h->la25_stub->offset;
5771
0
      }
5772
0
    else
5773
0
      {
5774
0
        sec = h->fn_stub;
5775
0
        value = 0;
5776
0
      }
5777
0
  }
5778
5779
0
      symbol = sec->output_section->vma + sec->output_offset + value;
5780
      /* The target is 16-bit, but the stub isn't.  */
5781
0
      target_is_16_bit_code_p = false;
5782
0
    }
5783
  /* If this is a MIPS16 call with a stub, that is made through the PLT or
5784
     to a standard MIPS function, we need to redirect the call to the stub.
5785
     Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5786
     indirect calls should use an indirect stub instead.  */
5787
0
  else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
5788
0
     && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5789
0
         || (local_p
5790
0
       && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5791
0
       && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5792
0
     && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5793
0
    {
5794
0
      if (local_p)
5795
0
  sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5796
0
      else
5797
0
  {
5798
    /* If both call_stub and call_fp_stub are defined, we can figure
5799
       out which one to use by checking which one appears in the input
5800
       file.  */
5801
0
    if (h->call_stub != NULL && h->call_fp_stub != NULL)
5802
0
      {
5803
0
        asection *o;
5804
5805
0
        sec = NULL;
5806
0
        for (o = input_bfd->sections; o != NULL; o = o->next)
5807
0
    {
5808
0
      if (CALL_FP_STUB_P (bfd_section_name (o)))
5809
0
        {
5810
0
          sec = h->call_fp_stub;
5811
0
          break;
5812
0
        }
5813
0
    }
5814
0
        if (sec == NULL)
5815
0
    sec = h->call_stub;
5816
0
      }
5817
0
    else if (h->call_stub != NULL)
5818
0
      sec = h->call_stub;
5819
0
    else
5820
0
      sec = h->call_fp_stub;
5821
0
  }
5822
5823
0
      BFD_ASSERT (sec->size > 0);
5824
0
      symbol = sec->output_section->vma + sec->output_offset;
5825
0
    }
5826
  /* If this is a direct call to a PIC function, redirect to the
5827
     non-PIC stub.  */
5828
0
  else if (h != NULL && h->la25_stub
5829
0
     && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5830
0
               target_is_16_bit_code_p))
5831
0
    {
5832
0
  symbol = (h->la25_stub->stub_section->output_section->vma
5833
0
      + h->la25_stub->stub_section->output_offset
5834
0
      + h->la25_stub->offset);
5835
0
  if (ELF_ST_IS_MICROMIPS (h->root.other))
5836
0
    symbol |= 1;
5837
0
    }
5838
  /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5839
     entry is used if a standard PLT entry has also been made.  In this
5840
     case the symbol will have been set by mips_elf_set_plt_sym_value
5841
     to point to the standard PLT entry, so redirect to the compressed
5842
     one.  */
5843
0
  else if ((mips16_branch_reloc_p (r_type)
5844
0
      || micromips_branch_reloc_p (r_type))
5845
0
     && !bfd_link_relocatable (info)
5846
0
     && h != NULL
5847
0
     && h->use_plt_entry
5848
0
     && h->root.plt.plist->comp_offset != MINUS_ONE
5849
0
     && h->root.plt.plist->mips_offset != MINUS_ONE)
5850
0
    {
5851
0
      bool micromips_p = MICROMIPS_P (abfd);
5852
5853
0
      sec = htab->root.splt;
5854
0
      symbol = (sec->output_section->vma
5855
0
    + sec->output_offset
5856
0
    + htab->plt_header_size
5857
0
    + htab->plt_mips_offset
5858
0
    + h->root.plt.plist->comp_offset
5859
0
    + 1);
5860
5861
0
      target_is_16_bit_code_p = !micromips_p;
5862
0
      target_is_micromips_code_p = micromips_p;
5863
0
    }
5864
5865
  /* Make sure MIPS16 and microMIPS are not used together.  */
5866
0
  if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
5867
0
      || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5868
0
   {
5869
0
      _bfd_error_handler
5870
0
  (_("MIPS16 and microMIPS functions cannot call each other"));
5871
0
      return bfd_reloc_notsupported;
5872
0
   }
5873
5874
  /* Calls from 16-bit code to 32-bit code and vice versa require the
5875
     mode change.  However, we can ignore calls to undefined weak symbols,
5876
     which should never be executed at runtime.  This exception is important
5877
     because the assembly writer may have "known" that any definition of the
5878
     symbol would be 16-bit code, and that direct jumps were therefore
5879
     acceptable.  */
5880
0
  *cross_mode_jump_p = (!bfd_link_relocatable (info)
5881
0
      && !(h && h->root.root.type == bfd_link_hash_undefweak)
5882
0
      && ((mips16_branch_reloc_p (r_type)
5883
0
           && !target_is_16_bit_code_p)
5884
0
          || (micromips_branch_reloc_p (r_type)
5885
0
        && !target_is_micromips_code_p)
5886
0
          || ((branch_reloc_p (r_type)
5887
0
         || r_type == R_MIPS_JALR)
5888
0
        && (target_is_16_bit_code_p
5889
0
            || target_is_micromips_code_p))));
5890
5891
0
  resolved_to_zero = (h != NULL
5892
0
          && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
5893
5894
0
  switch (r_type)
5895
0
    {
5896
0
    case R_MIPS16_CALL16:
5897
0
    case R_MIPS16_GOT16:
5898
0
    case R_MIPS_CALL16:
5899
0
    case R_MIPS_GOT16:
5900
0
    case R_MIPS_GOT_PAGE:
5901
0
    case R_MIPS_GOT_DISP:
5902
0
    case R_MIPS_GOT_LO16:
5903
0
    case R_MIPS_CALL_LO16:
5904
0
    case R_MICROMIPS_CALL16:
5905
0
    case R_MICROMIPS_GOT16:
5906
0
    case R_MICROMIPS_GOT_PAGE:
5907
0
    case R_MICROMIPS_GOT_DISP:
5908
0
    case R_MICROMIPS_GOT_LO16:
5909
0
    case R_MICROMIPS_CALL_LO16:
5910
0
      if (resolved_to_zero
5911
0
    && !bfd_link_relocatable (info)
5912
0
    && bfd_reloc_offset_in_range (howto, input_bfd, input_section,
5913
0
          relocation->r_offset)
5914
0
    && mips_elf_nullify_got_load (input_bfd, contents,
5915
0
          relocation, howto, true))
5916
0
  return bfd_reloc_continue;
5917
5918
      /* Fall through.  */
5919
0
    case R_MIPS_GOT_HI16:
5920
0
    case R_MIPS_CALL_HI16:
5921
0
    case R_MICROMIPS_GOT_HI16:
5922
0
    case R_MICROMIPS_CALL_HI16:
5923
0
      if (resolved_to_zero
5924
0
    && htab->use_absolute_zero
5925
0
    && bfd_link_pic (info))
5926
0
  {
5927
    /* Redirect to the special `__gnu_absolute_zero' symbol.  */
5928
0
    h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
5929
0
           false, false, false);
5930
0
    BFD_ASSERT (h != NULL);
5931
0
  }
5932
0
      break;
5933
0
    }
5934
5935
0
  local_p = (h == NULL || mips_use_local_got_p (info, h));
5936
5937
0
  gp0 = _bfd_get_gp_value (input_bfd);
5938
0
  gp = _bfd_get_gp_value (abfd);
5939
0
  if (htab->got_info)
5940
0
    gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5941
5942
0
  if (gnu_local_gp_p)
5943
0
    symbol = gp;
5944
5945
  /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5946
     to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP.  The addend is applied by the
5947
     corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.  */
5948
0
  if (got_page_reloc_p (r_type) && !local_p)
5949
0
    {
5950
0
      r_type = (micromips_reloc_p (r_type)
5951
0
    ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5952
0
      addend = 0;
5953
0
    }
5954
5955
  /* If we haven't already determined the GOT offset, and we're going
5956
     to need it, get it now.  */
5957
0
  switch (r_type)
5958
0
    {
5959
0
    case R_MIPS16_CALL16:
5960
0
    case R_MIPS16_GOT16:
5961
0
    case R_MIPS_CALL16:
5962
0
    case R_MIPS_GOT16:
5963
0
    case R_MIPS_GOT_DISP:
5964
0
    case R_MIPS_GOT_HI16:
5965
0
    case R_MIPS_CALL_HI16:
5966
0
    case R_MIPS_GOT_LO16:
5967
0
    case R_MIPS_CALL_LO16:
5968
0
    case R_MICROMIPS_CALL16:
5969
0
    case R_MICROMIPS_GOT16:
5970
0
    case R_MICROMIPS_GOT_DISP:
5971
0
    case R_MICROMIPS_GOT_HI16:
5972
0
    case R_MICROMIPS_CALL_HI16:
5973
0
    case R_MICROMIPS_GOT_LO16:
5974
0
    case R_MICROMIPS_CALL_LO16:
5975
0
    case R_MIPS_TLS_GD:
5976
0
    case R_MIPS_TLS_GOTTPREL:
5977
0
    case R_MIPS_TLS_LDM:
5978
0
    case R_MIPS16_TLS_GD:
5979
0
    case R_MIPS16_TLS_GOTTPREL:
5980
0
    case R_MIPS16_TLS_LDM:
5981
0
    case R_MICROMIPS_TLS_GD:
5982
0
    case R_MICROMIPS_TLS_GOTTPREL:
5983
0
    case R_MICROMIPS_TLS_LDM:
5984
      /* Find the index into the GOT where this value is located.  */
5985
0
      if (tls_ldm_reloc_p (r_type))
5986
0
  {
5987
0
    g = mips_elf_local_got_index (abfd, input_bfd, info,
5988
0
          0, 0, NULL, r_type);
5989
0
    if (g == MINUS_ONE)
5990
0
      return bfd_reloc_outofrange;
5991
0
  }
5992
0
      else if (!local_p)
5993
0
  {
5994
    /* On VxWorks, CALL relocations should refer to the .got.plt
5995
       entry, which is initialized to point at the PLT stub.  */
5996
0
    if (htab->root.target_os == is_vxworks
5997
0
        && (call_hi16_reloc_p (r_type)
5998
0
      || call_lo16_reloc_p (r_type)
5999
0
      || call16_reloc_p (r_type)))
6000
0
      {
6001
0
        BFD_ASSERT (addend == 0);
6002
0
        BFD_ASSERT (h->root.needs_plt);
6003
0
        g = mips_elf_gotplt_index (info, &h->root);
6004
0
      }
6005
0
    else
6006
0
      {
6007
0
        BFD_ASSERT (addend == 0);
6008
0
        g = mips_elf_global_got_index (abfd, info, input_bfd,
6009
0
               &h->root, r_type);
6010
0
        if (!TLS_RELOC_P (r_type)
6011
0
      && !elf_hash_table (info)->dynamic_sections_created)
6012
    /* This is a static link.  We must initialize the GOT entry.  */
6013
0
    MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
6014
0
      }
6015
0
  }
6016
0
      else if (htab->root.target_os != is_vxworks
6017
0
         && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
6018
  /* The calculation below does not involve "g".  */
6019
0
  break;
6020
0
      else
6021
0
  {
6022
0
    g = mips_elf_local_got_index (abfd, input_bfd, info,
6023
0
          symbol + addend, r_symndx, h, r_type);
6024
0
    if (g == MINUS_ONE)
6025
0
      return bfd_reloc_outofrange;
6026
0
  }
6027
6028
      /* Convert GOT indices to actual offsets.  */
6029
0
      g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
6030
0
      break;
6031
0
    }
6032
6033
  /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
6034
     symbols are resolved by the loader.  Add them to .rela.dyn.  */
6035
0
  if (h != NULL && is_gott_symbol (info, &h->root))
6036
0
    {
6037
0
      Elf_Internal_Rela outrel;
6038
0
      bfd_byte *loc;
6039
0
      asection *s;
6040
6041
0
      s = mips_elf_rel_dyn_section (info, false);
6042
0
      loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6043
6044
0
      outrel.r_offset = (input_section->output_section->vma
6045
0
       + input_section->output_offset
6046
0
       + relocation->r_offset);
6047
0
      outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
6048
0
      outrel.r_addend = addend;
6049
0
      bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
6050
6051
      /* If we've written this relocation for a readonly section,
6052
   we need to set DF_TEXTREL again, so that we do not delete the
6053
   DT_TEXTREL tag.  */
6054
0
      if (MIPS_ELF_READONLY_SECTION (input_section))
6055
0
  info->flags |= DF_TEXTREL;
6056
6057
0
      *valuep = 0;
6058
0
      return bfd_reloc_ok;
6059
0
    }
6060
6061
  /* Figure out what kind of relocation is being performed.  */
6062
0
  switch (r_type)
6063
0
    {
6064
0
    case R_MIPS_NONE:
6065
0
      return bfd_reloc_continue;
6066
6067
0
    case R_MIPS_16:
6068
0
      if (howto->partial_inplace)
6069
0
  addend = _bfd_mips_elf_sign_extend (addend, 16);
6070
0
      value = symbol + addend;
6071
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6072
0
      break;
6073
6074
0
    case R_MIPS_32:
6075
0
    case R_MIPS_REL32:
6076
0
    case R_MIPS_64:
6077
0
      if ((bfd_link_pic (info)
6078
0
     || (htab->root.dynamic_sections_created
6079
0
         && h != NULL
6080
0
         && h->root.def_dynamic
6081
0
         && !h->root.def_regular
6082
0
         && !h->has_static_relocs))
6083
0
    && r_symndx != STN_UNDEF
6084
0
    && (h == NULL
6085
0
        || h->root.root.type != bfd_link_hash_undefweak
6086
0
        || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
6087
0
      && !resolved_to_zero))
6088
0
    && (input_section->flags & SEC_ALLOC) != 0)
6089
0
  {
6090
    /* If we're creating a shared library, then we can't know
6091
       where the symbol will end up.  So, we create a relocation
6092
       record in the output, and leave the job up to the dynamic
6093
       linker.  We must do the same for executable references to
6094
       shared library symbols, unless we've decided to use copy
6095
       relocs or PLTs instead.  */
6096
0
    value = addend;
6097
0
    if (!mips_elf_create_dynamic_relocation (abfd,
6098
0
               info,
6099
0
               relocation,
6100
0
               h,
6101
0
               sec,
6102
0
               symbol,
6103
0
               &value,
6104
0
               input_section))
6105
0
      return bfd_reloc_undefined;
6106
0
  }
6107
0
      else
6108
0
  {
6109
0
    if (r_type != R_MIPS_REL32)
6110
0
      value = symbol + addend;
6111
0
    else
6112
0
      value = addend;
6113
0
  }
6114
0
      value &= howto->dst_mask;
6115
0
      break;
6116
6117
0
    case R_MIPS_PC32:
6118
0
      value = symbol + addend - p;
6119
0
      value &= howto->dst_mask;
6120
0
      break;
6121
6122
0
    case R_MIPS16_26:
6123
      /* The calculation for R_MIPS16_26 is just the same as for an
6124
   R_MIPS_26.  It's only the storage of the relocated field into
6125
   the output file that's different.  That's handled in
6126
   mips_elf_perform_relocation.  So, we just fall through to the
6127
   R_MIPS_26 case here.  */
6128
0
    case R_MIPS_26:
6129
0
    case R_MICROMIPS_26_S1:
6130
0
      {
6131
0
  unsigned int shift;
6132
6133
  /* Shift is 2, unusually, for microMIPS JALX.  */
6134
0
  shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
6135
6136
0
  if (howto->partial_inplace && !section_p)
6137
0
    value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
6138
0
  else
6139
0
    value = addend;
6140
0
  value += symbol;
6141
6142
  /* Make sure the target of a jump is suitably aligned.  Bit 0 must
6143
     be the correct ISA mode selector except for weak undefined
6144
     symbols.  */
6145
0
  if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6146
0
      && (*cross_mode_jump_p
6147
0
    ? (value & 3) != (r_type == R_MIPS_26)
6148
0
    : (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
6149
0
    return bfd_reloc_outofrange;
6150
6151
0
  value >>= shift;
6152
0
  if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6153
0
    overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
6154
0
  value &= howto->dst_mask;
6155
0
      }
6156
0
      break;
6157
6158
0
    case R_MIPS_TLS_DTPREL_HI16:
6159
0
    case R_MIPS16_TLS_DTPREL_HI16:
6160
0
    case R_MICROMIPS_TLS_DTPREL_HI16:
6161
0
      value = (mips_elf_high (addend + symbol - dtprel_base (info))
6162
0
         & howto->dst_mask);
6163
0
      break;
6164
6165
0
    case R_MIPS_TLS_DTPREL_LO16:
6166
0
    case R_MIPS_TLS_DTPREL32:
6167
0
    case R_MIPS_TLS_DTPREL64:
6168
0
    case R_MIPS16_TLS_DTPREL_LO16:
6169
0
    case R_MICROMIPS_TLS_DTPREL_LO16:
6170
0
      value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
6171
0
      break;
6172
6173
0
    case R_MIPS_TLS_TPREL_HI16:
6174
0
    case R_MIPS16_TLS_TPREL_HI16:
6175
0
    case R_MICROMIPS_TLS_TPREL_HI16:
6176
0
      value = (mips_elf_high (addend + symbol - tprel_base (info))
6177
0
         & howto->dst_mask);
6178
0
      break;
6179
6180
0
    case R_MIPS_TLS_TPREL_LO16:
6181
0
    case R_MIPS_TLS_TPREL32:
6182
0
    case R_MIPS_TLS_TPREL64:
6183
0
    case R_MIPS16_TLS_TPREL_LO16:
6184
0
    case R_MICROMIPS_TLS_TPREL_LO16:
6185
0
      value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
6186
0
      break;
6187
6188
0
    case R_MIPS_HI16:
6189
0
    case R_MIPS16_HI16:
6190
0
    case R_MICROMIPS_HI16:
6191
0
      if (!gp_disp_p)
6192
0
  {
6193
0
    value = mips_elf_high (addend + symbol);
6194
0
    value &= howto->dst_mask;
6195
0
  }
6196
0
      else
6197
0
  {
6198
    /* For MIPS16 ABI code we generate this sequence
6199
    0: li      $v0,%hi(_gp_disp)
6200
    4: addiupc $v1,%lo(_gp_disp)
6201
    8: sll     $v0,16
6202
         12: addu    $v0,$v1
6203
         14: move    $gp,$v0
6204
       So the offsets of hi and lo relocs are the same, but the
6205
       base $pc is that used by the ADDIUPC instruction at $t9 + 4.
6206
       ADDIUPC clears the low two bits of the instruction address,
6207
       so the base is ($t9 + 4) & ~3.  */
6208
0
    if (r_type == R_MIPS16_HI16)
6209
0
      value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
6210
    /* The microMIPS .cpload sequence uses the same assembly
6211
       instructions as the traditional psABI version, but the
6212
       incoming $t9 has the low bit set.  */
6213
0
    else if (r_type == R_MICROMIPS_HI16)
6214
0
      value = mips_elf_high (addend + gp - p - 1);
6215
0
    else
6216
0
      value = mips_elf_high (addend + gp - p);
6217
0
  }
6218
0
      break;
6219
6220
0
    case R_MIPS_LO16:
6221
0
    case R_MIPS16_LO16:
6222
0
    case R_MICROMIPS_LO16:
6223
0
    case R_MICROMIPS_HI0_LO16:
6224
0
      if (!gp_disp_p)
6225
0
  value = (symbol + addend) & howto->dst_mask;
6226
0
      else
6227
0
  {
6228
    /* See the comment for R_MIPS16_HI16 above for the reason
6229
       for this conditional.  */
6230
0
    if (r_type == R_MIPS16_LO16)
6231
0
      value = addend + gp - (p & ~(bfd_vma) 0x3);
6232
0
    else if (r_type == R_MICROMIPS_LO16
6233
0
       || r_type == R_MICROMIPS_HI0_LO16)
6234
0
      value = addend + gp - p + 3;
6235
0
    else
6236
0
      value = addend + gp - p + 4;
6237
    /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6238
       for overflow.  But, on, say, IRIX5, relocations against
6239
       _gp_disp are normally generated from the .cpload
6240
       pseudo-op.  It generates code that normally looks like
6241
       this:
6242
6243
         lui    $gp,%hi(_gp_disp)
6244
         addiu  $gp,$gp,%lo(_gp_disp)
6245
         addu   $gp,$gp,$t9
6246
6247
       Here $t9 holds the address of the function being called,
6248
       as required by the MIPS ELF ABI.  The R_MIPS_LO16
6249
       relocation can easily overflow in this situation, but the
6250
       R_MIPS_HI16 relocation will handle the overflow.
6251
       Therefore, we consider this a bug in the MIPS ABI, and do
6252
       not check for overflow here.  */
6253
0
  }
6254
0
      break;
6255
6256
0
    case R_MIPS_LITERAL:
6257
0
    case R_MICROMIPS_LITERAL:
6258
      /* Because we don't merge literal sections, we can handle this
6259
   just like R_MIPS_GPREL16.  In the long run, we should merge
6260
   shared literals, and then we will need to additional work
6261
   here.  */
6262
6263
      /* Fall through.  */
6264
6265
0
    case R_MIPS16_GPREL:
6266
      /* The R_MIPS16_GPREL performs the same calculation as
6267
   R_MIPS_GPREL16, but stores the relocated bits in a different
6268
   order.  We don't need to do anything special here; the
6269
   differences are handled in mips_elf_perform_relocation.  */
6270
0
    case R_MIPS_GPREL16:
6271
0
    case R_MICROMIPS_GPREL7_S2:
6272
0
    case R_MICROMIPS_GPREL16:
6273
0
      {
6274
0
  int bits = howto->bitsize + howto->rightshift;
6275
  /* Only sign-extend the addend if it was extracted from the
6276
     instruction.  If the addend was separate, leave it alone,
6277
     otherwise we may lose significant bits.  */
6278
0
  if (howto->partial_inplace)
6279
0
    addend = _bfd_mips_elf_sign_extend (addend, bits);
6280
0
  value = symbol + addend - gp;
6281
  /* If the symbol was local, any earlier relocatable links will
6282
     have adjusted its addend with the gp offset, so compensate
6283
     for that now.  Don't do it for symbols forced local in this
6284
     link, though, since they won't have had the gp offset applied
6285
     to them before.  */
6286
0
  if (was_local_p)
6287
0
    value += gp0;
6288
0
  if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6289
0
    overflowed_p = mips_elf_overflow_p (value, bits);
6290
0
      }
6291
0
      break;
6292
6293
0
    case R_MIPS16_GOT16:
6294
0
    case R_MIPS16_CALL16:
6295
0
    case R_MIPS_GOT16:
6296
0
    case R_MIPS_CALL16:
6297
0
    case R_MICROMIPS_GOT16:
6298
0
    case R_MICROMIPS_CALL16:
6299
      /* VxWorks does not have separate local and global semantics for
6300
   R_MIPS*_GOT16; every relocation evaluates to "G".  */
6301
0
      if (htab->root.target_os != is_vxworks && local_p)
6302
0
  {
6303
0
    value = mips_elf_got16_entry (abfd, input_bfd, info,
6304
0
          symbol + addend, !was_local_p);
6305
0
    if (value == MINUS_ONE)
6306
0
      return bfd_reloc_outofrange;
6307
0
    value
6308
0
      = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6309
0
    overflowed_p = mips_elf_overflow_p (value, 16);
6310
0
    break;
6311
0
  }
6312
6313
      /* Fall through.  */
6314
6315
0
    case R_MIPS_TLS_GD:
6316
0
    case R_MIPS_TLS_GOTTPREL:
6317
0
    case R_MIPS_TLS_LDM:
6318
0
    case R_MIPS_GOT_DISP:
6319
0
    case R_MIPS16_TLS_GD:
6320
0
    case R_MIPS16_TLS_GOTTPREL:
6321
0
    case R_MIPS16_TLS_LDM:
6322
0
    case R_MICROMIPS_TLS_GD:
6323
0
    case R_MICROMIPS_TLS_GOTTPREL:
6324
0
    case R_MICROMIPS_TLS_LDM:
6325
0
    case R_MICROMIPS_GOT_DISP:
6326
0
      value = g;
6327
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6328
0
      break;
6329
6330
0
    case R_MIPS_GPREL32:
6331
0
      value = (addend + symbol + gp0 - gp);
6332
0
      if (!save_addend)
6333
0
  value &= howto->dst_mask;
6334
0
      break;
6335
6336
0
    case R_MIPS_PC16:
6337
0
    case R_MIPS_GNU_REL16_S2:
6338
0
      if (howto->partial_inplace)
6339
0
  addend = _bfd_mips_elf_sign_extend (addend, 18);
6340
6341
      /* No need to exclude weak undefined symbols here as they resolve
6342
   to 0 and never set `*cross_mode_jump_p', so this alignment check
6343
   will never trigger for them.  */
6344
0
      if (*cross_mode_jump_p
6345
0
    ? ((symbol + addend) & 3) != 1
6346
0
    : ((symbol + addend) & 3) != 0)
6347
0
  return bfd_reloc_outofrange;
6348
6349
0
      value = symbol + addend - p;
6350
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6351
0
  overflowed_p = mips_elf_overflow_p (value, 18);
6352
0
      value >>= howto->rightshift;
6353
0
      value &= howto->dst_mask;
6354
0
      break;
6355
6356
0
    case R_MIPS16_PC16_S1:
6357
0
      if (howto->partial_inplace)
6358
0
  addend = _bfd_mips_elf_sign_extend (addend, 17);
6359
6360
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6361
0
    && (*cross_mode_jump_p
6362
0
        ? ((symbol + addend) & 3) != 0
6363
0
        : ((symbol + addend) & 1) == 0))
6364
0
  return bfd_reloc_outofrange;
6365
6366
0
      value = symbol + addend - p;
6367
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6368
0
  overflowed_p = mips_elf_overflow_p (value, 17);
6369
0
      value >>= howto->rightshift;
6370
0
      value &= howto->dst_mask;
6371
0
      break;
6372
6373
0
    case R_MIPS_PC21_S2:
6374
0
      if (howto->partial_inplace)
6375
0
  addend = _bfd_mips_elf_sign_extend (addend, 23);
6376
6377
0
      if ((symbol + addend) & 3)
6378
0
  return bfd_reloc_outofrange;
6379
6380
0
      value = symbol + addend - p;
6381
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6382
0
  overflowed_p = mips_elf_overflow_p (value, 23);
6383
0
      value >>= howto->rightshift;
6384
0
      value &= howto->dst_mask;
6385
0
      break;
6386
6387
0
    case R_MIPS_PC26_S2:
6388
0
      if (howto->partial_inplace)
6389
0
  addend = _bfd_mips_elf_sign_extend (addend, 28);
6390
6391
0
      if ((symbol + addend) & 3)
6392
0
  return bfd_reloc_outofrange;
6393
6394
0
      value = symbol + addend - p;
6395
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6396
0
  overflowed_p = mips_elf_overflow_p (value, 28);
6397
0
      value >>= howto->rightshift;
6398
0
      value &= howto->dst_mask;
6399
0
      break;
6400
6401
0
    case R_MIPS_PC18_S3:
6402
0
      if (howto->partial_inplace)
6403
0
  addend = _bfd_mips_elf_sign_extend (addend, 21);
6404
6405
0
      if ((symbol + addend) & 7)
6406
0
  return bfd_reloc_outofrange;
6407
6408
0
      value = symbol + addend - ((p | 7) ^ 7);
6409
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6410
0
  overflowed_p = mips_elf_overflow_p (value, 21);
6411
0
      value >>= howto->rightshift;
6412
0
      value &= howto->dst_mask;
6413
0
      break;
6414
6415
0
    case R_MIPS_PC19_S2:
6416
0
      if (howto->partial_inplace)
6417
0
  addend = _bfd_mips_elf_sign_extend (addend, 21);
6418
6419
0
      if ((symbol + addend) & 3)
6420
0
  return bfd_reloc_outofrange;
6421
6422
0
      value = symbol + addend - p;
6423
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6424
0
  overflowed_p = mips_elf_overflow_p (value, 21);
6425
0
      value >>= howto->rightshift;
6426
0
      value &= howto->dst_mask;
6427
0
      break;
6428
6429
0
    case R_MIPS_PCHI16:
6430
0
      value = mips_elf_high (symbol + addend - p);
6431
0
      value &= howto->dst_mask;
6432
0
      break;
6433
6434
0
    case R_MIPS_PCLO16:
6435
0
      if (howto->partial_inplace)
6436
0
  addend = _bfd_mips_elf_sign_extend (addend, 16);
6437
0
      value = symbol + addend - p;
6438
0
      value &= howto->dst_mask;
6439
0
      break;
6440
6441
0
    case R_MICROMIPS_PC7_S1:
6442
0
      if (howto->partial_inplace)
6443
0
  addend = _bfd_mips_elf_sign_extend (addend, 8);
6444
6445
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6446
0
    && (*cross_mode_jump_p
6447
0
        ? ((symbol + addend + 2) & 3) != 0
6448
0
        : ((symbol + addend + 2) & 1) == 0))
6449
0
  return bfd_reloc_outofrange;
6450
6451
0
      value = symbol + addend - p;
6452
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6453
0
  overflowed_p = mips_elf_overflow_p (value, 8);
6454
0
      value >>= howto->rightshift;
6455
0
      value &= howto->dst_mask;
6456
0
      break;
6457
6458
0
    case R_MICROMIPS_PC10_S1:
6459
0
      if (howto->partial_inplace)
6460
0
  addend = _bfd_mips_elf_sign_extend (addend, 11);
6461
6462
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6463
0
    && (*cross_mode_jump_p
6464
0
        ? ((symbol + addend + 2) & 3) != 0
6465
0
        : ((symbol + addend + 2) & 1) == 0))
6466
0
  return bfd_reloc_outofrange;
6467
6468
0
      value = symbol + addend - p;
6469
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6470
0
  overflowed_p = mips_elf_overflow_p (value, 11);
6471
0
      value >>= howto->rightshift;
6472
0
      value &= howto->dst_mask;
6473
0
      break;
6474
6475
0
    case R_MICROMIPS_PC16_S1:
6476
0
      if (howto->partial_inplace)
6477
0
  addend = _bfd_mips_elf_sign_extend (addend, 17);
6478
6479
0
      if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6480
0
    && (*cross_mode_jump_p
6481
0
        ? ((symbol + addend) & 3) != 0
6482
0
        : ((symbol + addend) & 1) == 0))
6483
0
  return bfd_reloc_outofrange;
6484
6485
0
      value = symbol + addend - p;
6486
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6487
0
  overflowed_p = mips_elf_overflow_p (value, 17);
6488
0
      value >>= howto->rightshift;
6489
0
      value &= howto->dst_mask;
6490
0
      break;
6491
6492
0
    case R_MICROMIPS_PC23_S2:
6493
0
      if (howto->partial_inplace)
6494
0
  addend = _bfd_mips_elf_sign_extend (addend, 25);
6495
0
      value = symbol + addend - ((p | 3) ^ 3);
6496
0
      if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6497
0
  overflowed_p = mips_elf_overflow_p (value, 25);
6498
0
      value >>= howto->rightshift;
6499
0
      value &= howto->dst_mask;
6500
0
      break;
6501
6502
0
    case R_MIPS_GOT_HI16:
6503
0
    case R_MIPS_CALL_HI16:
6504
0
    case R_MICROMIPS_GOT_HI16:
6505
0
    case R_MICROMIPS_CALL_HI16:
6506
      /* We're allowed to handle these two relocations identically.
6507
   The dynamic linker is allowed to handle the CALL relocations
6508
   differently by creating a lazy evaluation stub.  */
6509
0
      value = g;
6510
0
      value = mips_elf_high (value);
6511
0
      value &= howto->dst_mask;
6512
0
      break;
6513
6514
0
    case R_MIPS_GOT_LO16:
6515
0
    case R_MIPS_CALL_LO16:
6516
0
    case R_MICROMIPS_GOT_LO16:
6517
0
    case R_MICROMIPS_CALL_LO16:
6518
0
      value = g & howto->dst_mask;
6519
0
      break;
6520
6521
0
    case R_MIPS_GOT_PAGE:
6522
0
    case R_MICROMIPS_GOT_PAGE:
6523
0
      value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
6524
0
      if (value == MINUS_ONE)
6525
0
  return bfd_reloc_outofrange;
6526
0
      value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6527
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6528
0
      break;
6529
6530
0
    case R_MIPS_GOT_OFST:
6531
0
    case R_MICROMIPS_GOT_OFST:
6532
0
      if (local_p)
6533
0
  mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
6534
0
      else
6535
0
  value = addend;
6536
0
      overflowed_p = mips_elf_overflow_p (value, 16);
6537
0
      break;
6538
6539
0
    case R_MIPS_SUB:
6540
0
    case R_MICROMIPS_SUB:
6541
0
      value = symbol - addend;
6542
0
      value &= howto->dst_mask;
6543
0
      break;
6544
6545
0
    case R_MIPS_HIGHER:
6546
0
    case R_MICROMIPS_HIGHER:
6547
0
      value = mips_elf_higher (addend + symbol);
6548
0
      value &= howto->dst_mask;
6549
0
      break;
6550
6551
0
    case R_MIPS_HIGHEST:
6552
0
    case R_MICROMIPS_HIGHEST:
6553
0
      value = mips_elf_highest (addend + symbol);
6554
0
      value &= howto->dst_mask;
6555
0
      break;
6556
6557
0
    case R_MIPS_SCN_DISP:
6558
0
    case R_MICROMIPS_SCN_DISP:
6559
0
      value = symbol + addend - sec->output_offset;
6560
0
      value &= howto->dst_mask;
6561
0
      break;
6562
6563
0
    case R_MIPS_JALR:
6564
0
    case R_MICROMIPS_JALR:
6565
      /* This relocation is only a hint.  In some cases, we optimize
6566
   it into a bal instruction.  But we don't try to optimize
6567
   when the symbol does not resolve locally.  */
6568
0
      if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
6569
0
  return bfd_reloc_continue;
6570
      /* We can't optimize cross-mode jumps either.  */
6571
0
      if (*cross_mode_jump_p)
6572
0
  return bfd_reloc_continue;
6573
0
      value = symbol + addend;
6574
      /* Neither we can non-instruction-aligned targets.  */
6575
0
      if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
6576
0
  return bfd_reloc_continue;
6577
0
      break;
6578
6579
0
    case R_MIPS_PJUMP:
6580
0
    case R_MIPS_GNU_VTINHERIT:
6581
0
    case R_MIPS_GNU_VTENTRY:
6582
      /* We don't do anything with these at present.  */
6583
0
      return bfd_reloc_continue;
6584
6585
0
    default:
6586
      /* An unrecognized relocation type.  */
6587
0
      return bfd_reloc_notsupported;
6588
0
    }
6589
6590
  /* Store the VALUE for our caller.  */
6591
0
  *valuep = value;
6592
0
  return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6593
0
}
6594
6595
/* It has been determined that the result of the RELOCATION is the
6596
   VALUE.  Use HOWTO to place VALUE into the output file at the
6597
   appropriate position.  The SECTION is the section to which the
6598
   relocation applies.
6599
   CROSS_MODE_JUMP_P is true if the relocation field
6600
   is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6601
6602
   Returns FALSE if anything goes wrong.  */
6603
6604
static bool
6605
mips_elf_perform_relocation (struct bfd_link_info *info,
6606
           reloc_howto_type *howto,
6607
           const Elf_Internal_Rela *relocation,
6608
           bfd_vma value, bfd *input_bfd,
6609
           asection *input_section, bfd_byte *contents,
6610
           bool cross_mode_jump_p)
6611
0
{
6612
0
  bfd_vma x;
6613
0
  bfd_byte *location;
6614
0
  int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6615
6616
  /* Figure out where the relocation is occurring.  */
6617
0
  location = contents + relocation->r_offset;
6618
6619
0
  _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
6620
6621
  /* Obtain the current value.  */
6622
0
  x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6623
6624
  /* Clear the field we are setting.  */
6625
0
  x &= ~howto->dst_mask;
6626
6627
  /* Set the field.  */
6628
0
  x |= (value & howto->dst_mask);
6629
6630
  /* Detect incorrect JALX usage.  If required, turn JAL or BAL into JALX.  */
6631
0
  if (!cross_mode_jump_p && jal_reloc_p (r_type))
6632
0
    {
6633
0
      bfd_vma opcode = x >> 26;
6634
6635
0
      if (r_type == R_MIPS16_26 ? opcode == 0x7
6636
0
    : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
6637
0
    : opcode == 0x1d)
6638
0
  {
6639
0
    info->callbacks->einfo
6640
0
      (_("%X%H: unsupported JALX to the same ISA mode\n"),
6641
0
       input_bfd, input_section, relocation->r_offset);
6642
0
    return true;
6643
0
  }
6644
0
    }
6645
0
  if (cross_mode_jump_p && jal_reloc_p (r_type))
6646
0
    {
6647
0
      bool ok;
6648
0
      bfd_vma opcode = x >> 26;
6649
0
      bfd_vma jalx_opcode;
6650
6651
      /* Check to see if the opcode is already JAL or JALX.  */
6652
0
      if (r_type == R_MIPS16_26)
6653
0
  {
6654
0
    ok = ((opcode == 0x6) || (opcode == 0x7));
6655
0
    jalx_opcode = 0x7;
6656
0
  }
6657
0
      else if (r_type == R_MICROMIPS_26_S1)
6658
0
  {
6659
0
    ok = ((opcode == 0x3d) || (opcode == 0x3c));
6660
0
    jalx_opcode = 0x3c;
6661
0
  }
6662
0
      else
6663
0
  {
6664
0
    ok = ((opcode == 0x3) || (opcode == 0x1d));
6665
0
    jalx_opcode = 0x1d;
6666
0
  }
6667
6668
      /* If the opcode is not JAL or JALX, there's a problem.  We cannot
6669
   convert J or JALS to JALX.  */
6670
0
      if (!ok)
6671
0
  {
6672
0
    info->callbacks->einfo
6673
0
      (_("%X%H: unsupported jump between ISA modes; "
6674
0
         "consider recompiling with interlinking enabled\n"),
6675
0
       input_bfd, input_section, relocation->r_offset);
6676
0
    return true;
6677
0
  }
6678
6679
      /* Make this the JALX opcode.  */
6680
0
      x = (x & ~(0x3fu << 26)) | (jalx_opcode << 26);
6681
0
    }
6682
0
  else if (cross_mode_jump_p && b_reloc_p (r_type))
6683
0
    {
6684
0
      bool ok = false;
6685
0
      bfd_vma opcode = x >> 16;
6686
0
      bfd_vma jalx_opcode = 0;
6687
0
      bfd_vma sign_bit = 0;
6688
0
      bfd_vma addr;
6689
0
      bfd_vma dest;
6690
6691
0
      if (r_type == R_MICROMIPS_PC16_S1)
6692
0
  {
6693
0
    ok = opcode == 0x4060;
6694
0
    jalx_opcode = 0x3c;
6695
0
    sign_bit = 0x10000;
6696
0
    value <<= 1;
6697
0
  }
6698
0
      else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
6699
0
  {
6700
0
    ok = opcode == 0x411;
6701
0
    jalx_opcode = 0x1d;
6702
0
    sign_bit = 0x20000;
6703
0
    value <<= 2;
6704
0
  }
6705
6706
0
      if (ok && !bfd_link_pic (info))
6707
0
  {
6708
0
    addr = (input_section->output_section->vma
6709
0
      + input_section->output_offset
6710
0
      + relocation->r_offset
6711
0
      + 4);
6712
0
    dest = (addr
6713
0
      + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
6714
6715
0
    if ((addr >> 28) << 28 != (dest >> 28) << 28)
6716
0
      {
6717
0
        info->callbacks->einfo
6718
0
    (_("%X%H: cannot convert branch between ISA modes "
6719
0
       "to JALX: relocation out of range\n"),
6720
0
     input_bfd, input_section, relocation->r_offset);
6721
0
        return true;
6722
0
      }
6723
6724
    /* Make this the JALX opcode.  */
6725
0
    x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
6726
0
  }
6727
0
      else if (!mips_elf_hash_table (info)->ignore_branch_isa)
6728
0
  {
6729
0
    info->callbacks->einfo
6730
0
      (_("%X%H: unsupported branch between ISA modes\n"),
6731
0
       input_bfd, input_section, relocation->r_offset);
6732
0
    return true;
6733
0
  }
6734
0
    }
6735
6736
  /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6737
     range.  */
6738
0
  if (!bfd_link_relocatable (info)
6739
0
      && !cross_mode_jump_p
6740
0
      && ((JAL_TO_BAL_P (input_bfd)
6741
0
     && r_type == R_MIPS_26
6742
0
     && (x >> 26) == 0x3)     /* jal addr */
6743
0
    || (JALR_TO_BAL_P (input_bfd)
6744
0
        && r_type == R_MIPS_JALR
6745
0
        && x == 0x0320f809)   /* jalr t9 */
6746
0
    || (JR_TO_B_P (input_bfd)
6747
0
        && r_type == R_MIPS_JALR
6748
0
        && (x & ~1) == 0x03200008))) /* jr t9 / jalr zero, t9 */
6749
0
    {
6750
0
      bfd_vma addr;
6751
0
      bfd_vma dest;
6752
0
      bfd_signed_vma off;
6753
6754
0
      addr = (input_section->output_section->vma
6755
0
        + input_section->output_offset
6756
0
        + relocation->r_offset
6757
0
        + 4);
6758
0
      if (r_type == R_MIPS_26)
6759
0
  dest = (value << 2) | ((addr >> 28) << 28);
6760
0
      else
6761
0
  dest = value;
6762
0
      off = dest - addr;
6763
0
      if (off <= 0x1ffff && off >= -0x20000)
6764
0
  {
6765
0
    if ((x & ~1) == 0x03200008)   /* jr t9 / jalr zero, t9 */
6766
0
      x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff);   /* b addr */
6767
0
    else
6768
0
      x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff);   /* bal addr */
6769
0
  }
6770
0
    }
6771
6772
  /* Put the value into the output.  */
6773
0
  mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
6774
6775
0
  _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
6776
0
             location);
6777
6778
0
  return true;
6779
0
}
6780

6781
/* Create a rel.dyn relocation for the dynamic linker to resolve.  REL
6782
   is the original relocation, which is now being transformed into a
6783
   dynamic relocation.  The ADDENDP is adjusted if necessary; the
6784
   caller should store the result in place of the original addend.  */
6785
6786
static bool
6787
mips_elf_create_dynamic_relocation (bfd *output_bfd,
6788
            struct bfd_link_info *info,
6789
            const Elf_Internal_Rela *rel,
6790
            struct mips_elf_link_hash_entry *h,
6791
            asection *sec, bfd_vma symbol,
6792
            bfd_vma *addendp, asection *input_section)
6793
0
{
6794
0
  Elf_Internal_Rela outrel[3];
6795
0
  asection *sreloc;
6796
0
  bfd *dynobj;
6797
0
  int r_type;
6798
0
  long indx;
6799
0
  bool defined_p;
6800
0
  struct mips_elf_link_hash_table *htab;
6801
6802
0
  htab = mips_elf_hash_table (info);
6803
0
  BFD_ASSERT (htab != NULL);
6804
6805
0
  r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6806
0
  dynobj = elf_hash_table (info)->dynobj;
6807
0
  sreloc = mips_elf_rel_dyn_section (info, false);
6808
0
  BFD_ASSERT (sreloc != NULL);
6809
0
  BFD_ASSERT (sreloc->contents != NULL);
6810
0
  BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6811
0
        < sreloc->size);
6812
6813
0
  outrel[0].r_offset =
6814
0
    _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6815
0
  if (ABI_64_P (output_bfd))
6816
0
    {
6817
0
      outrel[1].r_offset =
6818
0
  _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6819
0
      outrel[2].r_offset =
6820
0
  _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6821
0
    }
6822
6823
0
  if (outrel[0].r_offset == MINUS_ONE)
6824
    /* The relocation field has been deleted.  */
6825
0
    return true;
6826
6827
0
  if (outrel[0].r_offset == MINUS_TWO)
6828
0
    {
6829
      /* The relocation field has been converted into a relative value of
6830
   some sort.  Functions like _bfd_elf_write_section_eh_frame expect
6831
   the field to be fully relocated, so add in the symbol's value.  */
6832
0
      *addendp += symbol;
6833
0
      return true;
6834
0
    }
6835
6836
  /* We must now calculate the dynamic symbol table index to use
6837
     in the relocation.  */
6838
0
  if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6839
0
    {
6840
0
      BFD_ASSERT (htab->root.target_os == is_vxworks
6841
0
      || h->global_got_area != GGA_NONE);
6842
0
      indx = h->root.dynindx;
6843
0
      if (SGI_COMPAT (output_bfd))
6844
0
  defined_p = h->root.def_regular;
6845
0
      else
6846
  /* ??? glibc's ld.so just adds the final GOT entry to the
6847
     relocation field.  It therefore treats relocs against
6848
     defined symbols in the same way as relocs against
6849
     undefined symbols.  */
6850
0
  defined_p = false;
6851
0
    }
6852
0
  else
6853
0
    {
6854
0
      if (sec != NULL && bfd_is_abs_section (sec))
6855
0
  indx = 0;
6856
0
      else if (sec == NULL || sec->owner == NULL)
6857
0
  {
6858
0
    BFD_ASSERT (0);
6859
0
    bfd_set_error (bfd_error_bad_value);
6860
0
    return false;
6861
0
  }
6862
0
      else
6863
0
  {
6864
0
    indx = elf_section_data (sec->output_section)->dynindx;
6865
0
    if (indx == 0)
6866
0
      {
6867
0
        asection *osec = htab->root.text_index_section;
6868
0
        indx = elf_section_data (osec)->dynindx;
6869
0
      }
6870
0
    if (indx == 0)
6871
0
      abort ();
6872
0
  }
6873
6874
      /* Instead of generating a relocation using the section
6875
   symbol, we may as well make it a fully relative
6876
   relocation.  We want to avoid generating relocations to
6877
   local symbols because we used to generate them
6878
   incorrectly, without adding the original symbol value,
6879
   which is mandated by the ABI for section symbols.  In
6880
   order to give dynamic loaders and applications time to
6881
   phase out the incorrect use, we refrain from emitting
6882
   section-relative relocations.  It's not like they're
6883
   useful, after all.  This should be a bit more efficient
6884
   as well.  */
6885
      /* ??? Although this behavior is compatible with glibc's ld.so,
6886
   the ABI says that relocations against STN_UNDEF should have
6887
   a symbol value of 0.  Irix rld honors this, so relocations
6888
   against STN_UNDEF have no effect.  */
6889
0
      if (!SGI_COMPAT (output_bfd))
6890
0
  indx = 0;
6891
0
      defined_p = true;
6892
0
    }
6893
6894
  /* If the relocation was previously an absolute relocation and
6895
     this symbol will not be referred to by the relocation, we must
6896
     adjust it by the value we give it in the dynamic symbol table.
6897
     Otherwise leave the job up to the dynamic linker.  */
6898
0
  if (defined_p && r_type != R_MIPS_REL32)
6899
0
    *addendp += symbol;
6900
6901
0
  if (htab->root.target_os == is_vxworks)
6902
    /* VxWorks uses non-relative relocations for this.  */
6903
0
    outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6904
0
  else
6905
    /* The relocation is always an REL32 relocation because we don't
6906
       know where the shared library will wind up at load-time.  */
6907
0
    outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6908
0
           R_MIPS_REL32);
6909
6910
  /* For strict adherence to the ABI specification, we should
6911
     generate a R_MIPS_64 relocation record by itself before the
6912
     _REL32/_64 record as well, such that the addend is read in as
6913
     a 64-bit value (REL32 is a 32-bit relocation, after all).
6914
     However, since none of the existing ELF64 MIPS dynamic
6915
     loaders seems to care, we don't waste space with these
6916
     artificial relocations.  If this turns out to not be true,
6917
     mips_elf_allocate_dynamic_relocation() should be tweaked so
6918
     as to make room for a pair of dynamic relocations per
6919
     invocation if ABI_64_P, and here we should generate an
6920
     additional relocation record with R_MIPS_64 by itself for a
6921
     NULL symbol before this relocation record.  */
6922
0
  outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6923
0
         ABI_64_P (output_bfd)
6924
0
         ? R_MIPS_64
6925
0
         : R_MIPS_NONE);
6926
0
  outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6927
6928
  /* Adjust the output offset of the relocation to reference the
6929
     correct location in the output file.  */
6930
0
  outrel[0].r_offset += (input_section->output_section->vma
6931
0
       + input_section->output_offset);
6932
0
  outrel[1].r_offset += (input_section->output_section->vma
6933
0
       + input_section->output_offset);
6934
0
  outrel[2].r_offset += (input_section->output_section->vma
6935
0
       + input_section->output_offset);
6936
6937
  /* Put the relocation back out.  We have to use the special
6938
     relocation outputter in the 64-bit case since the 64-bit
6939
     relocation format is non-standard.  */
6940
0
  if (ABI_64_P (output_bfd))
6941
0
    {
6942
0
      (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6943
0
  (output_bfd, &outrel[0],
6944
0
   (sreloc->contents
6945
0
    + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6946
0
    }
6947
0
  else if (htab->root.target_os == is_vxworks)
6948
0
    {
6949
      /* VxWorks uses RELA rather than REL dynamic relocations.  */
6950
0
      outrel[0].r_addend = *addendp;
6951
0
      bfd_elf32_swap_reloca_out
6952
0
  (output_bfd, &outrel[0],
6953
0
   (sreloc->contents
6954
0
    + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6955
0
    }
6956
0
  else
6957
0
    bfd_elf32_swap_reloc_out
6958
0
      (output_bfd, &outrel[0],
6959
0
       (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6960
6961
  /* We've now added another relocation.  */
6962
0
  ++sreloc->reloc_count;
6963
6964
  /* Make sure the output section is writable.  The dynamic linker
6965
     will be writing to it.  */
6966
0
  elf_section_data (input_section->output_section)->this_hdr.sh_flags
6967
0
    |= SHF_WRITE;
6968
6969
  /* On IRIX5, make an entry of compact relocation info.  */
6970
0
  if (IRIX_COMPAT (output_bfd) == ict_irix5)
6971
0
    {
6972
0
      asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6973
0
      bfd_byte *cr;
6974
6975
0
      if (scpt)
6976
0
  {
6977
0
    Elf32_crinfo cptrel;
6978
6979
0
    mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6980
0
    cptrel.vaddr = (rel->r_offset
6981
0
        + input_section->output_section->vma
6982
0
        + input_section->output_offset);
6983
0
    if (r_type == R_MIPS_REL32)
6984
0
      mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6985
0
    else
6986
0
      mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6987
0
    mips_elf_set_cr_dist2to (cptrel, 0);
6988
0
    cptrel.konst = *addendp;
6989
6990
0
    cr = (scpt->contents
6991
0
    + sizeof (Elf32_External_compact_rel));
6992
0
    mips_elf_set_cr_relvaddr (cptrel, 0);
6993
0
    bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6994
0
             ((Elf32_External_crinfo *) cr
6995
0
              + scpt->reloc_count));
6996
0
    ++scpt->reloc_count;
6997
0
  }
6998
0
    }
6999
7000
  /* If we've written this relocation for a readonly section,
7001
     we need to set DF_TEXTREL again, so that we do not delete the
7002
     DT_TEXTREL tag.  */
7003
0
  if (MIPS_ELF_READONLY_SECTION (input_section))
7004
0
    info->flags |= DF_TEXTREL;
7005
7006
0
  return true;
7007
0
}
7008

7009
/* Return the MACH for a MIPS e_flags value.  */
7010
7011
unsigned long
7012
_bfd_elf_mips_mach (flagword flags)
7013
144k
{
7014
144k
  switch (flags & EF_MIPS_MACH)
7015
144k
    {
7016
6
    case EF_MIPS_MACH_3900:
7017
6
      return bfd_mach_mips3900;
7018
7019
3
    case EF_MIPS_MACH_4010:
7020
3
      return bfd_mach_mips4010;
7021
7022
303
    case EF_MIPS_MACH_ALLEGREX:
7023
303
      return bfd_mach_mips_allegrex;
7024
7025
3
    case EF_MIPS_MACH_4100:
7026
3
      return bfd_mach_mips4100;
7027
7028
4
    case EF_MIPS_MACH_4111:
7029
4
      return bfd_mach_mips4111;
7030
7031
3
    case EF_MIPS_MACH_4120:
7032
3
      return bfd_mach_mips4120;
7033
7034
0
    case EF_MIPS_MACH_4650:
7035
0
      return bfd_mach_mips4650;
7036
7037
0
    case EF_MIPS_MACH_5400:
7038
0
      return bfd_mach_mips5400;
7039
7040
7
    case EF_MIPS_MACH_5500:
7041
7
      return bfd_mach_mips5500;
7042
7043
773
    case EF_MIPS_MACH_5900:
7044
773
      return bfd_mach_mips5900;
7045
7046
2.23k
    case EF_MIPS_MACH_9000:
7047
2.23k
      return bfd_mach_mips9000;
7048
7049
7
    case EF_MIPS_MACH_SB1:
7050
7
      return bfd_mach_mips_sb1;
7051
7052
9
    case EF_MIPS_MACH_LS2E:
7053
9
      return bfd_mach_mips_loongson_2e;
7054
7055
833
    case EF_MIPS_MACH_LS2F:
7056
833
      return bfd_mach_mips_loongson_2f;
7057
7058
6
    case EF_MIPS_MACH_GS464:
7059
6
      return bfd_mach_mips_gs464;
7060
7061
3
    case EF_MIPS_MACH_GS464E:
7062
3
      return bfd_mach_mips_gs464e;
7063
7064
50
    case EF_MIPS_MACH_GS264E:
7065
50
      return bfd_mach_mips_gs264e;
7066
7067
692
    case EF_MIPS_MACH_OCTEON3:
7068
692
      return bfd_mach_mips_octeon3;
7069
7070
303
    case EF_MIPS_MACH_OCTEON2:
7071
303
      return bfd_mach_mips_octeon2;
7072
7073
937
    case EF_MIPS_MACH_OCTEON:
7074
937
      return bfd_mach_mips_octeon;
7075
7076
0
    case EF_MIPS_MACH_XLR:
7077
0
      return bfd_mach_mips_xlr;
7078
7079
6
    case EF_MIPS_MACH_IAMR2:
7080
6
      return bfd_mach_mips_interaptiv_mr2;
7081
7082
137k
    default:
7083
137k
      switch (flags & EF_MIPS_ARCH)
7084
137k
  {
7085
17.8k
  default:
7086
116k
  case EF_MIPS_ARCH_1:
7087
116k
    return bfd_mach_mips3000;
7088
7089
1.92k
  case EF_MIPS_ARCH_2:
7090
1.92k
    return bfd_mach_mips6000;
7091
7092
7.85k
  case EF_MIPS_ARCH_3:
7093
7.85k
    return bfd_mach_mips4000;
7094
7095
4.04k
  case EF_MIPS_ARCH_4:
7096
4.04k
    return bfd_mach_mips8000;
7097
7098
704
  case EF_MIPS_ARCH_5:
7099
704
    return bfd_mach_mips5;
7100
7101
31
  case EF_MIPS_ARCH_32:
7102
31
    return bfd_mach_mipsisa32;
7103
7104
1.05k
  case EF_MIPS_ARCH_64:
7105
1.05k
    return bfd_mach_mipsisa64;
7106
7107
1.36k
  case EF_MIPS_ARCH_32R2:
7108
1.36k
    return bfd_mach_mipsisa32r2;
7109
7110
1.87k
  case EF_MIPS_ARCH_64R2:
7111
1.87k
    return bfd_mach_mipsisa64r2;
7112
7113
1.30k
  case EF_MIPS_ARCH_32R6:
7114
1.30k
    return bfd_mach_mipsisa32r6;
7115
7116
715
  case EF_MIPS_ARCH_64R6:
7117
715
    return bfd_mach_mipsisa64r6;
7118
137k
  }
7119
144k
    }
7120
7121
0
  return 0;
7122
144k
}
7123
7124
/* Return printable name for ABI.  */
7125
7126
static inline char *
7127
elf_mips_abi_name (bfd *abfd)
7128
0
{
7129
0
  flagword flags;
7130
7131
0
  flags = elf_elfheader (abfd)->e_flags;
7132
0
  switch (flags & EF_MIPS_ABI)
7133
0
    {
7134
0
    case 0:
7135
0
      if (ABI_N32_P (abfd))
7136
0
  return "N32";
7137
0
      else if (ABI_64_P (abfd))
7138
0
  return "64";
7139
0
      else
7140
0
  return "none";
7141
0
    case EF_MIPS_ABI_O32:
7142
0
      return "O32";
7143
0
    case EF_MIPS_ABI_O64:
7144
0
      return "O64";
7145
0
    case EF_MIPS_ABI_EABI32:
7146
0
      return "EABI32";
7147
0
    case EF_MIPS_ABI_EABI64:
7148
0
      return "EABI64";
7149
0
    default:
7150
0
      return "unknown abi";
7151
0
    }
7152
0
}
7153

7154
/* MIPS ELF uses two common sections.  One is the usual one, and the
7155
   other is for small objects.  All the small objects are kept
7156
   together, and then referenced via the gp pointer, which yields
7157
   faster assembler code.  This is what we use for the small common
7158
   section.  This approach is copied from ecoff.c.  */
7159
static asection mips_elf_scom_section;
7160
static const asymbol mips_elf_scom_symbol =
7161
  GLOBAL_SYM_INIT (".scommon", &mips_elf_scom_section);
7162
static asection mips_elf_scom_section =
7163
  BFD_FAKE_SECTION (mips_elf_scom_section, &mips_elf_scom_symbol,
7164
        ".scommon", 0, SEC_IS_COMMON | SEC_SMALL_DATA);
7165
7166
/* MIPS ELF also uses an acommon section, which represents an
7167
   allocated common symbol which may be overridden by a
7168
   definition in a shared library.  */
7169
static asection mips_elf_acom_section;
7170
static const asymbol mips_elf_acom_symbol =
7171
  GLOBAL_SYM_INIT (".acommon", &mips_elf_acom_section);
7172
static asection mips_elf_acom_section =
7173
  BFD_FAKE_SECTION (mips_elf_acom_section, &mips_elf_acom_symbol,
7174
        ".acommon", 0, SEC_ALLOC);
7175
7176
/* This is used for both the 32-bit and the 64-bit ABI.  */
7177
7178
void
7179
_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
7180
19.6k
{
7181
19.6k
  elf_symbol_type *elfsym;
7182
7183
  /* Handle the special MIPS section numbers that a symbol may use.  */
7184
19.6k
  elfsym = (elf_symbol_type *) asym;
7185
19.6k
  switch (elfsym->internal_elf_sym.st_shndx)
7186
19.6k
    {
7187
4
    case SHN_MIPS_ACOMMON:
7188
      /* This section is used in a dynamically linked executable file.
7189
   It is an allocated common section.  The dynamic linker can
7190
   either resolve these symbols to something in a shared
7191
   library, or it can just leave them here.  For our purposes,
7192
   we can consider these symbols to be in a new section.  */
7193
4
      asym->section = &mips_elf_acom_section;
7194
4
      break;
7195
7196
10
    case SHN_COMMON:
7197
      /* Common symbols less than the GP size are automatically
7198
   treated as SHN_MIPS_SCOMMON symbols, with some exceptions.  */
7199
10
      if (asym->value > elf_gp_size (abfd)
7200
10
    || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
7201
10
    || IRIX_COMPAT (abfd) == ict_irix6
7202
10
    || strcmp (asym->name, "__gnu_lto_slim") == 0)
7203
10
  break;
7204
      /* Fall through.  */
7205
0
    case SHN_MIPS_SCOMMON:
7206
0
      asym->section = &mips_elf_scom_section;
7207
0
      asym->value = elfsym->internal_elf_sym.st_size;
7208
0
      break;
7209
7210
2
    case SHN_MIPS_SUNDEFINED:
7211
2
      asym->section = bfd_und_section_ptr;
7212
2
      break;
7213
7214
1
    case SHN_MIPS_TEXT:
7215
1
      {
7216
1
  asection *section = bfd_get_section_by_name (abfd, ".text");
7217
7218
1
  if (section != NULL)
7219
0
    {
7220
0
      asym->section = section;
7221
      /* MIPS_TEXT is a bit special, the address is not an offset
7222
         to the base of the .text section.  So subtract the section
7223
         base address to make it an offset.  */
7224
0
      asym->value -= section->vma;
7225
0
    }
7226
1
      }
7227
1
      break;
7228
7229
3
    case SHN_MIPS_DATA:
7230
3
      {
7231
3
  asection *section = bfd_get_section_by_name (abfd, ".data");
7232
7233
3
  if (section != NULL)
7234
0
    {
7235
0
      asym->section = section;
7236
      /* MIPS_DATA is a bit special, the address is not an offset
7237
         to the base of the .data section.  So subtract the section
7238
         base address to make it an offset.  */
7239
0
      asym->value -= section->vma;
7240
0
    }
7241
3
      }
7242
3
      break;
7243
19.6k
    }
7244
7245
  /* If this is an odd-valued function symbol, assume it's a MIPS16
7246
     or microMIPS one.  */
7247
19.6k
  if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
7248
19.6k
      && (asym->value & 1) != 0)
7249
7
    {
7250
7
      asym->value--;
7251
7
      if (MICROMIPS_P (abfd))
7252
1
  elfsym->internal_elf_sym.st_other
7253
1
    = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
7254
6
      else
7255
6
  elfsym->internal_elf_sym.st_other
7256
6
    = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
7257
7
    }
7258
19.6k
}
7259

7260
/* Implement elf_backend_eh_frame_address_size.  This differs from
7261
   the default in the way it handles EABI64.
7262
7263
   EABI64 was originally specified as an LP64 ABI, and that is what
7264
   -mabi=eabi normally gives on a 64-bit target.  However, gcc has
7265
   historically accepted the combination of -mabi=eabi and -mlong32,
7266
   and this ILP32 variation has become semi-official over time.
7267
   Both forms use elf32 and have pointer-sized FDE addresses.
7268
7269
   If an EABI object was generated by GCC 4.0 or above, it will have
7270
   an empty .gcc_compiled_longXX section, where XX is the size of longs
7271
   in bits.  Unfortunately, ILP32 objects generated by earlier compilers
7272
   have no special marking to distinguish them from LP64 objects.
7273
7274
   We don't want users of the official LP64 ABI to be punished for the
7275
   existence of the ILP32 variant, but at the same time, we don't want
7276
   to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
7277
   We therefore take the following approach:
7278
7279
      - If ABFD contains a .gcc_compiled_longXX section, use it to
7280
  determine the pointer size.
7281
7282
      - Otherwise check the type of the first relocation.  Assume that
7283
  the LP64 ABI is being used if the relocation is of type R_MIPS_64.
7284
7285
      - Otherwise punt.
7286
7287
   The second check is enough to detect LP64 objects generated by pre-4.0
7288
   compilers because, in the kind of output generated by those compilers,
7289
   the first relocation will be associated with either a CIE personality
7290
   routine or an FDE start address.  Furthermore, the compilers never
7291
   used a special (non-pointer) encoding for this ABI.
7292
7293
   Checking the relocation type should also be safe because there is no
7294
   reason to use R_MIPS_64 in an ILP32 object.  Pre-4.0 compilers never
7295
   did so.  */
7296
7297
unsigned int
7298
_bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
7299
0
{
7300
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
7301
0
    return 8;
7302
0
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64)
7303
0
    {
7304
0
      bool long32_p, long64_p;
7305
7306
0
      long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
7307
0
      long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
7308
0
      if (long32_p && long64_p)
7309
0
  return 0;
7310
0
      if (long32_p)
7311
0
  return 4;
7312
0
      if (long64_p)
7313
0
  return 8;
7314
7315
0
      if (sec->reloc_count > 0)
7316
0
  {
7317
    /* Load the relocations for this section.  */
7318
0
    Elf_Internal_Rela *internal_relocs =
7319
0
      _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, true);
7320
0
    if (internal_relocs == NULL)
7321
0
      return 0;
7322
7323
0
    unsigned int size = 0;
7324
0
    if (ELF32_R_TYPE (internal_relocs[0].r_info) == R_MIPS_64)
7325
0
      size = 8;
7326
7327
0
    if (elf_section_data (sec)->relocs != internal_relocs)
7328
0
      free (internal_relocs);
7329
7330
0
    return size;
7331
0
  }
7332
7333
0
      return 0;
7334
0
    }
7335
0
  return 4;
7336
0
}
7337

7338
/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
7339
   relocations against two unnamed section symbols to resolve to the
7340
   same address.  For example, if we have code like:
7341
7342
  lw  $4,%got_disp(.data)($gp)
7343
  lw  $25,%got_disp(.text)($gp)
7344
  jalr  $25
7345
7346
   then the linker will resolve both relocations to .data and the program
7347
   will jump there rather than to .text.
7348
7349
   We can work around this problem by giving names to local section symbols.
7350
   This is also what the MIPSpro tools do.  */
7351
7352
bool
7353
_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
7354
0
{
7355
0
  return elf_elfheader (abfd)->e_type == ET_REL && SGI_COMPAT (abfd);
7356
0
}
7357

7358
/* Work over a section just before writing it out.  This routine is
7359
   used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
7360
   sections that need the SHF_MIPS_GPREL flag by name; there has to be
7361
   a better way.  */
7362
7363
bool
7364
_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
7365
1
{
7366
1
  if (hdr->sh_type == SHT_MIPS_REGINFO
7367
1
      && hdr->sh_size > 0)
7368
0
    {
7369
0
      bfd_byte buf[4];
7370
7371
0
      BFD_ASSERT (hdr->contents == NULL);
7372
7373
0
      if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
7374
0
  {
7375
0
    _bfd_error_handler
7376
0
      (_("%pB: incorrect `.reginfo' section size; "
7377
0
         "expected %" PRIu64 ", got %" PRIu64),
7378
0
       abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
7379
0
       (uint64_t) hdr->sh_size);
7380
0
    bfd_set_error (bfd_error_bad_value);
7381
0
    return false;
7382
0
  }
7383
7384
0
      if (bfd_seek (abfd,
7385
0
        hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
7386
0
        SEEK_SET) != 0)
7387
0
  return false;
7388
0
      H_PUT_32 (abfd, elf_gp (abfd), buf);
7389
0
      if (bfd_write (buf, 4, abfd) != 4)
7390
0
  return false;
7391
0
    }
7392
7393
1
  if (hdr->sh_type == SHT_MIPS_OPTIONS
7394
1
      && hdr->bfd_section != NULL
7395
1
      && mips_elf_section_data (hdr->bfd_section) != NULL
7396
1
      && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
7397
0
    {
7398
0
      bfd_byte *contents, *l, *lend;
7399
7400
      /* We stored the section contents in the tdata field in the
7401
   set_section_contents routine.  We save the section contents
7402
   so that we don't have to read them again.
7403
   At this point we know that elf_gp is set, so we can look
7404
   through the section contents to see if there is an
7405
   ODK_REGINFO structure.  */
7406
7407
0
      contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
7408
0
      l = contents;
7409
0
      lend = contents + hdr->sh_size;
7410
0
      while (l + sizeof (Elf_External_Options) <= lend)
7411
0
  {
7412
0
    Elf_Internal_Options intopt;
7413
7414
0
    bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7415
0
          &intopt);
7416
0
    if (intopt.size < sizeof (Elf_External_Options))
7417
0
      {
7418
0
        _bfd_error_handler
7419
    /* xgettext:c-format */
7420
0
    (_("%pB: warning: bad `%s' option size %u smaller than"
7421
0
       " its header"),
7422
0
    abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7423
0
        break;
7424
0
      }
7425
0
    if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7426
0
      {
7427
0
        bfd_byte buf[8];
7428
7429
0
        if (bfd_seek (abfd,
7430
0
          (hdr->sh_offset
7431
0
           + (l - contents)
7432
0
           + sizeof (Elf_External_Options)
7433
0
           + (sizeof (Elf64_External_RegInfo) - 8)),
7434
0
           SEEK_SET) != 0)
7435
0
    return false;
7436
0
        H_PUT_64 (abfd, elf_gp (abfd), buf);
7437
0
        if (bfd_write (buf, 8, abfd) != 8)
7438
0
    return false;
7439
0
      }
7440
0
    else if (intopt.kind == ODK_REGINFO)
7441
0
      {
7442
0
        bfd_byte buf[4];
7443
7444
0
        if (bfd_seek (abfd,
7445
0
          (hdr->sh_offset
7446
0
           + (l - contents)
7447
0
           + sizeof (Elf_External_Options)
7448
0
           + (sizeof (Elf32_External_RegInfo) - 4)),
7449
0
          SEEK_SET) != 0)
7450
0
    return false;
7451
0
        H_PUT_32 (abfd, elf_gp (abfd), buf);
7452
0
        if (bfd_write (buf, 4, abfd) != 4)
7453
0
    return false;
7454
0
      }
7455
0
    l += intopt.size;
7456
0
  }
7457
0
    }
7458
7459
1
  if (hdr->bfd_section != NULL)
7460
0
    {
7461
0
      const char *name = bfd_section_name (hdr->bfd_section);
7462
7463
      /* .sbss is not handled specially here because the GNU/Linux
7464
   prelinker can convert .sbss from NOBITS to PROGBITS and
7465
   changing it back to NOBITS breaks the binary.  The entry in
7466
   _bfd_mips_elf_special_sections will ensure the correct flags
7467
   are set on .sbss if BFD creates it without reading it from an
7468
   input file, and without special handling here the flags set
7469
   on it in an input file will be followed.  */
7470
0
      if (strcmp (name, ".sdata") == 0
7471
0
    || strcmp (name, ".lit8") == 0
7472
0
    || strcmp (name, ".lit4") == 0)
7473
0
  hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7474
0
      else if (strcmp (name, ".srdata") == 0)
7475
0
  hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
7476
0
      else if (strcmp (name, ".compact_rel") == 0)
7477
0
  hdr->sh_flags = 0;
7478
0
      else if (strcmp (name, ".rtproc") == 0)
7479
0
  {
7480
0
    if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
7481
0
      {
7482
0
        unsigned int adjust;
7483
7484
0
        adjust = hdr->sh_size % hdr->sh_addralign;
7485
0
        if (adjust != 0)
7486
0
    hdr->sh_size += hdr->sh_addralign - adjust;
7487
0
      }
7488
0
  }
7489
0
    }
7490
7491
1
  return true;
7492
1
}
7493
7494
/* Handle a MIPS specific section when reading an object file.  This
7495
   is called when elfcode.h finds a section with an unknown type.
7496
   This routine supports both the 32-bit and 64-bit ELF ABI.  */
7497
7498
bool
7499
_bfd_mips_elf_section_from_shdr (bfd *abfd,
7500
         Elf_Internal_Shdr *hdr,
7501
         const char *name,
7502
         int shindex)
7503
451k
{
7504
451k
  flagword flags = 0;
7505
7506
  /* There ought to be a place to keep ELF backend specific flags, but
7507
     at the moment there isn't one.  We just keep track of the
7508
     sections by their name, instead.  Fortunately, the ABI gives
7509
     suggested names for all the MIPS specific sections, so we will
7510
     probably get away with this.  */
7511
451k
  switch (hdr->sh_type)
7512
451k
    {
7513
3.59k
    case SHT_MIPS_LIBLIST:
7514
3.59k
      if (strcmp (name, ".liblist") != 0)
7515
3.59k
  return false;
7516
0
      break;
7517
482
    case SHT_MIPS_MSYM:
7518
482
      if (strcmp (name, ".msym") != 0)
7519
482
  return false;
7520
0
      break;
7521
474
    case SHT_MIPS_CONFLICT:
7522
474
      if (strcmp (name, ".conflict") != 0)
7523
474
  return false;
7524
0
      break;
7525
466
    case SHT_MIPS_GPTAB:
7526
466
      if (! startswith (name, ".gptab."))
7527
466
  return false;
7528
0
      break;
7529
477
    case SHT_MIPS_UCODE:
7530
477
      if (strcmp (name, ".ucode") != 0)
7531
477
  return false;
7532
0
      break;
7533
477
    case SHT_MIPS_DEBUG:
7534
477
      if (strcmp (name, ".mdebug") != 0)
7535
472
  return false;
7536
5
      flags = SEC_DEBUGGING;
7537
5
      break;
7538
71
    case SHT_MIPS_REGINFO:
7539
71
      if (strcmp (name, ".reginfo") != 0
7540
71
    || hdr->sh_size != sizeof (Elf32_External_RegInfo))
7541
5
  return false;
7542
66
      flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7543
66
      break;
7544
734
    case SHT_MIPS_IFACE:
7545
734
      if (strcmp (name, ".MIPS.interfaces") != 0)
7546
734
  return false;
7547
0
      break;
7548
1.67k
    case SHT_MIPS_CONTENT:
7549
1.67k
      if (! startswith (name, ".MIPS.content"))
7550
971
  return false;
7551
701
      break;
7552
9.84k
    case SHT_MIPS_OPTIONS:
7553
9.84k
      if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7554
4.65k
  return false;
7555
5.19k
      break;
7556
5.19k
    case SHT_MIPS_ABIFLAGS:
7557
482
      if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
7558
482
  return false;
7559
0
      flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7560
0
      break;
7561
5.00k
    case SHT_MIPS_DWARF:
7562
5.00k
      if (! startswith (name, ".debug_")
7563
5.00k
         && ! startswith (name, ".gnu.debuglto_.debug_")
7564
5.00k
         && ! startswith (name, ".zdebug_")
7565
5.00k
         && ! startswith (name, ".gnu.debuglto_.zdebug_"))
7566
2.20k
  return false;
7567
2.80k
      break;
7568
2.80k
    case SHT_MIPS_SYMBOL_LIB:
7569
470
      if (strcmp (name, ".MIPS.symlib") != 0)
7570
470
  return false;
7571
0
      break;
7572
1.38k
    case SHT_MIPS_EVENTS:
7573
1.38k
      if (! startswith (name, ".MIPS.events")
7574
1.38k
    && ! startswith (name, ".MIPS.post_rel"))
7575
1.07k
  return false;
7576
308
      break;
7577
466
    case SHT_MIPS_XHASH:
7578
466
      if (strcmp (name, ".MIPS.xhash") != 0)
7579
466
  return false;
7580
425k
    default:
7581
425k
      break;
7582
451k
    }
7583
7584
434k
  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
7585
22
    return false;
7586
7587
434k
  if (hdr->sh_flags & SHF_MIPS_GPREL)
7588
67.4k
    flags |= SEC_SMALL_DATA;
7589
7590
434k
  if (flags)
7591
67.5k
    {
7592
67.5k
      if (!bfd_set_section_flags (hdr->bfd_section,
7593
67.5k
          (bfd_section_flags (hdr->bfd_section)
7594
67.5k
           | flags)))
7595
0
  return false;
7596
67.5k
    }
7597
7598
434k
  if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7599
0
    {
7600
0
      Elf_External_ABIFlags_v0 ext;
7601
7602
0
      if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7603
0
              &ext, 0, sizeof ext))
7604
0
  return false;
7605
0
      bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7606
0
          &mips_elf_tdata (abfd)->abiflags);
7607
0
      if (mips_elf_tdata (abfd)->abiflags.version != 0)
7608
0
  return false;
7609
0
      mips_elf_tdata (abfd)->abiflags_valid = true;
7610
0
    }
7611
7612
  /* FIXME: We should record sh_info for a .gptab section.  */
7613
7614
  /* For a .reginfo section, set the gp value in the tdata information
7615
     from the contents of this section.  We need the gp value while
7616
     processing relocs, so we just get it now.  The .reginfo section
7617
     is not used in the 64-bit MIPS ELF ABI.  */
7618
434k
  if (hdr->sh_type == SHT_MIPS_REGINFO)
7619
66
    {
7620
66
      Elf32_External_RegInfo ext;
7621
66
      Elf32_RegInfo s;
7622
7623
66
      if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7624
66
              &ext, 0, sizeof ext))
7625
0
  return false;
7626
66
      bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7627
66
      elf_gp (abfd) = s.ri_gp_value;
7628
66
    }
7629
7630
  /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7631
     set the gp value based on what we find.  We may see both
7632
     SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7633
     they should agree.  */
7634
434k
  if (hdr->sh_type == SHT_MIPS_OPTIONS)
7635
5.19k
    {
7636
5.19k
      bfd_byte *contents, *l, *lend;
7637
7638
5.19k
      if (!bfd_malloc_and_get_section (abfd, hdr->bfd_section, &contents))
7639
678
  {
7640
678
    free (contents);
7641
678
    return false;
7642
678
  }
7643
4.51k
      l = contents;
7644
4.51k
      lend = contents + hdr->sh_size;
7645
12.8k
      while (l + sizeof (Elf_External_Options) <= lend)
7646
11.1k
  {
7647
11.1k
    Elf_Internal_Options intopt;
7648
7649
11.1k
    bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7650
11.1k
          &intopt);
7651
11.1k
    if (intopt.size < sizeof (Elf_External_Options))
7652
2.11k
      {
7653
2.81k
      bad_opt:
7654
2.81k
        _bfd_error_handler
7655
    /* xgettext:c-format */
7656
2.81k
    (_("%pB: warning: truncated `%s' option"),
7657
2.81k
     abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd));
7658
2.81k
        break;
7659
2.11k
      }
7660
9.07k
    if (intopt.kind == ODK_REGINFO)
7661
3.48k
      {
7662
3.48k
        if (ABI_64_P (abfd))
7663
3.48k
    {
7664
3.48k
      Elf64_Internal_RegInfo intreg;
7665
3.48k
      size_t needed = (sizeof (Elf_External_Options)
7666
3.48k
           + sizeof (Elf64_External_RegInfo));
7667
3.48k
      if (intopt.size < needed || (size_t) (lend - l) < needed)
7668
704
        goto bad_opt;
7669
2.77k
      bfd_mips_elf64_swap_reginfo_in
7670
2.77k
        (abfd,
7671
2.77k
         ((Elf64_External_RegInfo *)
7672
2.77k
          (l + sizeof (Elf_External_Options))),
7673
2.77k
         &intreg);
7674
2.77k
      elf_gp (abfd) = intreg.ri_gp_value;
7675
2.77k
    }
7676
0
        else
7677
0
    {
7678
0
      Elf32_RegInfo intreg;
7679
0
      size_t needed = (sizeof (Elf_External_Options)
7680
0
           + sizeof (Elf32_External_RegInfo));
7681
0
      if (intopt.size < needed || (size_t) (lend - l) < needed)
7682
0
        goto bad_opt;
7683
0
      bfd_mips_elf32_swap_reginfo_in
7684
0
        (abfd,
7685
0
         ((Elf32_External_RegInfo *)
7686
0
          (l + sizeof (Elf_External_Options))),
7687
0
         &intreg);
7688
0
      elf_gp (abfd) = intreg.ri_gp_value;
7689
0
    }
7690
3.48k
      }
7691
8.37k
    l += intopt.size;
7692
8.37k
  }
7693
4.51k
      free (contents);
7694
4.51k
    }
7695
7696
433k
  return true;
7697
434k
}
7698
7699
/* Set the correct type for a MIPS ELF section.  We do this by the
7700
   section name, which is a hack, but ought to work.  This routine is
7701
   used by both the 32-bit and the 64-bit ABI.  */
7702
7703
bool
7704
_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7705
0
{
7706
0
  const char *name = bfd_section_name (sec);
7707
7708
0
  if (strcmp (name, ".liblist") == 0)
7709
0
    {
7710
0
      hdr->sh_type = SHT_MIPS_LIBLIST;
7711
0
      hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7712
      /* The sh_link field is set in final_write_processing.  */
7713
0
    }
7714
0
  else if (strcmp (name, ".conflict") == 0)
7715
0
    hdr->sh_type = SHT_MIPS_CONFLICT;
7716
0
  else if (startswith (name, ".gptab."))
7717
0
    {
7718
0
      hdr->sh_type = SHT_MIPS_GPTAB;
7719
0
      hdr->sh_entsize = sizeof (Elf32_External_gptab);
7720
      /* The sh_info field is set in final_write_processing.  */
7721
0
    }
7722
0
  else if (strcmp (name, ".ucode") == 0)
7723
0
    hdr->sh_type = SHT_MIPS_UCODE;
7724
0
  else if (strcmp (name, ".mdebug") == 0)
7725
0
    {
7726
0
      hdr->sh_type = SHT_MIPS_DEBUG;
7727
      /* In a shared object on IRIX 5.3, the .mdebug section has an
7728
   entsize of 0.  FIXME: Does this matter?  */
7729
0
      if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7730
0
  hdr->sh_entsize = 0;
7731
0
      else
7732
0
  hdr->sh_entsize = 1;
7733
0
    }
7734
0
  else if (strcmp (name, ".reginfo") == 0)
7735
0
    {
7736
0
      hdr->sh_type = SHT_MIPS_REGINFO;
7737
      /* In a shared object on IRIX 5.3, the .reginfo section has an
7738
   entsize of 0x18.  FIXME: Does this matter?  */
7739
0
      if (SGI_COMPAT (abfd))
7740
0
  {
7741
0
    if ((abfd->flags & DYNAMIC) != 0)
7742
0
      hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7743
0
    else
7744
0
      hdr->sh_entsize = 1;
7745
0
  }
7746
0
      else
7747
0
  hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7748
0
    }
7749
0
  else if (SGI_COMPAT (abfd)
7750
0
     && (strcmp (name, ".hash") == 0
7751
0
         || strcmp (name, ".dynamic") == 0
7752
0
         || strcmp (name, ".dynstr") == 0))
7753
0
    {
7754
0
      if (SGI_COMPAT (abfd))
7755
0
  hdr->sh_entsize = 0;
7756
#if 0
7757
      /* This isn't how the IRIX6 linker behaves.  */
7758
      hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7759
#endif
7760
0
    }
7761
0
  else if (strcmp (name, ".got") == 0
7762
0
     || strcmp (name, ".srdata") == 0
7763
0
     || strcmp (name, ".sdata") == 0
7764
0
     || strcmp (name, ".sbss") == 0
7765
0
     || strcmp (name, ".lit4") == 0
7766
0
     || strcmp (name, ".lit8") == 0)
7767
0
    hdr->sh_flags |= SHF_MIPS_GPREL;
7768
0
  else if (strcmp (name, ".MIPS.interfaces") == 0)
7769
0
    {
7770
0
      hdr->sh_type = SHT_MIPS_IFACE;
7771
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7772
0
    }
7773
0
  else if (startswith (name, ".MIPS.content"))
7774
0
    {
7775
0
      hdr->sh_type = SHT_MIPS_CONTENT;
7776
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7777
      /* The sh_info field is set in final_write_processing.  */
7778
0
    }
7779
0
  else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7780
0
    {
7781
0
      hdr->sh_type = SHT_MIPS_OPTIONS;
7782
0
      hdr->sh_entsize = 1;
7783
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7784
0
    }
7785
0
  else if (startswith (name, ".MIPS.abiflags"))
7786
0
    {
7787
0
      hdr->sh_type = SHT_MIPS_ABIFLAGS;
7788
0
      hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7789
0
    }
7790
0
  else if (startswith (name, ".debug_")
7791
0
     || startswith (name, ".gnu.debuglto_.debug_")
7792
0
     || startswith (name, ".zdebug_")
7793
0
     || startswith (name, ".gnu.debuglto_.zdebug_"))
7794
0
    {
7795
0
      hdr->sh_type = SHT_MIPS_DWARF;
7796
7797
      /* Irix facilities such as libexc expect a single .debug_frame
7798
   per executable, the system ones have NOSTRIP set and the linker
7799
   doesn't merge sections with different flags so ...  */
7800
0
      if (SGI_COMPAT (abfd) && startswith (name, ".debug_frame"))
7801
0
  hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7802
0
    }
7803
0
  else if (strcmp (name, ".MIPS.symlib") == 0)
7804
0
    {
7805
0
      hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7806
      /* The sh_link and sh_info fields are set in
7807
   final_write_processing.  */
7808
0
    }
7809
0
  else if (startswith (name, ".MIPS.events")
7810
0
     || startswith (name, ".MIPS.post_rel"))
7811
0
    {
7812
0
      hdr->sh_type = SHT_MIPS_EVENTS;
7813
0
      hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7814
      /* The sh_link field is set in final_write_processing.  */
7815
0
    }
7816
0
  else if (strcmp (name, ".msym") == 0)
7817
0
    {
7818
0
      hdr->sh_type = SHT_MIPS_MSYM;
7819
0
      hdr->sh_flags |= SHF_ALLOC;
7820
0
      hdr->sh_entsize = 8;
7821
0
    }
7822
0
  else if (strcmp (name, ".MIPS.xhash") == 0)
7823
0
    {
7824
0
      hdr->sh_type = SHT_MIPS_XHASH;
7825
0
      hdr->sh_flags |= SHF_ALLOC;
7826
0
      hdr->sh_entsize = get_elf_backend_data(abfd)->s->arch_size == 64 ? 0 : 4;
7827
0
    }
7828
7829
  /* The generic elf_fake_sections will set up REL_HDR using the default
7830
   kind of relocations.  We used to set up a second header for the
7831
   non-default kind of relocations here, but only NewABI would use
7832
   these, and the IRIX ld doesn't like resulting empty RELA sections.
7833
   Thus we create those header only on demand now.  */
7834
7835
0
  return true;
7836
0
}
7837
7838
/* Given a BFD section, try to locate the corresponding ELF section
7839
   index.  This is used by both the 32-bit and the 64-bit ABI.
7840
   Actually, it's not clear to me that the 64-bit ABI supports these,
7841
   but for non-PIC objects we will certainly want support for at least
7842
   the .scommon section.  */
7843
7844
bool
7845
_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7846
          asection *sec, int *retval)
7847
0
{
7848
0
  if (strcmp (bfd_section_name (sec), ".scommon") == 0)
7849
0
    {
7850
0
      *retval = SHN_MIPS_SCOMMON;
7851
0
      return true;
7852
0
    }
7853
0
  if (strcmp (bfd_section_name (sec), ".acommon") == 0)
7854
0
    {
7855
0
      *retval = SHN_MIPS_ACOMMON;
7856
0
      return true;
7857
0
    }
7858
0
  return false;
7859
0
}
7860

7861
/* Hook called by the linker routine which adds symbols from an object
7862
   file.  We must handle the special MIPS section numbers here.  */
7863
7864
bool
7865
_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7866
             Elf_Internal_Sym *sym, const char **namep,
7867
             flagword *flagsp ATTRIBUTE_UNUSED,
7868
             asection **secp, bfd_vma *valp)
7869
0
{
7870
0
  if (SGI_COMPAT (abfd)
7871
0
      && (abfd->flags & DYNAMIC) != 0
7872
0
      && strcmp (*namep, "_rld_new_interface") == 0)
7873
0
    {
7874
      /* Skip IRIX5 rld entry name.  */
7875
0
      *namep = NULL;
7876
0
      return true;
7877
0
    }
7878
7879
  /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7880
     a SECTION *ABS*.  This causes ld to think it can resolve _gp_disp
7881
     by setting a DT_NEEDED for the shared object.  Since _gp_disp is
7882
     a magic symbol resolved by the linker, we ignore this bogus definition
7883
     of _gp_disp.  New ABI objects do not suffer from this problem so this
7884
     is not done for them. */
7885
0
  if (!NEWABI_P(abfd)
7886
0
      && (sym->st_shndx == SHN_ABS)
7887
0
      && (strcmp (*namep, "_gp_disp") == 0))
7888
0
    {
7889
0
      *namep = NULL;
7890
0
      return true;
7891
0
    }
7892
7893
0
  switch (sym->st_shndx)
7894
0
    {
7895
0
    case SHN_COMMON:
7896
      /* Common symbols less than the GP size are automatically
7897
   treated as SHN_MIPS_SCOMMON symbols, with some exceptions.  */
7898
0
      if (sym->st_size > elf_gp_size (abfd)
7899
0
    || ELF_ST_TYPE (sym->st_info) == STT_TLS
7900
0
    || IRIX_COMPAT (abfd) == ict_irix6
7901
0
    || strcmp (*namep, "__gnu_lto_slim") == 0)
7902
0
  break;
7903
      /* Fall through.  */
7904
0
    case SHN_MIPS_SCOMMON:
7905
0
      *secp = bfd_make_section_old_way (abfd, ".scommon");
7906
0
      (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
7907
0
      *valp = sym->st_size;
7908
0
      break;
7909
7910
0
    case SHN_MIPS_TEXT:
7911
      /* This section is used in a shared object.  */
7912
0
      if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7913
0
  {
7914
0
    asymbol *elf_text_symbol;
7915
0
    asection *elf_text_section;
7916
0
    size_t amt = sizeof (asection);
7917
7918
0
    elf_text_section = bfd_zalloc (abfd, amt);
7919
0
    if (elf_text_section == NULL)
7920
0
      return false;
7921
7922
0
    amt = sizeof (asymbol);
7923
0
    elf_text_symbol = bfd_zalloc (abfd, amt);
7924
0
    if (elf_text_symbol == NULL)
7925
0
      return false;
7926
7927
    /* Initialize the section.  */
7928
7929
0
    mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7930
7931
0
    elf_text_section->symbol = elf_text_symbol;
7932
0
    elf_text_section->name = ".text";
7933
0
    elf_text_section->flags = SEC_NO_FLAGS;
7934
0
    elf_text_section->output_section = NULL;
7935
0
    elf_text_section->owner = abfd;
7936
0
    elf_text_symbol->name = ".text";
7937
0
    elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7938
0
    elf_text_symbol->section = elf_text_section;
7939
0
  }
7940
      /* This code used to do *secp = bfd_und_section_ptr if
7941
   bfd_link_pic (info).  I don't know why, and that doesn't make sense,
7942
   so I took it out.  */
7943
0
      *secp = mips_elf_tdata (abfd)->elf_text_section;
7944
0
      break;
7945
7946
0
    case SHN_MIPS_ACOMMON:
7947
      /* Fall through. XXX Can we treat this as allocated data?  */
7948
0
    case SHN_MIPS_DATA:
7949
      /* This section is used in a shared object.  */
7950
0
      if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7951
0
  {
7952
0
    asymbol *elf_data_symbol;
7953
0
    asection *elf_data_section;
7954
0
    size_t amt = sizeof (asection);
7955
7956
0
    elf_data_section = bfd_zalloc (abfd, amt);
7957
0
    if (elf_data_section == NULL)
7958
0
      return false;
7959
7960
0
    amt = sizeof (asymbol);
7961
0
    elf_data_symbol = bfd_zalloc (abfd, amt);
7962
0
    if (elf_data_symbol == NULL)
7963
0
      return false;
7964
7965
    /* Initialize the section.  */
7966
7967
0
    mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7968
7969
0
    elf_data_section->symbol = elf_data_symbol;
7970
0
    elf_data_section->name = ".data";
7971
0
    elf_data_section->flags = SEC_NO_FLAGS;
7972
0
    elf_data_section->output_section = NULL;
7973
0
    elf_data_section->owner = abfd;
7974
0
    elf_data_symbol->name = ".data";
7975
0
    elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7976
0
    elf_data_symbol->section = elf_data_section;
7977
0
  }
7978
      /* This code used to do *secp = bfd_und_section_ptr if
7979
   bfd_link_pic (info).  I don't know why, and that doesn't make sense,
7980
   so I took it out.  */
7981
0
      *secp = mips_elf_tdata (abfd)->elf_data_section;
7982
0
      break;
7983
7984
0
    case SHN_MIPS_SUNDEFINED:
7985
0
      *secp = bfd_und_section_ptr;
7986
0
      break;
7987
0
    }
7988
7989
0
  if (SGI_COMPAT (abfd)
7990
0
      && ! bfd_link_pic (info)
7991
0
      && info->output_bfd->xvec == abfd->xvec
7992
0
      && strcmp (*namep, "__rld_obj_head") == 0)
7993
0
    {
7994
0
      struct elf_link_hash_entry *h;
7995
0
      struct bfd_link_hash_entry *bh;
7996
7997
      /* Mark __rld_obj_head as dynamic.  */
7998
0
      bh = NULL;
7999
0
      if (! (_bfd_generic_link_add_one_symbol
8000
0
       (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, false,
8001
0
        get_elf_backend_data (abfd)->collect, &bh)))
8002
0
  return false;
8003
8004
0
      h = (struct elf_link_hash_entry *) bh;
8005
0
      h->non_elf = 0;
8006
0
      h->def_regular = 1;
8007
0
      h->type = STT_OBJECT;
8008
8009
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
8010
0
  return false;
8011
8012
0
      mips_elf_hash_table (info)->use_rld_obj_head = true;
8013
0
      mips_elf_hash_table (info)->rld_symbol = h;
8014
0
    }
8015
8016
  /* If this is a mips16 text symbol, add 1 to the value to make it
8017
     odd.  This will cause something like .word SYM to come up with
8018
     the right value when it is loaded into the PC.  */
8019
0
  if (ELF_ST_IS_COMPRESSED (sym->st_other))
8020
0
    ++*valp;
8021
8022
0
  return true;
8023
0
}
8024
8025
/* This hook function is called before the linker writes out a global
8026
   symbol.  We mark symbols as small common if appropriate.  This is
8027
   also where we undo the increment of the value for a mips16 symbol.  */
8028
8029
int
8030
_bfd_mips_elf_link_output_symbol_hook
8031
  (struct bfd_link_info *info ATTRIBUTE_UNUSED,
8032
   const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
8033
   asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
8034
0
{
8035
  /* If we see a common symbol, which implies a relocatable link, then
8036
     if a symbol was small common in an input file, mark it as small
8037
     common in the output file.  */
8038
0
  if (sym->st_shndx == SHN_COMMON
8039
0
      && strcmp (input_sec->name, ".scommon") == 0)
8040
0
    sym->st_shndx = SHN_MIPS_SCOMMON;
8041
8042
0
  if (ELF_ST_IS_COMPRESSED (sym->st_other))
8043
0
    sym->st_value &= ~1;
8044
8045
0
  return 1;
8046
0
}
8047

8048
/* Functions for the dynamic linker.  */
8049
8050
/* Create dynamic sections when linking against a dynamic object.  */
8051
8052
bool
8053
_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
8054
0
{
8055
0
  struct elf_link_hash_entry *h;
8056
0
  struct bfd_link_hash_entry *bh;
8057
0
  flagword flags;
8058
0
  register asection *s;
8059
0
  const char * const *namep;
8060
0
  struct mips_elf_link_hash_table *htab;
8061
8062
0
  htab = mips_elf_hash_table (info);
8063
0
  BFD_ASSERT (htab != NULL);
8064
8065
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
8066
0
     | SEC_LINKER_CREATED | SEC_READONLY);
8067
8068
  /* The psABI requires a read-only .dynamic section, but the VxWorks
8069
     EABI doesn't.  */
8070
0
  if (htab->root.target_os != is_vxworks)
8071
0
    {
8072
0
      s = bfd_get_linker_section (abfd, ".dynamic");
8073
0
      if (s != NULL)
8074
0
  {
8075
0
    if (!bfd_set_section_flags (s, flags))
8076
0
      return false;
8077
0
  }
8078
0
    }
8079
8080
  /* We need to create .got section.  */
8081
0
  if (!mips_elf_create_got_section (abfd, info))
8082
0
    return false;
8083
8084
0
  if (! mips_elf_rel_dyn_section (info, true))
8085
0
    return false;
8086
8087
  /* Create .stub section.  */
8088
0
  s = bfd_make_section_anyway_with_flags (abfd,
8089
0
            MIPS_ELF_STUB_SECTION_NAME (abfd),
8090
0
            flags | SEC_CODE);
8091
0
  if (s == NULL
8092
0
      || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8093
0
    return false;
8094
0
  htab->sstubs = s;
8095
8096
0
  if (!mips_elf_hash_table (info)->use_rld_obj_head
8097
0
      && bfd_link_executable (info)
8098
0
      && bfd_get_linker_section (abfd, ".rld_map") == NULL)
8099
0
    {
8100
0
      s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
8101
0
                flags &~ (flagword) SEC_READONLY);
8102
0
      if (s == NULL
8103
0
    || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8104
0
  return false;
8105
0
    }
8106
8107
  /* Create .MIPS.xhash section.  */
8108
0
  if (info->emit_gnu_hash)
8109
0
    s = bfd_make_section_anyway_with_flags (abfd, ".MIPS.xhash",
8110
0
              flags | SEC_READONLY);
8111
8112
  /* On IRIX5, we adjust add some additional symbols and change the
8113
     alignments of several sections.  There is no ABI documentation
8114
     indicating that this is necessary on IRIX6, nor any evidence that
8115
     the linker takes such action.  */
8116
0
  if (IRIX_COMPAT (abfd) == ict_irix5)
8117
0
    {
8118
0
      for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
8119
0
  {
8120
0
    bh = NULL;
8121
0
    if (! (_bfd_generic_link_add_one_symbol
8122
0
     (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
8123
0
      NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8124
0
      return false;
8125
8126
0
    h = (struct elf_link_hash_entry *) bh;
8127
0
    h->mark = 1;
8128
0
    h->non_elf = 0;
8129
0
    h->def_regular = 1;
8130
0
    h->type = STT_SECTION;
8131
8132
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
8133
0
      return false;
8134
0
  }
8135
8136
      /* We need to create a .compact_rel section.  */
8137
0
      if (SGI_COMPAT (abfd))
8138
0
  {
8139
0
    if (!mips_elf_create_compact_rel_section (abfd, info))
8140
0
      return false;
8141
0
  }
8142
8143
      /* Change alignments of some sections.  */
8144
0
      s = bfd_get_linker_section (abfd, ".hash");
8145
0
      if (s != NULL)
8146
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8147
8148
0
      s = bfd_get_linker_section (abfd, ".dynsym");
8149
0
      if (s != NULL)
8150
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8151
8152
0
      s = bfd_get_linker_section (abfd, ".dynstr");
8153
0
      if (s != NULL)
8154
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8155
8156
      /* ??? */
8157
0
      s = bfd_get_section_by_name (abfd, ".reginfo");
8158
0
      if (s != NULL)
8159
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8160
8161
0
      s = bfd_get_linker_section (abfd, ".dynamic");
8162
0
      if (s != NULL)
8163
0
  bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8164
0
    }
8165
8166
0
  if (bfd_link_executable (info))
8167
0
    {
8168
0
      const char *name;
8169
8170
0
      name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
8171
0
      bh = NULL;
8172
0
      if (!(_bfd_generic_link_add_one_symbol
8173
0
      (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
8174
0
       NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8175
0
  return false;
8176
8177
0
      h = (struct elf_link_hash_entry *) bh;
8178
0
      h->non_elf = 0;
8179
0
      h->def_regular = 1;
8180
0
      h->type = STT_SECTION;
8181
8182
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
8183
0
  return false;
8184
8185
0
      if (! mips_elf_hash_table (info)->use_rld_obj_head)
8186
0
  {
8187
    /* __rld_map is a four byte word located in the .data section
8188
       and is filled in by the rtld to contain a pointer to
8189
       the _r_debug structure. Its symbol value will be set in
8190
       _bfd_mips_elf_finish_dynamic_symbol.  */
8191
0
    s = bfd_get_linker_section (abfd, ".rld_map");
8192
0
    BFD_ASSERT (s != NULL);
8193
8194
0
    name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
8195
0
    bh = NULL;
8196
0
    if (!(_bfd_generic_link_add_one_symbol
8197
0
    (info, abfd, name, BSF_GLOBAL, s, 0, NULL, false,
8198
0
     get_elf_backend_data (abfd)->collect, &bh)))
8199
0
      return false;
8200
8201
0
    h = (struct elf_link_hash_entry *) bh;
8202
0
    h->non_elf = 0;
8203
0
    h->def_regular = 1;
8204
0
    h->type = STT_OBJECT;
8205
8206
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
8207
0
      return false;
8208
0
    mips_elf_hash_table (info)->rld_symbol = h;
8209
0
  }
8210
0
    }
8211
8212
  /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
8213
     Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol.  */
8214
0
  if (!_bfd_elf_create_dynamic_sections (abfd, info))
8215
0
    return false;
8216
8217
  /* Do the usual VxWorks handling.  */
8218
0
  if (htab->root.target_os == is_vxworks
8219
0
      && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
8220
0
    return false;
8221
8222
0
  return true;
8223
0
}
8224

8225
/* Return true if relocation REL against section SEC is a REL rather than
8226
   RELA relocation.  RELOCS is the first relocation in the section and
8227
   ABFD is the bfd that contains SEC.  */
8228
8229
static bool
8230
mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
8231
         const Elf_Internal_Rela *relocs,
8232
         const Elf_Internal_Rela *rel)
8233
0
{
8234
0
  Elf_Internal_Shdr *rel_hdr;
8235
0
  const struct elf_backend_data *bed;
8236
8237
  /* To determine which flavor of relocation this is, we depend on the
8238
     fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR.  */
8239
0
  rel_hdr = elf_section_data (sec)->rel.hdr;
8240
0
  if (rel_hdr == NULL)
8241
0
    return false;
8242
0
  bed = get_elf_backend_data (abfd);
8243
0
  return ((size_t) (rel - relocs)
8244
0
    < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
8245
0
}
8246
8247
/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
8248
   HOWTO is the relocation's howto and CONTENTS points to the contents
8249
   of the section that REL is against.  */
8250
8251
static bfd_vma
8252
mips_elf_read_rel_addend (bfd *abfd, asection *sec,
8253
        const Elf_Internal_Rela *rel,
8254
        reloc_howto_type *howto, bfd_byte *contents)
8255
0
{
8256
0
  bfd_byte *location;
8257
0
  unsigned int r_type;
8258
0
  bfd_vma addend;
8259
0
  bfd_vma bytes;
8260
8261
0
  if (!bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8262
0
    return 0;
8263
8264
0
  r_type = ELF_R_TYPE (abfd, rel->r_info);
8265
0
  location = contents + rel->r_offset;
8266
8267
  /* Get the addend, which is stored in the input file.  */
8268
0
  _bfd_mips_elf_reloc_unshuffle (abfd, r_type, false, location);
8269
0
  bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
8270
0
  _bfd_mips_elf_reloc_shuffle (abfd, r_type, false, location);
8271
8272
0
  addend = bytes & howto->src_mask;
8273
8274
  /* Shift is 2, unusually, for microMIPS JALX.  Adjust the addend
8275
     accordingly.  */
8276
0
  if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
8277
0
    addend <<= 1;
8278
8279
0
  return addend;
8280
0
}
8281
8282
/* REL is a relocation in ABFD that needs a partnering LO16 relocation
8283
   and *ADDEND is the addend for REL itself.  Look for the LO16 relocation
8284
   and update *ADDEND with the final addend.  Return true on success
8285
   or false if the LO16 could not be found.  RELEND is the exclusive
8286
   upper bound on the relocations for REL's section.  */
8287
8288
static bool
8289
mips_elf_add_lo16_rel_addend (bfd *abfd,
8290
            asection *sec,
8291
            const Elf_Internal_Rela *rel,
8292
            const Elf_Internal_Rela *relend,
8293
            bfd_byte *contents, bfd_vma *addend)
8294
0
{
8295
0
  unsigned int r_type, lo16_type;
8296
0
  const Elf_Internal_Rela *lo16_relocation;
8297
0
  reloc_howto_type *lo16_howto;
8298
0
  bfd_vma l;
8299
8300
0
  r_type = ELF_R_TYPE (abfd, rel->r_info);
8301
0
  if (mips16_reloc_p (r_type))
8302
0
    lo16_type = R_MIPS16_LO16;
8303
0
  else if (micromips_reloc_p (r_type))
8304
0
    lo16_type = R_MICROMIPS_LO16;
8305
0
  else if (r_type == R_MIPS_PCHI16)
8306
0
    lo16_type = R_MIPS_PCLO16;
8307
0
  else
8308
0
    lo16_type = R_MIPS_LO16;
8309
8310
  /* The combined value is the sum of the HI16 addend, left-shifted by
8311
     sixteen bits, and the LO16 addend, sign extended.  (Usually, the
8312
     code does a `lui' of the HI16 value, and then an `addiu' of the
8313
     LO16 value.)
8314
8315
     Scan ahead to find a matching LO16 relocation.
8316
8317
     According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
8318
     be immediately following.  However, for the IRIX6 ABI, the next
8319
     relocation may be a composed relocation consisting of several
8320
     relocations for the same address.  In that case, the R_MIPS_LO16
8321
     relocation may occur as one of these.  We permit a similar
8322
     extension in general, as that is useful for GCC.
8323
8324
     In some cases GCC dead code elimination removes the LO16 but keeps
8325
     the corresponding HI16.  This is strictly speaking a violation of
8326
     the ABI but not immediately harmful.  */
8327
0
  lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
8328
0
  if (lo16_relocation == NULL)
8329
0
    return false;
8330
8331
  /* Obtain the addend kept there.  */
8332
0
  lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, false);
8333
0
  l = mips_elf_read_rel_addend (abfd, sec, lo16_relocation, lo16_howto,
8334
0
        contents);
8335
8336
0
  l <<= lo16_howto->rightshift;
8337
0
  l = _bfd_mips_elf_sign_extend (l, 16);
8338
8339
0
  *addend <<= 16;
8340
0
  *addend += l;
8341
0
  return true;
8342
0
}
8343
8344
/* Try to read the contents of section SEC in bfd ABFD.  Return true and
8345
   store the contents in *CONTENTS on success.  Assume that *CONTENTS
8346
   already holds the contents if it is nonull on entry.  */
8347
8348
static bool
8349
mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
8350
0
{
8351
0
  if (*contents)
8352
0
    return true;
8353
8354
  /* Get cached copy if it exists.  */
8355
0
  if (elf_section_data (sec)->this_hdr.contents != NULL)
8356
0
    {
8357
0
      *contents = elf_section_data (sec)->this_hdr.contents;
8358
0
      return true;
8359
0
    }
8360
8361
0
  return bfd_malloc_and_get_section (abfd, sec, contents);
8362
0
}
8363
8364
/* Make a new PLT record to keep internal data.  */
8365
8366
static struct plt_entry *
8367
mips_elf_make_plt_record (bfd *abfd)
8368
0
{
8369
0
  struct plt_entry *entry;
8370
8371
0
  entry = bfd_zalloc (abfd, sizeof (*entry));
8372
0
  if (entry == NULL)
8373
0
    return NULL;
8374
8375
0
  entry->stub_offset = MINUS_ONE;
8376
0
  entry->mips_offset = MINUS_ONE;
8377
0
  entry->comp_offset = MINUS_ONE;
8378
0
  entry->gotplt_index = MINUS_ONE;
8379
0
  return entry;
8380
0
}
8381
8382
/* Define the special `__gnu_absolute_zero' symbol.  We only need this
8383
   for PIC code, as otherwise there is no load-time relocation involved
8384
   and local GOT entries whose value is zero at static link time will
8385
   retain their value at load time.  */
8386
8387
static bool
8388
mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
8389
             struct mips_elf_link_hash_table *htab,
8390
             unsigned int r_type)
8391
0
{
8392
0
  union
8393
0
    {
8394
0
      struct elf_link_hash_entry *eh;
8395
0
      struct bfd_link_hash_entry *bh;
8396
0
    }
8397
0
  hzero;
8398
8399
0
  BFD_ASSERT (!htab->use_absolute_zero);
8400
0
  BFD_ASSERT (bfd_link_pic (info));
8401
8402
0
  hzero.bh = NULL;
8403
0
  if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
8404
0
           BSF_GLOBAL, bfd_abs_section_ptr, 0,
8405
0
           NULL, false, false, &hzero.bh))
8406
0
    return false;
8407
8408
0
  BFD_ASSERT (hzero.bh != NULL);
8409
0
  hzero.eh->size = 0;
8410
0
  hzero.eh->type = STT_NOTYPE;
8411
0
  hzero.eh->other = STV_PROTECTED;
8412
0
  hzero.eh->def_regular = 1;
8413
0
  hzero.eh->non_elf = 0;
8414
8415
0
  if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, true, r_type))
8416
0
    return false;
8417
8418
0
  htab->use_absolute_zero = true;
8419
8420
0
  return true;
8421
0
}
8422
8423
/* Look through the relocs for a section during the first phase, and
8424
   allocate space in the global offset table and record the need for
8425
   standard MIPS and compressed procedure linkage table entries.  */
8426
8427
bool
8428
_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
8429
          asection *sec, const Elf_Internal_Rela *relocs)
8430
0
{
8431
0
  const char *name;
8432
0
  bfd *dynobj;
8433
0
  Elf_Internal_Shdr *symtab_hdr;
8434
0
  struct elf_link_hash_entry **sym_hashes;
8435
0
  size_t extsymoff;
8436
0
  const Elf_Internal_Rela *rel;
8437
0
  const Elf_Internal_Rela *rel_end;
8438
0
  asection *sreloc;
8439
0
  const struct elf_backend_data *bed;
8440
0
  struct mips_elf_link_hash_table *htab;
8441
0
  bfd_byte *contents;
8442
0
  bfd_vma addend;
8443
0
  reloc_howto_type *howto;
8444
8445
0
  if (bfd_link_relocatable (info))
8446
0
    return true;
8447
8448
0
  htab = mips_elf_hash_table (info);
8449
0
  BFD_ASSERT (htab != NULL);
8450
8451
0
  dynobj = elf_hash_table (info)->dynobj;
8452
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8453
0
  sym_hashes = elf_sym_hashes (abfd);
8454
0
  extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8455
8456
0
  bed = get_elf_backend_data (abfd);
8457
0
  rel_end = relocs + sec->reloc_count;
8458
8459
  /* Check for the mips16 stub sections.  */
8460
8461
0
  name = bfd_section_name (sec);
8462
0
  if (FN_STUB_P (name))
8463
0
    {
8464
0
      unsigned long r_symndx;
8465
8466
      /* Look at the relocation information to figure out which symbol
8467
   this is for.  */
8468
8469
0
      r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8470
0
      if (r_symndx == 0)
8471
0
  {
8472
0
    _bfd_error_handler
8473
      /* xgettext:c-format */
8474
0
      (_("%pB: warning: cannot determine the target function for"
8475
0
         " stub section `%s'"),
8476
0
       abfd, name);
8477
0
    bfd_set_error (bfd_error_bad_value);
8478
0
    return false;
8479
0
  }
8480
8481
0
      if (r_symndx < extsymoff
8482
0
    || sym_hashes[r_symndx - extsymoff] == NULL)
8483
0
  {
8484
0
    asection *o;
8485
8486
    /* This stub is for a local symbol.  This stub will only be
8487
       needed if there is some relocation in this BFD, other
8488
       than a 16 bit function call, which refers to this symbol.  */
8489
0
    for (o = abfd->sections; o != NULL; o = o->next)
8490
0
      {
8491
0
        Elf_Internal_Rela *sec_relocs;
8492
0
        const Elf_Internal_Rela *r, *rend;
8493
8494
        /* We can ignore stub sections when looking for relocs.  */
8495
0
        if ((o->flags & SEC_RELOC) == 0
8496
0
      || o->reloc_count == 0
8497
0
      || section_allows_mips16_refs_p (o))
8498
0
    continue;
8499
8500
0
        sec_relocs
8501
0
    = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8502
0
               info->keep_memory);
8503
0
        if (sec_relocs == NULL)
8504
0
    return false;
8505
8506
0
        rend = sec_relocs + o->reloc_count;
8507
0
        for (r = sec_relocs; r < rend; r++)
8508
0
    if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8509
0
        && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
8510
0
      break;
8511
8512
0
        if (elf_section_data (o)->relocs != sec_relocs)
8513
0
    free (sec_relocs);
8514
8515
0
        if (r < rend)
8516
0
    break;
8517
0
      }
8518
8519
0
    if (o == NULL)
8520
0
      {
8521
        /* There is no non-call reloc for this stub, so we do
8522
     not need it.  Since this function is called before
8523
     the linker maps input sections to output sections, we
8524
     can easily discard it by setting the SEC_EXCLUDE
8525
     flag.  */
8526
0
        sec->flags |= SEC_EXCLUDE;
8527
0
        return true;
8528
0
      }
8529
8530
    /* Record this stub in an array of local symbol stubs for
8531
       this BFD.  */
8532
0
    if (mips_elf_tdata (abfd)->local_stubs == NULL)
8533
0
      {
8534
0
        unsigned long symcount;
8535
0
        asection **n;
8536
0
        bfd_size_type amt;
8537
8538
0
        if (elf_bad_symtab (abfd))
8539
0
    symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8540
0
        else
8541
0
    symcount = symtab_hdr->sh_info;
8542
0
        amt = symcount * sizeof (asection *);
8543
0
        n = bfd_zalloc (abfd, amt);
8544
0
        if (n == NULL)
8545
0
    return false;
8546
0
        mips_elf_tdata (abfd)->local_stubs = n;
8547
0
      }
8548
8549
0
    sec->flags |= SEC_KEEP;
8550
0
    mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
8551
8552
    /* We don't need to set mips16_stubs_seen in this case.
8553
       That flag is used to see whether we need to look through
8554
       the global symbol table for stubs.  We don't need to set
8555
       it here, because we just have a local stub.  */
8556
0
  }
8557
0
      else
8558
0
  {
8559
0
    struct mips_elf_link_hash_entry *h;
8560
8561
0
    h = ((struct mips_elf_link_hash_entry *)
8562
0
         sym_hashes[r_symndx - extsymoff]);
8563
8564
0
    while (h->root.root.type == bfd_link_hash_indirect
8565
0
     || h->root.root.type == bfd_link_hash_warning)
8566
0
      h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8567
8568
    /* H is the symbol this stub is for.  */
8569
8570
    /* If we already have an appropriate stub for this function, we
8571
       don't need another one, so we can discard this one.  Since
8572
       this function is called before the linker maps input sections
8573
       to output sections, we can easily discard it by setting the
8574
       SEC_EXCLUDE flag.  */
8575
0
    if (h->fn_stub != NULL)
8576
0
      {
8577
0
        sec->flags |= SEC_EXCLUDE;
8578
0
        return true;
8579
0
      }
8580
8581
0
    sec->flags |= SEC_KEEP;
8582
0
    h->fn_stub = sec;
8583
0
    mips_elf_hash_table (info)->mips16_stubs_seen = true;
8584
0
  }
8585
0
    }
8586
0
  else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
8587
0
    {
8588
0
      unsigned long r_symndx;
8589
0
      struct mips_elf_link_hash_entry *h;
8590
0
      asection **loc;
8591
8592
      /* Look at the relocation information to figure out which symbol
8593
   this is for.  */
8594
8595
0
      r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8596
0
      if (r_symndx == 0)
8597
0
  {
8598
0
    _bfd_error_handler
8599
      /* xgettext:c-format */
8600
0
      (_("%pB: warning: cannot determine the target function for"
8601
0
         " stub section `%s'"),
8602
0
       abfd, name);
8603
0
    bfd_set_error (bfd_error_bad_value);
8604
0
    return false;
8605
0
  }
8606
8607
0
      if (r_symndx < extsymoff
8608
0
    || sym_hashes[r_symndx - extsymoff] == NULL)
8609
0
  {
8610
0
    asection *o;
8611
8612
    /* This stub is for a local symbol.  This stub will only be
8613
       needed if there is some relocation (R_MIPS16_26) in this BFD
8614
       that refers to this symbol.  */
8615
0
    for (o = abfd->sections; o != NULL; o = o->next)
8616
0
      {
8617
0
        Elf_Internal_Rela *sec_relocs;
8618
0
        const Elf_Internal_Rela *r, *rend;
8619
8620
        /* We can ignore stub sections when looking for relocs.  */
8621
0
        if ((o->flags & SEC_RELOC) == 0
8622
0
      || o->reloc_count == 0
8623
0
      || section_allows_mips16_refs_p (o))
8624
0
    continue;
8625
8626
0
        sec_relocs
8627
0
    = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8628
0
               info->keep_memory);
8629
0
        if (sec_relocs == NULL)
8630
0
    return false;
8631
8632
0
        rend = sec_relocs + o->reloc_count;
8633
0
        for (r = sec_relocs; r < rend; r++)
8634
0
    if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8635
0
        && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
8636
0
        break;
8637
8638
0
        if (elf_section_data (o)->relocs != sec_relocs)
8639
0
    free (sec_relocs);
8640
8641
0
        if (r < rend)
8642
0
    break;
8643
0
      }
8644
8645
0
    if (o == NULL)
8646
0
      {
8647
        /* There is no non-call reloc for this stub, so we do
8648
     not need it.  Since this function is called before
8649
     the linker maps input sections to output sections, we
8650
     can easily discard it by setting the SEC_EXCLUDE
8651
     flag.  */
8652
0
        sec->flags |= SEC_EXCLUDE;
8653
0
        return true;
8654
0
      }
8655
8656
    /* Record this stub in an array of local symbol call_stubs for
8657
       this BFD.  */
8658
0
    if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
8659
0
      {
8660
0
        unsigned long symcount;
8661
0
        asection **n;
8662
0
        bfd_size_type amt;
8663
8664
0
        if (elf_bad_symtab (abfd))
8665
0
    symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8666
0
        else
8667
0
    symcount = symtab_hdr->sh_info;
8668
0
        amt = symcount * sizeof (asection *);
8669
0
        n = bfd_zalloc (abfd, amt);
8670
0
        if (n == NULL)
8671
0
    return false;
8672
0
        mips_elf_tdata (abfd)->local_call_stubs = n;
8673
0
      }
8674
8675
0
    sec->flags |= SEC_KEEP;
8676
0
    mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
8677
8678
    /* We don't need to set mips16_stubs_seen in this case.
8679
       That flag is used to see whether we need to look through
8680
       the global symbol table for stubs.  We don't need to set
8681
       it here, because we just have a local stub.  */
8682
0
  }
8683
0
      else
8684
0
  {
8685
0
    h = ((struct mips_elf_link_hash_entry *)
8686
0
         sym_hashes[r_symndx - extsymoff]);
8687
8688
    /* H is the symbol this stub is for.  */
8689
8690
0
    if (CALL_FP_STUB_P (name))
8691
0
      loc = &h->call_fp_stub;
8692
0
    else
8693
0
      loc = &h->call_stub;
8694
8695
    /* If we already have an appropriate stub for this function, we
8696
       don't need another one, so we can discard this one.  Since
8697
       this function is called before the linker maps input sections
8698
       to output sections, we can easily discard it by setting the
8699
       SEC_EXCLUDE flag.  */
8700
0
    if (*loc != NULL)
8701
0
      {
8702
0
        sec->flags |= SEC_EXCLUDE;
8703
0
        return true;
8704
0
      }
8705
8706
0
    sec->flags |= SEC_KEEP;
8707
0
    *loc = sec;
8708
0
    mips_elf_hash_table (info)->mips16_stubs_seen = true;
8709
0
  }
8710
0
    }
8711
8712
0
  sreloc = NULL;
8713
0
  contents = NULL;
8714
0
  for (rel = relocs; rel < rel_end; ++rel)
8715
0
    {
8716
0
      unsigned long r_symndx;
8717
0
      unsigned int r_type;
8718
0
      struct elf_link_hash_entry *h;
8719
0
      bool can_make_dynamic_p;
8720
0
      bool call_reloc_p;
8721
0
      bool constrain_symbol_p;
8722
8723
0
      r_symndx = ELF_R_SYM (abfd, rel->r_info);
8724
0
      r_type = ELF_R_TYPE (abfd, rel->r_info);
8725
8726
0
      if (r_symndx < extsymoff)
8727
0
  h = NULL;
8728
0
      else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
8729
0
  {
8730
0
    _bfd_error_handler
8731
      /* xgettext:c-format */
8732
0
      (_("%pB: malformed reloc detected for section %s"),
8733
0
       abfd, name);
8734
0
    bfd_set_error (bfd_error_bad_value);
8735
0
    return false;
8736
0
  }
8737
0
      else
8738
0
  {
8739
0
    h = sym_hashes[r_symndx - extsymoff];
8740
0
    if (h != NULL)
8741
0
      {
8742
0
        while (h->root.type == bfd_link_hash_indirect
8743
0
         || h->root.type == bfd_link_hash_warning)
8744
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
8745
0
      }
8746
0
  }
8747
8748
      /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8749
   relocation into a dynamic one.  */
8750
0
      can_make_dynamic_p = false;
8751
8752
      /* Set CALL_RELOC_P to true if the relocation is for a call,
8753
   and if pointer equality therefore doesn't matter.  */
8754
0
      call_reloc_p = false;
8755
8756
      /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8757
   into account when deciding how to define the symbol.  */
8758
0
      constrain_symbol_p = true;
8759
8760
0
      switch (r_type)
8761
0
  {
8762
0
  case R_MIPS_CALL16:
8763
0
  case R_MIPS_CALL_HI16:
8764
0
  case R_MIPS_CALL_LO16:
8765
0
  case R_MIPS16_CALL16:
8766
0
  case R_MICROMIPS_CALL16:
8767
0
  case R_MICROMIPS_CALL_HI16:
8768
0
  case R_MICROMIPS_CALL_LO16:
8769
0
    call_reloc_p = true;
8770
    /* Fall through.  */
8771
8772
0
  case R_MIPS_GOT16:
8773
0
  case R_MIPS_GOT_LO16:
8774
0
  case R_MIPS_GOT_PAGE:
8775
0
  case R_MIPS_GOT_DISP:
8776
0
  case R_MIPS16_GOT16:
8777
0
  case R_MICROMIPS_GOT16:
8778
0
  case R_MICROMIPS_GOT_LO16:
8779
0
  case R_MICROMIPS_GOT_PAGE:
8780
0
  case R_MICROMIPS_GOT_DISP:
8781
    /* If we have a symbol that will resolve to zero at static link
8782
       time and it is used by a GOT relocation applied to code we
8783
       cannot relax to an immediate zero load, then we will be using
8784
       the special `__gnu_absolute_zero' symbol whose value is zero
8785
       at dynamic load time.  We ignore HI16-type GOT relocations at
8786
       this stage, because their handling will depend entirely on
8787
       the corresponding LO16-type GOT relocation.  */
8788
0
    if (!call_hi16_reloc_p (r_type)
8789
0
        && h != NULL
8790
0
        && bfd_link_pic (info)
8791
0
        && !htab->use_absolute_zero
8792
0
        && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8793
0
      {
8794
0
        bool rel_reloc;
8795
8796
0
        if (!mips_elf_get_section_contents (abfd, sec, &contents))
8797
0
    return false;
8798
8799
0
        rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
8800
0
        howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
8801
0
        if (bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8802
0
    if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
8803
0
            false))
8804
0
      if (!mips_elf_define_absolute_zero (abfd, info, htab,
8805
0
                  r_type))
8806
0
        return false;
8807
0
      }
8808
8809
    /* Fall through.  */
8810
0
  case R_MIPS_GOT_HI16:
8811
0
  case R_MIPS_GOT_OFST:
8812
0
  case R_MIPS_TLS_GOTTPREL:
8813
0
  case R_MIPS_TLS_GD:
8814
0
  case R_MIPS_TLS_LDM:
8815
0
  case R_MIPS16_TLS_GOTTPREL:
8816
0
  case R_MIPS16_TLS_GD:
8817
0
  case R_MIPS16_TLS_LDM:
8818
0
  case R_MICROMIPS_GOT_HI16:
8819
0
  case R_MICROMIPS_GOT_OFST:
8820
0
  case R_MICROMIPS_TLS_GOTTPREL:
8821
0
  case R_MICROMIPS_TLS_GD:
8822
0
  case R_MICROMIPS_TLS_LDM:
8823
0
    if (dynobj == NULL)
8824
0
      elf_hash_table (info)->dynobj = dynobj = abfd;
8825
0
    if (!mips_elf_create_got_section (dynobj, info))
8826
0
      return false;
8827
0
    if (htab->root.target_os == is_vxworks
8828
0
        && !bfd_link_pic (info))
8829
0
      {
8830
0
        _bfd_error_handler
8831
    /* xgettext:c-format */
8832
0
    (_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
8833
0
     abfd, (uint64_t) rel->r_offset);
8834
0
        bfd_set_error (bfd_error_bad_value);
8835
0
        return false;
8836
0
      }
8837
0
    can_make_dynamic_p = true;
8838
0
    break;
8839
8840
0
  case R_MIPS_NONE:
8841
0
  case R_MIPS_JALR:
8842
0
  case R_MICROMIPS_JALR:
8843
    /* These relocations have empty fields and are purely there to
8844
       provide link information.  The symbol value doesn't matter.  */
8845
0
    constrain_symbol_p = false;
8846
0
    break;
8847
8848
0
  case R_MIPS_GPREL16:
8849
0
  case R_MIPS_GPREL32:
8850
0
  case R_MIPS16_GPREL:
8851
0
  case R_MICROMIPS_GPREL16:
8852
    /* GP-relative relocations always resolve to a definition in a
8853
       regular input file, ignoring the one-definition rule.  This is
8854
       important for the GP setup sequence in NewABI code, which
8855
       always resolves to a local function even if other relocations
8856
       against the symbol wouldn't.  */
8857
0
    constrain_symbol_p = false;
8858
0
    break;
8859
8860
0
  case R_MIPS_32:
8861
0
  case R_MIPS_REL32:
8862
0
  case R_MIPS_64:
8863
    /* In VxWorks executables, references to external symbols
8864
       must be handled using copy relocs or PLT entries; it is not
8865
       possible to convert this relocation into a dynamic one.
8866
8867
       For executables that use PLTs and copy-relocs, we have a
8868
       choice between converting the relocation into a dynamic
8869
       one or using copy relocations or PLT entries.  It is
8870
       usually better to do the former, unless the relocation is
8871
       against a read-only section.  */
8872
0
    if ((bfd_link_pic (info)
8873
0
         || (h != NULL
8874
0
       && htab->root.target_os != is_vxworks
8875
0
       && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8876
0
       && !(!info->nocopyreloc
8877
0
      && !PIC_OBJECT_P (abfd)
8878
0
      && MIPS_ELF_READONLY_SECTION (sec))))
8879
0
        && (sec->flags & SEC_ALLOC) != 0)
8880
0
      {
8881
0
        can_make_dynamic_p = true;
8882
0
        if (dynobj == NULL)
8883
0
    elf_hash_table (info)->dynobj = dynobj = abfd;
8884
0
      }
8885
0
    break;
8886
8887
0
  case R_MIPS_26:
8888
0
  case R_MIPS_PC16:
8889
0
  case R_MIPS_PC21_S2:
8890
0
  case R_MIPS_PC26_S2:
8891
0
  case R_MIPS16_26:
8892
0
  case R_MIPS16_PC16_S1:
8893
0
  case R_MICROMIPS_26_S1:
8894
0
  case R_MICROMIPS_PC7_S1:
8895
0
  case R_MICROMIPS_PC10_S1:
8896
0
  case R_MICROMIPS_PC16_S1:
8897
0
  case R_MICROMIPS_PC23_S2:
8898
0
    call_reloc_p = true;
8899
0
    break;
8900
0
  }
8901
8902
0
      if (h)
8903
0
  {
8904
0
    if (constrain_symbol_p)
8905
0
      {
8906
0
        if (!can_make_dynamic_p)
8907
0
    ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8908
8909
0
        if (!call_reloc_p)
8910
0
    h->pointer_equality_needed = 1;
8911
8912
        /* We must not create a stub for a symbol that has
8913
     relocations related to taking the function's address.
8914
     This doesn't apply to VxWorks, where CALL relocs refer
8915
     to a .got.plt entry instead of a normal .got entry.  */
8916
0
        if (htab->root.target_os != is_vxworks
8917
0
      && (!can_make_dynamic_p || !call_reloc_p))
8918
0
    ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = true;
8919
0
      }
8920
8921
    /* Relocations against the special VxWorks __GOTT_BASE__ and
8922
       __GOTT_INDEX__ symbols must be left to the loader.  Allocate
8923
       room for them in .rela.dyn.  */
8924
0
    if (is_gott_symbol (info, h))
8925
0
      {
8926
0
        if (sreloc == NULL)
8927
0
    {
8928
0
      sreloc = mips_elf_rel_dyn_section (info, true);
8929
0
      if (sreloc == NULL)
8930
0
        return false;
8931
0
    }
8932
0
        mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8933
0
        if (MIPS_ELF_READONLY_SECTION (sec))
8934
    /* We tell the dynamic linker that there are
8935
       relocations against the text segment.  */
8936
0
    info->flags |= DF_TEXTREL;
8937
0
      }
8938
0
  }
8939
0
      else if (call_lo16_reloc_p (r_type)
8940
0
         || got_lo16_reloc_p (r_type)
8941
0
         || got_disp_reloc_p (r_type)
8942
0
         || (got16_reloc_p (r_type)
8943
0
       && htab->root.target_os == is_vxworks))
8944
0
  {
8945
    /* We may need a local GOT entry for this relocation.  We
8946
       don't count R_MIPS_GOT_PAGE because we can estimate the
8947
       maximum number of pages needed by looking at the size of
8948
       the segment.  Similar comments apply to R_MIPS*_GOT16 and
8949
       R_MIPS*_CALL16, except on VxWorks, where GOT relocations
8950
       always evaluate to "G".  We don't count R_MIPS_GOT_HI16, or
8951
       R_MIPS_CALL_HI16 because these are always followed by an
8952
       R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.  */
8953
0
    if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8954
0
             rel->r_addend, info, r_type))
8955
0
      return false;
8956
0
  }
8957
8958
0
      if (h != NULL
8959
0
    && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8960
0
              ELF_ST_IS_MIPS16 (h->other)))
8961
0
  ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = true;
8962
8963
0
      switch (r_type)
8964
0
  {
8965
0
  case R_MIPS_CALL16:
8966
0
  case R_MIPS16_CALL16:
8967
0
  case R_MICROMIPS_CALL16:
8968
0
    if (h == NULL)
8969
0
      {
8970
0
        _bfd_error_handler
8971
    /* xgettext:c-format */
8972
0
    (_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
8973
0
     abfd, (uint64_t) rel->r_offset);
8974
0
        bfd_set_error (bfd_error_bad_value);
8975
0
        return false;
8976
0
      }
8977
    /* Fall through.  */
8978
8979
0
  case R_MIPS_CALL_HI16:
8980
0
  case R_MIPS_CALL_LO16:
8981
0
  case R_MICROMIPS_CALL_HI16:
8982
0
  case R_MICROMIPS_CALL_LO16:
8983
0
    if (h != NULL)
8984
0
      {
8985
        /* Make sure there is room in the regular GOT to hold the
8986
     function's address.  We may eliminate it in favour of
8987
     a .got.plt entry later; see mips_elf_count_got_symbols.  */
8988
0
        if (!mips_elf_record_global_got_symbol (h, abfd, info, true,
8989
0
                  r_type))
8990
0
    return false;
8991
8992
        /* We need a stub, not a plt entry for the undefined
8993
     function.  But we record it as if it needs plt.  See
8994
     _bfd_elf_adjust_dynamic_symbol.  */
8995
0
        h->needs_plt = 1;
8996
0
        h->type = STT_FUNC;
8997
0
      }
8998
0
    break;
8999
9000
0
  case R_MIPS_GOT_PAGE:
9001
0
  case R_MICROMIPS_GOT_PAGE:
9002
0
  case R_MIPS16_GOT16:
9003
0
  case R_MIPS_GOT16:
9004
0
  case R_MIPS_GOT_HI16:
9005
0
  case R_MIPS_GOT_LO16:
9006
0
  case R_MICROMIPS_GOT16:
9007
0
  case R_MICROMIPS_GOT_HI16:
9008
0
  case R_MICROMIPS_GOT_LO16:
9009
0
    if (!h || got_page_reloc_p (r_type))
9010
0
      {
9011
        /* This relocation needs (or may need, if h != NULL) a
9012
     page entry in the GOT.  For R_MIPS_GOT_PAGE we do not
9013
     know for sure until we know whether the symbol is
9014
     preemptible.  */
9015
0
        if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
9016
0
    {
9017
0
      if (!mips_elf_get_section_contents (abfd, sec, &contents))
9018
0
        return false;
9019
0
      howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, false);
9020
0
      addend = mips_elf_read_rel_addend (abfd, sec, rel,
9021
0
                 howto, contents);
9022
0
      if (got16_reloc_p (r_type))
9023
0
        mips_elf_add_lo16_rel_addend (abfd, sec, rel, rel_end,
9024
0
              contents, &addend);
9025
0
      else
9026
0
        addend <<= howto->rightshift;
9027
0
    }
9028
0
        else
9029
0
    addend = rel->r_addend;
9030
0
        if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
9031
0
             h, addend))
9032
0
    return false;
9033
9034
0
        if (h)
9035
0
    {
9036
0
      struct mips_elf_link_hash_entry *hmips =
9037
0
        (struct mips_elf_link_hash_entry *) h;
9038
9039
      /* This symbol is definitely not overridable.  */
9040
0
      if (hmips->root.def_regular
9041
0
          && ! (bfd_link_pic (info) && ! info->symbolic
9042
0
          && ! hmips->root.forced_local))
9043
0
        h = NULL;
9044
0
    }
9045
0
      }
9046
    /* If this is a global, overridable symbol, GOT_PAGE will
9047
       decay to GOT_DISP, so we'll need a GOT entry for it.  */
9048
    /* Fall through.  */
9049
9050
0
  case R_MIPS_GOT_DISP:
9051
0
  case R_MICROMIPS_GOT_DISP:
9052
0
    if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
9053
0
                   false, r_type))
9054
0
      return false;
9055
0
    break;
9056
9057
0
  case R_MIPS_TLS_GOTTPREL:
9058
0
  case R_MIPS16_TLS_GOTTPREL:
9059
0
  case R_MICROMIPS_TLS_GOTTPREL:
9060
0
    if (bfd_link_pic (info))
9061
0
      info->flags |= DF_STATIC_TLS;
9062
    /* Fall through */
9063
9064
0
  case R_MIPS_TLS_LDM:
9065
0
  case R_MIPS16_TLS_LDM:
9066
0
  case R_MICROMIPS_TLS_LDM:
9067
0
    if (tls_ldm_reloc_p (r_type))
9068
0
      {
9069
0
        r_symndx = STN_UNDEF;
9070
0
        h = NULL;
9071
0
      }
9072
    /* Fall through */
9073
9074
0
  case R_MIPS_TLS_GD:
9075
0
  case R_MIPS16_TLS_GD:
9076
0
  case R_MICROMIPS_TLS_GD:
9077
    /* This symbol requires a global offset table entry, or two
9078
       for TLS GD relocations.  */
9079
0
    if (h != NULL)
9080
0
      {
9081
0
        if (!mips_elf_record_global_got_symbol (h, abfd, info,
9082
0
                  false, r_type))
9083
0
    return false;
9084
0
      }
9085
0
    else
9086
0
      {
9087
0
        if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
9088
0
                 rel->r_addend,
9089
0
                 info, r_type))
9090
0
    return false;
9091
0
      }
9092
0
    break;
9093
9094
0
  case R_MIPS_32:
9095
0
  case R_MIPS_REL32:
9096
0
  case R_MIPS_64:
9097
    /* In VxWorks executables, references to external symbols
9098
       are handled using copy relocs or PLT stubs, so there's
9099
       no need to add a .rela.dyn entry for this relocation.  */
9100
0
    if (can_make_dynamic_p)
9101
0
      {
9102
0
        if (sreloc == NULL)
9103
0
    {
9104
0
      sreloc = mips_elf_rel_dyn_section (info, true);
9105
0
      if (sreloc == NULL)
9106
0
        return false;
9107
0
    }
9108
0
        if (bfd_link_pic (info) && h == NULL)
9109
0
    {
9110
      /* When creating a shared object, we must copy these
9111
         reloc types into the output file as R_MIPS_REL32
9112
         relocs.  Make room for this reloc in .rel(a).dyn.  */
9113
0
      mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9114
0
      if (MIPS_ELF_READONLY_SECTION (sec))
9115
        /* We tell the dynamic linker that there are
9116
           relocations against the text segment.  */
9117
0
        info->flags |= DF_TEXTREL;
9118
0
    }
9119
0
        else
9120
0
    {
9121
0
      struct mips_elf_link_hash_entry *hmips;
9122
9123
      /* For a shared object, we must copy this relocation
9124
         unless the symbol turns out to be undefined and
9125
         weak with non-default visibility, in which case
9126
         it will be left as zero.
9127
9128
         We could elide R_MIPS_REL32 for locally binding symbols
9129
         in shared libraries, but do not yet do so.
9130
9131
         For an executable, we only need to copy this
9132
         reloc if the symbol is defined in a dynamic
9133
         object.  */
9134
0
      hmips = (struct mips_elf_link_hash_entry *) h;
9135
0
      ++hmips->possibly_dynamic_relocs;
9136
0
      if (MIPS_ELF_READONLY_SECTION (sec))
9137
        /* We need it to tell the dynamic linker if there
9138
           are relocations against the text segment.  */
9139
0
        hmips->readonly_reloc = true;
9140
0
    }
9141
0
      }
9142
9143
0
    if (SGI_COMPAT (abfd))
9144
0
      mips_elf_hash_table (info)->compact_rel_size +=
9145
0
        sizeof (Elf32_External_crinfo);
9146
0
    break;
9147
9148
0
  case R_MIPS_26:
9149
0
  case R_MIPS_GPREL16:
9150
0
  case R_MIPS_LITERAL:
9151
0
  case R_MIPS_GPREL32:
9152
0
  case R_MICROMIPS_26_S1:
9153
0
  case R_MICROMIPS_GPREL16:
9154
0
  case R_MICROMIPS_LITERAL:
9155
0
  case R_MICROMIPS_GPREL7_S2:
9156
0
    if (SGI_COMPAT (abfd))
9157
0
      mips_elf_hash_table (info)->compact_rel_size +=
9158
0
        sizeof (Elf32_External_crinfo);
9159
0
    break;
9160
9161
    /* This relocation describes the C++ object vtable hierarchy.
9162
       Reconstruct it for later use during GC.  */
9163
0
  case R_MIPS_GNU_VTINHERIT:
9164
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
9165
0
      return false;
9166
0
    break;
9167
9168
    /* This relocation describes which C++ vtable entries are actually
9169
       used.  Record for later use during GC.  */
9170
0
  case R_MIPS_GNU_VTENTRY:
9171
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
9172
0
      return false;
9173
0
    break;
9174
9175
0
  default:
9176
0
    break;
9177
0
  }
9178
9179
      /* Record the need for a PLT entry.  At this point we don't know
9180
   yet if we are going to create a PLT in the first place, but
9181
   we only record whether the relocation requires a standard MIPS
9182
   or a compressed code entry anyway.  If we don't make a PLT after
9183
   all, then we'll just ignore these arrangements.  Likewise if
9184
   a PLT entry is not created because the symbol is satisfied
9185
   locally.  */
9186
0
      if (h != NULL
9187
0
    && (branch_reloc_p (r_type)
9188
0
        || mips16_branch_reloc_p (r_type)
9189
0
        || micromips_branch_reloc_p (r_type))
9190
0
    && !SYMBOL_CALLS_LOCAL (info, h))
9191
0
  {
9192
0
    if (h->plt.plist == NULL)
9193
0
      h->plt.plist = mips_elf_make_plt_record (abfd);
9194
0
    if (h->plt.plist == NULL)
9195
0
      return false;
9196
9197
0
    if (branch_reloc_p (r_type))
9198
0
      h->plt.plist->need_mips = true;
9199
0
    else
9200
0
      h->plt.plist->need_comp = true;
9201
0
  }
9202
9203
      /* See if this reloc would need to refer to a MIPS16 hard-float stub,
9204
   if there is one.  We only need to handle global symbols here;
9205
   we decide whether to keep or delete stubs for local symbols
9206
   when processing the stub's relocations.  */
9207
0
      if (h != NULL
9208
0
    && !mips16_call_reloc_p (r_type)
9209
0
    && !section_allows_mips16_refs_p (sec))
9210
0
  {
9211
0
    struct mips_elf_link_hash_entry *mh;
9212
9213
0
    mh = (struct mips_elf_link_hash_entry *) h;
9214
0
    mh->need_fn_stub = true;
9215
0
  }
9216
9217
      /* Refuse some position-dependent relocations when creating a
9218
   shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
9219
   not PIC, but we can create dynamic relocations and the result
9220
   will be fine.  Also do not refuse R_MIPS_LO16, which can be
9221
   combined with R_MIPS_GOT16.  */
9222
0
      if (bfd_link_pic (info))
9223
0
  {
9224
0
    switch (r_type)
9225
0
      {
9226
0
      case R_MIPS_TLS_TPREL_HI16:
9227
0
      case R_MIPS16_TLS_TPREL_HI16:
9228
0
      case R_MICROMIPS_TLS_TPREL_HI16:
9229
0
      case R_MIPS_TLS_TPREL_LO16:
9230
0
      case R_MIPS16_TLS_TPREL_LO16:
9231
0
      case R_MICROMIPS_TLS_TPREL_LO16:
9232
        /* These are okay in PIE, but not in a shared library.  */
9233
0
        if (bfd_link_executable (info))
9234
0
    break;
9235
9236
        /* FALLTHROUGH */
9237
9238
0
      case R_MIPS16_HI16:
9239
0
      case R_MIPS_HI16:
9240
0
      case R_MIPS_HIGHER:
9241
0
      case R_MIPS_HIGHEST:
9242
0
      case R_MICROMIPS_HI16:
9243
0
      case R_MICROMIPS_HIGHER:
9244
0
      case R_MICROMIPS_HIGHEST:
9245
        /* Don't refuse a high part relocation if it's against
9246
     no symbol (e.g. part of a compound relocation).  */
9247
0
        if (r_symndx == STN_UNDEF)
9248
0
    break;
9249
9250
        /* Likewise an absolute symbol.  */
9251
0
        if (h != NULL && bfd_is_abs_symbol (&h->root))
9252
0
    break;
9253
9254
        /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
9255
     and has a special meaning.  */
9256
0
        if (!NEWABI_P (abfd) && h != NULL
9257
0
      && strcmp (h->root.root.string, "_gp_disp") == 0)
9258
0
    break;
9259
9260
        /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks.  */
9261
0
        if (is_gott_symbol (info, h))
9262
0
    break;
9263
9264
        /* FALLTHROUGH */
9265
9266
0
      case R_MIPS16_26:
9267
0
      case R_MIPS_26:
9268
0
      case R_MICROMIPS_26_S1:
9269
0
        howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
9270
        /* An error for unsupported relocations is raised as part
9271
     of the above search, so we can skip the following.  */
9272
0
        if (howto != NULL)
9273
0
    info->callbacks->einfo
9274
      /* xgettext:c-format */
9275
0
      (_("%X%H: relocation %s against `%s' cannot be used"
9276
0
         " when making a shared object; recompile with -fPIC\n"),
9277
0
       abfd, sec, rel->r_offset, howto->name,
9278
0
       (h) ? h->root.root.string : "a local symbol");
9279
0
        break;
9280
0
      default:
9281
0
        break;
9282
0
      }
9283
0
  }
9284
0
    }
9285
9286
0
  return true;
9287
0
}
9288

9289
/* Allocate space for global sym dynamic relocs.  */
9290
9291
static bool
9292
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9293
0
{
9294
0
  struct bfd_link_info *info = inf;
9295
0
  bfd *dynobj;
9296
0
  struct mips_elf_link_hash_entry *hmips;
9297
0
  struct mips_elf_link_hash_table *htab;
9298
9299
0
  htab = mips_elf_hash_table (info);
9300
0
  BFD_ASSERT (htab != NULL);
9301
9302
0
  dynobj = elf_hash_table (info)->dynobj;
9303
0
  hmips = (struct mips_elf_link_hash_entry *) h;
9304
9305
  /* VxWorks executables are handled elsewhere; we only need to
9306
     allocate relocations in shared objects.  */
9307
0
  if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9308
0
    return true;
9309
9310
  /* Ignore indirect symbols.  All relocations against such symbols
9311
     will be redirected to the target symbol.  */
9312
0
  if (h->root.type == bfd_link_hash_indirect)
9313
0
    return true;
9314
9315
  /* If this symbol is defined in a dynamic object, or we are creating
9316
     a shared library, we will need to copy any R_MIPS_32 or
9317
     R_MIPS_REL32 relocs against it into the output file.  */
9318
0
  if (! bfd_link_relocatable (info)
9319
0
      && hmips->possibly_dynamic_relocs != 0
9320
0
      && (h->root.type == bfd_link_hash_defweak
9321
0
    || (!h->def_regular && !ELF_COMMON_DEF_P (h))
9322
0
    || bfd_link_pic (info)))
9323
0
    {
9324
0
      bool do_copy = true;
9325
9326
0
      if (h->root.type == bfd_link_hash_undefweak)
9327
0
  {
9328
    /* Do not copy relocations for undefined weak symbols that
9329
       we are not going to export.  */
9330
0
    if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9331
0
      do_copy = false;
9332
9333
    /* Make sure undefined weak symbols are output as a dynamic
9334
       symbol in PIEs.  */
9335
0
    else if (h->dynindx == -1 && !h->forced_local)
9336
0
      {
9337
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
9338
0
    return false;
9339
0
      }
9340
0
  }
9341
9342
0
      if (do_copy)
9343
0
  {
9344
    /* Even though we don't directly need a GOT entry for this symbol,
9345
       the SVR4 psABI requires it to have a dynamic symbol table
9346
       index greater that DT_MIPS_GOTSYM if there are dynamic
9347
       relocations against it.
9348
9349
       VxWorks does not enforce the same mapping between the GOT
9350
       and the symbol table, so the same requirement does not
9351
       apply there.  */
9352
0
    if (htab->root.target_os != is_vxworks)
9353
0
      {
9354
0
        if (hmips->global_got_area > GGA_RELOC_ONLY)
9355
0
    hmips->global_got_area = GGA_RELOC_ONLY;
9356
0
        hmips->got_only_for_calls = false;
9357
0
      }
9358
9359
0
    mips_elf_allocate_dynamic_relocations
9360
0
      (dynobj, info, hmips->possibly_dynamic_relocs);
9361
0
    if (hmips->readonly_reloc)
9362
      /* We tell the dynamic linker that there are relocations
9363
         against the text segment.  */
9364
0
      info->flags |= DF_TEXTREL;
9365
0
  }
9366
0
    }
9367
9368
0
  return true;
9369
0
}
9370
9371
/* Adjust a symbol defined by a dynamic object and referenced by a
9372
   regular object.  The current definition is in some section of the
9373
   dynamic object, but we're not including those sections.  We have to
9374
   change the definition to something the rest of the link can
9375
   understand.  */
9376
9377
bool
9378
_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
9379
             struct elf_link_hash_entry *h)
9380
0
{
9381
0
  bfd *dynobj;
9382
0
  struct mips_elf_link_hash_entry *hmips;
9383
0
  struct mips_elf_link_hash_table *htab;
9384
0
  asection *s, *srel;
9385
9386
0
  htab = mips_elf_hash_table (info);
9387
0
  BFD_ASSERT (htab != NULL);
9388
9389
0
  dynobj = elf_hash_table (info)->dynobj;
9390
0
  hmips = (struct mips_elf_link_hash_entry *) h;
9391
9392
  /* Make sure we know what is going on here.  */
9393
0
  if (dynobj == NULL
9394
0
      || (! h->needs_plt
9395
0
    && ! h->is_weakalias
9396
0
    && (! h->def_dynamic
9397
0
        || ! h->ref_regular
9398
0
        || h->def_regular)))
9399
0
    {
9400
0
      if (h->type == STT_GNU_IFUNC)
9401
0
  _bfd_error_handler (_("IFUNC symbol %s in dynamic symbol table - IFUNCS are not supported"),
9402
0
          h->root.root.string);
9403
0
      else
9404
0
  _bfd_error_handler (_("non-dynamic symbol %s in dynamic symbol table"),
9405
0
          h->root.root.string);
9406
0
      return true;
9407
0
    }
9408
9409
0
  hmips = (struct mips_elf_link_hash_entry *) h;
9410
9411
  /* If there are call relocations against an externally-defined symbol,
9412
     see whether we can create a MIPS lazy-binding stub for it.  We can
9413
     only do this if all references to the function are through call
9414
     relocations, and in that case, the traditional lazy-binding stubs
9415
     are much more efficient than PLT entries.
9416
9417
     Traditional stubs are only available on SVR4 psABI-based systems;
9418
     VxWorks always uses PLTs instead.  */
9419
0
  if (htab->root.target_os != is_vxworks
9420
0
      && h->needs_plt
9421
0
      && !hmips->no_fn_stub)
9422
0
    {
9423
0
      if (! elf_hash_table (info)->dynamic_sections_created)
9424
0
  return true;
9425
9426
      /* If this symbol is not defined in a regular file, then set
9427
   the symbol to the stub location.  This is required to make
9428
   function pointers compare as equal between the normal
9429
   executable and the shared library.  */
9430
0
      if (!h->def_regular
9431
0
    && !bfd_is_abs_section (htab->sstubs->output_section))
9432
0
  {
9433
0
    hmips->needs_lazy_stub = true;
9434
0
    htab->lazy_stub_count++;
9435
0
    return true;
9436
0
  }
9437
0
    }
9438
  /* As above, VxWorks requires PLT entries for externally-defined
9439
     functions that are only accessed through call relocations.
9440
9441
     Both VxWorks and non-VxWorks targets also need PLT entries if there
9442
     are static-only relocations against an externally-defined function.
9443
     This can technically occur for shared libraries if there are
9444
     branches to the symbol, although it is unlikely that this will be
9445
     used in practice due to the short ranges involved.  It can occur
9446
     for any relative or absolute relocation in executables; in that
9447
     case, the PLT entry becomes the function's canonical address.  */
9448
0
  else if (((h->needs_plt && !hmips->no_fn_stub)
9449
0
      || (h->type == STT_FUNC && hmips->has_static_relocs))
9450
0
     && htab->use_plts_and_copy_relocs
9451
0
     && !SYMBOL_CALLS_LOCAL (info, h)
9452
0
     && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9453
0
    && h->root.type == bfd_link_hash_undefweak))
9454
0
    {
9455
0
      bool micromips_p = MICROMIPS_P (info->output_bfd);
9456
0
      bool newabi_p = NEWABI_P (info->output_bfd);
9457
9458
      /* If this is the first symbol to need a PLT entry, then make some
9459
   basic setup.  Also work out PLT entry sizes.  We'll need them
9460
   for PLT offset calculations.  */
9461
0
      if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
9462
0
  {
9463
0
    BFD_ASSERT (htab->root.sgotplt->size == 0);
9464
0
    BFD_ASSERT (htab->plt_got_index == 0);
9465
9466
    /* If we're using the PLT additions to the psABI, each PLT
9467
       entry is 16 bytes and the PLT0 entry is 32 bytes.
9468
       Encourage better cache usage by aligning.  We do this
9469
       lazily to avoid pessimizing traditional objects.  */
9470
0
    if (htab->root.target_os != is_vxworks
9471
0
        && !bfd_link_align_section (htab->root.splt, 5))
9472
0
      return false;
9473
9474
    /* Make sure that .got.plt is word-aligned.  We do this lazily
9475
       for the same reason as above.  */
9476
0
    if (!bfd_link_align_section (htab->root.sgotplt,
9477
0
               MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
9478
0
      return false;
9479
9480
    /* On non-VxWorks targets, the first two entries in .got.plt
9481
       are reserved.  */
9482
0
    if (htab->root.target_os != is_vxworks)
9483
0
      htab->plt_got_index
9484
0
        += (get_elf_backend_data (dynobj)->got_header_size
9485
0
      / MIPS_ELF_GOT_SIZE (dynobj));
9486
9487
    /* On VxWorks, also allocate room for the header's
9488
       .rela.plt.unloaded entries.  */
9489
0
    if (htab->root.target_os == is_vxworks
9490
0
        && !bfd_link_pic (info))
9491
0
      htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
9492
9493
    /* Now work out the sizes of individual PLT entries.  */
9494
0
    if (htab->root.target_os == is_vxworks
9495
0
        && bfd_link_pic (info))
9496
0
      htab->plt_mips_entry_size
9497
0
        = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
9498
0
    else if (htab->root.target_os == is_vxworks)
9499
0
      htab->plt_mips_entry_size
9500
0
        = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
9501
0
    else if (newabi_p)
9502
0
      htab->plt_mips_entry_size
9503
0
        = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9504
0
    else if (!micromips_p)
9505
0
      {
9506
0
        htab->plt_mips_entry_size
9507
0
    = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9508
0
        htab->plt_comp_entry_size
9509
0
    = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
9510
0
      }
9511
0
    else if (htab->insn32)
9512
0
      {
9513
0
        htab->plt_mips_entry_size
9514
0
    = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9515
0
        htab->plt_comp_entry_size
9516
0
    = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
9517
0
      }
9518
0
    else
9519
0
      {
9520
0
        htab->plt_mips_entry_size
9521
0
    = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9522
0
        htab->plt_comp_entry_size
9523
0
    = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
9524
0
      }
9525
0
  }
9526
9527
0
      if (h->plt.plist == NULL)
9528
0
  h->plt.plist = mips_elf_make_plt_record (dynobj);
9529
0
      if (h->plt.plist == NULL)
9530
0
  return false;
9531
9532
      /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
9533
   n32 or n64, so always use a standard entry there.
9534
9535
   If the symbol has a MIPS16 call stub and gets a PLT entry, then
9536
   all MIPS16 calls will go via that stub, and there is no benefit
9537
   to having a MIPS16 entry.  And in the case of call_stub a
9538
   standard entry actually has to be used as the stub ends with a J
9539
   instruction.  */
9540
0
      if (newabi_p
9541
0
    || htab->root.target_os == is_vxworks
9542
0
    || hmips->call_stub
9543
0
    || hmips->call_fp_stub)
9544
0
  {
9545
0
    h->plt.plist->need_mips = true;
9546
0
    h->plt.plist->need_comp = false;
9547
0
  }
9548
9549
      /* Otherwise, if there are no direct calls to the function, we
9550
   have a free choice of whether to use standard or compressed
9551
   entries.  Prefer microMIPS entries if the object is known to
9552
   contain microMIPS code, so that it becomes possible to create
9553
   pure microMIPS binaries.  Prefer standard entries otherwise,
9554
   because MIPS16 ones are no smaller and are usually slower.  */
9555
0
      if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
9556
0
  {
9557
0
    if (micromips_p)
9558
0
      h->plt.plist->need_comp = true;
9559
0
    else
9560
0
      h->plt.plist->need_mips = true;
9561
0
  }
9562
9563
0
      if (h->plt.plist->need_mips)
9564
0
  {
9565
0
    h->plt.plist->mips_offset = htab->plt_mips_offset;
9566
0
    htab->plt_mips_offset += htab->plt_mips_entry_size;
9567
0
  }
9568
0
      if (h->plt.plist->need_comp)
9569
0
  {
9570
0
    h->plt.plist->comp_offset = htab->plt_comp_offset;
9571
0
    htab->plt_comp_offset += htab->plt_comp_entry_size;
9572
0
  }
9573
9574
      /* Reserve the corresponding .got.plt entry now too.  */
9575
0
      h->plt.plist->gotplt_index = htab->plt_got_index++;
9576
9577
      /* If the output file has no definition of the symbol, set the
9578
   symbol's value to the address of the stub.  */
9579
0
      if (!bfd_link_pic (info) && !h->def_regular)
9580
0
  hmips->use_plt_entry = true;
9581
9582
      /* Make room for the R_MIPS_JUMP_SLOT relocation.  */
9583
0
      htab->root.srelplt->size += (htab->root.target_os == is_vxworks
9584
0
           ? MIPS_ELF_RELA_SIZE (dynobj)
9585
0
           : MIPS_ELF_REL_SIZE (dynobj));
9586
9587
      /* Make room for the .rela.plt.unloaded relocations.  */
9588
0
      if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9589
0
  htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9590
9591
      /* All relocations against this symbol that could have been made
9592
   dynamic will now refer to the PLT entry instead.  */
9593
0
      hmips->possibly_dynamic_relocs = 0;
9594
9595
0
      return true;
9596
0
    }
9597
9598
  /* If this is a weak symbol, and there is a real definition, the
9599
     processor independent code will have arranged for us to see the
9600
     real definition first, and we can just use the same value.  */
9601
0
  if (h->is_weakalias)
9602
0
    {
9603
0
      struct elf_link_hash_entry *def = weakdef (h);
9604
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
9605
0
      h->root.u.def.section = def->root.u.def.section;
9606
0
      h->root.u.def.value = def->root.u.def.value;
9607
0
      return true;
9608
0
    }
9609
9610
  /* Otherwise, there is nothing further to do for symbols defined
9611
     in regular objects.  */
9612
0
  if (h->def_regular)
9613
0
    return true;
9614
9615
  /* There's also nothing more to do if we'll convert all relocations
9616
     against this symbol into dynamic relocations.  */
9617
0
  if (!hmips->has_static_relocs)
9618
0
    return true;
9619
9620
  /* We're now relying on copy relocations.  Complain if we have
9621
     some that we can't convert.  */
9622
0
  if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
9623
0
    {
9624
0
      _bfd_error_handler (_("non-dynamic relocations refer to "
9625
0
          "dynamic symbol %s"),
9626
0
        h->root.root.string);
9627
0
      bfd_set_error (bfd_error_bad_value);
9628
0
      return false;
9629
0
    }
9630
9631
  /* We must allocate the symbol in our .dynbss section, which will
9632
     become part of the .bss section of the executable.  There will be
9633
     an entry for this symbol in the .dynsym section.  The dynamic
9634
     object will contain position independent code, so all references
9635
     from the dynamic object to this symbol will go through the global
9636
     offset table.  The dynamic linker will use the .dynsym entry to
9637
     determine the address it must put in the global offset table, so
9638
     both the dynamic object and the regular object will refer to the
9639
     same memory location for the variable.  */
9640
9641
0
  if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9642
0
    {
9643
0
      s = htab->root.sdynrelro;
9644
0
      srel = htab->root.sreldynrelro;
9645
0
    }
9646
0
  else
9647
0
    {
9648
0
      s = htab->root.sdynbss;
9649
0
      srel = htab->root.srelbss;
9650
0
    }
9651
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9652
0
    {
9653
0
      if (htab->root.target_os == is_vxworks)
9654
0
  srel->size += sizeof (Elf32_External_Rela);
9655
0
      else
9656
0
  mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9657
0
      h->needs_copy = 1;
9658
0
    }
9659
9660
  /* All relocations against this symbol that could have been made
9661
     dynamic will now refer to the local copy instead.  */
9662
0
  hmips->possibly_dynamic_relocs = 0;
9663
9664
0
  return _bfd_elf_adjust_dynamic_copy (info, h, s);
9665
0
}
9666

9667
/* If the link uses a GOT, lay it out and work out its size.  */
9668
9669
static bool
9670
mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9671
0
{
9672
0
  bfd *dynobj;
9673
0
  asection *s;
9674
0
  struct mips_got_info *g;
9675
0
  bfd_size_type loadable_size = 0;
9676
0
  bfd_size_type page_gotno;
9677
0
  bfd *ibfd;
9678
0
  struct mips_elf_traverse_got_arg tga;
9679
0
  struct mips_elf_link_hash_table *htab;
9680
9681
0
  htab = mips_elf_hash_table (info);
9682
0
  BFD_ASSERT (htab != NULL);
9683
9684
0
  s = htab->root.sgot;
9685
0
  if (s == NULL)
9686
0
    return true;
9687
9688
0
  dynobj = elf_hash_table (info)->dynobj;
9689
0
  g = htab->got_info;
9690
9691
  /* Allocate room for the reserved entries.  VxWorks always reserves
9692
     3 entries; other objects only reserve 2 entries.  */
9693
0
  BFD_ASSERT (g->assigned_low_gotno == 0);
9694
0
  if (htab->root.target_os == is_vxworks)
9695
0
    htab->reserved_gotno = 3;
9696
0
  else
9697
0
    htab->reserved_gotno = 2;
9698
0
  g->local_gotno += htab->reserved_gotno;
9699
0
  g->assigned_low_gotno = htab->reserved_gotno;
9700
9701
  /* Decide which symbols need to go in the global part of the GOT and
9702
     count the number of reloc-only GOT symbols.  */
9703
0
  mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9704
9705
0
  if (!mips_elf_resolve_final_got_entries (info, g))
9706
0
    return false;
9707
9708
  /* Calculate the total loadable size of the output.  That
9709
     will give us the maximum number of GOT_PAGE entries
9710
     required.  */
9711
0
  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9712
0
    {
9713
0
      asection *subsection;
9714
9715
0
      for (subsection = ibfd->sections;
9716
0
     subsection;
9717
0
     subsection = subsection->next)
9718
0
  {
9719
0
    if ((subsection->flags & SEC_ALLOC) == 0)
9720
0
      continue;
9721
0
    loadable_size += ((subsection->size + 0xf)
9722
0
          &~ (bfd_size_type) 0xf);
9723
0
  }
9724
0
    }
9725
9726
0
  if (htab->root.target_os == is_vxworks)
9727
    /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9728
       relocations against local symbols evaluate to "G", and the EABI does
9729
       not include R_MIPS_GOT_PAGE.  */
9730
0
    page_gotno = 0;
9731
0
  else
9732
    /* Assume there are two loadable segments consisting of contiguous
9733
       sections.  Is 5 enough?  */
9734
0
    page_gotno = (loadable_size >> 16) + 5;
9735
9736
  /* Choose the smaller of the two page estimates; both are intended to be
9737
     conservative.  */
9738
0
  if (page_gotno > g->page_gotno)
9739
0
    page_gotno = g->page_gotno;
9740
9741
0
  g->local_gotno += page_gotno;
9742
0
  g->assigned_high_gotno = g->local_gotno - 1;
9743
9744
0
  s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9745
0
  s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9746
0
  s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9747
9748
  /* VxWorks does not support multiple GOTs.  It initializes $gp to
9749
     __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9750
     dynamic loader.  */
9751
0
  if (htab->root.target_os != is_vxworks
9752
0
      && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9753
0
    {
9754
0
      if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9755
0
  return false;
9756
0
    }
9757
0
  else
9758
0
    {
9759
      /* Record that all bfds use G.  This also has the effect of freeing
9760
   the per-bfd GOTs, which we no longer need.  */
9761
0
      for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9762
0
  if (mips_elf_bfd_got (ibfd, false))
9763
0
    mips_elf_replace_bfd_got (ibfd, g);
9764
0
      mips_elf_replace_bfd_got (output_bfd, g);
9765
9766
      /* Set up TLS entries.  */
9767
0
      g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9768
0
      tga.info = info;
9769
0
      tga.g = g;
9770
0
      tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9771
0
      htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9772
0
      if (!tga.g)
9773
0
  return false;
9774
0
      BFD_ASSERT (g->tls_assigned_gotno
9775
0
      == g->global_gotno + g->local_gotno + g->tls_gotno);
9776
9777
      /* Each VxWorks GOT entry needs an explicit relocation.  */
9778
0
      if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
9779
0
  g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9780
9781
      /* Allocate room for the TLS relocations.  */
9782
0
      if (g->relocs)
9783
0
  mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9784
0
    }
9785
9786
0
  return true;
9787
0
}
9788
9789
/* Estimate the size of the .MIPS.stubs section.  */
9790
9791
static void
9792
mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9793
0
{
9794
0
  struct mips_elf_link_hash_table *htab;
9795
0
  bfd_size_type dynsymcount;
9796
9797
0
  htab = mips_elf_hash_table (info);
9798
0
  BFD_ASSERT (htab != NULL);
9799
9800
0
  if (htab->lazy_stub_count == 0)
9801
0
    return;
9802
9803
  /* IRIX rld assumes that a function stub isn't at the end of the .text
9804
     section, so add a dummy entry to the end.  */
9805
0
  htab->lazy_stub_count++;
9806
9807
  /* Get a worst-case estimate of the number of dynamic symbols needed.
9808
     At this point, dynsymcount does not account for section symbols
9809
     and count_section_dynsyms may overestimate the number that will
9810
     be needed.  */
9811
0
  dynsymcount = (elf_hash_table (info)->dynsymcount
9812
0
     + count_section_dynsyms (output_bfd, info));
9813
9814
  /* Determine the size of one stub entry.  There's no disadvantage
9815
     from using microMIPS code here, so for the sake of pure-microMIPS
9816
     binaries we prefer it whenever there's any microMIPS code in
9817
     output produced at all.  This has a benefit of stubs being
9818
     shorter by 4 bytes each too, unless in the insn32 mode.  */
9819
0
  if (!MICROMIPS_P (output_bfd))
9820
0
    htab->function_stub_size = (dynsymcount > 0x10000
9821
0
        ? MIPS_FUNCTION_STUB_BIG_SIZE
9822
0
        : MIPS_FUNCTION_STUB_NORMAL_SIZE);
9823
0
  else if (htab->insn32)
9824
0
    htab->function_stub_size = (dynsymcount > 0x10000
9825
0
        ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9826
0
        : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9827
0
  else
9828
0
    htab->function_stub_size = (dynsymcount > 0x10000
9829
0
        ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9830
0
        : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9831
9832
0
  htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9833
0
}
9834
9835
/* A mips_elf_link_hash_traverse callback for which DATA points to a
9836
   mips_htab_traverse_info.  If H needs a traditional MIPS lazy-binding
9837
   stub, allocate an entry in the stubs section.  */
9838
9839
static bool
9840
mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9841
0
{
9842
0
  struct mips_htab_traverse_info *hti = data;
9843
0
  struct mips_elf_link_hash_table *htab;
9844
0
  struct bfd_link_info *info;
9845
0
  bfd *output_bfd;
9846
9847
0
  info = hti->info;
9848
0
  output_bfd = hti->output_bfd;
9849
0
  htab = mips_elf_hash_table (info);
9850
0
  BFD_ASSERT (htab != NULL);
9851
9852
0
  if (h->needs_lazy_stub)
9853
0
    {
9854
0
      bool micromips_p = MICROMIPS_P (output_bfd);
9855
0
      unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9856
0
      bfd_vma isa_bit = micromips_p;
9857
9858
0
      BFD_ASSERT (htab->root.dynobj != NULL);
9859
0
      if (h->root.plt.plist == NULL)
9860
0
  h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9861
0
      if (h->root.plt.plist == NULL)
9862
0
  {
9863
0
    hti->error = true;
9864
0
    return false;
9865
0
  }
9866
0
      h->root.root.u.def.section = htab->sstubs;
9867
0
      h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9868
0
      h->root.plt.plist->stub_offset = htab->sstubs->size;
9869
0
      h->root.other = other;
9870
0
      htab->sstubs->size += htab->function_stub_size;
9871
0
    }
9872
0
  return true;
9873
0
}
9874
9875
/* Allocate offsets in the stubs section to each symbol that needs one.
9876
   Set the final size of the .MIPS.stub section.  */
9877
9878
static bool
9879
mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9880
0
{
9881
0
  bfd *output_bfd = info->output_bfd;
9882
0
  bool micromips_p = MICROMIPS_P (output_bfd);
9883
0
  unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9884
0
  bfd_vma isa_bit = micromips_p;
9885
0
  struct mips_elf_link_hash_table *htab;
9886
0
  struct mips_htab_traverse_info hti;
9887
0
  struct elf_link_hash_entry *h;
9888
0
  bfd *dynobj;
9889
9890
0
  htab = mips_elf_hash_table (info);
9891
0
  BFD_ASSERT (htab != NULL);
9892
9893
0
  if (htab->lazy_stub_count == 0)
9894
0
    return true;
9895
9896
0
  htab->sstubs->size = 0;
9897
0
  hti.info = info;
9898
0
  hti.output_bfd = output_bfd;
9899
0
  hti.error = false;
9900
0
  mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9901
0
  if (hti.error)
9902
0
    return false;
9903
0
  htab->sstubs->size += htab->function_stub_size;
9904
0
  BFD_ASSERT (htab->sstubs->size
9905
0
        == htab->lazy_stub_count * htab->function_stub_size);
9906
9907
0
  dynobj = elf_hash_table (info)->dynobj;
9908
0
  BFD_ASSERT (dynobj != NULL);
9909
0
  h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9910
0
  if (h == NULL)
9911
0
    return false;
9912
0
  h->root.u.def.value = isa_bit;
9913
0
  h->other = other;
9914
0
  h->type = STT_FUNC;
9915
9916
0
  return true;
9917
0
}
9918
9919
/* A mips_elf_link_hash_traverse callback for which DATA points to a
9920
   bfd_link_info.  If H uses the address of a PLT entry as the value
9921
   of the symbol, then set the entry in the symbol table now.  Prefer
9922
   a standard MIPS PLT entry.  */
9923
9924
static bool
9925
mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9926
0
{
9927
0
  struct bfd_link_info *info = data;
9928
0
  bool micromips_p = MICROMIPS_P (info->output_bfd);
9929
0
  struct mips_elf_link_hash_table *htab;
9930
0
  unsigned int other;
9931
0
  bfd_vma isa_bit;
9932
0
  bfd_vma val;
9933
9934
0
  htab = mips_elf_hash_table (info);
9935
0
  BFD_ASSERT (htab != NULL);
9936
9937
0
  if (h->use_plt_entry)
9938
0
    {
9939
0
      BFD_ASSERT (h->root.plt.plist != NULL);
9940
0
      BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9941
0
      || h->root.plt.plist->comp_offset != MINUS_ONE);
9942
9943
0
      val = htab->plt_header_size;
9944
0
      if (h->root.plt.plist->mips_offset != MINUS_ONE)
9945
0
  {
9946
0
    isa_bit = 0;
9947
0
    val += h->root.plt.plist->mips_offset;
9948
0
    other = 0;
9949
0
  }
9950
0
      else
9951
0
  {
9952
0
    isa_bit = 1;
9953
0
    val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9954
0
    other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9955
0
  }
9956
0
      val += isa_bit;
9957
      /* For VxWorks, point at the PLT load stub rather than the lazy
9958
   resolution stub; this stub will become the canonical function
9959
   address.  */
9960
0
      if (htab->root.target_os == is_vxworks)
9961
0
  val += 8;
9962
9963
0
      h->root.root.u.def.section = htab->root.splt;
9964
0
      h->root.root.u.def.value = val;
9965
0
      h->root.other = other;
9966
0
    }
9967
9968
0
  return true;
9969
0
}
9970
9971
/* Set the sizes of the dynamic sections, some mips non-dynamic sections,
9972
   and check for any mips16 stub sections that we can discard.  */
9973
9974
bool
9975
_bfd_mips_elf_late_size_sections (bfd *output_bfd,
9976
          struct bfd_link_info *info)
9977
0
{
9978
0
  bfd *dynobj;
9979
0
  asection *s, *sreldyn;
9980
0
  bool reltext;
9981
0
  struct mips_elf_link_hash_table *htab;
9982
0
  struct mips_htab_traverse_info hti;
9983
9984
0
  htab = mips_elf_hash_table (info);
9985
0
  BFD_ASSERT (htab != NULL);
9986
9987
  /* The .reginfo section has a fixed size.  */
9988
0
  s = bfd_get_section_by_name (output_bfd, ".reginfo");
9989
0
  if (s != NULL)
9990
0
    {
9991
0
      bfd_set_section_size (s, sizeof (Elf32_External_RegInfo));
9992
0
      s->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9993
0
    }
9994
9995
  /* The .MIPS.abiflags section has a fixed size.  */
9996
0
  s = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
9997
0
  if (s != NULL)
9998
0
    {
9999
0
      bfd_set_section_size (s, sizeof (Elf_External_ABIFlags_v0));
10000
0
      s->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
10001
0
    }
10002
10003
0
  hti.info = info;
10004
0
  hti.output_bfd = output_bfd;
10005
0
  hti.error = false;
10006
0
  mips_elf_link_hash_traverse (htab, mips_elf_check_symbols, &hti);
10007
0
  if (hti.error)
10008
0
    return false;
10009
10010
0
  dynobj = htab->root.dynobj;
10011
0
  if (dynobj == NULL)
10012
0
    return true;
10013
10014
0
  if (htab->root.dynamic_sections_created)
10015
0
    {
10016
      /* Set the contents of the .interp section to the interpreter.  */
10017
0
      if (bfd_link_executable (info) && !info->nointerp)
10018
0
  {
10019
0
    s = bfd_get_linker_section (dynobj, ".interp");
10020
0
    BFD_ASSERT (s != NULL);
10021
0
    s->size
10022
0
      = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
10023
0
    s->contents
10024
0
      = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
10025
0
    s->alloced = 1;
10026
0
  }
10027
10028
      /* Figure out the size of the PLT header if we know that we
10029
   are using it.  For the sake of cache alignment always use
10030
   a standard header whenever any standard entries are present
10031
   even if microMIPS entries are present as well.  This also
10032
   lets the microMIPS header rely on the value of $v0 only set
10033
   by microMIPS entries, for a small size reduction.
10034
10035
   Set symbol table entry values for symbols that use the
10036
   address of their PLT entry now that we can calculate it.
10037
10038
   Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
10039
   haven't already in _bfd_elf_create_dynamic_sections.  */
10040
0
      if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
10041
0
  {
10042
0
    bool micromips_p = (MICROMIPS_P (output_bfd)
10043
0
             && !htab->plt_mips_offset);
10044
0
    unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10045
0
    bfd_vma isa_bit = micromips_p;
10046
0
    struct elf_link_hash_entry *h;
10047
0
    bfd_vma size;
10048
10049
0
    BFD_ASSERT (htab->use_plts_and_copy_relocs);
10050
0
    BFD_ASSERT (htab->root.sgotplt->size == 0);
10051
0
    BFD_ASSERT (htab->root.splt->size == 0);
10052
10053
0
    if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
10054
0
      size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
10055
0
    else if (htab->root.target_os == is_vxworks)
10056
0
      size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
10057
0
    else if (ABI_64_P (output_bfd))
10058
0
      size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
10059
0
    else if (ABI_N32_P (output_bfd))
10060
0
      size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
10061
0
    else if (!micromips_p)
10062
0
      size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
10063
0
    else if (htab->insn32)
10064
0
      size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
10065
0
    else
10066
0
      size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
10067
10068
0
    htab->plt_header_is_comp = micromips_p;
10069
0
    htab->plt_header_size = size;
10070
0
    htab->root.splt->size = (size
10071
0
           + htab->plt_mips_offset
10072
0
           + htab->plt_comp_offset);
10073
0
    htab->root.sgotplt->size = (htab->plt_got_index
10074
0
              * MIPS_ELF_GOT_SIZE (dynobj));
10075
10076
0
    mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
10077
10078
0
    if (htab->root.hplt == NULL)
10079
0
      {
10080
0
        h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
10081
0
                 "_PROCEDURE_LINKAGE_TABLE_");
10082
0
        htab->root.hplt = h;
10083
0
        if (h == NULL)
10084
0
    return false;
10085
0
      }
10086
10087
0
    h = htab->root.hplt;
10088
0
    h->root.u.def.value = isa_bit;
10089
0
    h->other = other;
10090
0
    h->type = STT_FUNC;
10091
0
  }
10092
0
    }
10093
10094
  /* Allocate space for global sym dynamic relocs.  */
10095
0
  elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
10096
10097
0
  mips_elf_estimate_stub_size (output_bfd, info);
10098
10099
0
  if (!mips_elf_lay_out_got (output_bfd, info))
10100
0
    return false;
10101
10102
0
  mips_elf_lay_out_lazy_stubs (info);
10103
10104
  /* The check_relocs and adjust_dynamic_symbol entry points have
10105
     determined the sizes of the various dynamic sections.  Allocate
10106
     memory for them.  */
10107
0
  reltext = false;
10108
0
  for (s = dynobj->sections; s != NULL; s = s->next)
10109
0
    {
10110
0
      const char *name;
10111
10112
      /* It's OK to base decisions on the section name, because none
10113
   of the dynobj section names depend upon the input files.  */
10114
0
      name = bfd_section_name (s);
10115
10116
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
10117
0
  continue;
10118
10119
0
      if (startswith (name, ".rel"))
10120
0
  {
10121
0
    if (s->size != 0)
10122
0
      {
10123
0
        const char *outname;
10124
0
        asection *target;
10125
10126
        /* If this relocation section applies to a read only
10127
     section, then we probably need a DT_TEXTREL entry.
10128
     If the relocation section is .rel(a).dyn, we always
10129
     assert a DT_TEXTREL entry rather than testing whether
10130
     there exists a relocation to a read only section or
10131
     not.  */
10132
0
        outname = bfd_section_name (s->output_section);
10133
0
        target = bfd_get_section_by_name (output_bfd, outname + 4);
10134
0
        if ((target != NULL
10135
0
       && (target->flags & SEC_READONLY) != 0
10136
0
       && (target->flags & SEC_ALLOC) != 0)
10137
0
      || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
10138
0
    reltext = true;
10139
10140
        /* We use the reloc_count field as a counter if we need
10141
     to copy relocs into the output file.  */
10142
0
        if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
10143
0
    s->reloc_count = 0;
10144
10145
        /* If combreloc is enabled, elf_link_sort_relocs() will
10146
     sort relocations, but in a different way than we do,
10147
     and before we're done creating relocations.  Also, it
10148
     will move them around between input sections'
10149
     relocation's contents, so our sorting would be
10150
     broken, so don't let it run.  */
10151
0
        info->combreloc = 0;
10152
0
      }
10153
0
  }
10154
0
      else if (bfd_link_executable (info)
10155
0
         && !htab->use_rld_obj_head
10156
0
         && startswith (name, ".rld_map"))
10157
0
  {
10158
    /* We add a room for __rld_map.  It will be filled in by the
10159
       rtld to contain a pointer to the _r_debug structure.  */
10160
0
    s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
10161
0
  }
10162
0
      else if (SGI_COMPAT (output_bfd)
10163
0
         && startswith (name, ".compact_rel"))
10164
0
  s->size += htab->compact_rel_size;
10165
0
      else if (s == htab->root.splt)
10166
0
  {
10167
    /* If the last PLT entry has a branch delay slot, allocate
10168
       room for an extra nop to fill the delay slot.  This is
10169
       for CPUs without load interlocking.  */
10170
0
    if (! LOAD_INTERLOCKS_P (output_bfd)
10171
0
        && htab->root.target_os != is_vxworks
10172
0
        && s->size > 0)
10173
0
      s->size += 4;
10174
0
  }
10175
0
      else if (! startswith (name, ".init")
10176
0
         && s != htab->root.sgot
10177
0
         && s != htab->root.sgotplt
10178
0
         && s != htab->sstubs
10179
0
         && s != htab->root.sdynbss
10180
0
         && s != htab->root.sdynrelro)
10181
0
  {
10182
    /* It's not one of our sections, so don't allocate space.  */
10183
0
    continue;
10184
0
  }
10185
10186
0
      if (s->size == 0)
10187
0
  {
10188
0
    s->flags |= SEC_EXCLUDE;
10189
0
    continue;
10190
0
  }
10191
10192
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
10193
0
  continue;
10194
10195
      /* Allocate memory for the section contents.  */
10196
0
      s->contents = bfd_zalloc (dynobj, s->size);
10197
0
      if (s->contents == NULL)
10198
0
  {
10199
0
    bfd_set_error (bfd_error_no_memory);
10200
0
    return false;
10201
0
  }
10202
0
      s->alloced = 1;
10203
0
    }
10204
10205
0
  if (htab->root.dynamic_sections_created)
10206
0
    {
10207
      /* Add some entries to the .dynamic section.  We fill in the
10208
   values later, in _bfd_mips_elf_finish_dynamic_sections, but we
10209
   must add the entries now so that we get the correct size for
10210
   the .dynamic section.  */
10211
10212
      /* SGI object has the equivalence of DT_DEBUG in the
10213
   DT_MIPS_RLD_MAP entry.  This must come first because glibc
10214
   only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
10215
   may only look at the first one they see.  */
10216
0
      if (!bfd_link_pic (info)
10217
0
    && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
10218
0
  return false;
10219
10220
0
      if (bfd_link_executable (info)
10221
0
    && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
10222
0
  return false;
10223
10224
      /* The DT_DEBUG entry may be filled in by the dynamic linker and
10225
   used by the debugger.  */
10226
0
      if (bfd_link_executable (info)
10227
0
    && !SGI_COMPAT (output_bfd)
10228
0
    && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
10229
0
  return false;
10230
10231
0
      if (reltext
10232
0
    && (SGI_COMPAT (output_bfd)
10233
0
        || htab->root.target_os == is_vxworks))
10234
0
  info->flags |= DF_TEXTREL;
10235
10236
0
      if ((info->flags & DF_TEXTREL) != 0)
10237
0
  {
10238
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
10239
0
      return false;
10240
10241
    /* Clear the DF_TEXTREL flag.  It will be set again if we
10242
       write out an actual text relocation; we may not, because
10243
       at this point we do not know whether e.g. any .eh_frame
10244
       absolute relocations have been converted to PC-relative.  */
10245
0
    info->flags &= ~DF_TEXTREL;
10246
0
  }
10247
10248
0
      if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
10249
0
  return false;
10250
10251
0
      sreldyn = mips_elf_rel_dyn_section (info, false);
10252
0
      if (htab->root.target_os == is_vxworks)
10253
0
  {
10254
    /* VxWorks uses .rela.dyn instead of .rel.dyn.  It does not
10255
       use any of the DT_MIPS_* tags.  */
10256
0
    if (sreldyn && sreldyn->size > 0)
10257
0
      {
10258
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
10259
0
    return false;
10260
10261
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
10262
0
    return false;
10263
10264
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
10265
0
    return false;
10266
0
      }
10267
0
  }
10268
0
      else
10269
0
  {
10270
0
    if (sreldyn && sreldyn->size > 0
10271
0
        && !bfd_is_abs_section (sreldyn->output_section))
10272
0
      {
10273
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
10274
0
    return false;
10275
10276
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
10277
0
    return false;
10278
10279
0
        if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
10280
0
    return false;
10281
0
      }
10282
10283
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
10284
0
      return false;
10285
10286
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
10287
0
      return false;
10288
10289
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
10290
0
      return false;
10291
10292
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
10293
0
      return false;
10294
10295
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
10296
0
      return false;
10297
10298
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
10299
0
      return false;
10300
10301
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
10302
0
      return false;
10303
10304
0
    if (info->emit_gnu_hash
10305
0
        && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_XHASH, 0))
10306
0
      return false;
10307
10308
0
    if (IRIX_COMPAT (dynobj) == ict_irix5
10309
0
        && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
10310
0
      return false;
10311
10312
0
    if (IRIX_COMPAT (dynobj) == ict_irix6
10313
0
        && (bfd_get_section_by_name
10314
0
      (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
10315
0
        && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
10316
0
      return false;
10317
0
  }
10318
0
      if (htab->root.splt->size > 0)
10319
0
  {
10320
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
10321
0
      return false;
10322
10323
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
10324
0
      return false;
10325
10326
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
10327
0
      return false;
10328
10329
0
    if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
10330
0
      return false;
10331
0
  }
10332
0
      if (htab->root.target_os == is_vxworks
10333
0
    && !elf_vxworks_add_dynamic_entries (output_bfd, info))
10334
0
  return false;
10335
0
    }
10336
10337
0
  return true;
10338
0
}
10339

10340
/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
10341
   Adjust its R_ADDEND field so that it is correct for the output file.
10342
   LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
10343
   and sections respectively; both use symbol indexes.  */
10344
10345
static void
10346
mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
10347
      bfd *input_bfd, Elf_Internal_Sym *local_syms,
10348
      asection **local_sections, Elf_Internal_Rela *rel)
10349
0
{
10350
0
  unsigned int r_type, r_symndx;
10351
0
  Elf_Internal_Sym *sym;
10352
0
  asection *sec;
10353
10354
0
  if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10355
0
    {
10356
0
      r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10357
0
      if (gprel16_reloc_p (r_type)
10358
0
    || r_type == R_MIPS_GPREL32
10359
0
    || literal_reloc_p (r_type))
10360
0
  {
10361
0
    rel->r_addend += _bfd_get_gp_value (input_bfd);
10362
0
    rel->r_addend -= _bfd_get_gp_value (output_bfd);
10363
0
  }
10364
10365
0
      r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
10366
0
      sym = local_syms + r_symndx;
10367
10368
      /* Adjust REL's addend to account for section merging.  */
10369
0
      if (!bfd_link_relocatable (info))
10370
0
  {
10371
0
    sec = local_sections[r_symndx];
10372
0
    _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
10373
0
  }
10374
10375
      /* This would normally be done by the rela_normal code in elflink.c.  */
10376
0
      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10377
0
  rel->r_addend += local_sections[r_symndx]->output_offset;
10378
0
    }
10379
0
}
10380
10381
/* Handle relocations against symbols from removed linkonce sections,
10382
   or sections discarded by a linker script.  We use this wrapper around
10383
   RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
10384
   on 64-bit ELF targets.  In this case for any relocation handled, which
10385
   always be the first in a triplet, the remaining two have to be processed
10386
   together with the first, even if they are R_MIPS_NONE.  It is the symbol
10387
   index referred by the first reloc that applies to all the three and the
10388
   remaining two never refer to an object symbol.  And it is the final
10389
   relocation (the last non-null one) that determines the output field of
10390
   the whole relocation so retrieve the corresponding howto structure for
10391
   the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
10392
10393
   Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
10394
   and therefore requires to be pasted in a loop.  It also defines a block
10395
   and does not protect any of its arguments, hence the extra brackets.  */
10396
10397
static void
10398
mips_reloc_against_discarded_section (bfd *output_bfd,
10399
              struct bfd_link_info *info,
10400
              bfd *input_bfd, asection *input_section,
10401
              Elf_Internal_Rela **rel,
10402
              const Elf_Internal_Rela **relend,
10403
              bool rel_reloc,
10404
              reloc_howto_type *howto,
10405
              bfd_byte *contents)
10406
0
{
10407
0
  const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10408
0
  int count = bed->s->int_rels_per_ext_rel;
10409
0
  unsigned int r_type;
10410
0
  int i;
10411
10412
0
  for (i = count - 1; i > 0; i--)
10413
0
    {
10414
0
      r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
10415
0
      if (r_type != R_MIPS_NONE)
10416
0
  {
10417
0
    howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10418
0
    break;
10419
0
  }
10420
0
    }
10421
0
  do
10422
0
    {
10423
0
       RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
10424
0
          (*rel), count, (*relend),
10425
0
          howto, i, contents);
10426
0
    }
10427
0
  while (0);
10428
0
}
10429
10430
/* Relocate a MIPS ELF section.  */
10431
10432
int
10433
_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
10434
        bfd *input_bfd, asection *input_section,
10435
        bfd_byte *contents, Elf_Internal_Rela *relocs,
10436
        Elf_Internal_Sym *local_syms,
10437
        asection **local_sections)
10438
0
{
10439
0
  Elf_Internal_Rela *rel;
10440
0
  const Elf_Internal_Rela *relend;
10441
0
  bfd_vma addend = 0;
10442
0
  bool use_saved_addend_p = false;
10443
10444
0
  relend = relocs + input_section->reloc_count;
10445
0
  for (rel = relocs; rel < relend; ++rel)
10446
0
    {
10447
0
      const char *name;
10448
0
      bfd_vma value = 0;
10449
0
      reloc_howto_type *howto;
10450
0
      bool cross_mode_jump_p = false;
10451
      /* TRUE if the relocation is a RELA relocation, rather than a
10452
   REL relocation.  */
10453
0
      bool rela_relocation_p = true;
10454
0
      unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10455
0
      const char *msg;
10456
0
      unsigned long r_symndx;
10457
0
      asection *sec;
10458
0
      Elf_Internal_Shdr *symtab_hdr;
10459
0
      struct elf_link_hash_entry *h;
10460
0
      bool rel_reloc;
10461
10462
0
      rel_reloc = (NEWABI_P (input_bfd)
10463
0
       && mips_elf_rel_relocation_p (input_bfd, input_section,
10464
0
             relocs, rel));
10465
      /* Find the relocation howto for this relocation.  */
10466
0
      howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10467
10468
0
      r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
10469
0
      symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10470
0
      if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10471
0
  {
10472
0
    sec = local_sections[r_symndx];
10473
0
    h = NULL;
10474
0
  }
10475
0
      else
10476
0
  {
10477
0
    unsigned long extsymoff;
10478
10479
0
    extsymoff = 0;
10480
0
    if (!elf_bad_symtab (input_bfd))
10481
0
      extsymoff = symtab_hdr->sh_info;
10482
0
    h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
10483
0
    while (h->root.type == bfd_link_hash_indirect
10484
0
     || h->root.type == bfd_link_hash_warning)
10485
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
10486
10487
0
    sec = NULL;
10488
0
    if (h->root.type == bfd_link_hash_defined
10489
0
        || h->root.type == bfd_link_hash_defweak)
10490
0
      sec = h->root.u.def.section;
10491
0
  }
10492
10493
0
      if (sec != NULL && discarded_section (sec))
10494
0
  {
10495
0
    mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
10496
0
            input_section, &rel, &relend,
10497
0
            rel_reloc, howto, contents);
10498
0
    continue;
10499
0
  }
10500
10501
0
      if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
10502
0
  {
10503
    /* Some 32-bit code uses R_MIPS_64.  In particular, people use
10504
       64-bit code, but make sure all their addresses are in the
10505
       lowermost or uppermost 32-bit section of the 64-bit address
10506
       space.  Thus, when they use an R_MIPS_64 they mean what is
10507
       usually meant by R_MIPS_32, with the exception that the
10508
       stored value is sign-extended to 64 bits.  */
10509
0
    howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
10510
10511
    /* On big-endian systems, we need to lie about the position
10512
       of the reloc.  */
10513
0
    if (bfd_big_endian (input_bfd))
10514
0
      rel->r_offset += 4;
10515
0
  }
10516
10517
0
      if (!use_saved_addend_p)
10518
0
  {
10519
    /* If these relocations were originally of the REL variety,
10520
       we must pull the addend out of the field that will be
10521
       relocated.  Otherwise, we simply use the contents of the
10522
       RELA relocation.  */
10523
0
    if (mips_elf_rel_relocation_p (input_bfd, input_section,
10524
0
           relocs, rel))
10525
0
      {
10526
0
        rela_relocation_p = false;
10527
0
        addend = mips_elf_read_rel_addend (input_bfd, input_section,
10528
0
             rel, howto, contents);
10529
0
        if (hi16_reloc_p (r_type)
10530
0
      || (got16_reloc_p (r_type)
10531
0
          && mips_elf_local_relocation_p (input_bfd, rel,
10532
0
                  local_sections)))
10533
0
    {
10534
0
      if (!mips_elf_add_lo16_rel_addend (input_bfd, input_section,
10535
0
                 rel, relend,
10536
0
                 contents, &addend))
10537
0
        {
10538
0
          if (h)
10539
0
      name = h->root.root.string;
10540
0
          else
10541
0
      name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10542
0
             local_syms + r_symndx,
10543
0
             sec);
10544
0
          _bfd_error_handler
10545
      /* xgettext:c-format */
10546
0
      (_("%pB: can't find matching LO16 reloc against `%s'"
10547
0
         " for %s at %#" PRIx64 " in section `%pA'"),
10548
0
       input_bfd, name,
10549
0
       howto->name, (uint64_t) rel->r_offset, input_section);
10550
0
        }
10551
0
    }
10552
0
        else
10553
0
    addend <<= howto->rightshift;
10554
0
      }
10555
0
    else
10556
0
      addend = rel->r_addend;
10557
0
    mips_elf_adjust_addend (output_bfd, info, input_bfd,
10558
0
          local_syms, local_sections, rel);
10559
0
  }
10560
10561
0
      if (bfd_link_relocatable (info))
10562
0
  {
10563
0
    if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
10564
0
        && bfd_big_endian (input_bfd))
10565
0
      rel->r_offset -= 4;
10566
10567
0
    if (!rela_relocation_p && rel->r_addend)
10568
0
      {
10569
0
        addend += rel->r_addend;
10570
0
        if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
10571
0
    addend = mips_elf_high (addend);
10572
0
        else if (r_type == R_MIPS_HIGHER)
10573
0
    addend = mips_elf_higher (addend);
10574
0
        else if (r_type == R_MIPS_HIGHEST)
10575
0
    addend = mips_elf_highest (addend);
10576
0
        else
10577
0
    addend >>= howto->rightshift;
10578
10579
        /* We use the source mask, rather than the destination
10580
     mask because the place to which we are writing will be
10581
     source of the addend in the final link.  */
10582
0
        addend &= howto->src_mask;
10583
10584
0
        if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10585
    /* See the comment above about using R_MIPS_64 in the 32-bit
10586
       ABI.  Here, we need to update the addend.  It would be
10587
       possible to get away with just using the R_MIPS_32 reloc
10588
       but for endianness.  */
10589
0
    {
10590
0
      bfd_vma sign_bits;
10591
0
      bfd_vma low_bits;
10592
0
      bfd_vma high_bits;
10593
10594
0
      if (addend & ((bfd_vma) 1 << 31))
10595
0
#ifdef BFD64
10596
0
        sign_bits = ((bfd_vma) 1 << 32) - 1;
10597
#else
10598
        sign_bits = -1;
10599
#endif
10600
0
      else
10601
0
        sign_bits = 0;
10602
10603
      /* If we don't know that we have a 64-bit type,
10604
         do two separate stores.  */
10605
0
      if (bfd_big_endian (input_bfd))
10606
0
        {
10607
          /* Store the sign-bits (which are most significant)
10608
       first.  */
10609
0
          low_bits = sign_bits;
10610
0
          high_bits = addend;
10611
0
        }
10612
0
      else
10613
0
        {
10614
0
          low_bits = addend;
10615
0
          high_bits = sign_bits;
10616
0
        }
10617
0
      bfd_put_32 (input_bfd, low_bits,
10618
0
            contents + rel->r_offset);
10619
0
      bfd_put_32 (input_bfd, high_bits,
10620
0
            contents + rel->r_offset + 4);
10621
0
      continue;
10622
0
    }
10623
10624
0
        if (! mips_elf_perform_relocation (info, howto, rel, addend,
10625
0
             input_bfd, input_section,
10626
0
             contents, false))
10627
0
    return false;
10628
0
      }
10629
10630
    /* Go on to the next relocation.  */
10631
0
    continue;
10632
0
  }
10633
10634
      /* In the N32 and 64-bit ABIs there may be multiple consecutive
10635
   relocations for the same offset.  In that case we are
10636
   supposed to treat the output of each relocation as the addend
10637
   for the next.  */
10638
0
      if (rel + 1 < relend
10639
0
    && rel->r_offset == rel[1].r_offset
10640
0
    && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
10641
0
  use_saved_addend_p = true;
10642
0
      else
10643
0
  use_saved_addend_p = false;
10644
10645
      /* Figure out what value we are supposed to relocate.  */
10646
0
      switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
10647
0
               input_section, contents,
10648
0
               info, rel, addend, howto,
10649
0
               local_syms, local_sections,
10650
0
               &value, &name, &cross_mode_jump_p,
10651
0
               use_saved_addend_p))
10652
0
  {
10653
0
  case bfd_reloc_continue:
10654
    /* There's nothing to do.  */
10655
0
    continue;
10656
10657
0
  case bfd_reloc_undefined:
10658
    /* mips_elf_calculate_relocation already called the
10659
       undefined_symbol callback.  There's no real point in
10660
       trying to perform the relocation at this point, so we
10661
       just skip ahead to the next relocation.  */
10662
0
    continue;
10663
10664
0
  case bfd_reloc_notsupported:
10665
0
    msg = _("internal error: unsupported relocation error");
10666
0
    info->callbacks->warning
10667
0
      (info, msg, name, input_bfd, input_section, rel->r_offset);
10668
0
    return false;
10669
10670
0
  case bfd_reloc_overflow:
10671
0
    if (use_saved_addend_p)
10672
      /* Ignore overflow until we reach the last relocation for
10673
         a given location.  */
10674
0
      ;
10675
0
    else
10676
0
      {
10677
0
        struct mips_elf_link_hash_table *htab;
10678
10679
0
        htab = mips_elf_hash_table (info);
10680
0
        BFD_ASSERT (htab != NULL);
10681
0
        BFD_ASSERT (name != NULL);
10682
0
        if (!htab->small_data_overflow_reported
10683
0
      && (gprel16_reloc_p (howto->type)
10684
0
          || literal_reloc_p (howto->type)))
10685
0
    {
10686
0
      msg = _("small-data section too large;"
10687
0
        " lower small-data size limit (see option -G)");
10688
10689
0
      htab->small_data_overflow_reported = true;
10690
0
      (*info->callbacks->einfo) ("%P: %s\n", msg);
10691
0
    }
10692
0
        (*info->callbacks->reloc_overflow)
10693
0
    (info, NULL, name, howto->name, (bfd_vma) 0,
10694
0
     input_bfd, input_section, rel->r_offset);
10695
0
      }
10696
0
    break;
10697
10698
0
  case bfd_reloc_ok:
10699
0
    break;
10700
10701
0
  case bfd_reloc_outofrange:
10702
0
    msg = NULL;
10703
0
    if (jal_reloc_p (howto->type))
10704
0
      msg = (cross_mode_jump_p
10705
0
       ? _("cannot convert a jump to JALX "
10706
0
           "for a non-word-aligned address")
10707
0
       : (howto->type == R_MIPS16_26
10708
0
          ? _("jump to a non-word-aligned address")
10709
0
          : _("jump to a non-instruction-aligned address")));
10710
0
    else if (b_reloc_p (howto->type))
10711
0
      msg = (cross_mode_jump_p
10712
0
       ? _("cannot convert a branch to JALX "
10713
0
           "for a non-word-aligned address")
10714
0
       : _("branch to a non-instruction-aligned address"));
10715
0
    else if (aligned_pcrel_reloc_p (howto->type))
10716
0
      msg = _("PC-relative load from unaligned address");
10717
0
    if (msg)
10718
0
      {
10719
0
        info->callbacks->einfo
10720
0
    ("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
10721
0
        break;
10722
0
      }
10723
    /* Fall through.  */
10724
10725
0
  default:
10726
0
    abort ();
10727
0
    break;
10728
0
  }
10729
10730
      /* If we've got another relocation for the address, keep going
10731
   until we reach the last one.  */
10732
0
      if (use_saved_addend_p)
10733
0
  {
10734
0
    addend = value;
10735
0
    continue;
10736
0
  }
10737
10738
0
      if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10739
  /* See the comment above about using R_MIPS_64 in the 32-bit
10740
     ABI.  Until now, we've been using the HOWTO for R_MIPS_32;
10741
     that calculated the right value.  Now, however, we
10742
     sign-extend the 32-bit result to 64-bits, and store it as a
10743
     64-bit value.  We are especially generous here in that we
10744
     go to extreme lengths to support this usage on systems with
10745
     only a 32-bit VMA.  */
10746
0
  {
10747
0
    bfd_vma sign_bits;
10748
0
    bfd_vma low_bits;
10749
0
    bfd_vma high_bits;
10750
10751
0
    if (value & ((bfd_vma) 1 << 31))
10752
0
#ifdef BFD64
10753
0
      sign_bits = ((bfd_vma) 1 << 32) - 1;
10754
#else
10755
      sign_bits = -1;
10756
#endif
10757
0
    else
10758
0
      sign_bits = 0;
10759
10760
    /* If we don't know that we have a 64-bit type,
10761
       do two separate stores.  */
10762
0
    if (bfd_big_endian (input_bfd))
10763
0
      {
10764
        /* Undo what we did above.  */
10765
0
        rel->r_offset -= 4;
10766
        /* Store the sign-bits (which are most significant)
10767
     first.  */
10768
0
        low_bits = sign_bits;
10769
0
        high_bits = value;
10770
0
      }
10771
0
    else
10772
0
      {
10773
0
        low_bits = value;
10774
0
        high_bits = sign_bits;
10775
0
      }
10776
0
    bfd_put_32 (input_bfd, low_bits,
10777
0
          contents + rel->r_offset);
10778
0
    bfd_put_32 (input_bfd, high_bits,
10779
0
          contents + rel->r_offset + 4);
10780
0
    continue;
10781
0
  }
10782
10783
      /* Actually perform the relocation.  */
10784
0
      if (! mips_elf_perform_relocation (info, howto, rel, value,
10785
0
           input_bfd, input_section,
10786
0
           contents, cross_mode_jump_p))
10787
0
  return false;
10788
0
    }
10789
10790
0
  return true;
10791
0
}
10792

10793
/* A function that iterates over each entry in la25_stubs and fills
10794
   in the code for each one.  DATA points to a mips_htab_traverse_info.  */
10795
10796
static int
10797
mips_elf_create_la25_stub (void **slot, void *data)
10798
0
{
10799
0
  struct mips_htab_traverse_info *hti;
10800
0
  struct mips_elf_link_hash_table *htab;
10801
0
  struct mips_elf_la25_stub *stub;
10802
0
  asection *s;
10803
0
  bfd_byte *loc;
10804
0
  bfd_vma offset, target, target_high, target_low;
10805
0
  bfd_vma branch_pc;
10806
0
  bfd_signed_vma pcrel_offset = 0;
10807
10808
0
  stub = (struct mips_elf_la25_stub *) *slot;
10809
0
  hti = (struct mips_htab_traverse_info *) data;
10810
0
  htab = mips_elf_hash_table (hti->info);
10811
0
  BFD_ASSERT (htab != NULL);
10812
10813
  /* Create the section contents, if we haven't already.  */
10814
0
  s = stub->stub_section;
10815
0
  loc = s->contents;
10816
0
  if (loc == NULL)
10817
0
    {
10818
0
      loc = bfd_malloc (s->size);
10819
0
      if (loc == NULL)
10820
0
  {
10821
0
    hti->error = true;
10822
0
    return false;
10823
0
  }
10824
0
      s->contents = loc;
10825
0
    }
10826
10827
  /* Work out where in the section this stub should go.  */
10828
0
  offset = stub->offset;
10829
10830
  /* We add 8 here to account for the LUI/ADDIU instructions
10831
     before the branch instruction.  This cannot be moved down to
10832
     where pcrel_offset is calculated as 's' is updated in
10833
     mips_elf_get_la25_target.  */
10834
0
  branch_pc = s->output_section->vma + s->output_offset + offset + 8;
10835
10836
  /* Work out the target address.  */
10837
0
  target = mips_elf_get_la25_target (stub, &s);
10838
0
  target += s->output_section->vma + s->output_offset;
10839
10840
0
  target_high = ((target + 0x8000) >> 16) & 0xffff;
10841
0
  target_low = (target & 0xffff);
10842
10843
  /* Calculate the PC of the compact branch instruction (for the case where
10844
     compact branches are used for either microMIPSR6 or MIPSR6 with
10845
     compact branches.  Add 4-bytes to account for BC using the PC of the
10846
     next instruction as the base.  */
10847
0
  pcrel_offset = target - (branch_pc + 4);
10848
10849
0
  if (stub->stub_section != htab->strampoline)
10850
0
    {
10851
      /* This is a simple LUI/ADDIU stub.  Zero out the beginning
10852
   of the section and write the two instructions at the end.  */
10853
0
      memset (loc, 0, offset);
10854
0
      loc += offset;
10855
0
      if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10856
0
  {
10857
0
    bfd_put_micromips_32 (hti->output_bfd,
10858
0
        LA25_LUI_MICROMIPS (target_high),
10859
0
        loc);
10860
0
    bfd_put_micromips_32 (hti->output_bfd,
10861
0
        LA25_ADDIU_MICROMIPS (target_low),
10862
0
        loc + 4);
10863
0
  }
10864
0
      else
10865
0
  {
10866
0
    bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10867
0
    bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10868
0
  }
10869
0
    }
10870
0
  else
10871
0
    {
10872
      /* This is trampoline.  */
10873
0
      loc += offset;
10874
0
      if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10875
0
  {
10876
0
    bfd_put_micromips_32 (hti->output_bfd,
10877
0
        LA25_LUI_MICROMIPS (target_high), loc);
10878
0
    bfd_put_micromips_32 (hti->output_bfd,
10879
0
        LA25_J_MICROMIPS (target), loc + 4);
10880
0
    bfd_put_micromips_32 (hti->output_bfd,
10881
0
        LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10882
0
    bfd_put_32 (hti->output_bfd, 0, loc + 12);
10883
0
  }
10884
0
      else
10885
0
  {
10886
0
    bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10887
0
    if (MIPSR6_P (hti->output_bfd) && htab->compact_branches)
10888
0
      {
10889
0
        bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10890
0
        bfd_put_32 (hti->output_bfd, LA25_BC (pcrel_offset), loc + 8);
10891
0
      }
10892
0
    else
10893
0
      {
10894
0
        bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10895
0
        bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10896
0
      }
10897
0
    bfd_put_32 (hti->output_bfd, 0, loc + 12);
10898
0
  }
10899
0
    }
10900
0
  return true;
10901
0
}
10902
10903
/* If NAME is one of the special IRIX6 symbols defined by the linker,
10904
   adjust it appropriately now.  */
10905
10906
static void
10907
mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10908
              const char *name, Elf_Internal_Sym *sym)
10909
0
{
10910
  /* The linker script takes care of providing names and values for
10911
     these, but we must place them into the right sections.  */
10912
0
  static const char* const text_section_symbols[] = {
10913
0
    "_ftext",
10914
0
    "_etext",
10915
0
    "__dso_displacement",
10916
0
    "__elf_header",
10917
0
    "__program_header_table",
10918
0
    NULL
10919
0
  };
10920
10921
0
  static const char* const data_section_symbols[] = {
10922
0
    "_fdata",
10923
0
    "_edata",
10924
0
    "_end",
10925
0
    "_fbss",
10926
0
    NULL
10927
0
  };
10928
10929
0
  const char* const *p;
10930
0
  int i;
10931
10932
0
  for (i = 0; i < 2; ++i)
10933
0
    for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10934
0
   *p;
10935
0
   ++p)
10936
0
      if (strcmp (*p, name) == 0)
10937
0
  {
10938
    /* All of these symbols are given type STT_SECTION by the
10939
       IRIX6 linker.  */
10940
0
    sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10941
0
    sym->st_other = STO_PROTECTED;
10942
10943
    /* The IRIX linker puts these symbols in special sections.  */
10944
0
    if (i == 0)
10945
0
      sym->st_shndx = SHN_MIPS_TEXT;
10946
0
    else
10947
0
      sym->st_shndx = SHN_MIPS_DATA;
10948
10949
0
    break;
10950
0
  }
10951
0
}
10952
10953
/* Finish up dynamic symbol handling.  We set the contents of various
10954
   dynamic sections here.  */
10955
10956
bool
10957
_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10958
             struct bfd_link_info *info,
10959
             struct elf_link_hash_entry *h,
10960
             Elf_Internal_Sym *sym)
10961
0
{
10962
0
  bfd *dynobj;
10963
0
  asection *sgot;
10964
0
  struct mips_got_info *g, *gg;
10965
0
  const char *name;
10966
0
  int idx;
10967
0
  struct mips_elf_link_hash_table *htab;
10968
0
  struct mips_elf_link_hash_entry *hmips;
10969
10970
0
  htab = mips_elf_hash_table (info);
10971
0
  BFD_ASSERT (htab != NULL);
10972
0
  dynobj = elf_hash_table (info)->dynobj;
10973
0
  hmips = (struct mips_elf_link_hash_entry *) h;
10974
10975
0
  BFD_ASSERT (htab->root.target_os != is_vxworks);
10976
10977
0
  if (h->plt.plist != NULL
10978
0
      && (h->plt.plist->mips_offset != MINUS_ONE
10979
0
    || h->plt.plist->comp_offset != MINUS_ONE))
10980
0
    {
10981
      /* We've decided to create a PLT entry for this symbol.  */
10982
0
      bfd_byte *loc;
10983
0
      bfd_vma header_address, got_address;
10984
0
      bfd_vma got_address_high, got_address_low, load;
10985
0
      bfd_vma got_index;
10986
0
      bfd_vma isa_bit;
10987
10988
0
      got_index = h->plt.plist->gotplt_index;
10989
10990
0
      BFD_ASSERT (htab->use_plts_and_copy_relocs);
10991
0
      BFD_ASSERT (h->dynindx != -1);
10992
0
      BFD_ASSERT (htab->root.splt != NULL);
10993
0
      BFD_ASSERT (got_index != MINUS_ONE);
10994
0
      BFD_ASSERT (!h->def_regular);
10995
10996
      /* Calculate the address of the PLT header.  */
10997
0
      isa_bit = htab->plt_header_is_comp;
10998
0
      header_address = (htab->root.splt->output_section->vma
10999
0
      + htab->root.splt->output_offset + isa_bit);
11000
11001
      /* Calculate the address of the .got.plt entry.  */
11002
0
      got_address = (htab->root.sgotplt->output_section->vma
11003
0
         + htab->root.sgotplt->output_offset
11004
0
         + got_index * MIPS_ELF_GOT_SIZE (dynobj));
11005
11006
0
      got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11007
0
      got_address_low = got_address & 0xffff;
11008
11009
      /* The PLT sequence is not safe for N64 if .got.plt entry's address
11010
   cannot be loaded in two instructions.  */
11011
0
      if (ABI_64_P (output_bfd)
11012
0
    && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11013
0
  {
11014
0
    _bfd_error_handler
11015
      /* xgettext:c-format */
11016
0
      (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
11017
0
         "supported; consider using `-Ttext-segment=...'"),
11018
0
       output_bfd,
11019
0
       htab->root.sgotplt->output_section,
11020
0
       (int64_t) got_address);
11021
0
    bfd_set_error (bfd_error_no_error);
11022
0
    return false;
11023
0
  }
11024
11025
      /* Initially point the .got.plt entry at the PLT header.  */
11026
0
      loc = (htab->root.sgotplt->contents
11027
0
       + got_index * MIPS_ELF_GOT_SIZE (dynobj));
11028
0
      if (ABI_64_P (output_bfd))
11029
0
  bfd_put_64 (output_bfd, header_address, loc);
11030
0
      else
11031
0
  bfd_put_32 (output_bfd, header_address, loc);
11032
11033
      /* Now handle the PLT itself.  First the standard entry (the order
11034
   does not matter, we just have to pick one).  */
11035
0
      if (h->plt.plist->mips_offset != MINUS_ONE)
11036
0
  {
11037
0
    const bfd_vma *plt_entry;
11038
0
    bfd_vma plt_offset;
11039
11040
0
    plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11041
11042
0
    BFD_ASSERT (plt_offset <= htab->root.splt->size);
11043
11044
    /* Find out where the .plt entry should go.  */
11045
0
    loc = htab->root.splt->contents + plt_offset;
11046
11047
    /* Pick the load opcode.  */
11048
0
    load = MIPS_ELF_LOAD_WORD (output_bfd);
11049
11050
    /* Fill in the PLT entry itself.  */
11051
11052
0
    if (MIPSR6_P (output_bfd))
11053
0
      plt_entry = htab->compact_branches ? mipsr6_exec_plt_entry_compact
11054
0
                 : mipsr6_exec_plt_entry;
11055
0
    else
11056
0
      plt_entry = mips_exec_plt_entry;
11057
0
    bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
11058
0
    bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
11059
0
          loc + 4);
11060
11061
0
    if (! LOAD_INTERLOCKS_P (output_bfd)
11062
0
        || (MIPSR6_P (output_bfd) && htab->compact_branches))
11063
0
      {
11064
0
        bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
11065
0
        bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11066
0
      }
11067
0
    else
11068
0
      {
11069
0
        bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
11070
0
        bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
11071
0
        loc + 12);
11072
0
      }
11073
0
  }
11074
11075
      /* Now the compressed entry.  They come after any standard ones.  */
11076
0
      if (h->plt.plist->comp_offset != MINUS_ONE)
11077
0
  {
11078
0
    bfd_vma plt_offset;
11079
11080
0
    plt_offset = (htab->plt_header_size + htab->plt_mips_offset
11081
0
      + h->plt.plist->comp_offset);
11082
11083
0
    BFD_ASSERT (plt_offset <= htab->root.splt->size);
11084
11085
    /* Find out where the .plt entry should go.  */
11086
0
    loc = htab->root.splt->contents + plt_offset;
11087
11088
    /* Fill in the PLT entry itself.  */
11089
0
    if (!MICROMIPS_P (output_bfd))
11090
0
      {
11091
0
        const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
11092
11093
0
        bfd_put_16 (output_bfd, plt_entry[0], loc);
11094
0
        bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
11095
0
        bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11096
0
        bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11097
0
        bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11098
0
        bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11099
0
        bfd_put_32 (output_bfd, got_address, loc + 12);
11100
0
      }
11101
0
    else if (htab->insn32)
11102
0
      {
11103
0
        const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
11104
11105
0
        bfd_put_16 (output_bfd, plt_entry[0], loc);
11106
0
        bfd_put_16 (output_bfd, got_address_high, loc + 2);
11107
0
        bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11108
0
        bfd_put_16 (output_bfd, got_address_low, loc + 6);
11109
0
        bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11110
0
        bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11111
0
        bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
11112
0
        bfd_put_16 (output_bfd, got_address_low, loc + 14);
11113
0
      }
11114
0
    else
11115
0
      {
11116
0
        const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
11117
0
        bfd_signed_vma gotpc_offset;
11118
0
        bfd_vma loc_address;
11119
11120
0
        BFD_ASSERT (got_address % 4 == 0);
11121
11122
0
        loc_address = (htab->root.splt->output_section->vma
11123
0
           + htab->root.splt->output_offset + plt_offset);
11124
0
        gotpc_offset = got_address - ((loc_address | 3) ^ 3);
11125
11126
        /* ADDIUPC has a span of +/-16MB, check we're in range.  */
11127
0
        if (gotpc_offset + 0x1000000 >= 0x2000000)
11128
0
    {
11129
0
      _bfd_error_handler
11130
        /* xgettext:c-format */
11131
0
        (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11132
0
           "beyond the range of ADDIUPC"),
11133
0
         output_bfd,
11134
0
         htab->root.sgotplt->output_section,
11135
0
         (int64_t) gotpc_offset,
11136
0
         htab->root.splt->output_section);
11137
0
      bfd_set_error (bfd_error_no_error);
11138
0
      return false;
11139
0
    }
11140
0
        bfd_put_16 (output_bfd,
11141
0
        plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11142
0
        bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11143
0
        bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11144
0
        bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11145
0
        bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11146
0
        bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11147
0
      }
11148
0
  }
11149
11150
      /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
11151
0
      mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
11152
0
            got_index - 2, h->dynindx,
11153
0
            R_MIPS_JUMP_SLOT, got_address);
11154
11155
      /* We distinguish between PLT entries and lazy-binding stubs by
11156
   giving the former an st_other value of STO_MIPS_PLT.  Set the
11157
   flag and leave the value if there are any relocations in the
11158
   binary where pointer equality matters.  */
11159
0
      sym->st_shndx = SHN_UNDEF;
11160
0
      if (h->pointer_equality_needed)
11161
0
  sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
11162
0
      else
11163
0
  {
11164
0
    sym->st_value = 0;
11165
0
    sym->st_other = 0;
11166
0
  }
11167
0
    }
11168
11169
0
  if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
11170
0
    {
11171
      /* We've decided to create a lazy-binding stub.  */
11172
0
      bool micromips_p = MICROMIPS_P (output_bfd);
11173
0
      unsigned int other = micromips_p ? STO_MICROMIPS : 0;
11174
0
      bfd_vma stub_size = htab->function_stub_size;
11175
0
      bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
11176
0
      bfd_vma isa_bit = micromips_p;
11177
0
      bfd_vma stub_big_size;
11178
11179
0
      if (!micromips_p)
11180
0
  stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
11181
0
      else if (htab->insn32)
11182
0
  stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
11183
0
      else
11184
0
  stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
11185
11186
      /* This symbol has a stub.  Set it up.  */
11187
11188
0
      BFD_ASSERT (h->dynindx != -1);
11189
11190
0
      BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
11191
11192
      /* Values up to 2^31 - 1 are allowed.  Larger values would cause
11193
   sign extension at runtime in the stub, resulting in a negative
11194
   index value.  */
11195
0
      if (h->dynindx & ~0x7fffffff)
11196
0
  {
11197
0
    _bfd_error_handler
11198
0
      (_("%pB: cannot handle more than %d dynamic symbols"),
11199
0
       output_bfd, 0x7fffffff);
11200
0
    bfd_set_error (bfd_error_bad_value);
11201
0
    return false;
11202
0
  }
11203
11204
      /* Fill the stub.  */
11205
0
      if (micromips_p)
11206
0
  {
11207
0
    idx = 0;
11208
0
    bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
11209
0
        stub + idx);
11210
0
    idx += 4;
11211
0
    if (htab->insn32)
11212
0
      {
11213
0
        bfd_put_micromips_32 (output_bfd,
11214
0
            STUB_MOVE32_MICROMIPS, stub + idx);
11215
0
        idx += 4;
11216
0
      }
11217
0
    else
11218
0
      {
11219
0
        bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
11220
0
        idx += 2;
11221
0
      }
11222
0
    if (stub_size == stub_big_size)
11223
0
      {
11224
0
        long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
11225
11226
0
        bfd_put_micromips_32 (output_bfd,
11227
0
            STUB_LUI_MICROMIPS (dynindx_hi),
11228
0
            stub + idx);
11229
0
        idx += 4;
11230
0
      }
11231
0
    if (htab->insn32)
11232
0
      {
11233
0
        bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
11234
0
            stub + idx);
11235
0
        idx += 4;
11236
0
      }
11237
0
    else
11238
0
      {
11239
0
        bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
11240
0
        idx += 2;
11241
0
      }
11242
11243
    /* If a large stub is not required and sign extension is not a
11244
       problem, then use legacy code in the stub.  */
11245
0
    if (stub_size == stub_big_size)
11246
0
      bfd_put_micromips_32 (output_bfd,
11247
0
          STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
11248
0
          stub + idx);
11249
0
    else if (h->dynindx & ~0x7fff)
11250
0
      bfd_put_micromips_32 (output_bfd,
11251
0
          STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
11252
0
          stub + idx);
11253
0
    else
11254
0
      bfd_put_micromips_32 (output_bfd,
11255
0
          STUB_LI16S_MICROMIPS (output_bfd,
11256
0
              h->dynindx),
11257
0
          stub + idx);
11258
0
  }
11259
0
      else
11260
0
  {
11261
0
    idx = 0;
11262
0
    bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
11263
0
    idx += 4;
11264
0
    bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
11265
0
    idx += 4;
11266
0
    if (stub_size == stub_big_size)
11267
0
      {
11268
0
        bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
11269
0
        stub + idx);
11270
0
        idx += 4;
11271
0
      }
11272
11273
0
    if (!(MIPSR6_P (output_bfd) && htab->compact_branches))
11274
0
      {
11275
0
        bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
11276
0
        idx += 4;
11277
0
      }
11278
11279
    /* If a large stub is not required and sign extension is not a
11280
       problem, then use legacy code in the stub.  */
11281
0
    if (stub_size == stub_big_size)
11282
0
      bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
11283
0
      stub + idx);
11284
0
    else if (h->dynindx & ~0x7fff)
11285
0
      bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
11286
0
      stub + idx);
11287
0
    else
11288
0
      bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
11289
0
      stub + idx);
11290
0
    idx += 4;
11291
11292
0
    if (MIPSR6_P (output_bfd) && htab->compact_branches)
11293
0
      bfd_put_32 (output_bfd, STUB_JALRC, stub + idx);
11294
0
  }
11295
11296
0
      BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
11297
0
      memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
11298
0
        stub, stub_size);
11299
11300
      /* Mark the symbol as undefined.  stub_offset != -1 occurs
11301
   only for the referenced symbol.  */
11302
0
      sym->st_shndx = SHN_UNDEF;
11303
11304
      /* The run-time linker uses the st_value field of the symbol
11305
   to reset the global offset table entry for this external
11306
   to its stub address when unlinking a shared object.  */
11307
0
      sym->st_value = (htab->sstubs->output_section->vma
11308
0
           + htab->sstubs->output_offset
11309
0
           + h->plt.plist->stub_offset
11310
0
           + isa_bit);
11311
0
      sym->st_other = other;
11312
0
    }
11313
11314
  /* If we have a MIPS16 function with a stub, the dynamic symbol must
11315
     refer to the stub, since only the stub uses the standard calling
11316
     conventions.  */
11317
0
  if (h->dynindx != -1 && hmips->fn_stub != NULL)
11318
0
    {
11319
0
      BFD_ASSERT (hmips->need_fn_stub);
11320
0
      sym->st_value = (hmips->fn_stub->output_section->vma
11321
0
           + hmips->fn_stub->output_offset);
11322
0
      sym->st_size = hmips->fn_stub->size;
11323
0
      sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
11324
0
    }
11325
11326
0
  BFD_ASSERT (h->dynindx != -1
11327
0
        || h->forced_local);
11328
11329
0
  sgot = htab->root.sgot;
11330
0
  g = htab->got_info;
11331
0
  BFD_ASSERT (g != NULL);
11332
11333
  /* Run through the global symbol table, creating GOT entries for all
11334
     the symbols that need them.  */
11335
0
  if (hmips->global_got_area != GGA_NONE)
11336
0
    {
11337
0
      bfd_vma offset;
11338
0
      bfd_vma value;
11339
11340
0
      value = sym->st_value;
11341
0
      offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11342
0
      MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
11343
0
    }
11344
11345
0
  if (hmips->global_got_area != GGA_NONE && g->next)
11346
0
    {
11347
0
      struct mips_got_entry e, *p;
11348
0
      bfd_vma entry;
11349
0
      bfd_vma offset;
11350
11351
0
      gg = g;
11352
11353
0
      e.abfd = output_bfd;
11354
0
      e.symndx = -1;
11355
0
      e.d.h = hmips;
11356
0
      e.tls_type = GOT_TLS_NONE;
11357
11358
0
      for (g = g->next; g->next != gg; g = g->next)
11359
0
  {
11360
0
    if (g->got_entries
11361
0
        && (p = (struct mips_got_entry *) htab_find (g->got_entries,
11362
0
                 &e)))
11363
0
      {
11364
0
        offset = p->gotidx;
11365
0
        BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
11366
0
        if (bfd_link_pic (info)
11367
0
      || (elf_hash_table (info)->dynamic_sections_created
11368
0
          && p->d.h != NULL
11369
0
          && p->d.h->root.def_dynamic
11370
0
          && !p->d.h->root.def_regular))
11371
0
    {
11372
      /* Create an R_MIPS_REL32 relocation for this entry.  Due to
11373
         the various compatibility problems, it's easier to mock
11374
         up an R_MIPS_32 or R_MIPS_64 relocation and leave
11375
         mips_elf_create_dynamic_relocation to calculate the
11376
         appropriate addend.  */
11377
0
      Elf_Internal_Rela rel[3];
11378
11379
0
      memset (rel, 0, sizeof (rel));
11380
0
      if (ABI_64_P (output_bfd))
11381
0
        rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
11382
0
      else
11383
0
        rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
11384
0
      rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
11385
11386
0
      entry = 0;
11387
0
      if (! (mips_elf_create_dynamic_relocation
11388
0
       (output_bfd, info, rel,
11389
0
        e.d.h, NULL, sym->st_value, &entry, sgot)))
11390
0
        return false;
11391
0
    }
11392
0
        else
11393
0
    entry = sym->st_value;
11394
0
        MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
11395
0
      }
11396
0
  }
11397
0
    }
11398
11399
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
11400
0
  name = h->root.root.string;
11401
0
  if (h == elf_hash_table (info)->hdynamic
11402
0
      || h == elf_hash_table (info)->hgot)
11403
0
    sym->st_shndx = SHN_ABS;
11404
0
  else if (strcmp (name, "_DYNAMIC_LINK") == 0
11405
0
     || strcmp (name, "_DYNAMIC_LINKING") == 0)
11406
0
    {
11407
0
      sym->st_shndx = SHN_ABS;
11408
0
      sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11409
0
      sym->st_value = 1;
11410
0
    }
11411
0
  else if (SGI_COMPAT (output_bfd))
11412
0
    {
11413
0
      if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
11414
0
    || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
11415
0
  {
11416
0
    sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11417
0
    sym->st_other = STO_PROTECTED;
11418
0
    sym->st_value = 0;
11419
0
    sym->st_shndx = SHN_MIPS_DATA;
11420
0
  }
11421
0
      else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
11422
0
  {
11423
0
    sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11424
0
    sym->st_other = STO_PROTECTED;
11425
0
    sym->st_value = mips_elf_hash_table (info)->procedure_count;
11426
0
    sym->st_shndx = SHN_ABS;
11427
0
  }
11428
0
      else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
11429
0
  {
11430
0
    if (h->type == STT_FUNC)
11431
0
      sym->st_shndx = SHN_MIPS_TEXT;
11432
0
    else if (h->type == STT_OBJECT)
11433
0
      sym->st_shndx = SHN_MIPS_DATA;
11434
0
  }
11435
0
    }
11436
11437
  /* Emit a copy reloc, if needed.  */
11438
0
  if (h->needs_copy)
11439
0
    {
11440
0
      asection *s;
11441
0
      bfd_vma symval;
11442
11443
0
      BFD_ASSERT (h->dynindx != -1);
11444
0
      BFD_ASSERT (htab->use_plts_and_copy_relocs);
11445
11446
0
      s = mips_elf_rel_dyn_section (info, false);
11447
0
      symval = (h->root.u.def.section->output_section->vma
11448
0
    + h->root.u.def.section->output_offset
11449
0
    + h->root.u.def.value);
11450
0
      mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
11451
0
            h->dynindx, R_MIPS_COPY, symval);
11452
0
    }
11453
11454
  /* Handle the IRIX6-specific symbols.  */
11455
0
  if (IRIX_COMPAT (output_bfd) == ict_irix6)
11456
0
    mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
11457
11458
  /* Keep dynamic compressed symbols odd.  This allows the dynamic linker
11459
     to treat compressed symbols like any other.  */
11460
0
  if (ELF_ST_IS_MIPS16 (sym->st_other))
11461
0
    {
11462
0
      BFD_ASSERT (sym->st_value & 1);
11463
0
      sym->st_other -= STO_MIPS16;
11464
0
    }
11465
0
  else if (ELF_ST_IS_MICROMIPS (sym->st_other))
11466
0
    {
11467
0
      BFD_ASSERT (sym->st_value & 1);
11468
0
      sym->st_other -= STO_MICROMIPS;
11469
0
    }
11470
11471
0
  return true;
11472
0
}
11473
11474
/* Likewise, for VxWorks.  */
11475
11476
bool
11477
_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
11478
           struct bfd_link_info *info,
11479
           struct elf_link_hash_entry *h,
11480
           Elf_Internal_Sym *sym)
11481
0
{
11482
0
  bfd *dynobj;
11483
0
  asection *sgot;
11484
0
  struct mips_got_info *g;
11485
0
  struct mips_elf_link_hash_table *htab;
11486
0
  struct mips_elf_link_hash_entry *hmips;
11487
11488
0
  htab = mips_elf_hash_table (info);
11489
0
  BFD_ASSERT (htab != NULL);
11490
0
  dynobj = elf_hash_table (info)->dynobj;
11491
0
  hmips = (struct mips_elf_link_hash_entry *) h;
11492
11493
0
  if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
11494
0
    {
11495
0
      bfd_byte *loc;
11496
0
      bfd_vma plt_address, got_address, got_offset, branch_offset;
11497
0
      Elf_Internal_Rela rel;
11498
0
      static const bfd_vma *plt_entry;
11499
0
      bfd_vma gotplt_index;
11500
0
      bfd_vma plt_offset;
11501
11502
0
      plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11503
0
      gotplt_index = h->plt.plist->gotplt_index;
11504
11505
0
      BFD_ASSERT (h->dynindx != -1);
11506
0
      BFD_ASSERT (htab->root.splt != NULL);
11507
0
      BFD_ASSERT (gotplt_index != MINUS_ONE);
11508
0
      BFD_ASSERT (plt_offset <= htab->root.splt->size);
11509
11510
      /* Calculate the address of the .plt entry.  */
11511
0
      plt_address = (htab->root.splt->output_section->vma
11512
0
         + htab->root.splt->output_offset
11513
0
         + plt_offset);
11514
11515
      /* Calculate the address of the .got.plt entry.  */
11516
0
      got_address = (htab->root.sgotplt->output_section->vma
11517
0
         + htab->root.sgotplt->output_offset
11518
0
         + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
11519
11520
      /* Calculate the offset of the .got.plt entry from
11521
   _GLOBAL_OFFSET_TABLE_.  */
11522
0
      got_offset = mips_elf_gotplt_index (info, h);
11523
11524
      /* Calculate the offset for the branch at the start of the PLT
11525
   entry.  The branch jumps to the beginning of .plt.  */
11526
0
      branch_offset = -(plt_offset / 4 + 1) & 0xffff;
11527
11528
      /* Fill in the initial value of the .got.plt entry.  */
11529
0
      bfd_put_32 (output_bfd, plt_address,
11530
0
      (htab->root.sgotplt->contents
11531
0
       + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
11532
11533
      /* Find out where the .plt entry should go.  */
11534
0
      loc = htab->root.splt->contents + plt_offset;
11535
11536
0
      if (bfd_link_pic (info))
11537
0
  {
11538
0
    plt_entry = mips_vxworks_shared_plt_entry;
11539
0
    bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11540
0
    bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11541
0
  }
11542
0
      else
11543
0
  {
11544
0
    bfd_vma got_address_high, got_address_low;
11545
11546
0
    plt_entry = mips_vxworks_exec_plt_entry;
11547
0
    got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11548
0
    got_address_low = got_address & 0xffff;
11549
11550
0
    bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11551
0
    bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11552
0
    bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
11553
0
    bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
11554
0
    bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11555
0
    bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11556
0
    bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11557
0
    bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11558
11559
0
    loc = (htab->srelplt2->contents
11560
0
     + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
11561
11562
    /* Emit a relocation for the .got.plt entry.  */
11563
0
    rel.r_offset = got_address;
11564
0
    rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11565
0
    rel.r_addend = plt_offset;
11566
0
    bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11567
11568
    /* Emit a relocation for the lui of %hi(<.got.plt slot>).  */
11569
0
    loc += sizeof (Elf32_External_Rela);
11570
0
    rel.r_offset = plt_address + 8;
11571
0
    rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11572
0
    rel.r_addend = got_offset;
11573
0
    bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11574
11575
    /* Emit a relocation for the addiu of %lo(<.got.plt slot>).  */
11576
0
    loc += sizeof (Elf32_External_Rela);
11577
0
    rel.r_offset += 4;
11578
0
    rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11579
0
    bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11580
0
  }
11581
11582
      /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry.  */
11583
0
      loc = (htab->root.srelplt->contents
11584
0
       + gotplt_index * sizeof (Elf32_External_Rela));
11585
0
      rel.r_offset = got_address;
11586
0
      rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
11587
0
      rel.r_addend = 0;
11588
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11589
11590
0
      if (!h->def_regular)
11591
0
  sym->st_shndx = SHN_UNDEF;
11592
0
    }
11593
11594
0
  BFD_ASSERT (h->dynindx != -1 || h->forced_local);
11595
11596
0
  sgot = htab->root.sgot;
11597
0
  g = htab->got_info;
11598
0
  BFD_ASSERT (g != NULL);
11599
11600
  /* See if this symbol has an entry in the GOT.  */
11601
0
  if (hmips->global_got_area != GGA_NONE)
11602
0
    {
11603
0
      bfd_vma offset;
11604
0
      Elf_Internal_Rela outrel;
11605
0
      bfd_byte *loc;
11606
0
      asection *s;
11607
11608
      /* Install the symbol value in the GOT.   */
11609
0
      offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11610
0
      MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
11611
11612
      /* Add a dynamic relocation for it.  */
11613
0
      s = mips_elf_rel_dyn_section (info, false);
11614
0
      loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
11615
0
      outrel.r_offset = (sgot->output_section->vma
11616
0
       + sgot->output_offset
11617
0
       + offset);
11618
0
      outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
11619
0
      outrel.r_addend = 0;
11620
0
      bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
11621
0
    }
11622
11623
  /* Emit a copy reloc, if needed.  */
11624
0
  if (h->needs_copy)
11625
0
    {
11626
0
      Elf_Internal_Rela rel;
11627
0
      asection *srel;
11628
0
      bfd_byte *loc;
11629
11630
0
      BFD_ASSERT (h->dynindx != -1);
11631
11632
0
      rel.r_offset = (h->root.u.def.section->output_section->vma
11633
0
          + h->root.u.def.section->output_offset
11634
0
          + h->root.u.def.value);
11635
0
      rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
11636
0
      rel.r_addend = 0;
11637
0
      if (h->root.u.def.section == htab->root.sdynrelro)
11638
0
  srel = htab->root.sreldynrelro;
11639
0
      else
11640
0
  srel = htab->root.srelbss;
11641
0
      loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
11642
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11643
0
      ++srel->reloc_count;
11644
0
    }
11645
11646
  /* If this is a mips16/microMIPS symbol, force the value to be even.  */
11647
0
  if (ELF_ST_IS_COMPRESSED (sym->st_other))
11648
0
    sym->st_value &= ~1;
11649
11650
0
  return true;
11651
0
}
11652
11653
/* Write out a plt0 entry to the beginning of .plt.  */
11654
11655
static bool
11656
mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11657
0
{
11658
0
  bfd_byte *loc;
11659
0
  bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11660
0
  static const bfd_vma *plt_entry;
11661
0
  struct mips_elf_link_hash_table *htab;
11662
11663
0
  htab = mips_elf_hash_table (info);
11664
0
  BFD_ASSERT (htab != NULL);
11665
11666
0
  if (ABI_64_P (output_bfd))
11667
0
    plt_entry = (htab->compact_branches
11668
0
     ? mipsr6_n64_exec_plt0_entry_compact
11669
0
     : mips_n64_exec_plt0_entry);
11670
0
  else if (ABI_N32_P (output_bfd))
11671
0
    plt_entry = (htab->compact_branches
11672
0
     ? mipsr6_n32_exec_plt0_entry_compact
11673
0
     : mips_n32_exec_plt0_entry);
11674
0
  else if (!htab->plt_header_is_comp)
11675
0
    plt_entry = (htab->compact_branches
11676
0
     ? mipsr6_o32_exec_plt0_entry_compact
11677
0
     : mips_o32_exec_plt0_entry);
11678
0
  else if (htab->insn32)
11679
0
    plt_entry = micromips_insn32_o32_exec_plt0_entry;
11680
0
  else
11681
0
    plt_entry = micromips_o32_exec_plt0_entry;
11682
11683
  /* Calculate the value of .got.plt.  */
11684
0
  gotplt_value = (htab->root.sgotplt->output_section->vma
11685
0
      + htab->root.sgotplt->output_offset);
11686
0
  gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11687
0
  gotplt_value_low = gotplt_value & 0xffff;
11688
11689
  /* The PLT sequence is not safe for N64 if .got.plt's address can
11690
     not be loaded in two instructions.  */
11691
0
  if (ABI_64_P (output_bfd)
11692
0
      && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11693
0
    {
11694
0
      _bfd_error_handler
11695
  /* xgettext:c-format */
11696
0
  (_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
11697
0
     "supported; consider using `-Ttext-segment=...'"),
11698
0
   output_bfd,
11699
0
   htab->root.sgotplt->output_section,
11700
0
   (int64_t) gotplt_value);
11701
0
      bfd_set_error (bfd_error_no_error);
11702
0
      return false;
11703
0
    }
11704
11705
  /* Install the PLT header.  */
11706
0
  loc = htab->root.splt->contents;
11707
0
  if (plt_entry == micromips_o32_exec_plt0_entry)
11708
0
    {
11709
0
      bfd_vma gotpc_offset;
11710
0
      bfd_vma loc_address;
11711
0
      size_t i;
11712
11713
0
      BFD_ASSERT (gotplt_value % 4 == 0);
11714
11715
0
      loc_address = (htab->root.splt->output_section->vma
11716
0
         + htab->root.splt->output_offset);
11717
0
      gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11718
11719
      /* ADDIUPC has a span of +/-16MB, check we're in range.  */
11720
0
      if (gotpc_offset + 0x1000000 >= 0x2000000)
11721
0
  {
11722
0
    _bfd_error_handler
11723
      /* xgettext:c-format */
11724
0
      (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11725
0
         "beyond the range of ADDIUPC"),
11726
0
       output_bfd,
11727
0
       htab->root.sgotplt->output_section,
11728
0
       (int64_t) gotpc_offset,
11729
0
       htab->root.splt->output_section);
11730
0
    bfd_set_error (bfd_error_no_error);
11731
0
    return false;
11732
0
  }
11733
0
      bfd_put_16 (output_bfd,
11734
0
      plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11735
0
      bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11736
0
      for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11737
0
  bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11738
0
    }
11739
0
  else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11740
0
    {
11741
0
      size_t i;
11742
11743
0
      bfd_put_16 (output_bfd, plt_entry[0], loc);
11744
0
      bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11745
0
      bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11746
0
      bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11747
0
      bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11748
0
      bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11749
0
      for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11750
0
  bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11751
0
    }
11752
0
  else
11753
0
    {
11754
0
      bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11755
0
      bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11756
0
      bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11757
0
      bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11758
0
      bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11759
0
      bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11760
0
      bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11761
0
      bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11762
0
    }
11763
11764
0
  return true;
11765
0
}
11766
11767
/* Install the PLT header for a VxWorks executable and finalize the
11768
   contents of .rela.plt.unloaded.  */
11769
11770
static void
11771
mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11772
0
{
11773
0
  Elf_Internal_Rela rela;
11774
0
  bfd_byte *loc;
11775
0
  bfd_vma got_value, got_value_high, got_value_low, plt_address;
11776
0
  static const bfd_vma *plt_entry;
11777
0
  struct mips_elf_link_hash_table *htab;
11778
11779
0
  htab = mips_elf_hash_table (info);
11780
0
  BFD_ASSERT (htab != NULL);
11781
11782
0
  plt_entry = mips_vxworks_exec_plt0_entry;
11783
11784
  /* Calculate the value of _GLOBAL_OFFSET_TABLE_.  */
11785
0
  got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11786
0
         + htab->root.hgot->root.u.def.section->output_offset
11787
0
         + htab->root.hgot->root.u.def.value);
11788
11789
0
  got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11790
0
  got_value_low = got_value & 0xffff;
11791
11792
  /* Calculate the address of the PLT header.  */
11793
0
  plt_address = (htab->root.splt->output_section->vma
11794
0
     + htab->root.splt->output_offset);
11795
11796
  /* Install the PLT header.  */
11797
0
  loc = htab->root.splt->contents;
11798
0
  bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11799
0
  bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11800
0
  bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11801
0
  bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11802
0
  bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11803
0
  bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11804
11805
  /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_).  */
11806
0
  loc = htab->srelplt2->contents;
11807
0
  rela.r_offset = plt_address;
11808
0
  rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11809
0
  rela.r_addend = 0;
11810
0
  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11811
0
  loc += sizeof (Elf32_External_Rela);
11812
11813
  /* Output the relocation for the following addiu of
11814
     %lo(_GLOBAL_OFFSET_TABLE_).  */
11815
0
  rela.r_offset += 4;
11816
0
  rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11817
0
  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11818
0
  loc += sizeof (Elf32_External_Rela);
11819
11820
  /* Fix up the remaining relocations.  They may have the wrong
11821
     symbol index for _G_O_T_ or _P_L_T_ depending on the order
11822
     in which symbols were output.  */
11823
0
  while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11824
0
    {
11825
0
      Elf_Internal_Rela rel;
11826
11827
0
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11828
0
      rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11829
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11830
0
      loc += sizeof (Elf32_External_Rela);
11831
11832
0
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11833
0
      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11834
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11835
0
      loc += sizeof (Elf32_External_Rela);
11836
11837
0
      bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11838
0
      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11839
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11840
0
      loc += sizeof (Elf32_External_Rela);
11841
0
    }
11842
0
}
11843
11844
/* Install the PLT header for a VxWorks shared library.  */
11845
11846
static void
11847
mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11848
0
{
11849
0
  unsigned int i;
11850
0
  struct mips_elf_link_hash_table *htab;
11851
11852
0
  htab = mips_elf_hash_table (info);
11853
0
  BFD_ASSERT (htab != NULL);
11854
11855
  /* We just need to copy the entry byte-by-byte.  */
11856
0
  for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11857
0
    bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11858
0
    htab->root.splt->contents + i * 4);
11859
0
}
11860
11861
/* Finish up the dynamic sections.  */
11862
11863
bool
11864
_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11865
               struct bfd_link_info *info)
11866
0
{
11867
0
  bfd *dynobj;
11868
0
  asection *sdyn;
11869
0
  asection *sgot;
11870
0
  struct mips_got_info *gg, *g;
11871
0
  struct mips_elf_link_hash_table *htab;
11872
11873
0
  htab = mips_elf_hash_table (info);
11874
0
  BFD_ASSERT (htab != NULL);
11875
11876
0
  dynobj = elf_hash_table (info)->dynobj;
11877
11878
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11879
11880
0
  sgot = htab->root.sgot;
11881
0
  gg = htab->got_info;
11882
11883
0
  if (elf_hash_table (info)->dynamic_sections_created)
11884
0
    {
11885
0
      bfd_byte *b;
11886
0
      int dyn_to_skip = 0, dyn_skipped = 0;
11887
11888
0
      BFD_ASSERT (sdyn != NULL);
11889
0
      BFD_ASSERT (gg != NULL);
11890
11891
0
      g = mips_elf_bfd_got (output_bfd, false);
11892
0
      BFD_ASSERT (g != NULL);
11893
11894
0
      for (b = sdyn->contents;
11895
0
     b < sdyn->contents + sdyn->size;
11896
0
     b += MIPS_ELF_DYN_SIZE (dynobj))
11897
0
  {
11898
0
    Elf_Internal_Dyn dyn;
11899
0
    const char *name;
11900
0
    size_t elemsize;
11901
0
    asection *s;
11902
0
    bool swap_out_p;
11903
11904
    /* Read in the current dynamic entry.  */
11905
0
    (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11906
11907
    /* Assume that we're going to modify it and write it out.  */
11908
0
    swap_out_p = true;
11909
11910
0
    switch (dyn.d_tag)
11911
0
      {
11912
0
      case DT_RELENT:
11913
0
        dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11914
0
        break;
11915
11916
0
      case DT_RELAENT:
11917
0
        BFD_ASSERT (htab->root.target_os == is_vxworks);
11918
0
        dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11919
0
        break;
11920
11921
0
      case DT_STRSZ:
11922
        /* Rewrite DT_STRSZ.  */
11923
0
        dyn.d_un.d_val =
11924
0
    _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11925
0
        break;
11926
11927
0
      case DT_PLTGOT:
11928
0
        s = htab->root.sgot;
11929
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11930
0
        break;
11931
11932
0
      case DT_MIPS_PLTGOT:
11933
0
        s = htab->root.sgotplt;
11934
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11935
0
        break;
11936
11937
0
      case DT_MIPS_RLD_VERSION:
11938
0
        dyn.d_un.d_val = 1; /* XXX */
11939
0
        break;
11940
11941
0
      case DT_MIPS_FLAGS:
11942
0
        dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11943
0
        break;
11944
11945
0
      case DT_MIPS_TIME_STAMP:
11946
0
        {
11947
0
    time_t t;
11948
0
    time (&t);
11949
0
    dyn.d_un.d_val = t;
11950
0
        }
11951
0
        break;
11952
11953
0
      case DT_MIPS_ICHECKSUM:
11954
        /* XXX FIXME: */
11955
0
        swap_out_p = false;
11956
0
        break;
11957
11958
0
      case DT_MIPS_IVERSION:
11959
        /* XXX FIXME: */
11960
0
        swap_out_p = false;
11961
0
        break;
11962
11963
0
      case DT_MIPS_BASE_ADDRESS:
11964
0
        s = output_bfd->sections;
11965
0
        BFD_ASSERT (s != NULL);
11966
0
        dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11967
0
        break;
11968
11969
0
      case DT_MIPS_LOCAL_GOTNO:
11970
0
        dyn.d_un.d_val = g->local_gotno;
11971
0
        break;
11972
11973
0
      case DT_MIPS_UNREFEXTNO:
11974
        /* The index into the dynamic symbol table which is the
11975
     entry of the first external symbol that is not
11976
     referenced within the same object.  */
11977
0
        dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11978
0
        break;
11979
11980
0
      case DT_MIPS_GOTSYM:
11981
0
        if (htab->global_gotsym)
11982
0
    {
11983
0
      dyn.d_un.d_val = htab->global_gotsym->dynindx;
11984
0
      break;
11985
0
    }
11986
        /* In case if we don't have global got symbols we default
11987
     to setting DT_MIPS_GOTSYM to the same value as
11988
     DT_MIPS_SYMTABNO.  */
11989
        /* Fall through.  */
11990
11991
0
      case DT_MIPS_SYMTABNO:
11992
0
        name = ".dynsym";
11993
0
        elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11994
0
        s = bfd_get_linker_section (dynobj, name);
11995
11996
0
        if (s != NULL)
11997
0
    dyn.d_un.d_val = s->size / elemsize;
11998
0
        else
11999
0
    dyn.d_un.d_val = 0;
12000
0
        break;
12001
12002
0
      case DT_MIPS_HIPAGENO:
12003
0
        dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
12004
0
        break;
12005
12006
0
      case DT_MIPS_RLD_MAP:
12007
0
        {
12008
0
    struct elf_link_hash_entry *h;
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 tag stores the absolute address of the
12019
       debug pointer.  */
12020
0
    dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
12021
0
          + h->root.u.def.value);
12022
0
        }
12023
0
        break;
12024
12025
0
      case DT_MIPS_RLD_MAP_REL:
12026
0
        {
12027
0
    struct elf_link_hash_entry *h;
12028
0
    bfd_vma dt_addr, rld_addr;
12029
0
    h = mips_elf_hash_table (info)->rld_symbol;
12030
0
    if (!h)
12031
0
      {
12032
0
        dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12033
0
        swap_out_p = false;
12034
0
        break;
12035
0
      }
12036
0
    s = h->root.u.def.section;
12037
12038
    /* The MIPS_RLD_MAP_REL tag stores the offset to the debug
12039
       pointer, relative to the address of the tag.  */
12040
0
    dt_addr = (sdyn->output_section->vma + sdyn->output_offset
12041
0
         + (b - sdyn->contents));
12042
0
    rld_addr = (s->output_section->vma + s->output_offset
12043
0
          + h->root.u.def.value);
12044
0
    dyn.d_un.d_ptr = rld_addr - dt_addr;
12045
0
        }
12046
0
        break;
12047
12048
0
      case DT_MIPS_OPTIONS:
12049
0
        s = (bfd_get_section_by_name
12050
0
       (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
12051
0
        dyn.d_un.d_ptr = s->vma;
12052
0
        break;
12053
12054
0
      case DT_PLTREL:
12055
0
        BFD_ASSERT (htab->use_plts_and_copy_relocs);
12056
0
        if (htab->root.target_os == is_vxworks)
12057
0
    dyn.d_un.d_val = DT_RELA;
12058
0
        else
12059
0
    dyn.d_un.d_val = DT_REL;
12060
0
        break;
12061
12062
0
      case DT_PLTRELSZ:
12063
0
        BFD_ASSERT (htab->use_plts_and_copy_relocs);
12064
0
        dyn.d_un.d_val = htab->root.srelplt->size;
12065
0
        break;
12066
12067
0
      case DT_JMPREL:
12068
0
        BFD_ASSERT (htab->use_plts_and_copy_relocs);
12069
0
        dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
12070
0
        + htab->root.srelplt->output_offset);
12071
0
        break;
12072
12073
0
      case DT_TEXTREL:
12074
        /* If we didn't need any text relocations after all, delete
12075
     the dynamic tag.  */
12076
0
        if (!(info->flags & DF_TEXTREL))
12077
0
    {
12078
0
      dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12079
0
      swap_out_p = false;
12080
0
    }
12081
0
        break;
12082
12083
0
      case DT_FLAGS:
12084
        /* If we didn't need any text relocations after all, clear
12085
     DF_TEXTREL from DT_FLAGS.  */
12086
0
        if (!(info->flags & DF_TEXTREL))
12087
0
    dyn.d_un.d_val &= ~DF_TEXTREL;
12088
0
        else
12089
0
    swap_out_p = false;
12090
0
        break;
12091
12092
0
      case DT_MIPS_XHASH:
12093
0
        name = ".MIPS.xhash";
12094
0
        s = bfd_get_linker_section (dynobj, name);
12095
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
12096
0
        break;
12097
12098
0
      default:
12099
0
        swap_out_p = false;
12100
0
        if (htab->root.target_os == is_vxworks
12101
0
      && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
12102
0
    swap_out_p = true;
12103
0
        break;
12104
0
      }
12105
12106
0
    if (swap_out_p || dyn_skipped)
12107
0
      (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12108
0
        (dynobj, &dyn, b - dyn_skipped);
12109
12110
0
    if (dyn_to_skip)
12111
0
      {
12112
0
        dyn_skipped += dyn_to_skip;
12113
0
        dyn_to_skip = 0;
12114
0
      }
12115
0
  }
12116
12117
      /* Wipe out any trailing entries if we shifted down a dynamic tag.  */
12118
0
      if (dyn_skipped > 0)
12119
0
  memset (b - dyn_skipped, 0, dyn_skipped);
12120
0
    }
12121
12122
0
  if (sgot != NULL && sgot->size > 0
12123
0
      && !bfd_is_abs_section (sgot->output_section))
12124
0
    {
12125
0
      if (htab->root.target_os == is_vxworks)
12126
0
  {
12127
    /* The first entry of the global offset table points to the
12128
       ".dynamic" section.  The second is initialized by the
12129
       loader and contains the shared library identifier.
12130
       The third is also initialized by the loader and points
12131
       to the lazy resolution stub.  */
12132
0
    MIPS_ELF_PUT_WORD (output_bfd,
12133
0
           sdyn->output_offset + sdyn->output_section->vma,
12134
0
           sgot->contents);
12135
0
    MIPS_ELF_PUT_WORD (output_bfd, 0,
12136
0
           sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12137
0
    MIPS_ELF_PUT_WORD (output_bfd, 0,
12138
0
           sgot->contents
12139
0
           + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
12140
0
  }
12141
0
      else
12142
0
  {
12143
    /* The first entry of the global offset table will be filled at
12144
       runtime. The second entry will be used by some runtime loaders.
12145
       This isn't the case of IRIX rld.  */
12146
0
    MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
12147
0
    MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12148
0
           sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12149
0
  }
12150
12151
0
      elf_section_data (sgot->output_section)->this_hdr.sh_entsize
12152
0
   = MIPS_ELF_GOT_SIZE (output_bfd);
12153
0
    }
12154
12155
  /* Generate dynamic relocations for the non-primary gots.  */
12156
0
  if (gg != NULL && gg->next)
12157
0
    {
12158
0
      Elf_Internal_Rela rel[3];
12159
0
      bfd_vma addend = 0;
12160
12161
0
      memset (rel, 0, sizeof (rel));
12162
0
      rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
12163
12164
0
      for (g = gg->next; g->next != gg; g = g->next)
12165
0
  {
12166
0
    bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
12167
0
      + g->next->tls_gotno;
12168
12169
0
    MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
12170
0
           + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12171
0
    MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12172
0
           sgot->contents
12173
0
           + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12174
12175
0
    if (! bfd_link_pic (info))
12176
0
      continue;
12177
12178
0
    for (; got_index < g->local_gotno; got_index++)
12179
0
      {
12180
0
        if (got_index >= g->assigned_low_gotno
12181
0
      && got_index <= g->assigned_high_gotno)
12182
0
    continue;
12183
12184
0
        rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
12185
0
    = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
12186
0
        if (!(mips_elf_create_dynamic_relocation
12187
0
        (output_bfd, info, rel, NULL,
12188
0
         bfd_abs_section_ptr,
12189
0
         0, &addend, sgot)))
12190
0
    return false;
12191
0
        BFD_ASSERT (addend == 0);
12192
0
      }
12193
0
  }
12194
0
    }
12195
12196
  /* The generation of dynamic relocations for the non-primary gots
12197
     adds more dynamic relocations.  We cannot count them until
12198
     here.  */
12199
12200
0
  if (elf_hash_table (info)->dynamic_sections_created)
12201
0
    {
12202
0
      bfd_byte *b;
12203
0
      bool swap_out_p;
12204
12205
0
      BFD_ASSERT (sdyn != NULL);
12206
12207
0
      for (b = sdyn->contents;
12208
0
     b < sdyn->contents + sdyn->size;
12209
0
     b += MIPS_ELF_DYN_SIZE (dynobj))
12210
0
  {
12211
0
    Elf_Internal_Dyn dyn;
12212
0
    asection *s;
12213
12214
    /* Read in the current dynamic entry.  */
12215
0
    (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
12216
12217
    /* Assume that we're going to modify it and write it out.  */
12218
0
    swap_out_p = true;
12219
12220
0
    switch (dyn.d_tag)
12221
0
      {
12222
0
      case DT_RELSZ:
12223
        /* Reduce DT_RELSZ to account for any relocations we
12224
     decided not to make.  This is for the n64 irix rld,
12225
     which doesn't seem to apply any relocations if there
12226
     are trailing null entries.  */
12227
0
        s = mips_elf_rel_dyn_section (info, false);
12228
0
        dyn.d_un.d_val = (s->reloc_count
12229
0
        * (ABI_64_P (output_bfd)
12230
0
           ? sizeof (Elf64_Mips_External_Rel)
12231
0
           : sizeof (Elf32_External_Rel)));
12232
        /* Adjust the section size too.  Tools like the prelinker
12233
     can reasonably expect the values to the same.  */
12234
0
        BFD_ASSERT (!bfd_is_abs_section (s->output_section));
12235
0
        elf_section_data (s->output_section)->this_hdr.sh_size
12236
0
    = dyn.d_un.d_val;
12237
0
        break;
12238
12239
0
      default:
12240
0
        swap_out_p = false;
12241
0
        break;
12242
0
      }
12243
12244
0
    if (swap_out_p)
12245
0
      (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12246
0
        (dynobj, &dyn, b);
12247
0
  }
12248
0
    }
12249
12250
0
  {
12251
0
    asection *s;
12252
0
    Elf32_compact_rel cpt;
12253
12254
0
    if (SGI_COMPAT (output_bfd))
12255
0
      {
12256
  /* Write .compact_rel section out.  */
12257
0
  s = bfd_get_linker_section (dynobj, ".compact_rel");
12258
0
  if (s != NULL)
12259
0
    {
12260
0
      cpt.id1 = 1;
12261
0
      cpt.num = s->reloc_count;
12262
0
      cpt.id2 = 2;
12263
0
      cpt.offset = (s->output_section->filepos
12264
0
        + sizeof (Elf32_External_compact_rel));
12265
0
      cpt.reserved0 = 0;
12266
0
      cpt.reserved1 = 0;
12267
0
      bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
12268
0
              ((Elf32_External_compact_rel *)
12269
0
               s->contents));
12270
12271
      /* Clean up a dummy stub function entry in .text.  */
12272
0
      if (htab->sstubs != NULL
12273
0
    && htab->sstubs->contents != NULL)
12274
0
        {
12275
0
    file_ptr dummy_offset;
12276
12277
0
    BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
12278
0
    dummy_offset = htab->sstubs->size - htab->function_stub_size;
12279
0
    memset (htab->sstubs->contents + dummy_offset, 0,
12280
0
      htab->function_stub_size);
12281
0
        }
12282
0
    }
12283
0
      }
12284
12285
    /* The psABI says that the dynamic relocations must be sorted in
12286
       increasing order of r_symndx.  The VxWorks EABI doesn't require
12287
       this, and because the code below handles REL rather than RELA
12288
       relocations, using it for VxWorks would be outright harmful.  */
12289
0
    if (htab->root.target_os != is_vxworks)
12290
0
      {
12291
0
  s = mips_elf_rel_dyn_section (info, false);
12292
0
  if (s != NULL
12293
0
      && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
12294
0
    {
12295
0
      reldyn_sorting_bfd = output_bfd;
12296
12297
0
      if (ABI_64_P (output_bfd))
12298
0
        qsort ((Elf64_External_Rel *) s->contents + 1,
12299
0
         s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
12300
0
         sort_dynamic_relocs_64);
12301
0
      else
12302
0
        qsort ((Elf32_External_Rel *) s->contents + 1,
12303
0
         s->reloc_count - 1, sizeof (Elf32_External_Rel),
12304
0
         sort_dynamic_relocs);
12305
0
    }
12306
0
      }
12307
0
  }
12308
12309
0
  if (htab->root.splt && htab->root.splt->size > 0)
12310
0
    {
12311
0
      if (htab->root.target_os == is_vxworks)
12312
0
  {
12313
0
    if (bfd_link_pic (info))
12314
0
      mips_vxworks_finish_shared_plt (output_bfd, info);
12315
0
    else
12316
0
      mips_vxworks_finish_exec_plt (output_bfd, info);
12317
0
  }
12318
0
      else
12319
0
  {
12320
0
    BFD_ASSERT (!bfd_link_pic (info));
12321
0
    if (!mips_finish_exec_plt (output_bfd, info))
12322
0
      return false;
12323
0
  }
12324
0
    }
12325
0
  return true;
12326
0
}
12327
12328
12329
/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags.  */
12330
12331
static void
12332
mips_set_isa_flags (bfd *abfd)
12333
1
{
12334
1
  flagword val;
12335
12336
1
  switch (bfd_get_mach (abfd))
12337
1
    {
12338
0
    default:
12339
0
      if (ABI_N32_P (abfd) || ABI_64_P (abfd))
12340
0
        val = MIPS_DEFAULT_R6 ? EF_MIPS_ARCH_64R6 : EF_MIPS_ARCH_3;
12341
0
      else
12342
0
        val = MIPS_DEFAULT_R6 ? EF_MIPS_ARCH_32R6 : EF_MIPS_ARCH_1;
12343
0
      break;
12344
12345
0
    case bfd_mach_mips3000:
12346
0
      val = EF_MIPS_ARCH_1;
12347
0
      break;
12348
12349
0
    case bfd_mach_mips3900:
12350
0
      val = EF_MIPS_ARCH_1 | EF_MIPS_MACH_3900;
12351
0
      break;
12352
12353
0
    case bfd_mach_mips6000:
12354
0
      val = EF_MIPS_ARCH_2;
12355
0
      break;
12356
12357
0
    case bfd_mach_mips4010:
12358
0
      val = EF_MIPS_ARCH_2 | EF_MIPS_MACH_4010;
12359
0
      break;
12360
12361
0
    case bfd_mach_mips_allegrex:
12362
0
      val = EF_MIPS_ARCH_2 | EF_MIPS_MACH_ALLEGREX;
12363
0
      break;
12364
12365
0
    case bfd_mach_mips4000:
12366
0
    case bfd_mach_mips4300:
12367
0
    case bfd_mach_mips4400:
12368
0
    case bfd_mach_mips4600:
12369
0
      val = EF_MIPS_ARCH_3;
12370
0
      break;
12371
12372
0
    case bfd_mach_mips4100:
12373
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4100;
12374
0
      break;
12375
12376
0
    case bfd_mach_mips4111:
12377
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4111;
12378
0
      break;
12379
12380
0
    case bfd_mach_mips4120:
12381
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4120;
12382
0
      break;
12383
12384
0
    case bfd_mach_mips4650:
12385
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_4650;
12386
0
      break;
12387
12388
0
    case bfd_mach_mips5400:
12389
0
      val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_5400;
12390
0
      break;
12391
12392
0
    case bfd_mach_mips5500:
12393
0
      val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_5500;
12394
0
      break;
12395
12396
0
    case bfd_mach_mips5900:
12397
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_5900;
12398
0
      break;
12399
12400
0
    case bfd_mach_mips9000:
12401
0
      val = EF_MIPS_ARCH_4 | EF_MIPS_MACH_9000;
12402
0
      break;
12403
12404
0
    case bfd_mach_mips5000:
12405
0
    case bfd_mach_mips7000:
12406
1
    case bfd_mach_mips8000:
12407
1
    case bfd_mach_mips10000:
12408
1
    case bfd_mach_mips12000:
12409
1
    case bfd_mach_mips14000:
12410
1
    case bfd_mach_mips16000:
12411
1
      val = EF_MIPS_ARCH_4;
12412
1
      break;
12413
12414
0
    case bfd_mach_mips5:
12415
0
      val = EF_MIPS_ARCH_5;
12416
0
      break;
12417
12418
0
    case bfd_mach_mips_loongson_2e:
12419
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_LS2E;
12420
0
      break;
12421
12422
0
    case bfd_mach_mips_loongson_2f:
12423
0
      val = EF_MIPS_ARCH_3 | EF_MIPS_MACH_LS2F;
12424
0
      break;
12425
12426
0
    case bfd_mach_mips_sb1:
12427
0
      val = EF_MIPS_ARCH_64 | EF_MIPS_MACH_SB1;
12428
0
      break;
12429
12430
0
    case bfd_mach_mips_gs464:
12431
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS464;
12432
0
      break;
12433
12434
0
    case bfd_mach_mips_gs464e:
12435
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS464E;
12436
0
      break;
12437
12438
0
    case bfd_mach_mips_gs264e:
12439
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_GS264E;
12440
0
      break;
12441
12442
0
    case bfd_mach_mips_octeon:
12443
0
    case bfd_mach_mips_octeonp:
12444
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON;
12445
0
      break;
12446
12447
0
    case bfd_mach_mips_octeon3:
12448
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON3;
12449
0
      break;
12450
12451
0
    case bfd_mach_mips_xlr:
12452
0
      val = EF_MIPS_ARCH_64 | EF_MIPS_MACH_XLR;
12453
0
      break;
12454
12455
0
    case bfd_mach_mips_octeon2:
12456
0
      val = EF_MIPS_ARCH_64R2 | EF_MIPS_MACH_OCTEON2;
12457
0
      break;
12458
12459
0
    case bfd_mach_mipsisa32:
12460
0
      val = EF_MIPS_ARCH_32;
12461
0
      break;
12462
12463
0
    case bfd_mach_mipsisa64:
12464
0
      val = EF_MIPS_ARCH_64;
12465
0
      break;
12466
12467
0
    case bfd_mach_mipsisa32r2:
12468
0
    case bfd_mach_mipsisa32r3:
12469
0
    case bfd_mach_mipsisa32r5:
12470
0
      val = EF_MIPS_ARCH_32R2;
12471
0
      break;
12472
12473
0
    case bfd_mach_mips_interaptiv_mr2:
12474
0
      val = EF_MIPS_ARCH_32R2 | EF_MIPS_MACH_IAMR2;
12475
0
      break;
12476
12477
0
    case bfd_mach_mipsisa64r2:
12478
0
    case bfd_mach_mipsisa64r3:
12479
0
    case bfd_mach_mipsisa64r5:
12480
0
      val = EF_MIPS_ARCH_64R2;
12481
0
      break;
12482
12483
0
    case bfd_mach_mipsisa32r6:
12484
0
      val = EF_MIPS_ARCH_32R6;
12485
0
      break;
12486
12487
0
    case bfd_mach_mipsisa64r6:
12488
0
      val = EF_MIPS_ARCH_64R6;
12489
0
      break;
12490
1
    }
12491
1
  elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12492
1
  elf_elfheader (abfd)->e_flags |= val;
12493
12494
1
}
12495
12496
12497
/* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
12498
   Don't do so for code sections.  We want to keep ordering of HI16/LO16
12499
   as is.  On the other hand, elf-eh-frame.c processing requires .eh_frame
12500
   relocs to be sorted.  */
12501
12502
bool
12503
_bfd_mips_elf_sort_relocs_p (asection *sec)
12504
0
{
12505
0
  return (sec->flags & SEC_CODE) == 0;
12506
0
}
12507
12508
12509
/* The final processing done just before writing out a MIPS ELF object
12510
   file.  This gets the MIPS architecture right based on the machine
12511
   number.  This is used by both the 32-bit and the 64-bit ABI.  */
12512
12513
void
12514
_bfd_mips_final_write_processing (bfd *abfd)
12515
1
{
12516
1
  unsigned int i;
12517
1
  Elf_Internal_Shdr **hdrpp;
12518
1
  const char *name;
12519
1
  asection *sec;
12520
12521
  /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
12522
     is nonzero.  This is for compatibility with old objects, which used
12523
     a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH.  */
12524
1
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
12525
1
    mips_set_isa_flags (abfd);
12526
12527
  /* Set the sh_info field for .gptab sections and other appropriate
12528
     info for each special section.  */
12529
1
  for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
12530
2
       i < elf_numsections (abfd);
12531
1
       i++, hdrpp++)
12532
1
    {
12533
1
      switch ((*hdrpp)->sh_type)
12534
1
  {
12535
0
  case SHT_MIPS_MSYM:
12536
0
  case SHT_MIPS_LIBLIST:
12537
0
    sec = bfd_get_section_by_name (abfd, ".dynstr");
12538
0
    if (sec != NULL)
12539
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12540
0
    break;
12541
12542
0
  case SHT_MIPS_GPTAB:
12543
0
    BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12544
0
    name = bfd_section_name ((*hdrpp)->bfd_section);
12545
0
    if (startswith (name, ".gptab."))
12546
0
      {
12547
0
        sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
12548
0
        if (sec != NULL)
12549
0
    (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12550
0
      }
12551
0
    break;
12552
12553
0
  case SHT_MIPS_CONTENT:
12554
0
    BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12555
0
    name = bfd_section_name ((*hdrpp)->bfd_section);
12556
0
    if (startswith (name, ".MIPS.content"))
12557
0
      {
12558
0
        sec = bfd_get_section_by_name (abfd,
12559
0
               name + sizeof ".MIPS.content" - 1);
12560
0
        if (sec != NULL)
12561
0
    (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12562
0
      }
12563
0
    break;
12564
12565
0
  case SHT_MIPS_SYMBOL_LIB:
12566
0
    sec = bfd_get_section_by_name (abfd, ".dynsym");
12567
0
    if (sec != NULL)
12568
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12569
0
    sec = bfd_get_section_by_name (abfd, ".liblist");
12570
0
    if (sec != NULL)
12571
0
      (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12572
0
    break;
12573
12574
0
  case SHT_MIPS_EVENTS:
12575
0
    BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12576
0
    name = bfd_section_name ((*hdrpp)->bfd_section);
12577
0
    if (startswith (name, ".MIPS.events"))
12578
0
      sec = bfd_get_section_by_name (abfd,
12579
0
             name + sizeof ".MIPS.events" - 1);
12580
0
    else if (startswith (name, ".MIPS.post_rel"))
12581
0
      sec = bfd_get_section_by_name (abfd,
12582
0
             name + sizeof ".MIPS.post_rel" - 1);
12583
0
    else
12584
0
      sec = NULL;
12585
0
    if (sec != NULL)
12586
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12587
0
    break;
12588
12589
0
  case SHT_MIPS_XHASH:
12590
0
    sec = bfd_get_section_by_name (abfd, ".dynsym");
12591
0
    if (sec != NULL)
12592
0
      (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12593
1
  }
12594
1
    }
12595
1
}
12596
12597
bool
12598
_bfd_mips_elf_final_write_processing (bfd *abfd)
12599
1
{
12600
1
  _bfd_mips_final_write_processing (abfd);
12601
1
  return _bfd_elf_final_write_processing (abfd);
12602
1
}
12603

12604
/* When creating an IRIX5 executable, we need REGINFO and RTPROC
12605
   segments.  */
12606
12607
int
12608
_bfd_mips_elf_additional_program_headers (bfd *abfd,
12609
            struct bfd_link_info *info ATTRIBUTE_UNUSED)
12610
0
{
12611
0
  asection *s;
12612
0
  int ret = 0;
12613
12614
  /* See if we need a PT_MIPS_REGINFO segment.  */
12615
0
  s = bfd_get_section_by_name (abfd, ".reginfo");
12616
0
  if (s && (s->flags & SEC_LOAD))
12617
0
    ++ret;
12618
12619
  /* See if we need a PT_MIPS_ABIFLAGS segment.  */
12620
0
  if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
12621
0
    ++ret;
12622
12623
  /* See if we need a PT_MIPS_OPTIONS segment.  */
12624
0
  if (IRIX_COMPAT (abfd) == ict_irix6
12625
0
      && bfd_get_section_by_name (abfd,
12626
0
          MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
12627
0
    ++ret;
12628
12629
  /* See if we need a PT_MIPS_RTPROC segment.  */
12630
0
  if (IRIX_COMPAT (abfd) == ict_irix5
12631
0
      && bfd_get_section_by_name (abfd, ".dynamic")
12632
0
      && bfd_get_section_by_name (abfd, ".mdebug"))
12633
0
    ++ret;
12634
12635
  /* Allocate a PT_NULL header in dynamic objects.  See
12636
     _bfd_mips_elf_modify_segment_map for details.  */
12637
0
  if (!SGI_COMPAT (abfd)
12638
0
      && bfd_get_section_by_name (abfd, ".dynamic"))
12639
0
    ++ret;
12640
12641
0
  return ret;
12642
0
}
12643
12644
/* Modify the segment map for an IRIX5 executable.  */
12645
12646
bool
12647
_bfd_mips_elf_modify_segment_map (bfd *abfd,
12648
          struct bfd_link_info *info)
12649
1
{
12650
1
  asection *s;
12651
1
  struct elf_segment_map *m, **pm;
12652
1
  size_t amt;
12653
12654
  /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
12655
     segment.  */
12656
1
  s = bfd_get_section_by_name (abfd, ".reginfo");
12657
1
  if (s != NULL && (s->flags & SEC_LOAD) != 0)
12658
0
    {
12659
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12660
0
  if (m->p_type == PT_MIPS_REGINFO)
12661
0
    break;
12662
0
      if (m == NULL)
12663
0
  {
12664
0
    amt = sizeof *m;
12665
0
    m = bfd_zalloc (abfd, amt);
12666
0
    if (m == NULL)
12667
0
      return false;
12668
12669
0
    m->p_type = PT_MIPS_REGINFO;
12670
0
    m->count = 1;
12671
0
    m->sections[0] = s;
12672
12673
    /* We want to put it after the PHDR and INTERP segments.  */
12674
0
    pm = &elf_seg_map (abfd);
12675
0
    while (*pm != NULL
12676
0
     && ((*pm)->p_type == PT_PHDR
12677
0
         || (*pm)->p_type == PT_INTERP))
12678
0
      pm = &(*pm)->next;
12679
12680
0
    m->next = *pm;
12681
0
    *pm = m;
12682
0
  }
12683
0
    }
12684
12685
  /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
12686
     segment.  */
12687
1
  s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
12688
1
  if (s != NULL && (s->flags & SEC_LOAD) != 0)
12689
0
    {
12690
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12691
0
  if (m->p_type == PT_MIPS_ABIFLAGS)
12692
0
    break;
12693
0
      if (m == NULL)
12694
0
  {
12695
0
    amt = sizeof *m;
12696
0
    m = bfd_zalloc (abfd, amt);
12697
0
    if (m == NULL)
12698
0
      return false;
12699
12700
0
    m->p_type = PT_MIPS_ABIFLAGS;
12701
0
    m->count = 1;
12702
0
    m->sections[0] = s;
12703
12704
    /* We want to put it after the PHDR and INTERP segments.  */
12705
0
    pm = &elf_seg_map (abfd);
12706
0
    while (*pm != NULL
12707
0
     && ((*pm)->p_type == PT_PHDR
12708
0
         || (*pm)->p_type == PT_INTERP))
12709
0
      pm = &(*pm)->next;
12710
12711
0
    m->next = *pm;
12712
0
    *pm = m;
12713
0
  }
12714
0
    }
12715
12716
  /* For IRIX 6, we don't have .mdebug sections, nor does anything but
12717
     .dynamic end up in PT_DYNAMIC.  However, we do have to insert a
12718
     PT_MIPS_OPTIONS segment immediately following the program header
12719
     table.  */
12720
1
  if (NEWABI_P (abfd)
12721
      /* On non-IRIX6 new abi, we'll have already created a segment
12722
   for this section, so don't create another.  I'm not sure this
12723
   is not also the case for IRIX 6, but I can't test it right
12724
   now.  */
12725
1
      && IRIX_COMPAT (abfd) == ict_irix6)
12726
0
    {
12727
0
      for (s = abfd->sections; s; s = s->next)
12728
0
  if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
12729
0
    break;
12730
12731
0
      if (s)
12732
0
  {
12733
0
    struct elf_segment_map *options_segment;
12734
12735
0
    pm = &elf_seg_map (abfd);
12736
0
    while (*pm != NULL
12737
0
     && ((*pm)->p_type == PT_PHDR
12738
0
         || (*pm)->p_type == PT_INTERP))
12739
0
      pm = &(*pm)->next;
12740
12741
0
    if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
12742
0
      {
12743
0
        amt = sizeof (struct elf_segment_map);
12744
0
        options_segment = bfd_zalloc (abfd, amt);
12745
0
        options_segment->next = *pm;
12746
0
        options_segment->p_type = PT_MIPS_OPTIONS;
12747
0
        options_segment->p_flags = PF_R;
12748
0
        options_segment->p_flags_valid = true;
12749
0
        options_segment->count = 1;
12750
0
        options_segment->sections[0] = s;
12751
0
        *pm = options_segment;
12752
0
      }
12753
0
  }
12754
0
    }
12755
1
  else
12756
1
    {
12757
1
      if (IRIX_COMPAT (abfd) == ict_irix5)
12758
1
  {
12759
    /* If there are .dynamic and .mdebug sections, we make a room
12760
       for the RTPROC header.  FIXME: Rewrite without section names.  */
12761
1
    if (bfd_get_section_by_name (abfd, ".interp") == NULL
12762
1
        && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12763
1
        && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12764
0
      {
12765
0
        for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12766
0
    if (m->p_type == PT_MIPS_RTPROC)
12767
0
      break;
12768
0
        if (m == NULL)
12769
0
    {
12770
0
      amt = sizeof *m;
12771
0
      m = bfd_zalloc (abfd, amt);
12772
0
      if (m == NULL)
12773
0
        return false;
12774
12775
0
      m->p_type = PT_MIPS_RTPROC;
12776
12777
0
      s = bfd_get_section_by_name (abfd, ".rtproc");
12778
0
      if (s == NULL)
12779
0
        {
12780
0
          m->count = 0;
12781
0
          m->p_flags = 0;
12782
0
          m->p_flags_valid = 1;
12783
0
        }
12784
0
      else
12785
0
        {
12786
0
          m->count = 1;
12787
0
          m->sections[0] = s;
12788
0
        }
12789
12790
      /* We want to put it after the DYNAMIC segment.  */
12791
0
      pm = &elf_seg_map (abfd);
12792
0
      while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12793
0
        pm = &(*pm)->next;
12794
0
      if (*pm != NULL)
12795
0
        pm = &(*pm)->next;
12796
12797
0
      m->next = *pm;
12798
0
      *pm = m;
12799
0
    }
12800
0
      }
12801
1
  }
12802
      /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
12803
   .dynstr, .dynsym, and .hash sections, and everything in
12804
   between.  */
12805
1
      for (pm = &elf_seg_map (abfd); *pm != NULL;
12806
1
     pm = &(*pm)->next)
12807
0
  if ((*pm)->p_type == PT_DYNAMIC)
12808
0
    break;
12809
1
      m = *pm;
12810
      /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12811
   glibc's dynamic linker has traditionally derived the number of
12812
   tags from the p_filesz field, and sometimes allocates stack
12813
   arrays of that size.  An overly-big PT_DYNAMIC segment can
12814
   be actively harmful in such cases.  Making PT_DYNAMIC contain
12815
   other sections can also make life hard for the prelinker,
12816
   which might move one of the other sections to a different
12817
   PT_LOAD segment.  */
12818
1
      if (SGI_COMPAT (abfd)
12819
1
    && m != NULL
12820
1
    && m->count == 1
12821
1
    && strcmp (m->sections[0]->name, ".dynamic") == 0)
12822
0
  {
12823
0
    static const char *sec_names[] =
12824
0
    {
12825
0
      ".dynamic", ".dynstr", ".dynsym", ".hash"
12826
0
    };
12827
0
    bfd_vma low, high;
12828
0
    unsigned int i, c;
12829
0
    struct elf_segment_map *n;
12830
12831
0
    low = ~(bfd_vma) 0;
12832
0
    high = 0;
12833
0
    for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12834
0
      {
12835
0
        s = bfd_get_section_by_name (abfd, sec_names[i]);
12836
0
        if (s != NULL && (s->flags & SEC_LOAD) != 0)
12837
0
    {
12838
0
      bfd_size_type sz;
12839
12840
0
      if (low > s->vma)
12841
0
        low = s->vma;
12842
0
      sz = s->size;
12843
0
      if (high < s->vma + sz)
12844
0
        high = s->vma + sz;
12845
0
    }
12846
0
      }
12847
12848
0
    c = 0;
12849
0
    for (s = abfd->sections; s != NULL; s = s->next)
12850
0
      if ((s->flags & SEC_LOAD) != 0
12851
0
    && s->vma >= low
12852
0
    && s->vma + s->size <= high)
12853
0
        ++c;
12854
12855
0
    amt = sizeof *n - sizeof (asection *) + c * sizeof (asection *);
12856
0
    n = bfd_zalloc (abfd, amt);
12857
0
    if (n == NULL)
12858
0
      return false;
12859
0
    *n = *m;
12860
0
    n->count = c;
12861
12862
0
    i = 0;
12863
0
    for (s = abfd->sections; s != NULL; s = s->next)
12864
0
      {
12865
0
        if ((s->flags & SEC_LOAD) != 0
12866
0
      && s->vma >= low
12867
0
      && s->vma + s->size <= high)
12868
0
    {
12869
0
      n->sections[i] = s;
12870
0
      ++i;
12871
0
    }
12872
0
      }
12873
12874
0
    *pm = n;
12875
0
  }
12876
1
    }
12877
12878
  /* Allocate a spare program header in dynamic objects so that tools
12879
     like the prelinker can add an extra PT_LOAD entry.
12880
12881
     If the prelinker needs to make room for a new PT_LOAD entry, its
12882
     standard procedure is to move the first (read-only) sections into
12883
     the new (writable) segment.  However, the MIPS ABI requires
12884
     .dynamic to be in a read-only segment, and the section will often
12885
     start within sizeof (ElfNN_Phdr) bytes of the last program header.
12886
12887
     Although the prelinker could in principle move .dynamic to a
12888
     writable segment, it seems better to allocate a spare program
12889
     header instead, and avoid the need to move any sections.
12890
     There is a long tradition of allocating spare dynamic tags,
12891
     so allocating a spare program header seems like a natural
12892
     extension.
12893
12894
     If INFO is NULL, we may be copying an already prelinked binary
12895
     with objcopy or strip, so do not add this header.  */
12896
1
  if (info != NULL
12897
1
      && !SGI_COMPAT (abfd)
12898
1
      && bfd_get_section_by_name (abfd, ".dynamic"))
12899
0
    {
12900
0
      for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12901
0
  if ((*pm)->p_type == PT_NULL)
12902
0
    break;
12903
0
      if (*pm == NULL)
12904
0
  {
12905
0
    m = bfd_zalloc (abfd, sizeof (*m));
12906
0
    if (m == NULL)
12907
0
      return false;
12908
12909
0
    m->p_type = PT_NULL;
12910
0
    *pm = m;
12911
0
  }
12912
0
    }
12913
12914
1
  return true;
12915
1
}
12916

12917
/* Return the section that should be marked against GC for a given
12918
   relocation.  */
12919
12920
asection *
12921
_bfd_mips_elf_gc_mark_hook (asection *sec,
12922
          struct bfd_link_info *info,
12923
          Elf_Internal_Rela *rel,
12924
          struct elf_link_hash_entry *h,
12925
          Elf_Internal_Sym *sym)
12926
0
{
12927
  /* ??? Do mips16 stub sections need to be handled special?  */
12928
12929
0
  if (h != NULL)
12930
0
    switch (ELF_R_TYPE (sec->owner, rel->r_info))
12931
0
      {
12932
0
      case R_MIPS_GNU_VTINHERIT:
12933
0
      case R_MIPS_GNU_VTENTRY:
12934
0
  return NULL;
12935
0
      }
12936
12937
0
  return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12938
0
}
12939
12940
/* Prevent .MIPS.abiflags from being discarded with --gc-sections.  */
12941
12942
bool
12943
_bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12944
              elf_gc_mark_hook_fn gc_mark_hook)
12945
0
{
12946
0
  bfd *sub;
12947
12948
0
  _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12949
12950
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12951
0
    {
12952
0
      asection *o;
12953
12954
0
      if (! is_mips_elf (sub))
12955
0
  continue;
12956
12957
0
      for (o = sub->sections; o != NULL; o = o->next)
12958
0
  if (!o->gc_mark
12959
0
      && MIPS_ELF_ABIFLAGS_SECTION_NAME_P (bfd_section_name (o)))
12960
0
    {
12961
0
      if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12962
0
        return false;
12963
0
    }
12964
0
    }
12965
12966
0
  return true;
12967
0
}
12968

12969
/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12970
   hiding the old indirect symbol.  Process additional relocation
12971
   information.  Also called for weakdefs, in which case we just let
12972
   _bfd_elf_link_hash_copy_indirect copy the flags for us.  */
12973
12974
void
12975
_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
12976
            struct elf_link_hash_entry *dir,
12977
            struct elf_link_hash_entry *ind)
12978
0
{
12979
0
  struct mips_elf_link_hash_entry *dirmips, *indmips;
12980
12981
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
12982
12983
0
  dirmips = (struct mips_elf_link_hash_entry *) dir;
12984
0
  indmips = (struct mips_elf_link_hash_entry *) ind;
12985
  /* Any absolute non-dynamic relocations against an indirect or weak
12986
     definition will be against the target symbol.  */
12987
0
  if (indmips->has_static_relocs)
12988
0
    dirmips->has_static_relocs = true;
12989
12990
0
  if (ind->root.type != bfd_link_hash_indirect)
12991
0
    return;
12992
12993
0
  dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12994
0
  if (indmips->readonly_reloc)
12995
0
    dirmips->readonly_reloc = true;
12996
0
  if (indmips->no_fn_stub)
12997
0
    dirmips->no_fn_stub = true;
12998
0
  if (indmips->fn_stub)
12999
0
    {
13000
0
      dirmips->fn_stub = indmips->fn_stub;
13001
0
      indmips->fn_stub = NULL;
13002
0
    }
13003
0
  if (indmips->need_fn_stub)
13004
0
    {
13005
0
      dirmips->need_fn_stub = true;
13006
0
      indmips->need_fn_stub = false;
13007
0
    }
13008
0
  if (indmips->call_stub)
13009
0
    {
13010
0
      dirmips->call_stub = indmips->call_stub;
13011
0
      indmips->call_stub = NULL;
13012
0
    }
13013
0
  if (indmips->call_fp_stub)
13014
0
    {
13015
0
      dirmips->call_fp_stub = indmips->call_fp_stub;
13016
0
      indmips->call_fp_stub = NULL;
13017
0
    }
13018
0
  if (indmips->global_got_area < dirmips->global_got_area)
13019
0
    dirmips->global_got_area = indmips->global_got_area;
13020
0
  if (indmips->global_got_area < GGA_NONE)
13021
0
    indmips->global_got_area = GGA_NONE;
13022
0
  if (indmips->has_nonpic_branches)
13023
0
    dirmips->has_nonpic_branches = true;
13024
0
}
13025
13026
/* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
13027
   to hide it.  It has to remain global (it will also be protected) so as to
13028
   be assigned a global GOT entry, which will then remain unchanged at load
13029
   time.  */
13030
13031
void
13032
_bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
13033
         struct elf_link_hash_entry *entry,
13034
         bool force_local)
13035
0
{
13036
0
  struct mips_elf_link_hash_table *htab;
13037
13038
0
  htab = mips_elf_hash_table (info);
13039
0
  BFD_ASSERT (htab != NULL);
13040
0
  if (htab->use_absolute_zero
13041
0
      && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
13042
0
    return;
13043
13044
0
  _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
13045
0
}
13046

13047
0
#define PDR_SIZE 32
13048
13049
bool
13050
_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
13051
          struct bfd_link_info *info)
13052
0
{
13053
0
  asection *o;
13054
0
  bool ret = false;
13055
0
  unsigned char *tdata;
13056
0
  size_t i, skip;
13057
13058
0
  o = bfd_get_section_by_name (abfd, ".pdr");
13059
0
  if (! o)
13060
0
    return false;
13061
0
  if (o->size == 0)
13062
0
    return false;
13063
0
  if (o->size % PDR_SIZE != 0)
13064
0
    return false;
13065
0
  if (o->output_section != NULL
13066
0
      && bfd_is_abs_section (o->output_section))
13067
0
    return false;
13068
13069
0
  tdata = bfd_zmalloc (o->size / PDR_SIZE);
13070
0
  if (! tdata)
13071
0
    return false;
13072
13073
0
  cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
13074
0
              info->keep_memory);
13075
0
  if (!cookie->rels)
13076
0
    {
13077
0
      free (tdata);
13078
0
      return false;
13079
0
    }
13080
13081
0
  cookie->rel = cookie->rels;
13082
0
  cookie->relend = cookie->rels + o->reloc_count;
13083
13084
0
  for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
13085
0
    {
13086
0
      if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
13087
0
  {
13088
0
    tdata[i] = 1;
13089
0
    skip ++;
13090
0
  }
13091
0
    }
13092
13093
0
  if (skip != 0)
13094
0
    {
13095
0
      mips_elf_section_data (o)->u.tdata = tdata;
13096
0
      if (o->rawsize == 0)
13097
0
  o->rawsize = o->size;
13098
0
      o->size -= skip * PDR_SIZE;
13099
0
      ret = true;
13100
0
    }
13101
0
  else
13102
0
    free (tdata);
13103
13104
0
  if (! info->keep_memory)
13105
0
    free (cookie->rels);
13106
13107
0
  return ret;
13108
0
}
13109
13110
bool
13111
_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
13112
0
{
13113
0
  if (strcmp (sec->name, ".pdr") == 0)
13114
0
    return true;
13115
0
  return false;
13116
0
}
13117
13118
bool
13119
_bfd_mips_elf_write_section (bfd *output_bfd,
13120
           struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
13121
           asection *sec, bfd_byte *contents)
13122
0
{
13123
0
  bfd_byte *to, *from, *end;
13124
0
  int i;
13125
13126
0
  if (strcmp (sec->name, ".pdr") != 0)
13127
0
    return false;
13128
13129
0
  if (mips_elf_section_data (sec)->u.tdata == NULL)
13130
0
    return false;
13131
13132
0
  to = contents;
13133
0
  end = contents + sec->size;
13134
0
  for (from = contents, i = 0;
13135
0
       from < end;
13136
0
       from += PDR_SIZE, i++)
13137
0
    {
13138
0
      if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
13139
0
  continue;
13140
0
      if (to != from)
13141
0
  memcpy (to, from, PDR_SIZE);
13142
0
      to += PDR_SIZE;
13143
0
    }
13144
0
  bfd_set_section_contents (output_bfd, sec->output_section, contents,
13145
0
          sec->output_offset, sec->size);
13146
0
  return true;
13147
0
}
13148

13149
/* microMIPS code retains local labels for linker relaxation.  Omit them
13150
   from output by default for clarity.  */
13151
13152
bool
13153
_bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
13154
1.34k
{
13155
1.34k
  return _bfd_elf_is_local_label_name (abfd, sym->name);
13156
1.34k
}
13157
13158
bool
13159
_bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
13160
         asection *section, bfd_vma offset,
13161
         const char **filename_ptr,
13162
         const char **functionname_ptr,
13163
         unsigned int *line_ptr,
13164
         unsigned int *discriminator_ptr)
13165
2.73k
{
13166
2.73k
  asection *msec;
13167
13168
2.73k
  if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
13169
2.73k
             filename_ptr, functionname_ptr,
13170
2.73k
             line_ptr, discriminator_ptr,
13171
2.73k
             dwarf_debug_sections,
13172
2.73k
             &elf_tdata (abfd)->dwarf2_find_line_info)
13173
2.73k
      == 1)
13174
6
    return true;
13175
13176
2.73k
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
13177
2.73k
             filename_ptr, functionname_ptr,
13178
2.73k
             line_ptr))
13179
0
    {
13180
0
      if (!*functionname_ptr)
13181
0
  _bfd_elf_find_function (abfd, symbols, section, offset,
13182
0
        *filename_ptr ? NULL : filename_ptr,
13183
0
        functionname_ptr);
13184
0
      return true;
13185
0
    }
13186
13187
2.73k
  msec = bfd_get_section_by_name (abfd, ".mdebug");
13188
2.73k
  if (msec != NULL)
13189
1.89k
    {
13190
1.89k
      flagword origflags;
13191
1.89k
      struct mips_elf_find_line *fi;
13192
1.89k
      const struct ecoff_debug_swap * const swap =
13193
1.89k
  get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
13194
13195
      /* If we are called during a link, mips_elf_final_link may have
13196
   cleared the SEC_HAS_CONTENTS field.  We force it back on here
13197
   if appropriate (which it normally will be).  */
13198
1.89k
      origflags = msec->flags;
13199
1.89k
      if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
13200
1.89k
  msec->flags |= SEC_HAS_CONTENTS;
13201
13202
1.89k
      fi = mips_elf_tdata (abfd)->find_line_info;
13203
1.89k
      if (fi == NULL)
13204
1.37k
  {
13205
1.37k
    bfd_size_type external_fdr_size;
13206
1.37k
    char *fraw_src;
13207
1.37k
    char *fraw_end;
13208
1.37k
    struct fdr *fdr_ptr;
13209
1.37k
    bfd_size_type amt = sizeof (struct mips_elf_find_line);
13210
13211
1.37k
    fi = bfd_zalloc (abfd, amt);
13212
1.37k
    if (fi == NULL)
13213
0
      {
13214
0
        msec->flags = origflags;
13215
0
        return false;
13216
0
      }
13217
13218
1.37k
    if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
13219
1.27k
      {
13220
1.27k
        msec->flags = origflags;
13221
1.27k
        return false;
13222
1.27k
      }
13223
13224
    /* Swap in the FDR information.  */
13225
104
    amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
13226
104
    fi->d.fdr = bfd_alloc (abfd, amt);
13227
104
    if (fi->d.fdr == NULL)
13228
0
      {
13229
0
        _bfd_ecoff_free_ecoff_debug_info (&fi->d);
13230
0
        msec->flags = origflags;
13231
0
        return false;
13232
0
      }
13233
104
    external_fdr_size = swap->external_fdr_size;
13234
104
    fdr_ptr = fi->d.fdr;
13235
104
    fraw_src = (char *) fi->d.external_fdr;
13236
104
    fraw_end = (fraw_src
13237
104
          + fi->d.symbolic_header.ifdMax * external_fdr_size);
13238
10.2k
    for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
13239
10.1k
      (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
13240
13241
104
    mips_elf_tdata (abfd)->find_line_info = fi;
13242
104
  }
13243
13244
619
      if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
13245
619
          &fi->i, filename_ptr, functionname_ptr,
13246
619
          line_ptr))
13247
377
  {
13248
377
    msec->flags = origflags;
13249
377
    return true;
13250
377
  }
13251
13252
242
      msec->flags = origflags;
13253
242
    }
13254
13255
  /* Fall back on the generic ELF find_nearest_line routine.  */
13256
13257
1.08k
  return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
13258
1.08k
             filename_ptr, functionname_ptr,
13259
1.08k
             line_ptr, discriminator_ptr);
13260
2.73k
}
13261
13262
bool
13263
_bfd_mips_elf_find_inliner_info (bfd *abfd,
13264
         const char **filename_ptr,
13265
         const char **functionname_ptr,
13266
         unsigned int *line_ptr)
13267
0
{
13268
0
  bool found;
13269
0
  found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13270
0
           functionname_ptr, line_ptr,
13271
0
           & elf_tdata (abfd)->dwarf2_find_line_info);
13272
0
  return found;
13273
0
}
13274
13275

13276
/* When are writing out the .options or .MIPS.options section,
13277
   remember the bytes we are writing out, so that we can install the
13278
   GP value in the section_processing routine.  */
13279
13280
bool
13281
_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
13282
            const void *location,
13283
            file_ptr offset, bfd_size_type count)
13284
0
{
13285
0
  if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
13286
0
    {
13287
0
      bfd_byte *c;
13288
13289
0
      if (elf_section_data (section) == NULL)
13290
0
  {
13291
0
    size_t amt = sizeof (struct bfd_elf_section_data);
13292
0
    section->used_by_bfd = bfd_zalloc (abfd, amt);
13293
0
    if (elf_section_data (section) == NULL)
13294
0
      return false;
13295
0
  }
13296
0
      c = mips_elf_section_data (section)->u.tdata;
13297
0
      if (c == NULL)
13298
0
  {
13299
0
    c = bfd_zalloc (abfd, section->size);
13300
0
    if (c == NULL)
13301
0
      return false;
13302
0
    mips_elf_section_data (section)->u.tdata = c;
13303
0
  }
13304
13305
0
      memcpy (c + offset, location, count);
13306
0
    }
13307
13308
0
  return _bfd_elf_set_section_contents (abfd, section, location, offset,
13309
0
          count);
13310
0
}
13311
13312
/* This is almost identical to bfd_generic_get_... except that some
13313
   MIPS relocations need to be handled specially.  Sigh.  */
13314
13315
bfd_byte *
13316
_bfd_elf_mips_get_relocated_section_contents
13317
  (bfd *abfd,
13318
   struct bfd_link_info *link_info,
13319
   struct bfd_link_order *link_order,
13320
   bfd_byte *data,
13321
   bool relocatable,
13322
   asymbol **symbols)
13323
97
{
13324
97
  bfd *input_bfd = link_order->u.indirect.section->owner;
13325
97
  asection *input_section = link_order->u.indirect.section;
13326
97
  long reloc_size;
13327
97
  arelent **reloc_vector;
13328
97
  long reloc_count;
13329
13330
97
  reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13331
97
  if (reloc_size < 0)
13332
1
    return NULL;
13333
13334
  /* Read in the section.  */
13335
96
  bfd_byte *orig_data = data;
13336
96
  if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
13337
0
    return NULL;
13338
13339
96
  if (data == NULL)
13340
1
    return NULL;
13341
13342
95
  if (reloc_size == 0)
13343
0
    return data;
13344
13345
95
  reloc_vector = (arelent **) bfd_malloc (reloc_size);
13346
95
  if (reloc_vector == NULL)
13347
0
    {
13348
0
      struct mips_elf_obj_tdata *tdata;
13349
0
      struct mips_hi16 **hip, *hi;
13350
68
    error_return:
13351
      /* If we are going to return an error, remove entries on
13352
   mips_hi16_list that point into this section's data.  Data
13353
   will typically be freed on return from this function.  */
13354
68
      tdata = mips_elf_tdata (abfd);
13355
68
      hip = &tdata->mips_hi16_list;
13356
68
      while ((hi = *hip) != NULL)
13357
0
  {
13358
0
    if (hi->input_section == input_section)
13359
0
      {
13360
0
        *hip = hi->next;
13361
0
        free (hi);
13362
0
      }
13363
0
    else
13364
0
      hip = &hi->next;
13365
0
  }
13366
68
      if (orig_data == NULL)
13367
11
  free (data);
13368
68
      data = NULL;
13369
68
      goto out;
13370
0
    }
13371
13372
95
  reloc_count = bfd_canonicalize_reloc (input_bfd,
13373
95
          input_section,
13374
95
          reloc_vector,
13375
95
          symbols);
13376
95
  if (reloc_count < 0)
13377
29
    goto error_return;
13378
13379
66
  if (reloc_count > 0)
13380
53
    {
13381
53
      arelent **parent;
13382
      /* for mips */
13383
53
      int gp_found;
13384
53
      bfd_vma gp = 0x12345678;  /* initialize just to shut gcc up */
13385
13386
53
      {
13387
53
  struct bfd_hash_entry *h;
13388
53
  struct bfd_link_hash_entry *lh;
13389
  /* Skip all this stuff if we aren't mixing formats.  */
13390
53
  if (abfd && input_bfd
13391
53
      && abfd->xvec == input_bfd->xvec)
13392
53
    lh = 0;
13393
0
  else
13394
0
    {
13395
0
      h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
13396
0
      lh = (struct bfd_link_hash_entry *) h;
13397
0
    }
13398
53
      lookup:
13399
53
  if (lh)
13400
0
    {
13401
0
      switch (lh->type)
13402
0
        {
13403
0
        case bfd_link_hash_undefined:
13404
0
        case bfd_link_hash_undefweak:
13405
0
        case bfd_link_hash_common:
13406
0
    gp_found = 0;
13407
0
    break;
13408
0
        case bfd_link_hash_defined:
13409
0
        case bfd_link_hash_defweak:
13410
0
    gp_found = 1;
13411
0
    gp = lh->u.def.value;
13412
0
    break;
13413
0
        case bfd_link_hash_indirect:
13414
0
        case bfd_link_hash_warning:
13415
0
    lh = lh->u.i.link;
13416
    /* @@FIXME  ignoring warning for now */
13417
0
    goto lookup;
13418
0
        case bfd_link_hash_new:
13419
0
        default:
13420
0
    abort ();
13421
0
        }
13422
0
    }
13423
53
  else
13424
53
    gp_found = 0;
13425
53
      }
13426
      /* end mips */
13427
13428
493
      for (parent = reloc_vector; *parent != NULL; parent++)
13429
479
  {
13430
479
    char *error_message = NULL;
13431
479
    asymbol *symbol;
13432
479
    bfd_reloc_status_type r;
13433
13434
479
    symbol = *(*parent)->sym_ptr_ptr;
13435
    /* PR ld/19628: A specially crafted input file
13436
       can result in a NULL symbol pointer here.  */
13437
479
    if (symbol == NULL)
13438
0
      {
13439
0
        link_info->callbacks->einfo
13440
    /* xgettext:c-format */
13441
0
    (_("%X%P: %pB(%pA): error: relocation for offset %V has no value\n"),
13442
0
     abfd, input_section, (* parent)->address);
13443
0
        goto error_return;
13444
0
      }
13445
13446
    /* Zap reloc field when the symbol is from a discarded
13447
       section, ignoring any addend.  Do the same when called
13448
       from bfd_simple_get_relocated_section_contents for
13449
       undefined symbols in debug sections.  This is to keep
13450
       debug info reasonably sane, in particular so that
13451
       DW_FORM_ref_addr to another file's .debug_info isn't
13452
       confused with an offset into the current file's
13453
       .debug_info.  */
13454
479
    if ((symbol->section != NULL && discarded_section (symbol->section))
13455
479
        || (symbol->section == bfd_und_section_ptr
13456
479
      && (input_section->flags & SEC_DEBUGGING) != 0
13457
479
      && link_info->input_bfds == link_info->output_bfd))
13458
8
      {
13459
8
        bfd_vma off;
13460
8
        static reloc_howto_type none_howto
13461
8
    = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
13462
8
       "unused", false, 0, 0, false);
13463
13464
8
        off = ((*parent)->address
13465
8
         * bfd_octets_per_byte (input_bfd, input_section));
13466
8
        _bfd_clear_contents ((*parent)->howto, input_bfd,
13467
8
           input_section, data, off);
13468
8
        (*parent)->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13469
8
        (*parent)->addend = 0;
13470
8
        (*parent)->howto = &none_howto;
13471
8
        r = bfd_reloc_ok;
13472
8
      }
13473
13474
    /* Specific to MIPS: Deal with relocation types that require
13475
       knowing the gp of the output bfd.  */
13476
13477
    /* If we've managed to find the gp and have a special
13478
       function for the relocation then go ahead, else default
13479
       to the generic handling.  */
13480
471
    else if (gp_found
13481
471
       && ((*parent)->howto->special_function
13482
0
           == _bfd_mips_elf32_gprel16_reloc))
13483
0
      r = _bfd_mips_elf_gprel16_with_gp (input_bfd, symbol, *parent,
13484
0
                 input_section, relocatable,
13485
0
                 data, gp);
13486
471
    else
13487
471
      r = bfd_perform_relocation (input_bfd,
13488
471
          *parent,
13489
471
          data,
13490
471
          input_section,
13491
471
          relocatable ? abfd : NULL,
13492
471
          &error_message);
13493
13494
479
    if (relocatable)
13495
0
      {
13496
0
        asection *os = input_section->output_section;
13497
13498
        /* A partial link, so keep the relocs.  */
13499
0
        os->orelocation[os->reloc_count] = *parent;
13500
0
        os->reloc_count++;
13501
0
      }
13502
13503
479
    if (r != bfd_reloc_ok)
13504
77
      {
13505
77
        switch (r)
13506
77
    {
13507
7
    case bfd_reloc_undefined:
13508
7
      (*link_info->callbacks->undefined_symbol)
13509
7
        (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13510
7
         input_bfd, input_section, (*parent)->address, true);
13511
7
      break;
13512
8
    case bfd_reloc_dangerous:
13513
8
      BFD_ASSERT (error_message != NULL);
13514
8
      (*link_info->callbacks->reloc_dangerous)
13515
8
        (link_info, error_message,
13516
8
         input_bfd, input_section, (*parent)->address);
13517
8
      break;
13518
23
    case bfd_reloc_overflow:
13519
23
      (*link_info->callbacks->reloc_overflow)
13520
23
        (link_info, NULL,
13521
23
         bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13522
23
         (*parent)->howto->name, (*parent)->addend,
13523
23
         input_bfd, input_section, (*parent)->address);
13524
23
      break;
13525
39
    case bfd_reloc_outofrange:
13526
      /* PR ld/13730:
13527
         This error can result when processing some partially
13528
         complete binaries.  Do not abort, but issue an error
13529
         message instead.  */
13530
39
      link_info->callbacks->einfo
13531
        /* xgettext:c-format */
13532
39
        (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13533
39
         abfd, input_section, * parent);
13534
39
      goto error_return;
13535
13536
0
    case bfd_reloc_notsupported:
13537
      /* PR ld/17512
13538
         This error can result when processing a corrupt binary.
13539
         Do not abort.  Issue an error message instead.  */
13540
0
      link_info->callbacks->einfo
13541
        /* xgettext:c-format */
13542
0
        (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
13543
0
         abfd, input_section, * parent);
13544
0
      goto error_return;
13545
13546
0
    default:
13547
      /* PR 17512; file: 90c2a92e.
13548
         Report unexpected results, without aborting.  */
13549
0
      link_info->callbacks->einfo
13550
        /* xgettext:c-format */
13551
0
        (_("%X%P: %pB(%pA): relocation \"%pR\" returns an unrecognized value %x\n"),
13552
0
         abfd, input_section, * parent, r);
13553
0
      break;
13554
77
    }
13555
13556
77
      }
13557
479
  }
13558
53
    }
13559
13560
95
 out:
13561
95
  free (reloc_vector);
13562
95
  return data;
13563
66
}
13564

13565
static bool
13566
mips_elf_relax_delete_bytes (bfd *abfd,
13567
           asection *sec, bfd_vma addr, int count)
13568
0
{
13569
0
  Elf_Internal_Shdr *symtab_hdr;
13570
0
  unsigned int sec_shndx;
13571
0
  bfd_byte *contents;
13572
0
  Elf_Internal_Rela *irel, *irelend;
13573
0
  Elf_Internal_Sym *isym;
13574
0
  Elf_Internal_Sym *isymend;
13575
0
  struct elf_link_hash_entry **sym_hashes;
13576
0
  struct elf_link_hash_entry **end_hashes;
13577
0
  struct elf_link_hash_entry **start_hashes;
13578
0
  unsigned int symcount;
13579
13580
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
13581
0
  contents = elf_section_data (sec)->this_hdr.contents;
13582
13583
0
  irel = elf_section_data (sec)->relocs;
13584
0
  irelend = irel + sec->reloc_count;
13585
13586
  /* Actually delete the bytes.  */
13587
0
  memmove (contents + addr, contents + addr + count,
13588
0
     (size_t) (sec->size - addr - count));
13589
0
  sec->size -= count;
13590
13591
  /* Adjust all the relocs.  */
13592
0
  for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
13593
0
    {
13594
      /* Get the new reloc address.  */
13595
0
      if (irel->r_offset > addr)
13596
0
  irel->r_offset -= count;
13597
0
    }
13598
13599
0
  BFD_ASSERT (addr % 2 == 0);
13600
0
  BFD_ASSERT (count % 2 == 0);
13601
13602
  /* Adjust the local symbols defined in this section.  */
13603
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13604
0
  isym = (Elf_Internal_Sym *) symtab_hdr->contents;
13605
0
  for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
13606
0
    if (isym->st_shndx == sec_shndx && isym->st_value > addr)
13607
0
      isym->st_value -= count;
13608
13609
  /* Now adjust the global symbols defined in this section.  */
13610
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
13611
0
        - symtab_hdr->sh_info);
13612
0
  sym_hashes = start_hashes = elf_sym_hashes (abfd);
13613
0
  end_hashes = sym_hashes + symcount;
13614
13615
0
  for (; sym_hashes < end_hashes; sym_hashes++)
13616
0
    {
13617
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
13618
13619
0
      if ((sym_hash->root.type == bfd_link_hash_defined
13620
0
     || sym_hash->root.type == bfd_link_hash_defweak)
13621
0
    && sym_hash->root.u.def.section == sec)
13622
0
  {
13623
0
    bfd_vma value = sym_hash->root.u.def.value;
13624
13625
0
    if (ELF_ST_IS_MICROMIPS (sym_hash->other))
13626
0
      value &= MINUS_TWO;
13627
0
    if (value > addr)
13628
0
      sym_hash->root.u.def.value -= count;
13629
0
  }
13630
0
    }
13631
13632
0
  return true;
13633
0
}
13634
13635
13636
/* Opcodes needed for microMIPS relaxation as found in
13637
   opcodes/micromips-opc.c.  */
13638
13639
struct opcode_descriptor {
13640
  unsigned long match;
13641
  unsigned long mask;
13642
};
13643
13644
/* The $ra register aka $31.  */
13645
13646
0
#define RA 31
13647
13648
/* 32-bit instruction format register fields.  */
13649
13650
0
#define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
13651
0
#define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
13652
13653
/* Check if a 5-bit register index can be abbreviated to 3 bits.  */
13654
13655
#define OP16_VALID_REG(r) \
13656
0
  ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
13657
13658
13659
/* 32-bit and 16-bit branches.  */
13660
13661
static const struct opcode_descriptor b_insns_32[] = {
13662
  { /* "b", "p",    */ 0x40400000, 0xffff0000 }, /* bgez 0 */
13663
  { /* "b", "p",    */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
13664
  { 0, 0 }  /* End marker for find_match().  */
13665
};
13666
13667
static const struct opcode_descriptor bc_insn_32 =
13668
  { /* "bc(1|2)(ft)", "N,p",  */ 0x42800000, 0xfec30000 };
13669
13670
static const struct opcode_descriptor bz_insn_32 =
13671
  { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
13672
13673
static const struct opcode_descriptor bzal_insn_32 =
13674
  { /* "b(ge|lt)zal", "s,p",  */ 0x40200000, 0xffa00000 };
13675
13676
static const struct opcode_descriptor beq_insn_32 =
13677
  { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
13678
13679
static const struct opcode_descriptor b_insn_16 =
13680
  { /* "b", "mD",   */ 0xcc00,     0xfc00 };
13681
13682
static const struct opcode_descriptor bz_insn_16 =
13683
  { /* "b(eq|ne)z", "md,mE",  */ 0x8c00,     0xdc00 };
13684
13685
13686
/* 32-bit and 16-bit branch EQ and NE zero.  */
13687
13688
/* NOTE: All opcode tables have BEQ/BNE in the same order: first the
13689
   eq and second the ne.  This convention is used when replacing a
13690
   32-bit BEQ/BNE with the 16-bit version.  */
13691
13692
0
#define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
13693
13694
static const struct opcode_descriptor bz_rs_insns_32[] = {
13695
  { /* "beqz",  "s,p",    */ 0x94000000, 0xffe00000 },
13696
  { /* "bnez",  "s,p",    */ 0xb4000000, 0xffe00000 },
13697
  { 0, 0 }  /* End marker for find_match().  */
13698
};
13699
13700
static const struct opcode_descriptor bz_rt_insns_32[] = {
13701
  { /* "beqz",  "t,p",    */ 0x94000000, 0xfc01f000 },
13702
  { /* "bnez",  "t,p",    */ 0xb4000000, 0xfc01f000 },
13703
  { 0, 0 }  /* End marker for find_match().  */
13704
};
13705
13706
static const struct opcode_descriptor bzc_insns_32[] = {
13707
  { /* "beqzc", "s,p",    */ 0x40e00000, 0xffe00000 },
13708
  { /* "bnezc", "s,p",    */ 0x40a00000, 0xffe00000 },
13709
  { 0, 0 }  /* End marker for find_match().  */
13710
};
13711
13712
static const struct opcode_descriptor bz_insns_16[] = {
13713
  { /* "beqz",  "md,mE",  */ 0x8c00,     0xfc00 },
13714
  { /* "bnez",  "md,mE",  */ 0xac00,     0xfc00 },
13715
  { 0, 0 }  /* End marker for find_match().  */
13716
};
13717
13718
/* Switch between a 5-bit register index and its 3-bit shorthand.  */
13719
13720
0
#define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
13721
#define BZ16_REG_FIELD(r) (((r) & 7) << 7)
13722
13723
13724
/* 32-bit instructions with a delay slot.  */
13725
13726
static const struct opcode_descriptor jal_insn_32_bd16 =
13727
  { /* "jals",  "a",    */ 0x74000000, 0xfc000000 };
13728
13729
static const struct opcode_descriptor jal_insn_32_bd32 =
13730
  { /* "jal", "a",    */ 0xf4000000, 0xfc000000 };
13731
13732
static const struct opcode_descriptor jal_x_insn_32_bd32 =
13733
  { /* "jal[x]", "a",   */ 0xf0000000, 0xf8000000 };
13734
13735
static const struct opcode_descriptor j_insn_32 =
13736
  { /* "j", "a",    */ 0xd4000000, 0xfc000000 };
13737
13738
static const struct opcode_descriptor jalr_insn_32 =
13739
  { /* "jalr[.hb]", "t,s",  */ 0x00000f3c, 0xfc00efff };
13740
13741
/* This table can be compacted, because no opcode replacement is made.  */
13742
13743
static const struct opcode_descriptor ds_insns_32_bd16[] = {
13744
  { /* "jals",  "a",    */ 0x74000000, 0xfc000000 },
13745
13746
  { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
13747
  { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
13748
13749
  { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
13750
  { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
13751
  { /* "j", "a",    */ 0xd4000000, 0xfc000000 },
13752
  { 0, 0 }  /* End marker for find_match().  */
13753
};
13754
13755
/* This table can be compacted, because no opcode replacement is made.  */
13756
13757
static const struct opcode_descriptor ds_insns_32_bd32[] = {
13758
  { /* "jal[x]", "a",   */ 0xf0000000, 0xf8000000 },
13759
13760
  { /* "jalr[.hb]", "t,s",  */ 0x00000f3c, 0xfc00efff },
13761
  { /* "b(ge|lt)zal", "s,p",  */ 0x40200000, 0xffa00000 },
13762
  { 0, 0 }  /* End marker for find_match().  */
13763
};
13764
13765
13766
/* 16-bit instructions with a delay slot.  */
13767
13768
static const struct opcode_descriptor jalr_insn_16_bd16 =
13769
  { /* "jalrs", "my,mj",  */ 0x45e0,     0xffe0 };
13770
13771
static const struct opcode_descriptor jalr_insn_16_bd32 =
13772
  { /* "jalr",  "my,mj",  */ 0x45c0,     0xffe0 };
13773
13774
static const struct opcode_descriptor jr_insn_16 =
13775
  { /* "jr",  "mj",   */ 0x4580,     0xffe0 };
13776
13777
0
#define JR16_REG(opcode) ((opcode) & 0x1f)
13778
13779
/* This table can be compacted, because no opcode replacement is made.  */
13780
13781
static const struct opcode_descriptor ds_insns_16_bd16[] = {
13782
  { /* "jalrs", "my,mj",  */ 0x45e0,     0xffe0 },
13783
13784
  { /* "b", "mD",   */ 0xcc00,     0xfc00 },
13785
  { /* "b(eq|ne)z", "md,mE",  */ 0x8c00,     0xdc00 },
13786
  { /* "jr",  "mj",   */ 0x4580,     0xffe0 },
13787
  { 0, 0 }  /* End marker for find_match().  */
13788
};
13789
13790
13791
/* LUI instruction.  */
13792
13793
static const struct opcode_descriptor lui_insn =
13794
 { /* "lui",  "s,u",    */ 0x41a00000, 0xffe00000 };
13795
13796
13797
/* ADDIU instruction.  */
13798
13799
static const struct opcode_descriptor addiu_insn =
13800
  { /* "addiu", "t,r,j",  */ 0x30000000, 0xfc000000 };
13801
13802
static const struct opcode_descriptor addiupc_insn =
13803
  { /* "addiu", "mb,$pc,mQ",  */ 0x78000000, 0xfc000000 };
13804
13805
#define ADDIUPC_REG_FIELD(r) \
13806
0
  (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13807
13808
13809
/* Relaxable instructions in a JAL delay slot: MOVE.  */
13810
13811
/* The 16-bit move has rd in 9:5 and rs in 4:0.  The 32-bit moves
13812
   (ADDU, OR) have rd in 15:11 and rs in 10:16.  */
13813
#define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13814
#define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13815
13816
#define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13817
#define MOVE16_RS_FIELD(r) (((r) & 0x1f)     )
13818
13819
static const struct opcode_descriptor move_insns_32[] = {
13820
  { /* "move",  "d,s",    */ 0x00000290, 0xffe007ff }, /* or   d,s,$0 */
13821
  { /* "move",  "d,s",    */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
13822
  { 0, 0 }  /* End marker for find_match().  */
13823
};
13824
13825
static const struct opcode_descriptor move_insn_16 =
13826
  { /* "move",  "mp,mj",  */ 0x0c00,     0xfc00 };
13827
13828
13829
/* NOP instructions.  */
13830
13831
static const struct opcode_descriptor nop_insn_32 =
13832
  { /* "nop", "",   */ 0x00000000, 0xffffffff };
13833
13834
static const struct opcode_descriptor nop_insn_16 =
13835
  { /* "nop", "",   */ 0x0c00,     0xffff };
13836
13837
13838
/* Instruction match support.  */
13839
13840
0
#define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13841
13842
static int
13843
find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13844
0
{
13845
0
  unsigned long indx;
13846
13847
0
  for (indx = 0; insn[indx].mask != 0; indx++)
13848
0
    if (MATCH (opcode, insn[indx]))
13849
0
      return indx;
13850
13851
0
  return -1;
13852
0
}
13853
13854
13855
/* Branch and delay slot decoding support.  */
13856
13857
/* If PTR points to what *might* be a 16-bit branch or jump, then
13858
   return the minimum length of its delay slot, otherwise return 0.
13859
   Non-zero results are not definitive as we might be checking against
13860
   the second half of another instruction.  */
13861
13862
static int
13863
check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13864
0
{
13865
0
  unsigned long opcode;
13866
0
  int bdsize;
13867
13868
0
  opcode = bfd_get_16 (abfd, ptr);
13869
0
  if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13870
    /* 16-bit branch/jump with a 32-bit delay slot.  */
13871
0
    bdsize = 4;
13872
0
  else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13873
0
     || find_match (opcode, ds_insns_16_bd16) >= 0)
13874
    /* 16-bit branch/jump with a 16-bit delay slot.  */
13875
0
    bdsize = 2;
13876
0
  else
13877
    /* No delay slot.  */
13878
0
    bdsize = 0;
13879
13880
0
  return bdsize;
13881
0
}
13882
13883
/* If PTR points to what *might* be a 32-bit branch or jump, then
13884
   return the minimum length of its delay slot, otherwise return 0.
13885
   Non-zero results are not definitive as we might be checking against
13886
   the second half of another instruction.  */
13887
13888
static int
13889
check_br32_dslot (bfd *abfd, bfd_byte *ptr)
13890
0
{
13891
0
  unsigned long opcode;
13892
0
  int bdsize;
13893
13894
0
  opcode = bfd_get_micromips_32 (abfd, ptr);
13895
0
  if (find_match (opcode, ds_insns_32_bd32) >= 0)
13896
    /* 32-bit branch/jump with a 32-bit delay slot.  */
13897
0
    bdsize = 4;
13898
0
  else if (find_match (opcode, ds_insns_32_bd16) >= 0)
13899
    /* 32-bit branch/jump with a 16-bit delay slot.  */
13900
0
    bdsize = 2;
13901
0
  else
13902
    /* No delay slot.  */
13903
0
    bdsize = 0;
13904
13905
0
  return bdsize;
13906
0
}
13907
13908
/* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
13909
   that doesn't fiddle with REG, then return TRUE, otherwise FALSE.  */
13910
13911
static bool
13912
check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13913
0
{
13914
0
  unsigned long opcode;
13915
13916
0
  opcode = bfd_get_16 (abfd, ptr);
13917
0
  if (MATCH (opcode, b_insn_16)
13918
            /* B16  */
13919
0
      || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
13920
            /* JR16  */
13921
0
      || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
13922
            /* BEQZ16, BNEZ16  */
13923
0
      || (MATCH (opcode, jalr_insn_16_bd32)
13924
            /* JALR16  */
13925
0
    && reg != JR16_REG (opcode) && reg != RA))
13926
0
    return true;
13927
13928
0
  return false;
13929
0
}
13930
13931
/* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
13932
   then return TRUE, otherwise FALSE.  */
13933
13934
static bool
13935
check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13936
0
{
13937
0
  unsigned long opcode;
13938
13939
0
  opcode = bfd_get_micromips_32 (abfd, ptr);
13940
0
  if (MATCH (opcode, j_insn_32)
13941
            /* J  */
13942
0
      || MATCH (opcode, bc_insn_32)
13943
            /* BC1F, BC1T, BC2F, BC2T  */
13944
0
      || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13945
            /* JAL, JALX  */
13946
0
      || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13947
            /* BGEZ, BGTZ, BLEZ, BLTZ  */
13948
0
      || (MATCH (opcode, bzal_insn_32)
13949
            /* BGEZAL, BLTZAL  */
13950
0
    && reg != OP32_SREG (opcode) && reg != RA)
13951
0
      || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13952
            /* JALR, JALR.HB, BEQ, BNE  */
13953
0
    && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13954
0
    return true;
13955
13956
0
  return false;
13957
0
}
13958
13959
/* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13960
   IRELEND) at OFFSET indicate that there must be a compact branch there,
13961
   then return TRUE, otherwise FALSE.  */
13962
13963
static bool
13964
check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13965
         const Elf_Internal_Rela *internal_relocs,
13966
         const Elf_Internal_Rela *irelend)
13967
0
{
13968
0
  const Elf_Internal_Rela *irel;
13969
0
  unsigned long opcode;
13970
13971
0
  opcode = bfd_get_micromips_32 (abfd, ptr);
13972
0
  if (find_match (opcode, bzc_insns_32) < 0)
13973
0
    return false;
13974
13975
0
  for (irel = internal_relocs; irel < irelend; irel++)
13976
0
    if (irel->r_offset == offset
13977
0
  && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13978
0
      return true;
13979
13980
0
  return false;
13981
0
}
13982
13983
/* Bitsize checking.  */
13984
#define IS_BITSIZE(val, N)            \
13985
0
  (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1)))   \
13986
0
    - (1ULL << ((N) - 1))) == (val))
13987
13988

13989
bool
13990
_bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13991
           struct bfd_link_info *link_info,
13992
           bool *again)
13993
0
{
13994
0
  bool insn32 = mips_elf_hash_table (link_info)->insn32;
13995
0
  Elf_Internal_Shdr *symtab_hdr;
13996
0
  Elf_Internal_Rela *internal_relocs;
13997
0
  Elf_Internal_Rela *irel, *irelend;
13998
0
  bfd_byte *contents = NULL;
13999
0
  Elf_Internal_Sym *isymbuf = NULL;
14000
14001
  /* Assume nothing changes.  */
14002
0
  *again = false;
14003
14004
  /* We don't have to do anything for a relocatable link, if
14005
     this section does not have relocs, or if this is not a
14006
     code section.  */
14007
14008
0
  if (bfd_link_relocatable (link_info)
14009
0
      || sec->reloc_count == 0
14010
0
      || (sec->flags & SEC_RELOC) == 0
14011
0
      || (sec->flags & SEC_HAS_CONTENTS) == 0
14012
0
      || (sec->flags & SEC_CODE) == 0)
14013
0
    return true;
14014
14015
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
14016
14017
  /* Get a copy of the native relocations.  */
14018
0
  internal_relocs = (_bfd_elf_link_read_relocs
14019
0
         (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
14020
0
          link_info->keep_memory));
14021
0
  if (internal_relocs == NULL)
14022
0
    goto error_return;
14023
14024
  /* Walk through them looking for relaxing opportunities.  */
14025
0
  irelend = internal_relocs + sec->reloc_count;
14026
0
  for (irel = internal_relocs; irel < irelend; irel++)
14027
0
    {
14028
0
      unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
14029
0
      unsigned int r_type = ELF32_R_TYPE (irel->r_info);
14030
0
      bool target_is_micromips_code_p;
14031
0
      unsigned long opcode;
14032
0
      bfd_vma symval;
14033
0
      bfd_vma pcrval;
14034
0
      bfd_byte *ptr;
14035
0
      int fndopc;
14036
14037
      /* The number of bytes to delete for relaxation and from where
14038
   to delete these bytes starting at irel->r_offset.  */
14039
0
      int delcnt = 0;
14040
0
      int deloff = 0;
14041
14042
      /* If this isn't something that can be relaxed, then ignore
14043
   this reloc.  */
14044
0
      if (r_type != R_MICROMIPS_HI16
14045
0
    && r_type != R_MICROMIPS_PC16_S1
14046
0
    && r_type != R_MICROMIPS_26_S1)
14047
0
  continue;
14048
14049
      /* Get the section contents if we haven't done so already.  */
14050
0
      if (contents == NULL)
14051
0
  {
14052
    /* Get cached copy if it exists.  */
14053
0
    if (elf_section_data (sec)->this_hdr.contents != NULL)
14054
0
      contents = elf_section_data (sec)->this_hdr.contents;
14055
    /* Go get them off disk.  */
14056
0
    else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
14057
0
      goto error_return;
14058
0
  }
14059
0
      ptr = contents + irel->r_offset;
14060
14061
      /* Read this BFD's local symbols if we haven't done so already.  */
14062
0
      if (isymbuf == NULL && symtab_hdr->sh_info != 0)
14063
0
  {
14064
0
    isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
14065
0
    if (isymbuf == NULL)
14066
0
      isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
14067
0
              symtab_hdr->sh_info, 0,
14068
0
              NULL, NULL, NULL);
14069
0
    if (isymbuf == NULL)
14070
0
      goto error_return;
14071
0
  }
14072
14073
      /* Get the value of the symbol referred to by the reloc.  */
14074
0
      if (r_symndx < symtab_hdr->sh_info)
14075
0
  {
14076
    /* A local symbol.  */
14077
0
    Elf_Internal_Sym *isym;
14078
0
    asection *sym_sec;
14079
14080
0
    isym = isymbuf + r_symndx;
14081
0
    if (isym->st_shndx == SHN_UNDEF)
14082
0
      sym_sec = bfd_und_section_ptr;
14083
0
    else if (isym->st_shndx == SHN_ABS)
14084
0
      sym_sec = bfd_abs_section_ptr;
14085
0
    else if (isym->st_shndx == SHN_COMMON)
14086
0
      sym_sec = bfd_com_section_ptr;
14087
0
    else
14088
0
      sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
14089
0
    symval = (isym->st_value
14090
0
        + sym_sec->output_section->vma
14091
0
        + sym_sec->output_offset);
14092
0
    target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
14093
0
  }
14094
0
      else
14095
0
  {
14096
0
    unsigned long indx;
14097
0
    struct elf_link_hash_entry *h;
14098
14099
    /* An external symbol.  */
14100
0
    indx = r_symndx - symtab_hdr->sh_info;
14101
0
    h = elf_sym_hashes (abfd)[indx];
14102
0
    BFD_ASSERT (h != NULL);
14103
14104
0
    if (h->root.type != bfd_link_hash_defined
14105
0
        && h->root.type != bfd_link_hash_defweak)
14106
      /* This appears to be a reference to an undefined
14107
         symbol.  Just ignore it -- it will be caught by the
14108
         regular reloc processing.  */
14109
0
      continue;
14110
14111
0
    symval = (h->root.u.def.value
14112
0
        + h->root.u.def.section->output_section->vma
14113
0
        + h->root.u.def.section->output_offset);
14114
0
    target_is_micromips_code_p = (!h->needs_plt
14115
0
          && ELF_ST_IS_MICROMIPS (h->other));
14116
0
  }
14117
14118
14119
      /* For simplicity of coding, we are going to modify the
14120
   section contents, the section relocs, and the BFD symbol
14121
   table.  We must tell the rest of the code not to free up this
14122
   information.  It would be possible to instead create a table
14123
   of changes which have to be made, as is done in coff-mips.c;
14124
   that would be more work, but would require less memory when
14125
   the linker is run.  */
14126
14127
      /* Only 32-bit instructions relaxed.  */
14128
0
      if (irel->r_offset + 4 > sec->size)
14129
0
  continue;
14130
14131
0
      opcode = bfd_get_micromips_32 (abfd, ptr);
14132
14133
      /* This is the pc-relative distance from the instruction the
14134
   relocation is applied to, to the symbol referred.  */
14135
0
      pcrval = (symval
14136
0
    - (sec->output_section->vma + sec->output_offset)
14137
0
    - irel->r_offset);
14138
14139
      /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
14140
   of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
14141
   R_MICROMIPS_PC23_S2.  The R_MICROMIPS_PC23_S2 condition is
14142
14143
     (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
14144
14145
   where pcrval has first to be adjusted to apply against the LO16
14146
   location (we make the adjustment later on, when we have figured
14147
   out the offset).  */
14148
0
      if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
14149
0
  {
14150
0
    bool bzc = false;
14151
0
    unsigned long nextopc;
14152
0
    unsigned long reg;
14153
0
    bfd_vma offset;
14154
14155
    /* Give up if the previous reloc was a HI16 against this symbol
14156
       too.  */
14157
0
    if (irel > internal_relocs
14158
0
        && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
14159
0
        && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
14160
0
      continue;
14161
14162
    /* Or if the next reloc is not a LO16 against this symbol.  */
14163
0
    if (irel + 1 >= irelend
14164
0
        || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
14165
0
        || ELF32_R_SYM (irel[1].r_info) != r_symndx)
14166
0
      continue;
14167
14168
    /* Or if the second next reloc is a LO16 against this symbol too.  */
14169
0
    if (irel + 2 >= irelend
14170
0
        && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
14171
0
        && ELF32_R_SYM (irel[2].r_info) == r_symndx)
14172
0
      continue;
14173
14174
    /* See if the LUI instruction *might* be in a branch delay slot.
14175
       We check whether what looks like a 16-bit branch or jump is
14176
       actually an immediate argument to a compact branch, and let
14177
       it through if so.  */
14178
0
    if (irel->r_offset >= 2
14179
0
        && check_br16_dslot (abfd, ptr - 2)
14180
0
        && !(irel->r_offset >= 4
14181
0
       && (bzc = check_relocated_bzc (abfd,
14182
0
              ptr - 4, irel->r_offset - 4,
14183
0
              internal_relocs, irelend))))
14184
0
      continue;
14185
0
    if (irel->r_offset >= 4
14186
0
        && !bzc
14187
0
        && check_br32_dslot (abfd, ptr - 4))
14188
0
      continue;
14189
14190
0
    reg = OP32_SREG (opcode);
14191
14192
    /* We only relax adjacent instructions or ones separated with
14193
       a branch or jump that has a delay slot.  The branch or jump
14194
       must not fiddle with the register used to hold the address.
14195
       Subtract 4 for the LUI itself.  */
14196
0
    offset = irel[1].r_offset - irel[0].r_offset;
14197
0
    switch (offset - 4)
14198
0
      {
14199
0
      case 0:
14200
0
        break;
14201
0
      case 2:
14202
0
        if (check_br16 (abfd, ptr + 4, reg))
14203
0
    break;
14204
0
        continue;
14205
0
      case 4:
14206
0
        if (check_br32 (abfd, ptr + 4, reg))
14207
0
    break;
14208
0
        continue;
14209
0
      default:
14210
0
        continue;
14211
0
      }
14212
14213
0
    nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
14214
14215
    /* Give up unless the same register is used with both
14216
       relocations.  */
14217
0
    if (OP32_SREG (nextopc) != reg)
14218
0
      continue;
14219
14220
    /* Now adjust pcrval, subtracting the offset to the LO16 reloc
14221
       and rounding up to take masking of the two LSBs into account.  */
14222
0
    pcrval = ((pcrval - offset + 3) | 3) ^ 3;
14223
14224
    /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16.  */
14225
0
    if (IS_BITSIZE (symval, 16))
14226
0
      {
14227
        /* Fix the relocation's type.  */
14228
0
        irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
14229
14230
        /* Instructions using R_MICROMIPS_LO16 have the base or
14231
     source register in bits 20:16.  This register becomes $0
14232
     (zero) as the result of the R_MICROMIPS_HI16 being 0.  */
14233
0
        nextopc &= ~0x001f0000;
14234
0
        bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
14235
0
        contents + irel[1].r_offset);
14236
0
      }
14237
14238
    /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
14239
       We add 4 to take LUI deletion into account while checking
14240
       the PC-relative distance.  */
14241
0
    else if (symval % 4 == 0
14242
0
       && IS_BITSIZE (pcrval + 4, 25)
14243
0
       && MATCH (nextopc, addiu_insn)
14244
0
       && OP32_TREG (nextopc) == OP32_SREG (nextopc)
14245
0
       && OP16_VALID_REG (OP32_TREG (nextopc)))
14246
0
      {
14247
        /* Fix the relocation's type.  */
14248
0
        irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
14249
14250
        /* Replace ADDIU with the ADDIUPC version.  */
14251
0
        nextopc = (addiupc_insn.match
14252
0
       | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
14253
14254
0
        bfd_put_micromips_32 (abfd, nextopc,
14255
0
            contents + irel[1].r_offset);
14256
0
      }
14257
14258
    /* Can't do anything, give up, sigh...  */
14259
0
    else
14260
0
      continue;
14261
14262
    /* Fix the relocation's type.  */
14263
0
    irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
14264
14265
    /* Delete the LUI instruction: 4 bytes at irel->r_offset.  */
14266
0
    delcnt = 4;
14267
0
    deloff = 0;
14268
0
  }
14269
14270
      /* Compact branch relaxation -- due to the multitude of macros
14271
   employed by the compiler/assembler, compact branches are not
14272
   always generated.  Obviously, this can/will be fixed elsewhere,
14273
   but there is no drawback in double checking it here.  */
14274
0
      else if (r_type == R_MICROMIPS_PC16_S1
14275
0
         && irel->r_offset + 5 < sec->size
14276
0
         && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14277
0
       || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
14278
0
         && ((!insn32
14279
0
        && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
14280
0
          nop_insn_16) ? 2 : 0))
14281
0
       || (irel->r_offset + 7 < sec->size
14282
0
           && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
14283
0
                 ptr + 4),
14284
0
             nop_insn_32) ? 4 : 0))))
14285
0
  {
14286
0
    unsigned long reg;
14287
14288
0
    reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14289
14290
    /* Replace BEQZ/BNEZ with the compact version.  */
14291
0
    opcode = (bzc_insns_32[fndopc].match
14292
0
        | BZC32_REG_FIELD (reg)
14293
0
        | (opcode & 0xffff));   /* Addend value.  */
14294
14295
0
    bfd_put_micromips_32 (abfd, opcode, ptr);
14296
14297
    /* Delete the delay slot NOP: two or four bytes from
14298
       irel->offset + 4; delcnt has already been set above.  */
14299
0
    deloff = 4;
14300
0
  }
14301
14302
      /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1.  We need
14303
   to check the distance from the next instruction, so subtract 2.  */
14304
0
      else if (!insn32
14305
0
         && r_type == R_MICROMIPS_PC16_S1
14306
0
         && IS_BITSIZE (pcrval - 2, 11)
14307
0
         && find_match (opcode, b_insns_32) >= 0)
14308
0
  {
14309
    /* Fix the relocation's type.  */
14310
0
    irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
14311
14312
    /* Replace the 32-bit opcode with a 16-bit opcode.  */
14313
0
    bfd_put_16 (abfd,
14314
0
          (b_insn_16.match
14315
0
           | (opcode & 0x3ff)),   /* Addend value.  */
14316
0
          ptr);
14317
14318
    /* Delete 2 bytes from irel->r_offset + 2.  */
14319
0
    delcnt = 2;
14320
0
    deloff = 2;
14321
0
  }
14322
14323
      /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1.  We need
14324
   to check the distance from the next instruction, so subtract 2.  */
14325
0
      else if (!insn32
14326
0
         && r_type == R_MICROMIPS_PC16_S1
14327
0
         && IS_BITSIZE (pcrval - 2, 8)
14328
0
         && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14329
0
        && OP16_VALID_REG (OP32_SREG (opcode)))
14330
0
       || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
14331
0
           && OP16_VALID_REG (OP32_TREG (opcode)))))
14332
0
  {
14333
0
    unsigned long reg;
14334
14335
0
    reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14336
14337
    /* Fix the relocation's type.  */
14338
0
    irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
14339
14340
    /* Replace the 32-bit opcode with a 16-bit opcode.  */
14341
0
    bfd_put_16 (abfd,
14342
0
          (bz_insns_16[fndopc].match
14343
0
           | BZ16_REG_FIELD (reg)
14344
0
           | (opcode & 0x7f)),    /* Addend value.  */
14345
0
          ptr);
14346
14347
    /* Delete 2 bytes from irel->r_offset + 2.  */
14348
0
    delcnt = 2;
14349
0
    deloff = 2;
14350
0
  }
14351
14352
      /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets.  */
14353
0
      else if (!insn32
14354
0
         && r_type == R_MICROMIPS_26_S1
14355
0
         && target_is_micromips_code_p
14356
0
         && irel->r_offset + 7 < sec->size
14357
0
         && MATCH (opcode, jal_insn_32_bd32))
14358
0
  {
14359
0
    unsigned long n32opc;
14360
0
    bool relaxed = false;
14361
14362
0
    n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
14363
14364
0
    if (MATCH (n32opc, nop_insn_32))
14365
0
      {
14366
        /* Replace delay slot 32-bit NOP with a 16-bit NOP.  */
14367
0
        bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
14368
14369
0
        relaxed = true;
14370
0
      }
14371
0
    else if (find_match (n32opc, move_insns_32) >= 0)
14372
0
      {
14373
        /* Replace delay slot 32-bit MOVE with 16-bit MOVE.  */
14374
0
        bfd_put_16 (abfd,
14375
0
        (move_insn_16.match
14376
0
         | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
14377
0
         | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
14378
0
        ptr + 4);
14379
14380
0
        relaxed = true;
14381
0
      }
14382
    /* Other 32-bit instructions relaxable to 16-bit
14383
       instructions will be handled here later.  */
14384
14385
0
    if (relaxed)
14386
0
      {
14387
        /* JAL with 32-bit delay slot that is changed to a JALS
14388
     with 16-bit delay slot.  */
14389
0
        bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
14390
14391
        /* Delete 2 bytes from irel->r_offset + 6.  */
14392
0
        delcnt = 2;
14393
0
        deloff = 6;
14394
0
      }
14395
0
  }
14396
14397
0
      if (delcnt != 0)
14398
0
  {
14399
    /* Note that we've changed the relocs, section contents, etc.  */
14400
0
    elf_section_data (sec)->relocs = internal_relocs;
14401
0
    elf_section_data (sec)->this_hdr.contents = contents;
14402
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
14403
14404
    /* Delete bytes depending on the delcnt and deloff.  */
14405
0
    if (!mips_elf_relax_delete_bytes (abfd, sec,
14406
0
              irel->r_offset + deloff, delcnt))
14407
0
      goto error_return;
14408
14409
    /* That will change things, so we should relax again.
14410
       Note that this is not required, and it may be slow.  */
14411
0
    *again = true;
14412
0
  }
14413
0
    }
14414
14415
0
  if (isymbuf != NULL
14416
0
      && symtab_hdr->contents != (unsigned char *) isymbuf)
14417
0
    {
14418
0
      if (! link_info->keep_memory)
14419
0
  free (isymbuf);
14420
0
      else
14421
0
  {
14422
    /* Cache the symbols for elf_link_input_bfd.  */
14423
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
14424
0
  }
14425
0
    }
14426
14427
0
  if (contents != NULL
14428
0
      && elf_section_data (sec)->this_hdr.contents != contents)
14429
0
    {
14430
0
      if (! link_info->keep_memory)
14431
0
  free (contents);
14432
0
      else
14433
0
  {
14434
    /* Cache the section contents for elf_link_input_bfd.  */
14435
0
    elf_section_data (sec)->this_hdr.contents = contents;
14436
0
  }
14437
0
    }
14438
14439
0
  if (elf_section_data (sec)->relocs != internal_relocs)
14440
0
    free (internal_relocs);
14441
14442
0
  return true;
14443
14444
0
 error_return:
14445
0
  if (symtab_hdr->contents != (unsigned char *) isymbuf)
14446
0
    free (isymbuf);
14447
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
14448
0
    free (contents);
14449
0
  if (elf_section_data (sec)->relocs != internal_relocs)
14450
0
    free (internal_relocs);
14451
14452
0
  return false;
14453
0
}
14454

14455
/* Create a MIPS ELF linker hash table.  */
14456
14457
struct bfd_link_hash_table *
14458
_bfd_mips_elf_link_hash_table_create (bfd *abfd)
14459
0
{
14460
0
  struct mips_elf_link_hash_table *ret;
14461
0
  size_t amt = sizeof (struct mips_elf_link_hash_table);
14462
14463
0
  ret = bfd_zmalloc (amt);
14464
0
  if (ret == NULL)
14465
0
    return NULL;
14466
14467
0
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
14468
0
              mips_elf_link_hash_newfunc,
14469
0
              sizeof (struct mips_elf_link_hash_entry)))
14470
0
    {
14471
0
      free (ret);
14472
0
      return NULL;
14473
0
    }
14474
0
  ret->root.init_plt_refcount.plist = NULL;
14475
0
  ret->root.init_plt_offset.plist = NULL;
14476
14477
0
  return &ret->root.root;
14478
0
}
14479
14480
/* Likewise, but indicate that the target is VxWorks.  */
14481
14482
struct bfd_link_hash_table *
14483
_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
14484
0
{
14485
0
  struct bfd_link_hash_table *ret;
14486
14487
0
  ret = _bfd_mips_elf_link_hash_table_create (abfd);
14488
0
  if (ret)
14489
0
    {
14490
0
      struct mips_elf_link_hash_table *htab;
14491
14492
0
      htab = (struct mips_elf_link_hash_table *) ret;
14493
0
      htab->use_plts_and_copy_relocs = true;
14494
0
    }
14495
0
  return ret;
14496
0
}
14497
14498
/* A function that the linker calls if we are allowed to use PLTs
14499
   and copy relocs.  */
14500
14501
void
14502
_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
14503
0
{
14504
0
  mips_elf_hash_table (info)->use_plts_and_copy_relocs = true;
14505
0
}
14506
14507
/* A function that the linker calls to select between all or only
14508
   32-bit microMIPS instructions, and between making or ignoring
14509
   branch relocation checks for invalid transitions between ISA modes.
14510
   Also record whether we have been configured for a GNU target.  */
14511
14512
void
14513
_bfd_mips_elf_linker_flags (struct bfd_link_info *info, bool insn32,
14514
          bool ignore_branch_isa,
14515
          bool gnu_target)
14516
0
{
14517
0
  mips_elf_hash_table (info)->insn32 = insn32;
14518
0
  mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
14519
0
  mips_elf_hash_table (info)->gnu_target = gnu_target;
14520
0
}
14521
14522
/* A function that the linker calls to enable use of compact branches in
14523
   linker generated code for MIPSR6.  */
14524
14525
void
14526
_bfd_mips_elf_compact_branches (struct bfd_link_info *info, bool on)
14527
0
{
14528
0
  mips_elf_hash_table (info)->compact_branches = on;
14529
0
}
14530
14531

14532
/* Structure for saying that BFD machine EXTENSION extends BASE.  */
14533
14534
struct mips_mach_extension
14535
{
14536
  unsigned long extension, base;
14537
};
14538
14539
/* An array that maps 64-bit architectures to the corresponding 32-bit
14540
   architectures.  */
14541
static const struct mips_mach_extension mips_mach_32_64[] =
14542
{
14543
  { bfd_mach_mipsisa64r6, bfd_mach_mipsisa32r6 },
14544
  { bfd_mach_mipsisa64r5, bfd_mach_mipsisa32r5 },
14545
  { bfd_mach_mipsisa64r3, bfd_mach_mipsisa32r3 },
14546
  { bfd_mach_mipsisa64r2, bfd_mach_mipsisa32r2 },
14547
  { bfd_mach_mipsisa64,   bfd_mach_mipsisa32 }
14548
};
14549
14550
/* An array describing how BFD machines relate to one another.  The entries
14551
   are ordered topologically with MIPS I extensions listed last.  */
14552
14553
static const struct mips_mach_extension mips_mach_extensions[] =
14554
{
14555
  /* MIPS64r2 extensions.  */
14556
  { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
14557
  { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14558
  { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14559
  { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14560
  { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
14561
  { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
14562
  { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
14563
14564
  /* MIPS64 extensions.  */
14565
  { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14566
  { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14567
  { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14568
14569
  /* MIPS V extensions.  */
14570
  { bfd_mach_mipsisa64, bfd_mach_mips5 },
14571
14572
  /* R10000 extensions.  */
14573
  { bfd_mach_mips12000, bfd_mach_mips10000 },
14574
  { bfd_mach_mips14000, bfd_mach_mips10000 },
14575
  { bfd_mach_mips16000, bfd_mach_mips10000 },
14576
14577
  /* R5000 extensions.  Note: the vr5500 ISA is an extension of the core
14578
     vr5400 ISA, but doesn't include the multimedia stuff.  It seems
14579
     better to allow vr5400 and vr5500 code to be merged anyway, since
14580
     many libraries will just use the core ISA.  Perhaps we could add
14581
     some sort of ASE flag if this ever proves a problem.  */
14582
  { bfd_mach_mips5500, bfd_mach_mips5400 },
14583
  { bfd_mach_mips5400, bfd_mach_mips5000 },
14584
14585
  /* MIPS IV extensions.  */
14586
  { bfd_mach_mips5, bfd_mach_mips8000 },
14587
  { bfd_mach_mips10000, bfd_mach_mips8000 },
14588
  { bfd_mach_mips5000, bfd_mach_mips8000 },
14589
  { bfd_mach_mips7000, bfd_mach_mips8000 },
14590
  { bfd_mach_mips9000, bfd_mach_mips8000 },
14591
14592
  /* VR4100 extensions.  */
14593
  { bfd_mach_mips4120, bfd_mach_mips4100 },
14594
  { bfd_mach_mips4111, bfd_mach_mips4100 },
14595
14596
  /* MIPS III extensions.  */
14597
  { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14598
  { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14599
  { bfd_mach_mips8000, bfd_mach_mips4000 },
14600
  { bfd_mach_mips4650, bfd_mach_mips4000 },
14601
  { bfd_mach_mips4600, bfd_mach_mips4000 },
14602
  { bfd_mach_mips4400, bfd_mach_mips4000 },
14603
  { bfd_mach_mips4300, bfd_mach_mips4000 },
14604
  { bfd_mach_mips4100, bfd_mach_mips4000 },
14605
  { bfd_mach_mips5900, bfd_mach_mips4000 },
14606
14607
  /* MIPS32r3 extensions.  */
14608
  { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
14609
14610
  /* MIPS32r2 extensions.  */
14611
  { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
14612
14613
  /* MIPS32 extensions.  */
14614
  { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14615
14616
  /* MIPS II extensions.  */
14617
  { bfd_mach_mips4000, bfd_mach_mips6000 },
14618
  { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14619
  { bfd_mach_mips4010, bfd_mach_mips6000 },
14620
  { bfd_mach_mips_allegrex, bfd_mach_mips6000 },
14621
14622
  /* MIPS I extensions.  */
14623
  { bfd_mach_mips6000, bfd_mach_mips3000 },
14624
  { bfd_mach_mips3900, bfd_mach_mips3000 }
14625
};
14626
14627
/* Return true if bfd machine EXTENSION is the same as BASE, or if
14628
   EXTENSION is the 64-bit equivalent of a 32-bit BASE.  */
14629
14630
static bool
14631
mips_mach_extends_32_64 (unsigned long base, unsigned long extension)
14632
0
{
14633
0
  size_t i;
14634
14635
0
  if (extension == base)
14636
0
    return true;
14637
14638
0
  for (i = 0; i < ARRAY_SIZE (mips_mach_32_64); i++)
14639
0
    if (extension == mips_mach_32_64[i].extension)
14640
0
      return base == mips_mach_32_64[i].base;
14641
14642
0
  return false;
14643
0
}
14644
14645
static bool
14646
mips_mach_extends_p (unsigned long base, unsigned long extension)
14647
0
{
14648
0
  size_t i;
14649
14650
0
  if (mips_mach_extends_32_64 (base, extension))
14651
0
    return true;
14652
14653
0
  for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14654
0
    if (extension == mips_mach_extensions[i].extension)
14655
0
      {
14656
0
  extension = mips_mach_extensions[i].base;
14657
0
  if (mips_mach_extends_32_64 (base, extension))
14658
0
    return true;
14659
0
      }
14660
14661
0
  return false;
14662
0
}
14663
14664
/* Return the BFD mach for each .MIPS.abiflags ISA Extension.  */
14665
14666
static unsigned long
14667
bfd_mips_isa_ext_mach (unsigned int isa_ext)
14668
0
{
14669
0
  switch (isa_ext)
14670
0
    {
14671
0
    case AFL_EXT_3900:       return bfd_mach_mips3900;
14672
0
    case AFL_EXT_4010:       return bfd_mach_mips4010;
14673
0
    case AFL_EXT_4100:       return bfd_mach_mips4100;
14674
0
    case AFL_EXT_4111:       return bfd_mach_mips4111;
14675
0
    case AFL_EXT_4120:       return bfd_mach_mips4120;
14676
0
    case AFL_EXT_4650:       return bfd_mach_mips4650;
14677
0
    case AFL_EXT_5400:       return bfd_mach_mips5400;
14678
0
    case AFL_EXT_5500:       return bfd_mach_mips5500;
14679
0
    case AFL_EXT_5900:       return bfd_mach_mips5900;
14680
0
    case AFL_EXT_10000:       return bfd_mach_mips10000;
14681
0
    case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
14682
0
    case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
14683
0
    case AFL_EXT_SB1:       return bfd_mach_mips_sb1;
14684
0
    case AFL_EXT_OCTEON:      return bfd_mach_mips_octeon;
14685
0
    case AFL_EXT_OCTEONP:     return bfd_mach_mips_octeonp;
14686
0
    case AFL_EXT_OCTEON2:     return bfd_mach_mips_octeon2;
14687
0
    case AFL_EXT_XLR:       return bfd_mach_mips_xlr;
14688
0
    default:          return bfd_mach_mips3000;
14689
0
    }
14690
0
}
14691
14692
/* Return the .MIPS.abiflags value representing each ISA Extension.  */
14693
14694
unsigned int
14695
bfd_mips_isa_ext (bfd *abfd)
14696
0
{
14697
0
  switch (bfd_get_mach (abfd))
14698
0
    {
14699
0
    case bfd_mach_mips3900:     return AFL_EXT_3900;
14700
0
    case bfd_mach_mips4010:     return AFL_EXT_4010;
14701
0
    case bfd_mach_mips4100:     return AFL_EXT_4100;
14702
0
    case bfd_mach_mips4111:     return AFL_EXT_4111;
14703
0
    case bfd_mach_mips4120:     return AFL_EXT_4120;
14704
0
    case bfd_mach_mips4650:     return AFL_EXT_4650;
14705
0
    case bfd_mach_mips5400:     return AFL_EXT_5400;
14706
0
    case bfd_mach_mips5500:     return AFL_EXT_5500;
14707
0
    case bfd_mach_mips5900:     return AFL_EXT_5900;
14708
0
    case bfd_mach_mips10000:     return AFL_EXT_10000;
14709
0
    case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
14710
0
    case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
14711
0
    case bfd_mach_mips_sb1:     return AFL_EXT_SB1;
14712
0
    case bfd_mach_mips_octeon:     return AFL_EXT_OCTEON;
14713
0
    case bfd_mach_mips_octeonp:     return AFL_EXT_OCTEONP;
14714
0
    case bfd_mach_mips_octeon3:     return AFL_EXT_OCTEON3;
14715
0
    case bfd_mach_mips_octeon2:     return AFL_EXT_OCTEON2;
14716
0
    case bfd_mach_mips_xlr:     return AFL_EXT_XLR;
14717
0
    case bfd_mach_mips_interaptiv_mr2:
14718
0
      return AFL_EXT_INTERAPTIV_MR2;
14719
0
    default:          return 0;
14720
0
    }
14721
0
}
14722
14723
/* Encode ISA level and revision as a single value.  */
14724
0
#define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
14725
14726
/* Decode a single value into level and revision.  */
14727
0
#define ISA_LEVEL(LEVREV)  ((LEVREV) >> 3)
14728
0
#define ISA_REV(LEVREV)    ((LEVREV) & 0x7)
14729
14730
/* Update the isa_level, isa_rev, isa_ext fields of abiflags.  */
14731
14732
static void
14733
update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
14734
0
{
14735
0
  int new_isa = 0;
14736
0
  switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
14737
0
    {
14738
0
    case EF_MIPS_ARCH_1:    new_isa = LEVEL_REV (1, 0); break;
14739
0
    case EF_MIPS_ARCH_2:    new_isa = LEVEL_REV (2, 0); break;
14740
0
    case EF_MIPS_ARCH_3:    new_isa = LEVEL_REV (3, 0); break;
14741
0
    case EF_MIPS_ARCH_4:    new_isa = LEVEL_REV (4, 0); break;
14742
0
    case EF_MIPS_ARCH_5:    new_isa = LEVEL_REV (5, 0); break;
14743
0
    case EF_MIPS_ARCH_32:   new_isa = LEVEL_REV (32, 1); break;
14744
0
    case EF_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
14745
0
    case EF_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
14746
0
    case EF_MIPS_ARCH_64:   new_isa = LEVEL_REV (64, 1); break;
14747
0
    case EF_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
14748
0
    case EF_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
14749
0
    default:
14750
0
      _bfd_error_handler
14751
  /* xgettext:c-format */
14752
0
  (_("%pB: unknown architecture %s"),
14753
0
   abfd, bfd_printable_name (abfd));
14754
0
    }
14755
14756
0
  if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
14757
0
    {
14758
0
      abiflags->isa_level = ISA_LEVEL (new_isa);
14759
0
      abiflags->isa_rev = ISA_REV (new_isa);
14760
0
    }
14761
14762
  /* Update the isa_ext if ABFD describes a further extension.  */
14763
0
  if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
14764
0
         bfd_get_mach (abfd)))
14765
0
    abiflags->isa_ext = bfd_mips_isa_ext (abfd);
14766
0
}
14767
14768
/* Return true if the given ELF header flags describe a 32-bit binary.  */
14769
14770
static bool
14771
mips_32bit_flags_p (flagword flags)
14772
0
{
14773
0
  return ((flags & EF_MIPS_32BITMODE) != 0
14774
0
    || (flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32
14775
0
    || (flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI32
14776
0
    || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_1
14777
0
    || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_2
14778
0
    || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32
14779
0
    || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2
14780
0
    || (flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6);
14781
0
}
14782
14783
/* Infer the content of the ABI flags based on the elf header.  */
14784
14785
static void
14786
infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
14787
0
{
14788
0
  obj_attribute *in_attr;
14789
14790
0
  memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
14791
0
  update_mips_abiflags_isa (abfd, abiflags);
14792
14793
0
  if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
14794
0
    abiflags->gpr_size = AFL_REG_32;
14795
0
  else
14796
0
    abiflags->gpr_size = AFL_REG_64;
14797
14798
0
  abiflags->cpr1_size = AFL_REG_NONE;
14799
14800
0
  in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
14801
0
  abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14802
14803
0
  if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
14804
0
      || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
14805
0
      || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14806
0
    && abiflags->gpr_size == AFL_REG_32))
14807
0
    abiflags->cpr1_size = AFL_REG_32;
14808
0
  else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14809
0
     || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
14810
0
     || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
14811
0
    abiflags->cpr1_size = AFL_REG_64;
14812
14813
0
  abiflags->cpr2_size = AFL_REG_NONE;
14814
14815
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14816
0
    abiflags->ases |= AFL_ASE_MDMX;
14817
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14818
0
    abiflags->ases |= AFL_ASE_MIPS16;
14819
0
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14820
0
    abiflags->ases |= AFL_ASE_MICROMIPS;
14821
14822
0
  if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14823
0
      && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
14824
0
      && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
14825
0
      && abiflags->isa_level >= 32
14826
0
      && abiflags->ases != AFL_ASE_LOONGSON_EXT)
14827
0
    abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
14828
0
}
14829
14830
/* We need to use a special link routine to handle the .reginfo and
14831
   the .mdebug sections.  We need to merge all instances of these
14832
   sections together, not write them all out sequentially.  */
14833
14834
bool
14835
_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
14836
0
{
14837
0
  asection *o;
14838
0
  struct bfd_link_order *p;
14839
0
  asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
14840
0
  asection *rtproc_sec, *abiflags_sec;
14841
0
  Elf32_RegInfo reginfo;
14842
0
  struct ecoff_debug_info debug;
14843
0
  struct mips_htab_traverse_info hti;
14844
0
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14845
0
  const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
14846
0
  HDRR *symhdr = &debug.symbolic_header;
14847
0
  void *mdebug_handle = NULL;
14848
0
  asection *s;
14849
0
  EXTR esym;
14850
0
  unsigned int i;
14851
0
  bfd_size_type amt;
14852
0
  struct mips_elf_link_hash_table *htab;
14853
14854
0
  static const char * const secname[] =
14855
0
  {
14856
0
    ".text", ".init", ".fini", ".data",
14857
0
    ".rodata", ".sdata", ".sbss", ".bss"
14858
0
  };
14859
0
  static const int sc[] =
14860
0
  {
14861
0
    scText, scInit, scFini, scData,
14862
0
    scRData, scSData, scSBss, scBss
14863
0
  };
14864
14865
0
  htab = mips_elf_hash_table (info);
14866
0
  BFD_ASSERT (htab != NULL);
14867
14868
  /* Sort the dynamic symbols so that those with GOT entries come after
14869
     those without.  */
14870
0
  if (!mips_elf_sort_hash_table (abfd, info))
14871
0
    return false;
14872
14873
  /* Create any scheduled LA25 stubs.  */
14874
0
  hti.info = info;
14875
0
  hti.output_bfd = abfd;
14876
0
  hti.error = false;
14877
0
  htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
14878
0
  if (hti.error)
14879
0
    return false;
14880
14881
  /* Get a value for the GP register.  */
14882
0
  if (elf_gp (abfd) == 0)
14883
0
    {
14884
0
      struct bfd_link_hash_entry *h;
14885
14886
0
      h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
14887
0
      if (h != NULL && h->type == bfd_link_hash_defined)
14888
0
  elf_gp (abfd) = (h->u.def.value
14889
0
       + h->u.def.section->output_section->vma
14890
0
       + h->u.def.section->output_offset);
14891
0
      else if (htab->root.target_os == is_vxworks
14892
0
         && (h = bfd_link_hash_lookup (info->hash,
14893
0
               "_GLOBAL_OFFSET_TABLE_",
14894
0
               false, false, true))
14895
0
         && h->type == bfd_link_hash_defined)
14896
0
  elf_gp (abfd) = (h->u.def.section->output_section->vma
14897
0
       + h->u.def.section->output_offset
14898
0
       + h->u.def.value);
14899
0
      else if (bfd_link_relocatable (info))
14900
0
  {
14901
0
    bfd_vma lo = MINUS_ONE;
14902
14903
    /* Find the GP-relative section with the lowest offset.  */
14904
0
    for (o = abfd->sections; o != NULL; o = o->next)
14905
0
      if (o->vma < lo
14906
0
    && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
14907
0
        lo = o->vma;
14908
14909
    /* And calculate GP relative to that.  */
14910
0
    elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
14911
0
  }
14912
0
      else
14913
0
  {
14914
    /* If the relocate_section function needs to do a reloc
14915
       involving the GP value, it should make a reloc_dangerous
14916
       callback to warn that GP is not defined.  */
14917
0
  }
14918
0
    }
14919
14920
  /* Go through the sections and collect the .reginfo and .mdebug
14921
     information.  */
14922
0
  abiflags_sec = NULL;
14923
0
  reginfo_sec = NULL;
14924
0
  mdebug_sec = NULL;
14925
0
  gptab_data_sec = NULL;
14926
0
  gptab_bss_sec = NULL;
14927
0
  for (o = abfd->sections; o != NULL; o = o->next)
14928
0
    {
14929
0
      if (strcmp (o->name, ".MIPS.abiflags") == 0)
14930
0
  {
14931
    /* We have found the .MIPS.abiflags section in the output file.
14932
       Look through all the link_orders comprising it and remove them.
14933
       The data is merged in _bfd_mips_elf_merge_private_bfd_data.  */
14934
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
14935
0
      {
14936
0
        asection *input_section;
14937
14938
0
        if (p->type != bfd_indirect_link_order)
14939
0
    {
14940
0
      if (p->type == bfd_data_link_order)
14941
0
        continue;
14942
0
      abort ();
14943
0
    }
14944
14945
0
        input_section = p->u.indirect.section;
14946
14947
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
14948
     elf_link_input_bfd ignores this section.  */
14949
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
14950
0
      }
14951
14952
    /* Size has been set in _bfd_mips_elf_late_size_sections.  */
14953
0
    BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
14954
14955
    /* Skip this section later on (I don't think this currently
14956
       matters, but someday it might).  */
14957
0
    o->map_head.link_order = NULL;
14958
14959
0
    abiflags_sec = o;
14960
0
  }
14961
14962
0
      if (strcmp (o->name, ".reginfo") == 0)
14963
0
  {
14964
0
    memset (&reginfo, 0, sizeof reginfo);
14965
14966
    /* We have found the .reginfo section in the output file.
14967
       Look through all the link_orders comprising it and merge
14968
       the information together.  */
14969
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
14970
0
      {
14971
0
        asection *input_section;
14972
0
        bfd *input_bfd;
14973
0
        Elf32_External_RegInfo ext;
14974
0
        Elf32_RegInfo sub;
14975
0
        bfd_size_type sz;
14976
14977
0
        if (p->type != bfd_indirect_link_order)
14978
0
    {
14979
0
      if (p->type == bfd_data_link_order)
14980
0
        continue;
14981
0
      abort ();
14982
0
    }
14983
14984
0
        input_section = p->u.indirect.section;
14985
0
        input_bfd = input_section->owner;
14986
14987
0
        sz = (input_section->size < sizeof (ext)
14988
0
        ? input_section->size : sizeof (ext));
14989
0
        memset (&ext, 0, sizeof (ext));
14990
0
        if (! bfd_get_section_contents (input_bfd, input_section,
14991
0
                &ext, 0, sz))
14992
0
    return false;
14993
14994
0
        bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
14995
14996
0
        reginfo.ri_gprmask |= sub.ri_gprmask;
14997
0
        reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
14998
0
        reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
14999
0
        reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
15000
0
        reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
15001
15002
        /* ri_gp_value is set by the function
15003
     `_bfd_mips_elf_section_processing' when the section is
15004
     finally written out.  */
15005
15006
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
15007
     elf_link_input_bfd ignores this section.  */
15008
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
15009
0
      }
15010
15011
    /* Size has been set in _bfd_mips_elf_late_size_sections.  */
15012
0
    BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
15013
15014
    /* Skip this section later on (I don't think this currently
15015
       matters, but someday it might).  */
15016
0
    o->map_head.link_order = NULL;
15017
15018
0
    reginfo_sec = o;
15019
0
  }
15020
15021
0
      if (strcmp (o->name, ".mdebug") == 0)
15022
0
  {
15023
0
    struct extsym_info einfo;
15024
0
    bfd_vma last;
15025
15026
    /* We have found the .mdebug section in the output file.
15027
       Look through all the link_orders comprising it and merge
15028
       the information together.  */
15029
0
    symhdr->magic = swap->sym_magic;
15030
    /* FIXME: What should the version stamp be?  */
15031
0
    symhdr->vstamp = 0;
15032
0
    symhdr->ilineMax = 0;
15033
0
    symhdr->cbLine = 0;
15034
0
    symhdr->idnMax = 0;
15035
0
    symhdr->ipdMax = 0;
15036
0
    symhdr->isymMax = 0;
15037
0
    symhdr->ioptMax = 0;
15038
0
    symhdr->iauxMax = 0;
15039
0
    symhdr->issMax = 0;
15040
0
    symhdr->issExtMax = 0;
15041
0
    symhdr->ifdMax = 0;
15042
0
    symhdr->crfd = 0;
15043
0
    symhdr->iextMax = 0;
15044
15045
    /* We accumulate the debugging information itself in the
15046
       debug_info structure.  */
15047
0
    debug.alloc_syments = false;
15048
0
    debug.line = NULL;
15049
0
    debug.external_dnr = NULL;
15050
0
    debug.external_pdr = NULL;
15051
0
    debug.external_sym = NULL;
15052
0
    debug.external_opt = NULL;
15053
0
    debug.external_aux = NULL;
15054
0
    debug.ss = NULL;
15055
0
    debug.ssext = debug.ssext_end = NULL;
15056
0
    debug.external_fdr = NULL;
15057
0
    debug.external_rfd = NULL;
15058
0
    debug.external_ext = debug.external_ext_end = NULL;
15059
15060
0
    mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
15061
0
    if (mdebug_handle == NULL)
15062
0
      return false;
15063
15064
0
    esym.jmptbl = 0;
15065
0
    esym.cobol_main = 0;
15066
0
    esym.weakext = 0;
15067
0
    esym.reserved = 0;
15068
0
    esym.ifd = ifdNil;
15069
0
    esym.asym.iss = issNil;
15070
0
    esym.asym.st = stLocal;
15071
0
    esym.asym.reserved = 0;
15072
0
    esym.asym.index = indexNil;
15073
0
    last = 0;
15074
0
    for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
15075
0
      {
15076
0
        esym.asym.sc = sc[i];
15077
0
        s = bfd_get_section_by_name (abfd, secname[i]);
15078
0
        if (s != NULL)
15079
0
    {
15080
0
      esym.asym.value = s->vma;
15081
0
      last = s->vma + s->size;
15082
0
    }
15083
0
        else
15084
0
    esym.asym.value = last;
15085
0
        if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
15086
0
             secname[i], &esym))
15087
0
    return false;
15088
0
      }
15089
15090
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
15091
0
      {
15092
0
        asection *input_section;
15093
0
        bfd *input_bfd;
15094
0
        const struct ecoff_debug_swap *input_swap;
15095
0
        struct ecoff_debug_info input_debug;
15096
0
        char *eraw_src;
15097
0
        char *eraw_end;
15098
15099
0
        if (p->type != bfd_indirect_link_order)
15100
0
    {
15101
0
      if (p->type == bfd_data_link_order)
15102
0
        continue;
15103
0
      abort ();
15104
0
    }
15105
15106
0
        input_section = p->u.indirect.section;
15107
0
        input_bfd = input_section->owner;
15108
15109
0
        if (!is_mips_elf (input_bfd))
15110
0
    {
15111
      /* I don't know what a non MIPS ELF bfd would be
15112
         doing with a .mdebug section, but I don't really
15113
         want to deal with it.  */
15114
0
      continue;
15115
0
    }
15116
15117
0
        input_swap = (get_elf_backend_data (input_bfd)
15118
0
          ->elf_backend_ecoff_debug_swap);
15119
15120
0
        BFD_ASSERT (p->size == input_section->size);
15121
15122
        /* The ECOFF linking code expects that we have already
15123
     read in the debugging information and set up an
15124
     ecoff_debug_info structure, so we do that now.  */
15125
0
        if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
15126
0
               &input_debug))
15127
0
    return false;
15128
15129
0
        if (! (bfd_ecoff_debug_accumulate
15130
0
         (mdebug_handle, abfd, &debug, swap, input_bfd,
15131
0
          &input_debug, input_swap, info)))
15132
0
    {
15133
0
      _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15134
0
      return false;
15135
0
    }
15136
15137
        /* Loop through the external symbols.  For each one with
15138
     interesting information, try to find the symbol in
15139
     the linker global hash table and save the information
15140
     for the output external symbols.  */
15141
0
        eraw_src = input_debug.external_ext;
15142
0
        eraw_end = (eraw_src
15143
0
        + (input_debug.symbolic_header.iextMax
15144
0
           * input_swap->external_ext_size));
15145
0
        for (;
15146
0
       eraw_src < eraw_end;
15147
0
       eraw_src += input_swap->external_ext_size)
15148
0
    {
15149
0
      EXTR ext;
15150
0
      const char *name;
15151
0
      struct mips_elf_link_hash_entry *h;
15152
15153
0
      (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
15154
0
      if (ext.asym.sc == scNil
15155
0
          || ext.asym.sc == scUndefined
15156
0
          || ext.asym.sc == scSUndefined)
15157
0
        continue;
15158
15159
0
      name = input_debug.ssext + ext.asym.iss;
15160
0
      h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
15161
0
             name, false, false, true);
15162
0
      if (h == NULL || h->esym.ifd != -2)
15163
0
        continue;
15164
15165
0
      if (ext.ifd != -1)
15166
0
        {
15167
0
          BFD_ASSERT (ext.ifd
15168
0
          < input_debug.symbolic_header.ifdMax);
15169
0
          ext.ifd = input_debug.ifdmap[ext.ifd];
15170
0
        }
15171
15172
0
      h->esym = ext;
15173
0
    }
15174
15175
        /* Free up the information we just read.  */
15176
0
        _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15177
15178
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
15179
     elf_link_input_bfd ignores this section.  */
15180
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
15181
0
      }
15182
15183
0
    if (SGI_COMPAT (abfd) && bfd_link_pic (info))
15184
0
      {
15185
        /* Create .rtproc section.  */
15186
0
        rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
15187
0
        if (rtproc_sec == NULL)
15188
0
    {
15189
0
      flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
15190
0
            | SEC_LINKER_CREATED | SEC_READONLY);
15191
15192
0
      rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
15193
0
                   ".rtproc",
15194
0
                   flags);
15195
0
      if (rtproc_sec == NULL
15196
0
          || !bfd_set_section_alignment (rtproc_sec, 4))
15197
0
        return false;
15198
0
    }
15199
15200
0
        if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
15201
0
                 info, rtproc_sec,
15202
0
                 &debug))
15203
0
    return false;
15204
0
      }
15205
15206
    /* Build the external symbol information.  */
15207
0
    einfo.abfd = abfd;
15208
0
    einfo.info = info;
15209
0
    einfo.debug = &debug;
15210
0
    einfo.swap = swap;
15211
0
    einfo.failed = false;
15212
0
    mips_elf_link_hash_traverse (mips_elf_hash_table (info),
15213
0
               mips_elf_output_extsym, &einfo);
15214
0
    if (einfo.failed)
15215
0
      return false;
15216
15217
    /* Set the size of the .mdebug section.  */
15218
0
    o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
15219
15220
    /* Skip this section later on (I don't think this currently
15221
       matters, but someday it might).  */
15222
0
    o->map_head.link_order = NULL;
15223
15224
0
    mdebug_sec = o;
15225
0
  }
15226
15227
0
      if (startswith (o->name, ".gptab."))
15228
0
  {
15229
0
    const char *subname;
15230
0
    unsigned int c;
15231
0
    Elf32_gptab *tab;
15232
0
    Elf32_External_gptab *ext_tab;
15233
0
    unsigned int j;
15234
15235
    /* The .gptab.sdata and .gptab.sbss sections hold
15236
       information describing how the small data area would
15237
       change depending upon the -G switch.  These sections
15238
       not used in executables files.  */
15239
0
    if (! bfd_link_relocatable (info))
15240
0
      {
15241
0
        for (p = o->map_head.link_order; p != NULL; p = p->next)
15242
0
    {
15243
0
      asection *input_section;
15244
15245
0
      if (p->type != bfd_indirect_link_order)
15246
0
        {
15247
0
          if (p->type == bfd_data_link_order)
15248
0
      continue;
15249
0
          abort ();
15250
0
        }
15251
15252
0
      input_section = p->u.indirect.section;
15253
15254
      /* Hack: reset the SEC_HAS_CONTENTS flag so that
15255
         elf_link_input_bfd ignores this section.  */
15256
0
      input_section->flags &= ~SEC_HAS_CONTENTS;
15257
0
    }
15258
15259
        /* Skip this section later on (I don't think this
15260
     currently matters, but someday it might).  */
15261
0
        o->map_head.link_order = NULL;
15262
15263
        /* Really remove the section.  */
15264
0
        bfd_section_list_remove (abfd, o);
15265
0
        --abfd->section_count;
15266
15267
0
        continue;
15268
0
      }
15269
15270
    /* There is one gptab for initialized data, and one for
15271
       uninitialized data.  */
15272
0
    if (strcmp (o->name, ".gptab.sdata") == 0)
15273
0
      gptab_data_sec = o;
15274
0
    else if (strcmp (o->name, ".gptab.sbss") == 0)
15275
0
      gptab_bss_sec = o;
15276
0
    else
15277
0
      {
15278
0
        _bfd_error_handler
15279
    /* xgettext:c-format */
15280
0
    (_("%pB: illegal section name `%pA'"), abfd, o);
15281
0
        bfd_set_error (bfd_error_nonrepresentable_section);
15282
0
        return false;
15283
0
      }
15284
15285
    /* The linker script always combines .gptab.data and
15286
       .gptab.sdata into .gptab.sdata, and likewise for
15287
       .gptab.bss and .gptab.sbss.  It is possible that there is
15288
       no .sdata or .sbss section in the output file, in which
15289
       case we must change the name of the output section.  */
15290
0
    subname = o->name + sizeof ".gptab" - 1;
15291
0
    if (bfd_get_section_by_name (abfd, subname) == NULL)
15292
0
      {
15293
0
        if (o == gptab_data_sec)
15294
0
    o->name = ".gptab.data";
15295
0
        else
15296
0
    o->name = ".gptab.bss";
15297
0
        subname = o->name + sizeof ".gptab" - 1;
15298
0
        BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
15299
0
      }
15300
15301
    /* Set up the first entry.  */
15302
0
    c = 1;
15303
0
    amt = c * sizeof (Elf32_gptab);
15304
0
    tab = bfd_malloc (amt);
15305
0
    if (tab == NULL)
15306
0
      return false;
15307
0
    tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
15308
0
    tab[0].gt_header.gt_unused = 0;
15309
15310
    /* Combine the input sections.  */
15311
0
    for (p = o->map_head.link_order; p != NULL; p = p->next)
15312
0
      {
15313
0
        asection *input_section;
15314
0
        bfd *input_bfd;
15315
0
        bfd_size_type size;
15316
0
        unsigned long last;
15317
0
        bfd_size_type gpentry;
15318
15319
0
        if (p->type != bfd_indirect_link_order)
15320
0
    {
15321
0
      if (p->type == bfd_data_link_order)
15322
0
        continue;
15323
0
      abort ();
15324
0
    }
15325
15326
0
        input_section = p->u.indirect.section;
15327
0
        input_bfd = input_section->owner;
15328
15329
        /* Combine the gptab entries for this input section one
15330
     by one.  We know that the input gptab entries are
15331
     sorted by ascending -G value.  */
15332
0
        size = input_section->size;
15333
0
        last = 0;
15334
0
        for (gpentry = sizeof (Elf32_External_gptab);
15335
0
       gpentry < size;
15336
0
       gpentry += sizeof (Elf32_External_gptab))
15337
0
    {
15338
0
      Elf32_External_gptab ext_gptab;
15339
0
      Elf32_gptab int_gptab;
15340
0
      unsigned long val;
15341
0
      unsigned long add;
15342
0
      bool exact;
15343
0
      unsigned int look;
15344
15345
0
      if (! (bfd_get_section_contents
15346
0
       (input_bfd, input_section, &ext_gptab, gpentry,
15347
0
        sizeof (Elf32_External_gptab))))
15348
0
        {
15349
0
          free (tab);
15350
0
          return false;
15351
0
        }
15352
15353
0
      bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
15354
0
            &int_gptab);
15355
0
      val = int_gptab.gt_entry.gt_g_value;
15356
0
      add = int_gptab.gt_entry.gt_bytes - last;
15357
15358
0
      exact = false;
15359
0
      for (look = 1; look < c; look++)
15360
0
        {
15361
0
          if (tab[look].gt_entry.gt_g_value >= val)
15362
0
      tab[look].gt_entry.gt_bytes += add;
15363
15364
0
          if (tab[look].gt_entry.gt_g_value == val)
15365
0
      exact = true;
15366
0
        }
15367
15368
0
      if (! exact)
15369
0
        {
15370
0
          Elf32_gptab *new_tab;
15371
0
          unsigned int max;
15372
15373
          /* We need a new table entry.  */
15374
0
          amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
15375
0
          new_tab = bfd_realloc (tab, amt);
15376
0
          if (new_tab == NULL)
15377
0
      {
15378
0
        free (tab);
15379
0
        return false;
15380
0
      }
15381
0
          tab = new_tab;
15382
0
          tab[c].gt_entry.gt_g_value = val;
15383
0
          tab[c].gt_entry.gt_bytes = add;
15384
15385
          /* Merge in the size for the next smallest -G
15386
       value, since that will be implied by this new
15387
       value.  */
15388
0
          max = 0;
15389
0
          for (look = 1; look < c; look++)
15390
0
      {
15391
0
        if (tab[look].gt_entry.gt_g_value < val
15392
0
            && (max == 0
15393
0
          || (tab[look].gt_entry.gt_g_value
15394
0
              > tab[max].gt_entry.gt_g_value)))
15395
0
          max = look;
15396
0
      }
15397
0
          if (max != 0)
15398
0
      tab[c].gt_entry.gt_bytes +=
15399
0
        tab[max].gt_entry.gt_bytes;
15400
15401
0
          ++c;
15402
0
        }
15403
15404
0
      last = int_gptab.gt_entry.gt_bytes;
15405
0
    }
15406
15407
        /* Hack: reset the SEC_HAS_CONTENTS flag so that
15408
     elf_link_input_bfd ignores this section.  */
15409
0
        input_section->flags &= ~SEC_HAS_CONTENTS;
15410
0
      }
15411
15412
    /* The table must be sorted by -G value.  */
15413
0
    if (c > 2)
15414
0
      qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
15415
15416
    /* Swap out the table.  */
15417
0
    amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
15418
0
    ext_tab = bfd_alloc (abfd, amt);
15419
0
    if (ext_tab == NULL)
15420
0
      {
15421
0
        free (tab);
15422
0
        return false;
15423
0
      }
15424
15425
0
    for (j = 0; j < c; j++)
15426
0
      bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
15427
0
    free (tab);
15428
15429
0
    o->size = c * sizeof (Elf32_External_gptab);
15430
0
    o->contents = (bfd_byte *) ext_tab;
15431
0
    o->alloced = 1;
15432
15433
    /* Skip this section later on (I don't think this currently
15434
       matters, but someday it might).  */
15435
0
    o->map_head.link_order = NULL;
15436
0
  }
15437
0
    }
15438
15439
  /* Invoke the regular ELF backend linker to do all the work.  */
15440
0
  if (!bfd_elf_final_link (abfd, info))
15441
0
    return false;
15442
15443
  /* Now write out the computed sections.  */
15444
15445
0
  if (abiflags_sec != NULL)
15446
0
    {
15447
0
      Elf_External_ABIFlags_v0 ext;
15448
0
      Elf_Internal_ABIFlags_v0 *abiflags;
15449
15450
0
      abiflags = &mips_elf_tdata (abfd)->abiflags;
15451
15452
      /* Set up the abiflags if no valid input sections were found.  */
15453
0
      if (!mips_elf_tdata (abfd)->abiflags_valid)
15454
0
  {
15455
0
    infer_mips_abiflags (abfd, abiflags);
15456
0
    mips_elf_tdata (abfd)->abiflags_valid = true;
15457
0
  }
15458
0
      bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
15459
0
      if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
15460
0
  return false;
15461
0
    }
15462
15463
0
  if (reginfo_sec != NULL)
15464
0
    {
15465
0
      Elf32_External_RegInfo ext;
15466
15467
0
      bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
15468
0
      if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
15469
0
  return false;
15470
0
    }
15471
15472
0
  if (mdebug_sec != NULL)
15473
0
    {
15474
0
      BFD_ASSERT (abfd->output_has_begun);
15475
0
      if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
15476
0
                 swap, info,
15477
0
                 mdebug_sec->filepos))
15478
0
  return false;
15479
15480
0
      bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
15481
0
    }
15482
15483
0
  if (gptab_data_sec != NULL)
15484
0
    {
15485
0
      if (! bfd_set_section_contents (abfd, gptab_data_sec,
15486
0
              gptab_data_sec->contents,
15487
0
              0, gptab_data_sec->size))
15488
0
  return false;
15489
0
    }
15490
15491
0
  if (gptab_bss_sec != NULL)
15492
0
    {
15493
0
      if (! bfd_set_section_contents (abfd, gptab_bss_sec,
15494
0
              gptab_bss_sec->contents,
15495
0
              0, gptab_bss_sec->size))
15496
0
  return false;
15497
0
    }
15498
15499
0
  if (SGI_COMPAT (abfd))
15500
0
    {
15501
0
      rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
15502
0
      if (rtproc_sec != NULL)
15503
0
  {
15504
0
    if (! bfd_set_section_contents (abfd, rtproc_sec,
15505
0
            rtproc_sec->contents,
15506
0
            0, rtproc_sec->size))
15507
0
      return false;
15508
0
  }
15509
0
    }
15510
15511
0
  return true;
15512
0
}
15513

15514
/* Merge object file header flags from IBFD into OBFD.  Raise an error
15515
   if there are conflicting settings.  */
15516
15517
static bool
15518
mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
15519
0
{
15520
0
  bfd *obfd = info->output_bfd;
15521
0
  struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15522
0
  flagword old_flags;
15523
0
  flagword new_flags;
15524
0
  bool ok;
15525
15526
0
  new_flags = elf_elfheader (ibfd)->e_flags;
15527
0
  elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15528
0
  old_flags = elf_elfheader (obfd)->e_flags;
15529
15530
  /* Check flag compatibility.  */
15531
15532
0
  new_flags &= ~EF_MIPS_NOREORDER;
15533
0
  old_flags &= ~EF_MIPS_NOREORDER;
15534
15535
  /* Some IRIX 6 BSD-compatibility objects have this bit set.  It
15536
     doesn't seem to matter.  */
15537
0
  new_flags &= ~EF_MIPS_XGOT;
15538
0
  old_flags &= ~EF_MIPS_XGOT;
15539
15540
  /* MIPSpro generates ucode info in n64 objects.  Again, we should
15541
     just be able to ignore this.  */
15542
0
  new_flags &= ~EF_MIPS_UCODE;
15543
0
  old_flags &= ~EF_MIPS_UCODE;
15544
15545
  /* DSOs should only be linked with CPIC code.  */
15546
0
  if ((ibfd->flags & DYNAMIC) != 0)
15547
0
    new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15548
15549
0
  if (new_flags == old_flags)
15550
0
    return true;
15551
15552
0
  ok = true;
15553
15554
0
  if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15555
0
      != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15556
0
    {
15557
0
      _bfd_error_handler
15558
0
  (_("%pB: warning: linking abicalls files with non-abicalls files"),
15559
0
   ibfd);
15560
0
      ok = true;
15561
0
    }
15562
15563
0
  if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15564
0
    elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15565
0
  if (! (new_flags & EF_MIPS_PIC))
15566
0
    elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15567
15568
0
  new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15569
0
  old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15570
15571
  /* Compare the ISAs.  */
15572
0
  if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15573
0
    {
15574
0
      _bfd_error_handler
15575
0
  (_("%pB: linking 32-bit code with 64-bit code"),
15576
0
   ibfd);
15577
0
      ok = false;
15578
0
    }
15579
0
  else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15580
0
    {
15581
      /* OBFD's ISA isn't the same as, or an extension of, IBFD's.  */
15582
0
      if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15583
0
  {
15584
    /* Copy the architecture info from IBFD to OBFD.  Also copy
15585
       the 32-bit flag (if set) so that we continue to recognise
15586
       OBFD as a 32-bit binary.  */
15587
0
    bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15588
0
    elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15589
0
    elf_elfheader (obfd)->e_flags
15590
0
      |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15591
15592
    /* Update the ABI flags isa_level, isa_rev, isa_ext fields.  */
15593
0
    update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15594
15595
    /* Copy across the ABI flags if OBFD doesn't use them
15596
       and if that was what caused us to treat IBFD as 32-bit.  */
15597
0
    if ((old_flags & EF_MIPS_ABI) == 0
15598
0
        && mips_32bit_flags_p (new_flags)
15599
0
        && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15600
0
      elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15601
0
  }
15602
0
      else
15603
0
  {
15604
    /* The ISAs aren't compatible.  */
15605
0
    _bfd_error_handler
15606
      /* xgettext:c-format */
15607
0
      (_("%pB: linking %s module with previous %s modules"),
15608
0
       ibfd,
15609
0
       bfd_printable_name (ibfd),
15610
0
       bfd_printable_name (obfd));
15611
0
    ok = false;
15612
0
  }
15613
0
    }
15614
15615
0
  new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15616
0
  old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15617
15618
  /* Compare ABIs.  The 64-bit ABI does not use EF_MIPS_ABI.  But, it
15619
     does set EI_CLASS differently from any 32-bit ABI.  */
15620
0
  if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15621
0
      || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15622
0
    != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15623
0
    {
15624
      /* Only error if both are set (to different values).  */
15625
0
      if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15626
0
    || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15627
0
        != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15628
0
  {
15629
0
    _bfd_error_handler
15630
      /* xgettext:c-format */
15631
0
      (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
15632
0
       ibfd,
15633
0
       elf_mips_abi_name (ibfd),
15634
0
       elf_mips_abi_name (obfd));
15635
0
    ok = false;
15636
0
  }
15637
0
      new_flags &= ~EF_MIPS_ABI;
15638
0
      old_flags &= ~EF_MIPS_ABI;
15639
0
    }
15640
15641
  /* Compare ASEs.  Forbid linking MIPS16 and microMIPS ASE modules together
15642
     and allow arbitrary mixing of the remaining ASEs (retain the union).  */
15643
0
  if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15644
0
    {
15645
0
      int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15646
0
      int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15647
0
      int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15648
0
      int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15649
0
      int micro_mis = old_m16 && new_micro;
15650
0
      int m16_mis = old_micro && new_m16;
15651
15652
0
      if (m16_mis || micro_mis)
15653
0
  {
15654
0
    _bfd_error_handler
15655
      /* xgettext:c-format */
15656
0
      (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
15657
0
       ibfd,
15658
0
       m16_mis ? "MIPS16" : "microMIPS",
15659
0
       m16_mis ? "microMIPS" : "MIPS16");
15660
0
    ok = false;
15661
0
  }
15662
15663
0
      elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15664
15665
0
      new_flags &= ~ EF_MIPS_ARCH_ASE;
15666
0
      old_flags &= ~ EF_MIPS_ARCH_ASE;
15667
0
    }
15668
15669
  /* Compare NaN encodings.  */
15670
0
  if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15671
0
    {
15672
      /* xgettext:c-format */
15673
0
      _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15674
0
        ibfd,
15675
0
        (new_flags & EF_MIPS_NAN2008
15676
0
         ? "-mnan=2008" : "-mnan=legacy"),
15677
0
        (old_flags & EF_MIPS_NAN2008
15678
0
         ? "-mnan=2008" : "-mnan=legacy"));
15679
0
      ok = false;
15680
0
      new_flags &= ~EF_MIPS_NAN2008;
15681
0
      old_flags &= ~EF_MIPS_NAN2008;
15682
0
    }
15683
15684
  /* Compare FP64 state.  */
15685
0
  if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15686
0
    {
15687
      /* xgettext:c-format */
15688
0
      _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15689
0
        ibfd,
15690
0
        (new_flags & EF_MIPS_FP64
15691
0
         ? "-mfp64" : "-mfp32"),
15692
0
        (old_flags & EF_MIPS_FP64
15693
0
         ? "-mfp64" : "-mfp32"));
15694
0
      ok = false;
15695
0
      new_flags &= ~EF_MIPS_FP64;
15696
0
      old_flags &= ~EF_MIPS_FP64;
15697
0
    }
15698
15699
  /* Warn about any other mismatches */
15700
0
  if (new_flags != old_flags)
15701
0
    {
15702
      /* xgettext:c-format */
15703
0
      _bfd_error_handler
15704
0
  (_("%pB: uses different e_flags (%#x) fields than previous modules "
15705
0
     "(%#x)"),
15706
0
   ibfd, new_flags, old_flags);
15707
0
      ok = false;
15708
0
    }
15709
15710
0
  return ok;
15711
0
}
15712
15713
/* Merge object attributes from IBFD into OBFD.  Raise an error if
15714
   there are conflicting attributes.  */
15715
static bool
15716
mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
15717
0
{
15718
0
  bfd *obfd = info->output_bfd;
15719
0
  obj_attribute *in_attr;
15720
0
  obj_attribute *out_attr;
15721
0
  bfd *abi_fp_bfd;
15722
0
  bfd *abi_msa_bfd;
15723
15724
0
  abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
15725
0
  in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15726
0
  if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
15727
0
    mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15728
15729
0
  abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
15730
0
  if (!abi_msa_bfd
15731
0
      && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15732
0
    mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
15733
15734
0
  if (!elf_known_obj_attributes_proc (obfd)[0].i)
15735
0
    {
15736
      /* This is the first object.  Copy the attributes.  */
15737
0
      _bfd_elf_copy_obj_attributes (ibfd, obfd);
15738
15739
      /* Use the Tag_null value to indicate the attributes have been
15740
   initialized.  */
15741
0
      elf_known_obj_attributes_proc (obfd)[0].i = 1;
15742
15743
0
      return true;
15744
0
    }
15745
15746
  /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
15747
     non-conflicting ones.  */
15748
0
  out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15749
0
  if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
15750
0
    {
15751
0
      int out_fp, in_fp;
15752
15753
0
      out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15754
0
      in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15755
0
      out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
15756
0
      if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
15757
0
  out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
15758
0
      else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
15759
0
         && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15760
0
       || in_fp == Val_GNU_MIPS_ABI_FP_64
15761
0
       || in_fp == Val_GNU_MIPS_ABI_FP_64A))
15762
0
  {
15763
0
    mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15764
0
    out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15765
0
  }
15766
0
      else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
15767
0
         && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15768
0
       || out_fp == Val_GNU_MIPS_ABI_FP_64
15769
0
       || out_fp == Val_GNU_MIPS_ABI_FP_64A))
15770
0
  /* Keep the current setting.  */;
15771
0
      else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
15772
0
         && in_fp == Val_GNU_MIPS_ABI_FP_64)
15773
0
  {
15774
0
    mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15775
0
    out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15776
0
  }
15777
0
      else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
15778
0
         && out_fp == Val_GNU_MIPS_ABI_FP_64)
15779
0
  /* Keep the current setting.  */;
15780
0
      else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
15781
0
  {
15782
0
    const char *out_string, *in_string;
15783
15784
0
    out_string = _bfd_mips_fp_abi_string (out_fp);
15785
0
    in_string = _bfd_mips_fp_abi_string (in_fp);
15786
    /* First warn about cases involving unrecognised ABIs.  */
15787
0
    if (!out_string && !in_string)
15788
      /* xgettext:c-format */
15789
0
      _bfd_error_handler
15790
0
        (_("warning: %pB uses unknown floating point ABI %d "
15791
0
     "(set by %pB), %pB uses unknown floating point ABI %d"),
15792
0
         obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
15793
0
    else if (!out_string)
15794
0
      _bfd_error_handler
15795
        /* xgettext:c-format */
15796
0
        (_("warning: %pB uses unknown floating point ABI %d "
15797
0
     "(set by %pB), %pB uses %s"),
15798
0
         obfd, out_fp, abi_fp_bfd, ibfd, in_string);
15799
0
    else if (!in_string)
15800
0
      _bfd_error_handler
15801
        /* xgettext:c-format */
15802
0
        (_("warning: %pB uses %s (set by %pB), "
15803
0
     "%pB uses unknown floating point ABI %d"),
15804
0
         obfd, out_string, abi_fp_bfd, ibfd, in_fp);
15805
0
    else
15806
0
      {
15807
        /* If one of the bfds is soft-float, the other must be
15808
     hard-float.  The exact choice of hard-float ABI isn't
15809
     really relevant to the error message.  */
15810
0
        if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15811
0
    out_string = "-mhard-float";
15812
0
        else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15813
0
    in_string = "-mhard-float";
15814
0
        _bfd_error_handler
15815
    /* xgettext:c-format */
15816
0
    (_("warning: %pB uses %s (set by %pB), %pB uses %s"),
15817
0
     obfd, out_string, abi_fp_bfd, ibfd, in_string);
15818
0
      }
15819
0
  }
15820
0
    }
15821
15822
  /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
15823
     non-conflicting ones.  */
15824
0
  if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15825
0
    {
15826
0
      out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
15827
0
      if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
15828
0
  out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
15829
0
      else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15830
0
  switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15831
0
    {
15832
0
    case Val_GNU_MIPS_ABI_MSA_128:
15833
0
      _bfd_error_handler
15834
        /* xgettext:c-format */
15835
0
        (_("warning: %pB uses %s (set by %pB), "
15836
0
     "%pB uses unknown MSA ABI %d"),
15837
0
         obfd, "-mmsa", abi_msa_bfd,
15838
0
         ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15839
0
      break;
15840
15841
0
    default:
15842
0
      switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
15843
0
        {
15844
0
        case Val_GNU_MIPS_ABI_MSA_128:
15845
0
    _bfd_error_handler
15846
      /* xgettext:c-format */
15847
0
      (_("warning: %pB uses unknown MSA ABI %d "
15848
0
         "(set by %pB), %pB uses %s"),
15849
0
         obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15850
0
       abi_msa_bfd, ibfd, "-mmsa");
15851
0
      break;
15852
15853
0
        default:
15854
0
    _bfd_error_handler
15855
      /* xgettext:c-format */
15856
0
      (_("warning: %pB uses unknown MSA ABI %d "
15857
0
         "(set by %pB), %pB uses unknown MSA ABI %d"),
15858
0
       obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15859
0
       abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15860
0
    break;
15861
0
        }
15862
0
    }
15863
0
    }
15864
15865
  /* Merge Tag_compatibility attributes and any common GNU ones.  */
15866
0
  return _bfd_elf_merge_object_attributes (ibfd, info);
15867
0
}
15868
15869
/* Merge object ABI flags from IBFD into OBFD.  Raise an error if
15870
   there are conflicting settings.  */
15871
15872
static bool
15873
mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
15874
0
{
15875
0
  obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15876
0
  struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15877
0
  struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
15878
15879
  /* Update the output abiflags fp_abi using the computed fp_abi.  */
15880
0
  out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15881
15882
0
#define max(a, b) ((a) > (b) ? (a) : (b))
15883
  /* Merge abiflags.  */
15884
0
  out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
15885
0
               in_tdata->abiflags.isa_level);
15886
0
  out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
15887
0
             in_tdata->abiflags.isa_rev);
15888
0
  out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
15889
0
              in_tdata->abiflags.gpr_size);
15890
0
  out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
15891
0
               in_tdata->abiflags.cpr1_size);
15892
0
  out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
15893
0
               in_tdata->abiflags.cpr2_size);
15894
0
#undef max
15895
0
  out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
15896
0
  out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
15897
15898
0
  return true;
15899
0
}
15900
15901
/* Merge backend specific data from an object file to the output
15902
   object file when linking.  */
15903
15904
bool
15905
_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
15906
0
{
15907
0
  bfd *obfd = info->output_bfd;
15908
0
  struct mips_elf_obj_tdata *out_tdata;
15909
0
  struct mips_elf_obj_tdata *in_tdata;
15910
0
  bool null_input_bfd = true;
15911
0
  asection *sec;
15912
0
  bool ok;
15913
15914
  /* Check if we have the same endianness.  */
15915
0
  if (! _bfd_generic_verify_endian_match (ibfd, info))
15916
0
    {
15917
0
      _bfd_error_handler
15918
0
  (_("%pB: endianness incompatible with that of the selected emulation"),
15919
0
   ibfd);
15920
0
      return false;
15921
0
    }
15922
15923
0
  if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
15924
0
    return true;
15925
15926
0
  in_tdata = mips_elf_tdata (ibfd);
15927
0
  out_tdata = mips_elf_tdata (obfd);
15928
15929
0
  if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
15930
0
    {
15931
0
      _bfd_error_handler
15932
0
  (_("%pB: ABI is incompatible with that of the selected emulation"),
15933
0
   ibfd);
15934
0
      return false;
15935
0
    }
15936
15937
  /* Check to see if the input BFD actually contains any sections.  If not,
15938
     then it has no attributes, and its flags may not have been initialized
15939
     either, but it cannot actually cause any incompatibility.  */
15940
  /* FIXME: This excludes any input shared library from consideration.  */
15941
0
  for (sec = ibfd->sections; sec != NULL; sec = sec->next)
15942
0
    {
15943
      /* Ignore synthetic sections and empty .text, .data and .bss sections
15944
   which are automatically generated by gas.  Also ignore fake
15945
   (s)common sections, since merely defining a common symbol does
15946
   not affect compatibility.  */
15947
0
      if ((sec->flags & SEC_IS_COMMON) == 0
15948
0
    && strcmp (sec->name, ".reginfo")
15949
0
    && strcmp (sec->name, ".mdebug")
15950
0
    && (sec->size != 0
15951
0
        || (strcmp (sec->name, ".text")
15952
0
      && strcmp (sec->name, ".data")
15953
0
      && strcmp (sec->name, ".bss"))))
15954
0
  {
15955
0
    null_input_bfd = false;
15956
0
    break;
15957
0
  }
15958
0
    }
15959
0
  if (null_input_bfd)
15960
0
    return true;
15961
15962
  /* Populate abiflags using existing information.  */
15963
0
  if (in_tdata->abiflags_valid)
15964
0
    {
15965
0
      obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15966
0
      Elf_Internal_ABIFlags_v0 in_abiflags;
15967
0
      Elf_Internal_ABIFlags_v0 abiflags;
15968
15969
      /* Set up the FP ABI attribute from the abiflags if it is not already
15970
   set.  */
15971
0
      if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
15972
0
  in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
15973
15974
0
      infer_mips_abiflags (ibfd, &abiflags);
15975
0
      in_abiflags = in_tdata->abiflags;
15976
15977
      /* It is not possible to infer the correct ISA revision
15978
   for R3 or R5 so drop down to R2 for the checks.  */
15979
0
      if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
15980
0
  in_abiflags.isa_rev = 2;
15981
15982
0
      if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
15983
0
    < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
15984
0
  _bfd_error_handler
15985
0
    (_("%pB: warning: inconsistent ISA between e_flags and "
15986
0
       ".MIPS.abiflags"), ibfd);
15987
0
      if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
15988
0
    && in_abiflags.fp_abi != abiflags.fp_abi)
15989
0
  _bfd_error_handler
15990
0
    (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
15991
0
       ".MIPS.abiflags"), ibfd);
15992
0
      if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
15993
0
  _bfd_error_handler
15994
0
    (_("%pB: warning: inconsistent ASEs between e_flags and "
15995
0
       ".MIPS.abiflags"), ibfd);
15996
      /* The isa_ext is allowed to be an extension of what can be inferred
15997
   from e_flags.  */
15998
0
      if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
15999
0
        bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
16000
0
  _bfd_error_handler
16001
0
    (_("%pB: warning: inconsistent ISA extensions between e_flags and "
16002
0
       ".MIPS.abiflags"), ibfd);
16003
0
      if (in_abiflags.flags2 != 0)
16004
0
  _bfd_error_handler
16005
0
    (_("%pB: warning: unexpected flag in the flags2 field of "
16006
0
       ".MIPS.abiflags (0x%lx)"), ibfd,
16007
0
     in_abiflags.flags2);
16008
0
    }
16009
0
  else
16010
0
    {
16011
0
      infer_mips_abiflags (ibfd, &in_tdata->abiflags);
16012
0
      in_tdata->abiflags_valid = true;
16013
0
    }
16014
16015
0
  if (!out_tdata->abiflags_valid)
16016
0
    {
16017
      /* Copy input abiflags if output abiflags are not already valid.  */
16018
0
      out_tdata->abiflags = in_tdata->abiflags;
16019
0
      out_tdata->abiflags_valid = true;
16020
0
    }
16021
16022
0
  if (! elf_flags_init (obfd))
16023
0
    {
16024
0
      elf_flags_init (obfd) = true;
16025
0
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
16026
0
      elf_elfheader (obfd)->e_ident[EI_CLASS]
16027
0
  = elf_elfheader (ibfd)->e_ident[EI_CLASS];
16028
16029
0
      if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
16030
0
    && (bfd_get_arch_info (obfd)->the_default
16031
0
        || mips_mach_extends_p (bfd_get_mach (obfd),
16032
0
              bfd_get_mach (ibfd))))
16033
0
  {
16034
0
    if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
16035
0
           bfd_get_mach (ibfd)))
16036
0
      return false;
16037
16038
    /* Update the ABI flags isa_level, isa_rev and isa_ext fields.  */
16039
0
    update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
16040
0
  }
16041
16042
0
      ok = true;
16043
0
    }
16044
0
  else
16045
0
    ok = mips_elf_merge_obj_e_flags (ibfd, info);
16046
16047
0
  ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
16048
16049
0
  ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
16050
16051
0
  if (!ok)
16052
0
    {
16053
0
      bfd_set_error (bfd_error_bad_value);
16054
0
      return false;
16055
0
    }
16056
16057
0
  return true;
16058
0
}
16059
16060
/* Function to keep MIPS specific file flags like as EF_MIPS_PIC.  */
16061
16062
bool
16063
_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
16064
0
{
16065
0
  BFD_ASSERT (!elf_flags_init (abfd)
16066
0
        || elf_elfheader (abfd)->e_flags == flags);
16067
16068
0
  elf_elfheader (abfd)->e_flags = flags;
16069
0
  elf_flags_init (abfd) = true;
16070
0
  return true;
16071
0
}
16072
16073
char *
16074
_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
16075
70
{
16076
70
  switch (dtag)
16077
70
    {
16078
6
    default: return "";
16079
8
    case DT_MIPS_RLD_VERSION:
16080
8
      return "MIPS_RLD_VERSION";
16081
0
    case DT_MIPS_TIME_STAMP:
16082
0
      return "MIPS_TIME_STAMP";
16083
0
    case DT_MIPS_ICHECKSUM:
16084
0
      return "MIPS_ICHECKSUM";
16085
0
    case DT_MIPS_IVERSION:
16086
0
      return "MIPS_IVERSION";
16087
8
    case DT_MIPS_FLAGS:
16088
8
      return "MIPS_FLAGS";
16089
8
    case DT_MIPS_BASE_ADDRESS:
16090
8
      return "MIPS_BASE_ADDRESS";
16091
0
    case DT_MIPS_MSYM:
16092
0
      return "MIPS_MSYM";
16093
0
    case DT_MIPS_CONFLICT:
16094
0
      return "MIPS_CONFLICT";
16095
0
    case DT_MIPS_LIBLIST:
16096
0
      return "MIPS_LIBLIST";
16097
8
    case DT_MIPS_LOCAL_GOTNO:
16098
8
      return "MIPS_LOCAL_GOTNO";
16099
0
    case DT_MIPS_CONFLICTNO:
16100
0
      return "MIPS_CONFLICTNO";
16101
0
    case DT_MIPS_LIBLISTNO:
16102
0
      return "MIPS_LIBLISTNO";
16103
8
    case DT_MIPS_SYMTABNO:
16104
8
      return "MIPS_SYMTABNO";
16105
8
    case DT_MIPS_UNREFEXTNO:
16106
8
      return "MIPS_UNREFEXTNO";
16107
8
    case DT_MIPS_GOTSYM:
16108
8
      return "MIPS_GOTSYM";
16109
0
    case DT_MIPS_HIPAGENO:
16110
0
      return "MIPS_HIPAGENO";
16111
8
    case DT_MIPS_RLD_MAP:
16112
8
      return "MIPS_RLD_MAP";
16113
0
    case DT_MIPS_RLD_MAP_REL:
16114
0
      return "MIPS_RLD_MAP_REL";
16115
0
    case DT_MIPS_DELTA_CLASS:
16116
0
      return "MIPS_DELTA_CLASS";
16117
0
    case DT_MIPS_DELTA_CLASS_NO:
16118
0
      return "MIPS_DELTA_CLASS_NO";
16119
0
    case DT_MIPS_DELTA_INSTANCE:
16120
0
      return "MIPS_DELTA_INSTANCE";
16121
0
    case DT_MIPS_DELTA_INSTANCE_NO:
16122
0
      return "MIPS_DELTA_INSTANCE_NO";
16123
0
    case DT_MIPS_DELTA_RELOC:
16124
0
      return "MIPS_DELTA_RELOC";
16125
0
    case DT_MIPS_DELTA_RELOC_NO:
16126
0
      return "MIPS_DELTA_RELOC_NO";
16127
0
    case DT_MIPS_DELTA_SYM:
16128
0
      return "MIPS_DELTA_SYM";
16129
0
    case DT_MIPS_DELTA_SYM_NO:
16130
0
      return "MIPS_DELTA_SYM_NO";
16131
0
    case DT_MIPS_DELTA_CLASSSYM:
16132
0
      return "MIPS_DELTA_CLASSSYM";
16133
0
    case DT_MIPS_DELTA_CLASSSYM_NO:
16134
0
      return "MIPS_DELTA_CLASSSYM_NO";
16135
0
    case DT_MIPS_CXX_FLAGS:
16136
0
      return "MIPS_CXX_FLAGS";
16137
0
    case DT_MIPS_PIXIE_INIT:
16138
0
      return "MIPS_PIXIE_INIT";
16139
0
    case DT_MIPS_SYMBOL_LIB:
16140
0
      return "MIPS_SYMBOL_LIB";
16141
0
    case DT_MIPS_LOCALPAGE_GOTIDX:
16142
0
      return "MIPS_LOCALPAGE_GOTIDX";
16143
0
    case DT_MIPS_LOCAL_GOTIDX:
16144
0
      return "MIPS_LOCAL_GOTIDX";
16145
0
    case DT_MIPS_HIDDEN_GOTIDX:
16146
0
      return "MIPS_HIDDEN_GOTIDX";
16147
0
    case DT_MIPS_PROTECTED_GOTIDX:
16148
0
      return "MIPS_PROTECTED_GOT_IDX";
16149
0
    case DT_MIPS_OPTIONS:
16150
0
      return "MIPS_OPTIONS";
16151
0
    case DT_MIPS_INTERFACE:
16152
0
      return "MIPS_INTERFACE";
16153
0
    case DT_MIPS_DYNSTR_ALIGN:
16154
0
      return "DT_MIPS_DYNSTR_ALIGN";
16155
0
    case DT_MIPS_INTERFACE_SIZE:
16156
0
      return "DT_MIPS_INTERFACE_SIZE";
16157
0
    case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
16158
0
      return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
16159
0
    case DT_MIPS_PERF_SUFFIX:
16160
0
      return "DT_MIPS_PERF_SUFFIX";
16161
0
    case DT_MIPS_COMPACT_SIZE:
16162
0
      return "DT_MIPS_COMPACT_SIZE";
16163
0
    case DT_MIPS_GP_VALUE:
16164
0
      return "DT_MIPS_GP_VALUE";
16165
0
    case DT_MIPS_AUX_DYNAMIC:
16166
0
      return "DT_MIPS_AUX_DYNAMIC";
16167
0
    case DT_MIPS_PLTGOT:
16168
0
      return "DT_MIPS_PLTGOT";
16169
0
    case DT_MIPS_RWPLT:
16170
0
      return "DT_MIPS_RWPLT";
16171
0
    case DT_MIPS_XHASH:
16172
0
      return "DT_MIPS_XHASH";
16173
70
    }
16174
70
}
16175
16176
/* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
16177
   not known.  */
16178
16179
const char *
16180
_bfd_mips_fp_abi_string (int fp)
16181
0
{
16182
0
  switch (fp)
16183
0
    {
16184
      /* These strings aren't translated because they're simply
16185
   option lists.  */
16186
0
    case Val_GNU_MIPS_ABI_FP_DOUBLE:
16187
0
      return "-mdouble-float";
16188
16189
0
    case Val_GNU_MIPS_ABI_FP_SINGLE:
16190
0
      return "-msingle-float";
16191
16192
0
    case Val_GNU_MIPS_ABI_FP_SOFT:
16193
0
      return "-msoft-float";
16194
16195
0
    case Val_GNU_MIPS_ABI_FP_OLD_64:
16196
0
      return _("-mips32r2 -mfp64 (12 callee-saved)");
16197
16198
0
    case Val_GNU_MIPS_ABI_FP_XX:
16199
0
      return "-mfpxx";
16200
16201
0
    case Val_GNU_MIPS_ABI_FP_64:
16202
0
      return "-mgp32 -mfp64";
16203
16204
0
    case Val_GNU_MIPS_ABI_FP_64A:
16205
0
      return "-mgp32 -mfp64 -mno-odd-spreg";
16206
16207
0
    default:
16208
0
      return 0;
16209
0
    }
16210
0
}
16211
16212
static void
16213
print_mips_ases (FILE *file, unsigned int mask)
16214
0
{
16215
0
  if (mask & AFL_ASE_DSP)
16216
0
    fputs ("\n\tDSP ASE", file);
16217
0
  if (mask & AFL_ASE_DSPR2)
16218
0
    fputs ("\n\tDSP R2 ASE", file);
16219
0
  if (mask & AFL_ASE_DSPR3)
16220
0
    fputs ("\n\tDSP R3 ASE", file);
16221
0
  if (mask & AFL_ASE_EVA)
16222
0
    fputs ("\n\tEnhanced VA Scheme", file);
16223
0
  if (mask & AFL_ASE_MCU)
16224
0
    fputs ("\n\tMCU (MicroController) ASE", file);
16225
0
  if (mask & AFL_ASE_MDMX)
16226
0
    fputs ("\n\tMDMX ASE", file);
16227
0
  if (mask & AFL_ASE_MIPS3D)
16228
0
    fputs ("\n\tMIPS-3D ASE", file);
16229
0
  if (mask & AFL_ASE_MT)
16230
0
    fputs ("\n\tMT ASE", file);
16231
0
  if (mask & AFL_ASE_SMARTMIPS)
16232
0
    fputs ("\n\tSmartMIPS ASE", file);
16233
0
  if (mask & AFL_ASE_VIRT)
16234
0
    fputs ("\n\tVZ ASE", file);
16235
0
  if (mask & AFL_ASE_MSA)
16236
0
    fputs ("\n\tMSA ASE", file);
16237
0
  if (mask & AFL_ASE_MIPS16)
16238
0
    fputs ("\n\tMIPS16 ASE", file);
16239
0
  if (mask & AFL_ASE_MICROMIPS)
16240
0
    fputs ("\n\tMICROMIPS ASE", file);
16241
0
  if (mask & AFL_ASE_XPA)
16242
0
    fputs ("\n\tXPA ASE", file);
16243
0
  if (mask & AFL_ASE_MIPS16E2)
16244
0
    fputs ("\n\tMIPS16e2 ASE", file);
16245
0
  if (mask & AFL_ASE_CRC)
16246
0
    fputs ("\n\tCRC ASE", file);
16247
0
  if (mask & AFL_ASE_GINV)
16248
0
    fputs ("\n\tGINV ASE", file);
16249
0
  if (mask & AFL_ASE_LOONGSON_MMI)
16250
0
    fputs ("\n\tLoongson MMI ASE", file);
16251
0
  if (mask & AFL_ASE_LOONGSON_CAM)
16252
0
    fputs ("\n\tLoongson CAM ASE", file);
16253
0
  if (mask & AFL_ASE_LOONGSON_EXT)
16254
0
    fputs ("\n\tLoongson EXT ASE", file);
16255
0
  if (mask & AFL_ASE_LOONGSON_EXT2)
16256
0
    fputs ("\n\tLoongson EXT2 ASE", file);
16257
0
  if (mask == 0)
16258
0
    fprintf (file, "\n\t%s", _("None"));
16259
0
  else if ((mask & ~AFL_ASE_MASK) != 0)
16260
0
    fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
16261
0
}
16262
16263
static void
16264
print_mips_isa_ext (FILE *file, unsigned int isa_ext)
16265
0
{
16266
0
  switch (isa_ext)
16267
0
    {
16268
0
    case 0:
16269
0
      fputs (_("None"), file);
16270
0
      break;
16271
0
    case AFL_EXT_XLR:
16272
0
      fputs ("RMI XLR", file);
16273
0
      break;
16274
0
    case AFL_EXT_OCTEON3:
16275
0
      fputs ("Cavium Networks Octeon3", file);
16276
0
      break;
16277
0
    case AFL_EXT_OCTEON2:
16278
0
      fputs ("Cavium Networks Octeon2", file);
16279
0
      break;
16280
0
    case AFL_EXT_OCTEONP:
16281
0
      fputs ("Cavium Networks OcteonP", file);
16282
0
      break;
16283
0
    case AFL_EXT_OCTEON:
16284
0
      fputs ("Cavium Networks Octeon", file);
16285
0
      break;
16286
0
    case AFL_EXT_5900:
16287
0
      fputs ("Toshiba R5900", file);
16288
0
      break;
16289
0
    case AFL_EXT_4650:
16290
0
      fputs ("MIPS R4650", file);
16291
0
      break;
16292
0
    case AFL_EXT_4010:
16293
0
      fputs ("LSI R4010", file);
16294
0
      break;
16295
0
    case AFL_EXT_4100:
16296
0
      fputs ("NEC VR4100", file);
16297
0
      break;
16298
0
    case AFL_EXT_3900:
16299
0
      fputs ("Toshiba R3900", file);
16300
0
      break;
16301
0
    case AFL_EXT_10000:
16302
0
      fputs ("MIPS R10000", file);
16303
0
      break;
16304
0
    case AFL_EXT_SB1:
16305
0
      fputs ("Broadcom SB-1", file);
16306
0
      break;
16307
0
    case AFL_EXT_4111:
16308
0
      fputs ("NEC VR4111/VR4181", file);
16309
0
      break;
16310
0
    case AFL_EXT_4120:
16311
0
      fputs ("NEC VR4120", file);
16312
0
      break;
16313
0
    case AFL_EXT_5400:
16314
0
      fputs ("NEC VR5400", file);
16315
0
      break;
16316
0
    case AFL_EXT_5500:
16317
0
      fputs ("NEC VR5500", file);
16318
0
      break;
16319
0
    case AFL_EXT_LOONGSON_2E:
16320
0
      fputs ("ST Microelectronics Loongson 2E", file);
16321
0
      break;
16322
0
    case AFL_EXT_LOONGSON_2F:
16323
0
      fputs ("ST Microelectronics Loongson 2F", file);
16324
0
      break;
16325
0
    case AFL_EXT_INTERAPTIV_MR2:
16326
0
      fputs ("Imagination interAptiv MR2", file);
16327
0
      break;
16328
0
    default:
16329
0
      fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
16330
0
      break;
16331
0
    }
16332
0
}
16333
16334
static void
16335
print_mips_fp_abi_value (FILE *file, int val)
16336
0
{
16337
0
  switch (val)
16338
0
    {
16339
0
    case Val_GNU_MIPS_ABI_FP_ANY:
16340
0
      fprintf (file, _("Hard or soft float\n"));
16341
0
      break;
16342
0
    case Val_GNU_MIPS_ABI_FP_DOUBLE:
16343
0
      fprintf (file, _("Hard float (double precision)\n"));
16344
0
      break;
16345
0
    case Val_GNU_MIPS_ABI_FP_SINGLE:
16346
0
      fprintf (file, _("Hard float (single precision)\n"));
16347
0
      break;
16348
0
    case Val_GNU_MIPS_ABI_FP_SOFT:
16349
0
      fprintf (file, _("Soft float\n"));
16350
0
      break;
16351
0
    case Val_GNU_MIPS_ABI_FP_OLD_64:
16352
0
      fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16353
0
      break;
16354
0
    case Val_GNU_MIPS_ABI_FP_XX:
16355
0
      fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
16356
0
      break;
16357
0
    case Val_GNU_MIPS_ABI_FP_64:
16358
0
      fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
16359
0
      break;
16360
0
    case Val_GNU_MIPS_ABI_FP_64A:
16361
0
      fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16362
0
      break;
16363
0
    default:
16364
0
      fprintf (file, "??? (%d)\n", val);
16365
0
      break;
16366
0
    }
16367
0
}
16368
16369
static int
16370
get_mips_reg_size (int reg_size)
16371
0
{
16372
0
  return (reg_size == AFL_REG_NONE) ? 0
16373
0
   : (reg_size == AFL_REG_32) ? 32
16374
0
   : (reg_size == AFL_REG_64) ? 64
16375
0
   : (reg_size == AFL_REG_128) ? 128
16376
0
   : -1;
16377
0
}
16378
16379
bool
16380
_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
16381
220
{
16382
220
  FILE *file = ptr;
16383
16384
220
  BFD_ASSERT (abfd != NULL && ptr != NULL);
16385
16386
  /* Print normal ELF private data.  */
16387
220
  _bfd_elf_print_private_bfd_data (abfd, ptr);
16388
16389
  /* xgettext:c-format */
16390
220
  fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
16391
16392
220
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O32)
16393
11
    fprintf (file, _(" [abi=O32]"));
16394
209
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_O64)
16395
5
    fprintf (file, _(" [abi=O64]"));
16396
204
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI32)
16397
3
    fprintf (file, _(" [abi=EABI32]"));
16398
201
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64)
16399
0
    fprintf (file, _(" [abi=EABI64]"));
16400
201
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
16401
82
    fprintf (file, _(" [abi unknown]"));
16402
119
  else if (ABI_N32_P (abfd))
16403
25
    fprintf (file, _(" [abi=N32]"));
16404
94
  else if (ABI_64_P (abfd))
16405
37
    fprintf (file, _(" [abi=64]"));
16406
57
  else
16407
57
    fprintf (file, _(" [no abi set]"));
16408
16409
220
  if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_1)
16410
116
    fprintf (file, " [mips1]");
16411
104
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_2)
16412
3
    fprintf (file, " [mips2]");
16413
101
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_3)
16414
3
    fprintf (file, " [mips3]");
16415
98
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_4)
16416
11
    fprintf (file, " [mips4]");
16417
87
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_5)
16418
2
    fprintf (file, " [mips5]");
16419
85
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32)
16420
5
    fprintf (file, " [mips32]");
16421
80
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64)
16422
2
    fprintf (file, " [mips64]");
16423
78
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2)
16424
33
    fprintf (file, " [mips32r2]");
16425
45
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R2)
16426
3
    fprintf (file, " [mips64r2]");
16427
42
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6)
16428
29
    fprintf (file, " [mips32r6]");
16429
13
  else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6)
16430
5
    fprintf (file, " [mips64r6]");
16431
8
  else
16432
8
    fprintf (file, _(" [unknown ISA]"));
16433
16434
220
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
16435
73
    fprintf (file, " [mdmx]");
16436
16437
220
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
16438
22
    fprintf (file, " [mips16]");
16439
16440
220
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
16441
105
    fprintf (file, " [micromips]");
16442
16443
220
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
16444
84
    fprintf (file, " [nan2008]");
16445
16446
220
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
16447
84
    fprintf (file, " [old fp64]");
16448
16449
220
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
16450
69
    fprintf (file, " [32bitmode]");
16451
151
  else
16452
151
    fprintf (file, _(" [not 32bitmode]"));
16453
16454
220
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
16455
90
    fprintf (file, " [noreorder]");
16456
16457
220
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
16458
40
    fprintf (file, " [PIC]");
16459
16460
220
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
16461
99
    fprintf (file, " [CPIC]");
16462
16463
220
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
16464
33
    fprintf (file, " [XGOT]");
16465
16466
220
  if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
16467
93
    fprintf (file, " [UCODE]");
16468
16469
220
  fputc ('\n', file);
16470
16471
220
  if (mips_elf_tdata (abfd)->abiflags_valid)
16472
0
    {
16473
0
      Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
16474
0
      fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
16475
0
      fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
16476
0
      if (abiflags->isa_rev > 1)
16477
0
  fprintf (file, "r%d", abiflags->isa_rev);
16478
0
      fprintf (file, "\nGPR size: %d",
16479
0
         get_mips_reg_size (abiflags->gpr_size));
16480
0
      fprintf (file, "\nCPR1 size: %d",
16481
0
         get_mips_reg_size (abiflags->cpr1_size));
16482
0
      fprintf (file, "\nCPR2 size: %d",
16483
0
         get_mips_reg_size (abiflags->cpr2_size));
16484
0
      fputs ("\nFP ABI: ", file);
16485
0
      print_mips_fp_abi_value (file, abiflags->fp_abi);
16486
0
      fputs ("ISA Extension: ", file);
16487
0
      print_mips_isa_ext (file, abiflags->isa_ext);
16488
0
      fputs ("\nASEs:", file);
16489
0
      print_mips_ases (file, abiflags->ases);
16490
0
      fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
16491
0
      fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
16492
0
      fputc ('\n', file);
16493
0
    }
16494
16495
220
  return true;
16496
220
}
16497
16498
const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
16499
{
16500
  { STRING_COMMA_LEN (".lit4"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16501
  { STRING_COMMA_LEN (".lit8"),   0, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16502
  { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
16503
  { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,     SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16504
  { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS,   SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16505
  { STRING_COMMA_LEN (".ucode"),  0, SHT_MIPS_UCODE, 0 },
16506
  { STRING_COMMA_LEN (".MIPS.xhash"),  0, SHT_MIPS_XHASH,   SHF_ALLOC },
16507
  { NULL,         0,  0, 0,        0 }
16508
};
16509
16510
/* Merge non visibility st_other attributes.  Ensure that the
16511
   STO_OPTIONAL flag is copied into h->other, even if this is not a
16512
   definiton of the symbol.  */
16513
void
16514
_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
16515
              unsigned int st_other,
16516
              bool definition,
16517
              bool dynamic ATTRIBUTE_UNUSED)
16518
0
{
16519
0
  if ((st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
16520
0
    {
16521
0
      unsigned char other;
16522
16523
0
      other = (definition ? st_other : h->other);
16524
0
      other &= ~ELF_ST_VISIBILITY (-1);
16525
0
      h->other = other | ELF_ST_VISIBILITY (h->other);
16526
0
    }
16527
16528
0
  if (!definition
16529
0
      && ELF_MIPS_IS_OPTIONAL (st_other))
16530
0
    h->other |= STO_OPTIONAL;
16531
0
}
16532
16533
/* Decide whether an undefined symbol is special and can be ignored.
16534
   This is the case for OPTIONAL symbols on IRIX.  */
16535
bool
16536
_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
16537
0
{
16538
0
  return ELF_MIPS_IS_OPTIONAL (h->other) != 0;
16539
0
}
16540
16541
bool
16542
_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
16543
0
{
16544
0
  return (sym->st_shndx == SHN_COMMON
16545
0
    || sym->st_shndx == SHN_MIPS_ACOMMON
16546
0
    || sym->st_shndx == SHN_MIPS_SCOMMON);
16547
0
}
16548
16549
/* Return address for Ith PLT stub in section PLT, for relocation REL
16550
   or (bfd_vma) -1 if it should not be included.  */
16551
16552
bfd_vma
16553
_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
16554
         const arelent *rel ATTRIBUTE_UNUSED)
16555
0
{
16556
0
  return (plt->vma
16557
0
    + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
16558
0
    + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
16559
0
}
16560
16561
/* Build a table of synthetic symbols to represent the PLT.  As with MIPS16
16562
   and microMIPS PLT slots we may have a many-to-one mapping between .plt
16563
   and .got.plt and also the slots may be of a different size each we walk
16564
   the PLT manually fetching instructions and matching them against known
16565
   patterns.  To make things easier standard MIPS slots, if any, always come
16566
   first.  As we don't create proper ELF symbols we use the UDATA.I member
16567
   of ASYMBOL to carry ISA annotation.  The encoding used is the same as
16568
   with the ST_OTHER member of the ELF symbol.  */
16569
16570
long
16571
_bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
16572
            long symcount ATTRIBUTE_UNUSED,
16573
            asymbol **syms ATTRIBUTE_UNUSED,
16574
            long dynsymcount, asymbol **dynsyms,
16575
            asymbol **ret)
16576
31
{
16577
31
  static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
16578
31
  static const char microsuffix[] = "@micromipsplt";
16579
31
  static const char m16suffix[] = "@mips16plt";
16580
31
  static const char mipssuffix[] = "@plt";
16581
16582
31
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
16583
31
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
16584
31
  bool micromips_p = MICROMIPS_P (abfd);
16585
31
  Elf_Internal_Shdr *hdr;
16586
31
  bfd_byte *plt_data;
16587
31
  bfd_vma plt_offset;
16588
31
  unsigned int other;
16589
31
  bfd_vma entry_size;
16590
31
  bfd_vma plt0_size;
16591
31
  asection *relplt;
16592
31
  bfd_vma opcode;
16593
31
  asection *plt;
16594
31
  asymbol *send;
16595
31
  size_t size;
16596
31
  char *names;
16597
31
  long counti;
16598
31
  arelent *p;
16599
31
  asymbol *s;
16600
31
  char *nend;
16601
31
  long count;
16602
31
  long pi;
16603
31
  long i;
16604
31
  long n;
16605
16606
31
  *ret = NULL;
16607
16608
31
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
16609
24
    return 0;
16610
16611
7
  relplt = bfd_get_section_by_name (abfd, ".rel.plt");
16612
7
  if (relplt == NULL)
16613
7
    return 0;
16614
16615
0
  hdr = &elf_section_data (relplt)->this_hdr;
16616
0
  if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
16617
0
    return 0;
16618
16619
0
  plt = bfd_get_section_by_name (abfd, ".plt");
16620
0
  if (plt == NULL || (plt->flags & SEC_HAS_CONTENTS) == 0)
16621
0
    return 0;
16622
16623
0
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
16624
0
  if (!(*slurp_relocs) (abfd, relplt, dynsyms, true))
16625
0
    return -1;
16626
0
  p = relplt->relocation;
16627
16628
  /* Calculating the exact amount of space required for symbols would
16629
     require two passes over the PLT, so just pessimise assuming two
16630
     PLT slots per relocation.  */
16631
0
  count = NUM_SHDR_ENTRIES (hdr);
16632
0
  counti = count * bed->s->int_rels_per_ext_rel;
16633
0
  size = 2 * count * sizeof (asymbol);
16634
0
  size += count * (sizeof (mipssuffix) +
16635
0
       (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
16636
0
  for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
16637
0
    size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
16638
16639
  /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too.  */
16640
0
  size += sizeof (asymbol) + sizeof (pltname);
16641
16642
0
  if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
16643
0
    return -1;
16644
16645
0
  if (plt->size < 16)
16646
0
    return -1;
16647
16648
0
  s = *ret = bfd_malloc (size);
16649
0
  if (s == NULL)
16650
0
    return -1;
16651
0
  send = s + 2 * count + 1;
16652
16653
0
  names = (char *) send;
16654
0
  nend = (char *) s + size;
16655
0
  n = 0;
16656
16657
0
  opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
16658
0
  if (opcode == 0x3302fffe)
16659
0
    {
16660
0
      if (!micromips_p)
16661
0
  return -1;
16662
0
      plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
16663
0
      other = STO_MICROMIPS;
16664
0
    }
16665
0
  else if (opcode == 0x0398c1d0)
16666
0
    {
16667
0
      if (!micromips_p)
16668
0
  return -1;
16669
0
      plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
16670
0
      other = STO_MICROMIPS;
16671
0
    }
16672
0
  else
16673
0
    {
16674
0
      plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
16675
0
      other = 0;
16676
0
    }
16677
16678
0
  s->the_bfd = abfd;
16679
0
  s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
16680
0
  s->section = plt;
16681
0
  s->value = 0;
16682
0
  s->name = names;
16683
0
  s->udata.i = other;
16684
0
  memcpy (names, pltname, sizeof (pltname));
16685
0
  names += sizeof (pltname);
16686
0
  ++s, ++n;
16687
16688
0
  pi = 0;
16689
0
  for (plt_offset = plt0_size;
16690
0
       plt_offset + 8 <= plt->size && s < send;
16691
0
       plt_offset += entry_size)
16692
0
    {
16693
0
      bfd_vma gotplt_addr;
16694
0
      const char *suffix;
16695
0
      bfd_vma gotplt_hi;
16696
0
      bfd_vma gotplt_lo;
16697
0
      size_t suffixlen;
16698
16699
0
      opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
16700
16701
      /* Check if the second word matches the expected MIPS16 instruction.  */
16702
0
      if (opcode == 0x651aeb00)
16703
0
  {
16704
0
    if (micromips_p)
16705
0
      return -1;
16706
    /* Truncated table???  */
16707
0
    if (plt_offset + 16 > plt->size)
16708
0
      break;
16709
0
    gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
16710
0
    entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
16711
0
    suffixlen = sizeof (m16suffix);
16712
0
    suffix = m16suffix;
16713
0
    other = STO_MIPS16;
16714
0
  }
16715
      /* Likewise the expected microMIPS instruction (no insn32 mode).  */
16716
0
      else if (opcode == 0xff220000)
16717
0
  {
16718
0
    if (!micromips_p)
16719
0
      return -1;
16720
0
    gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
16721
0
    gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16722
0
    gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
16723
0
    gotplt_lo <<= 2;
16724
0
    gotplt_addr = gotplt_hi + gotplt_lo;
16725
0
    gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
16726
0
    entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
16727
0
    suffixlen = sizeof (microsuffix);
16728
0
    suffix = microsuffix;
16729
0
    other = STO_MICROMIPS;
16730
0
  }
16731
      /* Likewise the expected microMIPS instruction (insn32 mode).  */
16732
0
      else if ((opcode & 0xffff0000) == 0xff2f0000)
16733
0
  {
16734
0
    gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16735
0
    gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
16736
0
    gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16737
0
    gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16738
0
    gotplt_addr = gotplt_hi + gotplt_lo;
16739
0
    entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
16740
0
    suffixlen = sizeof (microsuffix);
16741
0
    suffix = microsuffix;
16742
0
    other = STO_MICROMIPS;
16743
0
  }
16744
      /* Otherwise assume standard MIPS code.  */
16745
0
      else
16746
0
  {
16747
0
    gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
16748
0
    gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
16749
0
    gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16750
0
    gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16751
0
    gotplt_addr = gotplt_hi + gotplt_lo;
16752
0
    entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
16753
0
    suffixlen = sizeof (mipssuffix);
16754
0
    suffix = mipssuffix;
16755
0
    other = 0;
16756
0
  }
16757
      /* Truncated table???  */
16758
0
      if (plt_offset + entry_size > plt->size)
16759
0
  break;
16760
16761
0
      for (i = 0;
16762
0
     i < count && p[pi].address != gotplt_addr;
16763
0
     i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
16764
16765
0
      if (i < count)
16766
0
  {
16767
0
    size_t namelen;
16768
0
    size_t len;
16769
16770
0
    *s = **p[pi].sym_ptr_ptr;
16771
    /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
16772
       we are defining a symbol, ensure one of them is set.  */
16773
0
    if ((s->flags & BSF_LOCAL) == 0)
16774
0
      s->flags |= BSF_GLOBAL;
16775
0
    s->flags |= BSF_SYNTHETIC;
16776
0
    s->section = plt;
16777
0
    s->value = plt_offset;
16778
0
    s->name = names;
16779
0
    s->udata.i = other;
16780
16781
0
    len = strlen ((*p[pi].sym_ptr_ptr)->name);
16782
0
    namelen = len + suffixlen;
16783
0
    if (names + namelen > nend)
16784
0
      break;
16785
16786
0
    memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
16787
0
    names += len;
16788
0
    memcpy (names, suffix, suffixlen);
16789
0
    names += suffixlen;
16790
16791
0
    ++s, ++n;
16792
0
    pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
16793
0
  }
16794
0
    }
16795
16796
0
  free (plt_data);
16797
16798
0
  return n;
16799
0
}
16800
16801
/* Return the ABI flags associated with ABFD if available.  */
16802
16803
Elf_Internal_ABIFlags_v0 *
16804
bfd_mips_elf_get_abiflags (bfd *abfd)
16805
2.14M
{
16806
2.14M
  struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
16807
16808
2.14M
  return tdata->abiflags_valid ? &tdata->abiflags : NULL;
16809
2.14M
}
16810
16811
/* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
16812
   field.  Taken from `libc-abis.h' generated at GNU libc build time.
16813
   Using a MIPS_ prefix as other libc targets use different values.  */
16814
enum
16815
{
16816
  MIPS_LIBC_ABI_DEFAULT = 0,
16817
  MIPS_LIBC_ABI_MIPS_PLT,
16818
  MIPS_LIBC_ABI_UNIQUE,
16819
  MIPS_LIBC_ABI_MIPS_O32_FP64,
16820
  MIPS_LIBC_ABI_ABSOLUTE,
16821
  MIPS_LIBC_ABI_XHASH,
16822
  MIPS_LIBC_ABI_MAX
16823
};
16824
16825
bool
16826
_bfd_mips_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
16827
1
{
16828
1
  struct mips_elf_link_hash_table *htab = NULL;
16829
1
  Elf_Internal_Ehdr *i_ehdrp;
16830
16831
1
  if (!_bfd_elf_init_file_header (abfd, link_info))
16832
0
    return false;
16833
16834
1
  i_ehdrp = elf_elfheader (abfd);
16835
1
  if (link_info)
16836
0
    {
16837
0
      htab = mips_elf_hash_table (link_info);
16838
0
      BFD_ASSERT (htab != NULL);
16839
0
    }
16840
16841
1
  if (htab != NULL
16842
1
      && htab->use_plts_and_copy_relocs
16843
1
      && htab->root.target_os != is_vxworks)
16844
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
16845
16846
1
  if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
16847
1
      || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
16848
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
16849
16850
  /* Mark that we need support for absolute symbols in the dynamic loader.  */
16851
1
  if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
16852
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
16853
16854
  /* Mark that we need support for .MIPS.xhash in the dynamic linker,
16855
     if it is the only hash section that will be created.  */
16856
1
  if (link_info && link_info->emit_gnu_hash && !link_info->emit_hash)
16857
0
    i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_XHASH;
16858
1
  return true;
16859
1
}
16860
16861
int
16862
_bfd_mips_elf_compact_eh_encoding
16863
  (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16864
0
{
16865
0
  return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
16866
0
}
16867
16868
/* Return the opcode for can't unwind.  */
16869
16870
int
16871
_bfd_mips_elf_cant_unwind_opcode
16872
  (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16873
0
{
16874
0
  return COMPACT_EH_CANT_UNWIND_OPCODE;
16875
0
}
16876
16877
/* Record a position XLAT_LOC in the xlat translation table, associated with
16878
   the hash entry H.  The entry in the translation table will later be
16879
   populated with the real symbol dynindx.  */
16880
16881
void
16882
_bfd_mips_elf_record_xhash_symbol (struct elf_link_hash_entry *h,
16883
           bfd_vma xlat_loc)
16884
0
{
16885
0
  struct mips_elf_link_hash_entry *hmips;
16886
16887
0
  hmips = (struct mips_elf_link_hash_entry *) h;
16888
0
  hmips->mipsxhash_loc = xlat_loc;
16889
0
}