Branch data Line data Source code
1 : : /* Parses .debug_sup and .debug_dwp sections.
2 : : Copyright (C) 2026 Mark J. Wielaard
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 a copy 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 : : #ifdef HAVE_CONFIG_H
30 : : # include <config.h>
31 : : #endif
32 : :
33 : : #include "libdwelfP.h"
34 : :
35 : : /* .debug_sup and .debug_dwp are identical, just the name of the flag
36 : : is different. */
37 : : static ssize_t
38 : 162 : dwelf_dwarf_debug_sec (Dwarf *dwarf,
39 : : int idx, /* Either IDX_debug_sup or IDX_debug_dwp. */
40 : : uint16_t *versionp,
41 : : uint8_t *is_flagp,
42 : : const char **filepathp,
43 : : const uint8_t **idp)
44 : : {
45 [ - + ]: 162 : if (dwarf == NULL)
46 : : return -1;
47 : :
48 : 162 : Elf_Data *data = dwarf->sectiondata[idx];
49 [ + + ]: 162 : if (data == NULL)
50 : : {
51 [ + - ]: 148 : if (filepathp != NULL)
52 : 148 : *filepathp = NULL;
53 [ + - ]: 148 : if (idp != NULL)
54 : 148 : *idp = NULL;
55 : 148 : return 0;
56 : : }
57 : :
58 : : /* Minimal is version 2 bytes, flag 1 byte, empty path 1 byte, zero
59 : : id_len 1 byte for a total of 5. */
60 [ - + - + ]: 14 : if (data->d_buf == NULL || data->d_size < 5)
61 : : {
62 : 0 : bad_data:
63 : 0 : __libdw_seterrno (DWARF_E_INVALID_DWARF);
64 : 0 : return -1;
65 : : }
66 : :
67 : 14 : const unsigned char *readp = data->d_buf;
68 : 14 : const unsigned char *endp = data->d_buf + data->d_size;
69 [ - + ]: 14 : uint16_t version = read_2ubyte_unaligned_inc (dwarf, readp);
70 [ + - ]: 14 : if ((idx == IDX_debug_sup && version != 5)
71 [ - + ]: 14 : || (idx == IDX_debug_dwp && version != 6))
72 : 0 : goto bad_data;
73 : 14 : uint8_t flag = *readp++;
74 : :
75 : 14 : const char *filepath = (const char *) readp;
76 : : /* Must be '\0' terminated. */
77 : 14 : readp = memchr (filepath, '\0', (size_t) (endp - readp));
78 [ - + ]: 14 : if (readp == NULL)
79 : 0 : goto bad_data;
80 : :
81 : 14 : readp++;
82 [ - + ]: 14 : if (readp == endp)
83 : 0 : goto bad_data;
84 : :
85 : 14 : uint64_t id_len;
86 : 14 : get_uleb128 (id_len, readp, endp);
87 [ + - - + ]: 14 : if (id_len > 0 && (uint64_t) (endp - readp) < id_len)
88 : 0 : goto bad_data;
89 : :
90 [ + + ]: 14 : if (versionp != NULL)
91 : 4 : *versionp = version;
92 [ + - ]: 14 : if (is_flagp != NULL)
93 : 14 : *is_flagp = flag;
94 [ + - ]: 14 : if (filepathp != NULL)
95 : 14 : *filepathp = filepath;
96 [ + - ]: 14 : if (idp != NULL)
97 : 14 : *idp = readp;
98 : :
99 : 14 : return id_len;
100 : : }
101 : :
102 : : ssize_t
103 : 68 : dwelf_dwarf_debug_sup (Dwarf *dwarf,
104 : : uint16_t *versionp,
105 : : uint8_t *is_supp,
106 : : const char **filepathp,
107 : : const uint8_t **idp)
108 : : {
109 : 68 : int idx = IDX_debug_sup;
110 : 68 : return dwelf_dwarf_debug_sec (dwarf, idx,
111 : : versionp, is_supp, filepathp, idp);
112 : : }
113 : : INTDEF(dwelf_dwarf_debug_sup)
114 : :
115 : : ssize_t
116 : 94 : dwelf_dwarf_debug_dwp (Dwarf *dwarf,
117 : : uint16_t *versionp,
118 : : uint8_t *is_dwpp,
119 : : const char **filepathp,
120 : : const uint8_t **idp)
121 : : {
122 : 94 : int idx = IDX_debug_dwp;
123 : 94 : return dwelf_dwarf_debug_sec (dwarf, idx,
124 : : versionp, is_dwpp, filepathp, idp);
125 : : }
126 : : INTDEF(dwelf_dwarf_debug_dwp)
|