Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0-only
2 : /*
3 : * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
4 : */
5 :
6 : #include "fixup.h"
7 :
8 : #include <errno.h>
9 : #include <stdlib.h>
10 :
11 : #include "core/dump.h"
12 : #include "core/helper.h"
13 :
14 0 : int bf_fixup_new(struct bf_fixup **fixup, enum bf_fixup_type type,
15 : size_t insn_offset, const union bf_fixup_attr *attr)
16 : {
17 0 : bf_assert(fixup);
18 :
19 0 : *fixup = calloc(1, sizeof(struct bf_fixup));
20 : if (!fixup)
21 : return -ENOMEM;
22 :
23 0 : (*fixup)->type = type;
24 0 : (*fixup)->insn = insn_offset;
25 :
26 0 : if (attr)
27 0 : (*fixup)->attr = *attr;
28 :
29 : return 0;
30 : }
31 :
32 0 : void bf_fixup_free(struct bf_fixup **fixup)
33 : {
34 0 : bf_assert(fixup);
35 :
36 0 : if (!*fixup)
37 : return;
38 :
39 : freep((void *)fixup);
40 : }
41 :
42 0 : static const char *_bf_fixup_type_to_str(enum bf_fixup_type type)
43 : {
44 : static const char *str[] = {
45 : [BF_FIXUP_TYPE_JMP_NEXT_RULE] = "BF_FIXUP_TYPE_JMP_NEXT_RULE",
46 : [BF_FIXUP_TYPE_COUNTERS_MAP_FD] = "BF_FIXUP_TYPE_COUNTERS_MAP_FD",
47 : [BF_FIXUP_TYPE_PRINTER_MAP_FD] = "BF_FIXUP_TYPE_PRINTER_MAP_FD",
48 : [BF_FIXUP_TYPE_SET_MAP_FD] = "BF_FIXUP_TYPE_SET_MAP_FD",
49 : [BF_FIXUP_ELFSTUB_CALL] = "BF_FIXUP_ELFSTUB_CALL",
50 : };
51 :
52 0 : bf_assert(0 <= type && type < _BF_FIXUP_TYPE_MAX);
53 : static_assert(ARRAY_SIZE(str) == _BF_FIXUP_TYPE_MAX);
54 :
55 0 : return str[type];
56 : }
57 :
58 0 : void bf_fixup_dump(const struct bf_fixup *fixup, prefix_t *prefix)
59 : {
60 0 : bf_assert(fixup);
61 0 : bf_assert(prefix);
62 :
63 0 : DUMP(prefix, "struct bf_fixup at %p", fixup);
64 :
65 0 : bf_dump_prefix_push(prefix);
66 :
67 0 : DUMP(prefix, "type: %s", _bf_fixup_type_to_str(fixup->type));
68 0 : DUMP(prefix, "insn: %zu", fixup->insn);
69 :
70 0 : switch (fixup->type) {
71 : case BF_FIXUP_TYPE_JMP_NEXT_RULE:
72 : case BF_FIXUP_TYPE_COUNTERS_MAP_FD:
73 : case BF_FIXUP_TYPE_PRINTER_MAP_FD:
74 : // No specific value to dump
75 : break;
76 0 : case BF_FIXUP_TYPE_SET_MAP_FD:
77 0 : DUMP(prefix, "set_index: %lu", fixup->attr.set_index);
78 : break;
79 0 : default:
80 0 : DUMP(prefix, "unsupported bf_fixup_type: %d", fixup->type);
81 : break;
82 : };
83 :
84 0 : bf_dump_prefix_pop(prefix);
85 0 : }
|