| 1 |
/* BPF Compile Once - Run Everywhere (CO-RE) support. |
| 2 |
Copyright (C) 2021-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify it |
| 7 |
under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 3, or (at your option) |
| 9 |
any later version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with GCC; see the file COPYING3. If not see |
| 18 |
<http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#define IN_TARGET_CODE 1 |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
#include "system.h" |
| 24 |
#include "coretypes.h" |
| 25 |
#include "target.h" |
| 26 |
#include "memmodel.h" |
| 27 |
#include "tm_p.h" |
| 28 |
#include "output.h" |
| 29 |
#include "dwarf2asm.h" |
| 30 |
#include "ctfc.h" |
| 31 |
#include "btf.h" |
| 32 |
#include "rtl.h" |
| 33 |
#include "tree-pretty-print.h" |
| 34 |
#include "cgraph.h" |
| 35 |
|
| 36 |
#include "btfext-out.h" |
| 37 |
|
| 38 |
/* This file contains data structures and routines for construction and output |
| 39 |
of BPF Compile Once - Run Everywhere (BPF CO-RE) information. |
| 40 |
|
| 41 |
eBPF programs written in C usually include Linux kernel headers, so that |
| 42 |
they may interact with kernel data structures in a useful way. This |
| 43 |
intrudces two major portability issues: |
| 44 |
|
| 45 |
1. Kernel data structures regularly change, with fields added, moved or |
| 46 |
deleted between versions. An eBPF program cannot in general be expected |
| 47 |
to run on any systems which does not share an identical kernel version to |
| 48 |
the system on which it was compiled. |
| 49 |
|
| 50 |
2. Included kernel headers (and used data structures) may be internal, not |
| 51 |
exposed in an userspace API, and therefore target-specific. An eBPF |
| 52 |
program compiled on an x86_64 machine will include x86_64 kernel headers. |
| 53 |
The resulting program may not run well (or at all) in machines of |
| 54 |
another architecture. |
| 55 |
|
| 56 |
BPF CO-RE is designed to solve the first issue by leveraging the BPF loader |
| 57 |
to adjust references to kernel data structures made by the program as-needed |
| 58 |
according to versions of structures actually present on the host kernel. |
| 59 |
|
| 60 |
To achieve this, additional information is placed in a ".BTF.ext" section. |
| 61 |
This information tells the loader which references will require adjusting, |
| 62 |
and how to perform each necessary adjustment. |
| 63 |
|
| 64 |
For any access to a data structure which may require load-time adjustment, |
| 65 |
the following information is recorded (making up a CO-RE relocation record): |
| 66 |
- The BTF type ID of the outermost structure which is accessed. |
| 67 |
- An access string encoding the accessed member via a series of member and |
| 68 |
array indexes. These indexes are used to look up detailed BTF information |
| 69 |
about the member. |
| 70 |
- The offset of the appropriate instruction to patch in the BPF program. |
| 71 |
- An integer specifying what kind of relocation to perform. |
| 72 |
|
| 73 |
A CO-RE-capable BPF loader reads this information together with the BTF |
| 74 |
information of the program, compares it against BTF information of the host |
| 75 |
kernel, and determines the appropriate way to patch the specified |
| 76 |
instruction. |
| 77 |
|
| 78 |
Once all CO-RE relocations are resolved, the program is loaded and verified |
| 79 |
as usual. The process can be summarized with the following diagram: |
| 80 |
|
| 81 |
+------------+ |
| 82 |
| C compiler | |
| 83 |
+-----+------+ |
| 84 |
| BPF + BTF + CO-RE relocations |
| 85 |
v |
| 86 |
+------------+ |
| 87 |
+--->| BPF loader | |
| 88 |
| +-----+------+ |
| 89 |
| | BPF (adapted) |
| 90 |
BTF | v |
| 91 |
| +------------+ |
| 92 |
+----+ Kernel | |
| 93 |
+------------+ |
| 94 |
|
| 95 |
Note that a single ELF object may contain multiple eBPF programs. As a |
| 96 |
result, a single .BTF.ext section can contain CO-RE relocations for multiple |
| 97 |
programs in distinct sections. */ |
| 98 |
|
| 99 |
/* BTF.ext debug info section. */ |
| 100 |
static GTY (()) section * btf_ext_info_section; |
| 101 |
|
| 102 |
#ifndef BTF_EXT_INFO_SECTION_NAME |
| 103 |
#define BTF_EXT_INFO_SECTION_NAME ".BTF.ext" |
| 104 |
#endif |
| 105 |
#define BTF_EXT_INFO_SECTION_FLAGS (SECTION_DEBUG) |
| 106 |
|
| 107 |
#ifndef BTF_EXT_INFO_SECTION_LABEL |
| 108 |
#define BTF_EXT_INFO_SECTION_LABEL "Lbtfext" |
| 109 |
#endif |
| 110 |
|
| 111 |
#define MAX_BTF_EXT_LABEL_BYTES 40 |
| 112 |
static char btf_ext_info_section_label[MAX_BTF_EXT_LABEL_BYTES]; |
| 113 |
|
| 114 |
/* A funcinfo record, in the .BTF.ext funcinfo section. */ |
| 115 |
struct GTY ((chain_next ("%h.next"))) btf_ext_funcinfo |
| 116 |
{ |
| 117 |
uint32_t type; /* Type ID of a BTF_KIND_FUNC type. */ |
| 118 |
const char *fnname; |
| 119 |
const char *label; |
| 120 |
|
| 121 |
struct btf_ext_funcinfo *next; /* Linked list to collect func_info elems. */ |
| 122 |
}; |
| 123 |
|
| 124 |
/* A lineinfo record, in the .BTF.ext lineinfo section. */ |
| 125 |
struct GTY ((chain_next ("%h.next"))) btf_ext_lineinfo |
| 126 |
{ |
| 127 |
uint32_t insn_off; /* Offset of the instruction. */ |
| 128 |
uint32_t file_name_off; /* Offset of file name in BTF string table. */ |
| 129 |
uint32_t line_off; /* Offset of source line in BTF string table. */ |
| 130 |
uint32_t line_col; /* Line number (bits 31-11) and column (11-0). */ |
| 131 |
|
| 132 |
struct btf_ext_lineinfo *next; /* Linked list to collect line_info elems. */ |
| 133 |
}; |
| 134 |
|
| 135 |
/* Internal representation of a BPF CO-RE relocation record. */ |
| 136 |
struct GTY ((chain_next ("%h.next"))) btf_ext_core_reloc { |
| 137 |
ctf_dtdef_ref bpfcr_type; /* BTF type involved in relocation. */ |
| 138 |
unsigned int bpfcr_astr_off; /* Offset of access string in .BTF |
| 139 |
string table. */ |
| 140 |
rtx_code_label * bpfcr_insn_label; /* RTX label attached to instruction |
| 141 |
to patch. */ |
| 142 |
enum btf_core_reloc_kind bpfcr_kind; /* Kind of relocation to perform. */ |
| 143 |
|
| 144 |
struct { |
| 145 |
const char *accessor_str; |
| 146 |
tree type; |
| 147 |
} info; |
| 148 |
|
| 149 |
struct btf_ext_core_reloc *next; |
| 150 |
}; |
| 151 |
|
| 152 |
/* Main data structure to keep .BTF.ext section data. */ |
| 153 |
struct GTY ((chain_next ("%h.next"))) btf_ext_info_sec { |
| 154 |
const char *sec_name; |
| 155 |
uint32_t sec_name_off; /* offset to section name. */ |
| 156 |
|
| 157 |
struct { |
| 158 |
uint32_t num_info; |
| 159 |
struct btf_ext_funcinfo *head; |
| 160 |
} func_info; |
| 161 |
struct { |
| 162 |
uint32_t num_info; |
| 163 |
struct btf_ext_lineinfo *head; |
| 164 |
} line_info; |
| 165 |
struct { |
| 166 |
uint32_t num_info; |
| 167 |
struct btf_ext_core_reloc *head; |
| 168 |
} core_info; |
| 169 |
|
| 170 |
struct btf_ext_info_sec *next; |
| 171 |
}; |
| 172 |
|
| 173 |
static GTY (()) struct btf_ext_info_sec *btf_ext = NULL; |
| 174 |
|
| 175 |
/* Helper function to add a section structure to the linked list with entry |
| 176 |
point in info static variable. */ |
| 177 |
|
| 178 |
static struct btf_ext_info_sec * |
| 179 |
btfext_info_sec_find_or_add (const char *sec_name, bool add) |
| 180 |
{ |
| 181 |
struct btf_ext_info_sec **tmp = &btf_ext; |
| 182 |
|
| 183 |
while (*tmp != NULL) |
| 184 |
{ |
| 185 |
if (strcmp ((*tmp)->sec_name, sec_name) == 0) |
| 186 |
return *tmp; |
| 187 |
tmp = &((*tmp)->next); |
| 188 |
} |
| 189 |
|
| 190 |
if (add == false) |
| 191 |
return NULL; |
| 192 |
|
| 193 |
struct btf_ext_info_sec *ret = ggc_cleared_alloc<struct btf_ext_info_sec> (); |
| 194 |
*tmp = ret; |
| 195 |
|
| 196 |
/* Set data for section info. */ |
| 197 |
ret->sec_name = sec_name; |
| 198 |
ret->sec_name_off = btf_ext_add_string (sec_name); |
| 199 |
|
| 200 |
return ret; |
| 201 |
} |
| 202 |
|
| 203 |
#define SEARCH_NODE_AND_RETURN(TYPE, FIELD, CONDITION) __extension__ ({ \ |
| 204 |
TYPE **head = &(FIELD); \ |
| 205 |
while (*head != NULL) \ |
| 206 |
{ \ |
| 207 |
if (CONDITION) \ |
| 208 |
return (*head); \ |
| 209 |
head = &((*head)->next); \ |
| 210 |
} \ |
| 211 |
head; \ |
| 212 |
}) |
| 213 |
|
| 214 |
/* Function to create or find a funcinfo node in info. */ |
| 215 |
|
| 216 |
static struct btf_ext_funcinfo * |
| 217 |
bpf_create_or_find_funcinfo (const char *fnname, const char *sec_name, |
| 218 |
btf_ext_info_sec **in_sec = NULL) |
| 219 |
{ |
| 220 |
struct btf_ext_info_sec *sec_elem = |
| 221 |
btfext_info_sec_find_or_add (sec_name, true); |
| 222 |
|
| 223 |
if (in_sec != NULL) |
| 224 |
*in_sec = sec_elem; |
| 225 |
|
| 226 |
struct btf_ext_funcinfo **head = |
| 227 |
SEARCH_NODE_AND_RETURN(struct btf_ext_funcinfo, |
| 228 |
sec_elem->func_info.head, |
| 229 |
strcmp ((*head)->fnname, fnname) == 0); |
| 230 |
|
| 231 |
*head = ggc_cleared_alloc<struct btf_ext_funcinfo> (); |
| 232 |
(*head)->fnname = fnname; |
| 233 |
(*head)->label = NULL; |
| 234 |
|
| 235 |
return *head; |
| 236 |
} |
| 237 |
|
| 238 |
/* Function to create a core_reloc node in info. */ |
| 239 |
|
| 240 |
static struct btf_ext_core_reloc * |
| 241 |
bpf_create_core_reloc (const char *sec_name, |
| 242 |
struct btf_ext_info_sec **in_sec = NULL) |
| 243 |
{ |
| 244 |
struct btf_ext_info_sec *sec_elem = |
| 245 |
btfext_info_sec_find_or_add (sec_name, true); |
| 246 |
|
| 247 |
if (in_sec != NULL) |
| 248 |
*in_sec = sec_elem; |
| 249 |
|
| 250 |
struct btf_ext_core_reloc **head = |
| 251 |
SEARCH_NODE_AND_RETURN(struct btf_ext_core_reloc, |
| 252 |
sec_elem->core_info.head, |
| 253 |
false); |
| 254 |
|
| 255 |
*head = ggc_cleared_alloc<struct btf_ext_core_reloc> (); |
| 256 |
|
| 257 |
return *head; |
| 258 |
} |
| 259 |
|
| 260 |
/* String caching to avoid repeated strings added to BTF string table. */ |
| 261 |
struct GTY((chain_next ("%h.next"))) string_cache { |
| 262 |
const char *str; |
| 263 |
unsigned int offset; |
| 264 |
struct string_cache *next; |
| 265 |
}; |
| 266 |
static GTY(()) struct string_cache *btf_ext_strings = NULL; |
| 267 |
|
| 268 |
unsigned int |
| 269 |
btf_ext_add_string (const char *str) |
| 270 |
{ |
| 271 |
ctf_container_ref ctfc = ctf_get_tu_ctfc (); |
| 272 |
struct string_cache **tmp = &btf_ext_strings; |
| 273 |
while (*tmp != NULL) |
| 274 |
{ |
| 275 |
if (strcmp ((*tmp)->str, str) == 0) |
| 276 |
return (*tmp)->offset; |
| 277 |
tmp = &((*tmp)->next); |
| 278 |
} |
| 279 |
|
| 280 |
*tmp = ggc_cleared_alloc<struct string_cache> (); |
| 281 |
(*tmp)->str = ggc_strdup (str); |
| 282 |
ctf_add_string (ctfc, (*tmp)->str, &((*tmp)->offset), CTF_AUX_STRTAB); |
| 283 |
|
| 284 |
return (*tmp)->offset; |
| 285 |
} |
| 286 |
|
| 287 |
/* Create a new BPF CO-RE relocation record, and add it to the appropriate |
| 288 |
CO-RE section. */ |
| 289 |
void |
| 290 |
bpf_core_reloc_add (const tree type, const char * section_name, |
| 291 |
const char *accessor, |
| 292 |
rtx_code_label *label, |
| 293 |
enum btf_core_reloc_kind kind) |
| 294 |
{ |
| 295 |
struct btf_ext_info_sec *sec = NULL; |
| 296 |
struct btf_ext_core_reloc *bpfcr = bpf_create_core_reloc (section_name, &sec); |
| 297 |
|
| 298 |
ctf_container_ref ctfc = ctf_get_tu_ctfc (); |
| 299 |
ctf_dtdef_ref dtd = ctf_lookup_tree_type (ctfc, type); |
| 300 |
|
| 301 |
/* Make sure CO-RE type is never the const or volatile version. */ |
| 302 |
if ((btf_dtd_kind (dtd) == BTF_KIND_CONST |
| 303 |
|| btf_dtd_kind (dtd) == BTF_KIND_VOLATILE) |
| 304 |
&& kind >= BPF_RELO_FIELD_BYTE_OFFSET |
| 305 |
&& kind <= BPF_RELO_FIELD_RSHIFT_U64) |
| 306 |
dtd = dtd->ref_type; |
| 307 |
|
| 308 |
/* Buffer the access string in the auxiliary strtab. */ |
| 309 |
bpfcr->bpfcr_astr_off = 0; |
| 310 |
gcc_assert (accessor != NULL); |
| 311 |
bpfcr->bpfcr_astr_off = btf_ext_add_string (accessor); |
| 312 |
|
| 313 |
bpfcr->bpfcr_type = dtd; |
| 314 |
bpfcr->bpfcr_insn_label = label; |
| 315 |
bpfcr->bpfcr_kind = kind; |
| 316 |
|
| 317 |
bpfcr->info.accessor_str = accessor; |
| 318 |
bpfcr->info.type = type; |
| 319 |
|
| 320 |
sec->core_info.num_info += 1; |
| 321 |
} |
| 322 |
|
| 323 |
/* Return the 0-based index of the field NODE in its containing struct or union |
| 324 |
type. */ |
| 325 |
|
| 326 |
int |
| 327 |
bpf_core_get_sou_member_index (ctf_container_ref ctfc, const tree node) |
| 328 |
{ |
| 329 |
if (TREE_CODE (node) == FIELD_DECL) |
| 330 |
{ |
| 331 |
const tree container = DECL_CONTEXT (node); |
| 332 |
|
| 333 |
/* Lookup the CTF type info for the containing type. */ |
| 334 |
dw_die_ref die = lookup_type_die (container); |
| 335 |
if (die == NULL) |
| 336 |
return -1; |
| 337 |
|
| 338 |
ctf_dtdef_ref dtd = ctf_dtd_lookup (ctfc, die); |
| 339 |
if (dtd == NULL) |
| 340 |
return -1; |
| 341 |
|
| 342 |
unsigned int kind = CTF_V2_INFO_KIND (dtd->dtd_data.ctti_info); |
| 343 |
if (kind != CTF_K_STRUCT && kind != CTF_K_UNION) |
| 344 |
return -1; |
| 345 |
|
| 346 |
tree field = TYPE_FIELDS (container); |
| 347 |
int i = 0; |
| 348 |
ctf_dmdef_t * dmd; |
| 349 |
for (dmd = dtd->dtd_u.dtu_members; |
| 350 |
dmd != NULL; dmd = (ctf_dmdef_t *) ctf_dmd_list_next (dmd)) |
| 351 |
{ |
| 352 |
bool field_has_btf = (dmd->dmd_type |
| 353 |
&& dmd->dmd_type->dtd_type <= BTF_MAX_TYPE); |
| 354 |
|
| 355 |
if (field == node) |
| 356 |
return field_has_btf ? i : -1; |
| 357 |
|
| 358 |
if (field_has_btf) |
| 359 |
i++; |
| 360 |
|
| 361 |
field = DECL_CHAIN (field); |
| 362 |
} |
| 363 |
} |
| 364 |
return -1; |
| 365 |
} |
| 366 |
|
| 367 |
/* Helper function to check if a particular named function exists as a |
| 368 |
BTF_KIND_FUNC type record. */ |
| 369 |
|
| 370 |
static bool |
| 371 |
btf_funcinfo_type_callback (ctf_dtdef_ref func, void *data) |
| 372 |
{ |
| 373 |
struct btf_ext_funcinfo *info = (struct btf_ext_funcinfo *) data; |
| 374 |
if (strcmp (func->dtd_name, info->fnname) == 0) |
| 375 |
{ |
| 376 |
uint32_t type = func->dtd_type; |
| 377 |
info->type = type; |
| 378 |
return true; |
| 379 |
} |
| 380 |
return false; |
| 381 |
} |
| 382 |
|
| 383 |
/* Entry point function to add a func_info in local data structures |
| 384 |
represented by info static variable. |
| 385 |
This function is used in bpf.cc. */ |
| 386 |
|
| 387 |
struct btf_ext_funcinfo * |
| 388 |
btf_add_func_info_for (tree decl, const char *label) |
| 389 |
{ |
| 390 |
const char *fnname = IDENTIFIER_POINTER (DECL_NAME (decl)); |
| 391 |
const char *sec_name = decl_section_name (decl); |
| 392 |
|
| 393 |
/* Recover the original function name, which may have been mangled by |
| 394 |
optimizations. */ |
| 395 |
const char *cp_ptr = strstr (fnname, "."); |
| 396 |
if (cp_ptr != NULL) |
| 397 |
{ |
| 398 |
char new_name[100]; |
| 399 |
strcpy (new_name, fnname); |
| 400 |
int pos = cp_ptr - fnname; |
| 401 |
new_name[pos] = 0; |
| 402 |
fnname = ggc_strdup (new_name); |
| 403 |
} |
| 404 |
|
| 405 |
if (sec_name == NULL) |
| 406 |
sec_name = ".text"; |
| 407 |
|
| 408 |
struct btf_ext_info_sec *sec = NULL; |
| 409 |
struct btf_ext_funcinfo *info = |
| 410 |
bpf_create_or_find_funcinfo (fnname, sec_name, &sec); |
| 411 |
|
| 412 |
info->label = label; |
| 413 |
return info; |
| 414 |
} |
| 415 |
|
| 416 |
/* This function traverses all func_info entries and verified they do have a |
| 417 |
BTF_KIND_FUNC type record associated. If they do not it is marked as |
| 418 |
invalided by clearing the associated label. */ |
| 419 |
|
| 420 |
static void |
| 421 |
btf_validate_funcinfo (btf_ext_info_sec *sec) |
| 422 |
{ |
| 423 |
while (sec != NULL) |
| 424 |
{ |
| 425 |
struct btf_ext_funcinfo *funcinfo = sec->func_info.head; |
| 426 |
while (funcinfo != NULL) |
| 427 |
{ |
| 428 |
bool found = traverse_btf_func_types (btf_funcinfo_type_callback, |
| 429 |
funcinfo); |
| 430 |
if (found == true) |
| 431 |
sec->func_info.num_info += 1; |
| 432 |
else |
| 433 |
funcinfo->label = NULL; |
| 434 |
|
| 435 |
funcinfo = funcinfo->next; |
| 436 |
} |
| 437 |
sec = sec->next; |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
/* Compute the section size in section for func_info, line_info and core_info |
| 442 |
regions of .BTF.ext. */ |
| 443 |
|
| 444 |
static void |
| 445 |
btf_ext_info_len (uint32_t *fi_len, uint32_t *li_len, uint32_t *cr_len) |
| 446 |
{ |
| 447 |
*fi_len = *li_len = *cr_len = 0; |
| 448 |
struct btf_ext_info_sec *tmp = btf_ext; |
| 449 |
if (tmp != NULL) |
| 450 |
while (tmp != NULL) |
| 451 |
{ |
| 452 |
/* Size computation does 8 bytes per section entry plus num_info of the |
| 453 |
* respective structure size: |
| 454 |
- 8 bytes for func_info, |
| 455 |
- 16 bytes for both line_info and core_info. */ |
| 456 |
if (tmp->func_info.num_info > 0) |
| 457 |
*fi_len += 8 + (8 * tmp->func_info.num_info); |
| 458 |
if (tmp->line_info.num_info > 0) |
| 459 |
*li_len += 8 + (16 * tmp->line_info.num_info); |
| 460 |
if (tmp->core_info.num_info > 0) |
| 461 |
*cr_len += 8 + (16 * tmp->core_info.num_info); |
| 462 |
tmp = tmp->next; |
| 463 |
} |
| 464 |
|
| 465 |
/* If there are entries within the regions, add 4 bytes to set the header of |
| 466 |
the respective sections that contains the size for each of the entry. */ |
| 467 |
*fi_len += *fi_len != 0 ? 4 : 0; |
| 468 |
*li_len += *li_len != 0 ? 4 : 0; |
| 469 |
*cr_len += *cr_len != 0 ? 4 : 0; |
| 470 |
} |
| 471 |
|
| 472 |
/* Compute and output the header of a .BTF.ext debug info section. */ |
| 473 |
|
| 474 |
static void |
| 475 |
output_btfext_header (void) |
| 476 |
{ |
| 477 |
switch_to_section (btf_ext_info_section); |
| 478 |
ASM_OUTPUT_LABEL (asm_out_file, btf_ext_info_section_label); |
| 479 |
|
| 480 |
dw2_asm_output_data (2, BTF_MAGIC, "btf_magic"); |
| 481 |
dw2_asm_output_data (1, BTF_VERSION, "btfext_version"); |
| 482 |
dw2_asm_output_data (1, 0, "btfext_flags"); |
| 483 |
dw2_asm_output_data (4, sizeof (struct btf_ext_header), "btfext_hdr_len"); |
| 484 |
|
| 485 |
btf_validate_funcinfo (btf_ext); |
| 486 |
|
| 487 |
uint32_t func_info_len = 0; |
| 488 |
uint32_t line_info_len = 0; |
| 489 |
uint32_t core_info_len = 0; |
| 490 |
btf_ext_info_len (&func_info_len, &line_info_len, &core_info_len); |
| 491 |
|
| 492 |
if (!TARGET_BPF_CORE) |
| 493 |
core_info_len = 0; |
| 494 |
|
| 495 |
uint32_t func_info_off = 0; |
| 496 |
uint32_t line_info_off = func_info_len; |
| 497 |
uint32_t core_info_off = line_info_off + line_info_len; |
| 498 |
|
| 499 |
dw2_asm_output_data (4, func_info_off, "func_info_offset"); |
| 500 |
dw2_asm_output_data (4, func_info_len, "func_info_len"); |
| 501 |
|
| 502 |
dw2_asm_output_data (4, line_info_off, "line_info_offset"); |
| 503 |
dw2_asm_output_data (4, line_info_len, "line_info_len"); |
| 504 |
|
| 505 |
dw2_asm_output_data (4, core_info_off, "core_relo_offset"); |
| 506 |
dw2_asm_output_data (4, core_info_len, "core_relo_len"); |
| 507 |
} |
| 508 |
|
| 509 |
/* Outputs func_info region on .BTF.ext. */ |
| 510 |
|
| 511 |
static void |
| 512 |
output_btfext_func_info (struct btf_ext_info_sec *sec) |
| 513 |
{ |
| 514 |
unsigned int str_aux_off = ctfc_get_strtab_len (ctf_get_tu_ctfc (), |
| 515 |
CTF_STRTAB); |
| 516 |
bool executed = false; |
| 517 |
while (sec != NULL) |
| 518 |
{ |
| 519 |
uint32_t count = 0; |
| 520 |
if (sec->func_info.num_info > 0) |
| 521 |
{ |
| 522 |
if (executed == false && (executed = true)) |
| 523 |
dw2_asm_output_data (4, 8, "FuncInfo entry size"); |
| 524 |
dw2_asm_output_data (4, sec->sec_name_off + str_aux_off, |
| 525 |
"FuncInfo section string for %s", |
| 526 |
sec->sec_name); |
| 527 |
dw2_asm_output_data (4, sec->func_info.num_info, "Number of entries"); |
| 528 |
|
| 529 |
struct btf_ext_funcinfo *elem = sec->func_info.head; |
| 530 |
while (elem != NULL) |
| 531 |
{ |
| 532 |
if (elem->label != NULL) |
| 533 |
{ |
| 534 |
count += 1; |
| 535 |
dw2_asm_output_offset (4, elem->label, |
| 536 |
NULL, "label for function %s", elem->fnname); |
| 537 |
dw2_asm_output_data (4, elem->type, "btf_type_id"); |
| 538 |
} |
| 539 |
elem = elem->next; |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
gcc_assert (count == sec->func_info.num_info); |
| 544 |
sec = sec->next; |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
/* Output all CO-RE relocation sections. */ |
| 549 |
|
| 550 |
static void |
| 551 |
output_btfext_core_sections (void) |
| 552 |
{ |
| 553 |
struct btf_ext_info_sec *sec = btf_ext; |
| 554 |
unsigned int str_aux_off = ctfc_get_strtab_len (ctf_get_tu_ctfc (), |
| 555 |
CTF_STRTAB); |
| 556 |
bool executed = false; |
| 557 |
while (sec != NULL) |
| 558 |
{ |
| 559 |
uint32_t count = 0; |
| 560 |
if (sec->core_info.num_info > 0) |
| 561 |
{ |
| 562 |
if (executed == false && (executed = true)) |
| 563 |
dw2_asm_output_data (4, 16, "CoreInfo entry size"); |
| 564 |
dw2_asm_output_data (4, sec->sec_name_off + str_aux_off, |
| 565 |
"CoreInfo section string for %s", |
| 566 |
sec->sec_name); |
| 567 |
dw2_asm_output_data (4, sec->core_info.num_info, "Number of entries"); |
| 568 |
|
| 569 |
struct btf_ext_core_reloc *bpfcr = sec->core_info.head; |
| 570 |
while (bpfcr != NULL) |
| 571 |
{ |
| 572 |
count += 1; |
| 573 |
dw2_assemble_integer (4, |
| 574 |
gen_rtx_LABEL_REF (Pmode, bpfcr->bpfcr_insn_label)); |
| 575 |
fprintf (asm_out_file, "\t%s%s\n", |
| 576 |
flag_debug_asm ? ASM_COMMENT_START : "", |
| 577 |
(flag_debug_asm ? " bpfcr_insn" : "")); |
| 578 |
|
| 579 |
/* Extract the pretty print for the type expression. */ |
| 580 |
pretty_printer pp; |
| 581 |
dump_generic_node (&pp, bpfcr->info.type, 0, |
| 582 |
TDF_VOPS|TDF_MEMSYMS|TDF_SLIM, |
| 583 |
false); |
| 584 |
char *str = xstrdup (pp_formatted_text (&pp)); |
| 585 |
|
| 586 |
uint32_t type_id = bpfcr->bpfcr_type |
| 587 |
? bpfcr->bpfcr_type->dtd_type |
| 588 |
: BTF_VOID_TYPEID; |
| 589 |
dw2_asm_output_data (4, type_id, "bpfcr_type (%s)", str); |
| 590 |
dw2_asm_output_data (4, bpfcr->bpfcr_astr_off + str_aux_off, |
| 591 |
"bpfcr_astr_off (\"%s\")", |
| 592 |
bpfcr->info.accessor_str); |
| 593 |
dw2_asm_output_data (4, bpfcr->bpfcr_kind, "bpfcr_kind"); |
| 594 |
bpfcr = bpfcr->next; |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
gcc_assert (count == sec->core_info.num_info); |
| 599 |
sec = sec->next; |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
/* Initialize sections, labels, and data structures for BTF.ext output. */ |
| 604 |
|
| 605 |
void |
| 606 |
btf_ext_init (void) |
| 607 |
{ |
| 608 |
btf_ext_info_section = get_section (BTF_EXT_INFO_SECTION_NAME, |
| 609 |
BTF_EXT_INFO_SECTION_FLAGS, NULL); |
| 610 |
|
| 611 |
ASM_GENERATE_INTERNAL_LABEL (btf_ext_info_section_label, |
| 612 |
"Lbtfext", 0); |
| 613 |
} |
| 614 |
|
| 615 |
/* Output the entire .BTF.ext section. */ |
| 616 |
|
| 617 |
void |
| 618 |
btf_ext_output (void) |
| 619 |
{ |
| 620 |
if (!ctf_get_tu_ctfc ()) |
| 621 |
return; |
| 622 |
|
| 623 |
output_btfext_header (); |
| 624 |
output_btfext_func_info (btf_ext); |
| 625 |
if (TARGET_BPF_CORE) |
| 626 |
output_btfext_core_sections (); |
| 627 |
|
| 628 |
/* Extra padding required by BPF code, in case all structures are empty. */ |
| 629 |
dw2_asm_output_data (4, 0, "Required padding by libbpf structs"); |
| 630 |
} |
| 631 |
|
| 632 |
#include "gt-btfext-out.h" |