Coverage Report

Created: 2025-11-11 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/elfutils/libelf/note_xlate.h
Line
Count
Source
1
/* Conversion functions for notes.
2
   Copyright (C) 2007, 2009, 2014, 2018 Red Hat, Inc.
3
   This file is part of elfutils.
4
5
   This file is free software; you can redistribute it and/or modify
6
   it under the terms of either
7
8
     * the GNU Lesser General Public License as published by the Free
9
       Software Foundation; either version 3 of the License, or (at
10
       your option) any later version
11
12
   or
13
14
     * the GNU General Public License as published by the Free
15
       Software Foundation; either version 2 of the License, or (at
16
       your option) any later version
17
18
   or both in parallel, as here.
19
20
   elfutils is distributed in the hope that it will be useful, but
21
   WITHOUT ANY WARRANTY; without even the implied warranty of
22
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23
   General Public License for more details.
24
25
   You should have received copies of the GNU General Public License and
26
   the GNU Lesser General Public License along with this program.  If
27
   not, see <http://www.gnu.org/licenses/>.  */
28
29
static void
30
elf_cvt_note (void *dest, const void *src, size_t len, int encode,
31
        bool nhdr8)
32
3.59k
{
33
  /* Note that the header is always the same size, but the padding
34
     differs for GNU Property notes.  */
35
3.59k
  assert (sizeof (Elf32_Nhdr) == sizeof (Elf64_Nhdr));
36
37
12.3k
  while (len >= sizeof (Elf32_Nhdr))
38
11.2k
    {
39
      /* Convert the header.  */
40
11.2k
      (1 ? Elf32_cvt_Nhdr : Elf64_cvt_Nhdr) (dest, src, sizeof (Elf32_Nhdr),
41
11.2k
               encode);
42
11.2k
      const Elf32_Nhdr *n = encode ? src : dest;
43
44
11.2k
      size_t note_len = sizeof *n;
45
46
      /* desc needs to be aligned.  */
47
11.2k
      note_len += n->n_namesz;
48
11.2k
      note_len = nhdr8 ? NOTE_ALIGN8 (note_len) : NOTE_ALIGN4 (note_len);
49
11.2k
      if (note_len > len || note_len < sizeof *n)
50
1.30k
  {
51
    /* Header was translated, nothing else.  */
52
1.30k
    len -= sizeof *n;
53
1.30k
    src += sizeof *n;
54
1.30k
    dest += sizeof *n;
55
1.30k
    break;
56
1.30k
  }
57
58
      /* data as a whole needs to be aligned.  */
59
9.96k
      note_len += n->n_descsz;
60
9.96k
      note_len = nhdr8 ? NOTE_ALIGN8 (note_len) : NOTE_ALIGN4 (note_len);
61
9.96k
      if (note_len > len || note_len < sizeof *n)
62
1.25k
  {
63
    /* Header was translated, nothing else.  */
64
1.25k
    len -= sizeof *n;
65
1.25k
    src += sizeof *n;
66
1.25k
    dest += sizeof *n;
67
1.25k
    break;
68
1.25k
  }
69
70
      /* Copy or skip the note data.  */
71
8.71k
      size_t note_data_len = note_len - sizeof *n;
72
8.71k
      src += sizeof *n;
73
8.71k
      dest += sizeof *n;
74
8.71k
      if (src != dest)
75
8.71k
  memcpy (dest, src, note_data_len);
76
77
8.71k
      src += note_data_len;
78
8.71k
      dest += note_data_len;
79
8.71k
      len -= note_len;
80
8.71k
    }
81
82
    /* Copy over any leftover data unconverted.  Probably part of
83
       truncated name/desc data.  */
84
3.59k
    if (unlikely (len > 0) && src != dest)
85
2.53k
      memcpy (dest, src, len);
86
3.59k
}
87
88
static void
89
elf_cvt_note4 (void *dest, const void *src, size_t len, int encode)
90
1.60k
{
91
1.60k
  elf_cvt_note (dest, src, len, encode, false);
92
1.60k
}
93
94
static void
95
elf_cvt_note8 (void *dest, const void *src, size_t len, int encode)
96
1.98k
{
97
  elf_cvt_note (dest, src, len, encode, true);
98
1.98k
}