Skip to content

Commit be96ea8

Browse files
Stephane Eranianacmel
authored andcommitted
perf symbols: Fix issue with binaries using 16-bytes buildids (v2)
Buildid can vary in size. According to the man page of ld, buildid can be 160 bits (sha1) or 128 bits (md5, uuid). Perf assumes buildid size of 20 bytes (160 bits) regardless. When dealing with md5 buildids, it would thus read more than needed and that would cause mismatches and samples without symbols. This patch fixes this by taking into account the actual buildid size as encoded int he section header. The leftover bytes are also cleared. This second version fixes a minor issue with the memset() base position. Cc: David S. Miller <davem@davemloft.net> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Robert Richter <robert.richter@amd.com> Cc: Stephane Eranian <eranian@gmail.com> Link: http://lkml.kernel.org/r/4cc1af3c.8ee7d80a.5a28.ffff868e@mx.google.com Signed-off-by: Stephane Eranian <eranian@google.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 936be50 commit be96ea8

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

‎tools/perf/util/symbol.c‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,8 +1170,7 @@ static int dso__load_sym(struct dso *dso, struct map *map, const char *name,
11701170
if (dso->has_build_id) {
11711171
u8 build_id[BUILD_ID_SIZE];
11721172

1173-
if (elf_read_build_id(elf, build_id,
1174-
BUILD_ID_SIZE) != BUILD_ID_SIZE)
1173+
if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
11751174
goto out_elf_end;
11761175

11771176
if (!dso__build_id_equal(dso, build_id))
@@ -1443,8 +1442,8 @@ static int elf_read_build_id(Elf *elf, void *bf, size_t size)
14431442
ptr = data->d_buf;
14441443
while (ptr < (data->d_buf + data->d_size)) {
14451444
GElf_Nhdr *nhdr = ptr;
1446-
int namesz = NOTE_ALIGN(nhdr->n_namesz),
1447-
descsz = NOTE_ALIGN(nhdr->n_descsz);
1445+
size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
1446+
descsz = NOTE_ALIGN(nhdr->n_descsz);
14481447
const char *name;
14491448

14501449
ptr += sizeof(*nhdr);
@@ -1453,8 +1452,10 @@ static int elf_read_build_id(Elf *elf, void *bf, size_t size)
14531452
if (nhdr->n_type == NT_GNU_BUILD_ID &&
14541453
nhdr->n_namesz == sizeof("GNU")) {
14551454
if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1456-
memcpy(bf, ptr, BUILD_ID_SIZE);
1457-
err = BUILD_ID_SIZE;
1455+
size_t sz = min(size, descsz);
1456+
memcpy(bf, ptr, sz);
1457+
memset(bf + sz, 0, size - sz);
1458+
err = descsz;
14581459
break;
14591460
}
14601461
}
@@ -1506,7 +1507,7 @@ int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
15061507
while (1) {
15071508
char bf[BUFSIZ];
15081509
GElf_Nhdr nhdr;
1509-
int namesz, descsz;
1510+
size_t namesz, descsz;
15101511

15111512
if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
15121513
break;
@@ -1515,15 +1516,16 @@ int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
15151516
descsz = NOTE_ALIGN(nhdr.n_descsz);
15161517
if (nhdr.n_type == NT_GNU_BUILD_ID &&
15171518
nhdr.n_namesz == sizeof("GNU")) {
1518-
if (read(fd, bf, namesz) != namesz)
1519+
if (read(fd, bf, namesz) != (ssize_t)namesz)
15191520
break;
15201521
if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1521-
if (read(fd, build_id,
1522-
BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1522+
size_t sz = min(descsz, size);
1523+
if (read(fd, build_id, sz) == (ssize_t)sz) {
1524+
memset(build_id + sz, 0, size - sz);
15231525
err = 0;
15241526
break;
15251527
}
1526-
} else if (read(fd, bf, descsz) != descsz)
1528+
} else if (read(fd, bf, descsz) != (ssize_t)descsz)
15271529
break;
15281530
} else {
15291531
int n = namesz + descsz;

0 commit comments

Comments
 (0)