aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDouglas Raillard <douglas.raillard@arm.com>2021-10-18 14:16:21 +0100
committerArnaldo Carvalho de Melo <acme@redhat.com>2021-10-27 15:45:47 -0300
commit836c139fdf6f2b13ec2e5513df881272bb78aeb4 (patch)
tree3272fe025ec62672d16d866d0125a58c0e08b0b9
parent4db65fe0cd02b3cc49d4fc8ff6c4c21a9ddb3642 (diff)
downloadpahole-836c139fdf6f2b13ec2e5513df881272bb78aeb4.tar.gz
btf_loader: Infer alignment info
BTF does not carry alignment information, but it carries the offset in structs. This allows inferring the original alignment, yielding a C header dump that is not identical to the original C code, but is guaranteed to lead to the same memory layout. This allows using the output of pahole in another program to poke at memory, with the assurance that we will not read garbage. Note: Since the alignment is inferred from the offset, it sometimes happens that the offset was already correctly aligned, which means the inferred alignment will be smaller than in the original source. This does not impact the ability to read existing structs, but it could impact creating such struct if other client code expects higher alignment than the one exposed in the generated header. Signed-off-by: Douglas Raillard <douglas.raillard@arm.com> Cc: dwarves@vger.kernel.org [ Split from a larger patch ] Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
-rw-r--r--btf_loader.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/btf_loader.c b/btf_loader.c
index 9c2daee9..2885252c 100644
--- a/btf_loader.c
+++ b/btf_loader.c
@@ -471,10 +471,37 @@ static int btf__load_sections(struct btf *btf, struct cu *cu)
return btf__load_types(btf, cu);
}
+static uint32_t class__infer_alignment(uint32_t byte_offset,
+ uint32_t natural_alignment,
+ uint32_t smallest_offset)
+{
+ uint32_t alignment = 0;
+ uint32_t offset_delta = byte_offset - smallest_offset;
+
+ if (offset_delta) {
+ if (byte_offset % 2 == 0) {
+ /* Find the power of 2 immediately higher than
+ * offset_delta
+ */
+ alignment = 1 << (8 * sizeof(offset_delta) -
+ __builtin_clz(offset_delta));
+ } else {
+ alignment = 0;
+ }
+ }
+
+ /* Natural alignment, nothing to do */
+ if (alignment <= natural_alignment || alignment == 1)
+ alignment = 0;
+
+ return alignment;
+}
+
static int class__fixup_btf_bitfields(struct tag *tag, struct cu *cu)
{
struct class_member *pos;
struct type *tag_type = tag__type(tag);
+ uint32_t smallest_offset = 0;
type__for_each_data_member(tag_type, pos) {
struct tag *type = tag__strip_typedefs_and_modifiers(&pos->tag, cu);
@@ -508,8 +535,17 @@ static int class__fixup_btf_bitfields(struct tag *tag, struct cu *cu)
pos->byte_offset = pos->bit_offset / 8;
}
}
+
+ pos->alignment = class__infer_alignment(pos->byte_offset,
+ tag__natural_alignment(type, cu),
+ smallest_offset);
+ smallest_offset = pos->byte_offset + pos->byte_size;
}
+ tag_type->alignment = class__infer_alignment(tag_type->size,
+ tag__natural_alignment(tag, cu),
+ smallest_offset);
+
return 0;
}