From: Jiri Olsa <jolsa@kernel.org>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: dwarves@vger.kernel.org, bpf@vger.kernel.org,
Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andriin@fb.com>, Yonghong Song <yhs@fb.com>,
Hao Luo <haoluo@google.com>
Subject: [PATCH 2/3] btf_encoder: Use address size based on ELF's class
Date: Thu, 3 Dec 2020 23:06:24 +0100 [thread overview]
Message-ID: <20201203220625.3704363-3-jolsa@kernel.org> (raw)
In-Reply-To: <20201203220625.3704363-1-jolsa@kernel.org>
We can't assume the address size is always size of unsigned
long, we have to use directly the ELF's address size.
Changing addrs array to __u64 and convert 32 bit address
values when copying from ELF section.
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
btf_encoder.c | 24 +++++++++++++++++-------
1 file changed, 17 insertions(+), 7 deletions(-)
diff --git a/btf_encoder.c b/btf_encoder.c
index 0a33388db675..398be0fbf7c7 100644
--- a/btf_encoder.c
+++ b/btf_encoder.c
@@ -93,8 +93,8 @@ static int collect_function(struct btf_elf *btfe, GElf_Sym *sym)
static int addrs_cmp(const void *_a, const void *_b)
{
- const unsigned long *a = _a;
- const unsigned long *b = _b;
+ const __u64 *a = _a;
+ const __u64 *b = _b;
if (*a == *b)
return 0;
@@ -102,9 +102,10 @@ static int addrs_cmp(const void *_a, const void *_b)
}
static int get_vmlinux_addrs(struct btf_elf *btfe, struct funcs_layout *fl,
- unsigned long **paddrs, unsigned long *pcount)
+ __u64 **paddrs, __u64 *pcount)
{
- unsigned long *addrs, count, offset;
+ __u64 *addrs, count, offset;
+ unsigned int addr_size, i;
Elf_Data *data;
GElf_Shdr shdr;
Elf_Scn *sec;
@@ -128,8 +129,11 @@ static int get_vmlinux_addrs(struct btf_elf *btfe, struct funcs_layout *fl,
return -1;
}
+ /* Get address size from processed file's ELF class. */
+ addr_size = gelf_getclass(btfe->elf) == ELFCLASS32 ? 4 : 8;
+
offset = fl->mcount_start - shdr.sh_addr;
- count = (fl->mcount_stop - fl->mcount_start) / 8;
+ count = (fl->mcount_stop - fl->mcount_start) / addr_size;
data = elf_getdata(sec, 0);
if (!data) {
@@ -144,7 +148,13 @@ static int get_vmlinux_addrs(struct btf_elf *btfe, struct funcs_layout *fl,
return -1;
}
- memcpy(addrs, data->d_buf + offset, count * sizeof(addrs[0]));
+ if (addr_size == sizeof(__u64)) {
+ memcpy(addrs, data->d_buf + offset, count * addr_size);
+ } else {
+ for (i = 0; i < count; i++)
+ addrs[i] = (__u64) *((__u32 *) (data->d_buf + offset + i * addr_size));
+ }
+
*paddrs = addrs;
*pcount = count;
return 0;
@@ -152,7 +162,7 @@ static int get_vmlinux_addrs(struct btf_elf *btfe, struct funcs_layout *fl,
static int setup_functions(struct btf_elf *btfe, struct funcs_layout *fl)
{
- unsigned long *addrs, count, i;
+ __u64 *addrs, count, i;
int functions_valid = 0;
/*
--
2.26.2
next prev parent reply other threads:[~2020-12-03 22:07 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-03 22:06 [PATCHv2 0/3] btf_encoder: Detect kernel modules Jiri Olsa
2020-12-03 22:06 ` [PATCH 1/3] btf_encoder: Factor filter_functions function Jiri Olsa
2020-12-03 23:20 ` Andrii Nakryiko
2020-12-03 22:06 ` Jiri Olsa [this message]
2020-12-03 23:22 ` [PATCH 2/3] btf_encoder: Use address size based on ELF's class Andrii Nakryiko
2020-12-03 23:37 ` Jiri Olsa
2020-12-07 15:45 ` Arnaldo Carvalho de Melo
2020-12-03 22:06 ` [PATCH 3/3] btf_encoder: Detect kernel module ftrace addresses Jiri Olsa
2020-12-03 23:23 ` Andrii Nakryiko
2020-12-07 15:54 ` Arnaldo Carvalho de Melo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201203220625.3704363-3-jolsa@kernel.org \
--to=jolsa@kernel.org \
--cc=acme@kernel.org \
--cc=andriin@fb.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=dwarves@vger.kernel.org \
--cc=haoluo@google.com \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox