/src/binutils-gdb/bfd/btf.c
Line | Count | Source |
1 | | /* btf.c -- display BTF contents of a BFD binary file |
2 | | Copyright (C) 2026 Free Software Foundation, Inc. |
3 | | |
4 | | This file is part of GNU Binutils. |
5 | | |
6 | | This program is free software; you can redistribute it and/or modify |
7 | | it under the terms of the GNU General Public License as published by |
8 | | the Free Software Foundation; either version 3 of the License, or |
9 | | (at your option) any later version. |
10 | | |
11 | | This program is distributed in the hope that it will be useful, |
12 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | GNU General Public License for more details. |
15 | | |
16 | | You should have received a copy of the GNU General Public License |
17 | | along with this program; if not, write to the Free Software |
18 | | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA |
19 | | 02110-1301, USA. */ |
20 | | |
21 | | #include "sysdep.h" |
22 | | #include "bfd.h" |
23 | | #include "libbfd.h" |
24 | | #include "btf.h" |
25 | | |
26 | | struct btf_header * |
27 | | btf_read_header (bfd *abfd, bfd_byte *buf) |
28 | 0 | { |
29 | 0 | struct btf_header *header = bfd_zmalloc (sizeof (struct btf_header)); |
30 | |
|
31 | 0 | header->magic = bfd_get_16 (abfd, buf); |
32 | 0 | buf += 2; |
33 | 0 | header->version = bfd_get_8 (abfd, buf); |
34 | 0 | buf += 1; |
35 | 0 | header->flags = bfd_get_8 (abfd, buf); |
36 | 0 | buf += 1; |
37 | 0 | header->hdr_len = bfd_get_32 (abfd, buf); |
38 | 0 | buf += 4; |
39 | 0 | header->type_off = bfd_get_32 (abfd, buf); |
40 | 0 | buf += 4; |
41 | 0 | header->type_len = bfd_get_32 (abfd, buf); |
42 | 0 | buf += 4; |
43 | 0 | header->str_off = bfd_get_32 (abfd, buf); |
44 | 0 | buf += 4; |
45 | 0 | header->str_len = bfd_get_32 (abfd, buf); |
46 | |
|
47 | 0 | return header; |
48 | 0 | } |
49 | | |
50 | | struct btf_type * |
51 | | btf_read_type (bfd *abfd, bfd_byte *buf) |
52 | 0 | { |
53 | 0 | struct btf_type *typ = bfd_zmalloc (sizeof (struct btf_type)); |
54 | |
|
55 | 0 | typ->name_off = bfd_get_32 (abfd, buf); |
56 | 0 | buf += 4; |
57 | 0 | typ->info = bfd_get_32 (abfd, buf); |
58 | 0 | buf += 4; |
59 | 0 | typ->size = bfd_get_32 (abfd, buf); |
60 | |
|
61 | 0 | return typ; |
62 | 0 | } |
63 | | |
64 | | struct btf_member * |
65 | | btf_read_member (bfd *abfd, bfd_byte *buf) |
66 | 0 | { |
67 | 0 | struct btf_member *member = bfd_zmalloc (sizeof (struct btf_member)); |
68 | |
|
69 | 0 | member->name_off = bfd_get_32 (abfd, buf); |
70 | 0 | buf += 4; |
71 | 0 | member->type = bfd_get_32 (abfd, buf); |
72 | 0 | buf += 4; |
73 | 0 | member->offset = bfd_get_32 (abfd, buf); |
74 | |
|
75 | 0 | return member; |
76 | 0 | } |
77 | | |
78 | | struct btf_decl_tag * |
79 | | btf_read_decl_tag (bfd *abfd, bfd_byte *buf) |
80 | 0 | { |
81 | 0 | struct btf_decl_tag *decl_tag = bfd_zmalloc (sizeof (struct btf_decl_tag)); |
82 | |
|
83 | 0 | decl_tag->component_idx = bfd_get_32 (abfd, buf); |
84 | 0 | return decl_tag; |
85 | 0 | } |
86 | | |
87 | | struct btf_var_secinfo * |
88 | | btf_read_var_secinfo (bfd *abfd, bfd_byte *buf) |
89 | 0 | { |
90 | 0 | struct btf_var_secinfo *var_secinfo = bfd_zmalloc (sizeof (struct btf_var_secinfo)); |
91 | |
|
92 | 0 | var_secinfo->type = bfd_get_32 (abfd, buf); |
93 | 0 | buf += 4; |
94 | 0 | var_secinfo->offset = bfd_get_32 (abfd, buf); |
95 | 0 | buf += 4; |
96 | 0 | var_secinfo->size = bfd_get_32 (abfd, buf); |
97 | |
|
98 | 0 | return var_secinfo; |
99 | 0 | } |
100 | | |
101 | | struct btf_var * |
102 | | btf_read_var (bfd *abfd, bfd_byte *buf) |
103 | 0 | { |
104 | 0 | struct btf_var *var = bfd_zmalloc (sizeof (struct btf_var)); |
105 | |
|
106 | 0 | var->linkage = bfd_get_32 (abfd, buf); |
107 | 0 | return var; |
108 | 0 | } |
109 | | |
110 | | struct btf_param * |
111 | | btf_read_param (bfd *abfd, bfd_byte *buf) |
112 | 0 | { |
113 | 0 | struct btf_param *param = bfd_zmalloc (sizeof (struct btf_param)); |
114 | |
|
115 | 0 | param->name_off = bfd_get_32 (abfd, buf); |
116 | 0 | buf += 4; |
117 | 0 | param->type = bfd_get_32 (abfd, buf); |
118 | |
|
119 | 0 | return param; |
120 | 0 | } |
121 | | |
122 | | struct btf_enum64 * |
123 | | btf_read_enum64 (bfd *abfd, bfd_byte *buf) |
124 | 0 | { |
125 | 0 | struct btf_enum64 *enum64 = bfd_zmalloc (sizeof (struct btf_enum64)); |
126 | |
|
127 | 0 | enum64->name_off = bfd_get_32 (abfd, buf); |
128 | 0 | buf += 4; |
129 | 0 | enum64->val_lo32 = bfd_get_32 (abfd, buf); |
130 | 0 | buf += 4; |
131 | 0 | enum64->val_hi32 = bfd_get_32 (abfd, buf); |
132 | |
|
133 | 0 | return enum64; |
134 | 0 | } |
135 | | |
136 | | struct btf_enum * |
137 | | btf_read_enum (bfd *abfd, bfd_byte *buf) |
138 | 0 | { |
139 | 0 | struct btf_enum *anenum = bfd_zmalloc (sizeof (struct btf_enum)); |
140 | |
|
141 | 0 | anenum->name_off = bfd_get_32 (abfd, buf); |
142 | 0 | buf += 4; |
143 | 0 | anenum->val = bfd_get_32 (abfd, buf); |
144 | |
|
145 | 0 | return anenum; |
146 | 0 | } |
147 | | |
148 | | struct btf_array * |
149 | | btf_read_array (bfd *abfd, bfd_byte *buf) |
150 | 0 | { |
151 | 0 | struct btf_array *array = bfd_zmalloc (sizeof (struct btf_array)); |
152 | |
|
153 | 0 | array->type = bfd_get_32 (abfd, buf); |
154 | 0 | buf += 4; |
155 | 0 | array->index_type = bfd_get_32 (abfd, buf); |
156 | 0 | buf += 4; |
157 | 0 | array->nelems = bfd_get_32 (abfd, buf); |
158 | |
|
159 | 0 | return array; |
160 | 0 | } |
161 | | |
162 | | uint32_t |
163 | | btf_read_integral (bfd *abfd, bfd_byte *buf) |
164 | 0 | { |
165 | 0 | return bfd_get_32 (abfd, buf); |
166 | 0 | } |
167 | | |
168 | | bfd_vma |
169 | | btf_type_size (struct btf_type *entry) |
170 | 0 | { |
171 | 0 | bfd_vma size = sizeof (struct btf_type); |
172 | 0 | uint16_t vlen = BTF_INFO_VLEN (entry->info); |
173 | |
|
174 | 0 | switch (BTF_INFO_KIND (entry->info)) |
175 | 0 | { |
176 | 0 | case BTF_KIND_INT: |
177 | 0 | size += 4; |
178 | 0 | break; |
179 | 0 | case BTF_KIND_ARRAY: |
180 | 0 | size += sizeof (struct btf_array); |
181 | 0 | break; |
182 | 0 | case BTF_KIND_ENUM: |
183 | 0 | size += sizeof (struct btf_enum) * vlen; |
184 | 0 | break; |
185 | 0 | case BTF_KIND_ENUM64: |
186 | 0 | size += sizeof (struct btf_enum64) * vlen; |
187 | 0 | break; |
188 | 0 | case BTF_KIND_FUNC_PROTO: |
189 | 0 | size += sizeof (struct btf_param) * vlen; |
190 | 0 | break; |
191 | 0 | case BTF_KIND_VAR: |
192 | 0 | size += sizeof (struct btf_var); |
193 | 0 | break; |
194 | 0 | case BTF_KIND_UNION: |
195 | | /* Fallthrough. */ |
196 | 0 | case BTF_KIND_STRUCT: |
197 | 0 | size += sizeof (struct btf_member) * vlen; |
198 | 0 | break; |
199 | 0 | case BTF_KIND_DATASEC: |
200 | 0 | size += sizeof (struct btf_var_secinfo) * vlen; |
201 | 0 | break; |
202 | 0 | case BTF_KIND_DECL_TAG: |
203 | 0 | size += sizeof (struct btf_decl_tag); |
204 | 0 | break; |
205 | 0 | default: |
206 | | /* No entry specific additional data follows this entry. */ |
207 | 0 | break; |
208 | 0 | } |
209 | | |
210 | 0 | return size; |
211 | 0 | } |
212 | | |
213 | | bool |
214 | | btf_map (bfd *abfd, asection *sec, btf_type_map_cb cb) |
215 | 0 | { |
216 | 0 | bfd_byte *btfdata = NULL; |
217 | 0 | struct btf_header *header = NULL; |
218 | 0 | bfd_size_type section_size = bfd_section_size (sec); |
219 | |
|
220 | 0 | if (!bfd_malloc_and_get_section (abfd, sec, &btfdata)) |
221 | 0 | goto error; |
222 | | |
223 | 0 | header = btf_read_header (abfd, btfdata); |
224 | 0 | if (header->magic != 0xeb9f) |
225 | 0 | goto error; |
226 | | |
227 | 0 | if (header->version != 1) |
228 | 0 | goto error; |
229 | | |
230 | | /* Make sure the BTF regions specified by the header (entries and |
231 | | string table) fall within the section. A corrupted header may |
232 | | lead to a buffer overflow below. */ |
233 | | |
234 | 0 | if (header->hdr_len + header->str_off + header->str_len > section_size |
235 | 0 | || header->hdr_len + header->type_off + header->type_len > section_size) |
236 | 0 | goto error; |
237 | | |
238 | | /* Iterate over all the entries in the section and invoke the user |
239 | | provided callback. */ |
240 | 0 | { |
241 | 0 | bfd_byte *str_base = btfdata + header->hdr_len + header->str_off; |
242 | 0 | bfd_byte *type_base = btfdata + header->hdr_len + header->type_off; |
243 | 0 | bfd_byte *type_off = type_base; |
244 | 0 | uint32_t type_id = 1; |
245 | |
|
246 | 0 | while (type_off < type_base + header->type_len) |
247 | 0 | { |
248 | 0 | struct btf_type *t = btf_read_type (abfd, type_off); |
249 | |
|
250 | 0 | (*cb) (abfd, btfdata, |
251 | 0 | str_base - btfdata, |
252 | 0 | type_off - btfdata, |
253 | 0 | type_id++, t); |
254 | 0 | type_off += btf_type_size (t); |
255 | 0 | free (t); |
256 | 0 | } |
257 | 0 | } |
258 | |
|
259 | 0 | free (btfdata); |
260 | 0 | return true; |
261 | | |
262 | 0 | error: |
263 | 0 | free (btfdata); |
264 | | return false; |
265 | 0 | } |