include program header in elf files generated by perf inject
PATCH 1/1 include program header in elf files generated by perf inject
The missing header makes it hard for programs like elfutils to open these files.
--- tools/perf/util/genelf.c | 14 ++++++++++++++ tools/perf/util/genelf.h | 4 ++++ 2 files changed, 18 insertions(+)
tools/perf/util/genelf.c +14 -0
@@ -251,6 +251,7 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym,Elf_Data *d;Elf_Scn *scn;Elf_Ehdr *ehdr; +Elf_Phdr *phdr;Elf_Shdr *shdr;uint64_t eh_frame_base_offset;char *strsym =NULL ;
@@ -285,6 +286,19 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym, ehdr->e_version =EV_CURRENT ; ehdr->e_shstrndx= unwinding ?4 :2 ;/* shdr index for section name */ +/* +* setup program header +*/ + phdr =elf_newphdr (e,1 ); + phdr[0 ].p_type =PT_LOAD ; + phdr[0 ].p_offset =0 ; + phdr[0 ].p_vaddr =0 ; + phdr[0 ].p_paddr =0 ; + phdr[0 ].p_filesz = csize; + phdr[0 ].p_memsz = csize; + phdr[0 ].p_flags =PF_X |PF_R ; + phdr[0 ].p_align =8 ; +/* * setup text section */
tools/perf/util/genelf.h +4 -0
@@ -53,8 +53,10 @@ int jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_ent#if GEN_ELF_CLASS ==ELFCLASS64 #define elf_newehdr elf64_newehdr +#define elf_newphdr elf64_newphdr#define elf_getshdr elf64_getshdr#define Elf_Ehdr Elf64_Ehdr +#define Elf_Phdr Elf64_Phdr#define Elf_Shdr Elf64_Shdr#define Elf_Sym Elf64_Sym#define ELF_ST_TYPE (a) ELF64_ST_TYPE(a)
@@ -62,8 +64,10 @@ int jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_ent#define ELF_ST_VIS (a) ELF64_ST_VISIBILITY(a)#else #define elf_newehdr elf32_newehdr +#define elf_newphdr elf32_newphdr#define elf_getshdr elf32_getshdr#define Elf_Ehdr Elf32_Ehdr +#define Elf_Phdr Elf32_Phdr#define Elf_Shdr Elf32_Shdr#define Elf_Sym Elf32_Sym#define ELF_ST_TYPE (a) ELF32_ST_TYPE(a)
Signature · 1 line
2.37.2
Quoted 2 lines · Lieven Hey
The missing header makes it hard for programs like elfutils to open these files.
Hi Lieven,
Do you have an example command that demonstrates this? I'm interested in trying it out.
I was also only aware of the elf files put in ~/.debug after perf record, but not perf inject.
Thanks
James
Quoted 63 lines
--- tools/perf/util/genelf.c | 14 ++++++++++++++ tools/perf/util/genelf.h | 4 ++++ 2 files changed, 18 insertions(+)tools/perf/util/genelf.c +14
-0@@ -251,6 +251,7 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym,Elf_Data *d;Elf_Scn *scn;Elf_Ehdr *ehdr; +Elf_Phdr *phdr;Elf_Shdr *shdr;uint64_t eh_frame_base_offset;char *strsym =NULL ;@@ -285,6 +286,19 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym, ehdr->e_version =EV_CURRENT ; ehdr->e_shstrndx= unwinding ?4 :2 ;/* shdr index for section name */ +/* +* setup program header +*/ + phdr =elf_newphdr (e,1 ); + phdr[0 ].p_type =PT_LOAD ; + phdr[0 ].p_offset =0 ; + phdr[0 ].p_vaddr =0 ; + phdr[0 ].p_paddr =0 ; + phdr[0 ].p_filesz = csize; + phdr[0 ].p_memsz = csize; + phdr[0 ].p_flags =PF_X |PF_R ; + phdr[0 ].p_align =8 ; +/* * setup text section */ tools/perf/util/genelf.h +4
-0@@ -53,8 +53,10 @@ int jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_ent#if GEN_ELF_CLASS ==ELFCLASS64 #define elf_newehdr elf64_newehdr +#define elf_newphdr elf64_newphdr#define elf_getshdr elf64_getshdr#define Elf_Ehdr Elf64_Ehdr +#define Elf_Phdr Elf64_Phdr#define Elf_Shdr Elf64_Shdr#define Elf_Sym Elf64_Sym#define ELF_ST_TYPE (a) ELF64_ST_TYPE(a)@@ -62,8 +64,10 @@ int jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_ent#define ELF_ST_VIS (a) ELF64_ST_VISIBILITY(a)#else #define elf_newehdr elf32_newehdr +#define elf_newphdr elf32_newphdr#define elf_getshdr elf32_getshdr#define Elf_Ehdr Elf32_Ehdr +#define Elf_Phdr Elf32_Phdr#define Elf_Shdr Elf32_Shdr#define Elf_Sym Elf32_Sym#define ELF_ST_TYPE (a) ELF32_ST_TYPE(a)
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
Hi,
perf is unable to open elf files generated by perf inject.
Example:
Create a file named test.dart with the following content:
void main() {
print("Hello World");
}and then run
perf record --call-graph dwarf -k 1 dart --generate-perf-jitdump test.dart perf inject -j -i perf.data -o perf.data.jitted
The problem exists since 2d86612aacb78. The commit added some code that now requires an elf file to have a valid program header.
Cheers
Lieven
Am 13.09.22 um 11:56 schrieb James Clark:
Quoted 150 lines · Lieven Hey
Quoted 5 lines
Hi James,
I looked into this again. Commit
2d86612aacb78changed the elf loading code so that a program header is now required.Can you reply-all on the original mailing list thread with this and a 'to' to Leo. It's probably best to continue the discussion there so Leo is aware.
Thanks
Quoted 135 lines
Am 25.08.22 um 17:24 schrieb James Clark:
Quoted 133 lines · Lieven Hey
Quoted 8 lines
Hi James,
I created a file named test.dart with the following content:
void
main(){
print("Hello World");
}Quoted 8 lines
and then run
perf record --call-graph dwarf -k 1 dart --generate-perf-jitdump test.dart
perf inject -j -i perf.data -o perf.data.jittedIf you now open perf.data.jitted with perf report you can see lots of errors regarding the missing program header.
Do I need a debug build of dart or some extra dependency linked into perf? I don't see any errors but I do see a load of addresses without symbols:
13.27% 0.00% dart ld-2.31.so [.]
dl_main|
---dl_main| |--11.82%--_dl_relocate_object | | | |--9.56%--0xffffffff85c01284 | | 0xffffffff8507b04c | | 0xffffffff8507afe8 | | 0xffffffff8507aa59 | | 0xffffffff85253dda | | 0xffffffff85253a35 | | | | | |--3.81%--0xffffffff85a7831a | | | | | |--2.49%--0xffffffff85252e72Quoted 90 lines
Cheers,
LievenAm 25.08.22 um 15:51 schrieb James Clark:
Quoted 84 lines · Lieven Hey
Quoted 2 lines
The missing header makes it hard for programs like elfutils to open these files.
Hi Lieven,
Do you have an example command that demonstrates this? I'm interested in
trying it out.I was also only aware of the elf files put in ~/.debug after perf record, but not perf inject.
Thanks
JamesQuoted 66 lines
--- tools/perf/util/genelf.c | 14 ++++++++++++++ tools/perf/util/genelf.h | 4 ++++ 2 files changed, 18 insertions(+)tools/perf/util/genelf.c +1
-0@@ -251,6 +251,7 @@ jit_write_elf(int fd, uint64_t load_addr, constchar *sym, Elf_Data *d; Elf_Scn *scn; Elf_Ehdr *ehdr; + Elf_Phdr *phdr; Elf_Shdr *shdr; uint64_t eh_frame_base_offset;char *strsym = NULL;
tools/perf/util/genelf.c +0
-0@@ -285,6 +286,19 @@ jit_write_elf(int fd, uint64_t load_addr, constchar *sym, ehdr->e_version = EV_CURRENT; ehdr->e_shstrndx= unwinding ? 4 : 2; /* shdr index for sectionname */
+ /*
+ * setup program header
+ */
+ phdr =elf_newphdr(e, 1);
+ phdr[0].p_type =PT_LOAD;
+ phdr[0].p_offset = 0;
+ phdr[0].p_vaddr = 0;
+ phdr[0].p_paddr = 0;
+ phdr[0].p_filesz = csize;
+ phdr[0].p_memsz = csize;
+ phdr[0].p_flags =PF_X|PF_R;
+ phdr[0].p_align = 8;tools/perf/util/genelf.c +1
-0+ /** setup text section */ tools/perf/util/genelf.h +0
-0@@ -53,8 +53,10 @@ int jit_add_debug_info(Elf *e, uint64_t code_addr,void *debug, int
nr_debug_ent
#ifGEN_ELF_CLASS== ELFCLASS64
#define elf_newehdrelf64_newehdrtools/perf/util/genelf.h +2
-0+#define elf_newphdr elf64_newphdr #define elf_getshdr elf64_getshdr #define Elf_Ehdr Elf64_Ehdr +#define Elf_Phdr Elf64_Phdr #define Elf_Shdr Elf64_Shdr #define Elf_Sym Elf64_Sym #define ELF_ST_TYPE(a) ELF64_ST_TYPE(a)@@ -62,8 +64,10 @@ int jit_add_debug_info(Elf *e, uint64_t code_addr,void *debug, int
nr_debug_ent
#define ELF_ST_VIS(a)ELF64_ST_VISIBILITY(a)#else
#define elf_newehdrelf32_newehdrtools/perf/util/genelf.h +2
-0+#define elf_newphdr elf32_newphdr #define elf_getshdr elf32_getshdr #define Elf_Ehdr Elf32_Ehdr +#define Elf_Phdr Elf32_Phdr #define Elf_Shdr Elf32_Shdr #define Elf_Sym Elf32_Sym #define ELF_ST_TYPE(a) ELF32_ST_TYPE(a)
Signature · 4 lines
Lieven Hey | [email protected] | Software Developer KDAB (Deutschland) GmbH, a KDAB Group company Tel: +49-30-521325470 KDAB - The Qt, C++ and OpenGL Experts
Hi Lieven,
Quoted 11 lines · Lieven Hey
Hi,
perf is unable to open elf files generated by perf inject.
Example:
Create a file named test.dart with the following content:void main() { print("Hello World"); }
Quoted 7 lines
and then run
perf record --call-graph dwarf -k 1 dart --generate-perf-jitdump test.dart perf inject -j -i perf.data -o perf.data.jitted
The problem exists since
2d86612aacb78. The commit added some code that now requires an elf file to have a valid program header.
Sorry I missed this patch.
Could you confirm if the commit 6d518ac7be62 ("perf symbol: Fail to read phdr workaround") is included in your local code base?
After a quick look for this patch, I think it is doing right thing. Just want to check if this is the same issue with the one fixed by the commit 6d518ac7be62. I will review it in details in my next 1~2 days.
Quoted 3 lines
Quoted 3 lines
Can you reply-all on the original mailing list thread with this and a 'to' to Leo. It's probably best to continue the discussion there so Leo is aware.
Thanks a lot, James.
Leo
Hi Leo,
6d518ac7be62 was not in my code base. It fixes the issue, but not in my usecase. In my usecase I open the elf files with elfutils, but elfutils requires a program header to be present.
Cheers,
Lieven
Am 13.09.22 um 15:49 schrieb Leo Yan:
Quoted 38 lines
Hi Lieven,
Quoted 11 lines · Lieven Hey
Hi,
perf is unable to open elf files generated by perf inject.
Example:
Create a file named test.dart with the following content:void main() { print("Hello World"); }Quoted 7 lines
and then run
perf record --call-graph dwarf -k 1 dart --generate-perf-jitdump test.dart perf inject -j -i perf.data -o perf.data.jitted
The problem exists since
2d86612aacb78. The commit added some code that now requires an elf file to have a valid program header.Sorry I missed this patch.
Could you confirm if the commit
6d518ac7be62("perf symbol: Fail to read phdr workaround") is included in your local code base?After a quick look for this patch, I think it is doing right thing. Just want to check if this is the same issue with the one fixed by the commit
6d518ac7be62. I will review it in details in my next 1~2 days.Quoted 3 lines
Quoted 3 lines
Can you reply-all on the original mailing list thread with this and a 'to' to Leo. It's probably best to continue the discussion there so Leo is aware.
Thanks a lot, James.
Leo
Hi Lieven,
Quoted 2 lines · Lieven Hey
The missing header makes it hard for programs like elfutils to open these files.
Several minor suggestions:
- I would suggest to change the subject as:
perf jit: Include program header in elf files
- Please add your Signed-off-by tag in the commit log.
- I confirmed this patch works for Java jitted symbols with the test case [1], this is a real fixing for 2d86612aacb7805f, it's good to add the fix tag:
[1] https://lore.kernel.org/lkml/[email protected]/
I also followed you suggested commands to verify dart jitted symbols:
# perf record --call-graph dwarf -k 1 dart --generate-perf-jitdump test.dart # perf inject -j -i perf.data -o perf.data.jitted # perf --debug verbose=4 report -i perf.data.jitted
Before change:
unwind: find_proc_info dso /tmp/jitted-287756-38.so
dso__load_sym_internal: failed to find program header for symbol: CallToRuntime st_value: 0x40
dso__load_sym_internal: adjusting symbol: st_value: 0x40 sh_addr: 0x40 sh_offset: 0x40
symbol__new: CallToRuntime 0x40-0xc0
After change:
unwind: find_proc_info dso /tmp/jitted-287756-38.so
dso__load_sym_internal: adjusting symbol: st_value: 0x40 p_vaddr: 0 p_offset: 0
symbol__new: CallToRuntime 0x40-0xc0
Which proves the program header has been added for jitted symbols in the generated elf file.
So with addressing above comments:
Quoted 65 lines
--- tools/perf/util/genelf.c | 14 ++++++++++++++ tools/perf/util/genelf.h | 4 ++++ 2 files changed, 18 insertions(+)tools/perf/util/genelf.c +14
-0@@ -251,6 +251,7 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym,Elf_Data *d;Elf_Scn *scn;Elf_Ehdr *ehdr; +Elf_Phdr *phdr;Elf_Shdr *shdr;uint64_t eh_frame_base_offset;char *strsym =NULL ;@@ -285,6 +286,19 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym, ehdr->e_version =EV_CURRENT ; ehdr->e_shstrndx= unwinding ?4 :2 ;/* shdr index for section name */ +/* +* setup program header +*/ + phdr =elf_newphdr (e,1 ); + phdr[0 ].p_type =PT_LOAD ; + phdr[0 ].p_offset =0 ; + phdr[0 ].p_vaddr =0 ; + phdr[0 ].p_paddr =0 ; + phdr[0 ].p_filesz = csize; + phdr[0 ].p_memsz = csize; + phdr[0 ].p_flags =PF_X |PF_R ; + phdr[0 ].p_align =8 ; +/* * setup text section */ tools/perf/util/genelf.h +4
-0@@ -53,8 +53,10 @@ int jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_ent#if GEN_ELF_CLASS ==ELFCLASS64 #define elf_newehdr elf64_newehdr +#define elf_newphdr elf64_newphdr#define elf_getshdr elf64_getshdr#define Elf_Ehdr Elf64_Ehdr +#define Elf_Phdr Elf64_Phdr#define Elf_Shdr Elf64_Shdr#define Elf_Sym Elf64_Sym#define ELF_ST_TYPE (a) ELF64_ST_TYPE(a)@@ -62,8 +64,10 @@ int jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_ent#define ELF_ST_VIS (a) ELF64_ST_VISIBILITY(a)#else #define elf_newehdr elf32_newehdr +#define elf_newphdr elf32_newphdr#define elf_getshdr elf32_getshdr#define Elf_Ehdr Elf32_Ehdr +#define Elf_Phdr Elf32_Phdr#define Elf_Shdr Elf32_Shdr#define Elf_Sym Elf32_Sym#define ELF_ST_TYPE (a) ELF32_ST_TYPE(a)Signature · 1 line
2.37.2
Hi Leo,
can I simply call git send-email after amending my commit or do I need to add some kind of id or reference to the old patch?
Am 14.09.22 um 15:52 schrieb Leo Yan:
Quoted 111 lines
Hi Lieven,
Quoted 2 lines · Lieven Hey
The missing header makes it hard for programs like elfutils to open these files.
Several minor suggestions:
- I would suggest to change the subject as:
perf jit: Include program header in elf files
- Please add your Signed-off-by tag in the commit log.
- I confirmed this patch works for Java jitted symbols with the test case [1], this is a real fixing for2d86612aacb7805f, it's good to add the fix tag:Fixes 2d86612aacb7805f ("perf symbol: Correct address for bss symbols")[1] https://lore.kernel.org/lkml/[email protected]/
I also followed you suggested commands to verify dart jitted symbols:
# perf record --call-graph dwarf -k 1 dart --generate-perf-jitdump test.dart # perf inject -j -i perf.data -o perf.data.jitted # perf --debug verbose=4 report -i perf.data.jittedBefore change:
unwind: find_proc_info dso /tmp/jitted-287756-38.so dso__load_sym_internal: failed to find program header for symbol: CallToRuntime st_value: 0x40 dso__load_sym_internal: adjusting symbol: st_value: 0x40 sh_addr: 0x40 sh_offset: 0x40 symbol__new: CallToRuntime 0x40-0xc0After change:
unwind: find_proc_info dso /tmp/jitted-287756-38.so dso__load_sym_internal: adjusting symbol: st_value: 0x40 p_vaddr: 0 p_offset: 0 symbol__new: CallToRuntime 0x40-0xc0Which proves the program header has been added for jitted symbols in the generated elf file.
So with addressing above comments:
Reviewed-by Leo Yan <[email protected]>Tested-by Leo Yan <[email protected]>Quoted 65 lines
--- tools/perf/util/genelf.c | 14 ++++++++++++++ tools/perf/util/genelf.h | 4 ++++ 2 files changed, 18 insertions(+)tools/perf/util/genelf.c +14
-0@@ -251,6 +251,7 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym,Elf_Data *d;Elf_Scn *scn;Elf_Ehdr *ehdr; +Elf_Phdr *phdr;Elf_Shdr *shdr;uint64_t eh_frame_base_offset;char *strsym =NULL ;@@ -285,6 +286,19 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym, ehdr->e_version =EV_CURRENT ; ehdr->e_shstrndx= unwinding ?4 :2 ;/* shdr index for section name */ +/* +* setup program header +*/ + phdr =elf_newphdr (e,1 ); + phdr[0 ].p_type =PT_LOAD ; + phdr[0 ].p_offset =0 ; + phdr[0 ].p_vaddr =0 ; + phdr[0 ].p_paddr =0 ; + phdr[0 ].p_filesz = csize; + phdr[0 ].p_memsz = csize; + phdr[0 ].p_flags =PF_X |PF_R ; + phdr[0 ].p_align =8 ; +/* * setup text section */ tools/perf/util/genelf.h +4
-0@@ -53,8 +53,10 @@ int jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_ent#if GEN_ELF_CLASS ==ELFCLASS64 #define elf_newehdr elf64_newehdr +#define elf_newphdr elf64_newphdr#define elf_getshdr elf64_getshdr#define Elf_Ehdr Elf64_Ehdr +#define Elf_Phdr Elf64_Phdr#define Elf_Shdr Elf64_Shdr#define Elf_Sym Elf64_Sym#define ELF_ST_TYPE (a) ELF64_ST_TYPE(a)@@ -62,8 +64,10 @@ int jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_ent#define ELF_ST_VIS (a) ELF64_ST_VISIBILITY(a)#else #define elf_newehdr elf32_newehdr +#define elf_newphdr elf32_newphdr#define elf_getshdr elf32_getshdr#define Elf_Ehdr Elf32_Ehdr +#define Elf_Phdr Elf32_Phdr#define Elf_Shdr Elf32_Shdr#define Elf_Sym Elf32_Sym#define ELF_ST_TYPE (a) ELF32_ST_TYPE(a)Signature · 1 line
2.37.2
Signature · 4 lines
Lieven Hey | [email protected] | Software Developer KDAB (Deutschland) GmbH, a KDAB Group company Tel: +49-30-521325470 KDAB - The Qt, C++ and OpenGL Experts
Hi Lieven,
Quoted 4 lines · Lieven Hey
Hi Leo,
can I simply call git send-email after amending my commit or do I need to add some kind of id or reference to the old patch?
Yeah, you just need to use "git send-email" to send the patch; so far we doesn't need something like gerrit's SHA ID.
Some tips: it's good to use b4 tool and generate patch with increment version number, this would be easier for maintainers to track your patch. Something like:
$ cd $LINUX_SRC
# Download the patch v1 and append review / test tags automatically
$ b4 am [email protected] $ git am 20220825_lieven_hey_include_program_header_in_elf_files_generated_by_perf_inject.mbx
# Do yourself's modificantion for the change ....
# ... Generate the patch v2 $ git format-patch -v2 HEAD~1 $ git send-email v2-...patch
Thanks,
Leo