LLVM 20.0.0git
BuildID.cpp
Go to the documentation of this file.
1//===- llvm/Object/BuildID.cpp - Build ID ---------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file defines a library for handling Build IDs and using them to find
11/// debug info.
12///
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Object/BuildID.h"
16
19#include "llvm/Support/Path.h"
20
21using namespace llvm;
22using namespace llvm::object;
23
24namespace {
25
26template <typename ELFT> BuildIDRef getBuildID(const ELFFile<ELFT> &Obj) {
27 auto PhdrsOrErr = Obj.program_headers();
28 if (!PhdrsOrErr) {
29 consumeError(PhdrsOrErr.takeError());
30 return {};
31 }
32 for (const auto &P : *PhdrsOrErr) {
33 if (P.p_type != ELF::PT_NOTE)
34 continue;
35 Error Err = Error::success();
36 for (auto N : Obj.notes(P, Err))
37 if (N.getType() == ELF::NT_GNU_BUILD_ID &&
38 N.getName() == ELF::ELF_NOTE_GNU)
39 return N.getDesc(P.p_align);
40 consumeError(std::move(Err));
41 }
42 return {};
43}
44
45} // namespace
46
48 std::string Bytes;
49 if (!tryGetFromHex(Str, Bytes))
50 return {};
51 ArrayRef<uint8_t> BuildID(reinterpret_cast<const uint8_t *>(Bytes.data()),
52 Bytes.size());
54}
55
57 if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(Obj))
58 return ::getBuildID(O->getELFFile());
59 if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(Obj))
60 return ::getBuildID(O->getELFFile());
61 if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(Obj))
62 return ::getBuildID(O->getELFFile());
63 if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(Obj))
64 return ::getBuildID(O->getELFFile());
65 return {};
66}
67
68std::optional<std::string> BuildIDFetcher::fetch(BuildIDRef BuildID) const {
69 auto GetDebugPath = [&](StringRef Directory) {
70 SmallString<128> Path{Directory};
71 sys::path::append(Path, ".build-id",
72 llvm::toHex(BuildID[0], /*LowerCase=*/true),
73 llvm::toHex(BuildID.slice(1), /*LowerCase=*/true));
74 Path += ".debug";
75 return Path;
76 };
77 if (DebugFileDirectories.empty()) {
78 SmallString<128> Path = GetDebugPath(
79#if defined(__NetBSD__)
80 // Try /usr/libdata/debug/.build-id/../...
81 "/usr/libdata/debug"
82#else
83 // Try /usr/lib/debug/.build-id/../...
84 "/usr/lib/debug"
85#endif
86 );
87 if (llvm::sys::fs::exists(Path))
88 return std::string(Path);
89 } else {
90 for (const auto &Directory : DebugFileDirectories) {
91 // Try <debug-file-directory>/.build-id/../...
92 SmallString<128> Path = GetDebugPath(Directory);
93 if (llvm::sys::fs::exists(Path))
94 return std::string(Path);
95 }
96 }
97 return std::nullopt;
98}
This file declares a library for handling Build IDs and using them to find debug info.
#define P(N)
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
virtual std::optional< std::string > fetch(BuildIDRef BuildID) const
Returns the path to the debug file with the given build ID.
Definition: BuildID.cpp:68
iterator_range< Elf_Note_Iterator > notes(const Elf_Phdr &Phdr, Error &Err) const
Get an iterator range over notes of a program header.
Definition: ELF.h:462
Expected< Elf_Phdr_Range > program_headers() const
Iterate over program header table.
Definition: ELF.h:376
This class is the base class for all object file types.
Definition: ObjectFile.h:229
constexpr const char * ELF_NOTE_GNU
Definition: ELF.h:1935
@ NT_GNU_BUILD_ID
Definition: ELF.h:1745
@ PT_NOTE
Definition: ELF.h:1500
SmallVector< uint8_t, 10 > BuildID
A build ID in binary form.
Definition: BuildID.h:25
BuildIDRef getBuildID(const ObjectFile *Obj)
Returns the build ID, if any, contained in the given object file.
Definition: BuildID.cpp:56
BuildID parseBuildID(StringRef Str)
Parses a build ID from a hex string.
Definition: BuildID.cpp:47
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:1077
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:456
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition: Casting.h:649
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
#define N