Coverage Report

Created: 2026-11-21 09:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf32-loongarch.c
Line
Count
Source
1
#line 1 "elfnn-loongarch.c"
2
/* LoongArch-specific support for 32-bit ELF.
3
   Copyright (C) 2021-2026 Free Software Foundation, Inc.
4
   Contributed by Loongson Ltd.
5
6
   This file is part of BFD, the Binary File Descriptor library.
7
8
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 3 of the License, or
11
   (at your option) any later version.
12
13
   This program is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
18
   You should have received a copy of the GNU General Public License
19
   along with this program; see the file COPYING3.  If not,
20
   see <http://www.gnu.org/licenses/>.  */
21
22
#include "ansidecl.h"
23
#include "sysdep.h"
24
#include "bfd.h"
25
#include "libbfd.h"
26
0
#define ARCH_SIZE 32
27
#include "elf-bfd.h"
28
#include "objalloc.h"
29
#include "splay-tree.h"
30
#include "elf/loongarch.h"
31
#include "elfxx-loongarch.h"
32
#include "opcode/loongarch.h"
33
34
static bool
35
loongarch_info_to_howto_rela (bfd *abfd, arelent *cache_ptr,
36
            Elf_Internal_Rela *dst)
37
485
{
38
485
  cache_ptr->howto = loongarch_elf_rtype_to_howto (abfd,
39
485
               ELF32_R_TYPE (dst->r_info));
40
485
  return cache_ptr->howto != NULL;
41
485
}
42
43
/* LoongArch ELF linker hash entry.  */
44
struct loongarch_elf_link_hash_entry
45
{
46
  struct elf_link_hash_entry elf;
47
48
0
#define GOT_UNKNOWN 0
49
0
#define GOT_NORMAL  1
50
0
#define GOT_TLS_GD  2
51
0
#define GOT_TLS_IE  4
52
0
#define GOT_TLS_LE  8
53
0
#define GOT_TLS_GDESC 16
54
55
#define GOT_TLS_GD_BOTH_P(tls_type) \
56
0
  ((tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_GDESC))
57
#define GOT_TLS_GD_ANY_P(tls_type) \
58
0
  ((tls_type & GOT_TLS_GD) || (tls_type & GOT_TLS_GDESC))
59
  char tls_type;
60
};
61
62
#define loongarch_elf_hash_entry(ent) \
63
0
  ((struct loongarch_elf_link_hash_entry *) (ent))
64
65
struct _bfd_loongarch_elf_obj_tdata
66
{
67
  struct elf_obj_tdata root;
68
69
  /* The tls_type for each local got entry.  */
70
  char *local_got_tls_type;
71
};
72
73
#define _bfd_loongarch_elf_tdata(abfd)  \
74
0
  ((struct _bfd_loongarch_elf_obj_tdata *) (abfd)->tdata.any)
75
76
#define _bfd_loongarch_elf_local_got_tls_type(abfd) \
77
0
  (_bfd_loongarch_elf_tdata (abfd)->local_got_tls_type)
78
79
#define _bfd_loongarch_elf_tls_type(abfd, h, symndx)      \
80
0
  (*((h) != NULL              \
81
0
     ? &loongarch_elf_hash_entry (h)->tls_type        \
82
0
     : &_bfd_loongarch_elf_local_got_tls_type (abfd)[symndx]))
83
84
#define is_loongarch_elf(bfd)           \
85
0
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour      \
86
0
   && elf_tdata (bfd) != NULL            \
87
0
   && elf_object_id (bfd) == LARCH_ELF_DATA)
88
89
static bool
90
elf32_loongarch_object (bfd *abfd)
91
23.7k
{
92
23.7k
  return bfd_elf_allocate_object (abfd,
93
23.7k
          sizeof (struct _bfd_loongarch_elf_obj_tdata));
94
23.7k
}
95
96
struct relr_entry
97
{
98
  asection *sec;
99
  bfd_vma off;
100
};
101
102
struct loongarch_elf_link_hash_table
103
{
104
  struct elf_link_hash_table elf;
105
106
  /* Short-cuts to get to dynamic linker sections.  */
107
  asection *sdyntdata;
108
109
  /* Small local sym to section mapping cache.  */
110
  struct sym_cache sym_cache;
111
112
  /* Used by local STT_GNU_IFUNC symbols.  */
113
  htab_t loc_hash_table;
114
  void *loc_hash_memory;
115
116
  /* The max alignment of output sections.  */
117
  bfd_vma max_alignment;
118
119
  /* The data segment phase, don't relax the section
120
     when it is exp_seg_relro_adjust.  */
121
  int *data_segment_phase;
122
123
  /* Array of relative relocs to be emitted in DT_RELR format.  */
124
  bfd_size_type relr_alloc;
125
  bfd_size_type relr_count;
126
  struct relr_entry *relr;
127
128
  /* Sorted output addresses of above relative relocs.  */
129
  bfd_vma *relr_sorted;
130
131
  /* Layout recomputation count.  */
132
  bfd_size_type relr_layout_iter;
133
134
  /* In BFD DT_RELR is implemented as a "relaxation."  If in a relax trip
135
     size_relative_relocs is updating the layout, relax_section may see
136
     a partially updated state (some sections have vma updated but the
137
     others do not), and it's unsafe to do the normal relaxation.  */
138
  bool layout_mutating_for_relr;
139
140
  /* Pending relaxation (byte deletion) operations meant for roughly
141
     sequential access.  */
142
  splay_tree pending_delete_ops;
143
144
  /* If any input contains a reloc potentially removing bytes, i.e.
145
     R_LARCH_ALIGN or R_LARCH_RELAX.  */
146
  bool reloc_may_remove_bytes;
147
};
148
149
struct loongarch_elf_section_data
150
{
151
  struct bfd_elf_section_data elf;
152
153
  /* &htab->relr[i] where i is the smallest number s.t.
154
     elf_section_data (htab->relr[i].sec) == &elf.
155
     NULL if there exists no such i.  */
156
  struct relr_entry *relr;
157
};
158
159
/* We need an additional field in elf_section_data to handle complex
160
   interactions between DT_RELR and relaxation.  */
161
static bool
162
loongarch_elf_new_section_hook (bfd *abfd, asection *sec)
163
2.07k
{
164
2.07k
  struct loongarch_elf_section_data *sdata;
165
166
2.07k
  sdata = bfd_zalloc (abfd, sizeof (*sdata));
167
2.07k
  if (!sdata)
168
0
    return false;
169
2.07k
  sec->used_by_bfd = sdata;
170
171
2.07k
  return _bfd_elf_new_section_hook (abfd, sec);
172
2.07k
}
173
174
#define loongarch_elf_section_data(x) \
175
0
  ((struct loongarch_elf_section_data *) elf_section_data (x))
176
177
/* Get the LoongArch ELF linker hash table from a link_info structure.  */
178
#define loongarch_elf_hash_table(p)         \
179
0
    ((struct loongarch_elf_link_hash_table *) ((p)->hash))    \
180
181
/* True if the STT_SECTION symbol of this section is referenced
182
   by a relocation with a nonzero addend.  */
183
0
#define loongarch_sec_sym_has_reloc_ref(sec) ((sec)->sec_flg1)
184
185
/* During linker relaxation, indicates whether the section has already
186
   undergone alignment processing and no more byte deletion is permitted.  */
187
0
#define loongarch_sec_closed_for_deletion(sec) ((sec)->sec_flg0)
188
189
0
#define MINUS_ONE ((bfd_vma) 0 - 1)
190
191
0
#define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
192
193
0
#define LARCH_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
194
0
#define LARCH_ELF_WORD_BYTES (1 << LARCH_ELF_LOG_WORD_BYTES)
195
196
0
#define PLT_HEADER_INSNS 8
197
0
#define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
198
199
0
#define PLT_ENTRY_INSNS 4
200
0
#define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
201
202
0
#define GOT_ENTRY_SIZE (LARCH_ELF_WORD_BYTES)
203
204
/* Reserve two entries of GOTPLT for ld.so, one is used for PLT
205
   resolver _dl_runtime_resolve, the other is used for link map.  */
206
0
#define GOTPLT_HEADER_SIZE (GOT_ENTRY_SIZE * 2)
207
208
#define elf_backend_want_got_plt 1
209
210
#define elf_backend_plt_readonly 1
211
212
#define elf_backend_want_plt_sym 1
213
#define elf_backend_plt_alignment 4
214
#define elf_backend_can_gc_sections 1
215
#define elf_backend_can_refcount 1
216
#define elf_backend_want_got_sym 1
217
218
#define elf_backend_got_header_size (GOT_ENTRY_SIZE * 1)
219
220
#define elf_backend_want_dynrelro 1
221
#define elf_backend_rela_normal 1
222
#define elf_backend_default_execstack 0
223
224
#define IS_LOONGARCH_TLS_TRANS_RELOC(R_TYPE)  \
225
0
  ((R_TYPE) == R_LARCH_TLS_DESC_PC_HI20        \
226
0
   || (R_TYPE) == R_LARCH_TLS_DESC_PC_LO12    \
227
0
   || (R_TYPE) == R_LARCH_TLS_DESC_LD        \
228
0
   || (R_TYPE) == R_LARCH_TLS_DESC_CALL        \
229
0
   || (R_TYPE) == R_LARCH_TLS_IE_PC_HI20      \
230
0
   || (R_TYPE) == R_LARCH_TLS_IE_PC_LO12      \
231
0
   || (R_TYPE) == R_LARCH_TLS_DESC_PCADD_HI20 \
232
0
   || (R_TYPE) == R_LARCH_TLS_DESC_PCADD_LO12 \
233
0
   || (R_TYPE) == R_LARCH_TLS_IE_PCADD_HI20   \
234
0
   || (R_TYPE) == R_LARCH_TLS_IE_PCADD_LO12)
235
236
#define IS_OUTDATED_TLS_LE_RELOC(R_TYPE)  \
237
  ((R_TYPE) == R_LARCH_TLS_LE_HI20    \
238
   || (R_TYPE) == R_LARCH_TLS_LE_LO12   \
239
   || (R_TYPE) == R_LARCH_TLS_LE64_LO20   \
240
   || (R_TYPE) == R_LARCH_TLS_LE64_HI12)
241
242
#define IS_CALL_RELOC(R_TYPE)   \
243
0
  ((R_TYPE) == R_LARCH_B26    \
244
0
   ||(R_TYPE) == R_LARCH_CALL36 \
245
0
   ||(R_TYPE) == R_LARCH_CALL30)
246
247
/* If TLS GD/IE need dynamic relocations, INDX will be the dynamic indx,
248
   and set NEED_RELOC to true used in allocate_dynrelocs and
249
   loongarch_elf_relocate_section for TLS GD/IE.  */
250
#define LARCH_TLS_GD_IE_NEED_DYN_RELOC(INFO, DYN, H, INDX, NEED_RELOC) \
251
0
  do \
252
0
    { \
253
0
      if ((H) != NULL \
254
0
    && (H)->dynindx != -1 \
255
0
    && WILL_CALL_FINISH_DYNAMIC_SYMBOL ((DYN), \
256
0
            bfd_link_pic (INFO), (H))) \
257
0
      (INDX) = (H)->dynindx; \
258
0
      if (((H) == NULL \
259
0
      || ELF_ST_VISIBILITY ((H)->other) == STV_DEFAULT \
260
0
      || (H)->root.type != bfd_link_hash_undefweak) \
261
0
      && (!bfd_link_executable (INFO) \
262
0
        || (INDX) != 0)) \
263
0
      (NEED_RELOC) = true; \
264
0
    } \
265
0
    while (0)
266
267
/* TL;DR always use it in this file instead when you want to type
268
   SYMBOL_REFERENCES_LOCAL.
269
270
   It's like SYMBOL_REFERENCES_LOCAL, but it returns true for local
271
   protected functions.  It happens to be same as SYMBOL_CALLS_LOCAL but
272
   let's not reuse SYMBOL_CALLS_LOCAL or "CALLS" may puzzle people.
273
274
   We do generate a PLT entry when someone attempts to la.pcrel an external
275
   function.  But we never really implemented "R_LARCH_COPY", thus we've
276
   never supported la.pcrel an external symbol unless the loaded address is
277
   only used for locating a function to be called.  Thus the PLT entry is
278
   a normal PLT entry, not intended to be a so-called "canonical PLT entry"
279
   on the ports supporting copy relocation.  So attempting to la.pcrel an
280
   external function will just break pointer equality, even it's a
281
   STV_DEFAULT function:
282
283
   $ cat t.c
284
   #include <assert.h>
285
   void check(void *p) {assert(p == check);}
286
   $ cat main.c
287
   extern void check(void *);
288
   int main(void) { check(check); }
289
   $ cc t.c -fPIC -shared -o t.so
290
   $ cc main.c -mdirect-extern-access t.so -Wl,-rpath=. -fpie -pie
291
   $ ./a.out
292
   a.out: t.c:2: check: Assertion `p == check' failed.
293
   Aborted
294
295
   Thus handling STV_PROTECTED function specially just fixes nothing:
296
   adding -fvisibility=protected compiling t.c will not magically fix
297
   the inequality.  The only possible and correct fix is not to use
298
   -mdirect-extern-access.
299
300
   So we should remove this special handling, because it's only an
301
   unsuccessful workaround for invalid code and it's penalizing valid
302
   code.  */
303
#define LARCH_REF_LOCAL(info, h) \
304
0
  (_bfd_elf_symbol_refs_local_p ((h), (info), true))
305
306
/* Generate a PLT header.  */
307
308
static bool
309
loongarch_make_plt_header (bfd_vma got_plt_addr, bfd_vma plt_header_addr,
310
         uint32_t *entry)
311
0
{
312
0
  bfd_vma pcrel = got_plt_addr - plt_header_addr;
313
0
  bfd_vma hi, lo;
314
315
0
  if (pcrel + 0x80000800 > 0xffffffff)
316
0
    {
317
0
      _bfd_error_handler (_("%#" PRIx64 " invaild imm"), (uint64_t) pcrel);
318
0
      bfd_set_error (bfd_error_bad_value);
319
0
      return false;
320
0
    }
321
0
  hi = ((pcrel + 0x800) >> 12) & 0xfffff;
322
0
  lo = pcrel & 0xfff;
323
324
  /* pcaddu12i  $t2, %hi(%pcrel(.got.plt))
325
     sub.[wd]   $t1, $t1, $t3
326
     ld.[wd]    $t3, $t2, %lo(%pcrel(.got.plt)) # _dl_runtime_resolve
327
     addi.[wd]  $t1, $t1, -(PLT_HEADER_SIZE + 12)
328
     addi.[wd]  $t0, $t2, %lo(%pcrel(.got.plt))
329
     srli.[wd]  $t1, $t1, log2(16 / GOT_ENTRY_SIZE)
330
     ld.[wd]    $t0, $t0, GOT_ENTRY_SIZE
331
     jirl   $r0, $t3, 0 */
332
333
0
  if (GOT_ENTRY_SIZE == 8)
334
0
    {
335
0
      entry[0] = 0x1c00000e | (hi & 0xfffff) << 5;
336
0
      entry[1] = 0x0011bdad;
337
0
      entry[2] = 0x28c001cf | (lo & 0xfff) << 10;
338
0
      entry[3] = 0x02c001ad | ((-(PLT_HEADER_SIZE + 12)) & 0xfff) << 10;
339
0
      entry[4] = 0x02c001cc | (lo & 0xfff) << 10;
340
0
      entry[5] = 0x004501ad | (4 - LARCH_ELF_LOG_WORD_BYTES) << 10;
341
0
      entry[6] = 0x28c0018c | GOT_ENTRY_SIZE << 10;
342
0
      entry[7] = 0x4c0001e0;
343
0
    }
344
0
  else
345
0
    {
346
0
      entry[0] = 0x1c00000e | (hi & 0xfffff) << 5;
347
0
      entry[1] = 0x00113dad;
348
0
      entry[2] = 0x288001cf | (lo & 0xfff) << 10;
349
0
      entry[3] = 0x028001ad | ((-(PLT_HEADER_SIZE + 12)) & 0xfff) << 10;
350
0
      entry[4] = 0x028001cc | (lo & 0xfff) << 10;
351
0
      entry[5] = 0x004481ad | (4 - LARCH_ELF_LOG_WORD_BYTES) << 10;
352
0
      entry[6] = 0x2880018c | GOT_ENTRY_SIZE << 10;
353
0
      entry[7] = 0x4c0001e0;
354
0
    }
355
0
  return true;
356
0
}
357
358
/* Generate a PLT entry.  */
359
360
static bool
361
loongarch_make_plt_entry (bfd_vma got_plt_entry_addr, bfd_vma plt_entry_addr,
362
        uint32_t *entry)
363
0
{
364
0
  bfd_vma pcrel = got_plt_entry_addr - plt_entry_addr;
365
0
  bfd_vma hi, lo;
366
367
0
  if (pcrel + 0x80000800 > 0xffffffff)
368
0
    {
369
0
      _bfd_error_handler (_("%#" PRIx64 " invaild imm"), (uint64_t) pcrel);
370
0
      bfd_set_error (bfd_error_bad_value);
371
0
      return false;
372
0
    }
373
0
  hi = ((pcrel + 0x800) >> 12) & 0xfffff;
374
0
  lo = pcrel & 0xfff;
375
376
0
  entry[0] = 0x1c00000f | (hi & 0xfffff) << 5;
377
0
  entry[1] = ((GOT_ENTRY_SIZE == 8 ? 0x28c001ef : 0x288001ef)
378
0
        | (lo & 0xfff) << 10);
379
0
  entry[2] = 0x4c0001ed;  /* jirl $r13, $15, 0 */
380
0
  entry[3] = 0x03400000;  /* nop */
381
382
0
  return true;
383
0
}
384
385
/* Create an entry in an LoongArch ELF linker hash table.  */
386
387
static struct bfd_hash_entry *
388
link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
389
       const char *string)
390
0
{
391
0
  struct loongarch_elf_link_hash_entry *eh;
392
393
  /* Allocate the structure if it has not already been allocated by a
394
     subclass.  */
395
0
  if (entry == NULL)
396
0
    {
397
0
      entry = bfd_hash_allocate (table, sizeof (*eh));
398
0
      if (entry == NULL)
399
0
  return entry;
400
0
    }
401
402
  /* Call the allocation method of the superclass.  */
403
0
  entry = _bfd_elf_link_hash_newfunc (entry, table, string);
404
0
  if (entry != NULL)
405
0
    {
406
0
      eh = (struct loongarch_elf_link_hash_entry *) entry;
407
0
      eh->tls_type = GOT_UNKNOWN;
408
0
    }
409
410
0
  return entry;
411
0
}
412
413
/* Compute a hash of a local hash entry.  We use elf_link_hash_entry
414
  for local symbol so that we can handle local STT_GNU_IFUNC symbols
415
  as global symbol.  We reuse indx and dynstr_index for local symbol
416
  hash since they aren't used by global symbols in this backend.  */
417
418
static hashval_t
419
elf32_loongarch_local_htab_hash (const void *ptr)
420
0
{
421
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) ptr;
422
0
  return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
423
0
}
424
425
/* Compare local hash entries.  */
426
427
static int
428
elf32_loongarch_local_htab_eq (const void *ptr1, const void *ptr2)
429
0
{
430
0
  struct elf_link_hash_entry *h1 = (struct elf_link_hash_entry *) ptr1;
431
0
  struct elf_link_hash_entry *h2 = (struct elf_link_hash_entry *) ptr2;
432
433
0
  return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
434
0
}
435
436
/* Find and/or create a hash entry for local symbol.  */
437
static struct elf_link_hash_entry *
438
elf32_loongarch_get_local_sym_hash (struct loongarch_elf_link_hash_table *htab,
439
            bfd *abfd, const Elf_Internal_Rela *rel,
440
            bool create)
441
0
{
442
0
  struct loongarch_elf_link_hash_entry e, *ret;
443
0
  asection *sec = abfd->sections;
444
0
  hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id, ELF32_R_SYM (rel->r_info));
445
0
  void **slot;
446
447
0
  e.elf.indx = sec->id;
448
0
  e.elf.dynstr_index = ELF32_R_SYM (rel->r_info);
449
0
  slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
450
0
           create ? INSERT : NO_INSERT);
451
452
0
  if (!slot)
453
0
    return NULL;
454
455
0
  if (*slot)
456
0
    {
457
0
      ret = (struct loongarch_elf_link_hash_entry *) *slot;
458
0
      return &ret->elf;
459
0
    }
460
461
0
  ret = ((struct loongarch_elf_link_hash_entry *)
462
0
   objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
463
0
       sizeof (struct loongarch_elf_link_hash_entry)));
464
0
  if (ret)
465
0
    {
466
0
      memset (ret, 0, sizeof (*ret));
467
0
      ret->elf.indx = sec->id;
468
0
      ret->elf.pointer_equality_needed = 0;
469
0
      ret->elf.dynstr_index = ELF32_R_SYM (rel->r_info);
470
0
      ret->elf.dynindx = -1;
471
0
      ret->elf.needs_plt = 0;
472
0
      ret->elf.plt.refcount = -1;
473
0
      ret->elf.got.refcount = -1;
474
0
      ret->elf.def_dynamic = 0;
475
0
      ret->elf.def_regular = 1;
476
0
      ret->elf.ref_dynamic = 0; /* This should be always 0 for local.  */
477
0
      ret->elf.ref_regular = 0;
478
0
      ret->elf.forced_local = 1;
479
0
      ret->elf.root.type = bfd_link_hash_defined;
480
0
      *slot = ret;
481
0
    }
482
0
  return &ret->elf;
483
0
}
484
485
/* Destroy an LoongArch elf linker hash table.  */
486
487
static void
488
elf32_loongarch_link_hash_table_free (bfd *obfd)
489
0
{
490
0
  struct loongarch_elf_link_hash_table *ret;
491
0
  ret = (struct loongarch_elf_link_hash_table *) obfd->link.hash;
492
493
0
  if (ret->loc_hash_table)
494
0
    htab_delete (ret->loc_hash_table);
495
0
  if (ret->loc_hash_memory)
496
0
    objalloc_free ((struct objalloc *) ret->loc_hash_memory);
497
498
0
  _bfd_elf_link_hash_table_free (obfd);
499
0
}
500
501
/* Create a LoongArch ELF linker hash table.  */
502
503
static struct bfd_link_hash_table *
504
loongarch_elf_link_hash_table_create (bfd *abfd)
505
0
{
506
0
  struct loongarch_elf_link_hash_table *ret;
507
0
  bfd_size_type amt = sizeof (struct loongarch_elf_link_hash_table);
508
509
0
  ret = (struct loongarch_elf_link_hash_table *) bfd_zmalloc (amt);
510
0
  if (ret == NULL)
511
0
    return NULL;
512
513
0
  if (!_bfd_elf_link_hash_table_init
514
0
      (&ret->elf, abfd, link_hash_newfunc,
515
0
       sizeof (struct loongarch_elf_link_hash_entry)))
516
0
    {
517
0
      free (ret);
518
0
      return NULL;
519
0
    }
520
521
0
  ret->max_alignment = MINUS_ONE;
522
523
0
  ret->loc_hash_table = htab_try_create (1024, elf32_loongarch_local_htab_hash,
524
0
           elf32_loongarch_local_htab_eq, NULL);
525
0
  ret->loc_hash_memory = objalloc_create ();
526
0
  if (!ret->loc_hash_table || !ret->loc_hash_memory)
527
0
    {
528
0
      elf32_loongarch_link_hash_table_free (abfd);
529
0
      return NULL;
530
0
    }
531
0
  ret->elf.root.hash_table_free = elf32_loongarch_link_hash_table_free;
532
533
0
  return &ret->elf.root;
534
0
}
535
536
/* Merge backend specific data from an object file to the output
537
   object file when linking.  */
538
539
static bool
540
elf32_loongarch_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
541
0
{
542
0
  bfd *obfd = info->output_bfd;
543
0
  flagword in_flags = elf_elfheader (ibfd)->e_flags;
544
0
  flagword out_flags = elf_elfheader (obfd)->e_flags;
545
546
0
  if (!is_loongarch_elf (ibfd))
547
0
    return true;
548
549
0
  if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
550
0
    {
551
0
      _bfd_error_handler (_("%pB: ABI is incompatible with that of "
552
0
          "the selected emulation:\n"
553
0
          "  target emulation `%s' does not match `%s'"),
554
0
        ibfd, bfd_get_target (ibfd), bfd_get_target (obfd));
555
0
      return false;
556
0
    }
557
558
0
  if (!_bfd_elf_merge_object_attributes (ibfd, info))
559
0
    return false;
560
561
  /* If the input BFD is not a dynamic object and it does not contain any
562
     non-data sections, do not account its ABI.  For example, various
563
     packages produces such data-only relocatable objects with
564
     `ld -r -b binary` or `objcopy`, and these objects have zero e_flags.
565
     But they are compatible with all ABIs.  */
566
0
  if (!(ibfd->flags & DYNAMIC))
567
0
    {
568
0
      asection *sec;
569
0
      bool have_code_sections = false;
570
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
571
0
  if ((bfd_section_flags (sec)
572
0
       & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
573
0
      == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
574
0
    {
575
0
      have_code_sections = true;
576
0
      break;
577
0
    }
578
0
      if (!have_code_sections)
579
0
  return true;
580
0
    }
581
582
0
  if (!elf_flags_init (obfd))
583
0
    {
584
0
      elf_flags_init (obfd) = true;
585
0
      elf_elfheader (obfd)->e_flags = in_flags;
586
0
      return true;
587
0
    }
588
0
  else if (out_flags != in_flags)
589
0
    {
590
0
      if ((EF_LOONGARCH_IS_OBJ_V0 (out_flags)
591
0
     && EF_LOONGARCH_IS_OBJ_V1 (in_flags))
592
0
    || (EF_LOONGARCH_IS_OBJ_V0 (in_flags)
593
0
        && EF_LOONGARCH_IS_OBJ_V1 (out_flags)))
594
0
  {
595
0
    elf_elfheader (obfd)->e_flags |= EF_LOONGARCH_OBJABI_V1;
596
0
    out_flags = elf_elfheader (obfd)->e_flags;
597
0
    in_flags = out_flags;
598
0
  }
599
0
    }
600
601
  /* Disallow linking different ABIs.  */
602
  /* Only check relocation version.
603
     The obj_v0 is compatible with obj_v1.  */
604
0
  if (EF_LOONGARCH_ABI(out_flags ^ in_flags) & EF_LOONGARCH_ABI_MASK)
605
0
    {
606
0
      _bfd_error_handler (_("%pB: can't link different ABI object."), ibfd);
607
0
      goto fail;
608
0
    }
609
610
0
  return true;
611
612
0
 fail:
613
0
  bfd_set_error (bfd_error_bad_value);
614
0
  return false;
615
0
}
616
617
/* Create the .got section.  */
618
619
static bool
620
loongarch_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
621
0
{
622
0
  flagword flags;
623
0
  char *name;
624
0
  asection *s, *s_got;
625
0
  struct elf_link_hash_entry *h;
626
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
627
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
628
629
  /* This function may be called more than once.  */
630
0
  if (htab->sgot != NULL)
631
0
    return true;
632
633
0
  flags = bed->dynamic_sec_flags;
634
0
  name = bed->rela_plts_and_copies_p ? ".rela.got" : ".rel.got";
635
0
  s = bfd_make_section_anyway_with_flags (abfd, name, flags | SEC_READONLY);
636
637
0
  if (s == NULL || !bfd_set_section_alignment (s, bed->s->log_file_align))
638
0
    return false;
639
0
  htab->srelgot = s;
640
641
0
  s = s_got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
642
0
  if (s == NULL || !bfd_set_section_alignment (s, bed->s->log_file_align))
643
0
    return false;
644
0
  htab->sgot = s;
645
646
  /* The first bit of the global offset table is the header.  */
647
0
  s->size += bed->got_header_size;
648
649
0
  if (bed->want_got_plt)
650
0
    {
651
0
      s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
652
0
      if (s == NULL || !bfd_set_section_alignment (s, bed->s->log_file_align))
653
0
  return false;
654
0
      htab->sgotplt = s;
655
656
      /* Reserve room for the header.  */
657
0
      s->size = GOTPLT_HEADER_SIZE;
658
0
    }
659
660
0
  if (bed->want_got_sym)
661
0
    {
662
      /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
663
   section.  We don't do this in the linker script because we don't want
664
   to define the symbol if we are not creating a global offset table.  */
665
0
      h = _bfd_elf_define_linkage_sym (abfd, info, s_got,
666
0
               "_GLOBAL_OFFSET_TABLE_");
667
0
      elf_hash_table (info)->hgot = h;
668
0
      if (h == NULL)
669
0
  return false;
670
0
    }
671
0
  return true;
672
0
}
673
674
/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
675
   .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
676
   hash table.  */
677
678
static bool
679
loongarch_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
680
0
{
681
0
  struct loongarch_elf_link_hash_table *htab;
682
683
0
  htab = loongarch_elf_hash_table (info);
684
0
  BFD_ASSERT (htab != NULL);
685
686
0
  if (!loongarch_elf_create_got_section (dynobj, info))
687
0
    return false;
688
689
0
  if (!_bfd_elf_create_dynamic_sections (dynobj, info))
690
0
    return false;
691
692
0
  if (!bfd_link_pic (info))
693
0
    htab->sdyntdata
694
0
      = bfd_make_section_anyway_with_flags (dynobj, ".tdata.dyn",
695
0
              SEC_ALLOC | SEC_THREAD_LOCAL);
696
697
0
  if (!htab->elf.splt || !htab->elf.srelplt || !htab->elf.sdynbss
698
0
      || (!bfd_link_pic (info) && (!htab->elf.srelbss || !htab->sdyntdata)))
699
0
    abort ();
700
701
0
  return true;
702
0
}
703
704
static bool
705
loongarch_elf_record_tls_and_got_reference (bfd *abfd,
706
              struct bfd_link_info *info,
707
              struct elf_link_hash_entry *h,
708
              unsigned long symndx,
709
              char tls_type,
710
              bool with_relax_reloc)
711
0
{
712
0
  struct loongarch_elf_link_hash_table *htab = loongarch_elf_hash_table (info);
713
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
714
715
  /* This is a global offset table entry for a local symbol.  */
716
0
  if (elf_local_got_refcounts (abfd) == NULL)
717
0
    {
718
0
      bfd_size_type size =
719
0
  symtab_hdr->sh_info * (sizeof (bfd_vma) + sizeof (tls_type));
720
0
      if (!(elf_local_got_refcounts (abfd) = bfd_zalloc (abfd, size)))
721
0
  return false;
722
0
      _bfd_loongarch_elf_local_got_tls_type (abfd) =
723
0
  (char *) (elf_local_got_refcounts (abfd) + symtab_hdr->sh_info);
724
0
    }
725
726
0
  switch (tls_type)
727
0
    {
728
0
    case GOT_NORMAL:
729
0
    case GOT_TLS_GD:
730
0
    case GOT_TLS_IE:
731
0
    case GOT_TLS_GDESC:
732
      /* Need GOT.  */
733
0
      if (htab->elf.sgot == NULL
734
0
    && !loongarch_elf_create_got_section (htab->elf.dynobj, info))
735
0
  return false;
736
0
      if (h)
737
0
  {
738
0
    if (h->got.refcount < 0)
739
0
      h->got.refcount = 0;
740
0
    h->got.refcount++;
741
0
  }
742
0
      else
743
0
  elf_local_got_refcounts (abfd)[symndx]++;
744
0
      break;
745
0
    case GOT_TLS_LE:
746
      /* No need for GOT.  */
747
0
      break;
748
0
    default:
749
0
      _bfd_error_handler (_("Internal error: unreachable."));
750
0
      return false;
751
0
    }
752
753
0
  char *new_tls_type = &_bfd_loongarch_elf_tls_type (abfd, h, symndx);
754
0
  *new_tls_type |= tls_type;
755
756
  /* If DESC relocs can do transitions and accessed by both IE and DESC,
757
     transition DESC to IE.  */
758
0
  if (with_relax_reloc
759
0
      && (*new_tls_type & GOT_TLS_IE) && (*new_tls_type & GOT_TLS_GDESC))
760
0
    *new_tls_type &= ~ (GOT_TLS_GDESC);
761
762
0
  if ((*new_tls_type & GOT_NORMAL) && (*new_tls_type & ~GOT_NORMAL))
763
0
    {
764
0
      _bfd_error_handler (_("%pB: `%s' accessed both as normal and "
765
0
          "thread local symbol"),
766
0
        abfd,
767
0
        h ? h->root.root.string : "<local>");
768
0
      return false;
769
0
    }
770
771
0
  return true;
772
0
}
773
774
static unsigned int
775
loongarch_reloc_got_type (unsigned int r_type)
776
0
{
777
0
  switch (r_type)
778
0
    {
779
0
      case R_LARCH_TLS_DESC_PC_HI20:
780
0
      case R_LARCH_TLS_DESC_PC_LO12:
781
0
      case R_LARCH_TLS_DESC_LD:
782
0
      case R_LARCH_TLS_DESC_CALL:
783
0
      case R_LARCH_TLS_DESC_PCADD_HI20:
784
0
      case R_LARCH_TLS_DESC_PCADD_LO12:
785
0
  return GOT_TLS_GDESC;
786
787
0
      case R_LARCH_TLS_IE_PC_HI20:
788
0
      case R_LARCH_TLS_IE_PC_LO12:
789
0
      case R_LARCH_TLS_IE_PCADD_HI20:
790
0
      case R_LARCH_TLS_IE_PCADD_LO12:
791
0
  return GOT_TLS_IE;
792
793
0
      default:
794
0
  break;
795
0
    }
796
0
  return GOT_UNKNOWN;
797
0
}
798
799
typedef struct
800
{
801
  /* PC value.  */
802
  bfd_vma address;
803
  /* Relocation value with addend.  */
804
  bfd_vma value;
805
  unsigned int hi_sym;
806
  struct elf_link_hash_entry *h;
807
} loongarch_pcrel_hi_reloc;
808
809
typedef struct loongarch_pcrel_lo_reloc
810
{
811
  /* PC value of pcaddu12i.  */
812
  bfd_vma address;
813
  /* Internal relocation.  */
814
  Elf_Internal_Rela *reloc;
815
  /* loongarch_elf_relocate_section can only handle an input section at a time,
816
     so we can only resolved pcadd_hi20 and pcadd_lo12 in the same section.
817
     If these pcrel relocs are not in the same section we should report
818
     dangerous relocation errors.  */
819
  asection *input_section;
820
  struct bfd_link_info *info;
821
  reloc_howto_type *howto;
822
  bfd_byte *contents;
823
  /* The next loongarch_pcrel_lo_reloc.  */
824
  struct loongarch_pcrel_lo_reloc *next;
825
} loongarch_pcrel_lo_reloc;
826
827
typedef struct
828
{
829
  /* Hash table for loongarch_pcrel_hi_reloc.  */
830
  htab_t hi_relocs;
831
  /* Linked list for loongarch_pcrel_lo_reloc.  */
832
  loongarch_pcrel_lo_reloc *lo_relocs;
833
} loongarch_pcrel_relocs;
834
835
/* Hash function of the pcrel_hi_reloc hash table.  */
836
static hashval_t
837
loongarch_pcrel_reloc_hash (const void *entry)
838
0
{
839
0
  const loongarch_pcrel_hi_reloc *e = entry;
840
0
  return (hashval_t)(e->address >> 2);
841
0
}
842
843
/* Comparison function of the pcrel_hi_reloc hash table.  */
844
static int
845
loongarch_pcrel_reloc_eq (const void *entry1, const void *entry2)
846
0
{
847
0
  const loongarch_pcrel_hi_reloc *e1 = entry1, *e2 = entry2;
848
0
  return e1->address == e2->address;
849
0
}
850
851
static bool
852
loongarch_init_pcrel_relocs (loongarch_pcrel_relocs *p)
853
0
{
854
0
  p->lo_relocs = NULL;
855
0
  p->hi_relocs = htab_create (1024, loongarch_pcrel_reloc_hash,
856
0
            loongarch_pcrel_reloc_eq, free);
857
0
  return p->hi_relocs != NULL;
858
0
}
859
860
static void
861
loongarch_free_pcrel_reloc (loongarch_pcrel_relocs *p)
862
0
{
863
0
  loongarch_pcrel_lo_reloc *cur = p->lo_relocs;
864
865
0
  while (cur != NULL)
866
0
    {
867
0
      loongarch_pcrel_lo_reloc *next = cur->next;
868
0
      free (cur);
869
0
      cur = next;
870
0
    }
871
0
  htab_delete (p->hi_relocs);
872
0
}
873
874
/* sym only need pass on relax. just pass 0 on relocate sections.  */
875
static bool
876
loongarch_record_pcrel_hi_reloc (loongarch_pcrel_relocs *p,
877
         bfd_vma addr,
878
         bfd_vma value,
879
         unsigned int sym,
880
         struct elf_link_hash_entry *h)
881
0
{
882
0
  loongarch_pcrel_hi_reloc entry = {addr, value, sym, h};
883
0
  loongarch_pcrel_hi_reloc **slot =
884
0
    (loongarch_pcrel_hi_reloc **)htab_find_slot (p->hi_relocs, &entry, INSERT);
885
886
0
  if (*slot != NULL)
887
0
    _bfd_error_handler (_("duplicate pcrel_hi record"));
888
889
0
  *slot = (loongarch_pcrel_hi_reloc *) bfd_malloc (sizeof (loongarch_pcrel_hi_reloc));
890
0
  if (*slot == NULL)
891
0
    return false;
892
0
  **slot = entry;
893
0
  return true;
894
0
}
895
896
static bool
897
loongarch_record_pcrel_lo_reloc (loongarch_pcrel_relocs *p,
898
         bfd_vma addr,
899
         Elf_Internal_Rela *reloc,
900
         asection *input_section,
901
         struct bfd_link_info *info,
902
         reloc_howto_type *howto,
903
         bfd_byte *contents)
904
0
{
905
0
  loongarch_pcrel_lo_reloc *entry;
906
0
  entry = (loongarch_pcrel_lo_reloc *) bfd_malloc (sizeof (loongarch_pcrel_lo_reloc));
907
0
  if (entry == NULL)
908
0
    return false;
909
0
  *entry = (loongarch_pcrel_lo_reloc) {addr, reloc, input_section, info,
910
0
               howto, contents, p->lo_relocs};
911
0
  p->lo_relocs = entry;
912
0
  return true;
913
0
}
914
915
static loongarch_pcrel_hi_reloc *
916
loongarch_find_pcrel_hi_reloc (loongarch_pcrel_relocs *p, bfd_vma address)
917
0
{
918
0
  loongarch_pcrel_hi_reloc search = {address, 0, 0, NULL};
919
0
  loongarch_pcrel_hi_reloc *entry = htab_find (p->hi_relocs, &search);
920
921
0
  if (entry == NULL)
922
0
    _bfd_error_handler (_("pcrel_lo missing matching pcrel_hi"));
923
924
0
  return entry;
925
0
}
926
927
/* Return true if tls type transition can be performed.  */
928
static bool
929
loongarch_can_trans_tls (bfd *input_bfd, asection *sec,
930
       const Elf_Internal_Rela *rel,
931
       struct bfd_link_info *info,
932
       struct elf_link_hash_entry *h,
933
       bfd_vma symval,
934
       loongarch_pcrel_relocs *pcrel_relocs)
935
0
{
936
0
  char symbol_tls_type;
937
0
  unsigned int reloc_got_type;
938
0
  unsigned long r_type = ELF32_R_TYPE (rel->r_info);
939
0
  unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
940
941
  /* Only TLS DESC/IE in normal code mode will perform type
942
     transition.  */
943
0
  if (! IS_LOONGARCH_TLS_TRANS_RELOC (r_type))
944
0
    return false;
945
946
  /* Only record hi reloc in loongarch_elf_relax_section.  */
947
0
  if (sec != NULL && pcrel_relocs != NULL
948
0
      && (r_type == R_LARCH_TLS_DESC_PCADD_HI20
949
0
    || r_type == R_LARCH_TLS_IE_PCADD_HI20))
950
0
    {
951
0
      bfd_vma pc = sec_addr (sec) + rel->r_offset;
952
0
      loongarch_record_pcrel_hi_reloc (pcrel_relocs, pc, 0, r_symndx, h);
953
0
    }
954
955
  /* Obtaining tls got type here may occur before
956
     loongarch_elf_record_tls_and_got_reference, so it is necessary
957
     to ensure that tls got type has been initialized, otherwise it
958
     is set to GOT_UNKNOWN.  */
959
0
  symbol_tls_type = GOT_UNKNOWN;
960
961
  /* Only find hi reloc in loongarch_elf_relax_section.  */
962
0
  loongarch_pcrel_hi_reloc *hi;
963
0
  if (sec != NULL && pcrel_relocs != NULL
964
0
      && (r_type == R_LARCH_TLS_DESC_PCADD_LO12
965
0
    || r_type == R_LARCH_TLS_IE_PCADD_LO12))
966
0
    {
967
0
      hi = loongarch_find_pcrel_hi_reloc(pcrel_relocs, symval);
968
0
      h = hi->h;
969
0
      r_symndx = hi->hi_sym;
970
0
    }
971
972
0
  if (_bfd_loongarch_elf_local_got_tls_type (input_bfd) || h)
973
0
    symbol_tls_type = _bfd_loongarch_elf_tls_type (input_bfd, h, r_symndx);
974
975
0
  reloc_got_type = loongarch_reloc_got_type (r_type);
976
977
0
  if (symbol_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (reloc_got_type))
978
0
    return true;
979
980
0
  if (! bfd_link_executable (info))
981
0
      return false;
982
983
0
  if (h && h->root.type == bfd_link_hash_undefweak)
984
0
    return false;
985
986
0
  return true;
987
0
}
988
989
/* The type of relocation that can be transitioned.  */
990
static unsigned int
991
loongarch_tls_transition_without_check (struct bfd_link_info *info,
992
          unsigned int r_type,
993
          struct elf_link_hash_entry *h)
994
0
{
995
0
  bool local_exec = bfd_link_executable (info)
996
0
        && LARCH_REF_LOCAL (info, h);
997
998
0
  switch (r_type)
999
0
    {
1000
0
      case R_LARCH_TLS_DESC_PC_HI20:
1001
0
  return (local_exec
1002
0
    ? R_LARCH_TLS_LE_HI20
1003
0
    : R_LARCH_TLS_IE_PC_HI20);
1004
1005
0
      case R_LARCH_TLS_DESC_PC_LO12:
1006
0
  return (local_exec
1007
0
    ? R_LARCH_TLS_LE_LO12
1008
0
    : R_LARCH_TLS_IE_PC_LO12);
1009
1010
0
      case R_LARCH_TLS_DESC_PCADD_HI20:
1011
0
  return (local_exec
1012
0
    ? R_LARCH_TLS_LE_HI20
1013
0
    : R_LARCH_TLS_IE_PCADD_HI20);
1014
1015
0
      case R_LARCH_TLS_DESC_PCADD_LO12:
1016
0
  return (local_exec
1017
0
    ? R_LARCH_TLS_LE_HI20
1018
0
    : R_LARCH_TLS_IE_PCADD_LO12);
1019
1020
0
      case R_LARCH_TLS_DESC_LD:
1021
0
      case R_LARCH_TLS_DESC_CALL:
1022
0
  return R_LARCH_NONE;
1023
1024
0
      case R_LARCH_TLS_IE_PC_HI20:
1025
0
      case R_LARCH_TLS_IE_PCADD_HI20:
1026
0
  return local_exec ? R_LARCH_TLS_LE_HI20 : r_type;
1027
1028
0
      case R_LARCH_TLS_IE_PC_LO12:
1029
0
      case R_LARCH_TLS_IE_PCADD_LO12:
1030
0
  return local_exec ? R_LARCH_TLS_LE_LO12 : r_type;
1031
1032
0
      default:
1033
0
  break;
1034
0
    }
1035
1036
0
  return r_type;
1037
0
}
1038
1039
static unsigned int
1040
loongarch_tls_transition (bfd *input_bfd,
1041
        struct bfd_link_info *info,
1042
        struct elf_link_hash_entry *h,
1043
        const Elf_Internal_Rela *rel)
1044
0
{
1045
0
  unsigned long r_type = ELF32_R_TYPE (rel->r_info);
1046
0
  if (! loongarch_can_trans_tls (input_bfd, NULL, rel, info, h, 0, NULL))
1047
0
    return r_type;
1048
1049
0
  return loongarch_tls_transition_without_check (info, r_type, h);
1050
0
}
1051
1052
static bool
1053
bad_static_reloc (struct bfd_link_info *info,
1054
      bfd *abfd, const Elf_Internal_Rela *rel,
1055
      asection *sec, unsigned r_type,
1056
      struct elf_link_hash_entry *h,
1057
      const Elf_Internal_Sym *isym)
1058
0
{
1059
0
  reloc_howto_type * r = loongarch_elf_rtype_to_howto (abfd, r_type);
1060
0
  const char *object;
1061
0
  const char *pic_opt;
1062
0
  const char *name = NULL;
1063
1064
  /* If this, the problem is we are referring an external symbol in
1065
     a way only working for local symbols, not PC-relative vs.
1066
   absolute.  */
1067
0
  bool bad_extern_access =
1068
0
    (bfd_link_pde (info)
1069
0
     || r_type == R_LARCH_PCREL20_S2
1070
0
     || r_type == R_LARCH_PCALA_HI20
1071
0
     || r_type == R_LARCH_PCADD_HI20);
1072
1073
0
  if (h)
1074
0
    name = h->root.root.string;
1075
0
  else if (isym)
1076
0
    name = bfd_elf_string_from_elf_section (abfd,
1077
0
              elf_symtab_hdr (abfd).sh_link,
1078
0
              isym->st_name);
1079
0
  if (name == NULL || *name == '\0')
1080
0
    name = "<nameless>";
1081
1082
0
  if (bfd_link_dll (info))
1083
0
    {
1084
0
      object = _("a shared object");
1085
0
      pic_opt = "-fPIC";
1086
0
    }
1087
0
  else
1088
0
    {
1089
0
      if (bfd_link_pie (info))
1090
0
  object = _("a PIE object");
1091
0
      else
1092
0
  object = _("a PDE object");
1093
1094
0
      pic_opt = bad_extern_access ? "-mno-direct-extern-access" : "-fPIE";
1095
0
    }
1096
1097
0
  (*_bfd_error_handler)
1098
0
   (_("%pB:(%pA+%#lx): relocation %s against `%s` can not be used when making "
1099
0
      "%s; recompile with %s%s"),
1100
0
    abfd, sec, (long) rel->r_offset, r ? r->name : _("<unknown>"), name,
1101
0
    object, pic_opt,
1102
0
    bad_extern_access ? _(" and check the symbol visibility") : "");
1103
0
  bfd_set_error (bfd_error_bad_value);
1104
0
  return false;
1105
0
}
1106
1107
/* Look through the relocs for a section during the first phase, and
1108
   allocate space in the global offset table or procedure linkage
1109
   table.  */
1110
1111
static bool
1112
loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
1113
          asection *sec, const Elf_Internal_Rela *relocs)
1114
0
{
1115
0
  struct loongarch_elf_link_hash_table *htab;
1116
0
  Elf_Internal_Shdr *symtab_hdr;
1117
0
  struct elf_link_hash_entry **sym_hashes;
1118
0
  const Elf_Internal_Rela *rel;
1119
0
  asection *sreloc = NULL;
1120
1121
0
  htab = loongarch_elf_hash_table (info);
1122
1123
0
  if (bfd_link_relocatable (info))
1124
0
    {
1125
0
      if (!htab->reloc_may_remove_bytes)
1126
0
  for (rel = relocs; rel < relocs + sec->reloc_count; rel++)
1127
0
    switch (ELF32_R_TYPE (rel->r_info))
1128
0
      {
1129
0
      case R_LARCH_ALIGN:
1130
0
      case R_LARCH_RELAX:
1131
0
        htab->reloc_may_remove_bytes = true;
1132
0
        return true;
1133
0
      default:
1134
0
        continue;
1135
0
      }
1136
1137
      /* No need for more checks with ld -r.  */
1138
0
      return true;
1139
0
    }
1140
1141
0
  if (htab->elf.dynobj == NULL
1142
0
      && !_bfd_elf_link_dynobj (info))
1143
0
    return false;
1144
1145
0
  symtab_hdr = &elf_symtab_hdr (abfd);
1146
0
  sym_hashes = elf_sym_hashes (abfd);
1147
1148
0
  for (rel = relocs; rel < relocs + sec->reloc_count; rel++)
1149
0
    {
1150
0
      unsigned int r_type;
1151
0
      unsigned int r_symndx;
1152
0
      struct elf_link_hash_entry *h;
1153
0
      const Elf_Internal_Sym *isym = NULL;
1154
0
      bool is_abs_symbol = false;
1155
1156
0
      r_symndx = ELF32_R_SYM (rel->r_info);
1157
0
      r_type = ELF32_R_TYPE (rel->r_info);
1158
1159
0
      if (r_symndx < symtab_hdr->sh_info)
1160
0
  {
1161
    /* A local symbol.  */
1162
0
    isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
1163
0
    if (isym == NULL)
1164
0
      return false;
1165
1166
0
    is_abs_symbol = isym->st_shndx == SHN_ABS;
1167
0
    if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1168
0
      {
1169
0
        h = elf32_loongarch_get_local_sym_hash (htab, abfd, rel, true);
1170
0
        if (h == NULL)
1171
0
    return false;
1172
1173
0
        h->type = STT_GNU_IFUNC;
1174
0
        h->ref_regular = 1;
1175
0
      }
1176
0
    else
1177
0
      h = NULL;
1178
0
  }
1179
0
      else
1180
0
  {
1181
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1182
0
    while (h->root.type == bfd_link_hash_indirect
1183
0
     || h->root.type == bfd_link_hash_warning)
1184
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
1185
0
    is_abs_symbol = bfd_is_abs_symbol (&h->root);
1186
0
  }
1187
1188
0
      if (r_symndx < symtab_hdr->sh_info
1189
0
    && rel->r_addend != 0
1190
0
    && r_type != R_LARCH_ALIGN
1191
0
    && ELF_ST_TYPE (isym->st_info) == STT_SECTION
1192
0
    && isym->st_shndx < elf_numsections (abfd)
1193
0
    && elf_elfsections (abfd)[isym->st_shndx] != NULL)
1194
0
  {
1195
0
    asection *sym_sec
1196
0
        = elf_elfsections (abfd)[isym->st_shndx]->bfd_section;
1197
0
    if (sym_sec != NULL)
1198
0
      loongarch_sec_sym_has_reloc_ref (sym_sec) = true;
1199
0
  }
1200
1201
      /* It is referenced by a non-shared object.  */
1202
0
      if (h != NULL)
1203
0
  h->ref_regular = 1;
1204
1205
0
      if (h && h->type == STT_GNU_IFUNC)
1206
0
  {
1207
    /* Create 'irelifunc' in PIC object.  */
1208
0
    if (bfd_link_pic (info)
1209
0
        && !_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
1210
0
      return false;
1211
    /* If '.plt' not represent, create '.iplt' to deal with ifunc.  */
1212
0
    else if (!htab->elf.splt
1213
0
       && !_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
1214
0
      return false;
1215
    /* Create the ifunc sections, iplt and ipltgot, for static
1216
       executables.  */
1217
0
    if ((r_type == R_LARCH_64 || r_type == R_LARCH_32)
1218
0
        && !_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
1219
0
      return false;
1220
1221
0
    if (h->plt.refcount < 0)
1222
0
      h->plt.refcount = 0;
1223
0
    h->plt.refcount++;
1224
0
    h->needs_plt = 1;
1225
1226
0
    elf_tdata (info->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
1227
0
  }
1228
1229
0
      int need_dynreloc = 0;
1230
0
      int only_need_pcrel = 0;
1231
1232
      /* Type transitions are only possible with relocations accompanied
1233
   by R_LARCH_RELAX.  */
1234
0
      bool with_relax_reloc = false;
1235
0
      if (IS_LOONGARCH_TLS_TRANS_RELOC (r_type)
1236
0
    && rel + 1 != relocs + sec->reloc_count
1237
0
    && ELF32_R_TYPE (rel[1].r_info) == R_LARCH_RELAX)
1238
0
  {
1239
0
    r_type = loongarch_tls_transition (abfd, info, h, rel);
1240
0
    with_relax_reloc = true;
1241
0
  }
1242
1243
      /* I don't want to spend time supporting DT_RELR with old object
1244
   files doing stack-based relocs.  */
1245
0
      if (info->enable_dt_relr
1246
0
    && r_type >= R_LARCH_SOP_PUSH_PCREL
1247
0
    && r_type <= R_LARCH_SOP_POP_32_U)
1248
0
  {
1249
    /* xgettext:c-format */
1250
0
    _bfd_error_handler (_("%pB: stack based reloc type (%u) is not "
1251
0
        "supported with -z pack-relative-relocs"),
1252
0
            abfd, r_type);
1253
0
    return false;
1254
0
  }
1255
1256
0
      switch (r_type)
1257
0
  {
1258
0
  case R_LARCH_GOT_PC_HI20:
1259
0
  case R_LARCH_GOT_PCADD_HI20:
1260
0
  case R_LARCH_GOT_HI20:
1261
0
  case R_LARCH_SOP_PUSH_GPREL:
1262
0
    if (!loongarch_elf_record_tls_and_got_reference (abfd, info, h,
1263
0
                 r_symndx,
1264
0
                 GOT_NORMAL,
1265
0
                 with_relax_reloc))
1266
0
      return false;
1267
0
    break;
1268
1269
0
  case R_LARCH_TLS_LD_PC_HI20:
1270
0
  case R_LARCH_TLS_LD_PCADD_HI20:
1271
0
  case R_LARCH_TLS_LD_HI20:
1272
0
  case R_LARCH_TLS_GD_PC_HI20:
1273
0
  case R_LARCH_TLS_GD_PCADD_HI20:
1274
0
  case R_LARCH_TLS_GD_HI20:
1275
0
  case R_LARCH_TLS_LD_PCREL20_S2:
1276
0
  case R_LARCH_TLS_GD_PCREL20_S2:
1277
0
  case R_LARCH_SOP_PUSH_TLS_GD:
1278
0
    if (!loongarch_elf_record_tls_and_got_reference (abfd, info, h,
1279
0
                 r_symndx,
1280
0
                 GOT_TLS_GD,
1281
0
                 with_relax_reloc))
1282
0
      return false;
1283
0
    break;
1284
1285
0
  case R_LARCH_TLS_IE_PC_HI20:
1286
0
  case R_LARCH_TLS_IE_PCADD_HI20:
1287
0
  case R_LARCH_TLS_IE_HI20:
1288
0
  case R_LARCH_SOP_PUSH_TLS_GOT:
1289
0
    if (bfd_link_pic (info))
1290
      /* May fail for lazy-bind.  */
1291
0
      info->flags |= DF_STATIC_TLS;
1292
1293
0
    if (!loongarch_elf_record_tls_and_got_reference (abfd, info, h,
1294
0
                 r_symndx,
1295
0
                 GOT_TLS_IE,
1296
0
                 with_relax_reloc))
1297
0
      return false;
1298
0
    break;
1299
1300
0
  case R_LARCH_TLS_LE_HI20:
1301
0
  case R_LARCH_TLS_LE_HI20_R:
1302
0
  case R_LARCH_SOP_PUSH_TLS_TPREL:
1303
0
    if (!bfd_link_executable (info))
1304
0
      return bad_static_reloc (info, abfd, rel, sec, r_type, h, isym);
1305
1306
0
    if (!loongarch_elf_record_tls_and_got_reference (abfd, info, h,
1307
0
                 r_symndx,
1308
0
                 GOT_TLS_LE,
1309
0
                 with_relax_reloc))
1310
0
      return false;
1311
0
    break;
1312
1313
0
  case R_LARCH_TLS_DESC_PC_HI20:
1314
0
  case R_LARCH_TLS_DESC_PCADD_HI20:
1315
0
  case R_LARCH_TLS_DESC_HI20:
1316
0
  case R_LARCH_TLS_DESC_PCREL20_S2:
1317
0
    if (!loongarch_elf_record_tls_and_got_reference (abfd, info, h,
1318
0
                 r_symndx,
1319
0
                 GOT_TLS_GDESC,
1320
0
                 with_relax_reloc))
1321
0
      return false;
1322
0
    break;
1323
1324
0
  case R_LARCH_ABS_HI20:
1325
0
    if (bfd_link_pic (info))
1326
0
      return bad_static_reloc (info, abfd, rel, sec, r_type, h, isym);
1327
1328
    /* Fall through.  */
1329
0
  case R_LARCH_SOP_PUSH_ABSOLUTE:
1330
0
    if (h != NULL)
1331
      /* If this reloc is in a read-only section, we might
1332
         need a copy reloc.  We can't check reliably at this
1333
         stage whether the section is read-only, as input
1334
         sections have not yet been mapped to output sections.
1335
         Tentatively set the flag for now, and correct in
1336
         adjust_dynamic_symbol.  */
1337
0
      h->non_got_ref = 1;
1338
0
    break;
1339
1340
  /* Since shared library global symbols interpose, any
1341
     PC-relative relocations against external symbols
1342
     should not be used to build shared libraries.
1343
     In static PIE undefined weak symbols may be allowed
1344
     by rewriting pcaddi to addi.w if addend is in [-2048, 2048).  */
1345
0
  case R_LARCH_PCADD_HI20:
1346
0
  case R_LARCH_PCREL20_S2:
1347
0
    if (bfd_link_pic (info)
1348
0
        && (sec->flags & SEC_ALLOC) != 0
1349
0
        && (sec->flags & SEC_READONLY) != 0
1350
0
        && ! LARCH_REF_LOCAL (info, h)
1351
0
        && (!info->nointerp
1352
0
      || h->root.type != bfd_link_hash_undefweak))
1353
0
      return bad_static_reloc (info, abfd, rel, sec, r_type, h, NULL);
1354
1355
0
    break;
1356
1357
  /* For normal cmodel, pcalau12i + addi.d/w used to data.
1358
     For first version medium cmodel, pcalau12i + jirl are used to
1359
     function call, it need to creat PLT entry for STT_FUNC and
1360
     STT_GNU_IFUNC type symbol.  */
1361
0
  case R_LARCH_PCALA_HI20:
1362
0
    if (h != NULL && (STT_FUNC == h->type || STT_GNU_IFUNC == h->type))
1363
0
      {
1364
        /* For pcalau12i + jirl.  */
1365
0
        h->needs_plt = 1;
1366
0
        if (h->plt.refcount < 0)
1367
0
    h->plt.refcount = 0;
1368
0
        h->plt.refcount++;
1369
1370
0
        h->non_got_ref = 1;
1371
0
        h->pointer_equality_needed = 1;
1372
0
      }
1373
1374
    /* PC-relative relocations are allowed For first version
1375
       medium cmodel function call.  Those against undefined
1376
       weak symbol are allowed for static PIE by rewritting
1377
       pcalau12i to lu12i.w.  */
1378
0
    if (h != NULL && !h->needs_plt
1379
0
        && bfd_link_pic (info)
1380
0
        && (sec->flags & SEC_ALLOC) != 0
1381
0
        && (sec->flags & SEC_READONLY) != 0
1382
0
        && ! LARCH_REF_LOCAL (info, h)
1383
0
        && (!info->nointerp
1384
0
      || h->root.type != bfd_link_hash_undefweak))
1385
0
      return bad_static_reloc (info, abfd, rel, sec, r_type, h, NULL);
1386
1387
0
    break;
1388
1389
0
  case R_LARCH_B16:
1390
0
  case R_LARCH_B21:
1391
0
  case R_LARCH_B26:
1392
0
  case R_LARCH_CALL36:
1393
0
  case R_LARCH_CALL30:
1394
0
    if (h != NULL)
1395
0
      {
1396
0
        h->needs_plt = 1;
1397
0
        if (!bfd_link_pic (info))
1398
0
    h->non_got_ref = 1;
1399
1400
        /* We try to create PLT stub for all non-local function.  */
1401
0
        if (h->plt.refcount < 0)
1402
0
    h->plt.refcount = 0;
1403
0
        h->plt.refcount++;
1404
0
      }
1405
1406
0
    break;
1407
1408
0
  case R_LARCH_SOP_PUSH_PCREL:
1409
0
    if (h != NULL)
1410
0
      {
1411
0
        if (!bfd_link_pic (info))
1412
0
    h->non_got_ref = 1;
1413
1414
        /* We try to create PLT stub for all non-local function.  */
1415
0
        if (h->plt.refcount < 0)
1416
0
    h->plt.refcount = 0;
1417
0
        h->plt.refcount++;
1418
0
        h->pointer_equality_needed = 1;
1419
0
      }
1420
1421
0
    break;
1422
1423
0
  case R_LARCH_SOP_PUSH_PLT_PCREL:
1424
    /* This symbol requires a procedure linkage table entry.  We
1425
       actually build the entry in adjust_dynamic_symbol,
1426
       because this might be a case of linking PIC code without
1427
       linking in any dynamic objects, in which case we don't
1428
       need to generate a procedure linkage table after all.  */
1429
0
    if (h != NULL)
1430
0
      {
1431
0
        h->needs_plt = 1;
1432
0
        if (h->plt.refcount < 0)
1433
0
    h->plt.refcount = 0;
1434
0
        h->plt.refcount++;
1435
0
      }
1436
0
    break;
1437
1438
0
  case R_LARCH_TLS_DTPREL32:
1439
0
  case R_LARCH_TLS_DTPREL64:
1440
0
    need_dynreloc = 1;
1441
0
    only_need_pcrel = 1;
1442
0
    break;
1443
1444
0
  case R_LARCH_32:
1445
0
    if (ARCH_SIZE > 32
1446
0
        && bfd_link_pic (info)
1447
0
        && (sec->flags & SEC_ALLOC) != 0)
1448
0
      {
1449
0
        if (!is_abs_symbol)
1450
0
    {
1451
0
      _bfd_error_handler
1452
0
        (_("%pB: relocation R_LARCH_32 against non-absolute "
1453
0
           "symbol `%s' cannot be used in ELFCLASS64 when "
1454
0
           "making a shared object or PIE"),
1455
0
         abfd, h ? h->root.root.string : "a local symbol");
1456
0
      bfd_set_error (bfd_error_bad_value);
1457
0
      return false;
1458
0
    }
1459
0
      }
1460
1461
    /* Fall through.  */
1462
0
  case R_LARCH_JUMP_SLOT:
1463
0
  case R_LARCH_64:
1464
1465
    /* Resolved to const.  */
1466
0
    if (is_abs_symbol)
1467
0
      break;
1468
1469
0
    need_dynreloc = 1;
1470
1471
    /* If resolved symbol is defined in this object,
1472
       1. Under pie, the symbol is known.  We convert it
1473
       into R_LARCH_RELATIVE and need load-addr still.
1474
       2. Under pde, the symbol is known and we can discard R_LARCH_32.
1475
       3. Under dll, R_LARCH_32 can't be changed normally, since
1476
       its defination could be covered by the one in executable.
1477
       For symbolic, we convert it into R_LARCH_RELATIVE.
1478
       Thus, only under pde, it needs pcrel only.  We discard it.  */
1479
0
    only_need_pcrel = bfd_link_pde (info);
1480
1481
0
    if (h != NULL
1482
0
        && (!bfd_link_pic (info)
1483
0
      || h->type == STT_GNU_IFUNC))
1484
0
      {
1485
        /* This reloc might not bind locally.  */
1486
0
        h->non_got_ref = 1;
1487
0
        h->pointer_equality_needed = 1;
1488
1489
0
        if (!h->def_regular
1490
0
      || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
1491
0
    {
1492
      /* We may need a .plt entry if the symbol is a function
1493
         defined in a shared lib or is a function referenced
1494
         from the code or read-only section.  */
1495
0
      h->plt.refcount += 1;
1496
0
    }
1497
0
      }
1498
0
    break;
1499
1500
0
  case R_LARCH_GNU_VTINHERIT:
1501
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1502
0
      return false;
1503
0
    break;
1504
1505
0
  case R_LARCH_GNU_VTENTRY:
1506
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1507
0
      return false;
1508
0
    break;
1509
1510
0
  case R_LARCH_ALIGN:
1511
    /* Check against irrational R_LARCH_ALIGN relocs which may cause
1512
       removing an odd number of bytes and disrupt DT_RELR.  */
1513
0
    if (rel->r_offset % 4 != 0)
1514
0
      {
1515
        /* xgettext:c-format */
1516
0
        _bfd_error_handler (
1517
0
    _("%pB: R_LARCH_ALIGN with offset %" PRId64 " not aligned "
1518
0
      "to instruction boundary"),
1519
0
    abfd, (uint64_t) rel->r_offset);
1520
0
        return false;
1521
0
      }
1522
0
    break;
1523
1524
0
  default:
1525
0
    break;
1526
0
  }
1527
1528
      /* Record some info for sizing and allocating dynamic entry.  */
1529
0
      if (need_dynreloc && (sec->flags & SEC_ALLOC))
1530
0
  {
1531
    /* When creating a shared object, we must copy these
1532
       relocs into the output file.  We create a reloc
1533
       section in dynobj and make room for the reloc.  */
1534
0
    struct elf_dyn_relocs *p;
1535
0
    struct elf_dyn_relocs **head;
1536
1537
0
    if (sreloc == NULL)
1538
0
      {
1539
0
        sreloc
1540
0
    = _bfd_elf_make_dynamic_reloc_section (sec, htab->elf.dynobj,
1541
0
                   LARCH_ELF_LOG_WORD_BYTES,
1542
0
                   abfd, /*rela?*/ true);
1543
0
        if (sreloc == NULL)
1544
0
    return false;
1545
0
      }
1546
1547
    /* If this is a global symbol, we count the number of
1548
       relocations we need for this symbol.  */
1549
0
    if (h != NULL)
1550
0
      head = &h->dyn_relocs;
1551
0
    else
1552
0
      {
1553
        /* Track dynamic relocs needed for local syms too.  */
1554
0
        asection *s;
1555
0
        void *vpp;
1556
1557
0
        s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1558
0
        if (s == NULL)
1559
0
    s = sec;
1560
1561
0
        vpp = &elf_section_data (s)->local_dynrel;
1562
0
        head = (struct elf_dyn_relocs **) vpp;
1563
0
      }
1564
1565
0
    p = *head;
1566
0
    if (p == NULL || p->sec != sec)
1567
0
      {
1568
0
        p = bfd_alloc (htab->elf.dynobj, sizeof (*p));
1569
0
        if (p == NULL)
1570
0
    return false;
1571
0
        p->next = *head;
1572
0
        *head = p;
1573
0
        p->sec = sec;
1574
0
        p->count = 0;
1575
0
        p->pc_count = 0;
1576
0
      }
1577
1578
0
    p->count++;
1579
0
    p->pc_count += only_need_pcrel;
1580
0
  }
1581
0
    }
1582
1583
0
  return true;
1584
0
}
1585
1586
/* Find dynamic relocs for H that apply to read-only sections.  */
1587
1588
static asection *
1589
readonly_dynrelocs (struct elf_link_hash_entry *h)
1590
0
{
1591
0
  struct elf_dyn_relocs *p;
1592
1593
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
1594
0
    {
1595
0
      asection *s = p->sec->output_section;
1596
1597
0
      if (s != NULL && (s->flags & SEC_READONLY) != 0)
1598
0
  return p->sec;
1599
0
    }
1600
0
  return NULL;
1601
0
}
1602
1603
/* Adjust a symbol defined by a dynamic object and referenced by a
1604
   regular object.  The current definition is in some section of the
1605
   dynamic object, but we're not including those sections.  We have to
1606
   change the definition to something the rest of the link can
1607
   understand.  */
1608
static bool
1609
loongarch_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
1610
             struct elf_link_hash_entry *h)
1611
0
{
1612
0
  struct loongarch_elf_link_hash_table *htab;
1613
0
  bfd *dynobj;
1614
1615
0
  htab = loongarch_elf_hash_table (info);
1616
0
  BFD_ASSERT (htab != NULL);
1617
1618
0
  dynobj = htab->elf.dynobj;
1619
1620
  /* Make sure we know what is going on here.  */
1621
0
  BFD_ASSERT (dynobj != NULL
1622
0
        && (h->needs_plt
1623
0
      || h->type == STT_GNU_IFUNC
1624
0
      || h->is_weakalias
1625
0
      || (h->def_dynamic
1626
0
          && h->ref_regular
1627
0
          && !h->def_regular)));
1628
1629
  /* If this is a function, put it in the procedure linkage table.  We
1630
     will fill in the contents of the procedure linkage table later
1631
     (although we could actually do it here).  */
1632
0
  if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
1633
0
    {
1634
0
      if (h->plt.refcount <= 0
1635
0
    || (h->type != STT_GNU_IFUNC
1636
0
        && (LARCH_REF_LOCAL (info, h)
1637
0
      || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1638
0
          && h->root.type == bfd_link_hash_undefweak))))
1639
0
  {
1640
    /* This case can occur if we saw a R_LARCH_SOP_PUSH_PLT_PCREL reloc
1641
       in an input file, but the symbol was never referred to by a
1642
       dynamic object, or if all references were garbage collected.
1643
       In such a case, we don't actually need to build a PLT entry.  */
1644
0
    h->plt.offset = MINUS_ONE;
1645
0
    h->needs_plt = 0;
1646
0
  }
1647
1648
0
      return true;
1649
0
    }
1650
0
  else
1651
0
    h->plt.offset = MINUS_ONE;
1652
1653
  /* If this is a weak symbol, and there is a real definition, the
1654
     processor independent code will have arranged for us to see the
1655
     real definition first, and we can just use the same value.  */
1656
0
  if (h->is_weakalias)
1657
0
    {
1658
0
      struct elf_link_hash_entry *def = weakdef (h);
1659
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1660
0
      h->root.u.def.section = def->root.u.def.section;
1661
0
      h->root.u.def.value = def->root.u.def.value;
1662
0
      return true;
1663
0
    }
1664
1665
  /* R_LARCH_COPY is not adept glibc, not to generate.  */
1666
  /* Can not print anything, because make check ld.  */
1667
0
  return true;
1668
0
}
1669
1670
/* Allocate space in .plt, .got and associated reloc sections for
1671
   dynamic relocs.  */
1672
1673
static bool
1674
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1675
0
{
1676
0
  struct bfd_link_info *info;
1677
0
  struct loongarch_elf_link_hash_table *htab;
1678
0
  struct elf_dyn_relocs *p;
1679
1680
0
  if (h->root.type == bfd_link_hash_indirect)
1681
0
    return true;
1682
1683
0
  if (h->type == STT_GNU_IFUNC
1684
0
      && h->def_regular)
1685
0
    return true;
1686
1687
0
  info = (struct bfd_link_info *) inf;
1688
0
  htab = loongarch_elf_hash_table (info);
1689
0
  bool dyn = htab->elf.dynamic_sections_created;
1690
0
  BFD_ASSERT (htab != NULL);
1691
1692
0
  do
1693
0
    {
1694
0
      asection *plt, *gotplt, *relplt;
1695
1696
0
      if (!h->needs_plt)
1697
0
  break;
1698
1699
0
      h->needs_plt = 0;
1700
1701
0
      if (htab->elf.splt)
1702
0
  {
1703
0
    if (h->dynindx == -1 && !h->forced_local && dyn
1704
0
        && h->root.type == bfd_link_hash_undefweak)
1705
0
      {
1706
0
        if (!bfd_elf_link_record_dynamic_symbol (info, h))
1707
0
    return false;
1708
0
      }
1709
1710
0
    if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h)
1711
0
        && h->type != STT_GNU_IFUNC)
1712
0
      break;
1713
1714
0
    plt = htab->elf.splt;
1715
0
    gotplt = htab->elf.sgotplt;
1716
0
    relplt = htab->elf.srelplt;
1717
0
  }
1718
0
      else if (htab->elf.iplt)
1719
0
  {
1720
    /* .iplt only for IFUNC.  */
1721
0
    if (h->type != STT_GNU_IFUNC)
1722
0
      break;
1723
1724
0
    plt = htab->elf.iplt;
1725
0
    gotplt = htab->elf.igotplt;
1726
0
    relplt = htab->elf.irelplt;
1727
0
  }
1728
0
      else
1729
0
  break;
1730
1731
0
      if (plt->size == 0)
1732
0
  plt->size = PLT_HEADER_SIZE;
1733
1734
0
      h->plt.offset = plt->size;
1735
0
      plt->size += PLT_ENTRY_SIZE;
1736
0
      gotplt->size += GOT_ENTRY_SIZE;
1737
0
      relplt->size += sizeof (Elf32_External_Rela);
1738
1739
      /* If this symbol is not defined in a regular file, and we are
1740
   not generating a shared library, then set the symbol to this
1741
   location in the .plt.  This is required to make function
1742
   pointers compare as equal between the normal executable and
1743
   the shared library.  */
1744
0
      if (!bfd_link_pic (info)
1745
0
    && !h->def_regular)
1746
0
  {
1747
0
    h->root.u.def.section = plt;
1748
0
    h->root.u.def.value = h->plt.offset;
1749
0
  }
1750
1751
0
      h->needs_plt = 1;
1752
0
    }
1753
0
  while (0);
1754
1755
0
  if (!h->needs_plt)
1756
0
    h->plt.offset = MINUS_ONE;
1757
1758
0
  if (0 < h->got.refcount)
1759
0
    {
1760
0
      asection *s;
1761
0
      int tls_type = loongarch_elf_hash_entry (h)->tls_type;
1762
1763
      /* Make sure this symbol is output as a dynamic symbol.
1764
   Undefined weak syms won't yet be marked as dynamic.  */
1765
0
      if (h->dynindx == -1 && !h->forced_local && dyn
1766
0
    && h->root.type == bfd_link_hash_undefweak)
1767
0
  {
1768
0
    if (!bfd_elf_link_record_dynamic_symbol (info, h))
1769
0
      return false;
1770
0
  }
1771
1772
0
      s = htab->elf.sgot;
1773
0
      h->got.offset = s->size;
1774
0
      if (tls_type & (GOT_TLS_GD | GOT_TLS_IE | GOT_TLS_GDESC))
1775
0
  {
1776
0
    int indx = 0;
1777
0
    bool need_reloc = false;
1778
0
    LARCH_TLS_GD_IE_NEED_DYN_RELOC (info, dyn, h, indx,
1779
0
          need_reloc);
1780
    /* TLS_GD needs two dynamic relocs and two GOT slots.  */
1781
0
    if (tls_type & GOT_TLS_GD)
1782
0
      {
1783
0
        s->size += 2 * GOT_ENTRY_SIZE;
1784
0
        if (need_reloc)
1785
0
    htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rela);
1786
0
      }
1787
1788
    /* TLS_IE needs one dynamic reloc and one GOT slot.  */
1789
0
    if (tls_type & GOT_TLS_IE)
1790
0
      {
1791
0
        s->size += GOT_ENTRY_SIZE;
1792
0
        if (need_reloc)
1793
0
    htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1794
0
      }
1795
1796
    /* TLS_DESC needs one dynamic reloc and two GOT slot.  */
1797
0
    if (tls_type & GOT_TLS_GDESC)
1798
0
      {
1799
0
        s->size += GOT_ENTRY_SIZE * 2;
1800
0
        htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1801
0
      }
1802
0
  }
1803
1804
0
      else
1805
0
  {
1806
0
    s->size += GOT_ENTRY_SIZE;
1807
0
    if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1808
0
         || h->root.type != bfd_link_hash_undefweak)
1809
0
        && (bfd_link_pic (info)
1810
0
      || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info),
1811
0
                  h))
1812
0
        && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1813
        /* Undefined weak symbol in static PIE resolves to 0 without
1814
     any dynamic relocations.  */
1815
0
      htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1816
0
  }
1817
0
    }
1818
0
  else
1819
0
    h->got.offset = MINUS_ONE;
1820
1821
0
  if (h->dyn_relocs == NULL)
1822
0
    return true;
1823
1824
  /* Extra dynamic relocate,
1825
   * R_LARCH_64
1826
   * R_LARCH_TLS_DTPREL32
1827
   * R_LARCH_JUMP_SLOT
1828
   * R_LARCH_32.  */
1829
1830
0
  if (SYMBOL_CALLS_LOCAL (info, h))
1831
0
    {
1832
0
      struct elf_dyn_relocs **pp;
1833
1834
0
      for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
1835
0
  {
1836
0
    p->count -= p->pc_count;
1837
0
    p->pc_count = 0;
1838
0
    if (p->count == 0)
1839
0
      *pp = p->next;
1840
0
    else
1841
0
      pp = &p->next;
1842
0
  }
1843
0
    }
1844
1845
0
  if (h->root.type == bfd_link_hash_undefweak)
1846
0
    {
1847
0
      if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)
1848
0
    || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1849
0
    || (!bfd_link_pic (info) && h->non_got_ref))
1850
0
  h->dyn_relocs = NULL;
1851
0
      else if (h->dynindx == -1 && !h->forced_local)
1852
0
  {
1853
    /* Make sure this symbol is output as a dynamic symbol.
1854
       Undefined weak syms won't yet be marked as dynamic.  */
1855
0
    if (!bfd_elf_link_record_dynamic_symbol (info, h))
1856
0
      return false;
1857
1858
0
    if (h->dynindx == -1)
1859
0
      h->dyn_relocs = NULL;
1860
0
  }
1861
0
    }
1862
1863
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
1864
0
    {
1865
0
      if (discarded_section (p->sec))
1866
0
  continue;
1867
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
1868
0
      sreloc->size += p->count * sizeof (Elf32_External_Rela);
1869
0
    }
1870
1871
0
  return true;
1872
0
}
1873
1874
/* A modified version of _bfd_elf_allocate_ifunc_dyn_relocs.
1875
   For local def and ref ifunc,
1876
   dynamic relocations are stored in
1877
   1.  rela.srelgot section in dynamic object (dll or exec).
1878
   2.  rela.irelplt section in static executable.
1879
   Unlike _bfd_elf_allocate_ifunc_dyn_relocs, rela.srelgot is used
1880
   instead of rela.srelplt.  Glibc ELF loader will not support
1881
   R_LARCH_IRELATIVE relocation in rela.plt.  */
1882
1883
static bool
1884
local_allocate_ifunc_dyn_relocs (struct bfd_link_info *info,
1885
            struct elf_link_hash_entry *h,
1886
            struct elf_dyn_relocs **head,
1887
            unsigned int plt_entry_size,
1888
            unsigned int plt_header_size,
1889
            unsigned int got_entry_size,
1890
            bool avoid_plt)
1891
0
{
1892
0
  asection *plt, *gotplt, *relplt;
1893
0
  struct elf_dyn_relocs *p;
1894
0
  unsigned int sizeof_reloc;
1895
0
  elf_backend_data *bed;
1896
0
  struct elf_link_hash_table *htab;
1897
  /* If AVOID_PLT is TRUE, don't use PLT if possible.  */
1898
0
  bool use_plt = !avoid_plt || h->plt.refcount > 0;
1899
0
  bool need_dynreloc = !use_plt || bfd_link_pic (info);
1900
1901
  /* When a PIC object references a STT_GNU_IFUNC symbol defined
1902
     in executable or it isn't referenced via PLT, the address of
1903
     the resolved function may be used.  But in non-PIC executable,
1904
     the address of its plt slot may be used.  Pointer equality may
1905
     not work correctly.  PIE or non-PLT reference should be used if
1906
     pointer equality is required here.
1907
1908
     If STT_GNU_IFUNC symbol is defined in position-dependent executable,
1909
     backend should change it to the normal function and set its address
1910
     to its PLT entry which should be resolved by R_*_IRELATIVE at
1911
     run-time.  All external references should be resolved to its PLT in
1912
     executable.  */
1913
0
  if (!need_dynreloc
1914
0
      && !(bfd_link_pde (info) && h->def_regular)
1915
0
      && (h->dynindx != -1
1916
0
    || info->export_dynamic)
1917
0
      && h->pointer_equality_needed)
1918
0
    {
1919
0
      info->callbacks->fatal
1920
  /* xgettext:c-format.  */
1921
0
  (_("%P: dynamic STT_GNU_IFUNC symbol `%s' with pointer "
1922
0
     "equality in `%pB' can not be used when making an "
1923
0
     "executable; recompile with -fPIE and relink with -pie\n"),
1924
0
   h->root.root.string,
1925
0
   h->root.u.def.section->owner);
1926
0
      bfd_set_error (bfd_error_bad_value);
1927
0
      return false;
1928
0
    }
1929
1930
0
  htab = elf_hash_table (info);
1931
1932
  /* When the symbol is marked with regular reference, if PLT isn't used
1933
     or we are building a PIC object, we must keep dynamic relocation
1934
     if there is non-GOT reference and use PLT if there is PC-relative
1935
     reference.  */
1936
0
  if (need_dynreloc && h->ref_regular)
1937
0
    {
1938
0
      bool keep = false;
1939
0
      for (p = *head; p != NULL; p = p->next)
1940
0
  if (p->count)
1941
0
    {
1942
0
      h->non_got_ref = 1;
1943
      /* Need dynamic relocations for non-GOT reference.  */
1944
0
      keep = true;
1945
0
      if (p->pc_count)
1946
0
        {
1947
    /* Must use PLT for PC-relative reference.  */
1948
0
    use_plt = true;
1949
0
    need_dynreloc = bfd_link_pic (info);
1950
0
    break;
1951
0
        }
1952
0
    }
1953
0
      if (keep)
1954
0
  goto keep;
1955
0
    }
1956
1957
  /* Support garbage collection against STT_GNU_IFUNC symbols.  */
1958
0
  if (h->plt.refcount <= 0 && h->got.refcount <= 0)
1959
0
    {
1960
0
      h->got = htab->init_got_offset;
1961
0
      h->plt = htab->init_plt_offset;
1962
0
      *head = NULL;
1963
0
      return true;
1964
0
    }
1965
1966
  /* Return and discard space for dynamic relocations against it if
1967
     it is never referenced.  */
1968
0
  if (!h->ref_regular)
1969
0
    {
1970
0
      if (h->plt.refcount > 0
1971
0
    || h->got.refcount > 0)
1972
0
  abort ();
1973
0
      h->got = htab->init_got_offset;
1974
0
      h->plt = htab->init_plt_offset;
1975
0
      *head = NULL;
1976
0
      return true;
1977
0
    }
1978
1979
0
 keep:
1980
0
  bed = get_elf_backend_data (info->output_bfd);
1981
0
  if (bed->rela_plts_and_copies_p)
1982
0
    sizeof_reloc = bed->s->sizeof_rela;
1983
0
  else
1984
0
    sizeof_reloc = bed->s->sizeof_rel;
1985
1986
  /* When building a static executable, use iplt, igot.plt and
1987
     rela.iplt sections for STT_GNU_IFUNC symbols.  */
1988
0
  if (htab->splt != NULL)
1989
0
    {
1990
0
      plt = htab->splt;
1991
0
      gotplt = htab->sgotplt;
1992
      /* Change dynamic info of ifunc gotplt from srelplt to srelgot.  */
1993
0
      relplt = htab->srelgot;
1994
1995
      /* If this is the first plt entry and PLT is used, make room for
1996
   the special first entry.  */
1997
0
      if (plt->size == 0 && use_plt)
1998
0
  plt->size += plt_header_size;
1999
0
    }
2000
0
  else
2001
0
    {
2002
0
      plt = htab->iplt;
2003
0
      gotplt = htab->igotplt;
2004
0
      relplt = htab->irelplt;
2005
0
    }
2006
2007
0
  if (use_plt)
2008
0
    {
2009
      /* Don't update value of STT_GNU_IFUNC symbol to PLT.  We need
2010
   the original value for R_*_IRELATIVE.  */
2011
0
      h->plt.offset = plt->size;
2012
2013
      /* Make room for this entry in the plt/iplt section.  */
2014
0
      plt->size += plt_entry_size;
2015
2016
      /* We also need to make an entry in the got.plt/got.iplt section,
2017
   which will be placed in the got section by the linker script.  */
2018
0
      gotplt->size += got_entry_size;
2019
0
    }
2020
2021
  /* We also need to make an entry in the rela.plt/.rela.iplt
2022
     section for GOTPLT relocation if PLT is used.  */
2023
0
  if (use_plt)
2024
0
    {
2025
0
      relplt->size += sizeof_reloc;
2026
0
      relplt->reloc_count++;
2027
0
    }
2028
2029
  /* We need dynamic relocation for STT_GNU_IFUNC symbol only when
2030
     there is a non-GOT reference in a PIC object or PLT isn't used.  */
2031
0
  if (!need_dynreloc || !h->non_got_ref)
2032
0
    *head = NULL;
2033
2034
  /* Finally, allocate space.  */
2035
0
  p = *head;
2036
0
  if (p != NULL)
2037
0
    {
2038
0
      bfd_size_type count = 0;
2039
0
      do
2040
0
  {
2041
0
    count += p->count;
2042
0
    p = p->next;
2043
0
  }
2044
0
      while (p != NULL);
2045
2046
0
      htab->ifunc_resolvers = count != 0;
2047
2048
      /* Dynamic relocations are stored in
2049
   1.  rela.srelgot section in PIC object.
2050
   2.  rela.srelgot section in dynamic executable.
2051
   3.  rela.irelplt section in static executable.  */
2052
0
      if (htab->splt != NULL)
2053
0
  htab->srelgot->size += count * sizeof_reloc;
2054
0
      else
2055
0
  {
2056
0
    relplt->size += count * sizeof_reloc;
2057
0
    relplt->reloc_count += count;
2058
0
  }
2059
0
    }
2060
2061
  /* For STT_GNU_IFUNC symbol, got.plt has the real function address
2062
     and got has the PLT entry adddress.  We will load the GOT entry
2063
     with the PLT entry in finish_dynamic_symbol if it is used.  For
2064
     branch, it uses got.plt.  For symbol value, if PLT is used,
2065
     1.  Use got.plt in a PIC object if it is forced local or not
2066
     dynamic.
2067
     2.  Use got.plt in a non-PIC object if pointer equality isn't
2068
     needed.
2069
     3.  Use got.plt in PIE.
2070
     4.  Use got.plt if got isn't used.
2071
     5.  Otherwise use got so that it can be shared among different
2072
     objects at run-time.
2073
     If PLT isn't used, always use got for symbol value.
2074
     We only need to relocate got entry in PIC object or in dynamic
2075
     executable without PLT.  */
2076
0
  if (use_plt
2077
0
      && (h->got.refcount <= 0
2078
0
    || (bfd_link_pic (info)
2079
0
        && (h->dynindx == -1
2080
0
      || h->forced_local))
2081
0
    || (
2082
0
        !h->pointer_equality_needed)
2083
0
    || htab->sgot == NULL))
2084
0
    {
2085
      /* Use got.plt.  */
2086
0
      h->got.offset = (bfd_vma) -1;
2087
0
    }
2088
0
  else
2089
0
    {
2090
0
      if (!use_plt)
2091
0
  {
2092
    /* PLT isn't used.  */
2093
0
    h->plt.offset = (bfd_vma) -1;
2094
0
  }
2095
0
      if (h->got.refcount <= 0)
2096
0
  {
2097
    /* GOT isn't need when there are only relocations for static
2098
       pointers.  */
2099
0
    h->got.offset = (bfd_vma) -1;
2100
0
  }
2101
0
      else
2102
0
  {
2103
0
    h->got.offset = htab->sgot->size;
2104
0
    htab->sgot->size += got_entry_size;
2105
    /* Need to relocate the GOT entry in a PIC object or PLT isn't
2106
       used.  Otherwise, the GOT entry will be filled with the PLT
2107
       entry and dynamic GOT relocation isn't needed.  */
2108
0
    if (need_dynreloc)
2109
0
      {
2110
        /* For non-static executable, dynamic GOT relocation is in
2111
     rela.got section, but for static executable, it is
2112
     in rela.iplt section.  */
2113
0
        if (htab->splt != NULL)
2114
0
    htab->srelgot->size += sizeof_reloc;
2115
0
        else
2116
0
    {
2117
0
      relplt->size += sizeof_reloc;
2118
0
      relplt->reloc_count++;
2119
0
    }
2120
0
      }
2121
0
  }
2122
0
    }
2123
2124
0
  return true;
2125
0
}
2126
2127
/* Allocate space in .plt, .got and associated reloc sections for
2128
   ifunc dynamic relocs.  */
2129
2130
static bool
2131
elf32_allocate_ifunc_dynrelocs (struct elf_link_hash_entry *h,
2132
        struct bfd_link_info *info,
2133
        bool ref_local)
2134
0
{
2135
  /* An example of a bfd_link_hash_indirect symbol is versioned
2136
     symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
2137
     -> __gxx_personality_v0(bfd_link_hash_defined)
2138
2139
     There is no need to process bfd_link_hash_indirect symbols here
2140
     because we will also be presented with the concrete instance of
2141
     the symbol and loongarch_elf_copy_indirect_symbol () will have been
2142
     called to copy all relevant data from the generic to the concrete
2143
     symbol instance.  */
2144
0
  if (h->root.type == bfd_link_hash_indirect)
2145
0
    return true;
2146
2147
0
  if (h->root.type == bfd_link_hash_warning)
2148
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
2149
2150
  /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2151
     here if it is defined and referenced in a non-shared object.  */
2152
0
  if (h->type == STT_GNU_IFUNC && h->def_regular)
2153
0
    {
2154
0
      if (ref_local && LARCH_REF_LOCAL (info, h))
2155
0
  return local_allocate_ifunc_dyn_relocs (info, h,
2156
0
            &h->dyn_relocs,
2157
0
            PLT_ENTRY_SIZE,
2158
0
            PLT_HEADER_SIZE,
2159
0
            GOT_ENTRY_SIZE,
2160
0
            false);
2161
0
      else if (!ref_local && !LARCH_REF_LOCAL (info, h))
2162
0
  return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
2163
0
               &h->dyn_relocs,
2164
0
               PLT_ENTRY_SIZE,
2165
0
               PLT_HEADER_SIZE,
2166
0
               GOT_ENTRY_SIZE,
2167
0
               false);
2168
0
    }
2169
2170
0
  return true;
2171
0
}
2172
2173
static bool
2174
elf32_allocate_ifunc_dynrelocs_ref_local (struct elf_link_hash_entry *h,
2175
            void *info)
2176
0
{
2177
0
  return elf32_allocate_ifunc_dynrelocs (h, (struct bfd_link_info *) info,
2178
0
           true);
2179
0
}
2180
2181
static bool
2182
elf32_allocate_ifunc_dynrelocs_ref_global (struct elf_link_hash_entry *h,
2183
             void *info)
2184
0
{
2185
0
  return elf32_allocate_ifunc_dynrelocs (h, (struct bfd_link_info *) info,
2186
0
           false);
2187
0
}
2188
2189
/* Allocate space in .plt, .got and associated reloc sections for
2190
   ifunc dynamic relocs.  */
2191
2192
static int
2193
elf32_allocate_local_ifunc_dynrelocs (void **slot, void *inf)
2194
0
{
2195
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) *slot;
2196
2197
0
  if (h->type != STT_GNU_IFUNC
2198
0
      || !h->def_regular
2199
0
      || !h->ref_regular
2200
0
      || !h->forced_local
2201
0
      || h->root.type != bfd_link_hash_defined)
2202
0
    abort ();
2203
2204
0
  return elf32_allocate_ifunc_dynrelocs_ref_local (h, inf);
2205
0
}
2206
2207
/* Set DF_TEXTREL if we find any dynamic relocs that apply to
2208
   read-only sections.  */
2209
2210
static bool
2211
maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
2212
0
{
2213
0
  asection *sec;
2214
2215
0
  if (h->root.type == bfd_link_hash_indirect)
2216
0
    return true;
2217
2218
0
  sec = readonly_dynrelocs (h);
2219
0
  if (sec != NULL)
2220
0
    {
2221
0
      struct bfd_link_info *info = (struct bfd_link_info *) info_p;
2222
2223
0
      info->flags |= DF_TEXTREL;
2224
0
      info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' in "
2225
0
        "read-only section `%pA'\n"),
2226
0
            sec->owner, h->root.root.string, sec);
2227
2228
      /* Not an error, just cut short the traversal.  */
2229
0
      return false;
2230
0
    }
2231
0
  return true;
2232
0
}
2233
2234
static bool
2235
record_relr (struct loongarch_elf_link_hash_table *htab, asection *sec,
2236
       bfd_vma off, asection *sreloc)
2237
0
{
2238
0
  struct relr_entry **sec_relr = &loongarch_elf_section_data (sec)->relr;
2239
2240
  /* Undo the relocation section size accounting.  */
2241
0
  BFD_ASSERT (sreloc->size >= sizeof (Elf32_External_Rela));
2242
0
  sreloc->size -= sizeof (Elf32_External_Rela);
2243
2244
0
  BFD_ASSERT (off % 2 == 0 && sec->alignment_power > 0);
2245
0
  if (htab->relr_count >= htab->relr_alloc)
2246
0
    {
2247
0
      if (htab->relr_alloc == 0)
2248
0
  htab->relr_alloc = 4096;
2249
0
      else
2250
0
  htab->relr_alloc *= 2;
2251
2252
0
      htab->relr = bfd_realloc (htab->relr,
2253
0
        htab->relr_alloc * sizeof (*htab->relr));
2254
0
      if (!htab->relr)
2255
0
  return false;
2256
0
    }
2257
0
  htab->relr[htab->relr_count].sec = sec;
2258
0
  htab->relr[htab->relr_count].off = off;
2259
0
  if (*sec_relr == NULL)
2260
0
    *sec_relr = &htab->relr[htab->relr_count];
2261
0
  htab->relr_count++;
2262
0
  return true;
2263
0
}
2264
2265
static bool
2266
record_relr_local_got_relocs (bfd *input_bfd, struct bfd_link_info *info)
2267
0
{
2268
0
  bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
2269
0
  char *local_tls_type = _bfd_loongarch_elf_local_got_tls_type (input_bfd);
2270
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_bfd);
2271
0
  struct loongarch_elf_link_hash_table *htab =
2272
0
    loongarch_elf_hash_table (info);
2273
2274
0
  if (!local_got_offsets || !local_tls_type || !bfd_link_pic (info))
2275
0
    return true;
2276
2277
0
  for (unsigned i = 0; i < symtab_hdr->sh_info; i++)
2278
0
    {
2279
0
      bfd_vma off = local_got_offsets[i];
2280
2281
      /* FIXME: If the local symbol is in SHN_ABS then emitting
2282
   a relative relocation is not correct, but it seems to be wrong
2283
   in loongarch_elf_relocate_section too.  */
2284
0
      if (local_tls_type[i] == GOT_NORMAL
2285
0
    && !record_relr (htab, htab->elf.sgot, off, htab->elf.srelgot))
2286
0
  return false;
2287
0
    }
2288
2289
0
  return true;
2290
0
}
2291
2292
static bool
2293
record_relr_dyn_got_relocs (struct elf_link_hash_entry *h, void *inf)
2294
0
{
2295
0
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
2296
0
  struct loongarch_elf_link_hash_table *htab =
2297
0
    loongarch_elf_hash_table (info);
2298
2299
0
  if (h->root.type == bfd_link_hash_indirect)
2300
0
    return true;
2301
0
  if (h->type == STT_GNU_IFUNC && h->def_regular)
2302
0
    return true;
2303
0
  if (h->got.offset == (bfd_vma) -1)
2304
0
    return true;
2305
0
  if (loongarch_elf_hash_entry (h)->tls_type
2306
0
      & (GOT_TLS_GD | GOT_TLS_IE | GOT_TLS_GDESC))
2307
0
    return true;
2308
0
  if (!bfd_link_pic (info))
2309
0
    return true;
2310
2311
  /* On LoongArch a GOT entry for undefined weak symbol is never relocated
2312
     with R_LARCH_RELATIVE: we don't have -z dynamic-undefined-weak, thus
2313
     the GOT entry is either const 0 (if the symbol is LARCH_REF_LOCAL) or
2314
     relocated with R_LARCH_32 (otherwise).  */
2315
0
  if (h->root.type == bfd_link_hash_undefweak)
2316
0
    return true;
2317
2318
0
  if (!LARCH_REF_LOCAL (info, h))
2319
0
    return true;
2320
0
  if (bfd_is_abs_symbol (&h->root))
2321
0
    return true;
2322
2323
0
  if (!record_relr (htab, htab->elf.sgot, h->got.offset,
2324
0
        htab->elf.srelgot))
2325
0
    return false;
2326
2327
0
  return true;
2328
0
}
2329
2330
static bool
2331
record_relr_non_got_relocs (bfd *input_bfd, struct bfd_link_info *info,
2332
          asection *sec)
2333
0
{
2334
0
  asection *sreloc;
2335
0
  struct loongarch_elf_link_hash_table *htab;
2336
0
  Elf_Internal_Rela *relocs, *rel, *rel_end;
2337
0
  Elf_Internal_Shdr *symtab_hdr;
2338
0
  struct elf_link_hash_entry **sym_hashes;
2339
2340
0
  if (!bfd_link_pic (info))
2341
0
    return true;
2342
0
  if (sec->reloc_count == 0)
2343
0
    return true;
2344
0
  if ((sec->flags & (SEC_RELOC | SEC_ALLOC | SEC_DEBUGGING))
2345
0
       != (SEC_RELOC | SEC_ALLOC))
2346
0
    return true;
2347
0
  if (sec->alignment_power == 0)
2348
0
    return true;
2349
0
  if (discarded_section (sec))
2350
0
    return true;
2351
2352
0
  sreloc = elf_section_data (sec)->sreloc;
2353
0
  if (sreloc == NULL)
2354
0
    return true;
2355
2356
0
  htab = loongarch_elf_hash_table (info);
2357
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
2358
0
  sym_hashes = elf_sym_hashes (input_bfd);
2359
0
  relocs = _bfd_elf_link_info_read_relocs (input_bfd, info, sec, NULL,
2360
0
             NULL, info->keep_memory);
2361
0
  BFD_ASSERT (relocs != NULL);
2362
0
  rel_end = relocs + sec->reloc_count;
2363
0
  for (rel = relocs; rel < rel_end; rel++)
2364
0
    {
2365
0
      unsigned r_symndx = ELF32_R_SYM (rel->r_info);
2366
0
      unsigned int r_type = ELF32_R_TYPE (rel->r_info);
2367
0
      struct elf_link_hash_entry *h = NULL;
2368
0
      asection *def_sec = NULL;
2369
2370
0
      if ((r_type != R_LARCH_64 && r_type != R_LARCH_32)
2371
0
    || rel->r_offset % 2 != 0)
2372
0
  continue;
2373
2374
      /* The logical below must match loongarch_elf_relocate_section.  */
2375
0
      if (r_symndx < symtab_hdr->sh_info)
2376
0
  {
2377
    /* A local symbol.  */
2378
0
    Elf_Internal_Sym *isym;
2379
0
    isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, input_bfd,
2380
0
          r_symndx);
2381
0
    BFD_ASSERT(isym != NULL);
2382
2383
    /* Local STT_GNU_IFUNC symbol uses R_LARCH_IRELATIVE for
2384
       R_LARCH_32, not R_LARCH_RELATIVE.  */
2385
0
    if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
2386
0
      continue;
2387
0
    def_sec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2388
0
  }
2389
0
      else
2390
0
  {
2391
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2392
0
    while (h->root.type == bfd_link_hash_indirect
2393
0
     || h->root.type == bfd_link_hash_warning)
2394
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
2395
2396
    /* Filter out symbols that cannot have a relative reloc.  */
2397
0
    if (h->dyn_relocs == NULL)
2398
0
      continue;
2399
0
    if (bfd_is_abs_symbol (&h->root))
2400
0
      continue;
2401
0
    if (h->type == STT_GNU_IFUNC)
2402
0
      continue;
2403
2404
0
    if (h->root.type == bfd_link_hash_defined
2405
0
        || h->root.type == bfd_link_hash_defweak)
2406
0
      def_sec = h->root.u.def.section;
2407
2408
    /* On LoongArch an R_LARCH_32 against undefined weak symbol
2409
       is never converted to R_LARCH_RELATIVE: we don't have
2410
       -z dynamic-undefined-weak, thus the reloc is either removed
2411
       (if the symbol is LARCH_REF_LOCAL) or kept (otherwise).  */
2412
0
    if (h->root.type == bfd_link_hash_undefweak)
2413
0
      continue;
2414
2415
0
    if (!LARCH_REF_LOCAL (info, h))
2416
0
      continue;
2417
0
  }
2418
2419
0
      if (!def_sec || discarded_section (def_sec))
2420
0
  continue;
2421
2422
0
      if (!record_relr (htab, sec, rel->r_offset, sreloc))
2423
0
  return false;
2424
0
    }
2425
2426
0
  return true;
2427
0
}
2428
2429
static int
2430
cmp_relr_addr (const void *p, const void *q)
2431
0
{
2432
0
  const bfd_vma *a = p, *b = q;
2433
0
  return (*a > *b) - (*a < *b);
2434
0
}
2435
2436
static bool
2437
sort_relr (struct bfd_link_info *info,
2438
     struct loongarch_elf_link_hash_table *htab)
2439
0
{
2440
0
  if (htab->relr_count == 0)
2441
0
    return true;
2442
2443
0
  bfd_vma *addr = htab->relr_sorted;
2444
0
  if (!addr)
2445
0
    {
2446
0
      addr = bfd_malloc (htab->relr_count * sizeof (*addr));
2447
0
      if (!addr)
2448
0
  return false;
2449
0
      htab->relr_sorted = addr;
2450
0
    }
2451
2452
0
  for (bfd_size_type i = 0; i < htab->relr_count; i++)
2453
0
    {
2454
0
      bfd_vma off = _bfd_elf_section_offset (info->output_bfd, info,
2455
0
               htab->relr[i].sec,
2456
0
               htab->relr[i].off);
2457
0
      addr[i] = htab->relr[i].sec->output_section->vma
2458
0
    + htab->relr[i].sec->output_offset + off;
2459
0
    }
2460
0
  qsort(addr, htab->relr_count, sizeof (*addr), cmp_relr_addr);
2461
0
  return true;
2462
0
}
2463
2464
static bool
2465
loongarch_elf_size_relative_relocs (struct bfd_link_info *info,
2466
            bool *need_layout)
2467
0
{
2468
0
  struct loongarch_elf_link_hash_table *htab =
2469
0
    loongarch_elf_hash_table (info);
2470
0
  asection *srelrdyn = htab->elf.srelrdyn;
2471
2472
0
  *need_layout = false;
2473
2474
0
  if (!sort_relr (info, htab))
2475
0
    return false;
2476
0
  bfd_vma *addr = htab->relr_sorted;
2477
2478
0
  BFD_ASSERT (srelrdyn != NULL);
2479
0
  bfd_size_type oldsize = srelrdyn->size;
2480
0
  srelrdyn->size = 0;
2481
0
  for (bfd_size_type i = 0; i < htab->relr_count; )
2482
0
    {
2483
0
      bfd_vma base = addr[i];
2484
0
      i++;
2485
0
      srelrdyn->size += 32 / 8;
2486
0
      base += 32 / 8;
2487
0
      while (1)
2488
0
  {
2489
0
    bfd_size_type start_i = i;
2490
0
    while (i < htab->relr_count
2491
0
     && addr[i] - base < (32 - 1) * (32 / 8)
2492
0
     && (addr[i] - base) % (32 / 8) == 0)
2493
0
      i++;
2494
0
    if (i == start_i)
2495
0
      break;
2496
0
    srelrdyn->size += 32 / 8;
2497
0
    base += (32 - 1) * (32 / 8);
2498
0
  }
2499
0
    }
2500
0
  if (srelrdyn->size != oldsize)
2501
0
    {
2502
0
      *need_layout = true;
2503
      /* Stop after a few iterations in case the layout does not converge,
2504
   but we can only stop when the size would shrink (and pad the
2505
   spare space with 1.  */
2506
0
      if (htab->relr_layout_iter++ > 5 && srelrdyn->size < oldsize)
2507
0
  {
2508
0
    srelrdyn->size = oldsize;
2509
0
    *need_layout = false;
2510
0
  }
2511
0
    }
2512
2513
0
  htab->layout_mutating_for_relr = *need_layout;
2514
0
  return true;
2515
0
}
2516
2517
static bool
2518
loongarch_elf_finish_relative_relocs (struct bfd_link_info *info)
2519
0
{
2520
0
  struct loongarch_elf_link_hash_table *htab =
2521
0
    loongarch_elf_hash_table (info);
2522
0
  asection *srelrdyn = htab->elf.srelrdyn;
2523
0
  bfd *dynobj = htab->elf.dynobj;
2524
2525
0
  if (!srelrdyn || srelrdyn->size == 0)
2526
0
    return true;
2527
2528
0
  srelrdyn->contents = bfd_alloc (dynobj, srelrdyn->size);
2529
0
  if (!srelrdyn->contents)
2530
0
    return false;
2531
0
  srelrdyn->alloced = 1;
2532
2533
0
  bfd_vma *addr = htab->relr_sorted;
2534
0
  bfd_byte *loc = srelrdyn->contents;
2535
0
  for (bfd_size_type i = 0; i < htab->relr_count; )
2536
0
    {
2537
0
      bfd_vma base = addr[i];
2538
0
      i++;
2539
0
      bfd_put_32 (dynobj, base, loc);
2540
0
      loc += 32 / 8;
2541
0
      base += 32 / 8;
2542
0
      while (1)
2543
0
  {
2544
0
    uint32_t bits = 0;
2545
0
    while (i < htab->relr_count)
2546
0
      {
2547
0
        bfd_vma delta = addr[i] - base;
2548
0
        if (delta >= (32 - 1) * (32 / 8) || delta % (32 / 8) != 0)
2549
0
    break;
2550
0
        bits |= (uint32_t) 1 << (delta / (32 / 8));
2551
0
        i++;
2552
0
      }
2553
0
    if (bits == 0)
2554
0
      break;
2555
0
    bfd_put_32 (dynobj, (bits << 1) | 1, loc);
2556
0
    loc += 32 / 8;
2557
0
    base += (32 - 1) * (32 / 8);
2558
0
  }
2559
0
    }
2560
2561
0
  free (addr);
2562
0
  htab->relr_sorted = NULL;
2563
2564
  /* Pad any excess with 1's, a do-nothing encoding.  */
2565
0
  while (loc < srelrdyn->contents + srelrdyn->size)
2566
0
    {
2567
0
      bfd_put_32 (dynobj, 1, loc);
2568
0
      loc += 32 / 8;
2569
0
    }
2570
2571
0
  return true;
2572
0
}
2573
2574
static bool
2575
loongarch_elf_late_size_sections (struct bfd_link_info *info)
2576
0
{
2577
0
  struct loongarch_elf_link_hash_table *htab;
2578
0
  bfd *dynobj;
2579
0
  asection *s;
2580
0
  bfd *ibfd;
2581
2582
0
  htab = loongarch_elf_hash_table (info);
2583
0
  BFD_ASSERT (htab != NULL);
2584
0
  dynobj = htab->elf.dynobj;
2585
0
  if (dynobj == NULL)
2586
0
    return true;
2587
2588
0
  if (htab->elf.dynamic_sections_created)
2589
0
    {
2590
      /* Set the contents of the .interp section to the interpreter.  */
2591
0
      if (bfd_link_executable (info) && !info->nointerp)
2592
0
  {
2593
0
    const char *interpreter;
2594
0
    s = htab->elf.interp;
2595
0
    BFD_ASSERT (s != NULL);
2596
2597
0
    if (elf_elfheader (info->output_bfd)->e_ident[EI_CLASS] == ELFCLASS32)
2598
0
      interpreter = "/lib32/ld.so.1";
2599
0
    else if (elf_elfheader (info->output_bfd)->e_ident[EI_CLASS] == ELFCLASS64)
2600
0
      interpreter = "/lib64/ld.so.1";
2601
0
    else
2602
0
      interpreter = "/lib/ld.so.1";
2603
2604
0
    s->contents = (unsigned char *) interpreter;
2605
0
    s->alloced = 1;
2606
0
    s->size = strlen (interpreter) + 1;
2607
0
  }
2608
0
    }
2609
2610
  /* Set up .got offsets for local syms, and space for local dynamic
2611
     relocs.  */
2612
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2613
0
    {
2614
0
      bfd_signed_vma *local_got;
2615
0
      bfd_signed_vma *end_local_got;
2616
0
      char *local_tls_type;
2617
0
      bfd_size_type locsymcount;
2618
0
      Elf_Internal_Shdr *symtab_hdr;
2619
0
      asection *srel;
2620
2621
0
      if (!is_loongarch_elf (ibfd))
2622
0
  continue;
2623
2624
0
      for (s = ibfd->sections; s != NULL; s = s->next)
2625
0
  {
2626
0
    struct elf_dyn_relocs *p;
2627
2628
0
    for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
2629
0
      {
2630
0
        p->count -= p->pc_count;
2631
0
        if (!bfd_is_abs_section (p->sec)
2632
0
      && bfd_is_abs_section (p->sec->output_section))
2633
0
    {
2634
      /* Input section has been discarded, either because
2635
         it is a copy of a linkonce section or due to
2636
         linker script /DISCARD/, so we'll be discarding
2637
         the relocs too.  */
2638
0
    }
2639
0
        else if (0 < p->count)
2640
0
    {
2641
0
      srel = elf_section_data (p->sec)->sreloc;
2642
0
      srel->size += p->count * sizeof (Elf32_External_Rela);
2643
0
      if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2644
0
        info->flags |= DF_TEXTREL;
2645
0
    }
2646
0
      }
2647
0
  }
2648
2649
0
      local_got = elf_local_got_refcounts (ibfd);
2650
0
      if (!local_got)
2651
0
  continue;
2652
2653
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
2654
0
      locsymcount = symtab_hdr->sh_info;
2655
0
      end_local_got = local_got + locsymcount;
2656
0
      local_tls_type = _bfd_loongarch_elf_local_got_tls_type (ibfd);
2657
0
      s = htab->elf.sgot;
2658
0
      srel = htab->elf.srelgot;
2659
0
      for (; local_got < end_local_got; ++local_got, ++local_tls_type)
2660
0
  {
2661
0
    if (0 < *local_got)
2662
0
      {
2663
0
        *local_got = s->size;
2664
0
        if (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE | GOT_TLS_GDESC))
2665
0
    {
2666
      /* TLS gd use two got.  */
2667
0
      if (*local_tls_type & GOT_TLS_GD)
2668
0
        {
2669
0
          s->size += 2 * GOT_ENTRY_SIZE;
2670
0
          if (!bfd_link_executable (info))
2671
0
      srel->size += sizeof (Elf32_External_Rela);
2672
0
        }
2673
2674
      /* TLS_DESC use two got.  */
2675
0
      if (*local_tls_type & GOT_TLS_GDESC)
2676
0
        {
2677
0
          s->size += 2 * GOT_ENTRY_SIZE;
2678
0
          srel->size += sizeof (Elf32_External_Rela);
2679
0
        }
2680
2681
      /* TLS ie and use one got.  */
2682
0
      if (*local_tls_type & GOT_TLS_IE)
2683
0
        {
2684
0
          s->size += GOT_ENTRY_SIZE;
2685
0
          if (!bfd_link_executable (info))
2686
0
      srel->size += sizeof (Elf32_External_Rela);
2687
0
        }
2688
0
    }
2689
0
        else
2690
0
    {
2691
0
      s->size += GOT_ENTRY_SIZE;
2692
0
      srel->size += sizeof (Elf32_External_Rela);
2693
0
    }
2694
0
      }
2695
0
    else
2696
0
      *local_got = MINUS_ONE;
2697
0
  }
2698
0
    }
2699
2700
  /* Allocate global sym .plt and .got entries, and space for global
2701
     sym dynamic relocs.  */
2702
0
  elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
2703
2704
  /* Allocate global ifunc sym .plt and .got entries, and space for
2705
     *preemptible* ifunc sym dynamic relocs.  Note that we must do it
2706
     for *all* preemptible ifunc (including local ifuncs and STV_HIDDEN
2707
     ifuncs) before doing it for any non-preemptible ifunc symbol:
2708
     assuming we are not so careful, when we link a shared library the
2709
     correlation of .plt and .rela.plt might look like:
2710
2711
        idx in .plt idx in .rela.plt
2712
  ext_func1@plt   0   0
2713
  ext_func2@plt   1   1
2714
  ext_func3@plt   2   2
2715
  hidden_ifunc1@plt 3   None: it's in .rela.got
2716
  hidden_ifunc2@plt 4   None: it's in .rela.got
2717
  normal_ifunc1@plt 5 !=  3
2718
  normal_ifunc2@plt 6 !=  4
2719
  local_ifunc@plt   7   None: it's in .rela.got
2720
2721
     Now oops the indices for normal_ifunc{1,2} in .rela.plt were different
2722
     from the indices in .plt :(.  This would break finish_dynamic_symbol
2723
     which assumes the index in .rela.plt matches the index in .plt.
2724
2725
     So let's be careful and make it correct:
2726
2727
        idx in .plt idx in .rela.plt
2728
  ext_func1@plt   0   0
2729
  ext_func2@plt   1   1
2730
  ext_func3@plt   2   2
2731
  normal_ifunc1@plt 3   3
2732
  normal_ifunc2@plt 4   4
2733
  hidden_ifunc1@plt 5   None: it's in .rela.got
2734
  hidden_ifunc2@plt 6   None: it's in .rela.got
2735
  local_ifunc@plt   7   None: it's in .rela.got
2736
2737
     Now normal_ifuncs first.  */
2738
0
  elf_link_hash_traverse (&htab->elf,
2739
0
        elf32_allocate_ifunc_dynrelocs_ref_global, info);
2740
2741
  /* Next hidden_ifuncs follows.  */
2742
0
  elf_link_hash_traverse (&htab->elf,
2743
0
        elf32_allocate_ifunc_dynrelocs_ref_local, info);
2744
2745
  /* Finally local_ifuncs.  */
2746
0
  htab_traverse (htab->loc_hash_table,
2747
0
     elf32_allocate_local_ifunc_dynrelocs, info);
2748
2749
  /* Don't allocate .got.plt section if there are no PLT.  */
2750
0
  if (htab->elf.sgotplt && htab->elf.sgotplt->size == GOTPLT_HEADER_SIZE
2751
0
      && (htab->elf.splt == NULL || htab->elf.splt->size == 0))
2752
0
    htab->elf.sgotplt->size = 0;
2753
2754
0
  if (info->enable_dt_relr && !bfd_link_relocatable (info))
2755
0
    {
2756
0
      elf_link_hash_traverse (&htab->elf, record_relr_dyn_got_relocs, info);
2757
2758
0
      for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2759
0
  {
2760
0
    if (!is_loongarch_elf (ibfd))
2761
0
      continue;
2762
2763
0
    for (s = ibfd->sections; s != NULL; s = s->next)
2764
0
      if (!record_relr_non_got_relocs (ibfd, info, s))
2765
0
        return false;
2766
2767
0
    if (!record_relr_local_got_relocs (ibfd, info))
2768
0
      return false;
2769
0
  }
2770
0
    }
2771
2772
  /* The check_relocs and adjust_dynamic_symbol entry points have
2773
     determined the sizes of the various dynamic sections.  Allocate
2774
     memory for them.  */
2775
0
  for (s = dynobj->sections; s != NULL; s = s->next)
2776
0
    {
2777
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
2778
0
  continue;
2779
2780
0
      if (s == htab->elf.splt || s == htab->elf.iplt || s == htab->elf.sgot
2781
0
    || s == htab->elf.sgotplt || s == htab->elf.igotplt
2782
0
    || s == htab->elf.sdynbss || s == htab->elf.sdynrelro)
2783
0
  {
2784
    /* Strip this section if we don't need it; see the
2785
       comment below.  */
2786
0
  }
2787
0
      else if (strncmp (s->name, ".rela", 5) == 0)
2788
0
  {
2789
0
    if (s->size != 0)
2790
0
      {
2791
        /* We use the reloc_count field as a counter if we need
2792
     to copy relocs into the output file.  */
2793
0
        s->reloc_count = 0;
2794
0
      }
2795
0
  }
2796
0
      else if (s == htab->elf.srelrdyn && htab->relr_count == 0)
2797
0
  {
2798
    /* Remove .relr.dyn based on relr_count, not size, since
2799
       it is not sized yet.  */
2800
0
    s->flags |= SEC_EXCLUDE;
2801
    /* Allocate contents later.  */
2802
0
    continue;
2803
0
  }
2804
0
      else
2805
0
  {
2806
    /* It's not one of our sections.  */
2807
0
    continue;
2808
0
  }
2809
2810
0
      if (s->size == 0)
2811
0
  {
2812
    /* If we don't need this section, strip it from the
2813
       output file.  This is mostly to handle .rela.bss and
2814
       .rela.plt.  We must create both sections in
2815
       create_dynamic_sections, because they must be created
2816
       before the linker maps input sections to output
2817
       sections.  The linker does that before
2818
       adjust_dynamic_symbol is called, and it is that
2819
       function which decides whether anything needs to go
2820
       into these sections.  */
2821
0
    s->flags |= SEC_EXCLUDE;
2822
0
    continue;
2823
0
  }
2824
2825
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
2826
0
  continue;
2827
2828
      /* Allocate memory for the section contents.  Zero the memory
2829
   for the benefit of .rela.plt, which has 4 unused entries
2830
   at the beginning, and we don't want garbage.  */
2831
0
      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2832
0
      if (s->contents == NULL)
2833
0
  return false;
2834
0
      s->alloced = 1;
2835
0
    }
2836
2837
0
  if (elf_hash_table (info)->dynamic_sections_created)
2838
0
    {
2839
      /* Add some entries to the .dynamic section.  We fill in the
2840
   values later, in loongarch_elf_finish_dynamic_sections, but we
2841
   must add the entries now so that we get the correct size for
2842
   the .dynamic section.  The DT_DEBUG entry is filled in by the
2843
   dynamic linker and used by the debugger.  */
2844
0
#define add_dynamic_entry(TAG, VAL) _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2845
2846
0
      if (bfd_link_executable (info))
2847
0
  {
2848
0
    if (!add_dynamic_entry (DT_DEBUG, 0))
2849
0
      return false;
2850
0
  }
2851
2852
0
      if (htab->elf.srelplt->size != 0)
2853
0
  {
2854
0
    if (!add_dynamic_entry (DT_PLTGOT, 0)
2855
0
        || !add_dynamic_entry (DT_PLTRELSZ, 0)
2856
0
        || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2857
0
        || !add_dynamic_entry (DT_JMPREL, 0))
2858
0
      return false;
2859
0
  }
2860
2861
0
      if (!add_dynamic_entry (DT_RELA, 0)
2862
0
    || !add_dynamic_entry (DT_RELASZ, 0)
2863
0
    || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2864
0
  return false;
2865
2866
      /* If any dynamic relocs apply to a read-only section,
2867
   then we need a DT_TEXTREL entry.  */
2868
0
      if ((info->flags & DF_TEXTREL) == 0)
2869
0
  elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
2870
2871
0
      if (info->flags & DF_TEXTREL)
2872
0
  {
2873
0
    if (!add_dynamic_entry (DT_TEXTREL, 0))
2874
0
      return false;
2875
    /* Clear the DF_TEXTREL flag.  It will be set again if we
2876
       write out an actual text relocation; we may not, because
2877
       at this point we do not know whether e.g.  any .eh_frame
2878
       absolute relocations have been converted to PC-relative.  */
2879
0
    info->flags &= ~DF_TEXTREL;
2880
0
  }
2881
0
    }
2882
0
#undef add_dynamic_entry
2883
2884
0
  return true;
2885
0
}
2886
2887
0
#define LARCH_LD_STACK_DEPTH 16
2888
static int64_t larch_opc_stack[LARCH_LD_STACK_DEPTH];
2889
static size_t larch_stack_top = 0;
2890
2891
static bfd_reloc_status_type
2892
loongarch_push (int64_t val)
2893
0
{
2894
0
  if (LARCH_LD_STACK_DEPTH <= larch_stack_top)
2895
0
    return bfd_reloc_outofrange;
2896
0
  larch_opc_stack[larch_stack_top++] = val;
2897
0
  return bfd_reloc_ok;
2898
0
}
2899
2900
static bfd_reloc_status_type
2901
loongarch_pop (int64_t *val)
2902
0
{
2903
0
  if (larch_stack_top == 0)
2904
0
    return bfd_reloc_outofrange;
2905
0
  BFD_ASSERT (val);
2906
0
  *val = larch_opc_stack[--larch_stack_top];
2907
0
  return bfd_reloc_ok;
2908
0
}
2909
2910
static bfd_reloc_status_type
2911
loongarch_top (int64_t *val)
2912
0
{
2913
0
  if (larch_stack_top == 0)
2914
0
    return bfd_reloc_outofrange;
2915
0
  BFD_ASSERT (val);
2916
0
  *val = larch_opc_stack[larch_stack_top - 1];
2917
0
  return bfd_reloc_ok;
2918
0
}
2919
2920
static void
2921
loongarch_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
2922
0
{
2923
0
  BFD_ASSERT (s && s->contents);
2924
0
  elf_backend_data *bed;
2925
0
  bfd_byte *loc;
2926
2927
0
  bed = get_elf_backend_data (abfd);
2928
0
  if (!(s->size > s->reloc_count * bed->s->sizeof_rela))
2929
0
    BFD_ASSERT (s->size > s->reloc_count * bed->s->sizeof_rela);
2930
0
  loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
2931
0
  bed->s->swap_reloca_out (abfd, rel, loc);
2932
0
}
2933
2934
/* Check rel->r_offset in range of contents.  */
2935
static bfd_reloc_status_type
2936
loongarch_check_offset (const Elf_Internal_Rela *rel,
2937
      const asection *input_section)
2938
0
{
2939
0
  if (0 == strcmp(input_section->name, ".text")
2940
0
      && rel->r_offset > input_section->size)
2941
0
    return bfd_reloc_overflow;
2942
2943
0
  return bfd_reloc_ok;
2944
0
}
2945
2946
#define LARCH_RELOC_PERFORM_3OP(op1, op2, op3)        \
2947
0
  ({                  \
2948
0
    bfd_reloc_status_type ret = loongarch_pop (&op2); \
2949
0
    if (ret == bfd_reloc_ok)           \
2950
0
      {                 \
2951
0
  ret = loongarch_pop (&op1);         \
2952
0
  if (ret == bfd_reloc_ok)         \
2953
0
    ret = loongarch_push (op3);         \
2954
0
      }                 \
2955
0
    ret;                \
2956
0
   })
2957
2958
/* Write immediate to instructions.  */
2959
2960
static bfd_reloc_status_type
2961
loongarch_reloc_rewrite_imm_insn (const Elf_Internal_Rela *rel,
2962
          const asection *input_section ATTRIBUTE_UNUSED,
2963
          reloc_howto_type *howto, bfd *input_bfd,
2964
          bfd_byte *contents, bfd_vma reloc_val)
2965
0
{
2966
  /* Adjust the immediate based on alignment and
2967
     its position in the instruction.  */
2968
0
  if (!bfd_elf_loongarch_adjust_reloc_bitsfield (input_bfd, howto, &reloc_val))
2969
0
    return bfd_reloc_overflow;
2970
2971
0
  int bits = bfd_get_reloc_size (howto) * 8;
2972
0
  uint64_t insn = bfd_get (bits, input_bfd, contents + rel->r_offset);
2973
2974
  /* Write immediate to instruction.  */
2975
0
  insn = (insn & ~howto->dst_mask) | (reloc_val & howto->dst_mask);
2976
2977
0
  bfd_put (bits, input_bfd, insn, contents + rel->r_offset);
2978
2979
0
  return bfd_reloc_ok;
2980
0
}
2981
2982
static bfd_reloc_status_type
2983
perform_relocation (const Elf_Internal_Rela *rel, asection *input_section,
2984
        reloc_howto_type *howto, bfd_vma value,
2985
        bfd *input_bfd, bfd_byte *contents)
2986
0
{
2987
0
  int64_t opr1, opr2, opr3;
2988
0
  bfd_reloc_status_type r = bfd_reloc_ok;
2989
0
  int bits = bfd_get_reloc_size (howto) * 8;
2990
2991
0
  switch (ELF32_R_TYPE (rel->r_info))
2992
0
    {
2993
0
    case R_LARCH_SOP_PUSH_PCREL:
2994
0
    case R_LARCH_SOP_PUSH_ABSOLUTE:
2995
0
    case R_LARCH_SOP_PUSH_GPREL:
2996
0
    case R_LARCH_SOP_PUSH_TLS_TPREL:
2997
0
    case R_LARCH_SOP_PUSH_TLS_GOT:
2998
0
    case R_LARCH_SOP_PUSH_TLS_GD:
2999
0
    case R_LARCH_SOP_PUSH_PLT_PCREL:
3000
0
      r = loongarch_push (value);
3001
0
      break;
3002
3003
0
    case R_LARCH_SOP_PUSH_DUP:
3004
0
      r = loongarch_pop (&opr1);
3005
0
      if (r == bfd_reloc_ok)
3006
0
  {
3007
0
    r = loongarch_push (opr1);
3008
0
    if (r == bfd_reloc_ok)
3009
0
      r = loongarch_push (opr1);
3010
0
  }
3011
0
      break;
3012
3013
0
    case R_LARCH_SOP_ASSERT:
3014
0
      r = loongarch_pop (&opr1);
3015
0
      if (r != bfd_reloc_ok || !opr1)
3016
0
  r = bfd_reloc_notsupported;
3017
0
      break;
3018
3019
0
    case R_LARCH_SOP_NOT:
3020
0
      r = loongarch_pop (&opr1);
3021
0
      if (r == bfd_reloc_ok)
3022
0
  r = loongarch_push (!opr1);
3023
0
      break;
3024
3025
0
    case R_LARCH_SOP_SUB:
3026
0
      r = LARCH_RELOC_PERFORM_3OP (opr1, opr2, opr1 - opr2);
3027
0
      break;
3028
3029
0
    case R_LARCH_SOP_SL:
3030
0
      r = LARCH_RELOC_PERFORM_3OP (opr1, opr2, opr1 << opr2);
3031
0
      break;
3032
3033
0
    case R_LARCH_SOP_SR:
3034
0
      r = LARCH_RELOC_PERFORM_3OP (opr1, opr2, opr1 >> opr2);
3035
0
      break;
3036
3037
0
    case R_LARCH_SOP_AND:
3038
0
      r = LARCH_RELOC_PERFORM_3OP (opr1, opr2, opr1 & opr2);
3039
0
      break;
3040
3041
0
    case R_LARCH_SOP_ADD:
3042
0
      r = LARCH_RELOC_PERFORM_3OP (opr1, opr2, opr1 + opr2);
3043
0
      break;
3044
3045
0
    case R_LARCH_SOP_IF_ELSE:
3046
0
      r = loongarch_pop (&opr3);
3047
0
      if (r == bfd_reloc_ok)
3048
0
  {
3049
0
    r = loongarch_pop (&opr2);
3050
0
    if (r == bfd_reloc_ok)
3051
0
      {
3052
0
        r = loongarch_pop (&opr1);
3053
0
        if (r == bfd_reloc_ok)
3054
0
    r = loongarch_push (opr1 ? opr2 : opr3);
3055
0
      }
3056
0
  }
3057
0
      break;
3058
3059
0
    case R_LARCH_SOP_POP_32_S_10_5:
3060
0
    case R_LARCH_SOP_POP_32_S_10_12:
3061
0
    case R_LARCH_SOP_POP_32_S_10_16:
3062
0
    case R_LARCH_SOP_POP_32_S_10_16_S2:
3063
0
    case R_LARCH_SOP_POP_32_S_0_5_10_16_S2:
3064
0
    case R_LARCH_SOP_POP_32_S_0_10_10_16_S2:
3065
0
    case R_LARCH_SOP_POP_32_S_5_20:
3066
0
    case R_LARCH_SOP_POP_32_U_10_12:
3067
0
    case R_LARCH_SOP_POP_32_U:
3068
0
      r = loongarch_pop (&opr1);
3069
0
      if (r != bfd_reloc_ok)
3070
0
  break;
3071
0
      r = loongarch_check_offset (rel, input_section);
3072
0
      if (r != bfd_reloc_ok)
3073
0
  break;
3074
3075
0
      r = loongarch_reloc_rewrite_imm_insn (rel, input_section,
3076
0
              howto, input_bfd,
3077
0
              contents, (bfd_vma)opr1);
3078
0
      break;
3079
3080
0
    case R_LARCH_TLS_DTPREL32:
3081
0
    case R_LARCH_32:
3082
0
    case R_LARCH_TLS_DTPREL64:
3083
0
    case R_LARCH_64:
3084
0
      r = loongarch_check_offset (rel, input_section);
3085
0
      if (r != bfd_reloc_ok)
3086
0
  break;
3087
3088
0
      bfd_put (bits, input_bfd, value, contents + rel->r_offset);
3089
0
      break;
3090
3091
    /* LoongArch only has add/sub reloc pair, not has set/sub reloc pair.
3092
       Because set/sub reloc pair not support multi-thread. While add/sub
3093
       reloc pair process order not affect the final result.
3094
3095
       For add/sub reloc, the original value will be involved in the
3096
       calculation. In order not to add/sub extra value, we write 0 to symbol
3097
       address at assembly time.
3098
3099
       add/sub reloc bits determined by the value after symbol subtraction,
3100
       not symbol value.
3101
3102
       add/sub reloc save part of the symbol value, so we only need to
3103
       save howto->dst_mask bits.  */
3104
0
    case R_LARCH_ADD6:
3105
0
    case R_LARCH_SUB6:
3106
0
      {
3107
0
  bfd_vma word = bfd_get (howto->bitsize, input_bfd,
3108
0
        contents + rel->r_offset);
3109
0
  word = (word & ~howto->dst_mask) | (value & howto->dst_mask);
3110
0
  bfd_put (howto->bitsize, input_bfd, word, contents + rel->r_offset);
3111
0
  r = bfd_reloc_ok;
3112
0
  break;
3113
0
      }
3114
3115
    /* Not need to read the original value, just write the new value.  */
3116
0
    case R_LARCH_ADD8:
3117
0
    case R_LARCH_ADD16:
3118
0
    case R_LARCH_ADD24:
3119
0
    case R_LARCH_ADD32:
3120
0
    case R_LARCH_ADD64:
3121
0
    case R_LARCH_SUB8:
3122
0
    case R_LARCH_SUB16:
3123
0
    case R_LARCH_SUB24:
3124
0
    case R_LARCH_SUB32:
3125
0
    case R_LARCH_SUB64:
3126
0
      {
3127
  /* Because add/sub reloc is processed separately,
3128
     so the high bits is invalid.  */
3129
0
  bfd_vma word = value & howto->dst_mask;
3130
0
  bfd_put (howto->bitsize, input_bfd, word, contents + rel->r_offset);
3131
0
  r = bfd_reloc_ok;
3132
0
  break;
3133
0
      }
3134
3135
0
    case R_LARCH_ADD_ULEB128:
3136
0
    case R_LARCH_SUB_ULEB128:
3137
0
      {
3138
  /* Before write uleb128, first read it to get its length.  */
3139
0
  bfd_byte *p = contents + rel->r_offset;
3140
0
  bfd_byte *end = contents + input_section->size;
3141
0
  _bfd_safe_read_leb128 (input_bfd, &p, false, end);
3142
0
  size_t len = p - (contents + rel->r_offset);
3143
0
  loongarch_write_unsigned_leb128 (contents + rel->r_offset, len, value);
3144
0
  r = bfd_reloc_ok;
3145
0
  break;
3146
0
      }
3147
3148
    /* For eh_frame and debug info.  */
3149
0
    case R_LARCH_32_PCREL:
3150
0
    case R_LARCH_64_PCREL:
3151
0
      {
3152
0
  value -= sec_addr (input_section) + rel->r_offset;
3153
0
  value += rel->r_addend;
3154
  /* Check overflow.  */
3155
0
  if (ELF32_R_TYPE (rel->r_info) == R_LARCH_32_PCREL)
3156
0
    {
3157
0
      r = loongarch_reloc_rewrite_imm_insn (rel, input_section,
3158
0
              howto, input_bfd,
3159
0
              contents, value);
3160
0
    }
3161
0
  else
3162
0
    {
3163
0
      bfd_vma word = bfd_get (howto->bitsize, input_bfd,
3164
0
            contents + rel->r_offset);
3165
0
      word = (word & ~howto->dst_mask) | (value & howto->dst_mask);
3166
0
      bfd_put (howto->bitsize, input_bfd, word, contents + rel->r_offset);
3167
0
      r = bfd_reloc_ok;
3168
0
    }
3169
0
  break;
3170
0
      }
3171
3172
    /* New reloc type.
3173
       R_LARCH_B16 ~ R_LARCH_TLS_GD_HI20.  */
3174
0
    case R_LARCH_B16:
3175
0
    case R_LARCH_B21:
3176
0
    case R_LARCH_B26:
3177
0
    case R_LARCH_ABS_HI20:
3178
0
    case R_LARCH_ABS_LO12:
3179
0
    case R_LARCH_ABS64_LO20:
3180
0
    case R_LARCH_ABS64_HI12:
3181
0
    case R_LARCH_PCALA_HI20:
3182
0
    case R_LARCH_PCALA_LO12:
3183
0
    case R_LARCH_PCALA64_LO20:
3184
0
    case R_LARCH_PCALA64_HI12:
3185
0
    case R_LARCH_GOT_PC_HI20:
3186
0
    case R_LARCH_GOT_PC_LO12:
3187
0
    case R_LARCH_GOT64_PC_LO20:
3188
0
    case R_LARCH_GOT64_PC_HI12:
3189
0
    case R_LARCH_GOT_HI20:
3190
0
    case R_LARCH_GOT_LO12:
3191
0
    case R_LARCH_GOT64_LO20:
3192
0
    case R_LARCH_GOT64_HI12:
3193
0
    case R_LARCH_TLS_LE_HI20:
3194
0
    case R_LARCH_TLS_LE_LO12:
3195
0
    case R_LARCH_TLS_LE_HI20_R:
3196
0
    case R_LARCH_TLS_LE_LO12_R:
3197
0
    case R_LARCH_TLS_LE64_LO20:
3198
0
    case R_LARCH_TLS_LE64_HI12:
3199
0
    case R_LARCH_TLS_IE_PC_HI20:
3200
0
    case R_LARCH_TLS_IE_PC_LO12:
3201
0
    case R_LARCH_TLS_IE64_PC_LO20:
3202
0
    case R_LARCH_TLS_IE64_PC_HI12:
3203
0
    case R_LARCH_TLS_IE_HI20:
3204
0
    case R_LARCH_TLS_IE_LO12:
3205
0
    case R_LARCH_TLS_IE64_LO20:
3206
0
    case R_LARCH_TLS_IE64_HI12:
3207
0
    case R_LARCH_TLS_LD_PC_HI20:
3208
0
    case R_LARCH_TLS_LD_HI20:
3209
0
    case R_LARCH_TLS_GD_PC_HI20:
3210
0
    case R_LARCH_TLS_GD_HI20:
3211
0
    case R_LARCH_PCREL20_S2:
3212
0
    case R_LARCH_CALL36:
3213
0
    case R_LARCH_TLS_DESC_PC_HI20:
3214
0
    case R_LARCH_TLS_DESC_PC_LO12:
3215
0
    case R_LARCH_TLS_DESC64_PC_LO20:
3216
0
    case R_LARCH_TLS_DESC64_PC_HI12:
3217
0
    case R_LARCH_TLS_DESC_HI20:
3218
0
    case R_LARCH_TLS_DESC_LO12:
3219
0
    case R_LARCH_TLS_DESC64_LO20:
3220
0
    case R_LARCH_TLS_DESC64_HI12:
3221
0
    case R_LARCH_TLS_LD_PCREL20_S2:
3222
0
    case R_LARCH_TLS_GD_PCREL20_S2:
3223
0
    case R_LARCH_TLS_DESC_PCREL20_S2:
3224
0
    case R_LARCH_CALL30:
3225
0
    case R_LARCH_PCADD_HI20:
3226
0
    case R_LARCH_PCADD_LO12:
3227
0
    case R_LARCH_GOT_PCADD_HI20:
3228
0
    case R_LARCH_GOT_PCADD_LO12:
3229
0
    case R_LARCH_TLS_IE_PCADD_HI20:
3230
0
    case R_LARCH_TLS_IE_PCADD_LO12:
3231
0
    case R_LARCH_TLS_LD_PCADD_HI20:
3232
0
    case R_LARCH_TLS_LD_PCADD_LO12:
3233
0
    case R_LARCH_TLS_GD_PCADD_HI20:
3234
0
    case R_LARCH_TLS_GD_PCADD_LO12:
3235
0
    case R_LARCH_TLS_DESC_PCADD_HI20:
3236
0
    case R_LARCH_TLS_DESC_PCADD_LO12:
3237
0
      r = loongarch_check_offset (rel, input_section);
3238
0
      if (r != bfd_reloc_ok)
3239
0
  break;
3240
3241
0
      r = loongarch_reloc_rewrite_imm_insn (rel, input_section,
3242
0
              howto, input_bfd,
3243
0
              contents, value);
3244
0
      break;
3245
3246
0
    case R_LARCH_TLS_DESC_LD:
3247
0
    case R_LARCH_TLS_DESC_CALL:
3248
0
      r = bfd_reloc_ok;
3249
0
      break;
3250
3251
0
    case R_LARCH_RELAX:
3252
0
    case R_LARCH_TLS_LE_ADD_R:
3253
0
      break;
3254
3255
0
    default:
3256
0
      r = bfd_reloc_notsupported;
3257
0
    }
3258
0
  return r;
3259
0
}
3260
3261
0
#define LARCH_RECENT_RELOC_QUEUE_LENGTH 72
3262
static struct
3263
{
3264
  bfd *bfd;
3265
  asection *section;
3266
  bfd_vma r_offset;
3267
  int r_type;
3268
  bfd_vma relocation;
3269
  const Elf_Internal_Sym *sym;
3270
  struct elf_link_hash_entry *h;
3271
  bfd_vma addend;
3272
  int64_t top_then;
3273
} larch_reloc_queue[LARCH_RECENT_RELOC_QUEUE_LENGTH];
3274
static size_t larch_reloc_queue_head = 0;
3275
static size_t larch_reloc_queue_tail = 0;
3276
3277
static const char *
3278
loongarch_sym_name (bfd *input_bfd, struct elf_link_hash_entry *h,
3279
        const Elf_Internal_Sym *sym)
3280
0
{
3281
0
  const char *ret = NULL;
3282
0
  if (sym)
3283
0
    ret = bfd_elf_string_from_elf_section (input_bfd,
3284
0
             elf_symtab_hdr (input_bfd).sh_link,
3285
0
             sym->st_name);
3286
0
  else if (h)
3287
0
    ret = h->root.root.string;
3288
3289
0
  if (ret == NULL || *ret == '\0')
3290
0
    ret = "<nameless>";
3291
0
  return ret;
3292
0
}
3293
3294
static void
3295
loongarch_record_one_reloc (bfd *abfd, asection *section, int r_type,
3296
          bfd_vma r_offset, const Elf_Internal_Sym *sym,
3297
          struct elf_link_hash_entry *h, bfd_vma addend)
3298
0
{
3299
0
  if ((larch_reloc_queue_head == 0
3300
0
       && larch_reloc_queue_tail == LARCH_RECENT_RELOC_QUEUE_LENGTH - 1)
3301
0
      || larch_reloc_queue_head == larch_reloc_queue_tail + 1)
3302
0
    larch_reloc_queue_head =
3303
0
      (larch_reloc_queue_head + 1) % LARCH_RECENT_RELOC_QUEUE_LENGTH;
3304
0
  larch_reloc_queue[larch_reloc_queue_tail].bfd = abfd;
3305
0
  larch_reloc_queue[larch_reloc_queue_tail].section = section;
3306
0
  larch_reloc_queue[larch_reloc_queue_tail].r_offset = r_offset;
3307
0
  larch_reloc_queue[larch_reloc_queue_tail].r_type = r_type;
3308
0
  larch_reloc_queue[larch_reloc_queue_tail].sym = sym;
3309
0
  larch_reloc_queue[larch_reloc_queue_tail].h = h;
3310
0
  larch_reloc_queue[larch_reloc_queue_tail].addend = addend;
3311
0
  loongarch_top (&larch_reloc_queue[larch_reloc_queue_tail].top_then);
3312
0
  larch_reloc_queue_tail =
3313
0
    (larch_reloc_queue_tail + 1) % LARCH_RECENT_RELOC_QUEUE_LENGTH;
3314
0
}
3315
3316
static void
3317
loongarch_dump_reloc_record (void (*p) (const char *fmt, ...))
3318
0
{
3319
0
  size_t i = larch_reloc_queue_head;
3320
0
  bfd *a_bfd = NULL;
3321
0
  asection *section = NULL;
3322
0
  bfd_vma r_offset = 0;
3323
0
  int inited = 0;
3324
0
  p ("Dump relocate record:\n");
3325
0
  p ("stack top\t\trelocation name\t\tsymbol");
3326
0
  while (i != larch_reloc_queue_tail)
3327
0
    {
3328
0
      if (a_bfd != larch_reloc_queue[i].bfd
3329
0
    || section != larch_reloc_queue[i].section
3330
0
    || r_offset != larch_reloc_queue[i].r_offset)
3331
0
  {
3332
0
    a_bfd = larch_reloc_queue[i].bfd;
3333
0
    section = larch_reloc_queue[i].section;
3334
0
    r_offset = larch_reloc_queue[i].r_offset;
3335
0
    p ("\nat %pB(%pA+0x%v):\n", larch_reloc_queue[i].bfd,
3336
0
       larch_reloc_queue[i].section, larch_reloc_queue[i].r_offset);
3337
0
  }
3338
3339
0
      if (!inited)
3340
0
  inited = 1, p ("...\n");
3341
3342
0
      reloc_howto_type *howto =
3343
0
  loongarch_elf_rtype_to_howto (larch_reloc_queue[i].bfd,
3344
0
              larch_reloc_queue[i].r_type);
3345
0
      p ("0x%V %s\t`%s'", (bfd_vma) larch_reloc_queue[i].top_then,
3346
0
   howto ? howto->name : "<unknown reloc>",
3347
0
   loongarch_sym_name (larch_reloc_queue[i].bfd, larch_reloc_queue[i].h,
3348
0
           larch_reloc_queue[i].sym));
3349
3350
0
      bfd_signed_vma addend = larch_reloc_queue[i].addend;
3351
0
      if (addend < 0)
3352
0
  {
3353
0
    bfd_vma addend_u = (bfd_vma) 0 - (bfd_vma) addend;
3354
0
    p (" - %" PRIu64, (uint64_t) addend_u);
3355
0
  }
3356
0
      else if (0 < addend)
3357
0
  p (" + %" PRId64 "(0x%v)", (int64_t) addend,
3358
0
     larch_reloc_queue[i].addend);
3359
3360
0
      p ("\n");
3361
0
      i = (i + 1) % LARCH_RECENT_RELOC_QUEUE_LENGTH;
3362
0
    }
3363
0
  p ("\n"
3364
0
     "-- Record dump end --\n\n");
3365
0
}
3366
3367
static bool
3368
loongarch_reloc_is_fatal (struct bfd_link_info *info,
3369
        bfd *input_bfd,
3370
        asection *input_section,
3371
        Elf_Internal_Rela *rel,
3372
        reloc_howto_type *howto,
3373
        bfd_reloc_status_type rtype,
3374
        bool is_undefweak,
3375
        const char *name,
3376
        const char *msg)
3377
0
{
3378
0
  bool fatal = true;
3379
0
  switch (rtype)
3380
0
    {
3381
      /* 'dangerous' means we do it but can't promise it's ok
3382
   'unsupport' means out of ability of relocation type
3383
   'undefined' means we can't deal with the undefined symbol.  */
3384
0
    case bfd_reloc_undefined:
3385
0
      info->callbacks->undefined_symbol (info, name, input_bfd, input_section,
3386
0
           rel->r_offset, true);
3387
0
      info->callbacks->info ("%X%pB(%pA+0x%v): error: %s against %s`%s':\n%s\n",
3388
0
           input_bfd, input_section, rel->r_offset,
3389
0
           howto->name,
3390
0
           is_undefweak ? "[undefweak] " : "", name, msg);
3391
0
      break;
3392
0
    case bfd_reloc_dangerous:
3393
0
      info->callbacks->info ("%pB(%pA+0x%v): warning: %s against %s`%s':\n%s\n",
3394
0
           input_bfd, input_section, rel->r_offset,
3395
0
           howto->name,
3396
0
           is_undefweak ? "[undefweak] " : "", name, msg);
3397
0
      fatal = false;
3398
0
      break;
3399
0
    case bfd_reloc_notsupported:
3400
0
      info->callbacks->info ("%X%pB(%pA+0x%v): error: %s against %s`%s':\n%s\n",
3401
0
           input_bfd, input_section, rel->r_offset,
3402
0
           howto->name,
3403
0
           is_undefweak ? "[undefweak] " : "", name, msg);
3404
0
      break;
3405
0
    default:
3406
0
      break;
3407
0
    }
3408
0
  return fatal;
3409
0
}
3410
3411
/* If lo12 immediate > 0x7ff, because sign-extend caused by addi.d/ld.d,
3412
   hi20 immediate need to add 0x1.
3413
   For example: pc 0x120000000, symbol 0x120000812
3414
   lo12 immediate is 0x812, 0x120000812 & 0xfff = 0x812
3415
   hi20 immediate is 1, because lo12 imm > 0x7ff, symbol need to add 0x1000
3416
   (((0x120000812 + 0x1000) & ~0xfff) - (0x120000000 & ~0xfff)) >> 12 = 0x1
3417
3418
   At run:
3419
   pcalau12i $t0, hi20 (0x1)
3420
      $t0 = 0x120000000 + (0x1 << 12) = 0x120001000
3421
   addi.d $t0, $t0, lo12 (0x812)
3422
      $t0 = 0x120001000 + 0xfffffffffffff812 (-(0x1000 - 0x812) = -0x7ee)
3423
    = 0x120001000 - 0x7ee (0x1000 - 0x7ee = 0x812)
3424
    = 0x120000812
3425
    Without hi20 add 0x1000, the result 0x120000000 - 0x7ee = 0x11ffff812 is
3426
    error.
3427
    0x1000 + sign-extend-to64(0x8xx) = 0x8xx.  */
3428
#define RELOCATE_CALC_PC32_HI20(relocation, pc)   \
3429
0
  ({              \
3430
0
    bfd_vma __lo = (relocation) & ((bfd_vma)0xfff); \
3431
0
    relocation = (relocation & ~(bfd_vma)0xfff)   \
3432
0
      - (pc & ~(bfd_vma)0xfff);   \
3433
0
    if (__lo > 0x7ff)         \
3434
0
  relocation += 0x1000;       \
3435
0
  })
3436
3437
#define RELOCATE_CALC_PCADD_HI20(relocation, pc)  \
3438
0
  ({              \
3439
0
    relocation = (relocation) - (pc);     \
3440
0
    bfd_vma __lo = (relocation) & ((bfd_vma)0xfff); \
3441
0
    if (__lo > 0x7ff)         \
3442
0
  relocation += 0x1000;       \
3443
0
  })
3444
3445
/* Handle problems caused by symbol extensions in TLS LE, The processing
3446
   is similar to the macro RELOCATE_CALC_PC32_HI20 method.  */
3447
#define RELOCATE_TLS_TP32_HI20(relocation)    \
3448
0
  ({              \
3449
0
    bfd_vma __lo = (relocation) & ((bfd_vma)0xfff); \
3450
0
    if (__lo > 0x7ff)         \
3451
0
  relocation += 0x800;       \
3452
0
    relocation = relocation & ~(bfd_vma)0xfff;    \
3453
0
  })
3454
3455
/* For example: pc is 0x11000010000100, symbol is 0x1812348ffff812
3456
   offset = (0x1812348ffff812 & ~0xfff) - (0x11000010000100 & ~0xfff)
3457
    = 0x712347ffff000
3458
   lo12: 0x1812348ffff812 & 0xfff = 0x812
3459
   hi20: 0x7ffff + 0x1(lo12 > 0x7ff) = 0x80000
3460
   lo20: 0x71234 - 0x1(lo12 > 0x7ff) + 0x1(hi20 > 0x7ffff)
3461
   hi12: 0x0
3462
3463
   pcalau12i $t1, hi20 (0x80000)
3464
      $t1 = 0x11000010000100 + sign-extend(0x80000 << 12)
3465
    = 0x11000010000100 + 0xffffffff80000000
3466
    = 0x10ffff90000000
3467
   addi.d $t0, $zero, lo12 (0x812)
3468
      $t0 = 0xfffffffffffff812 (if lo12 > 0x7ff, because sign-extend,
3469
      lo20 need to sub 0x1)
3470
   lu32i.d $t0, lo20 (0x71234)
3471
      $t0 = {0x71234, 0xfffff812}
3472
    = 0x71234fffff812
3473
   lu52i.d $t0, hi12 (0x0)
3474
      $t0 = {0x0, 0x71234fffff812}
3475
    = 0x71234fffff812
3476
   add.d $t1, $t1, $t0
3477
      $t1 = 0x10ffff90000000 + 0x71234fffff812
3478
    = 0x1812348ffff812.  */
3479
#define RELOCATE_CALC_PC64_HI32(relocation, pc)   \
3480
0
  ({              \
3481
0
    bfd_vma __lo = (relocation & (bfd_vma)0xfff); \
3482
0
    relocation = (relocation & ~(bfd_vma)0xfff)   \
3483
0
      - ((pc) & ~(bfd_vma)0xfff);   \
3484
0
    if (__lo > 0x7ff)         \
3485
0
  relocation += (0x1000 - 0x100000000);   \
3486
0
    if (relocation & 0x80000000)     \
3487
0
      relocation += 0x100000000;     \
3488
0
  })
3489
3490
3491
/* Compute the tp/dtp offset of a tls symbol.
3492
   It is dtp offset in dynamic tls model (gd/ld) and tp
3493
   offset in static tls model (ie/le). Both offsets are
3494
   calculated the same way on LoongArch, so the same
3495
   function is used.  */
3496
static bfd_vma
3497
tlsoff (struct bfd_link_info *info, bfd_vma addr)
3498
0
{
3499
  /* If tls_sec is NULL, we should have signalled an error already.  */
3500
0
  if (elf_hash_table (info)->tls_sec == NULL)
3501
0
    return 0;
3502
0
  return addr - elf_hash_table (info)->tls_sec->vma;
3503
0
}
3504
3505
static bool
3506
loongarch_resolve_pcrel_lo_relocs (loongarch_pcrel_relocs *p)
3507
0
{
3508
0
  loongarch_pcrel_lo_reloc *r;
3509
0
  for (r = p->lo_relocs; r != NULL; r = r->next)
3510
0
    {
3511
0
      bfd *input_bfd = r->input_section->owner;
3512
0
      loongarch_pcrel_hi_reloc search = {r->address, 0, 0, NULL};
3513
0
      loongarch_pcrel_hi_reloc *entry = htab_find (p->hi_relocs, &search);
3514
3515
0
      if (entry == NULL)
3516
0
  {
3517
0
    ((*r->info->callbacks->reloc_overflow)
3518
0
     (r->info, NULL, NULL, r->howto->name, (bfd_vma) 0,
3519
0
      input_bfd, r->input_section, r->reloc->r_offset));
3520
0
    return true;
3521
0
  }
3522
3523
0
      perform_relocation (r->reloc, r->input_section, r->howto, entry->value,
3524
0
        input_bfd, r->contents);
3525
0
    }
3526
0
  return true;
3527
0
}
3528
3529
static bfd_vma
3530
ifunc_got_off (struct elf_link_hash_table *htab,
3531
         struct elf_link_hash_entry *h)
3532
0
{
3533
0
  bfd_vma idx =
3534
0
    (h->plt.offset - (htab->splt ? PLT_HEADER_SIZE : 0)) / PLT_ENTRY_SIZE;
3535
3536
0
  return sec_addr (htab->splt ? htab->sgotplt : htab->igotplt)
3537
0
   + (htab->splt ? GOTPLT_HEADER_SIZE : 0)
3538
0
   + (idx * GOT_ENTRY_SIZE)
3539
0
   - sec_addr (htab->sgot);
3540
0
}
3541
3542
static int
3543
loongarch_elf_relocate_section (struct bfd_link_info *info,
3544
        bfd *input_bfd, asection *input_section,
3545
        bfd_byte *contents, Elf_Internal_Rela *relocs,
3546
        Elf_Internal_Sym *local_syms,
3547
        asection **local_sections)
3548
0
{
3549
0
  Elf_Internal_Rela *rel;
3550
0
  Elf_Internal_Rela *relend;
3551
0
  bool fatal = false;
3552
0
  asection *sreloc = elf_section_data (input_section)->sreloc;
3553
0
  struct loongarch_elf_link_hash_table *htab = loongarch_elf_hash_table (info);
3554
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_bfd);
3555
0
  struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3556
0
  bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
3557
0
  bool is_pic = bfd_link_pic (info);
3558
0
  bool is_dyn = elf_hash_table (info)->dynamic_sections_created;
3559
0
  asection *plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3560
0
  asection *got = htab->elf.sgot;
3561
0
  uint32_t insn;
3562
3563
0
  if (!is_loongarch_elf (input_bfd))
3564
0
    {
3565
0
      bfd_set_error (bfd_error_wrong_format);
3566
0
      return false;
3567
0
    }
3568
3569
0
  loongarch_pcrel_relocs pcrel_relocs;
3570
0
  if (!loongarch_init_pcrel_relocs (&pcrel_relocs))
3571
0
    return false;
3572
3573
0
  relend = relocs + input_section->reloc_count;
3574
0
  for (rel = relocs; rel < relend; rel++)
3575
0
    {
3576
0
      unsigned int r_type = ELF32_R_TYPE (rel->r_info);
3577
0
      unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
3578
0
      bfd_vma pc = sec_addr (input_section) + rel->r_offset;
3579
0
      reloc_howto_type *howto = NULL;
3580
0
      asection *sec = NULL;
3581
0
      const Elf_Internal_Sym *sym = NULL;
3582
0
      struct elf_link_hash_entry *h = NULL;
3583
0
      const char *name;
3584
0
      bfd_reloc_status_type r = bfd_reloc_ok;
3585
0
      bool is_ie, is_desc, is_undefweak, unresolved_reloc, defined_local;
3586
0
      bool resolved_local, resolved_dynly, resolved_to_const;
3587
0
      char tls_type;
3588
0
      bfd_vma relocation, off, ie_off, desc_off;
3589
0
      int i, j;
3590
0
      bool resolve_pcrel_undef_weak = false;
3591
0
      bool ignored = false;
3592
3593
      /* When an unrecognized relocation is encountered, which usually
3594
   occurs when using a newer assembler but an older linker, an error
3595
   should be reported instead of continuing to the next relocation.  */
3596
0
      howto = loongarch_elf_rtype_to_howto (input_bfd, r_type);
3597
0
      if (howto == NULL)
3598
0
  return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
3599
3600
0
      if (r_type == R_LARCH_GNU_VTINHERIT || r_type == R_LARCH_GNU_VTENTRY)
3601
0
  continue;
3602
3603
      /* This is a final link.  */
3604
0
      if (r_symndx < symtab_hdr->sh_info)
3605
0
  {
3606
0
    is_undefweak = false;
3607
0
    unresolved_reloc = false;
3608
0
    sym = local_syms + r_symndx;
3609
0
    sec = local_sections[r_symndx];
3610
0
    relocation = _bfd_elf_rela_local_sym (info->output_bfd,
3611
0
            sym, &sec, rel);
3612
3613
    /* Relocate against local STT_GNU_IFUNC symbol.  */
3614
0
    if (!bfd_link_relocatable (info)
3615
0
        && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3616
0
      {
3617
0
        h = elf32_loongarch_get_local_sym_hash (htab, input_bfd, rel,
3618
0
                  false);
3619
0
        if (h == NULL)
3620
0
    abort ();
3621
3622
        /* Set STT_GNU_IFUNC symbol value.  */
3623
0
        h->root.u.def.value = sym->st_value;
3624
0
        h->root.u.def.section = sec;
3625
0
      }
3626
0
    defined_local = true;
3627
0
    resolved_local = true;
3628
0
    resolved_dynly = false;
3629
0
    resolved_to_const = false;
3630
3631
    /* Calc in funtion elf_link_input_bfd,
3632
     * if #define elf_backend_rela_normal to 1.  */
3633
0
    if (bfd_link_relocatable (info)
3634
0
        && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3635
0
      continue;
3636
0
  }
3637
0
      else
3638
0
  {
3639
0
    bool warned;
3640
3641
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3642
0
           r_symndx, symtab_hdr, sym_hashes,
3643
0
           h, sec, relocation,
3644
0
           unresolved_reloc, warned, ignored);
3645
    /* Here means symbol isn't local symbol only and 'h != NULL'.  */
3646
3647
    /* The 'unresolved_syms_in_objects' specify how to deal with undefined
3648
       symbol.  And 'dynamic_undefined_weak' specify what to do when
3649
       meeting undefweak.  */
3650
3651
0
    if ((is_undefweak = h->root.type == bfd_link_hash_undefweak))
3652
0
      {
3653
0
        defined_local = false;
3654
0
        resolved_local = false;
3655
0
        resolved_to_const = (!is_dyn || h->dynindx == -1
3656
0
           || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
3657
0
        resolved_dynly = !resolved_local && !resolved_to_const;
3658
0
      }
3659
0
    else if (warned)
3660
0
      {
3661
        /* Symbol undefined offen means failed already.  I don't know why
3662
     'warned' here but I guess it want to continue relocating as if
3663
     no error occures to find other errors as more as possible.  */
3664
3665
        /* To avoid generating warning messages about truncated
3666
     relocations, set the relocation's address to be the same as
3667
     the start of this section.  */
3668
0
        relocation = (input_section->output_section
3669
0
          ? input_section->output_section->vma
3670
0
          : 0);
3671
3672
0
        defined_local = relocation != 0;
3673
0
        resolved_local = defined_local;
3674
0
        resolved_to_const = !resolved_local;
3675
0
        resolved_dynly = false;
3676
0
      }
3677
0
    else
3678
0
      {
3679
0
        defined_local = !unresolved_reloc && !ignored;
3680
0
        resolved_local =
3681
0
    defined_local && LARCH_REF_LOCAL (info, h);
3682
0
        resolved_dynly = !resolved_local;
3683
0
        resolved_to_const = !resolved_local && !resolved_dynly;
3684
0
      }
3685
0
  }
3686
3687
0
      name = loongarch_sym_name (input_bfd, h, sym);
3688
3689
0
      if (sec != NULL && discarded_section (sec))
3690
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3691
0
           rel, 1, relend, R_LARCH_NONE,
3692
0
           howto, 0, contents);
3693
3694
0
      if (bfd_link_relocatable (info))
3695
0
  continue;
3696
3697
      /* The r_symndx will be STN_UNDEF (zero) only for relocs against symbols
3698
   from removed linkonce sections, or sections discarded by a linker
3699
   script.  Also for R_*_SOP_PUSH_ABSOLUTE and PCREL to specify const.  */
3700
0
      if (r_symndx == STN_UNDEF)
3701
0
  {
3702
0
    defined_local = false;
3703
0
    resolved_local = false;
3704
0
    resolved_dynly = false;
3705
0
    resolved_to_const = true;
3706
0
  }
3707
3708
      /* The ifunc reference generate plt.  */
3709
0
      if (h && h->type == STT_GNU_IFUNC && h->plt.offset != MINUS_ONE)
3710
0
  {
3711
0
    defined_local = true;
3712
0
    resolved_local = true;
3713
0
    resolved_dynly = false;
3714
0
    resolved_to_const = false;
3715
0
    relocation = sec_addr (plt) + h->plt.offset;
3716
0
  }
3717
3718
0
      unresolved_reloc = resolved_dynly;
3719
3720
0
      BFD_ASSERT (resolved_local + resolved_dynly + resolved_to_const == 1);
3721
3722
      /* BFD_ASSERT (!resolved_dynly || (h && h->dynindx != -1));.  */
3723
3724
0
      BFD_ASSERT (!resolved_local || defined_local);
3725
3726
0
      is_desc = false;
3727
0
      is_ie = false;
3728
0
      switch (r_type)
3729
0
  {
3730
0
  case R_LARCH_MARK_PCREL:
3731
0
  case R_LARCH_MARK_LA:
3732
0
  case R_LARCH_NONE:
3733
0
    r = bfd_reloc_continue;
3734
0
    unresolved_reloc = false;
3735
0
    break;
3736
3737
0
  case R_LARCH_32:
3738
0
  case R_LARCH_64:
3739
0
    if (resolved_dynly || (is_pic && resolved_local))
3740
0
      {
3741
0
        Elf_Internal_Rela outrel;
3742
3743
        /* When generating a shared object, these relocations are copied
3744
     into the output file to be resolved at run time.  */
3745
3746
0
        outrel.r_offset = _bfd_elf_section_offset (info->output_bfd, info,
3747
0
               input_section,
3748
0
               rel->r_offset);
3749
3750
0
        unresolved_reloc = (!((bfd_vma) -2 <= outrel.r_offset)
3751
0
          && (input_section->flags & SEC_ALLOC));
3752
3753
0
        outrel.r_offset += sec_addr (input_section);
3754
3755
        /* A pointer point to a ifunc symbol.  */
3756
0
        if (h && h->type == STT_GNU_IFUNC)
3757
0
    {
3758
0
      if (h->dynindx == -1)
3759
0
        {
3760
0
          outrel.r_info = ELF32_R_INFO (0, R_LARCH_IRELATIVE);
3761
0
          outrel.r_addend = (h->root.u.def.value
3762
0
          + h->root.u.def.section->output_section->vma
3763
0
          + h->root.u.def.section->output_offset);
3764
0
        }
3765
0
      else
3766
0
        {
3767
0
          outrel.r_info = ELF32_R_INFO (h->dynindx, R_LARCH_32);
3768
0
          outrel.r_addend = 0;
3769
0
        }
3770
3771
0
      if (LARCH_REF_LOCAL (info, h))
3772
0
        {
3773
3774
0
          if (htab->elf.splt != NULL)
3775
0
      sreloc = htab->elf.srelgot;
3776
0
          else
3777
0
      sreloc = htab->elf.irelplt;
3778
0
        }
3779
0
      else
3780
0
        {
3781
3782
0
          if (bfd_link_pic (info))
3783
0
      sreloc = htab->elf.irelifunc;
3784
0
          else if (htab->elf.splt != NULL)
3785
0
      sreloc = htab->elf.srelgot;
3786
0
          else
3787
0
      sreloc = htab->elf.irelplt;
3788
0
        }
3789
0
    }
3790
0
        else if (resolved_dynly)
3791
0
    {
3792
0
      if (h->dynindx == -1)
3793
0
        outrel.r_info = ELF32_R_INFO (0, r_type);
3794
0
      else
3795
0
        outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3796
3797
0
      outrel.r_addend = rel->r_addend;
3798
0
    }
3799
0
        else
3800
0
    {
3801
0
      outrel.r_info = ELF32_R_INFO (0, R_LARCH_RELATIVE);
3802
0
      outrel.r_addend = relocation + rel->r_addend;
3803
0
    }
3804
3805
        /* No alloc space of func allocate_dynrelocs.
3806
     No alloc space of invalid R_LARCH_32 in ELFCLASS64.  */
3807
0
        if (unresolved_reloc
3808
0
      && (ARCH_SIZE == 32 || r_type != R_LARCH_32)
3809
0
      && !(h && (h->is_weakalias || !h->dyn_relocs)))
3810
0
    {
3811
0
      if (info->enable_dt_relr
3812
0
          && (ELF32_R_TYPE (outrel.r_info) == R_LARCH_RELATIVE)
3813
0
          && input_section->alignment_power != 0
3814
0
          && rel->r_offset % 2 == 0)
3815
        /* Don't emit a relative relocation that is packed,
3816
           only apply the addend (as if we are applying the
3817
           original R_LARCH_32 reloc in a PDE).  */
3818
0
        r = perform_relocation (rel, input_section, howto,
3819
0
              relocation, input_bfd,
3820
0
              contents);
3821
0
      else
3822
0
        loongarch_elf_append_rela (info->output_bfd,
3823
0
                 sreloc, &outrel);
3824
0
    }
3825
0
      }
3826
3827
0
    relocation += rel->r_addend;
3828
0
    break;
3829
3830
0
  case R_LARCH_ADD6:
3831
0
  case R_LARCH_ADD8:
3832
0
  case R_LARCH_ADD16:
3833
0
  case R_LARCH_ADD24:
3834
0
  case R_LARCH_ADD32:
3835
0
  case R_LARCH_ADD64:
3836
0
    {
3837
0
      bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
3838
0
           contents + rel->r_offset);
3839
0
      relocation = old_value + relocation + rel->r_addend;
3840
0
      break;
3841
0
    }
3842
3843
0
  case R_LARCH_SUB6:
3844
0
  case R_LARCH_SUB8:
3845
0
  case R_LARCH_SUB16:
3846
0
  case R_LARCH_SUB24:
3847
0
  case R_LARCH_SUB32:
3848
0
  case R_LARCH_SUB64:
3849
0
    {
3850
0
      bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
3851
0
            contents + rel->r_offset);
3852
0
      relocation = old_value - relocation - rel->r_addend;
3853
0
      break;
3854
0
    }
3855
3856
0
  case R_LARCH_ADD_ULEB128:
3857
0
  case R_LARCH_SUB_ULEB128:
3858
0
    {
3859
      /* Get the value and length of the uleb128 data.  */
3860
0
      bfd_byte *p = contents + rel->r_offset;
3861
0
      bfd_byte *end = contents + input_section->size;
3862
0
      bfd_vma old_value = _bfd_safe_read_leb128 (input_bfd, &p,
3863
0
                   false, end);
3864
0
      size_t len = p - (contents + rel->r_offset);
3865
3866
0
      if (R_LARCH_ADD_ULEB128 == ELF32_R_TYPE (rel->r_info))
3867
0
        relocation = old_value + relocation + rel->r_addend;
3868
0
      else if (R_LARCH_SUB_ULEB128 == ELF32_R_TYPE (rel->r_info))
3869
0
        relocation = old_value - relocation - rel->r_addend;
3870
3871
0
      bfd_vma mask = (1 << (7 * len)) - 1;
3872
0
      relocation &= mask;
3873
0
      break;
3874
0
    }
3875
3876
0
  case R_LARCH_TLS_DTPREL32:
3877
0
  case R_LARCH_TLS_DTPREL64:
3878
    /* GCC version <= 16.1 output extra addend 0x8000 in
3879
       loongarch_output_dwarf_dtprel.  Add the addend here
3880
       wuold cause problems for TLS debug info.  */
3881
0
    relocation = tlsoff (info, relocation);
3882
0
    unresolved_reloc = false;
3883
0
    break;
3884
3885
0
  case R_LARCH_SOP_PUSH_TLS_TPREL:
3886
0
    if (resolved_local)
3887
0
      {
3888
0
        if (!elf_hash_table (info)->tls_sec)
3889
0
    fatal = (loongarch_reloc_is_fatal
3890
0
       (info, input_bfd, input_section, rel, howto,
3891
0
        bfd_reloc_notsupported, is_undefweak, name,
3892
0
        "TLS section not be created"));
3893
0
        else
3894
0
    relocation = tlsoff (info, relocation);
3895
0
      }
3896
0
    else
3897
0
      fatal = (loongarch_reloc_is_fatal
3898
0
         (info, input_bfd, input_section, rel, howto,
3899
0
          bfd_reloc_undefined, is_undefweak, name,
3900
0
          "TLS LE just can be resolved local only."));
3901
0
    break;
3902
3903
0
  case R_LARCH_SOP_PUSH_ABSOLUTE:
3904
0
    if (is_undefweak)
3905
0
      {
3906
0
        if (resolved_dynly)
3907
0
    fatal = (loongarch_reloc_is_fatal
3908
0
       (info, input_bfd, input_section, rel, howto,
3909
0
        bfd_reloc_dangerous, is_undefweak, name,
3910
0
        "Someone require us to resolve undefweak "
3911
0
        "symbol dynamically.  \n"
3912
0
        "But this reloc can't be done.  "
3913
0
        "I think I can't throw error "
3914
0
        "for this\n"
3915
0
        "so I resolved it to 0.  "
3916
0
        "I suggest to re-compile with '-fpic'."));
3917
3918
0
        relocation = 0;
3919
0
        unresolved_reloc = false;
3920
0
        break;
3921
0
      }
3922
3923
0
    if (resolved_to_const)
3924
0
      {
3925
0
        relocation += rel->r_addend;
3926
0
        break;
3927
0
      }
3928
3929
0
    if (is_pic)
3930
0
      {
3931
0
        fatal = (loongarch_reloc_is_fatal
3932
0
           (info, input_bfd, input_section, rel, howto,
3933
0
      bfd_reloc_notsupported, is_undefweak, name,
3934
0
      "Under PIC we don't know load address.  Re-compile "
3935
0
      "with '-fpic'?"));
3936
0
        break;
3937
0
      }
3938
3939
0
    if (resolved_dynly)
3940
0
      {
3941
0
        if (!(plt && h && h->plt.offset != MINUS_ONE))
3942
0
    {
3943
0
      fatal = (loongarch_reloc_is_fatal
3944
0
         (info, input_bfd, input_section, rel, howto,
3945
0
          bfd_reloc_undefined, is_undefweak, name,
3946
0
          "Can't be resolved dynamically.  Try to re-compile "
3947
0
          "with '-fpic'?"));
3948
0
      break;
3949
0
    }
3950
3951
0
        if (rel->r_addend != 0)
3952
0
    {
3953
0
      fatal = (loongarch_reloc_is_fatal
3954
0
         (info, input_bfd, input_section, rel, howto,
3955
0
          bfd_reloc_notsupported, is_undefweak, name,
3956
0
          "Shouldn't be with r_addend."));
3957
0
      break;
3958
0
    }
3959
3960
0
        relocation = sec_addr (plt) + h->plt.offset;
3961
0
        unresolved_reloc = false;
3962
0
        break;
3963
0
      }
3964
3965
0
    if (resolved_local)
3966
0
      {
3967
0
        relocation += rel->r_addend;
3968
0
        break;
3969
0
      }
3970
3971
0
    break;
3972
3973
0
  case R_LARCH_SOP_PUSH_PCREL:
3974
0
  case R_LARCH_SOP_PUSH_PLT_PCREL:
3975
0
    unresolved_reloc = false;
3976
3977
0
    if (is_undefweak)
3978
0
      {
3979
0
        i = 0, j = 0;
3980
0
        relocation = 0;
3981
0
        if (resolved_dynly)
3982
0
    {
3983
0
      if (h && h->plt.offset != MINUS_ONE)
3984
0
        i = 1, j = 2;
3985
0
      else
3986
0
        fatal = (loongarch_reloc_is_fatal
3987
0
           (info, input_bfd, input_section, rel, howto,
3988
0
            bfd_reloc_dangerous, is_undefweak, name,
3989
0
            "Undefweak need to be resolved dynamically, "
3990
0
            "but PLT stub doesn't represent."));
3991
0
    }
3992
0
      }
3993
0
    else
3994
0
      {
3995
0
        if (!(defined_local || (h && h->plt.offset != MINUS_ONE)))
3996
0
    {
3997
0
      fatal = (loongarch_reloc_is_fatal
3998
0
         (info, input_bfd, input_section, rel, howto,
3999
0
          bfd_reloc_undefined, is_undefweak, name,
4000
0
          "PLT stub does not represent and "
4001
0
          "symbol not defined."));
4002
0
      break;
4003
0
    }
4004
4005
0
        if (resolved_local)
4006
0
    i = 0, j = 2;
4007
0
        else /* if (resolved_dynly) */
4008
0
    {
4009
0
      if (!(h && h->plt.offset != MINUS_ONE))
4010
0
        fatal = (loongarch_reloc_is_fatal
4011
0
           (info, input_bfd, input_section, rel, howto,
4012
0
            bfd_reloc_dangerous, is_undefweak, name,
4013
0
            "Internal: PLT stub doesn't represent.  "
4014
0
            "Resolve it with pcrel"));
4015
0
      i = 1, j = 3;
4016
0
    }
4017
0
      }
4018
4019
0
    for (; i < j; i++)
4020
0
      {
4021
0
        if ((i & 1) == 0 && defined_local)
4022
0
    {
4023
0
      relocation -= pc;
4024
0
      relocation += rel->r_addend;
4025
0
      break;
4026
0
    }
4027
4028
0
        if ((i & 1) && h && h->plt.offset != MINUS_ONE)
4029
0
    {
4030
0
      if (rel->r_addend != 0)
4031
0
        {
4032
0
          fatal = (loongarch_reloc_is_fatal
4033
0
             (info, input_bfd, input_section, rel, howto,
4034
0
        bfd_reloc_notsupported, is_undefweak, name,
4035
0
        "PLT shouldn't be with r_addend."));
4036
0
          break;
4037
0
        }
4038
0
      relocation = sec_addr (plt) + h->plt.offset - pc;
4039
0
      break;
4040
0
    }
4041
0
      }
4042
0
    break;
4043
4044
0
  case R_LARCH_SOP_PUSH_GPREL:
4045
0
    unresolved_reloc = false;
4046
4047
0
    if (rel->r_addend != 0)
4048
0
      {
4049
0
        fatal = (loongarch_reloc_is_fatal
4050
0
           (info, input_bfd, input_section, rel, howto,
4051
0
      bfd_reloc_notsupported, is_undefweak, name,
4052
0
      "Shouldn't be with r_addend."));
4053
0
        break;
4054
0
      }
4055
4056
0
    if (h != NULL)
4057
0
      {
4058
0
        off = h->got.offset & (~1);
4059
4060
0
        if (h->got.offset == MINUS_ONE && h->type != STT_GNU_IFUNC)
4061
0
    {
4062
0
      fatal = (loongarch_reloc_is_fatal
4063
0
         (info, input_bfd, input_section, rel, howto,
4064
0
          bfd_reloc_notsupported, is_undefweak, name,
4065
0
          "Internal: GOT entry doesn't represent."));
4066
0
      break;
4067
0
    }
4068
4069
        /* Hidden symbol not has .got entry, only .got.plt entry
4070
     so gprel is (plt - got).  */
4071
0
        if (h->got.offset == MINUS_ONE && h->type == STT_GNU_IFUNC)
4072
0
    {
4073
0
      if (h->plt.offset == (bfd_vma) -1)
4074
0
        {
4075
0
          abort();
4076
0
        }
4077
4078
0
      off = ifunc_got_off (&htab->elf, h);
4079
0
    }
4080
4081
0
        if ((h->got.offset & 1) == 0)
4082
0
    {
4083
0
      if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (is_dyn,
4084
0
              bfd_link_pic (info), h)
4085
0
          && ((bfd_link_pic (info)
4086
0
         && LARCH_REF_LOCAL (info, h))))
4087
0
        {
4088
          /* This is actually a static link, or it is a
4089
       -Bsymbolic link and the symbol is defined
4090
       locally, or the symbol was forced to be local
4091
       because of a version file.  We must initialize
4092
       this entry in the global offset table.  Since the
4093
       offset must always be a multiple of the word size,
4094
       we use the least significant bit to record whether
4095
       we have initialized it already.
4096
4097
       When doing a dynamic link, we create a rela.got
4098
       relocation entry to initialize the value.  This
4099
       is done in the finish_dynamic_symbol routine.  */
4100
4101
0
          if (resolved_dynly)
4102
0
      {
4103
0
        fatal = (loongarch_reloc_is_fatal
4104
0
           (info, input_bfd, input_section, rel, howto,
4105
0
            bfd_reloc_dangerous, is_undefweak, name,
4106
0
            "Internal: here shouldn't dynamic."));
4107
0
      }
4108
4109
0
          if (!(defined_local || resolved_to_const))
4110
0
      {
4111
0
        fatal = (loongarch_reloc_is_fatal
4112
0
           (info, input_bfd, input_section, rel, howto,
4113
0
            bfd_reloc_undefined, is_undefweak, name,
4114
0
            "Internal: "));
4115
0
        break;
4116
0
      }
4117
4118
0
          asection *s;
4119
0
          Elf_Internal_Rela outrel;
4120
          /* We need to generate a R_LARCH_RELATIVE reloc
4121
       for the dynamic linker.  */
4122
0
          s = htab->elf.srelgot;
4123
0
          if (!s)
4124
0
      {
4125
0
        fatal = loongarch_reloc_is_fatal
4126
0
          (info, input_bfd,
4127
0
           input_section, rel, howto,
4128
0
           bfd_reloc_notsupported, is_undefweak, name,
4129
0
           "Internal: '.rel.got' not represent");
4130
0
        break;
4131
0
      }
4132
4133
0
          outrel.r_offset = sec_addr (got) + off;
4134
0
          outrel.r_info = ELF32_R_INFO (0, R_LARCH_RELATIVE);
4135
0
          outrel.r_addend = relocation; /* Link-time addr.  */
4136
0
          loongarch_elf_append_rela (info->output_bfd, s, &outrel);
4137
0
        }
4138
0
      bfd_put_32 (info->output_bfd, relocation, got->contents + off);
4139
0
      h->got.offset |= 1;
4140
0
    }
4141
0
      }
4142
0
    else
4143
0
      {
4144
0
        if (!local_got_offsets)
4145
0
    {
4146
0
      fatal = (loongarch_reloc_is_fatal
4147
0
         (info, input_bfd, input_section, rel, howto,
4148
0
          bfd_reloc_notsupported, is_undefweak, name,
4149
0
          "Internal: local got offsets not reporesent."));
4150
0
      break;
4151
0
    }
4152
4153
0
        off = local_got_offsets[r_symndx] & (~1);
4154
4155
0
        if (local_got_offsets[r_symndx] == MINUS_ONE)
4156
0
    {
4157
0
      fatal = (loongarch_reloc_is_fatal
4158
0
         (info, input_bfd, input_section, rel, howto,
4159
0
          bfd_reloc_notsupported, is_undefweak, name,
4160
0
          "Internal: GOT entry doesn't represent."));
4161
0
      break;
4162
0
    }
4163
4164
        /* The offset must always be a multiple of the word size.
4165
     So, we can use the least significant bit to record
4166
     whether we have already processed this entry.  */
4167
0
        if ((local_got_offsets[r_symndx] & 1) == 0)
4168
0
    {
4169
0
      if (is_pic)
4170
0
        {
4171
0
          asection *s;
4172
0
          Elf_Internal_Rela outrel;
4173
          /* We need to generate a R_LARCH_RELATIVE reloc
4174
       for the dynamic linker.  */
4175
0
          s = htab->elf.srelgot;
4176
0
          if (!s)
4177
0
      {
4178
0
        fatal = (loongarch_reloc_is_fatal
4179
0
           (info, input_bfd, input_section, rel, howto,
4180
0
            bfd_reloc_notsupported, is_undefweak, name,
4181
0
            "Internal: '.rel.got' not represent"));
4182
0
        break;
4183
0
      }
4184
4185
0
          outrel.r_offset = sec_addr (got) + off;
4186
0
          outrel.r_info = ELF32_R_INFO (0, R_LARCH_RELATIVE);
4187
0
          outrel.r_addend = relocation; /* Link-time addr.  */
4188
0
          loongarch_elf_append_rela (info->output_bfd, s, &outrel);
4189
0
        }
4190
4191
0
      bfd_put_32 (info->output_bfd, relocation, got->contents + off);
4192
0
      local_got_offsets[r_symndx] |= 1;
4193
0
    }
4194
0
      }
4195
0
    relocation = off;
4196
4197
0
    break;
4198
4199
0
  case R_LARCH_SOP_PUSH_TLS_GOT:
4200
0
  case R_LARCH_SOP_PUSH_TLS_GD:
4201
0
    {
4202
0
      unresolved_reloc = false;
4203
0
      if (r_type == R_LARCH_SOP_PUSH_TLS_GOT)
4204
0
        is_ie = true;
4205
4206
0
      bfd_vma got_off = 0;
4207
0
      if (h != NULL)
4208
0
        {
4209
0
    got_off = h->got.offset;
4210
0
    h->got.offset |= 1;
4211
0
        }
4212
0
      else
4213
0
        {
4214
0
    got_off = local_got_offsets[r_symndx];
4215
0
    local_got_offsets[r_symndx] |= 1;
4216
0
        }
4217
4218
0
      BFD_ASSERT (got_off != MINUS_ONE);
4219
4220
0
      ie_off = 0;
4221
0
      tls_type = _bfd_loongarch_elf_tls_type (input_bfd, h, r_symndx);
4222
0
      if ((tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_IE))
4223
0
        ie_off = 2 * GOT_ENTRY_SIZE;
4224
4225
0
      if ((got_off & 1) == 0)
4226
0
        {
4227
0
    Elf_Internal_Rela rela;
4228
0
    asection *srel = htab->elf.srelgot;
4229
4230
0
    int indx = 0;
4231
0
    bool need_reloc = false;
4232
0
    LARCH_TLS_GD_IE_NEED_DYN_RELOC (info, is_dyn, h, indx,
4233
0
            need_reloc);
4234
4235
0
    if (tls_type & GOT_TLS_GD)
4236
0
      {
4237
0
        if (need_reloc)
4238
0
          {
4239
      /* Dynamic resolved Module ID.  */
4240
0
      rela.r_offset = sec_addr (got) + got_off;
4241
0
      rela.r_addend = 0;
4242
0
      rela.r_info = ELF32_R_INFO (indx, R_LARCH_TLS_DTPMOD32);
4243
0
      bfd_put_32 (info->output_bfd, 0,
4244
0
            got->contents + got_off);
4245
0
      loongarch_elf_append_rela (info->output_bfd,
4246
0
               srel, &rela);
4247
4248
0
      if (indx == 0)
4249
0
        {
4250
          /* Local symbol, tp offset has been known.  */
4251
0
          BFD_ASSERT (! unresolved_reloc);
4252
0
          bfd_put_32 (info->output_bfd,
4253
0
        tlsoff (info, relocation),
4254
0
        (got->contents + got_off + GOT_ENTRY_SIZE));
4255
0
        }
4256
0
      else
4257
0
        {
4258
          /* Dynamic resolved block offset.  */
4259
0
          bfd_put_32 (info->output_bfd, 0,
4260
0
        got->contents + got_off + GOT_ENTRY_SIZE);
4261
0
          rela.r_info = ELF32_R_INFO (indx,
4262
0
            R_LARCH_TLS_DTPREL32);
4263
0
          rela.r_offset += GOT_ENTRY_SIZE;
4264
0
          loongarch_elf_append_rela (info->output_bfd,
4265
0
                   srel, &rela);
4266
0
        }
4267
0
          }
4268
0
        else
4269
0
          {
4270
      /* In a static link or an executable link with the symbol
4271
         binding locally.  Mark it as belonging to module 1.  */
4272
0
      bfd_put_32 (info->output_bfd, 1,
4273
0
            got->contents + got_off);
4274
0
      bfd_put_32 (info->output_bfd, tlsoff (info, relocation),
4275
0
          got->contents + got_off + GOT_ENTRY_SIZE);
4276
0
          }
4277
0
      }
4278
0
    if (tls_type & GOT_TLS_IE)
4279
0
      {
4280
0
        if (need_reloc)
4281
0
          {
4282
0
      bfd_put_32 (info->output_bfd, 0,
4283
0
          got->contents + got_off + ie_off);
4284
0
      rela.r_offset = sec_addr (got) + got_off + ie_off;
4285
0
      rela.r_addend = 0;
4286
4287
0
      if (indx == 0)
4288
0
        rela.r_addend = tlsoff (info, relocation);
4289
0
      rela.r_info = ELF32_R_INFO (indx, R_LARCH_TLS_TPREL32);
4290
0
      loongarch_elf_append_rela (info->output_bfd,
4291
0
               srel, &rela);
4292
0
          }
4293
0
        else
4294
0
          {
4295
      /* In a static link or an executable link with the symbol
4296
         binding locally, compute offset directly.  */
4297
0
      bfd_put_32 (info->output_bfd, tlsoff (info, relocation),
4298
0
          got->contents + got_off + ie_off);
4299
0
          }
4300
0
      }
4301
0
        }
4302
4303
0
      relocation = (got_off & (~(bfd_vma)1)) + (is_ie ? ie_off : 0);
4304
0
    }
4305
0
    break;
4306
4307
  /* New reloc types.  */
4308
0
  case R_LARCH_B16:
4309
0
  case R_LARCH_B21:
4310
0
  case R_LARCH_B26:
4311
0
  case R_LARCH_CALL36:
4312
0
  case R_LARCH_CALL30:
4313
0
    unresolved_reloc = false;
4314
0
    bool via_plt =
4315
0
      plt != NULL && h != NULL && h->plt.offset != (bfd_vma) - 1;
4316
4317
0
    bool ignored_undef = (ignored && !via_plt);
4318
0
    if (is_undefweak || ignored_undef)
4319
0
      {
4320
0
        relocation = 0;
4321
4322
        /* A call to an undefined weak symbol is converted to 0.  */
4323
0
        if (!via_plt && IS_CALL_RELOC (r_type))
4324
0
    {
4325
      /* call36 fn1 => pcaddu18i $ra,0 + jirl $ra,$zero,0
4326
         tail36 $t0,fn1 => pcaddi18i $t0,0 + jirl $t0,$zero,0
4327
         call30 fn1 => pcaddu12i $ra,0 + jirl $ra,$zero,0
4328
         tail30 $t0,fn1 => pcaddi12i $t0,0 + jirl $t0,$zero,0  */
4329
0
      if (r_type == R_LARCH_CALL36
4330
0
          || r_type == R_LARCH_CALL30)
4331
0
        {
4332
0
          uint32_t jirl = bfd_get (32, input_bfd,
4333
0
            contents + rel->r_offset + 4);
4334
0
          uint32_t rd = LARCH_GET_RD (jirl);
4335
0
          jirl = LARCH_OP_JIRL | rd;
4336
4337
0
          bfd_put (32, input_bfd, jirl,
4338
0
             contents + rel->r_offset + 4);
4339
0
        }
4340
0
      else
4341
0
        {
4342
0
          uint32_t b_bl = bfd_get (32, input_bfd,
4343
0
                 contents + rel->r_offset);
4344
          /* b %plt(fn1) => jirl $zero,zero,0.  */
4345
0
          if (LARCH_INSN_B (b_bl))
4346
0
      bfd_put (32, input_bfd, LARCH_OP_JIRL,
4347
0
         contents + rel->r_offset);
4348
0
          else /* bl %plt(fn1) => jirl $ra,zero,0.  */
4349
0
      bfd_put (32, input_bfd, LARCH_OP_JIRL | 0x1,
4350
0
         contents + rel->r_offset);
4351
0
        }
4352
0
      r = bfd_reloc_continue;
4353
0
      break;
4354
0
    }
4355
0
      }
4356
4357
    /* B16 and B21 cannot be rewritten as an unconditional jump
4358
       without changing their semantics.  Apply a zero displacement,
4359
       matching the existing undefined weak symbol behavior.  */
4360
0
    if (ignored_undef && !via_plt)
4361
0
      break;
4362
4363
0
    if (resolved_local)
4364
0
      {
4365
0
        relocation -= pc;
4366
0
        relocation += rel->r_addend;
4367
0
      }
4368
0
    else if (resolved_dynly)
4369
0
      {
4370
0
        BFD_ASSERT (rel->r_addend == 0);
4371
0
        if (h && h->plt.offset == MINUS_ONE
4372
0
      && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4373
0
    {
4374
0
      relocation -= pc;
4375
0
      relocation += rel->r_addend;
4376
0
    }
4377
0
        else if (via_plt)
4378
0
    relocation = sec_addr (plt) + h->plt.offset - pc;
4379
0
        else
4380
0
    {
4381
0
      fatal = loongarch_reloc_is_fatal
4382
0
        (info, input_bfd, input_section, rel, howto,
4383
0
         bfd_reloc_notsupported, is_undefweak, name,
4384
0
         _("cannot resolve a dynamic branch without a PLT entry"));
4385
0
      break;
4386
0
    }
4387
0
      }
4388
4389
0
    break;
4390
4391
0
  case R_LARCH_ABS_HI20:
4392
0
  case R_LARCH_ABS_LO12:
4393
0
  case R_LARCH_ABS64_LO20:
4394
0
  case R_LARCH_ABS64_HI12:
4395
4396
0
    if (is_undefweak)
4397
0
      {
4398
0
        BFD_ASSERT (resolved_dynly);
4399
0
        relocation = 0;
4400
0
        break;
4401
0
      }
4402
0
    else if (resolved_to_const || resolved_local)
4403
0
      {
4404
0
        relocation += rel->r_addend;
4405
0
      }
4406
0
    else if (resolved_dynly)
4407
0
      {
4408
0
        unresolved_reloc = false;
4409
0
        BFD_ASSERT ((plt && h && h->plt.offset != MINUS_ONE)
4410
0
        && rel->r_addend == 0);
4411
0
        relocation = sec_addr (plt) + h->plt.offset;
4412
0
      }
4413
4414
0
    break;
4415
4416
0
  case R_LARCH_PCALA64_HI12:
4417
0
    pc -= 4;
4418
    /* Fall through.  */
4419
0
  case R_LARCH_PCALA64_LO20:
4420
0
    pc -= 8;
4421
    /* Fall through.  */
4422
0
  case R_LARCH_PCALA_HI20:
4423
0
  case R_LARCH_PCREL20_S2:
4424
0
    unresolved_reloc = false;
4425
4426
    /* If sym is undef weak and it's hidden or we are doing a static
4427
       link, (sym + addend) should be resolved to runtime address
4428
       (0 + addend).  */
4429
0
    resolve_pcrel_undef_weak =
4430
0
      ((info->nointerp
4431
0
        || (h && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT))
4432
0
       && is_undefweak);
4433
4434
0
    if (resolve_pcrel_undef_weak)
4435
0
      pc = 0;
4436
4437
0
    if (h && h->plt.offset != MINUS_ONE)
4438
0
      relocation = sec_addr (plt) + h->plt.offset;
4439
0
    else
4440
0
      relocation += rel->r_addend;
4441
4442
0
    switch (r_type)
4443
0
      {
4444
0
      case R_LARCH_PCREL20_S2:
4445
0
        relocation -= pc;
4446
0
        if (resolve_pcrel_undef_weak)
4447
0
    {
4448
0
      bfd_signed_vma addr = (bfd_signed_vma) relocation;
4449
0
      if (addr >= 2048 || addr < -2048)
4450
0
        {
4451
0
          const char *msg =
4452
0
      _("cannot resolve R_LARCH_PCREL20_S2 against "
4453
0
        "undefined weak symbol with addend out of "
4454
0
        "[-2048, 2048)");
4455
0
          fatal =
4456
0
      loongarch_reloc_is_fatal (info, input_bfd,
4457
0
              input_section, rel,
4458
0
              howto,
4459
0
              bfd_reloc_notsupported,
4460
0
              is_undefweak, name, msg);
4461
0
          break;
4462
0
        }
4463
4464
0
      insn = bfd_get (32, input_bfd,
4465
0
             contents + rel->r_offset);
4466
0
      insn = LARCH_GET_RD (insn) | LARCH_OP_ADDI_W;
4467
0
      insn |= (relocation & 0xfff) << 10;
4468
0
      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
4469
0
      r = bfd_reloc_continue;
4470
0
    }
4471
0
        break;
4472
0
      case R_LARCH_PCALA_HI20:
4473
0
        RELOCATE_CALC_PC32_HI20 (relocation, pc);
4474
0
        if (resolve_pcrel_undef_weak)
4475
0
    {
4476
0
      insn = bfd_get (32, input_bfd,
4477
0
             contents + rel->r_offset);
4478
0
      insn = LARCH_GET_RD (insn) | LARCH_OP_LU12I_W;
4479
0
      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
4480
0
    }
4481
0
        break;
4482
0
      default:
4483
0
        RELOCATE_CALC_PC64_HI32 (relocation, pc);
4484
0
      }
4485
0
    break;
4486
4487
0
  case R_LARCH_PCALA_LO12:
4488
    /* Not support if sym_addr in 2k page edge.
4489
       pcalau12i pc_hi20 (sym_addr)
4490
       ld.w/d pc_lo12 (sym_addr)
4491
       ld.w/d pc_lo12 (sym_addr + x)
4492
       ...
4493
       can not calc correct address
4494
       if sym_addr < 0x800 && sym_addr + x >= 0x800.  */
4495
4496
0
    if (h && h->plt.offset != MINUS_ONE)
4497
0
      relocation = sec_addr (plt) + h->plt.offset;
4498
0
    else
4499
0
      relocation += rel->r_addend;
4500
4501
    /* For 2G jump, generate pcalau12i, jirl.  */
4502
    /* If use jirl, turns to R_LARCH_B16.  */
4503
0
    insn = bfd_get (32, input_bfd, contents + rel->r_offset);
4504
0
    if (LARCH_INSN_JIRL (insn))
4505
0
      {
4506
0
        relocation &= 0xfff;
4507
        /* Signed extend.  */
4508
0
        relocation = (relocation ^ 0x800) - 0x800;
4509
4510
0
        rel->r_info = ELF32_R_INFO (r_symndx, R_LARCH_B16);
4511
0
        howto = loongarch_elf_rtype_to_howto (input_bfd, R_LARCH_B16);
4512
0
      }
4513
0
    break;
4514
4515
0
  case R_LARCH_PCADD_HI20:
4516
0
    resolve_pcrel_undef_weak =
4517
0
      ((info->nointerp
4518
0
        || (h && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT))
4519
0
       && is_undefweak);
4520
0
    if (resolve_pcrel_undef_weak)
4521
0
      relocation = pc; /* Use pc to avoid duplicate pcrel_hi record.  */
4522
4523
0
    if (h && h->plt.offset != MINUS_ONE)
4524
0
      relocation = sec_addr (plt) + h->plt.offset;
4525
0
    else
4526
0
      relocation += rel->r_addend;
4527
4528
0
    RELOCATE_CALC_PCADD_HI20(relocation, pc);
4529
0
    if (!loongarch_record_pcrel_hi_reloc (&pcrel_relocs, pc, relocation, 0, NULL))
4530
0
      r = bfd_reloc_overflow;
4531
4532
0
    if (resolve_pcrel_undef_weak)
4533
0
      {
4534
0
        insn = bfd_get (32, input_bfd,
4535
0
               contents + rel->r_offset);
4536
0
        insn = LARCH_GET_RD (insn) | LARCH_OP_LU12I_W;
4537
0
        bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
4538
0
      }
4539
0
    break;
4540
4541
0
  case R_LARCH_PCADD_LO12:
4542
0
  case R_LARCH_GOT_PCADD_LO12:
4543
0
  case R_LARCH_TLS_IE_PCADD_LO12:
4544
0
  case R_LARCH_TLS_LD_PCADD_LO12:
4545
0
  case R_LARCH_TLS_GD_PCADD_LO12:
4546
0
  case R_LARCH_TLS_DESC_PCADD_LO12:
4547
0
    if (loongarch_record_pcrel_lo_reloc (&pcrel_relocs, relocation, rel,
4548
0
                 input_section, info, howto,
4549
0
                 contents))
4550
0
      continue;
4551
0
    r = bfd_reloc_overflow;
4552
0
    break;
4553
4554
0
  case R_LARCH_GOT_PC_HI20:
4555
0
  case R_LARCH_GOT_PCADD_HI20:
4556
0
  case R_LARCH_GOT_HI20:
4557
    /* Calc got offset.  */
4558
0
      {
4559
0
        unresolved_reloc = false;
4560
0
        BFD_ASSERT (rel->r_addend == 0);
4561
4562
0
        bfd_vma got_off = 0;
4563
0
        if (h != NULL)
4564
0
    {
4565
      /* GOT ref or ifunc.  */
4566
0
      BFD_ASSERT (h->got.offset != MINUS_ONE
4567
0
            || h->type == STT_GNU_IFUNC);
4568
4569
0
      got_off = h->got.offset  & (~(bfd_vma)1);
4570
      /* Hidden symbol not has got entry,
4571
       * only got.plt entry so it is (plt - got).  */
4572
0
      if (h->got.offset == MINUS_ONE && h->type == STT_GNU_IFUNC)
4573
0
        got_off = ifunc_got_off (&htab->elf, h);
4574
4575
0
      if ((h->got.offset & 1) == 0)
4576
0
        {
4577
          /* We need to generate a R_LARCH_RELATIVE reloc once
4578
           * in loongarch_elf_finish_dynamic_symbol or now,
4579
           * call finish_dyn && nopic
4580
           * or !call finish_dyn && pic.  */
4581
0
          if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (is_dyn,
4582
0
                  bfd_link_pic (info),
4583
0
                  h)
4584
0
        && !bfd_is_abs_section(h->root.u.def.section)
4585
0
        && bfd_link_pic (info)
4586
0
        && LARCH_REF_LOCAL (info, h)
4587
0
        && !info->enable_dt_relr)
4588
0
      {
4589
0
        Elf_Internal_Rela rela;
4590
0
        rela.r_offset = sec_addr (got) + got_off;
4591
0
        rela.r_info = ELF32_R_INFO (0, R_LARCH_RELATIVE);
4592
0
        rela.r_addend = relocation;
4593
0
        loongarch_elf_append_rela (info->output_bfd,
4594
0
                 htab->elf.srelgot, &rela);
4595
0
      }
4596
0
          h->got.offset |= 1;
4597
0
          bfd_put_32 (info->output_bfd, relocation,
4598
0
          got->contents + got_off);
4599
0
        }
4600
0
    }
4601
0
        else
4602
0
    {
4603
0
      BFD_ASSERT (local_got_offsets
4604
0
            && local_got_offsets[r_symndx] != MINUS_ONE);
4605
4606
0
      got_off = local_got_offsets[r_symndx] & (~(bfd_vma)1);
4607
0
      if (sym->st_shndx != SHN_ABS
4608
0
          && (local_got_offsets[r_symndx] & 1) == 0)
4609
0
        {
4610
0
          if (bfd_link_pic (info) && !info->enable_dt_relr)
4611
0
      {
4612
0
        Elf_Internal_Rela rela;
4613
0
        rela.r_offset = sec_addr (got) + got_off;
4614
0
        rela.r_info = ELF32_R_INFO (0, R_LARCH_RELATIVE);
4615
0
        rela.r_addend = relocation;
4616
0
        loongarch_elf_append_rela (info->output_bfd,
4617
0
                 htab->elf.srelgot, &rela);
4618
0
      }
4619
0
          local_got_offsets[r_symndx] |= 1;
4620
0
        }
4621
0
      bfd_put_32 (info->output_bfd, relocation,
4622
0
            got->contents + got_off);
4623
0
    }
4624
4625
0
        relocation = got_off + sec_addr (got);
4626
0
      }
4627
4628
0
    if (r_type == R_LARCH_GOT_PC_HI20)
4629
0
      RELOCATE_CALC_PC32_HI20 (relocation, pc);
4630
0
    else if (r_type == R_LARCH_GOT_PCADD_HI20)
4631
0
      {
4632
0
        RELOCATE_CALC_PCADD_HI20(relocation, pc);
4633
0
        if (!loongarch_record_pcrel_hi_reloc (&pcrel_relocs, pc,
4634
0
                relocation, 0, NULL))
4635
0
    r = bfd_reloc_overflow;
4636
0
      }
4637
0
    break;
4638
4639
0
  case R_LARCH_GOT_PC_LO12:
4640
0
  case R_LARCH_GOT64_PC_LO20:
4641
0
  case R_LARCH_GOT64_PC_HI12:
4642
0
  case R_LARCH_GOT_LO12:
4643
0
  case R_LARCH_GOT64_LO20:
4644
0
  case R_LARCH_GOT64_HI12:
4645
0
      {
4646
0
        unresolved_reloc = false;
4647
0
        bfd_vma got_off;
4648
0
        if (h)
4649
0
    got_off = h->got.offset & (~(bfd_vma)1);
4650
0
        else
4651
0
    got_off = local_got_offsets[r_symndx] & (~(bfd_vma)1);
4652
4653
0
        if (h && h->got.offset == MINUS_ONE && h->type == STT_GNU_IFUNC)
4654
0
    got_off = ifunc_got_off (&htab->elf, h);
4655
4656
0
        relocation = got_off + sec_addr (got);
4657
0
      }
4658
4659
0
    if (r_type == R_LARCH_GOT64_PC_HI12)
4660
0
      RELOCATE_CALC_PC64_HI32 (relocation, pc - 12);
4661
0
    else if (r_type == R_LARCH_GOT64_PC_LO20)
4662
0
      RELOCATE_CALC_PC64_HI32 (relocation, pc - 8);
4663
4664
0
    break;
4665
4666
0
  case R_LARCH_TLS_LE_HI20_R:
4667
0
    relocation += rel->r_addend;
4668
0
    relocation = tlsoff (info, relocation);
4669
0
    RELOCATE_TLS_TP32_HI20 (relocation);
4670
0
    break;
4671
4672
0
  case R_LARCH_TLS_LE_HI20:
4673
0
  case R_LARCH_TLS_LE_LO12:
4674
0
  case R_LARCH_TLS_LE_LO12_R:
4675
0
  case R_LARCH_TLS_LE64_LO20:
4676
0
  case R_LARCH_TLS_LE64_HI12:
4677
0
    BFD_ASSERT (bfd_link_executable (info));
4678
4679
0
    relocation += rel->r_addend;
4680
0
    relocation = tlsoff (info, relocation);
4681
0
    break;
4682
4683
  /* TLS IE LD/GD process separately is troublesome.
4684
     When a symbol is both ie and LD/GD, h->got.off |= 1
4685
     make only one type be relocated.  We must use
4686
     h->got.offset |= 1 and h->got.offset |= 2
4687
     diff IE and LD/GD.  And all (got_off & (~(bfd_vma)1))
4688
     (IE LD/GD and reusable GOT reloc) must change to
4689
     (got_off & (~(bfd_vma)3)), beause we use lowest 2 bits
4690
     as a tag.
4691
     Now, LD and GD is both GOT_TLS_GD type, LD seems to
4692
     can be omitted.  */
4693
0
  case R_LARCH_TLS_IE_PC_HI20:
4694
0
  case R_LARCH_TLS_IE_PCADD_HI20:
4695
0
  case R_LARCH_TLS_IE_HI20:
4696
0
  case R_LARCH_TLS_LD_PC_HI20:
4697
0
  case R_LARCH_TLS_LD_PCADD_HI20:
4698
0
  case R_LARCH_TLS_LD_HI20:
4699
0
  case R_LARCH_TLS_GD_PC_HI20:
4700
0
  case R_LARCH_TLS_GD_PCADD_HI20:
4701
0
  case R_LARCH_TLS_GD_HI20:
4702
0
  case R_LARCH_TLS_DESC_PC_HI20:
4703
0
  case R_LARCH_TLS_DESC_PCADD_HI20:
4704
0
  case R_LARCH_TLS_DESC_HI20:
4705
0
  case R_LARCH_TLS_LD_PCREL20_S2:
4706
0
  case R_LARCH_TLS_GD_PCREL20_S2:
4707
0
  case R_LARCH_TLS_DESC_PCREL20_S2:
4708
0
    BFD_ASSERT (rel->r_addend == 0);
4709
0
    unresolved_reloc = false;
4710
4711
0
    if (r_type == R_LARCH_TLS_IE_PC_HI20
4712
0
        || r_type == R_LARCH_TLS_IE_PCADD_HI20
4713
0
        || r_type == R_LARCH_TLS_IE_HI20)
4714
0
      is_ie = true;
4715
4716
0
    if (r_type == R_LARCH_TLS_DESC_PC_HI20
4717
0
        || r_type == R_LARCH_TLS_DESC_PCADD_HI20
4718
0
        || r_type == R_LARCH_TLS_DESC_HI20
4719
0
        || r_type == R_LARCH_TLS_DESC_PCREL20_S2)
4720
0
      is_desc = true;
4721
4722
0
    bfd_vma got_off = 0;
4723
0
    if (h != NULL)
4724
0
      {
4725
0
        got_off = h->got.offset;
4726
0
        h->got.offset |= 1;
4727
0
      }
4728
0
    else
4729
0
      {
4730
0
        got_off = local_got_offsets[r_symndx];
4731
0
        local_got_offsets[r_symndx] |= 1;
4732
0
      }
4733
4734
0
    BFD_ASSERT (got_off != MINUS_ONE);
4735
4736
0
    tls_type = _bfd_loongarch_elf_tls_type (input_bfd, h, r_symndx);
4737
4738
    /* If a tls variable is accessed in multiple ways, GD uses
4739
       the first two slots of GOT, desc follows with two slots,
4740
       and IE uses one slot at the end.  */
4741
0
    off = 0;
4742
0
    if (tls_type & GOT_TLS_GD)
4743
0
      off += 2 * GOT_ENTRY_SIZE;
4744
0
    desc_off = off;
4745
0
    if (tls_type & GOT_TLS_GDESC)
4746
0
      off += 2 * GOT_ENTRY_SIZE;
4747
0
    ie_off = off;
4748
4749
0
    if ((got_off & 1) == 0)
4750
0
      {
4751
0
        Elf_Internal_Rela rela;
4752
0
        asection *relgot = htab->elf.srelgot;
4753
4754
0
        int indx = 0;
4755
0
        bool need_reloc = false;
4756
0
        LARCH_TLS_GD_IE_NEED_DYN_RELOC (info, is_dyn, h, indx,
4757
0
                need_reloc);
4758
4759
0
        if (tls_type & GOT_TLS_GD)
4760
0
    {
4761
0
      if (need_reloc)
4762
0
        {
4763
      /* Dynamic resolved Module ID.  */
4764
0
          rela.r_offset = sec_addr (got) + got_off;
4765
0
          rela.r_addend = 0;
4766
0
          rela.r_info = ELF32_R_INFO (indx,R_LARCH_TLS_DTPMOD32);
4767
0
          bfd_put_32 (info->output_bfd, 0, got->contents + got_off);
4768
0
          loongarch_elf_append_rela (info->output_bfd,
4769
0
             relgot, &rela);
4770
4771
0
          if (indx == 0)
4772
0
      {
4773
        /* Local symbol, tp offset has been known.  */
4774
0
        BFD_ASSERT (! unresolved_reloc);
4775
0
        bfd_put_32 (info->output_bfd,
4776
0
            tlsoff (info, relocation),
4777
0
            (got->contents + got_off + GOT_ENTRY_SIZE));
4778
0
      }
4779
0
          else
4780
0
      {
4781
        /* Dynamic resolved block offset.  */
4782
0
        bfd_put_32 (info->output_bfd, 0,
4783
0
            got->contents + got_off + GOT_ENTRY_SIZE);
4784
0
        rela.r_info = ELF32_R_INFO (indx,
4785
0
            R_LARCH_TLS_DTPREL32);
4786
0
        rela.r_offset += GOT_ENTRY_SIZE;
4787
0
        loongarch_elf_append_rela (info->output_bfd,
4788
0
                 relgot, &rela);
4789
0
      }
4790
0
        }
4791
0
      else
4792
0
        {
4793
          /* In a static link or an executable link with the symbol
4794
       binding locally.  Mark it as belonging to module 1.  */
4795
0
          bfd_put_32 (info->output_bfd, 1, got->contents + got_off);
4796
0
          bfd_put_32 (info->output_bfd, tlsoff (info, relocation),
4797
0
        got->contents + got_off + GOT_ENTRY_SIZE);
4798
0
        }
4799
0
    }
4800
0
        if (tls_type & GOT_TLS_GDESC)
4801
0
    {
4802
      /* Unless it is a static link, DESC always emits a
4803
         dynamic relocation.  */
4804
0
      indx = h && h->dynindx != -1 ? h->dynindx : 0;
4805
0
      rela.r_offset = sec_addr (got) + got_off + desc_off;
4806
0
      rela.r_addend = 0;
4807
0
      if (indx == 0)
4808
0
        rela.r_addend = tlsoff (info, relocation);
4809
4810
0
      rela.r_info = ELF32_R_INFO (indx, R_LARCH_TLS_DESC32);
4811
0
      loongarch_elf_append_rela (info->output_bfd, relgot, &rela);
4812
0
      bfd_put_32 (info->output_bfd, 0,
4813
0
            got->contents + got_off + desc_off);
4814
0
    }
4815
0
        if (tls_type & GOT_TLS_IE)
4816
0
    {
4817
0
      if (need_reloc)
4818
0
        {
4819
0
          bfd_put_32 (info->output_bfd, 0,
4820
0
        got->contents + got_off + ie_off);
4821
0
          rela.r_offset = sec_addr (got) + got_off + ie_off;
4822
0
          rela.r_addend = 0;
4823
4824
0
          if (indx == 0)
4825
0
      rela.r_addend = tlsoff (info, relocation);
4826
0
          rela.r_info = ELF32_R_INFO (indx, R_LARCH_TLS_TPREL32);
4827
0
          loongarch_elf_append_rela (info->output_bfd,
4828
0
             relgot, &rela);
4829
0
        }
4830
0
      else
4831
0
        {
4832
          /* In a static link or an executable link with the symbol
4833
       bindinglocally, compute offset directly.  */
4834
0
          bfd_put_32 (info->output_bfd, tlsoff (info, relocation),
4835
0
        got->contents + got_off + ie_off);
4836
0
        }
4837
0
    }
4838
0
      }
4839
0
    relocation = (got_off & (~(bfd_vma)1)) + sec_addr (got);
4840
0
    if (is_desc)
4841
0
      relocation += desc_off;
4842
0
    else if (is_ie)
4843
0
      relocation += ie_off;
4844
4845
0
    if (r_type == R_LARCH_TLS_LD_PC_HI20
4846
0
        || r_type == R_LARCH_TLS_GD_PC_HI20
4847
0
        || r_type == R_LARCH_TLS_IE_PC_HI20
4848
0
        || r_type == R_LARCH_TLS_DESC_PC_HI20)
4849
0
      RELOCATE_CALC_PC32_HI20 (relocation, pc);
4850
0
    else if (r_type == R_LARCH_TLS_LD_PCREL20_S2
4851
0
        || r_type == R_LARCH_TLS_GD_PCREL20_S2
4852
0
        || r_type == R_LARCH_TLS_DESC_PCREL20_S2)
4853
0
      relocation -= pc;
4854
0
    else if (r_type == R_LARCH_TLS_IE_PCADD_HI20
4855
0
       || r_type == R_LARCH_TLS_LD_PCADD_HI20
4856
0
       || r_type == R_LARCH_TLS_GD_PCADD_HI20
4857
0
       || r_type == R_LARCH_TLS_DESC_PCADD_HI20)
4858
4859
0
      {
4860
0
        RELOCATE_CALC_PCADD_HI20(relocation, pc);
4861
0
        if (!loongarch_record_pcrel_hi_reloc (&pcrel_relocs, pc,
4862
0
                relocation, 0, NULL))
4863
0
    r = bfd_reloc_overflow;
4864
0
      }
4865
    /* else {} ABS relocations.  */
4866
0
    break;
4867
4868
0
  case R_LARCH_TLS_DESC_PC_LO12:
4869
0
  case R_LARCH_TLS_DESC64_PC_LO20:
4870
0
  case R_LARCH_TLS_DESC64_PC_HI12:
4871
0
  case R_LARCH_TLS_DESC_LO12:
4872
0
  case R_LARCH_TLS_DESC64_LO20:
4873
0
  case R_LARCH_TLS_DESC64_HI12:
4874
0
    {
4875
0
      unresolved_reloc = false;
4876
4877
0
      if (h)
4878
0
        relocation = sec_addr (got) + (h->got.offset & (~(bfd_vma)1));
4879
0
      else
4880
0
        relocation = sec_addr (got)
4881
0
         + (local_got_offsets[r_symndx] & (~(bfd_vma)1));
4882
4883
0
      tls_type = _bfd_loongarch_elf_tls_type (input_bfd, h, r_symndx);
4884
      /* Use both TLS_GD and TLS_DESC.  */
4885
0
      if (GOT_TLS_GD_BOTH_P (tls_type))
4886
0
        relocation += 2 * GOT_ENTRY_SIZE;
4887
4888
0
      if (r_type == R_LARCH_TLS_DESC64_PC_LO20)
4889
0
        RELOCATE_CALC_PC64_HI32 (relocation, pc - 8);
4890
0
      else if (r_type == R_LARCH_TLS_DESC64_PC_HI12)
4891
0
        RELOCATE_CALC_PC64_HI32 (relocation, pc - 12);
4892
4893
0
      break;
4894
0
    }
4895
4896
0
  case R_LARCH_TLS_DESC_LD:
4897
0
  case R_LARCH_TLS_DESC_CALL:
4898
0
    unresolved_reloc = false;
4899
0
    break;
4900
4901
0
  case R_LARCH_TLS_IE_PC_LO12:
4902
0
  case R_LARCH_TLS_IE64_PC_LO20:
4903
0
  case R_LARCH_TLS_IE64_PC_HI12:
4904
0
  case R_LARCH_TLS_IE_LO12:
4905
0
  case R_LARCH_TLS_IE64_LO20:
4906
0
  case R_LARCH_TLS_IE64_HI12:
4907
0
    unresolved_reloc = false;
4908
4909
0
    if (h)
4910
0
      relocation = sec_addr (got) + (h->got.offset & (~(bfd_vma)1));
4911
0
    else
4912
0
      relocation = sec_addr (got)
4913
0
        + (local_got_offsets[r_symndx] & (~(bfd_vma)1));
4914
4915
0
    tls_type = _bfd_loongarch_elf_tls_type (input_bfd, h, r_symndx);
4916
    /* Use TLS_GD TLS_DESC and TLS_IE.  */
4917
0
    if (GOT_TLS_GD_BOTH_P (tls_type) && (tls_type & GOT_TLS_IE))
4918
0
      relocation += 4 * GOT_ENTRY_SIZE;
4919
    /* Use GOT_TLS_GD_ANY_P (tls_type) and TLS_IE.  */
4920
0
    else if (GOT_TLS_GD_ANY_P (tls_type) && (tls_type & GOT_TLS_IE))
4921
0
      relocation += 2 * GOT_ENTRY_SIZE;
4922
4923
0
    if (r_type == R_LARCH_TLS_IE64_PC_LO20)
4924
0
      RELOCATE_CALC_PC64_HI32 (relocation, pc - 8);
4925
0
    else if (r_type == R_LARCH_TLS_IE64_PC_HI12)
4926
0
      RELOCATE_CALC_PC64_HI32 (relocation, pc - 12);
4927
4928
0
    break;
4929
4930
0
  case R_LARCH_RELAX:
4931
0
  case R_LARCH_ALIGN:
4932
0
    r = bfd_reloc_continue;
4933
0
    unresolved_reloc = false;
4934
0
    break;
4935
4936
0
  default:
4937
0
    break;
4938
0
  }
4939
4940
0
      if (fatal)
4941
0
  break;
4942
4943
0
      do
4944
0
  {
4945
    /* 'unresolved_reloc' means we haven't done it yet.
4946
       We need help of dynamic linker to fix this memory location up.  */
4947
0
    if (!unresolved_reloc)
4948
0
      break;
4949
4950
0
    if (_bfd_elf_section_offset (info->output_bfd, info, input_section,
4951
0
               rel->r_offset) == MINUS_ONE)
4952
      /* WHY? May because it's invalid so skip checking.
4953
         But why dynamic reloc a invalid section?  */
4954
0
      break;
4955
4956
0
    if (input_section->output_section->flags & SEC_DEBUGGING)
4957
0
      {
4958
0
        fatal = (loongarch_reloc_is_fatal
4959
0
           (info, input_bfd, input_section, rel, howto,
4960
0
      bfd_reloc_dangerous, is_undefweak, name,
4961
0
      "Seems dynamic linker not process "
4962
0
      "sections 'SEC_DEBUGGING'."));
4963
0
      }
4964
0
    if (!is_dyn)
4965
0
      break;
4966
4967
0
    if ((info->flags & DF_TEXTREL) == 0)
4968
0
      if (input_section->output_section->flags & SEC_READONLY)
4969
0
        info->flags |= DF_TEXTREL;
4970
0
  }
4971
0
      while (0);
4972
4973
0
      if (fatal)
4974
0
  break;
4975
4976
0
      loongarch_record_one_reloc (input_bfd, input_section, r_type,
4977
0
          rel->r_offset, sym, h, rel->r_addend);
4978
4979
0
      if (r != bfd_reloc_continue)
4980
0
  r = perform_relocation (rel, input_section, howto, relocation,
4981
0
        input_bfd, contents);
4982
4983
0
      switch (r)
4984
0
  {
4985
0
  case bfd_reloc_dangerous:
4986
0
  case bfd_reloc_continue:
4987
0
  case bfd_reloc_ok:
4988
0
    continue;
4989
4990
0
  case bfd_reloc_overflow:
4991
    /* Overflow value can't be filled in.  */
4992
0
    loongarch_dump_reloc_record (info->callbacks->info);
4993
0
    info->callbacks->reloc_overflow
4994
0
      (info, h ? &h->root : NULL, name, howto->name, rel->r_addend,
4995
0
       input_bfd, input_section, rel->r_offset);
4996
0
    if (r_type == R_LARCH_PCREL20_S2
4997
0
        || r_type == R_LARCH_TLS_LD_PCREL20_S2
4998
0
        || r_type == R_LARCH_TLS_GD_PCREL20_S2
4999
0
        || r_type == R_LARCH_TLS_DESC_PCREL20_S2)
5000
0
      _bfd_error_handler (_("recompile with 'gcc -mno-relax' or"
5001
0
          " 'as -mno-relax' or 'ld --no-relax'"));
5002
0
    break;
5003
5004
0
  case bfd_reloc_outofrange:
5005
    /* Stack state incorrect.  */
5006
0
    loongarch_dump_reloc_record (info->callbacks->info);
5007
0
    info->callbacks->info
5008
0
      ("%X%H: Internal stack state is incorrect.\n"
5009
0
       "Want to push to full stack or pop from empty stack?\n",
5010
0
       input_bfd, input_section, rel->r_offset);
5011
0
    break;
5012
5013
0
  case bfd_reloc_notsupported:
5014
0
    info->callbacks->info ("%X%H: Unknown relocation type.\n", input_bfd,
5015
0
         input_section, rel->r_offset);
5016
0
    break;
5017
5018
0
  default:
5019
0
    info->callbacks->info ("%X%H: Internal: unknown error.\n", input_bfd,
5020
0
         input_section, rel->r_offset);
5021
0
    break;
5022
0
  }
5023
5024
0
      fatal = true;
5025
0
    }
5026
5027
0
  bool ret = loongarch_resolve_pcrel_lo_relocs (&pcrel_relocs);
5028
0
  fatal = !ret;
5029
0
  loongarch_free_pcrel_reloc (&pcrel_relocs);
5030
5031
0
  return !fatal;
5032
0
}
5033
5034
/* A pending delete op during a linker relaxation trip, to be stored in a
5035
   splay tree.
5036
   The key is the starting offset of this op's deletion range, interpreted
5037
   as if no delete op were executed for this trip.  */
5038
struct pending_delete_op
5039
{
5040
  /* Number of bytes to delete at the address.  */
5041
  bfd_size_type size;
5042
5043
  /* The total offset adjustment at the address as if all preceding delete
5044
     ops had been executed.  Used for calculating expected addresses after
5045
     relaxation without actually adjusting anything.  */
5046
  bfd_size_type cumulative_offset;
5047
};
5048
5049
static int
5050
pending_delete_op_compare (splay_tree_key a, splay_tree_key b)
5051
0
{
5052
0
  bfd_vma off_a = (bfd_vma)a;
5053
0
  bfd_vma off_b = (bfd_vma)b;
5054
5055
0
  if (off_a < off_b)
5056
0
    return -1;
5057
0
  else if (off_a > off_b)
5058
0
    return 1;
5059
0
  else
5060
0
    return 0;
5061
0
}
5062
5063
static void *
5064
_allocate_on_bfd (int wanted, void *data)
5065
0
{
5066
0
  bfd *abfd = (bfd *)data;
5067
0
  return bfd_alloc (abfd, wanted);
5068
0
}
5069
5070
static void
5071
_deallocate_on_bfd (void *p ATTRIBUTE_UNUSED, void *data ATTRIBUTE_UNUSED)
5072
0
{
5073
  /* Nothing to do; the data will get released along with the associated BFD
5074
     or an early bfd_release call.  */
5075
0
}
5076
5077
static splay_tree
5078
pending_delete_ops_new (bfd *abfd)
5079
0
{
5080
  /* The node values are allocated with bfd_zalloc, so they are automatically
5081
     taken care of at BFD release time.  */
5082
0
  return splay_tree_new_with_allocator (pending_delete_op_compare, NULL, NULL,
5083
0
      _allocate_on_bfd, _deallocate_on_bfd, abfd);
5084
0
}
5085
5086
static bfd_vma
5087
loongarch_calc_relaxed_addr (struct bfd_link_info *info, bfd_vma offset)
5088
0
{
5089
0
  struct loongarch_elf_link_hash_table *htab = loongarch_elf_hash_table (info);
5090
0
  splay_tree pdops = htab->pending_delete_ops;
5091
0
  struct pending_delete_op *op;
5092
0
  splay_tree_node node;
5093
5094
0
  if (!pdops)
5095
    /* Currently this means we are past the stages where byte deletion could
5096
       possibly happen.  */
5097
0
    return offset;
5098
5099
  /* Find the op that starts just before the given address.  */
5100
0
  node = splay_tree_predecessor (pdops, (splay_tree_key)offset);
5101
0
  if (node == NULL)
5102
    /* Nothing has been deleted yet.  */
5103
0
    return offset;
5104
0
  BFD_ASSERT (((bfd_vma)node->key) < offset);
5105
0
  op = (struct pending_delete_op *)node->value;
5106
5107
  /* If offset is inside this op's range, it is actually one of the deleted
5108
     bytes, so the adjusted node->key should be returned in this case.  */
5109
0
  bfd_vma op_end_off = (bfd_vma)node->key + op->size;
5110
0
  if (offset < op_end_off)
5111
0
    {
5112
0
      offset = (bfd_vma)node->key;
5113
0
      node = splay_tree_predecessor (pdops, node->key);
5114
0
      op = node ? (struct pending_delete_op *)node->value : NULL;
5115
0
    }
5116
5117
0
  return offset - (op ? op->cumulative_offset : 0);
5118
0
}
5119
5120
static void
5121
loongarch_relax_delete_bytes (bfd *abfd,
5122
            bfd_vma addr,
5123
            size_t count,
5124
            struct bfd_link_info *link_info)
5125
0
{
5126
0
  struct loongarch_elf_link_hash_table *htab
5127
0
      = loongarch_elf_hash_table (link_info);
5128
0
  splay_tree pdops = htab->pending_delete_ops;
5129
0
  splay_tree_node node;
5130
0
  struct pending_delete_op *op = NULL, *new_op = NULL;
5131
0
  bool need_new_node = true;
5132
5133
0
  if (count == 0)
5134
0
    return;
5135
5136
0
  BFD_ASSERT (pdops != NULL);
5137
5138
0
  node = splay_tree_predecessor (pdops, addr);
5139
0
  if (node)
5140
0
    {
5141
0
      op = (struct pending_delete_op *)node->value;
5142
0
      if ((bfd_vma)node->key + op->size >= addr)
5143
0
  {
5144
    /* The previous op already covers this offset, coalesce the new op
5145
       into it.  */
5146
0
    op->size += count;
5147
0
    op->cumulative_offset += count;
5148
0
    need_new_node = false;
5149
0
  }
5150
0
    }
5151
5152
0
  if (need_new_node)
5153
0
    {
5154
0
      new_op = bfd_zalloc (abfd, sizeof (struct pending_delete_op));
5155
0
      new_op->size = count;
5156
0
      new_op->cumulative_offset = (op ? op->cumulative_offset : 0) + count;
5157
0
      node = splay_tree_insert (pdops, (splay_tree_key)addr,
5158
0
        (splay_tree_value)new_op);
5159
0
    }
5160
5161
  /* Adjust all cumulative offsets after this op.  At this point either:
5162
     - a new node is created, in which case `node` has been updated with the
5163
       new value, or
5164
     - an existing node is to be reused, in which case `node` is untouched by
5165
       the new node logic above and appropriate to use,
5166
     so we can just re-use `node` here.  */
5167
0
  for (node = splay_tree_successor (pdops, node->key); node != NULL;
5168
0
       node = splay_tree_successor (pdops, node->key))
5169
0
    {
5170
0
      op = (struct pending_delete_op *)node->value;
5171
0
      op->cumulative_offset += count;
5172
0
    }
5173
0
}
5174
5175
static void
5176
loongarch_relax_delete_or_nop (bfd *abfd,
5177
             asection *sec,
5178
             bfd_vma addr,
5179
             size_t count,
5180
             struct bfd_link_info *link_info)
5181
0
{
5182
0
  struct bfd_elf_section_data *data = elf_section_data (sec);
5183
0
  bfd_byte *contents = data->this_hdr.contents;
5184
5185
0
  BFD_ASSERT (count % 4 == 0);
5186
5187
0
  if (!loongarch_sec_closed_for_deletion (sec))
5188
0
    {
5189
      /* Deletions are still possible within the section.  */
5190
0
      loongarch_relax_delete_bytes (abfd, addr, count, link_info);
5191
0
      return;
5192
0
    }
5193
5194
  /* We can no longer delete bytes in the section after enforcing alignment.
5195
     But as the resulting shrinkage may open up a few more relaxation chances,
5196
     allowing unnecessary instructions to be replaced with NOPs instead of
5197
     being removed altogether may still benefit performance to a lesser
5198
     extent.  */
5199
0
  for (; count; addr += 4, count -= 4)
5200
0
    bfd_put (32, abfd, LARCH_NOP, contents + addr);
5201
0
}
5202
5203
/* If some bytes in a symbol is deleted, we need to adjust its size.  */
5204
static void
5205
loongarch_relax_resize_symbol (bfd_size_type *size, bfd_vma orig_value,
5206
             splay_tree pdops)
5207
0
{
5208
0
  splay_tree_key key = (splay_tree_key)orig_value;
5209
0
  bfd_vma orig_end = orig_value + *size;
5210
0
  splay_tree_node node = splay_tree_predecessor (pdops, key);
5211
5212
0
  if (node)
5213
0
    {
5214
0
      bfd_vma addr = (bfd_vma)node->key;
5215
0
      struct pending_delete_op *op = (struct pending_delete_op *)node->value;
5216
5217
      /* This shouldn't happen unless people write something really insane like
5218
       .reloc ., R_LARCH_ALIGN, 60
5219
       .rept 15
5220
       1: nop
5221
       .endr
5222
       .set x, 1b
5223
       .size x, . - 1b
5224
   But let's just try to make it "work" anyway.  */
5225
0
      if (orig_value < addr + op->size)
5226
0
  {
5227
0
    bfd_size_type n_deleted = op->size - (orig_value - addr);
5228
0
    if (n_deleted >= *size)
5229
0
      {
5230
0
        *size = 0;
5231
0
        return;
5232
0
      }
5233
5234
0
    *size -= n_deleted;
5235
0
  }
5236
0
    }
5237
5238
0
  node = splay_tree_lookup (pdops, key);
5239
0
  if (!node)
5240
0
    node = splay_tree_successor (pdops, key);
5241
5242
0
  for (; node; node = splay_tree_successor (pdops, node->key))
5243
0
    {
5244
0
      bfd_vma addr = (bfd_vma)node->key;
5245
0
      struct pending_delete_op *op = (struct pending_delete_op *)node->value;
5246
5247
0
      if (addr >= orig_end)
5248
0
  return;
5249
5250
0
      if (orig_end < addr + op->size)
5251
0
  *size -= orig_end - addr;
5252
0
      else
5253
0
  *size -= op->size;
5254
0
    }
5255
0
}
5256
5257
/* Adjust relocations that refer to STT_SECTION symbol.  The section symbol
5258
   value is zero, so the offset into SEC is stored in the addend.  */
5259
static bool
5260
loongarch_relax_adjust_reloc_addends (bfd *abfd, asection *sec,
5261
              struct bfd_link_info *link_info)
5262
0
{
5263
0
  asection *relsec;
5264
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
5265
0
  Elf_Internal_Sym *local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
5266
0
  unsigned int sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5267
5268
0
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
5269
0
    {
5270
0
      Elf_Internal_Rela *relocs;
5271
0
      unsigned int i;
5272
5273
0
      if (relsec->reloc_count == 0)
5274
0
  continue;
5275
5276
0
      relocs = elf_section_data (relsec)->relocs;
5277
0
      if (relocs == NULL)
5278
0
  {
5279
    /* Keep addend updates for later relaxation passes.  */
5280
0
    relocs = _bfd_elf_link_read_relocs (abfd, relsec, NULL, NULL,
5281
0
                true);
5282
0
    if (relocs == NULL)
5283
0
      return false;
5284
0
  }
5285
5286
0
      for (i = 0; i < relsec->reloc_count; i++)
5287
0
  {
5288
0
    Elf_Internal_Rela *rel = relocs + i;
5289
0
    if (rel->r_addend == 0)
5290
0
      continue;
5291
5292
0
    unsigned int r_symndx = ELF32_R_SYM (rel->r_info);
5293
0
    unsigned int r_type = ELF32_R_TYPE (rel->r_info);
5294
5295
    /* R_LARCH_ALIGN stores alignment information in r_addend.  */
5296
0
    if (r_symndx >= symtab_hdr->sh_info
5297
0
        || r_type == R_LARCH_ALIGN)
5298
0
      continue;
5299
5300
0
    Elf_Internal_Sym *sym = local_syms + r_symndx;
5301
0
    if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
5302
0
        && sym->st_shndx == sec_shndx)
5303
0
      rel->r_addend = loongarch_calc_relaxed_addr (link_info,
5304
0
               rel->r_addend);
5305
0
  }
5306
0
    }
5307
5308
0
  return true;
5309
0
}
5310
5311
static bool
5312
loongarch_relax_perform_deletes (bfd *abfd, asection *sec,
5313
         struct bfd_link_info *link_info)
5314
0
{
5315
0
  unsigned int i, symcount;
5316
0
  bfd_vma toaddr = sec->size;
5317
0
  struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
5318
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
5319
0
  unsigned int sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5320
0
  struct bfd_elf_section_data *data = elf_section_data (sec);
5321
0
  bfd_byte *contents = data->this_hdr.contents, *contents_end = NULL;
5322
0
  struct relr_entry *relr = loongarch_elf_section_data (sec)->relr;
5323
0
  struct loongarch_elf_link_hash_table *htab =
5324
0
    loongarch_elf_hash_table (link_info);
5325
0
  struct relr_entry *relr_end = NULL;
5326
0
  splay_tree pdops = htab->pending_delete_ops;
5327
0
  splay_tree_node node1 = NULL, node2 = NULL;
5328
5329
0
  if (htab->relr_count)
5330
0
    relr_end = htab->relr + htab->relr_count;
5331
5332
0
  BFD_ASSERT (pdops != NULL);
5333
0
  node1 = splay_tree_min (pdops);
5334
5335
0
  if (node1 == NULL)
5336
    /* No pending delete ops, nothing to do.  */
5337
0
    return true;
5338
5339
  /* Actually delete the bytes.  For each delete op the pointer arithmetics
5340
     look like this:
5341
5342
       node1->key -\      /- node2->key
5343
       |<- op1->size ->|      |
5344
       v     v      v
5345
  ...-DDDDDD-------xxxxxxxxxxxxxxxxxSSSSSSxxxxxxxxxx----...
5346
      ^     ^       ^
5347
      contents_end      node1->key + op1->size
5348
      |
5349
      contents_end after this memmove
5350
5351
     where the "S" and "D" bytes are the memmove's source and destination
5352
     respectively.  In case node1 is the first op, contents_end is initialized
5353
     to the op's start; in case node2 == NULL, the chunk's end is the section's
5354
     end.  The contents_end pointer will be bumped to the new end of content
5355
     after each memmove.  As no byte is added during the process, it is
5356
     guaranteed to trail behind the delete ops, and all bytes overwritten are
5357
     either already copied by an earlier memmove or meant to be discarded.
5358
5359
     For memmove, we need to translate offsets to pointers by adding them to
5360
     `contents`.  */
5361
0
  for (; node1; node1 = node2)
5362
0
    {
5363
0
      struct pending_delete_op *op1 = (struct pending_delete_op *)node1->value;
5364
0
      bfd_vma op1_start_off = (bfd_vma)node1->key;
5365
0
      bfd_vma op1_end_off = op1_start_off + op1->size;
5366
0
      node2 = splay_tree_successor (pdops, node1->key);
5367
0
      bfd_vma op2_start_off = node2 ? (bfd_vma)node2->key : toaddr;
5368
0
      bfd_size_type count = op2_start_off - op1_end_off;
5369
5370
0
      if (count)
5371
0
  {
5372
0
    if (contents_end == NULL)
5373
      /* Start from the end of the first unmodified content chunk.  */
5374
0
      contents_end = contents + op1_start_off;
5375
5376
0
    memmove (contents_end, contents + op1_end_off, count);
5377
0
    contents_end += count;
5378
0
  }
5379
5380
      /* Adjust the section size once, when we have reached the end.  */
5381
0
      if (node2 == NULL)
5382
0
  sec->size -= op1->cumulative_offset;
5383
0
    }
5384
5385
0
  if (loongarch_sec_sym_has_reloc_ref (sec))
5386
0
      if (! loongarch_relax_adjust_reloc_addends (abfd, sec, link_info))
5387
0
  return false;
5388
5389
  /* Adjust the location of all of the relocs.  */
5390
0
  for (i = 0; i < sec->reloc_count; i++)
5391
0
    if (data->relocs[i].r_offset < toaddr)
5392
0
      data->relocs[i].r_offset = loongarch_calc_relaxed_addr (
5393
0
    link_info, data->relocs[i].r_offset);
5394
5395
  /* Likewise for relative relocs to be packed into .relr.  */
5396
0
  for (; relr && relr < relr_end && relr->sec == sec; relr++)
5397
0
    if (relr->off < toaddr)
5398
0
      relr->off = loongarch_calc_relaxed_addr (link_info, relr->off);
5399
5400
  /* Adjust the local symbols defined in this section.  */
5401
0
  for (i = 0; i < symtab_hdr->sh_info; i++)
5402
0
    {
5403
0
      Elf_Internal_Sym *sym = (Elf_Internal_Sym *) symtab_hdr->contents + i;
5404
0
      if (sym->st_shndx == sec_shndx)
5405
0
  {
5406
0
    bfd_vma orig_value = sym->st_value;
5407
0
    if (orig_value <= toaddr)
5408
0
      sym->st_value
5409
0
    = loongarch_calc_relaxed_addr (link_info, orig_value);
5410
5411
0
    if (orig_value + sym->st_size <= toaddr)
5412
0
      loongarch_relax_resize_symbol (&sym->st_size, orig_value, pdops);
5413
0
  }
5414
0
    }
5415
5416
  /* Now adjust the global symbols defined in this section.  */
5417
0
  symcount = ((symtab_hdr->sh_size / sizeof (Elf32_External_Sym))
5418
0
        - symtab_hdr->sh_info);
5419
5420
0
  for (i = 0; i < symcount; i++)
5421
0
    {
5422
0
      struct elf_link_hash_entry *sym_hash = sym_hashes[i];
5423
5424
      /* The '--wrap SYMBOL' option is causing a pain when the object file,
5425
   containing the definition of __wrap_SYMBOL, includes a direct
5426
   call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
5427
   the same symbol (which is __wrap_SYMBOL), but still exist as two
5428
   different symbols in 'sym_hashes', we don't want to adjust
5429
   the global symbol __wrap_SYMBOL twice.
5430
5431
   The same problem occurs with symbols that are versioned_hidden, as
5432
   foo becomes an alias for foo@BAR, and hence they need the same
5433
   treatment.  */
5434
0
      if (link_info->wrap_hash != NULL
5435
0
    || sym_hash->versioned != unversioned)
5436
0
  {
5437
0
    struct elf_link_hash_entry **cur_sym_hashes;
5438
5439
    /* Loop only over the symbols which have already been checked.  */
5440
0
    for (cur_sym_hashes = sym_hashes; cur_sym_hashes < &sym_hashes[i];
5441
0
         cur_sym_hashes++)
5442
0
      {
5443
        /* If the current symbol is identical to 'sym_hash', that means
5444
     the symbol was already adjusted (or at least checked).  */
5445
0
        if (*cur_sym_hashes == sym_hash)
5446
0
    break;
5447
0
      }
5448
    /* Don't adjust the symbol again.  */
5449
0
    if (cur_sym_hashes < &sym_hashes[i])
5450
0
      continue;
5451
0
  }
5452
5453
0
      if ((sym_hash->root.type == bfd_link_hash_defined
5454
0
     || sym_hash->root.type == bfd_link_hash_defweak)
5455
0
    && sym_hash->root.u.def.section == sec)
5456
0
  {
5457
0
    bfd_vma orig_value = sym_hash->root.u.def.value;
5458
5459
    /* As above, adjust the value and size.  */
5460
0
    if (orig_value <= toaddr)
5461
0
      sym_hash->root.u.def.value
5462
0
    = loongarch_calc_relaxed_addr (link_info, orig_value);
5463
5464
0
    if (orig_value + sym_hash->size <= toaddr)
5465
0
      loongarch_relax_resize_symbol (&sym_hash->size, orig_value, pdops);
5466
0
  }
5467
0
    }
5468
5469
0
  return true;
5470
0
}
5471
5472
/* Start perform TLS type transition.
5473
   Currently there are three cases of relocation handled here:
5474
   DESC -> IE, DEC -> LE and IE -> LE.  */
5475
static bool
5476
loongarch_tls_perform_trans (bfd *abfd, asection *sec,
5477
         Elf_Internal_Rela *rel,
5478
         struct elf_link_hash_entry *h,
5479
         struct bfd_link_info *info,
5480
         bfd_vma symval,
5481
         loongarch_pcrel_relocs *pcrel_relocs)
5482
0
{
5483
0
  unsigned long insn;
5484
0
  loongarch_pcrel_hi_reloc *hi;
5485
0
  unsigned long r_type = ELF32_R_TYPE (rel->r_info);
5486
0
  unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
5487
0
  bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
5488
5489
0
  bool local_exec = bfd_link_executable (info) && LARCH_REF_LOCAL (info, h);
5490
5491
  /* The symbol of the pcadd_lo12 is a local symbol at pcadd_hi20,
5492
     get the real tls symbol in hi20 relocation.  */
5493
0
  if (r_type == R_LARCH_TLS_DESC_PCADD_LO12
5494
0
      || r_type == R_LARCH_TLS_IE_PCADD_LO12)
5495
0
    {
5496
0
      hi = loongarch_find_pcrel_hi_reloc (pcrel_relocs, symval);
5497
0
      h = hi->h;
5498
0
      local_exec = bfd_link_executable (info) && LARCH_REF_LOCAL (info, h);
5499
      /* If tls desc/ie relax to tls le, change the symbol of lo12
5500
   to the symbol of hi20.  */
5501
0
      if (local_exec)
5502
0
  r_symndx = hi->hi_sym;
5503
0
    }
5504
5505
0
  switch (r_type)
5506
0
    {
5507
0
      case R_LARCH_TLS_DESC_PC_HI20:
5508
0
      case R_LARCH_TLS_DESC_PCADD_HI20:
5509
0
  if (local_exec)
5510
0
    {
5511
      /* DESC -> LE relaxation:
5512
         pcalau12i $a0,%desc_pc_hi20(var) =>
5513
         lu12i.w $a0,%le_hi20(var)  */
5514
0
      bfd_put (32, abfd, LARCH_OP_LU12I_W | LARCH_RD_A0,
5515
0
       contents + rel->r_offset);
5516
0
      rel->r_info = ELF32_R_INFO (r_symndx, R_LARCH_TLS_LE_HI20);
5517
0
    }
5518
0
  else
5519
0
    {
5520
      /* DESC -> IE relaxation:
5521
         pcalau12i $a0,%desc_pc_hi20(var) =>
5522
         pcalau12i $a0,%ie_pc_hi20(var)
5523
         or
5524
         pcaddu12i $a0,%desc_pcadd_hi20(var) =>
5525
         pcaddu12i $a0,%ie_pcadd_hi20(var) */
5526
0
      if (r_type == R_LARCH_TLS_DESC_PC_HI20)
5527
0
        rel->r_info = ELF32_R_INFO (r_symndx, R_LARCH_TLS_IE_PC_HI20);
5528
0
      else
5529
0
        rel->r_info = ELF32_R_INFO (r_symndx, R_LARCH_TLS_IE_PCADD_HI20);
5530
0
    }
5531
0
  return true;
5532
5533
0
      case R_LARCH_TLS_DESC_PC_LO12:
5534
0
      case R_LARCH_TLS_DESC_PCADD_LO12:
5535
0
  if (local_exec)
5536
0
    {
5537
      /* DESC -> LE relaxation:
5538
         addi.d $a0,$a0,%desc_pc_lo12(var) =>
5539
         ori  $a0,$a0,le_lo12(var)  */
5540
0
      insn = LARCH_OP_ORI | LARCH_RD_RJ_A0;
5541
0
      bfd_put (32, abfd, LARCH_OP_ORI | LARCH_RD_RJ_A0,
5542
0
         contents + rel->r_offset);
5543
0
      rel->r_info = ELF32_R_INFO (r_symndx, R_LARCH_TLS_LE_LO12);
5544
0
    }
5545
0
  else
5546
0
    {
5547
      /* DESC -> IE relaxation:
5548
         addi.d $a0,$a0,%desc_pc_lo12(var) =>
5549
         ld.d $a0,$a0,%ie_pc_lo12(var)
5550
      */
5551
0
      if (r_type == R_LARCH_TLS_DESC_PC_LO12)
5552
0
        {
5553
0
    bfd_put (32, abfd, LARCH_OP_LD_D | LARCH_RD_RJ_A0,
5554
0
       contents + rel->r_offset);
5555
0
    rel->r_info = ELF32_R_INFO (r_symndx, R_LARCH_TLS_IE_PC_LO12);
5556
0
        }
5557
0
      else
5558
0
        {
5559
    /* FIXME: Get insn to see if .d or .w and put insn.  */
5560
0
    bfd_put (32, abfd, LARCH_OP_LD_W | LARCH_RD_RJ_A0,
5561
0
       contents + rel->r_offset);
5562
0
    rel->r_info = ELF32_R_INFO (r_symndx,
5563
0
              R_LARCH_TLS_IE_PCADD_LO12);
5564
0
        }
5565
0
    }
5566
0
  return true;
5567
5568
0
      case R_LARCH_TLS_DESC_LD:
5569
0
      case R_LARCH_TLS_DESC_CALL:
5570
  /* DESC -> LE/IE relaxation:
5571
     ld.d $ra,$a0,%desc_ld(var) => NOP
5572
     jirl $ra,$ra,%desc_call(var) => NOP  */
5573
0
  rel->r_info = ELF32_R_INFO (0, R_LARCH_NONE);
5574
0
  bfd_put (32, abfd, LARCH_NOP, contents + rel->r_offset);
5575
  /* link with -relax option will delete NOP.  */
5576
0
  if (!info->disable_target_specific_optimizations)
5577
0
    loongarch_relax_delete_or_nop (abfd, sec, rel->r_offset, 4, info);
5578
0
  return true;
5579
5580
0
      case R_LARCH_TLS_IE_PC_HI20:
5581
0
      case R_LARCH_TLS_IE_PCADD_HI20:
5582
0
  if (local_exec)
5583
0
    {
5584
      /* IE -> LE relaxation:
5585
         pcalau12i $rd,%ie_pc_hi20(var) =>
5586
         lu12i.w $rd,%le_hi20(var)  */
5587
0
      insn = bfd_getl32 (contents + rel->r_offset);
5588
0
      bfd_put (32, abfd, LARCH_OP_LU12I_W | LARCH_GET_RD(insn),
5589
0
         contents + rel->r_offset);
5590
0
      rel->r_info = ELF32_R_INFO (r_symndx, R_LARCH_TLS_LE_HI20);
5591
0
    }
5592
0
  return true;
5593
5594
0
      case R_LARCH_TLS_IE_PC_LO12:
5595
0
      case R_LARCH_TLS_IE_PCADD_LO12:
5596
0
  if (local_exec)
5597
0
    {
5598
      /* IE -> LE relaxation:
5599
         ld.d $rd,$rj,%%ie_pc_lo12(var) =>
5600
         ori  $rd,$rj,le_lo12(var)
5601
      */
5602
0
      insn = bfd_getl32 (contents + rel->r_offset);
5603
0
      bfd_put (32, abfd, LARCH_OP_ORI | (insn & 0x3ff),
5604
0
         contents + rel->r_offset);
5605
0
      rel->r_info = ELF32_R_INFO (r_symndx, R_LARCH_TLS_LE_LO12);
5606
0
    }
5607
0
  return true;
5608
0
    }
5609
5610
0
  return false;
5611
0
}
5612
5613
5614
/*  Relax tls le, mainly relax the process of getting TLS le symbolic addresses.
5615
  there are three situations in which an assembly instruction sequence needs to
5616
  be relaxed:
5617
  symbol address = tp + offset (symbol),offset (symbol) = le_hi20_r + le_lo12_r
5618
5619
  Case 1:
5620
  in this case, the rd register in the st.{w/d} instruction does not store the
5621
  full tls symbolic address, but tp + le_hi20_r, which is a part of the tls
5622
  symbolic address, and then obtains the rd + le_lo12_r address through the
5623
  st.w instruction feature.
5624
  this is the full tls symbolic address (tp + le_hi20_r + le_lo12_r).
5625
5626
  before relax:       after relax:
5627
5628
  lu12i.w   $rd,%le_hi20_r (sym)  ==> (instruction deleted)
5629
  add.{w/d} $rd,$rd,$tp,%le_add_r (sym) ==> (instruction deleted)
5630
  st.{w/d}  $rs,$rd,%le_lo12_r (sym)    ==> st.{w/d}   $rs,$tp,%le_lo12_r (sym)
5631
5632
  Case 2:
5633
  in this case, ld.{w/d} is similar to st.{w/d} in case1.
5634
5635
  before relax:       after relax:
5636
5637
  lu12i.w   $rd,%le_hi20_r (sym)  ==> (instruction deleted)
5638
  add.{w/d} $rd,$rd,$tp,%le_add_r (sym) ==> (instruction deleted)
5639
  ld.{w/d}  $rs,$rd,%le_lo12_r (sym)    ==> ld.{w/d}   $rs,$tp,%le_lo12_r (sym)
5640
5641
  Case 3:
5642
  in this case,the rs register in addi.{w/d} stores the full address of the tls
5643
  symbol (tp + le_hi20_r + le_lo12_r).
5644
5645
  before relax:       after relax:
5646
5647
  lu12i.w    $rd,%le_hi20_r (sym)  ==> (instruction deleted)
5648
  add.{w/d}  $rd,$rd,$tp,%le_add_r (sym) ==> (instruction deleted)
5649
  addi.{w/d} $rs,$rd,%le_lo12_r (sym)    ==> addi.{w/d} $rs,$tp,%le_lo12_r (sym)
5650
5651
5652
  For relocation of all old LE instruction sequences, whether it is
5653
  a normal code model or an extreme code model, relaxation will be
5654
  performed when the relaxation conditions are met.
5655
5656
  nomal code model:
5657
  lu12i.w   $rd,%le_hi20(sym)     => (deleted)
5658
  ori     $rd,$rd,le_lo12(sym)    => ori  $rd,$zero,le_lo12(sym)
5659
5660
  extreme code model:
5661
  lu12i.w   $rd,%le_hi20(sym)     => (deleted)
5662
  ori     $rd,$rd,%le_lo12(sym)   => ori  $rd,$zero,le_lo12(sym)
5663
  lu32i.d   $rd,%le64_lo20(sym)     => (deleted)
5664
  lu52i.d   $rd,$rd,%le64_hi12(sym) => (deleted)
5665
*/
5666
static bool
5667
loongarch_relax_tls_le (bfd *abfd, asection *sec, asection *sym_sec,
5668
      Elf_Internal_Rela *rel, bfd_vma symval,
5669
      struct bfd_link_info *link_info,
5670
      bool *agin ATTRIBUTE_UNUSED,
5671
      bfd_vma max_alignment ATTRIBUTE_UNUSED)
5672
0
{
5673
0
  bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
5674
0
  uint32_t insn = bfd_get (32, abfd, contents + rel->r_offset);
5675
0
  static uint32_t insn_rj,insn_rd;
5676
0
  symval = symval - elf_hash_table (link_info)->tls_sec->vma;
5677
0
  if (sym_sec == sec)
5678
0
    symval = loongarch_calc_relaxed_addr (link_info, symval);
5679
  /* The old LE instruction sequence can be relaxed when the symbol offset
5680
     is smaller than the 12-bit range.  */
5681
0
  if (symval <= 0xfff)
5682
0
    {
5683
0
      switch (ELF32_R_TYPE (rel->r_info))
5684
0
  {
5685
    /*if offset < 0x800, then perform the new le instruction
5686
      sequence relax.  */
5687
0
    case R_LARCH_TLS_LE_HI20_R:
5688
0
    case R_LARCH_TLS_LE_ADD_R:
5689
      /* delete insn.  */
5690
0
      if (symval < 0x800)
5691
0
        {
5692
0
    rel->r_info = ELF32_R_INFO (0, R_LARCH_NONE);
5693
0
    loongarch_relax_delete_or_nop (abfd, sec, rel->r_offset,
5694
0
        4, link_info);
5695
0
        }
5696
0
      break;
5697
5698
0
    case R_LARCH_TLS_LE_LO12_R:
5699
0
      if (symval < 0x800)
5700
0
        {
5701
    /* Change rj to $tp.  */
5702
0
    insn_rj = 0x2 << 5;
5703
    /* Get rd register.  */
5704
0
    insn_rd = LARCH_GET_RD (insn);
5705
    /* Write symbol offset.  */
5706
0
    symval <<= 10;
5707
    /* Writes the modified instruction.  */
5708
0
    insn = insn & LARCH_MK_ADDI_D;
5709
0
    insn = insn | symval | insn_rj | insn_rd;
5710
0
    bfd_put (32, abfd, insn, contents + rel->r_offset);
5711
0
        }
5712
0
      break;
5713
5714
0
    case R_LARCH_TLS_LE_HI20:
5715
0
    case R_LARCH_TLS_LE64_LO20:
5716
0
    case R_LARCH_TLS_LE64_HI12:
5717
0
      rel->r_info = ELF32_R_INFO (0, R_LARCH_NONE);
5718
0
      loongarch_relax_delete_or_nop (abfd, sec, rel->r_offset,
5719
0
             4, link_info);
5720
0
      break;
5721
5722
0
    case R_LARCH_TLS_LE_LO12:
5723
0
      bfd_put (32, abfd, LARCH_OP_ORI | LARCH_GET_RD (insn),
5724
0
        contents + rel->r_offset);
5725
0
      break;
5726
5727
0
    default:
5728
0
      break;
5729
0
  }
5730
0
    }
5731
0
  return true;
5732
0
}
5733
5734
/* Whether two sections in the same segment.  */
5735
static bool
5736
loongarch_two_sections_in_same_segment (bfd *abfd, asection *a, asection *b)
5737
0
{
5738
0
  struct elf_segment_map *m;
5739
0
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5740
0
    {
5741
0
      int i;
5742
0
      int j = 0;
5743
0
      for (i = m->count - 1; i >= 0; i--)
5744
0
  {
5745
0
    if (m->sections[i] == a)
5746
0
      j++;
5747
0
    if (m->sections[i] == b)
5748
0
      j++;
5749
0
  }
5750
0
      if (1 == j)
5751
0
  return false;
5752
0
      if (2 == j)
5753
0
  return true;
5754
0
    }
5755
0
  return false;
5756
0
}
5757
5758
/* Relax pcalau12i,addi.d => pcaddi.  */
5759
static bool
5760
loongarch_relax_pcala_addi (bfd *abfd, asection *sec, asection *sym_sec,
5761
          Elf_Internal_Rela *rel_hi, bfd_vma symval,
5762
          struct bfd_link_info *info, bool *again,
5763
          bfd_vma max_alignment)
5764
0
{
5765
0
  bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
5766
0
  Elf_Internal_Rela *rel_lo = rel_hi + 2;
5767
0
  uint32_t pca = bfd_get (32, abfd, contents + rel_hi->r_offset);
5768
0
  uint32_t add = bfd_get (32, abfd, contents + rel_lo->r_offset);
5769
0
  uint32_t rd = LARCH_GET_RD (pca);
5770
5771
0
  bfd_vma pc = sec_addr (sec)
5772
0
         + loongarch_calc_relaxed_addr (info, rel_hi->r_offset);
5773
0
  if (sym_sec == sec)
5774
0
    symval = sec_addr (sec)
5775
0
       + loongarch_calc_relaxed_addr (info, symval - sec_addr (sec));
5776
5777
  /* If pc and symbol not in the same segment, add/sub segment alignment.  */
5778
0
  if (!loongarch_two_sections_in_same_segment (info->output_bfd,
5779
0
                 sec->output_section,
5780
0
                 sym_sec->output_section))
5781
0
    max_alignment = info->maxpagesize > max_alignment ? info->maxpagesize
5782
0
                  : max_alignment;
5783
5784
0
  if (symval > pc)
5785
0
    pc -= (max_alignment > 4 ? max_alignment : 0);
5786
0
  else if (symval < pc)
5787
0
    pc += (max_alignment > 4 ? max_alignment : 0);
5788
5789
0
  const uint32_t pcaddi = LARCH_OP_PCADDI;
5790
5791
  /* Is pcalau12i + addi.d insns?  */
5792
0
  if ((ELF32_R_TYPE (rel_lo->r_info) != R_LARCH_PCALA_LO12)
5793
0
      || !LARCH_INSN_ADDI_D (add)
5794
      /* Is pcalau12i $rd + addi.d $rd,$rd?  */
5795
0
      || (LARCH_GET_RD (add) != rd)
5796
0
      || (LARCH_GET_RJ (add) != rd)
5797
      /* Can be relaxed to pcaddi?  */
5798
0
      || (symval & 0x3) /* 4 bytes align.  */
5799
0
      || ((bfd_signed_vma)(symval - pc) < (bfd_signed_vma)(int32_t)0xffe00000)
5800
0
      || ((bfd_signed_vma)(symval - pc) > (bfd_signed_vma)(int32_t)0x1ffffc))
5801
0
    return false;
5802
5803
  /* Continue next relax trip.  */
5804
0
  *again = true;
5805
5806
0
  pca = pcaddi | rd;
5807
0
  bfd_put (32, abfd, pca, contents + rel_hi->r_offset);
5808
5809
  /* Adjust relocations.  */
5810
0
  rel_hi->r_info = ELF32_R_INFO (ELF32_R_SYM (rel_hi->r_info),
5811
0
         R_LARCH_PCREL20_S2);
5812
0
  rel_lo->r_info = ELF32_R_INFO (0, R_LARCH_NONE);
5813
5814
0
  loongarch_relax_delete_or_nop (abfd, sec, rel_lo->r_offset, 4, info);
5815
5816
0
  return true;
5817
0
}
5818
5819
/* call36 f -> bl f
5820
   tail36 $t0, f -> b f.  */
5821
static bool
5822
loongarch_relax_call36 (bfd *abfd, asection *sec, asection *sym_sec,
5823
          Elf_Internal_Rela *rel, bfd_vma symval,
5824
          struct bfd_link_info *info, bool *again,
5825
          bfd_vma max_alignment)
5826
0
{
5827
0
  bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
5828
0
  uint32_t jirl = bfd_get (32, abfd, contents + rel->r_offset + 4);
5829
0
  uint32_t rd = LARCH_GET_RD (jirl);
5830
5831
0
  bfd_vma pc = sec_addr (sec)
5832
0
         + loongarch_calc_relaxed_addr (info, rel->r_offset);
5833
0
  if (sym_sec == sec)
5834
0
    symval = sec_addr (sec)
5835
0
       + loongarch_calc_relaxed_addr (info, symval - sec_addr (sec));
5836
5837
  /* If pc and symbol not in the same segment, add/sub segment alignment.  */
5838
0
  if (!loongarch_two_sections_in_same_segment (info->output_bfd,
5839
0
                 sec->output_section,
5840
0
                 sym_sec->output_section))
5841
0
    max_alignment = info->maxpagesize > max_alignment ? info->maxpagesize
5842
0
                  : max_alignment;
5843
5844
0
  if (symval > pc)
5845
0
    pc -= (max_alignment > 4 ? max_alignment : 0);
5846
0
  else if (symval < pc)
5847
0
    pc += (max_alignment > 4 ? max_alignment : 0);
5848
5849
  /* Is pcalau12i + addi.d insns?  */
5850
0
  if (!LARCH_INSN_JIRL (jirl)
5851
0
      || ((bfd_signed_vma)(symval - pc) < (bfd_signed_vma)(int32_t)0xf8000000)
5852
0
      || ((bfd_signed_vma)(symval - pc) > (bfd_signed_vma)(int32_t)0x7fffffc))
5853
0
    return false;
5854
5855
  /* Continue next relax trip.  */
5856
0
  *again = true;
5857
5858
0
  const uint32_t bl = LARCH_OP_BL;
5859
0
  const uint32_t b = LARCH_OP_B;
5860
5861
0
  if (rd)
5862
0
    bfd_put (32, abfd, bl, contents + rel->r_offset);
5863
0
  else
5864
0
    bfd_put (32, abfd, b, contents + rel->r_offset);
5865
5866
  /* Adjust relocations.  */
5867
0
  rel->r_info = ELF32_R_INFO (ELF32_R_SYM (rel->r_info), R_LARCH_B26);
5868
  /* Delete jirl instruction.  */
5869
0
  loongarch_relax_delete_or_nop (abfd, sec, rel->r_offset + 4, 4, info);
5870
0
  return true;
5871
0
}
5872
5873
/* pcalau12i $t0, %got_pcala_hi20(a)   -> pcalau12i $t0, %pcala_hi20(a)
5874
   ld.w/d $t0, $t0, %got_pcala_lo12(a) -> addi.w/d $t0, $t0, %pcala_lo12(a)
5875
5876
   pcaddu12i $t0, %got_pcadd_hi20(a)   -> pcaddu12i $t0, %pcadd_hi20(a)
5877
   ld.w/d $t0, $t0, %got_pcadd_lo12(a) -> addi.w/d $t0, $t0, %pcadd_lo12(a)  */
5878
static bool
5879
loongarch_relax_pcala_ld (bfd *abfd, asection *sec,
5880
        asection *sym_sec,
5881
        Elf_Internal_Rela *rel_hi,
5882
        bfd_vma symval,
5883
        struct bfd_link_info *info,
5884
        bool *again ATTRIBUTE_UNUSED,
5885
        bfd_vma max_alignment)
5886
0
{
5887
0
  bfd_vma pc = sec_addr (sec)
5888
0
         + loongarch_calc_relaxed_addr (info, rel_hi->r_offset);
5889
0
  if (sym_sec == sec)
5890
0
    symval = sec_addr (sec)
5891
0
       + loongarch_calc_relaxed_addr (info, symval - sec_addr (sec));
5892
5893
  /* If pc and symbol not in the same segment, add/sub segment alignment.  */
5894
0
  if (!loongarch_two_sections_in_same_segment (info->output_bfd,
5895
0
                 sec->output_section,
5896
0
                 sym_sec->output_section))
5897
0
    max_alignment = info->maxpagesize > max_alignment ? info->maxpagesize
5898
0
                  : max_alignment;
5899
5900
0
  if (symval > pc)
5901
0
    pc -= (max_alignment > 4 ? max_alignment : 0);
5902
0
  else if (symval < pc)
5903
0
    pc += (max_alignment > 4 ? max_alignment : 0);
5904
5905
0
  bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
5906
0
  Elf_Internal_Rela *rel_lo = rel_hi + 2;
5907
0
  uint32_t pca = bfd_get (32, abfd, contents + rel_hi->r_offset);
5908
0
  uint32_t ld = bfd_get (32, abfd, contents + rel_lo->r_offset);
5909
0
  uint32_t rd = LARCH_GET_RD (pca);
5910
5911
0
  if ((ELF32_R_TYPE (rel_lo->r_info) != R_LARCH_GOT_PC_LO12
5912
0
  && ELF32_R_TYPE (rel_lo->r_info) != R_LARCH_GOT_PCADD_LO12)
5913
0
      || (LARCH_GET_RD (ld) != rd)
5914
0
      || (LARCH_GET_RJ (ld) != rd)
5915
0
      || (!LARCH_INSN_LD_D (ld) && !LARCH_INSN_LD_W (ld))
5916
      /* Within +-2G addressing range.  */
5917
0
      || (bfd_signed_vma)(symval - pc) < (bfd_signed_vma)(int32_t)0x80000000
5918
0
      || (bfd_signed_vma)(symval - pc) > (bfd_signed_vma)(int32_t)0x7fffffff)
5919
0
    return false;
5920
5921
0
  uint32_t addi;
5922
0
  if (LARCH_INSN_LD_D (ld))
5923
0
    addi = LARCH_OP_ADDI_D;
5924
0
  else
5925
0
    addi = LARCH_OP_ADDI_W;
5926
5927
0
  addi = addi | (rd << 5) | rd;
5928
0
  bfd_put (32, abfd, addi, contents + rel_lo->r_offset);
5929
5930
0
  if (ELF32_R_TYPE (rel_hi->r_info) == R_LARCH_GOT_PC_HI20)
5931
0
    {
5932
0
      rel_hi->r_info = ELF32_R_INFO (ELF32_R_SYM (rel_hi->r_info),
5933
0
             R_LARCH_PCALA_HI20);
5934
0
      rel_lo->r_info = ELF32_R_INFO (ELF32_R_SYM (rel_lo->r_info),
5935
0
             R_LARCH_PCALA_LO12);
5936
0
    }
5937
0
  else
5938
0
    {
5939
0
      rel_hi->r_info = ELF32_R_INFO (ELF32_R_SYM (rel_hi->r_info),
5940
0
             R_LARCH_PCADD_HI20);
5941
0
      rel_lo->r_info = ELF32_R_INFO (ELF32_R_SYM (rel_lo->r_info),
5942
0
             R_LARCH_PCADD_LO12);
5943
0
    }
5944
5945
0
  return true;
5946
0
}
5947
5948
/* Called by after_allocation to set the information of data segment
5949
   before relaxing.  */
5950
5951
void
5952
bfd_elf32_loongarch_set_data_segment_info (struct bfd_link_info *info,
5953
             int *data_segment_phase)
5954
0
{
5955
0
  if (is_elf_hash_table (info->hash)
5956
0
      && elf_hash_table_id (elf_hash_table (info)) == LARCH_ELF_DATA)
5957
0
    loongarch_elf_hash_table (info)->data_segment_phase = data_segment_phase;
5958
0
}
5959
5960
/* Honor R_LARCH_ALIGN requests by deleting excess alignment NOPs.
5961
   Once we've handled an R_LARCH_ALIGN, we can't relax anything else by deleting
5962
   bytes, or alignment will be disrupted.  */
5963
static bool
5964
loongarch_relax_align (bfd *abfd, asection *sec, asection *sym_sec,
5965
      Elf_Internal_Rela *rel,
5966
      bfd_vma symval,
5967
      struct bfd_link_info *link_info,
5968
      bool *again ATTRIBUTE_UNUSED,
5969
      bfd_vma max_alignment ATTRIBUTE_UNUSED)
5970
0
{
5971
0
  bfd_vma  addend, max = 0, alignment = 1;
5972
5973
0
  int sym_index = ELF32_R_SYM (rel->r_info);
5974
0
  if (sym_index > 0)
5975
0
    {
5976
0
      alignment = 1 << (rel->r_addend & 0xff);
5977
0
      max = rel->r_addend >> 8;
5978
0
    }
5979
0
  else
5980
0
    alignment = rel->r_addend + 4;
5981
5982
0
  if (sym_sec == sec)
5983
0
    symval = sec_addr (sec)
5984
0
       + loongarch_calc_relaxed_addr (link_info, symval - sec_addr (sec));
5985
5986
0
  addend = alignment - 4; /* The bytes of NOPs added by R_LARCH_ALIGN.  */
5987
0
  symval -= addend; /* The address of first NOP added by R_LARCH_ALIGN.  */
5988
0
  bfd_vma aligned_addr = ((symval - 1) & ~(alignment - 1)) + alignment;
5989
0
  bfd_vma need_nop_bytes = aligned_addr - symval; /* */
5990
5991
  /* Make sure there are enough NOPs to actually achieve the alignment.  */
5992
0
  if (addend < need_nop_bytes)
5993
0
    {
5994
0
      _bfd_error_handler
5995
0
  (_("%pB(%pA+%#" PRIx64 "): %" PRId64 " bytes required for alignment "
5996
0
     "to %" PRId64 "-byte boundary, but only %" PRId64 " present"),
5997
0
   abfd, sym_sec, (uint64_t) rel->r_offset,
5998
0
   (int64_t) need_nop_bytes, (int64_t) alignment, (int64_t) addend);
5999
0
      bfd_set_error (bfd_error_bad_value);
6000
0
      return false;
6001
0
    }
6002
6003
  /* Once we've handled an R_LARCH_ALIGN in a section, we can't relax anything
6004
     else by deleting bytes, or alignment will be disrupted.  */
6005
0
  loongarch_sec_closed_for_deletion (sec) = true;
6006
0
  rel->r_info = ELF32_R_INFO (0, R_LARCH_NONE);
6007
6008
  /* If skipping more bytes than the specified maximum,
6009
     then the alignment is not done at all and delete all NOPs.  */
6010
0
  if (max > 0 && need_nop_bytes > max)
6011
0
    {
6012
0
      loongarch_relax_delete_bytes (abfd, rel->r_offset, addend, link_info);
6013
0
      return true;
6014
0
    }
6015
6016
  /* If the number of NOPs is already correct, there's nothing to do.  */
6017
0
  if (need_nop_bytes == addend)
6018
0
    return true;
6019
6020
  /* Delete the excess NOPs.  */
6021
0
  loongarch_relax_delete_bytes (abfd, rel->r_offset + need_nop_bytes,
6022
0
        addend - need_nop_bytes, link_info);
6023
0
  return true;
6024
0
}
6025
6026
/* Relax pcalau12i + addi.d of TLS LD/GD/DESC to pcaddi.  */
6027
static bool
6028
loongarch_relax_tls_ld_gd_desc (bfd *abfd, asection *sec, asection *sym_sec,
6029
        Elf_Internal_Rela *rel_hi, bfd_vma symval,
6030
        struct bfd_link_info *info, bool *again,
6031
        bfd_vma max_alignment)
6032
0
{
6033
0
  bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
6034
0
  Elf_Internal_Rela *rel_lo = rel_hi + 2;
6035
0
  uint32_t pca = bfd_get (32, abfd, contents + rel_hi->r_offset);
6036
0
  uint32_t add = bfd_get (32, abfd, contents + rel_lo->r_offset);
6037
0
  uint32_t rd = LARCH_GET_RD (pca);
6038
6039
0
  bfd_vma pc = sec_addr (sec)
6040
0
         + loongarch_calc_relaxed_addr (info, rel_hi->r_offset);
6041
0
  if (sym_sec == sec)
6042
0
    symval = sec_addr (sec)
6043
0
       + loongarch_calc_relaxed_addr (info, symval - sec_addr (sec));
6044
6045
  /* If pc and symbol not in the same segment, add/sub segment alignment.  */
6046
0
  if (!loongarch_two_sections_in_same_segment (info->output_bfd,
6047
0
                 sec->output_section,
6048
0
                 sym_sec->output_section))
6049
0
    max_alignment = info->maxpagesize > max_alignment ? info->maxpagesize
6050
0
                  : max_alignment;
6051
6052
0
  if (symval > pc)
6053
0
    pc -= (max_alignment > 4 ? max_alignment : 0);
6054
0
  else if (symval < pc)
6055
0
    pc += (max_alignment > 4 ? max_alignment : 0);
6056
6057
0
  const uint32_t pcaddi = LARCH_OP_PCADDI;
6058
6059
  /* Is pcalau12i + addi.d insns?  */
6060
0
  if ((ELF32_R_TYPE (rel_lo->r_info) != R_LARCH_GOT_PC_LO12
6061
0
  && ELF32_R_TYPE (rel_lo->r_info) != R_LARCH_TLS_DESC_PC_LO12)
6062
0
      || !LARCH_INSN_ADDI_D (add)
6063
      /* Is pcalau12i $rd + addi.d $rd,$rd?  */
6064
0
      || (LARCH_GET_RD (add) != rd)
6065
0
      || (LARCH_GET_RJ (add) != rd)
6066
      /* Can be relaxed to pcaddi?  */
6067
0
      || (symval & 0x3) /* 4 bytes align.  */
6068
0
      || ((bfd_signed_vma)(symval - pc) < (bfd_signed_vma)(int32_t)0xffe00000)
6069
0
      || ((bfd_signed_vma)(symval - pc) > (bfd_signed_vma)(int32_t)0x1ffffc))
6070
0
    return false;
6071
6072
  /* Continue next relax trip.  */
6073
0
  *again = true;
6074
6075
0
  pca = pcaddi | rd;
6076
0
  bfd_put (32, abfd, pca, contents + rel_hi->r_offset);
6077
6078
  /* Adjust relocations.  */
6079
0
  switch (ELF32_R_TYPE (rel_hi->r_info))
6080
0
    {
6081
0
    case R_LARCH_TLS_LD_PC_HI20:
6082
0
      rel_hi->r_info = ELF32_R_INFO (ELF32_R_SYM (rel_hi->r_info),
6083
0
              R_LARCH_TLS_LD_PCREL20_S2);
6084
0
      break;
6085
0
    case R_LARCH_TLS_GD_PC_HI20:
6086
0
      rel_hi->r_info = ELF32_R_INFO (ELF32_R_SYM (rel_hi->r_info),
6087
0
              R_LARCH_TLS_GD_PCREL20_S2);
6088
0
      break;
6089
0
    case R_LARCH_TLS_DESC_PC_HI20:
6090
0
      rel_hi->r_info = ELF32_R_INFO (ELF32_R_SYM (rel_hi->r_info),
6091
0
              R_LARCH_TLS_DESC_PCREL20_S2);
6092
0
      break;
6093
0
    default:
6094
0
      break;
6095
0
    }
6096
0
  rel_lo->r_info = ELF32_R_INFO (0, R_LARCH_NONE);
6097
6098
0
  loongarch_relax_delete_or_nop (abfd, sec, rel_lo->r_offset, 4, info);
6099
6100
0
  return true;
6101
0
}
6102
6103
/* Traverse all output sections and return the max alignment.  */
6104
6105
static bfd_vma
6106
loongarch_get_max_alignment (asection *sec)
6107
0
{
6108
0
  asection *o;
6109
0
  unsigned int max_alignment_power = 0;
6110
6111
0
  for (o = sec->output_section->owner->sections; o != NULL; o = o->next)
6112
0
      if (o->alignment_power > max_alignment_power)
6113
0
  max_alignment_power = o->alignment_power;
6114
6115
0
  return (bfd_vma) 1 << max_alignment_power;
6116
0
}
6117
6118
typedef bool (*relax_func_t) (bfd *, asection *, asection *,
6119
            Elf_Internal_Rela *, bfd_vma,
6120
            struct bfd_link_info *, bool *,
6121
             bfd_vma);
6122
6123
static bool
6124
loongarch_elf_relax_section (bfd *abfd, asection *sec,
6125
           struct bfd_link_info *info,
6126
           bool *again)
6127
0
{
6128
0
  *again = false;
6129
6130
0
  if (!is_elf_hash_table (info->hash)
6131
0
      || elf_hash_table_id (elf_hash_table (info)) != LARCH_ELF_DATA)
6132
0
    return true;
6133
6134
0
  struct loongarch_elf_link_hash_table *htab = loongarch_elf_hash_table (info);
6135
6136
  /* It may happen that some sections have updated vma but the others do
6137
     not.  Go to the next relax trip (size_relative_relocs should have
6138
     been demending another relax trip anyway).  */
6139
0
  if (htab->layout_mutating_for_relr)
6140
0
    return true;
6141
6142
  /* Definition of LoongArch linker relaxation passes:
6143
6144
     - Pass 0: relaxes everything except R_LARCH_ALIGN, byte deletions are
6145
         performed; skipped if disable_target_specific_optimizations.
6146
     - Pass 1: handles alignment, byte deletions are performed.  Sections with
6147
         R_LARCH_ALIGN relocations are marked closed for further byte
6148
         deletion in order to not disturb alignment.  This pass is NOT
6149
         skipped even if disable_target_specific_optimizations is true.
6150
     - Pass 2: identical to Pass 0, but replacing relaxed insns with NOP in case
6151
         the containing section is closed for deletion; skip condition
6152
         also same as Pass 0.  */
6153
0
  bool is_alignment_pass = info->relax_pass == 1;
6154
0
  if (bfd_link_relocatable (info)
6155
0
      || sec->reloc_count == 0
6156
0
      || (sec->flags & SEC_RELOC) == 0
6157
0
      || (sec->flags & SEC_HAS_CONTENTS) == 0
6158
      /* The exp_seg_relro_adjust is enum phase_enum (0x4).  */
6159
0
      || *(htab->data_segment_phase) == 4
6160
0
      || (info->disable_target_specific_optimizations && !is_alignment_pass))
6161
0
    return true;
6162
6163
0
  struct bfd_elf_section_data *data = elf_section_data (sec);
6164
0
  Elf_Internal_Rela *relocs;
6165
0
  if (data->relocs)
6166
0
    relocs = data->relocs;
6167
0
  else if (!(relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
6168
0
             info->keep_memory)))
6169
0
    return true;
6170
0
  data->relocs = relocs;
6171
6172
  /* Read this BFD's contents if we haven't done so already.  */
6173
0
  if (!data->this_hdr.contents
6174
0
      && !bfd_malloc_and_get_section (abfd, sec, &data->this_hdr.contents))
6175
0
    return true;
6176
6177
  /* Read this BFD's symbols if we haven't done so already.  */
6178
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
6179
0
  if (symtab_hdr->sh_info != 0
6180
0
      && !symtab_hdr->contents
6181
0
      && !(symtab_hdr->contents =
6182
0
     (unsigned char *) bfd_elf_get_elf_syms (abfd, symtab_hdr,
6183
0
               symtab_hdr->sh_info,
6184
0
               0, NULL, NULL, NULL)))
6185
0
    return true;
6186
6187
6188
0
  loongarch_pcrel_relocs pcrel_relocs;
6189
0
  if (!loongarch_init_pcrel_relocs (&pcrel_relocs))
6190
0
    return false;
6191
6192
  /* Estimate the maximum alignment for all output sections once time
6193
     should be enough.  */
6194
0
  bfd_vma max_alignment = htab->max_alignment;
6195
0
  if (max_alignment == (bfd_vma) -1)
6196
0
    {
6197
0
      max_alignment = loongarch_get_max_alignment (sec);
6198
0
      htab->max_alignment = max_alignment;
6199
0
    }
6200
6201
0
  splay_tree pdops = NULL;
6202
0
  if (!loongarch_sec_closed_for_deletion (sec))
6203
0
    pdops = pending_delete_ops_new (abfd);
6204
6205
0
  htab->pending_delete_ops = pdops;
6206
6207
  /* The section's output_offset need to subtract the bytes of instructions
6208
     relaxed by the previous sections, so it needs to be updated beforehand.
6209
     size_input_section already took care of updating it after relaxation,
6210
     so we additionally update once here.  */
6211
6212
  /* update before tls trans and relax, or may cause same pcadd_hi20 address.  */
6213
6214
0
  sec->output_offset = align_power (sec->output_section->size,
6215
0
            sec->alignment_power);
6216
6217
0
  for (unsigned int i = 0; i < sec->reloc_count; i++)
6218
0
    {
6219
0
      char symtype;
6220
0
      asection *sym_sec;
6221
0
      bfd_vma symval = 0;
6222
0
      bool local_got = false;
6223
0
      Elf_Internal_Rela *rel = relocs + i;
6224
0
      struct elf_link_hash_entry *h = NULL;
6225
0
      unsigned long r_type = ELF32_R_TYPE (rel->r_info);
6226
0
      unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
6227
6228
0
      if (r_symndx >= symtab_hdr->sh_info)
6229
0
  {
6230
0
    h = elf_sym_hashes (abfd)[r_symndx - symtab_hdr->sh_info];
6231
0
    while (h->root.type == bfd_link_hash_indirect
6232
0
         || h->root.type == bfd_link_hash_warning)
6233
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
6234
0
  }
6235
6236
      /* Get the local symbol value of PCADD_LO12 to find PCADD_HI20.  */
6237
0
      if (r_type == R_LARCH_TLS_IE_PCADD_LO12
6238
0
    || r_type == R_LARCH_TLS_DESC_PCADD_LO12)
6239
0
  {
6240
0
    BFD_ASSERT (r_symndx <= symtab_hdr->sh_info);
6241
0
    Elf_Internal_Sym *sym = (Elf_Internal_Sym *)symtab_hdr->contents
6242
0
            + r_symndx;
6243
0
    sym_sec = elf_elfsections (abfd)[sym->st_shndx]->bfd_section;
6244
0
    symval = sym->st_value;
6245
0
    symval += sec_addr (sym_sec);
6246
0
  }
6247
6248
      /* If the conditions for tls type transition are met, type
6249
   transition is performed instead of relax.
6250
   During the transition from DESC->IE/LE, there are 2 situations
6251
   depending on the different configurations of the relax/norelax
6252
   option.
6253
   TLS type transition always perform.
6254
   If the -relax option is used, the extra nops will be removed,
6255
   and this transition is performed in pass 0.
6256
   If the --no-relax option is used, nop will be retained, and
6257
   this transition is performed in pass 1.  */
6258
0
      if (IS_LOONGARCH_TLS_TRANS_RELOC (r_type)
6259
0
    && (i + 1 != sec->reloc_count)
6260
0
    && ELF32_R_TYPE (rel[1].r_info) == R_LARCH_RELAX
6261
0
    && rel->r_offset == rel[1].r_offset
6262
0
    && loongarch_can_trans_tls (abfd, sec, rel, info, h, symval,
6263
0
              &pcrel_relocs))
6264
0
  {
6265
0
    loongarch_tls_perform_trans (abfd, sec, rel, h, info, symval,
6266
0
               &pcrel_relocs);
6267
0
    r_type = ELF32_R_TYPE (rel->r_info);
6268
0
    continue;
6269
0
  }
6270
6271
0
      relax_func_t relax_func = NULL;
6272
6273
0
      if (is_alignment_pass)
6274
0
  {
6275
0
    if (r_type != R_LARCH_ALIGN)
6276
0
      continue;
6277
0
    relax_func = loongarch_relax_align;
6278
0
  }
6279
0
      else
6280
0
  {
6281
0
    switch (r_type)
6282
0
      {
6283
0
      case R_LARCH_PCALA_HI20:
6284
0
        relax_func = loongarch_relax_pcala_addi;
6285
0
        break;
6286
0
      case R_LARCH_GOT_PC_HI20:
6287
0
      case R_LARCH_GOT_PCADD_HI20:
6288
0
        relax_func = loongarch_relax_pcala_ld;
6289
0
        break;
6290
0
      case R_LARCH_CALL36:
6291
0
      case R_LARCH_CALL30:
6292
0
        relax_func = loongarch_relax_call36;
6293
0
        break;
6294
0
      case R_LARCH_TLS_LE_HI20_R:
6295
0
      case R_LARCH_TLS_LE_LO12_R:
6296
0
      case R_LARCH_TLS_LE_ADD_R:
6297
0
      case R_LARCH_TLS_LE_HI20:
6298
0
      case R_LARCH_TLS_LE_LO12:
6299
0
      case R_LARCH_TLS_LE64_LO20:
6300
0
      case R_LARCH_TLS_LE64_HI12:
6301
0
        relax_func = loongarch_relax_tls_le;
6302
0
        break;
6303
0
      case R_LARCH_TLS_LD_PC_HI20:
6304
0
      case R_LARCH_TLS_GD_PC_HI20:
6305
0
      case R_LARCH_TLS_DESC_PC_HI20:
6306
0
        relax_func = loongarch_relax_tls_ld_gd_desc;
6307
0
        break;
6308
0
      default:
6309
0
    continue;
6310
0
      }
6311
6312
    /* Only relax this reloc if it is paired with R_RISCV_RELAX.  */
6313
0
    if (r_type == R_LARCH_TLS_LD_PC_HI20
6314
0
        || r_type == R_LARCH_TLS_GD_PC_HI20
6315
0
        || r_type == R_LARCH_TLS_DESC_PC_HI20
6316
0
        || r_type == R_LARCH_PCALA_HI20
6317
0
        || r_type == R_LARCH_GOT_PC_HI20
6318
0
        || r_type == R_LARCH_GOT_PCADD_HI20)
6319
0
      {
6320
0
        if ((i + 2) == sec->reloc_count - 1
6321
0
      || ELF32_R_TYPE ((rel + 1)->r_info) != R_LARCH_RELAX
6322
0
      || ELF32_R_TYPE ((rel + 3)->r_info) != R_LARCH_RELAX
6323
0
      || rel->r_offset != (rel + 1)->r_offset
6324
0
      || (rel + 2)->r_offset != (rel + 3)->r_offset
6325
0
      || rel->r_offset + 4 != (rel + 2)->r_offset)
6326
0
    continue;
6327
0
      }
6328
0
    else
6329
0
      {
6330
0
        if (i == sec->reloc_count - 1
6331
0
      || ELF32_R_TYPE ((rel + 1)->r_info) != R_LARCH_RELAX
6332
0
      || rel->r_offset != (rel + 1)->r_offset)
6333
0
    continue;
6334
0
      }
6335
0
  }
6336
6337
      /* Four kind of relocations:
6338
   Normal: symval is the symbol address.
6339
   R_LARCH_ALIGN: symval is the address of the last NOP instruction
6340
   added by this relocation, and then adds 4 more.
6341
   R_LARCH_CALL36: symval is the symbol address for local symbols,
6342
   or the PLT entry address of the symbol. (Todo)
6343
   R_LARCHL_TLS_LD/GD/DESC_PC_HI20: symval is the GOT entry address
6344
   of the symbol if transition is not possible.  */
6345
0
      if (r_symndx < symtab_hdr->sh_info)
6346
0
  {
6347
0
    Elf_Internal_Sym *sym = (Elf_Internal_Sym *)symtab_hdr->contents
6348
0
            + r_symndx;
6349
6350
0
    if ((ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
6351
0
         && (r_type != R_LARCH_CALL36 && r_type != R_LARCH_CALL30))
6352
0
        || sym->st_shndx == SHN_ABS)
6353
0
      continue;
6354
6355
    /* Only TLS instruction sequences that are accompanied by
6356
       R_LARCH_RELAX and cannot perform type transition can be
6357
       relaxed.  */
6358
0
    if (r_type == R_LARCH_TLS_LD_PC_HI20
6359
0
        || r_type == R_LARCH_TLS_GD_PC_HI20
6360
0
        || r_type == R_LARCH_TLS_DESC_PC_HI20)
6361
0
      {
6362
0
        sym_sec = htab->elf.sgot;
6363
0
        symval = elf_local_got_offsets (abfd)[r_symndx];
6364
0
        char tls_type = _bfd_loongarch_elf_tls_type (abfd, h,
6365
0
                  r_symndx);
6366
0
        if (r_type == R_LARCH_TLS_DESC_PC_HI20
6367
0
        && GOT_TLS_GD_BOTH_P (tls_type))
6368
0
    symval += 2 * GOT_ENTRY_SIZE;
6369
0
      }
6370
0
    else if (sym->st_shndx == SHN_UNDEF || r_type == R_LARCH_ALIGN)
6371
0
      {
6372
0
        sym_sec = sec;
6373
0
        symval = rel->r_offset;
6374
0
      }
6375
0
    else
6376
0
      {
6377
0
        sym_sec = elf_elfsections (abfd)[sym->st_shndx]->bfd_section;
6378
0
        symval = sym->st_value;
6379
0
      }
6380
0
    symtype = ELF_ST_TYPE (sym->st_info);
6381
0
  }
6382
0
      else
6383
0
  {
6384
    /* Do not relax __[start|stop]_SECNAME, since the symbol value
6385
       is not set yet.  */
6386
0
    if (h != NULL
6387
0
        && ((h->type == STT_GNU_IFUNC
6388
0
       && (r_type != R_LARCH_CALL36 && r_type != R_LARCH_CALL30))
6389
0
      || bfd_is_abs_section (h->root.u.def.section)
6390
0
      || h->start_stop))
6391
0
      continue;
6392
6393
    /* The GOT entry of tls symbols must in current execute file or
6394
       shared object.  */
6395
0
    if (r_type == R_LARCH_TLS_LD_PC_HI20
6396
0
        || r_type == R_LARCH_TLS_GD_PC_HI20
6397
0
        || r_type == R_LARCH_TLS_DESC_PC_HI20)
6398
0
      {
6399
0
        sym_sec = htab->elf.sgot;
6400
0
        symval = h->got.offset;
6401
0
        char tls_type = _bfd_loongarch_elf_tls_type (abfd, h,
6402
0
                  r_symndx);
6403
0
        if (r_type == R_LARCH_TLS_DESC_PC_HI20
6404
0
        && GOT_TLS_GD_BOTH_P (tls_type))
6405
0
    symval += 2 * GOT_ENTRY_SIZE;
6406
0
      }
6407
0
    else if (h->plt.offset != MINUS_ONE)
6408
0
      {
6409
0
        sym_sec = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
6410
0
        symval = h->plt.offset;
6411
0
      }
6412
    /* Like loongarch_elf_relocate_section, set relocation(offset) to 0.
6413
       Undefweak for other relocations handing in the future.  */
6414
0
    else if (h->root.type == bfd_link_hash_undefweak
6415
0
        && !h->root.linker_def
6416
0
        && (r_type == R_LARCH_CALL36 || r_type == R_LARCH_CALL30))
6417
0
      {
6418
0
        sym_sec = sec;
6419
0
        symval = rel->r_offset;
6420
0
      }
6421
0
    else if ((h->root.type == bfd_link_hash_defined
6422
0
      || h->root.type == bfd_link_hash_defweak)
6423
0
    && h->root.u.def.section != NULL
6424
0
    && h->root.u.def.section->output_section != NULL)
6425
0
      {
6426
0
        sym_sec = h->root.u.def.section;
6427
0
        symval = h->root.u.def.value;
6428
0
      }
6429
0
    else
6430
0
      continue;
6431
6432
0
    if (h && LARCH_REF_LOCAL (info, h))
6433
0
      local_got = true;
6434
0
    symtype = h->type;
6435
0
  }
6436
6437
0
      if (sym_sec->sec_info_type == SEC_INFO_TYPE_MERGE
6438
0
     && (sym_sec->flags & SEC_MERGE))
6439
0
  {
6440
0
     if (symtype == STT_SECTION)
6441
0
       symval += rel->r_addend;
6442
6443
0
     symval = _bfd_merged_section_offset (abfd, &sym_sec, symval);
6444
6445
0
     if (symtype != STT_SECTION)
6446
0
       symval += rel->r_addend;
6447
0
  }
6448
      /* For R_LARCH_ALIGN, symval is sec_addr (sec) + rel->r_offset
6449
   + (alingmeng - 4).
6450
   If r_symndx is 0, alignmeng-4 is r_addend.
6451
   If r_symndx > 0, alignment-4 is 2^(r_addend & 0xff)-4.  */
6452
0
      else if (r_type == R_LARCH_ALIGN)
6453
0
  if (r_symndx > 0)
6454
0
    symval += ((1 << (rel->r_addend & 0xff)) - 4);
6455
0
  else
6456
0
    symval += rel->r_addend;
6457
0
      else
6458
0
  symval += rel->r_addend;
6459
6460
0
      symval += sec_addr (sym_sec);
6461
6462
0
      if ((r_type == R_LARCH_GOT_PC_HI20
6463
0
      || r_type == R_LARCH_GOT_PCADD_HI20)
6464
0
    && !local_got)
6465
0
  continue;
6466
6467
0
      if (relax_func (abfd, sec, sym_sec, rel, symval,
6468
0
          info, again, max_alignment)
6469
0
    && relax_func == loongarch_relax_pcala_ld)
6470
0
  loongarch_relax_pcala_addi (abfd, sec, sym_sec, rel, symval,
6471
0
            info, again, max_alignment);
6472
0
    }
6473
6474
0
  loongarch_free_pcrel_reloc (&pcrel_relocs);
6475
6476
0
  if (pdops)
6477
0
    {
6478
0
      bool ok = loongarch_relax_perform_deletes (abfd, sec, info);
6479
0
      htab->pending_delete_ops = NULL;
6480
0
      splay_tree_delete (pdops);
6481
0
      if (!ok)
6482
0
  return false;
6483
0
    }
6484
6485
0
  return true;
6486
0
}
6487
6488
/* Finish up dynamic symbol handling.  We set the contents of various
6489
   dynamic sections here.  */
6490
6491
static bool
6492
loongarch_elf_finish_dynamic_symbol (struct bfd_link_info *info,
6493
             struct elf_link_hash_entry *h,
6494
             Elf_Internal_Sym *sym)
6495
0
{
6496
0
  struct loongarch_elf_link_hash_table *htab = loongarch_elf_hash_table (info);
6497
0
  elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
6498
6499
0
  if (h->plt.offset != MINUS_ONE)
6500
0
    {
6501
0
      size_t i, plt_idx;
6502
0
      asection *plt, *gotplt, *relplt;
6503
0
      bfd_vma got_address;
6504
0
      uint32_t plt_entry[PLT_ENTRY_INSNS];
6505
0
      bfd_byte *loc;
6506
0
      Elf_Internal_Rela rela;
6507
6508
0
      if (htab->elf.splt)
6509
0
  {
6510
0
    BFD_ASSERT ((h->type == STT_GNU_IFUNC
6511
0
           && LARCH_REF_LOCAL (info, h))
6512
0
          || h->dynindx != -1);
6513
6514
0
    plt = htab->elf.splt;
6515
0
    gotplt = htab->elf.sgotplt;
6516
0
    if (h->type == STT_GNU_IFUNC && LARCH_REF_LOCAL (info, h))
6517
0
      relplt = htab->elf.srelgot;
6518
0
    else
6519
0
      relplt = htab->elf.srelplt;
6520
0
    plt_idx = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
6521
0
    got_address =
6522
0
      sec_addr (gotplt) + GOTPLT_HEADER_SIZE + plt_idx * GOT_ENTRY_SIZE;
6523
0
  }
6524
0
      else /* if (htab->elf.iplt) */
6525
0
  {
6526
0
    BFD_ASSERT (h->type == STT_GNU_IFUNC
6527
0
          && LARCH_REF_LOCAL (info, h));
6528
6529
0
    plt = htab->elf.iplt;
6530
0
    gotplt = htab->elf.igotplt;
6531
0
    relplt = htab->elf.irelplt;
6532
0
    plt_idx = h->plt.offset / PLT_ENTRY_SIZE;
6533
0
    got_address = sec_addr (gotplt) + plt_idx * GOT_ENTRY_SIZE;
6534
0
  }
6535
6536
      /* Find out where the .plt entry should go.  */
6537
0
      loc = plt->contents + h->plt.offset;
6538
6539
      /* Fill in the PLT entry itself.  */
6540
0
      if (!loongarch_make_plt_entry (got_address,
6541
0
             sec_addr (plt) + h->plt.offset,
6542
0
             plt_entry))
6543
0
  return false;
6544
6545
0
      for (i = 0; i < PLT_ENTRY_INSNS; i++)
6546
0
  bfd_put_32 (info->output_bfd, plt_entry[i], loc + 4 * i);
6547
6548
      /* Fill in the initial value of the got.plt entry.  */
6549
0
      loc = gotplt->contents + (got_address - sec_addr (gotplt));
6550
0
      bfd_put_32 (info->output_bfd, sec_addr (plt), loc);
6551
6552
0
      rela.r_offset = got_address;
6553
6554
      /* TRUE if this is a PLT reference to a local IFUNC.  */
6555
0
      if (PLT_LOCAL_IFUNC_P (info, h)
6556
0
    && (relplt == htab->elf.srelgot
6557
0
        || relplt == htab->elf.irelplt))
6558
0
  {
6559
0
    rela.r_info = ELF32_R_INFO (0, R_LARCH_IRELATIVE);
6560
0
    rela.r_addend = (h->root.u.def.value
6561
0
             + h->root.u.def.section->output_section->vma
6562
0
             + h->root.u.def.section->output_offset);
6563
6564
0
    loongarch_elf_append_rela (info->output_bfd, relplt, &rela);
6565
0
  }
6566
0
      else
6567
0
  {
6568
    /* Fill in the entry in the rela.plt section.  */
6569
0
    rela.r_info = ELF32_R_INFO (h->dynindx, R_LARCH_JUMP_SLOT);
6570
0
    rela.r_addend = 0;
6571
0
    loc = relplt->contents + plt_idx * sizeof (Elf32_External_Rela);
6572
0
    bed->s->swap_reloca_out (info->output_bfd, &rela, loc);
6573
0
  }
6574
6575
0
      if (!h->def_regular)
6576
0
  {
6577
    /* Mark the symbol as undefined, rather than as defined in
6578
       the .plt section.  */
6579
0
    sym->st_shndx = SHN_UNDEF;
6580
    /* If the symbol is weak, we do need to clear the value.
6581
       Otherwise, the PLT entry would provide a definition for
6582
       the symbol even if the symbol wasn't defined anywhere,
6583
       and so the symbol would never be NULL.  Leave the value if
6584
       there were any relocations where pointer equality matters
6585
       (this is a clue for the dynamic linker, to make function
6586
       pointer comparisons work between an application and shared
6587
       library).  */
6588
0
    if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
6589
0
      sym->st_value = 0;
6590
0
  }
6591
0
    }
6592
6593
0
  if (h->got.offset != MINUS_ONE
6594
      /* TLS got entry have been handled in elf_relocate_section.  */
6595
0
      && !(loongarch_elf_hash_entry (h)->tls_type
6596
0
     & (GOT_TLS_GD | GOT_TLS_IE | GOT_TLS_GDESC))
6597
      /* Have allocated got entry but not allocated rela before.  */
6598
0
      && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
6599
0
    {
6600
0
      asection *sgot, *srela;
6601
0
      Elf_Internal_Rela rela;
6602
0
      bfd_vma off = h->got.offset & ~(bfd_vma)1;
6603
6604
      /* This symbol has an entry in the GOT.  Set it up.  */
6605
0
      sgot = htab->elf.sgot;
6606
0
      srela = htab->elf.srelgot;
6607
0
      BFD_ASSERT (sgot && srela);
6608
6609
0
      rela.r_offset = sec_addr (sgot) + off;
6610
6611
0
      if (h->def_regular
6612
0
    && h->type == STT_GNU_IFUNC)
6613
0
  {
6614
0
    if(h->plt.offset == MINUS_ONE)
6615
0
      {
6616
0
        if (htab->elf.splt == NULL)
6617
0
    srela = htab->elf.irelplt;
6618
6619
0
        if (LARCH_REF_LOCAL (info, h))
6620
0
    {
6621
0
      asection *sec = h->root.u.def.section;
6622
0
      rela.r_info = ELF32_R_INFO (0, R_LARCH_IRELATIVE);
6623
0
      rela.r_addend = h->root.u.def.value + sec->output_section->vma
6624
0
        + sec->output_offset;
6625
0
      bfd_put_32 (info->output_bfd, 0, sgot->contents + off);
6626
0
    }
6627
0
        else
6628
0
    {
6629
0
      BFD_ASSERT (h->dynindx != -1);
6630
0
      rela.r_info = ELF32_R_INFO (h->dynindx, R_LARCH_32);
6631
0
      rela.r_addend = 0;
6632
0
      bfd_put_32 (info->output_bfd, 0, sgot->contents + off);
6633
0
    }
6634
0
      }
6635
0
    else if(bfd_link_pic (info))
6636
0
      {
6637
0
        rela.r_info = ELF32_R_INFO (h->dynindx, R_LARCH_32);
6638
0
        rela.r_addend = 0;
6639
0
        bfd_put_32 (info->output_bfd, rela.r_addend, sgot->contents + off);
6640
0
      }
6641
0
    else
6642
0
      {
6643
0
        asection *plt;
6644
6645
0
        if (!h->pointer_equality_needed)
6646
0
    abort ();
6647
6648
        /* For non-shared object, we can't use .got.plt, which
6649
     contains the real function address if we need pointer
6650
     equality.  We load the GOT entry with the PLT entry.  */
6651
0
        plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
6652
0
        bfd_put_32 (info->output_bfd,
6653
0
        (plt->output_section->vma
6654
0
         + plt->output_offset
6655
0
         + h->plt.offset),
6656
0
        sgot->contents + off);
6657
0
        return true;
6658
0
      }
6659
0
  }
6660
0
      else if (bfd_link_pic (info) && LARCH_REF_LOCAL (info, h))
6661
0
  {
6662
0
    asection *sec = h->root.u.def.section;
6663
0
    bfd_vma linkaddr = h->root.u.def.value + sec->output_section->vma
6664
0
           + sec->output_offset;
6665
6666
    /* Don't emit relative relocs if they are packed, but we need
6667
       to write the addend (link-time addr) into the GOT then.  */
6668
0
    if (info->enable_dt_relr)
6669
0
      {
6670
0
        bfd_put_32 (info->output_bfd, linkaddr, sgot->contents + off);
6671
0
        goto skip_got_reloc;
6672
0
      }
6673
0
    rela.r_info = ELF32_R_INFO (0, R_LARCH_RELATIVE);
6674
0
    rela.r_addend = linkaddr;
6675
0
  }
6676
0
      else
6677
0
  {
6678
0
    BFD_ASSERT (h->dynindx != -1);
6679
0
    rela.r_info = ELF32_R_INFO (h->dynindx, R_LARCH_32);
6680
0
    rela.r_addend = 0;
6681
0
  }
6682
6683
0
      loongarch_elf_append_rela (info->output_bfd, srela, &rela);
6684
0
    }
6685
0
skip_got_reloc:
6686
6687
  /* Mark some specially defined symbols as absolute.  */
6688
0
  if (h == htab->elf.hdynamic || h == htab->elf.hgot || h == htab->elf.hplt)
6689
0
    sym->st_shndx = SHN_ABS;
6690
6691
0
  return true;
6692
0
}
6693
6694
/* Finish up the dynamic sections.  */
6695
6696
static bool
6697
loongarch_finish_dyn (struct bfd_link_info *info, bfd *dynobj, asection *sdyn)
6698
0
{
6699
0
  struct loongarch_elf_link_hash_table *htab = loongarch_elf_hash_table (info);
6700
0
  elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
6701
0
  size_t dynsize = bed->s->sizeof_dyn, skipped_size = 0;
6702
0
  bfd_byte *dyncon, *dynconend;
6703
6704
0
  dynconend = sdyn->contents + sdyn->size;
6705
0
  for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize)
6706
0
    {
6707
0
      Elf_Internal_Dyn dyn;
6708
0
      asection *s;
6709
0
      int skipped = 0;
6710
6711
0
      bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
6712
6713
0
      switch (dyn.d_tag)
6714
0
  {
6715
0
  case DT_PLTGOT:
6716
0
    s = htab->elf.sgotplt;
6717
0
    dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6718
0
    break;
6719
0
  case DT_JMPREL:
6720
0
    s = htab->elf.srelplt;
6721
0
    dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6722
0
    break;
6723
0
  case DT_PLTRELSZ:
6724
0
    s = htab->elf.srelplt;
6725
0
    dyn.d_un.d_val = s->size;
6726
0
    break;
6727
0
  case DT_TEXTREL:
6728
0
    if ((info->flags & DF_TEXTREL) == 0)
6729
0
      skipped = 1;
6730
0
    break;
6731
0
  case DT_FLAGS:
6732
0
    if ((info->flags & DF_TEXTREL) == 0)
6733
0
      dyn.d_un.d_val &= ~DF_TEXTREL;
6734
0
    break;
6735
0
  }
6736
0
      if (skipped)
6737
0
  skipped_size += dynsize;
6738
0
      else
6739
0
  bed->s->swap_dyn_out (info->output_bfd, &dyn, dyncon - skipped_size);
6740
0
    }
6741
  /* Wipe out any trailing entries if we shifted down a dynamic tag.  */
6742
0
  memset (dyncon - skipped_size, 0, skipped_size);
6743
0
  return true;
6744
0
}
6745
6746
/* Finish up local dynamic symbol handling.  We set the contents of
6747
   various dynamic sections here.  */
6748
6749
static int
6750
elf32_loongarch_finish_local_dynamic_symbol (void **slot, void *inf)
6751
0
{
6752
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) *slot;
6753
0
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
6754
6755
0
  return loongarch_elf_finish_dynamic_symbol (info, h, NULL);
6756
0
}
6757
6758
/* Value of struct elf_backend_data->elf_backend_output_arch_local_syms,
6759
   this function is called before elf_link_sort_relocs.
6760
   So relocation R_LARCH_IRELATIVE for local ifunc can be append to
6761
   .rela.dyn (.rela.got) by loongarch_elf_append_rela.  */
6762
6763
static bool
6764
elf_loongarch_output_arch_local_syms
6765
  (struct bfd_link_info *info,
6766
   void *flaginfo ATTRIBUTE_UNUSED,
6767
   int (*func) (void *, const char *,
6768
    Elf_Internal_Sym *,
6769
    asection *,
6770
    struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
6771
0
{
6772
0
  struct loongarch_elf_link_hash_table *htab = loongarch_elf_hash_table (info);
6773
0
  if (htab == NULL)
6774
0
    return false;
6775
6776
  /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
6777
0
  htab_traverse (htab->loc_hash_table,
6778
0
     elf32_loongarch_finish_local_dynamic_symbol,
6779
0
     info);
6780
6781
0
  return true;
6782
0
}
6783
6784
static bool
6785
loongarch_elf_finish_dynamic_sections (struct bfd_link_info *info,
6786
               bfd_byte *buf ATTRIBUTE_UNUSED)
6787
0
{
6788
0
  bfd *dynobj;
6789
0
  asection *sdyn, *plt, *gotplt = NULL;
6790
0
  struct loongarch_elf_link_hash_table *htab;
6791
6792
0
  htab = loongarch_elf_hash_table (info);
6793
0
  BFD_ASSERT (htab);
6794
0
  dynobj = htab->elf.dynobj;
6795
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
6796
6797
0
  if (elf_hash_table (info)->dynamic_sections_created)
6798
0
    {
6799
0
      BFD_ASSERT (htab->elf.splt && sdyn);
6800
6801
0
      if (!loongarch_finish_dyn (info, dynobj, sdyn))
6802
0
  return false;
6803
0
    }
6804
6805
0
  plt = htab->elf.splt;
6806
0
  gotplt = htab->elf.sgotplt;
6807
6808
0
  if (plt && 0 < plt->size)
6809
0
    {
6810
0
      size_t i;
6811
0
      uint32_t plt_header[PLT_HEADER_INSNS];
6812
0
      if (!loongarch_make_plt_header (sec_addr (gotplt), sec_addr (plt),
6813
0
              plt_header))
6814
0
  return false;
6815
6816
0
      for (i = 0; i < PLT_HEADER_INSNS; i++)
6817
0
  bfd_put_32 (info->output_bfd, plt_header[i], plt->contents + 4 * i);
6818
6819
0
      elf_section_data (plt->output_section)->this_hdr.sh_entsize =
6820
0
  PLT_ENTRY_SIZE;
6821
0
    }
6822
6823
0
  if (htab->elf.sgotplt)
6824
0
    {
6825
0
      asection *output_section = htab->elf.sgotplt->output_section;
6826
6827
0
      if (bfd_is_abs_section (output_section))
6828
0
  {
6829
0
    _bfd_error_handler (_("discarded output section: `%pA'"),
6830
0
            htab->elf.sgotplt);
6831
0
    return false;
6832
0
  }
6833
6834
0
      if (0 < htab->elf.sgotplt->size)
6835
0
  {
6836
    /* Write the first two entries in .got.plt, needed for the dynamic
6837
       linker.  */
6838
0
    bfd_put_32 (info->output_bfd, MINUS_ONE, htab->elf.sgotplt->contents);
6839
6840
0
    bfd_put_32 (info->output_bfd, 0,
6841
0
          htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
6842
0
  }
6843
6844
0
      elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
6845
0
    }
6846
6847
0
  if (htab->elf.sgot)
6848
0
    {
6849
0
      asection *output_section = htab->elf.sgot->output_section;
6850
6851
0
      if (0 < htab->elf.sgot->size)
6852
0
  {
6853
    /* Set the first entry in the global offset table to the address of
6854
       the dynamic section.  */
6855
0
    bfd_vma val = sdyn ? sec_addr (sdyn) : 0;
6856
0
    bfd_put_32 (info->output_bfd, val, htab->elf.sgot->contents);
6857
0
  }
6858
6859
0
      elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
6860
0
    }
6861
6862
0
  return true;
6863
0
}
6864
6865
/* Return address for Ith PLT stub in section PLT, for relocation REL
6866
   or (bfd_vma) -1 if it should not be included.  */
6867
6868
static bfd_vma
6869
loongarch_elf_plt_sym_val (bfd_vma i, const asection *plt,
6870
         const arelent *rel ATTRIBUTE_UNUSED)
6871
0
{
6872
0
  return plt->vma + PLT_HEADER_SIZE + i * PLT_ENTRY_SIZE;
6873
0
}
6874
6875
static enum elf_reloc_type_class
6876
loongarch_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
6877
          const asection *rel_sec ATTRIBUTE_UNUSED,
6878
          const Elf_Internal_Rela *rela)
6879
0
{
6880
0
  struct loongarch_elf_link_hash_table *htab;
6881
0
  htab = loongarch_elf_hash_table (info);
6882
6883
0
  if (htab->elf.dynsym != NULL && htab->elf.dynsym->contents != NULL)
6884
0
    {
6885
      /* Check relocation against STT_GNU_IFUNC symbol if there are
6886
   dynamic symbols.  */
6887
0
      bfd *abfd = info->output_bfd;
6888
0
      elf_backend_data *bed = get_elf_backend_data (abfd);
6889
0
      unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
6890
0
      if (r_symndx != STN_UNDEF)
6891
0
  {
6892
0
    Elf_Internal_Sym sym;
6893
0
    if (!bed->s->swap_symbol_in (abfd,
6894
0
               htab->elf.dynsym->contents
6895
0
               + r_symndx * bed->s->sizeof_sym,
6896
0
               0, &sym))
6897
0
      {
6898
        /* xgettext:c-format  */
6899
0
        _bfd_error_handler (_("%pB symbol number %lu references"
6900
0
            " nonexistent SHT_SYMTAB_SHNDX section"),
6901
0
          abfd, r_symndx);
6902
        /* Ideally an error class should be returned here.  */
6903
0
      }
6904
0
    else if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
6905
0
      return reloc_class_ifunc;
6906
0
  }
6907
0
    }
6908
6909
0
  switch (ELF32_R_TYPE (rela->r_info))
6910
0
    {
6911
0
    case R_LARCH_IRELATIVE:
6912
0
      return reloc_class_ifunc;
6913
0
    case R_LARCH_RELATIVE:
6914
0
      return reloc_class_relative;
6915
0
    case R_LARCH_JUMP_SLOT:
6916
0
      return reloc_class_plt;
6917
0
    case R_LARCH_COPY:
6918
0
      return reloc_class_copy;
6919
0
    default:
6920
0
      return reloc_class_normal;
6921
0
    }
6922
0
}
6923
6924
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
6925
6926
static void
6927
loongarch_elf_copy_indirect_symbol (struct bfd_link_info *info,
6928
            struct elf_link_hash_entry *dir,
6929
            struct elf_link_hash_entry *ind)
6930
0
{
6931
0
  struct elf_link_hash_entry *edir, *eind;
6932
6933
0
  edir = dir;
6934
0
  eind = ind;
6935
6936
0
  if (eind->dyn_relocs != NULL)
6937
0
    {
6938
0
      if (edir->dyn_relocs != NULL)
6939
0
  {
6940
0
    struct elf_dyn_relocs **pp;
6941
0
    struct elf_dyn_relocs *p;
6942
6943
    /* Add reloc counts against the indirect sym to the direct sym
6944
       list.  Merge any entries against the same section.  */
6945
0
    for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
6946
0
      {
6947
0
        struct elf_dyn_relocs *q;
6948
6949
0
        for (q = edir->dyn_relocs; q != NULL; q = q->next)
6950
0
    if (q->sec == p->sec)
6951
0
      {
6952
0
        q->pc_count += p->pc_count;
6953
0
        q->count += p->count;
6954
0
        *pp = p->next;
6955
0
        break;
6956
0
      }
6957
0
        if (q == NULL)
6958
0
    pp = &p->next;
6959
0
      }
6960
0
    *pp = edir->dyn_relocs;
6961
0
  }
6962
6963
0
      edir->dyn_relocs = eind->dyn_relocs;
6964
0
      eind->dyn_relocs = NULL;
6965
0
    }
6966
6967
0
  if (ind->root.type == bfd_link_hash_indirect && dir->got.refcount < 0)
6968
0
    {
6969
0
      loongarch_elf_hash_entry(edir)->tls_type
6970
0
  = loongarch_elf_hash_entry(eind)->tls_type;
6971
0
      loongarch_elf_hash_entry(eind)->tls_type = GOT_UNKNOWN;
6972
0
    }
6973
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
6974
0
}
6975
6976
0
#define PRSTATUS_SIZE       0x1e0
6977
#define PRSTATUS_OFFSET_PR_CURSIG   0xc
6978
#define PRSTATUS_OFFSET_PR_PID      0x20
6979
0
#define ELF_GREGSET_T_SIZE      0x168
6980
0
#define PRSTATUS_OFFSET_PR_REG      0x70
6981
6982
/* Support for core dump NOTE sections.  */
6983
6984
static bool
6985
loongarch_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
6986
0
{
6987
0
  switch (note->descsz)
6988
0
    {
6989
0
    default:
6990
0
      return false;
6991
6992
    /* The sizeof (struct elf_prstatus) on Linux/LoongArch.  */
6993
0
    case PRSTATUS_SIZE:
6994
      /* pr_cursig  */
6995
0
      elf_tdata (abfd)->core->signal =
6996
0
  bfd_get_16 (abfd, note->descdata + PRSTATUS_OFFSET_PR_CURSIG);
6997
6998
      /* pr_pid  */
6999
0
      elf_tdata (abfd)->core->lwpid =
7000
0
  bfd_get_32 (abfd, note->descdata + PRSTATUS_OFFSET_PR_PID);
7001
0
      break;
7002
0
    }
7003
7004
  /* Make a ".reg/999" section.  */
7005
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg", ELF_GREGSET_T_SIZE,
7006
0
            note->descpos
7007
0
            + PRSTATUS_OFFSET_PR_REG);
7008
0
}
7009
7010
0
#define PRPSINFO_SIZE       0x88
7011
#define PRPSINFO_OFFSET_PR_PID      0x18
7012
0
#define PRPSINFO_OFFSET_PR_FNAME    0x28
7013
0
#define PRPSINFO_SIZEOF_PR_FNAME    0x10
7014
0
#define PRPSINFO_OFFSET_PR_PS_ARGS  0x38
7015
0
#define PRPSINFO_SIZEOF_PR_PS_ARGS  0x50
7016
7017
static bool
7018
loongarch_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
7019
0
{
7020
0
  switch (note->descsz)
7021
0
    {
7022
0
    default:
7023
0
      return false;
7024
7025
    /* The sizeof (prpsinfo_t) on Linux/LoongArch.  */
7026
0
    case PRPSINFO_SIZE:
7027
      /* pr_pid  */
7028
0
      elf_tdata (abfd)->core->pid =
7029
0
  bfd_get_32 (abfd, note->descdata + PRPSINFO_OFFSET_PR_PID);
7030
7031
      /* pr_fname  */
7032
0
      elf_tdata (abfd)->core->program =
7033
0
  _bfd_elfcore_strndup (abfd, note->descdata + PRPSINFO_OFFSET_PR_FNAME,
7034
0
            PRPSINFO_SIZEOF_PR_FNAME);
7035
7036
      /* pr_psargs  */
7037
0
      elf_tdata (abfd)->core->command =
7038
0
  _bfd_elfcore_strndup (abfd, note->descdata + PRPSINFO_OFFSET_PR_PS_ARGS,
7039
0
            PRPSINFO_SIZEOF_PR_PS_ARGS);
7040
0
      break;
7041
0
    }
7042
7043
  /* Note that for some reason, a spurious space is tacked
7044
     onto the end of the args in some (at least one anyway)
7045
     implementations, so strip it off if it exists.  */
7046
7047
0
  {
7048
0
    char *command = elf_tdata (abfd)->core->command;
7049
0
    int n = strlen (command);
7050
7051
0
    if (0 < n && command[n - 1] == ' ')
7052
0
      command[n - 1] = '\0';
7053
0
  }
7054
7055
0
  return true;
7056
0
}
7057
7058
/* Set the right mach type.  */
7059
static bool
7060
loongarch_elf_object_p (bfd *abfd)
7061
166
{
7062
  /* There are only two mach types in LoongArch currently.  */
7063
166
  if (strcmp (abfd->xvec->name, "elf64-loongarch") == 0)
7064
0
    bfd_default_set_arch_mach (abfd, bfd_arch_loongarch, bfd_mach_loongarch64);
7065
166
  else
7066
166
    bfd_default_set_arch_mach (abfd, bfd_arch_loongarch, bfd_mach_loongarch32);
7067
166
  return true;
7068
166
}
7069
7070
static asection *
7071
loongarch_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
7072
          struct elf_reloc_cookie *cookie,
7073
          struct elf_link_hash_entry *h,
7074
          unsigned int symndx)
7075
0
{
7076
0
  if (h != NULL)
7077
0
    switch (ELF32_R_TYPE (cookie->rel->r_info))
7078
0
      {
7079
0
      case R_LARCH_GNU_VTINHERIT:
7080
0
      case R_LARCH_GNU_VTENTRY:
7081
0
  return NULL;
7082
0
      }
7083
7084
0
  return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
7085
0
}
7086
7087
/* Return TRUE if symbol H should be hashed in the `.gnu.hash' section.  For
7088
   executable PLT slots where the executable never takes the address of those
7089
   functions, the function symbols are not added to the hash table.  */
7090
7091
static bool
7092
elf_loongarch64_hash_symbol (struct elf_link_hash_entry *h)
7093
0
{
7094
0
  if (h->plt.offset != (bfd_vma) -1
7095
0
      && !h->def_regular
7096
0
      && !h->pointer_equality_needed)
7097
0
    return false;
7098
7099
0
  return _bfd_elf_hash_symbol (h);
7100
0
}
7101
7102
/* Write nops to align section.  */
7103
7104
static bool
7105
loongarch_build_align_nops (asection *align_section)
7106
0
{
7107
0
  bfd_byte *loc = align_section->contents;
7108
0
  for (bfd_size_type i = 0; i < (align_section->size / 4); i++)
7109
0
    {
7110
0
      bfd_putl32 (LARCH_NOP, loc);
7111
0
      loc += 4;
7112
0
    }
7113
7114
0
  return true;
7115
0
}
7116
7117
/* Emit an align relocation and a related undefined symbol.  */
7118
7119
static bool
7120
loongarch_add_align_relocs (asection *align_sec, bfd* align_bfd)
7121
0
{
7122
0
  elf_backend_data *bed = get_elf_backend_data (align_bfd);
7123
7124
0
  if (elf_section_data (align_sec)->relocs == NULL)
7125
0
    {
7126
0
      align_sec->reloc_count = 1;
7127
0
      align_sec->flags |= SEC_RELOC;
7128
7129
0
      Elf_Internal_Rela *rela = (Elf_Internal_Rela *)
7130
0
  bfd_malloc (sizeof (Elf_Internal_Rela));
7131
0
      if (rela == NULL)
7132
0
  return false;
7133
7134
0
      rela->r_offset = 0;
7135
0
      rela->r_addend = align_sec->size;
7136
0
      rela->r_info = ELF32_R_INFO (0, R_LARCH_ALIGN);
7137
0
      elf_section_data (align_sec)->relocs = rela;
7138
7139
0
      Elf_Internal_Shdr *rela_hdr = (Elf_Internal_Shdr *)
7140
0
  bfd_zalloc (align_bfd, sizeof (Elf_Internal_Shdr));
7141
0
      if (rela_hdr == NULL)
7142
0
       return false;
7143
7144
0
      rela_hdr->sh_size = sizeof (Elf32_External_Rela);
7145
0
      rela_hdr->sh_entsize = sizeof (Elf32_External_Rela);
7146
0
      rela_hdr->sh_type = SHT_RELA;
7147
0
      rela_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
7148
0
      elf_section_data (align_sec)->rela.hdr = rela_hdr;
7149
0
    }
7150
7151
  /* Used in loongarch_elf_relax_section for align relocations.  */
7152
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (align_bfd);
7153
0
  if (symtab_hdr->contents == NULL)
7154
0
    {
7155
0
      Elf_Internal_Sym *sym = (Elf_Internal_Sym *)
7156
0
  bfd_zmalloc (sizeof (Elf_Internal_Sym));
7157
0
      if (sym == NULL)
7158
0
  return false;
7159
7160
      /* Match if (r_symndx < symtab_hdr->sh_info) branch in
7161
   loongarch_elf_relocate_section to avoid find link failed.  */
7162
0
      symtab_hdr->sh_info = 1;
7163
0
      symtab_hdr->contents = (unsigned char *) sym;
7164
0
    }
7165
7166
0
  return true;
7167
0
}
7168
7169
/* Section name for aligns is the associated section name plus this
7170
   string.  */
7171
0
#define ALIGN_SUFFIX ".align"
7172
7173
/* Determine and set the size of the align section for a final link.  */
7174
7175
bool
7176
elf32_loongarch_size_aligns (bfd *output_bfd,
7177
           bfd *align_bfd,
7178
           struct bfd_link_info *info,
7179
           asection * (*add_align_section)
7180
            (const char *, asection *),
7181
           void (*layout_sections_again) (void))
7182
0
{
7183
7184
0
  struct loongarch_elf_link_hash_table *htab;
7185
0
  bool need_laying_out = false;
7186
7187
0
  htab = loongarch_elf_hash_table (info);
7188
0
  if (!htab->reloc_may_remove_bytes)
7189
0
    return true;
7190
7191
0
  for (bfd *input_bfd = info->input_bfds; input_bfd != NULL;
7192
0
       input_bfd = input_bfd->link.next)
7193
0
    {
7194
0
      asection *sec;
7195
0
      asection *align_sec;
7196
7197
0
      if (!is_loongarch_elf (input_bfd)
7198
0
    || (input_bfd->flags & BFD_LINKER_CREATED) != 0)
7199
0
  continue;
7200
7201
0
      for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
7202
0
  {
7203
    /* If this section is not a code section, do nothing.  */
7204
0
    if ((sec->flags & SEC_CODE) == 0)
7205
0
      continue;
7206
7207
    /* If this section is a link-once section that will be
7208
       discarded, then don't create any stubs.  */
7209
0
    if (sec->output_section == NULL
7210
0
        || sec->output_section->owner != output_bfd)
7211
0
      continue;
7212
7213
0
    if (sec->alignment_power > 2)
7214
0
      {
7215
0
        char *s_name;
7216
0
        size_t namelen;
7217
0
        bfd_size_type len;
7218
0
        namelen = strlen (sec->name);
7219
0
        len = namelen + sizeof (ALIGN_SUFFIX);
7220
0
        s_name = bfd_alloc (align_bfd, len);
7221
0
        if (s_name == NULL)
7222
0
    return false;
7223
0
        memcpy (s_name, sec->name, namelen);
7224
0
        memcpy (s_name + namelen, ALIGN_SUFFIX, sizeof (ALIGN_SUFFIX));
7225
7226
0
        align_sec = add_align_section (s_name, sec);
7227
7228
0
        if (align_sec == NULL)
7229
0
    return false;
7230
7231
0
        if (! loongarch_build_align_nops (align_sec))
7232
0
    return false;
7233
7234
0
        if (! loongarch_add_align_relocs (align_sec, align_bfd))
7235
0
    return false;
7236
7237
        /* Change the section alignment to 4 to disable the default
7238
     behavior of "dot += alignment_needed" in ldlang.c.  */
7239
0
        bfd_set_section_alignment (sec, 2);
7240
7241
0
        need_laying_out = true;
7242
0
      }
7243
0
  }
7244
0
    }
7245
7246
0
  if (need_laying_out)
7247
0
    layout_sections_again ();
7248
7249
  return true;
7250
0
}
7251
7252
#define TARGET_LITTLE_SYM loongarch_elf32_vec
7253
#define TARGET_LITTLE_NAME "elf32-loongarch"
7254
#define ELF_ARCH bfd_arch_loongarch
7255
#define ELF_TARGET_ID LARCH_ELF_DATA
7256
#define ELF_MACHINE_CODE EM_LOONGARCH
7257
#define ELF_MINPAGESIZE 0x1000
7258
#define ELF_MAXPAGESIZE 0x10000
7259
#define ELF_COMMONPAGESIZE 0x4000
7260
#define bfd_elf32_bfd_reloc_type_lookup loongarch_reloc_type_lookup
7261
#define bfd_elf32_bfd_link_hash_table_create          \
7262
  loongarch_elf_link_hash_table_create
7263
#define bfd_elf32_bfd_reloc_name_lookup loongarch_reloc_name_lookup
7264
#define elf_info_to_howto_rel NULL /* Fall through to elf_info_to_howto.  */
7265
#define elf_info_to_howto loongarch_info_to_howto_rela
7266
#define bfd_elf32_mkobject              \
7267
  elf32_loongarch_object
7268
#define bfd_elf32_bfd_merge_private_bfd_data          \
7269
  elf32_loongarch_merge_private_bfd_data
7270
7271
#define elf_backend_reloc_type_class loongarch_reloc_type_class
7272
#define elf_backend_copy_indirect_symbol loongarch_elf_copy_indirect_symbol
7273
#define elf_backend_create_dynamic_sections          \
7274
  loongarch_elf_create_dynamic_sections
7275
#define elf_backend_check_relocs loongarch_elf_check_relocs
7276
#define elf_backend_adjust_dynamic_symbol loongarch_elf_adjust_dynamic_symbol
7277
#define elf_backend_late_size_sections loongarch_elf_late_size_sections
7278
#define elf_backend_relocate_section loongarch_elf_relocate_section
7279
#define elf_backend_finish_dynamic_symbol loongarch_elf_finish_dynamic_symbol
7280
#define elf_backend_output_arch_local_syms \
7281
  elf_loongarch_output_arch_local_syms
7282
#define elf_backend_finish_dynamic_sections          \
7283
  loongarch_elf_finish_dynamic_sections
7284
#define elf_backend_object_p loongarch_elf_object_p
7285
#define elf_backend_gc_mark_hook loongarch_elf_gc_mark_hook
7286
#define elf_backend_plt_sym_val loongarch_elf_plt_sym_val
7287
#define elf_backend_grok_prstatus loongarch_elf_grok_prstatus
7288
#define elf_backend_grok_psinfo loongarch_elf_grok_psinfo
7289
#define elf_backend_hash_symbol elf_loongarch64_hash_symbol
7290
#define bfd_elf32_bfd_relax_section loongarch_elf_relax_section
7291
#define elf_backend_size_relative_relocs loongarch_elf_size_relative_relocs
7292
#define elf_backend_finish_relative_relocs \
7293
  loongarch_elf_finish_relative_relocs
7294
#define bfd_elf32_new_section_hook loongarch_elf_new_section_hook
7295
#define elf_backend_want_stub_bfd      1
7296
#define elf_backend_dtrel_excludes_plt 1
7297
7298
#include "elf32-target.h"