Coverage Report

Created: 2026-03-10 08:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf64-bpf.c
Line
Count
Source
1
/* Linux bpf specific support for 64-bit ELF
2
   Copyright (C) 2019-2026 Free Software Foundation, Inc.
3
   Contributed by Oracle Inc.
4
5
   This file is part of BFD, the Binary File Descriptor library.
6
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
22
#include "sysdep.h"
23
#include "bfd.h"
24
#include "libbfd.h"
25
#include "elf-bfd.h"
26
#include "elf/bpf.h"
27
#include "libiberty.h"
28
29
/* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
30
#define MINUS_ONE (~ (bfd_vma) 0)
31
32
47
#define BASEADDR(SEC) ((SEC)->output_section->vma + (SEC)->output_offset)
33
34
static bfd_reloc_status_type bpf_elf_generic_reloc
35
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
36
37
#undef BPF_HOWTO
38
#define BPF_HOWTO(type, right, size, bits, pcrel, left, ovf, func, name,   \
39
      inplace, src_mask, dst_mask, pcrel_off)                  \
40
  type##_IDX,
41
enum bpf_reloc_index {
42
  R_BPF_INVALID_IDX = -1,
43
#include "bpf-reloc.def"
44
  R_BPF_SIZE
45
};
46
#undef BPF_HOWTO
47
48
/* Relocation tables.  */
49
#define BPF_HOWTO(...) HOWTO(__VA_ARGS__),
50
static reloc_howto_type bpf_elf_howto_table [] =
51
{
52
  #include "bpf-reloc.def"
53
};
54
#undef AHOW
55
#undef BPF_HOWTO
56
57
#define BPF_HOWTO(type, right, size, bits, pcrel, left, ovf, func, name,   \
58
      inplace, src_mask, dst_mask, pcrel_off)                  \
59
140
    case type: { return type##_IDX; }
60
static enum bpf_reloc_index
61
bpf_index_for_rtype(unsigned int r_type)
62
150
{
63
150
  switch(r_type) {
64
0
#include "bpf-reloc.def"
65
10
    default:
66
      /* Unreachable code. */
67
10
      BFD_ASSERT(0);
68
10
      return -1;
69
150
  };
70
0
}
71
72
/* Map BFD reloc types to bpf ELF reloc types.  */
73
74
static reloc_howto_type *
75
bpf_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
76
                        bfd_reloc_code_real_type code)
77
0
{
78
0
  switch (code)
79
0
    {
80
0
    case BFD_RELOC_NONE:
81
0
      return &bpf_elf_howto_table[ (int) R_BPF_NONE_IDX];
82
83
0
    case BFD_RELOC_32:
84
0
      return &bpf_elf_howto_table[ (int) R_BPF_64_ABS32_IDX];
85
0
    case BFD_RELOC_64:
86
0
      return &bpf_elf_howto_table[ (int) R_BPF_64_ABS64_IDX];
87
88
0
    case BFD_RELOC_BPF_64:
89
0
      return &bpf_elf_howto_table[ (int) R_BPF_64_64_IDX];
90
0
    case BFD_RELOC_BPF_DISP32:
91
0
    case BFD_RELOC_BPF_DISPCALL32:
92
0
      return &bpf_elf_howto_table[ (int) R_BPF_64_32_IDX];
93
0
    case BFD_RELOC_BPF_DISP16:
94
0
      return &bpf_elf_howto_table[ (int) R_BPF_GNU_64_16_IDX];
95
96
0
    default:
97
      /* Pacify gcc -Wall.  */
98
0
      return NULL;
99
0
    }
100
0
  return NULL;
101
0
}
102
103
/* Map BFD reloc names to bpf ELF reloc names.  */
104
105
static reloc_howto_type *
106
bpf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
107
0
{
108
0
  unsigned int i;
109
110
0
  for (i = 0; i < R_BPF_SIZE; i++)
111
0
    if (bpf_elf_howto_table[i].name != NULL
112
0
  && strcasecmp (bpf_elf_howto_table[i].name, r_name) == 0)
113
0
      return &bpf_elf_howto_table[i];
114
115
0
  return NULL;
116
0
}
117
118
/* Set the howto pointer for a bpf reloc.  */
119
120
static bool
121
bpf_info_to_howto (bfd *abfd, arelent *bfd_reloc,
122
                    Elf_Internal_Rela *elf_reloc)
123
150
{
124
150
  unsigned int r_type;
125
150
  unsigned int i;
126
150
  r_type = ELF64_R_TYPE (elf_reloc->r_info);
127
128
150
  i = bpf_index_for_rtype(r_type);
129
150
  if (i == (unsigned int) -1)
130
10
    {
131
      /* xgettext:c-format */
132
10
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
133
10
                          abfd, r_type);
134
10
      bfd_set_error (bfd_error_bad_value);
135
10
      return false;
136
10
    }
137
138
140
  bfd_reloc->howto = &bpf_elf_howto_table [i];
139
140
  return true;
140
150
}
141
142
/* Relocate an eBPF ELF section.
143
144
   The RELOCATE_SECTION function is called by the new ELF backend linker
145
   to handle the relocations for a section.
146
147
   The relocs are always passed as Rela structures; if the section
148
   actually uses Rel structures, the r_addend field will always be
149
   zero.
150
151
   This function is responsible for adjusting the section contents as
152
   necessary, and (if using Rela relocs and generating a relocatable
153
   output file) adjusting the reloc addend as necessary.
154
155
   This function does not have to worry about setting the reloc
156
   address or the reloc symbol index.
157
158
   LOCAL_SYMS is a pointer to the swapped in local symbols.
159
160
   LOCAL_SECTIONS is an array giving the section in the input file
161
   corresponding to the st_shndx field of each local symbol.
162
163
   The global hash table entry for the global symbols can be found
164
   via elf_sym_hashes (input_bfd).
165
166
   When generating relocatable output, this function must handle
167
   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
168
   going to be the section symbol corresponding to the output
169
   section, which means that the addend must be adjusted
170
   accordingly.  */
171
172
0
#define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
173
174
static int
175
bpf_elf_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
176
                          struct bfd_link_info *info,
177
                          bfd *input_bfd,
178
                          asection *input_section,
179
                          bfd_byte *contents,
180
                          Elf_Internal_Rela *relocs,
181
                          Elf_Internal_Sym *local_syms,
182
                          asection **local_sections)
183
0
{
184
0
  Elf_Internal_Shdr *symtab_hdr;
185
0
  struct elf_link_hash_entry **sym_hashes;
186
0
  Elf_Internal_Rela *rel;
187
0
  Elf_Internal_Rela *relend;
188
189
0
  symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
190
0
  sym_hashes = elf_sym_hashes (input_bfd);
191
0
  relend     = relocs + input_section->reloc_count;
192
193
0
  for (rel = relocs; rel < relend; rel ++)
194
0
    {
195
0
      reloc_howto_type *     howto;
196
0
      unsigned int       howto_index;
197
0
      unsigned long      r_symndx;
198
0
      Elf_Internal_Sym *     sym;
199
0
      asection *       sec;
200
0
      struct elf_link_hash_entry * h;
201
0
      bfd_vma        relocation;
202
0
      bfd_reloc_status_type    r;
203
0
      const char *       name = NULL;
204
0
      int        r_type ATTRIBUTE_UNUSED;
205
0
      bfd_signed_vma               addend;
206
0
      bfd_byte                   * where;
207
208
0
      r_type = ELF64_R_TYPE (rel->r_info);
209
0
      r_symndx = ELF64_R_SYM (rel->r_info);
210
211
0
      howto_index = bpf_index_for_rtype (ELF64_R_TYPE (rel->r_info));
212
0
      howto  = &bpf_elf_howto_table[howto_index];
213
0
      h      = NULL;
214
0
      sym    = NULL;
215
0
      sec    = NULL;
216
0
      where  = contents + rel->r_offset;
217
218
0
      if (r_symndx < symtab_hdr->sh_info)
219
0
  {
220
0
    sym = local_syms + r_symndx;
221
0
    sec = local_sections [r_symndx];
222
0
    relocation = BASEADDR (sec) + sym->st_value;
223
224
0
    name = bfd_elf_string_from_elf_section
225
0
      (input_bfd, symtab_hdr->sh_link, sym->st_name);
226
0
    name = name == NULL ? bfd_section_name (sec) : name;
227
0
  }
228
0
      else
229
0
  {
230
0
    bool warned ATTRIBUTE_UNUSED;
231
0
    bool unresolved_reloc ATTRIBUTE_UNUSED;
232
0
    bool ignored ATTRIBUTE_UNUSED;
233
234
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
235
0
           r_symndx, symtab_hdr, sym_hashes,
236
0
           h, sec, relocation,
237
0
           unresolved_reloc, warned, ignored);
238
239
0
    name = h->root.root.string;
240
0
  }
241
242
0
      if (sec != NULL && discarded_section (sec))
243
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
244
0
           rel, 1, relend, R_BPF_NONE,
245
0
           howto, 0, contents);
246
247
0
      if (bfd_link_relocatable (info))
248
0
  continue;
249
250
0
      switch (howto->type)
251
0
        {
252
0
  case R_BPF_64_32:
253
0
          {
254
            /* Make the relocation PC-relative, and change its unit to
255
               64-bit words.  Note we need *signed* arithmetic
256
               here.  */
257
0
            relocation = ((bfd_signed_vma) relocation
258
0
        - (sec_addr (input_section) + rel->r_offset));
259
0
            relocation = (bfd_signed_vma) relocation / 8;
260
            
261
            /* Get the addend from the instruction and apply it.  */
262
0
            addend = bfd_get (howto->bitsize, input_bfd,
263
0
                              contents + rel->r_offset
264
0
                              + (howto->bitsize == 16 ? 2 : 4));
265
                              
266
0
            if ((addend & (((~howto->src_mask) >> 1) & howto->src_mask)) != 0)
267
0
              addend -= (((~howto->src_mask) >> 1) & howto->src_mask) << 1;
268
0
            relocation += addend;
269
270
            /* Write out the relocated value.  */
271
0
            bfd_put (howto->bitsize, input_bfd, relocation,
272
0
                     contents + rel->r_offset
273
0
                     + (howto->bitsize == 16 ? 2 : 4));
274
275
0
            r = bfd_reloc_ok;
276
0
            break;
277
0
          }
278
0
  case R_BPF_64_ABS64:
279
0
  case R_BPF_64_ABS32:
280
0
  case R_BPF_64_NODYLD32:
281
0
    {
282
0
      addend = bfd_get (howto->bitsize, input_bfd, where);
283
0
      relocation += addend;
284
0
      bfd_put (howto->bitsize, input_bfd, relocation, where);
285
286
0
      r = bfd_reloc_ok;
287
0
      break;
288
0
    }
289
0
  case R_BPF_64_64:
290
0
          {
291
            /*
292
                LDDW instructions are 128 bits long, with a 64-bit immediate.
293
                The lower 32 bits of the immediate are in the same position
294
                as the imm32 field of other instructions.
295
                The upper 32 bits of the immediate are stored at the end of
296
                the instruction.
297
             */
298
299
300
            /* Get the addend. The upper and lower 32 bits are split.
301
               'where' is the beginning of the 16-byte instruction. */
302
0
            addend = bfd_get_32 (input_bfd, where + 4);
303
0
            addend |= (bfd_get_32 (input_bfd, where + 12) << 32);
304
305
0
            relocation += addend;
306
307
0
            bfd_put_32 (input_bfd, (relocation & 0xFFFFFFFF), where + 4);
308
0
            bfd_put_32 (input_bfd, (relocation >> 32), where + 12);
309
0
            r = bfd_reloc_ok;
310
0
            break;
311
0
          }
312
0
        default:
313
0
    r = bfd_reloc_notsupported;
314
0
        }
315
316
0
      if (r == bfd_reloc_ok)
317
0
    r = bfd_check_overflow (howto->complain_on_overflow,
318
0
          howto->bitsize,
319
0
          howto->rightshift,
320
0
          64, relocation);
321
322
0
      if (r != bfd_reloc_ok)
323
0
  {
324
0
    const char * msg = NULL;
325
326
0
    switch (r)
327
0
      {
328
0
      case bfd_reloc_overflow:
329
0
        (*info->callbacks->reloc_overflow)
330
0
    (info, (h ? &h->root : NULL), name, howto->name,
331
0
     (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
332
0
        break;
333
334
0
      case bfd_reloc_undefined:
335
0
        (*info->callbacks->undefined_symbol)
336
0
    (info, name, input_bfd, input_section, rel->r_offset, true);
337
0
        break;
338
339
0
      case bfd_reloc_outofrange:
340
0
        msg = _("internal error: out of range error");
341
0
        break;
342
343
0
      case bfd_reloc_notsupported:
344
0
        if (sym != NULL) /* Only if it's not an unresolved symbol.  */
345
0
                msg = _("internal error: relocation not supported");
346
0
        break;
347
348
0
      case bfd_reloc_dangerous:
349
0
        msg = _("internal error: dangerous relocation");
350
0
        break;
351
352
0
      default:
353
0
        msg = _("internal error: unknown error");
354
0
        break;
355
0
      }
356
357
0
    if (msg)
358
0
      (*info->callbacks->warning) (info, msg, name, input_bfd,
359
0
           input_section, rel->r_offset);
360
0
  }
361
0
    }
362
363
0
  return true;
364
0
}
365
366
/* Merge backend specific data from an object file to the output
367
   object file when linking.  */
368
369
static bool
370
elf64_bpf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
371
0
{
372
  /* Check if we have the same endianness.  */
373
0
  if (! _bfd_generic_verify_endian_match (ibfd, info))
374
0
    return false;
375
376
0
  return true;
377
0
}
378
379
/* A generic howto special function for installing BPF relocations.
380
   This function will be called by the assembler (via bfd_install_relocation),
381
   and by various get_relocated_section_contents functions.
382
   At link time, bpf_elf_relocate_section will resolve the final relocations.
383
384
   BPF instructions are always big endian, and this approach avoids problems in
385
   bfd_install_relocation.  */
386
387
static bfd_reloc_status_type
388
bpf_elf_generic_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
389
           void *data, asection *input_section, bfd *output_bfd,
390
           char **error_message ATTRIBUTE_UNUSED)
391
89
{
392
393
89
  bfd_signed_vma relocation;
394
89
  bfd_reloc_status_type status;
395
89
  bfd_byte *where;
396
397
  /* From bfd_elf_generic_reloc.  */
398
89
  if (output_bfd != NULL
399
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
400
0
      && (! reloc_entry->howto->partial_inplace
401
0
    || reloc_entry->addend == 0))
402
0
    {
403
0
      reloc_entry->address += input_section->output_offset;
404
0
      return bfd_reloc_ok;
405
0
    }
406
407
89
  if (output_bfd == NULL
408
89
      && !reloc_entry->howto->pc_relative
409
40
      && (symbol->section->flags & SEC_DEBUGGING) != 0
410
13
      && (input_section->flags & SEC_DEBUGGING) != 0)
411
2
    reloc_entry->addend -= symbol->section->output_section->vma;
412
413
  /* Sanity check that the address is in range.  */
414
89
  bfd_size_type end = bfd_get_section_limit_octets (abfd, input_section);
415
89
  bfd_size_type reloc_size;
416
89
  if (reloc_entry->howto->type == R_BPF_64_64)
417
25
    reloc_size = 16;
418
64
  else
419
64
    reloc_size = (reloc_entry->howto->bitsize
420
64
      + reloc_entry->howto->bitpos) / 8;
421
422
89
  if (reloc_entry->address > end
423
81
      || end - reloc_entry->address < reloc_size)
424
14
    return bfd_reloc_outofrange;
425
426
  /* Behave similarly to bfd_install_relocation with install_addend set.
427
     That is, just install the addend and do not include the value of
428
     the symbol.  */
429
75
  relocation = reloc_entry->addend;
430
431
75
  if (symbol->flags & BSF_SECTION_SYM)
432
    /* Relocation against a section symbol: add in the section base address.  */
433
47
    relocation += BASEADDR (symbol->section);
434
435
75
  where = (bfd_byte *) data + reloc_entry->address;
436
437
75
  status = bfd_check_overflow (reloc_entry->howto->complain_on_overflow,
438
75
             reloc_entry->howto->bitsize,
439
75
             reloc_entry->howto->rightshift, 64, relocation);
440
441
75
  if (status != bfd_reloc_ok)
442
14
    return status;
443
444
  /* Now finally install the relocation.  */
445
61
  if (reloc_entry->howto->type == R_BPF_64_64)
446
23
    {
447
      /* lddw is a 128-bit (!) instruction that allows loading a 64-bit
448
   immediate into a register. the immediate is split in half, with the
449
   lower 32 bits in the same position as the imm32 field of other
450
   instructions, and the upper 32 bits placed at the very end of the
451
   instruction. that is, there are 32 unused bits between them. */
452
453
23
      bfd_put_32 (abfd, (relocation & 0xFFFFFFFF), where + 4);
454
23
      bfd_put_32 (abfd, (relocation >> 32), where + 12);
455
23
    }
456
38
  else
457
38
    {
458
      /* For other kinds of relocations, the relocated value simply goes
459
   BITPOS bits from the start of the entry. This is always a multiple
460
   of 8, i.e. whole bytes.  */
461
38
      bfd_put (reloc_entry->howto->bitsize, abfd, relocation,
462
38
         where + reloc_entry->howto->bitpos / 8);
463
38
    }
464
465
61
  if (output_bfd != NULL)
466
0
    reloc_entry->address += input_section->output_offset;
467
468
61
  return bfd_reloc_ok;
469
61
}
470
471
472
/* The macros below configure the architecture.  */
473
474
#define TARGET_LITTLE_SYM bpf_elf64_le_vec
475
#define TARGET_LITTLE_NAME "elf64-bpfle"
476
477
#define TARGET_BIG_SYM bpf_elf64_be_vec
478
#define TARGET_BIG_NAME "elf64-bpfbe"
479
480
#define ELF_ARCH bfd_arch_bpf
481
#define ELF_MACHINE_CODE EM_BPF
482
483
#define ELF_MAXPAGESIZE 0x100000
484
485
#define elf_info_to_howto_rel bpf_info_to_howto
486
#define elf_info_to_howto bpf_info_to_howto
487
488
#define elf_backend_may_use_rel_p   1
489
#define elf_backend_may_use_rela_p    0
490
#define elf_backend_default_use_rela_p    0
491
#define elf_backend_relocate_section    bpf_elf_relocate_section
492
493
#define elf_backend_can_gc_sections   0
494
495
#define elf_symbol_leading_char     '_'
496
#define bfd_elf64_bfd_reloc_type_lookup   bpf_reloc_type_lookup
497
#define bfd_elf64_bfd_reloc_name_lookup   bpf_reloc_name_lookup
498
499
#define bfd_elf64_bfd_merge_private_bfd_data elf64_bpf_merge_private_bfd_data
500
501
#include "elf64-target.h"