Skip to contentSkip to conversation

perf tools: don't adjust symbols in vDSO

PATCH In review Will Deacon 3 participants 9 messages Jun 27, 2015 – Jul 9, 2015 perf events

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 perf tools: don't adjust symbols in vDSO

Commit 922d0e4d9f04 ("perf tools: Adjust symbols in VDSO") changed the ELF symbol parsing so that the vDSO is treated the same as ET_EXEC and ET_REL binaries despite being an ET_DYN. This was a partial workaround to deal with older x86 vDSOs being prelinked at a high address that didn't correspond to the map, so using object-relative offsets and adding the base of the map allowed symbol resolution to succeed.

Unfortunately, this causes objdump not to produce any output in conjunction with perf annotate, which cheerfully passes the absolute address of the map symbol.

This patch fixes the problem by avoiding adjustment of vDSO symbols and instead setting the map->pgoff field to correspond to the virtual load address specified in the vDSO ELF header.

Cc Vladimir Nikulichev <[email protected]>
Cc Adrian Hunter <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Andy Lutomirski <[email protected]>

---

v1->v2: Adjust map->pgoff in ELF loader to avoid breaking symbol lookup
on older kernels.

 tools/perf/util/map.c        | 5 ++---
 tools/perf/util/symbol-elf.c | 9 ++++++++-
 2 files changed, 10 insertions(+), 4 deletions(-)
tools/perf/util/map.c +2 -3
index a14f08f41686..6ba38293be88 100644
@@ -173,10 +173,9 @@ struct map *map__new(struct machine *machine, u64 start, u64 len,
 				filename = newfilename;
 		}
 
-		if (vdso) {
-			pgoff = 0;
+		if (vdso)
 			dso = vdso__dso_findnew(machine, thread);
-		} else
+		else
 			dso = __dsos__findnew(&machine->user_dsos, filename);
 
 		if (dso == NULL)
tools/perf/util/symbol-elf.c +8 -1
index a7ab6063e038..83f8ba232575 100644
@@ -706,7 +706,6 @@ int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
 		GElf_Shdr shdr;
 		ss->adjust_symbols = (ehdr.e_type == ET_EXEC ||
 				ehdr.e_type == ET_REL ||
-				dso__is_vdso(dso) ||
 				elf_section_by_name(elf, &ehdr, &shdr,
 						     ".gnu.prelink_undo",
 						     NULL) != NULL);
@@ -824,6 +823,14 @@ int dso__load_sym(struct dso *dso, struct map *map,
 	sec = syms_ss->symtab;
 	shdr = syms_ss->symshdr;
 
+	/*
+	 * Older x86 kernels prelink the vDSO at a high address, so
+	 * we need to reflect that in map->pgoff in order to talk to
+	 * objdump.
+	 */
+	if (dso__is_vdso(dso))
+		map->pgoff = shdr.sh_addr - shdr.sh_offset;
+
 	if (runtime_ss->opdsec)
 		opddata = elf_rawdata(runtime_ss->opdsec, NULL);
 
Signature · 1 line
2.1.4
reviewer
#1 lore raw
Quoted 60 lines · Will Deacon

Commit 922d0e4d9f04 ("perf tools: Adjust symbols in VDSO") changed the ELF symbol parsing so that the vDSO is treated the same as ET_EXEC and ET_REL binaries despite being an ET_DYN. This was a partial workaround to deal with older x86 vDSOs being prelinked at a high address that didn't correspond to the map, so using object-relative offsets and adding the base of the map allowed symbol resolution to succeed.

Unfortunately, this causes objdump not to produce any output in conjunction with perf annotate, which cheerfully passes the absolute address of the map symbol.

This patch fixes the problem by avoiding adjustment of vDSO symbols and instead setting the map->pgoff field to correspond to the virtual load address specified in the vDSO ELF header.

Cc Vladimir Nikulichev <[email protected]>
Cc Adrian Hunter <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Andy Lutomirski <[email protected]>

---

v1->v2: Adjust map->pgoff in ELF loader to avoid breaking symbol lookup
on older kernels.

 tools/perf/util/map.c        | 5 ++---
 tools/perf/util/symbol-elf.c | 9 ++++++++-
 2 files changed, 10 insertions(+), 4 deletions(-)
tools/perf/util/map.c +2 -3
index a14f08f41686..6ba38293be88 100644
@@ -173,10 +173,9 @@ struct map *map__new(struct machine *machine, u64 start, u64 len,
 				filename = newfilename;
 		}
 
-		if (vdso) {
-			pgoff = 0;
+		if (vdso)
 			dso = vdso__dso_findnew(machine, thread);
-		} else
+		else
 			dso = __dsos__findnew(&machine->user_dsos, filename);
 
 		if (dso == NULL)
tools/perf/util/symbol-elf.c +0 -1
index a7ab6063e038..83f8ba232575 100644
@@ -706,7 +706,6 @@ int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
 		GElf_Shdr shdr;
 		ss->adjust_symbols = (ehdr.e_type == ET_EXEC ||
 				ehdr.e_type == ET_REL ||
-				dso__is_vdso(dso) ||
 				elf_section_by_name(elf, &ehdr, &shdr,
 						     ".gnu.prelink_undo",
 						     NULL) != NULL);

tools/perf/util/symbol-elf.c int dso__load_sym(struct dso *dso, struct map *map, · line 832

Quoted 10 lines
tools/perf/util/symbol-elf.c +7 -0
@@ -824,6 +823,14 @@ int dso__load_sym(struct dso *dso, struct map *map,
 	sec = syms_ss->symtab;
 	shdr = syms_ss->symshdr;
 
+	/*
+	 * Older x86 kernels prelink the vDSO at a high address, so
+	 * we need to reflect that in map->pgoff in order to talk to
+	 * objdump.
+	 */
+	if (dso__is_vdso(dso))
+		map->pgoff = shdr.sh_addr - shdr.sh_offset;

In the case of perf tools, maps map memory addresses to file offsets. That is used to read from the object file, so you can't change the map.

reviewer
#2 lore raw
Quoted 64 lines · Adrian Hunter
Quoted 63 lines · Will Deacon

Commit 922d0e4d9f04 ("perf tools: Adjust symbols in VDSO") changed the ELF symbol parsing so that the vDSO is treated the same as ET_EXEC and ET_REL binaries despite being an ET_DYN. This was a partial workaround to deal with older x86 vDSOs being prelinked at a high address that didn't correspond to the map, so using object-relative offsets and adding the base of the map allowed symbol resolution to succeed.

Unfortunately, this causes objdump not to produce any output in conjunction with perf annotate, which cheerfully passes the absolute address of the map symbol.

This patch fixes the problem by avoiding adjustment of vDSO symbols and instead setting the map->pgoff field to correspond to the virtual load address specified in the vDSO ELF header.

Cc Vladimir Nikulichev <[email protected]>
Cc Adrian Hunter <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Andy Lutomirski <[email protected]>

---

v1->v2: Adjust map->pgoff in ELF loader to avoid breaking symbol lookup
on older kernels.

 tools/perf/util/map.c        | 5 ++---
 tools/perf/util/symbol-elf.c | 9 ++++++++-
 2 files changed, 10 insertions(+), 4 deletions(-)
tools/perf/util/map.c +2 -3
index a14f08f41686..6ba38293be88 100644
@@ -173,10 +173,9 @@ struct map *map__new(struct machine *machine, u64 start, u64 len,
 				filename = newfilename;
 		}
 
-		if (vdso) {
-			pgoff = 0;
+		if (vdso)
 			dso = vdso__dso_findnew(machine, thread);
-		} else
+		else
 			dso = __dsos__findnew(&machine->user_dsos, filename);
 
 		if (dso == NULL)
tools/perf/util/symbol-elf.c +0 -1
index a7ab6063e038..83f8ba232575 100644
@@ -706,7 +706,6 @@ int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
 		GElf_Shdr shdr;
 		ss->adjust_symbols = (ehdr.e_type == ET_EXEC ||
 				ehdr.e_type == ET_REL ||
-				dso__is_vdso(dso) ||
 				elf_section_by_name(elf, &ehdr, &shdr,
 						     ".gnu.prelink_undo",
 						     NULL) != NULL);
@@ -824,6 +823,14 @@ int dso__load_sym(struct dso *dso, struct map *map,
 	sec = syms_ss->symtab;
 	shdr = syms_ss->symshdr;
 
Quoted 10 lines

tools/perf/util/symbol-elf.c int dso__load_sym(struct dso *dso, struct map *map, · line 832

Quoted 7 lines
tools/perf/util/symbol-elf.c +7 -0
+	/*
+	 * Older x86 kernels prelink the vDSO at a high address, so
+	 * we need to reflect that in map->pgoff in order to talk to
+	 * objdump.
+	 */
+	if (dso__is_vdso(dso))
+		map->pgoff = shdr.sh_addr - shdr.sh_offset;

In the case of perf tools, maps map memory addresses to file offsets. That is used to read from the object file, so you can't change the map.

So what about just this instead:

	if (dso__is_vdso(dso))
		map->reloc = shdr.sh_addr - shdr.sh_offset;

Although shdr is ".dynsym" whereas ".text" is what we typically care about. Don't know if they could be inconsistent.

reviewer
#3 lore raw
Quoted 76 lines · Adrian Hunter
Quoted 64 lines · Adrian Hunter
Quoted 63 lines · Will Deacon

Commit 922d0e4d9f04 ("perf tools: Adjust symbols in VDSO") changed the ELF symbol parsing so that the vDSO is treated the same as ET_EXEC and ET_REL binaries despite being an ET_DYN. This was a partial workaround to deal with older x86 vDSOs being prelinked at a high address that didn't correspond to the map, so using object-relative offsets and adding the base of the map allowed symbol resolution to succeed.

Unfortunately, this causes objdump not to produce any output in conjunction with perf annotate, which cheerfully passes the absolute address of the map symbol.

This patch fixes the problem by avoiding adjustment of vDSO symbols and instead setting the map->pgoff field to correspond to the virtual load address specified in the vDSO ELF header.

Cc Vladimir Nikulichev <[email protected]>
Cc Adrian Hunter <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Andy Lutomirski <[email protected]>

---

v1->v2: Adjust map->pgoff in ELF loader to avoid breaking symbol lookup
on older kernels.

 tools/perf/util/map.c        | 5 ++---
 tools/perf/util/symbol-elf.c | 9 ++++++++-
 2 files changed, 10 insertions(+), 4 deletions(-)
tools/perf/util/map.c +2 -3
index a14f08f41686..6ba38293be88 100644
@@ -173,10 +173,9 @@ struct map *map__new(struct machine *machine, u64 start, u64 len,
 				filename = newfilename;
 		}
 
-		if (vdso) {
-			pgoff = 0;
+		if (vdso)
 			dso = vdso__dso_findnew(machine, thread);
-		} else
+		else
 			dso = __dsos__findnew(&machine->user_dsos, filename);
 
 		if (dso == NULL)
tools/perf/util/symbol-elf.c +0 -1
index a7ab6063e038..83f8ba232575 100644
@@ -706,7 +706,6 @@ int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
 		GElf_Shdr shdr;
 		ss->adjust_symbols = (ehdr.e_type == ET_EXEC ||
 				ehdr.e_type == ET_REL ||
-				dso__is_vdso(dso) ||
 				elf_section_by_name(elf, &ehdr, &shdr,
 						     ".gnu.prelink_undo",
 						     NULL) != NULL);
@@ -824,6 +823,14 @@ int dso__load_sym(struct dso *dso, struct map *map,
 	sec = syms_ss->symtab;
 	shdr = syms_ss->symshdr;
 
Quoted 10 lines

tools/perf/util/symbol-elf.c int dso__load_sym(struct dso *dso, struct map *map, · line 832

Quoted 7 lines
tools/perf/util/symbol-elf.c +7 -0
+	/*
+	 * Older x86 kernels prelink the vDSO at a high address, so
+	 * we need to reflect that in map->pgoff in order to talk to
+	 * objdump.
+	 */
+	if (dso__is_vdso(dso))
+		map->pgoff = shdr.sh_addr - shdr.sh_offset;

In the case of perf tools, maps map memory addresses to file offsets. That is used to read from the object file, so you can't change the map.

Quoted 5 lines

So what about just this instead:

	if (dso__is_vdso(dso))
		map->reloc = shdr.sh_addr - shdr.sh_offset;

No that's no good either :-(

#4 lore raw
Quoted 18 lines · Adrian Hunter
Quoted 17 lines · Adrian Hunter
Quoted 15 lines · Adrian Hunter

tools/perf/util/symbol-elf.c int dso__load_sym(struct dso *dso, struct map *map, · line 832

Quoted 11 lines · Will Deacon
tools/perf/util/symbol-elf.c +7 -0
@@ -824,6 +823,14 @@ int dso__load_sym(struct dso *dso, struct map *map,
 	sec = syms_ss->symtab;
 	shdr = syms_ss->symshdr;
 
+	/*
+	 * Older x86 kernels prelink the vDSO at a high address, so
+	 * we need to reflect that in map->pgoff in order to talk to
+	 * objdump.
+	 */
+	if (dso__is_vdso(dso))
+		map->pgoff = shdr.sh_addr - shdr.sh_offset;

In the case of perf tools, maps map memory addresses to file offsets. That is used to read from the object file, so you can't change the map.

Quoted 7 lines
Quoted 5 lines

So what about just this instead:

	if (dso__is_vdso(dso))
		map->reloc = shdr.sh_addr - shdr.sh_offset;

No that's no good either :-(

Yeah... we're fighting against symbol lookup wanting ->map_ip to give the address of the ELF symbol but unwinding wanting ->map_ip to give a relative offset for file reads.

Also, the reloc is a bit weird and I think needs to be the other way around (i.e. we convert from a map-relative address to an objdump address by *subtracting* the reloc). Even with that change, I run into problems with annotate comparing ELF symbol addresses with map addresses. Urgh.

Will

#5 lore raw

Em Mon, Jun 29, 2015 at 02:52:39PM +0100, Will Deacon escreveu:

Quoted 27 lines · Adrian Hunter
Quoted 18 lines · Adrian Hunter
Quoted 17 lines · Adrian Hunter
Quoted 15 lines · Will Deacon

tools/perf/util/symbol-elf.c int dso__load_sym(struct dso *dso, struct map *map, · line 832

Quoted 11 lines
tools/perf/util/symbol-elf.c +7 -0
@@ -824,6 +823,14 @@ int dso__load_sym(struct dso *dso, struct map *map,
 	sec = syms_ss->symtab;
 	shdr = syms_ss->symshdr;
 
+	/*
+	 * Older x86 kernels prelink the vDSO at a high address, so
+	 * we need to reflect that in map->pgoff in order to talk to
+	 * objdump.
+	 */
+	if (dso__is_vdso(dso))
+		map->pgoff = shdr.sh_addr - shdr.sh_offset;

In the case of perf tools, maps map memory addresses to file offsets. That is used to read from the object file, so you can't change the map.

Quoted 7 lines
Quoted 5 lines

So what about just this instead:

	if (dso__is_vdso(dso))
		map->reloc = shdr.sh_addr - shdr.sh_offset;

No that's no good either :-(

Quoted 8 lines

Yeah... we're fighting against symbol lookup wanting ->map_ip to give the address of the ELF symbol but unwinding wanting ->map_ip to give a relative offset for file reads.

Also, the reloc is a bit weird and I think needs to be the other way around (i.e. we convert from a map-relative address to an objdump address by *subtracting* the reloc). Even with that change, I run into problems with annotate comparing ELF symbol addresses with map addresses. Urgh.

Its been a long time that I looked at this, but perhaps these can help:

/* rip/ip <-> addr suitable for passing to objdump --start-address= */ u64 map__rip_2objdump(struct map *map, u64 rip);

/* objdump address -> memory address */
u64 map__objdump_2mem(struct map *map, u64 ip);

The comments in the changeset that introduced it have more info:

https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=7a2b6209863626cf8362e5ff4653491558f91e67

- Arnaldo

- Arnaldo

reviewer
#6 lore raw
In reply to Will Deacon
Quoted 27 lines · Will Deacon
Quoted 18 lines · Adrian Hunter
Quoted 17 lines · Adrian Hunter
Quoted 15 lines · Adrian Hunter

tools/perf/util/symbol-elf.c int dso__load_sym(struct dso *dso, struct map *map, · line 832

Quoted 11 lines · Will Deacon
tools/perf/util/symbol-elf.c +7 -0
@@ -824,6 +823,14 @@ int dso__load_sym(struct dso *dso, struct map *map,
 	sec = syms_ss->symtab;
 	shdr = syms_ss->symshdr;
 
+	/*
+	 * Older x86 kernels prelink the vDSO at a high address, so
+	 * we need to reflect that in map->pgoff in order to talk to
+	 * objdump.
+	 */
+	if (dso__is_vdso(dso))
+		map->pgoff = shdr.sh_addr - shdr.sh_offset;

In the case of perf tools, maps map memory addresses to file offsets. That is used to read from the object file, so you can't change the map.

Quoted 7 lines
Quoted 5 lines

So what about just this instead:

	if (dso__is_vdso(dso))
		map->reloc = shdr.sh_addr - shdr.sh_offset;

No that's no good either :-(

Quoted 8 lines

Yeah... we're fighting against symbol lookup wanting ->map_ip to give the address of the ELF symbol but unwinding wanting ->map_ip to give a relative offset for file reads.

Also, the reloc is a bit weird and I think needs to be the other way around (i.e. we convert from a map-relative address to an objdump address by *subtracting* the reloc). Even with that change, I run into problems with annotate comparing ELF symbol addresses with map addresses. Urgh.

Adding or subtracting shouldn't matter.

This seems to work (without any other changes):

	if (dso__is_vdso(dso))
		map->reloc = map->start - shdr.sh_addr + shdr.sh_offset;

Haven't tested 32-bit yet though.

author Tested-by
#7 lore raw
Quoted 32 lines · Adrian Hunter
Quoted 31 lines · Will Deacon
Quoted 18 lines · Adrian Hunter
Quoted 17 lines · Adrian Hunter
Quoted 15 lines · Adrian Hunter

tools/perf/util/symbol-elf.c int dso__load_sym(struct dso *dso, struct map *map, · line 832

Quoted 11 lines · Will Deacon
tools/perf/util/symbol-elf.c +7 -0
@@ -824,6 +823,14 @@ int dso__load_sym(struct dso *dso, struct map *map,
 	sec = syms_ss->symtab;
 	shdr = syms_ss->symshdr;
 
+	/*
+	 * Older x86 kernels prelink the vDSO at a high address, so
+	 * we need to reflect that in map->pgoff in order to talk to
+	 * objdump.
+	 */
+	if (dso__is_vdso(dso))
+		map->pgoff = shdr.sh_addr - shdr.sh_offset;

In the case of perf tools, maps map memory addresses to file offsets. That is used to read from the object file, so you can't change the map.

Quoted 7 lines
Quoted 5 lines

So what about just this instead:

	if (dso__is_vdso(dso))
		map->reloc = shdr.sh_addr - shdr.sh_offset;

No that's no good either :-(

Yeah... we're fighting against symbol lookup wanting ->map_ip to give the address of the ELF symbol but unwinding wanting ->map_ip to give a relative offset for file reads.

Quoted 6 lines
Quoted 4 lines

Also, the reloc is a bit weird and I think needs to be the other way around (i.e. we convert from a map-relative address to an objdump address by *subtracting* the reloc). Even with that change, I run into problems with annotate comparing ELF symbol addresses with map addresses. Urgh.

Adding or subtracting shouldn't matter.

I was referring to the subtraction in map__rip_2objdump, but you've addressed that below.

Quoted 6 lines

This seems to work (without any other changes):

	if (dso__is_vdso(dso))
		map->reloc = map->start - shdr.sh_addr + shdr.sh_offset;

Haven't tested 32-bit yet though.

FWIW, this works on arm64 and x86_64 with my testing:

Cheers,

Will

#8 lore raw
In reply to Adrian Hunter

Hi Adrian,

Quoted 37 lines · Adrian Hunter
Quoted 27 lines · Will Deacon
Quoted 18 lines · Adrian Hunter
Quoted 17 lines · Adrian Hunter
Quoted 15 lines · Adrian Hunter

tools/perf/util/symbol-elf.c int dso__load_sym(struct dso *dso, struct map *map, · line 832

Quoted 11 lines · Will Deacon
tools/perf/util/symbol-elf.c +7 -0
@@ -824,6 +823,14 @@ int dso__load_sym(struct dso *dso, struct map *map,
 	sec = syms_ss->symtab;
 	shdr = syms_ss->symshdr;
 
+	/*
+	 * Older x86 kernels prelink the vDSO at a high address, so
+	 * we need to reflect that in map->pgoff in order to talk to
+	 * objdump.
+	 */
+	if (dso__is_vdso(dso))
+		map->pgoff = shdr.sh_addr - shdr.sh_offset;

In the case of perf tools, maps map memory addresses to file offsets. That is used to read from the object file, so you can't change the map.

Quoted 7 lines
Quoted 5 lines

So what about just this instead:

	if (dso__is_vdso(dso))
		map->reloc = shdr.sh_addr - shdr.sh_offset;

No that's no good either :-(

Quoted 8 lines

Yeah... we're fighting against symbol lookup wanting ->map_ip to give the address of the ELF symbol but unwinding wanting ->map_ip to give a relative offset for file reads.

Also, the reloc is a bit weird and I think needs to be the other way around (i.e. we convert from a map-relative address to an objdump address by *subtracting* the reloc). Even with that change, I run into problems with annotate comparing ELF symbol addresses with map addresses. Urgh.

Quoted 8 lines

Adding or subtracting shouldn't matter.

This seems to work (without any other changes):

	if (dso__is_vdso(dso))
		map->reloc = map->start - shdr.sh_addr + shdr.sh_offset;

Haven't tested 32-bit yet though.

Any chance of writing up a patch for this? I don't see it in -next.

Will