lin
2025-08-01 633231e833e21d5b8b1c00cb15aedb62b3b78e8f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2016 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#include "dso.h"
 
#include <elf.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>
 
#include <algorithm>
#include <vector>
 
#include <build_id.h>
#include <read_elf.h>
 
#include "base/logging.h"
#include "compat/string.h"
#include "file_reader.h"
 
namespace quipper {
 
void InitializeLibelf() {
  // Unnecessary.
}
 
bool ReadElfBuildId(const string &filename, string *buildid) {
  BuildId id;
  ElfStatus status = GetBuildIdFromElfFile(filename, &id);
  if (status == ElfStatus::NO_ERROR) {
    *buildid = id.ToString();
    return true;
  }
  return false;
}
 
bool ReadElfBuildId(int fd, string *buildid) {
  // TODO: Implement. b/74410255.
  return false;
}
 
// read /sys/module/<module_name>/notes/.note.gnu.build-id
bool ReadModuleBuildId(const string &module_name, string *buildid) {
  string note_filename =
      "/sys/module/" + module_name + "/notes/.note.gnu.build-id";
 
  FileReader file(note_filename);
  if (!file.IsOpen()) return false;
 
  return ReadBuildIdNote(&file, buildid);
}
 
bool ReadBuildIdNote(DataReader *data, string *buildid) {
  // Non-simpleperf implementation, as a reader is given.
  Elf64_Nhdr note_header;
 
  while (data->ReadData(sizeof(note_header), &note_header)) {
    size_t name_size = Align<4>(note_header.n_namesz);
    size_t desc_size = Align<4>(note_header.n_descsz);
 
    string name;
    if (!data->ReadString(name_size, &name)) return false;
    string desc;
    if (!data->ReadDataString(desc_size, &desc)) return false;
    if (note_header.n_type == NT_GNU_BUILD_ID && name == ELF_NOTE_GNU) {
      *buildid = desc;
      return true;
    }
  }
  return false;
}
 
bool IsKernelNonModuleName(string name) {
  // List from kernel: tools/perf/util/dso.c : __kmod_path__parse()
  static const std::vector<string> kKernelNonModuleNames{
      "[kernel.kallsyms]",
      "[guest.kernel.kallsyms",
      "[vdso]",
      "[vsyscall]",
  };
 
  for (const auto &n : kKernelNonModuleNames) {
    if (name.compare(0, n.size(), n) == 0) return true;
  }
  return false;
}
 
// Do the |DSOInfo| and |struct stat| refer to the same inode?
bool SameInode(const DSOInfo &dso, const struct stat *s) {
  return dso.maj == major(s->st_dev) && dso.min == minor(s->st_dev) &&
         dso.ino == s->st_ino;
}
 
}  // namespace quipper