LLVM 22.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 findBuildID = [&Obj](const auto &ShdrOrPhdr,
28 uint64_t Alignment) -> std::optional<BuildIDRef> {
29 Error Err = Error::success();
30 for (auto N : Obj.notes(ShdrOrPhdr, Err))
31 if (N.getType() == ELF::NT_GNU_BUILD_ID &&
32 N.getName() == ELF::ELF_NOTE_GNU)
33 return N.getDesc(Alignment);
34 consumeError(std::move(Err));
35 return std::nullopt;
36 };
37
38 auto Sections = cantFail(Obj.sections());
39 for (const auto &S : Sections) {
40 if (S.sh_type != ELF::SHT_NOTE)
41 continue;
42 if (std::optional<BuildIDRef> ShdrRes = findBuildID(S, S.sh_addralign))
43 return ShdrRes.value();
44 }
45 auto PhdrsOrErr = Obj.program_headers();
46 if (!PhdrsOrErr) {
47 consumeError(PhdrsOrErr.takeError());
48 return {};
49 }
50 for (const auto &P : *PhdrsOrErr) {
51 if (P.p_type != ELF::PT_NOTE)
52 continue;
53 if (std::optional<BuildIDRef> PhdrRes = findBuildID(P, P.p_align))
54 return PhdrRes.value();
55 }
56 return {};
57}
58
59} // namespace
60
62 std::string Bytes;
63 if (!tryGetFromHex(Str, Bytes))
64 return {};
65 ArrayRef<uint8_t> BuildID(reinterpret_cast<const uint8_t *>(Bytes.data()),
66 Bytes.size());
68}
69
71 if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(Obj))
72 return ::getBuildID(O->getELFFile());
73 if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(Obj))
74 return ::getBuildID(O->getELFFile());
75 if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(Obj))
76 return ::getBuildID(O->getELFFile());
77 if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(Obj))
78 return ::getBuildID(O->getELFFile());
79 return {};
80}
81
82std::optional<std::string> BuildIDFetcher::fetch(BuildIDRef BuildID) const {
83 auto GetDebugPath = [&](StringRef Directory) {
84 SmallString<128> Path{Directory};
85 sys::path::append(Path, ".build-id",
86 llvm::toHex(BuildID[0], /*LowerCase=*/true),
87 llvm::toHex(BuildID.slice(1), /*LowerCase=*/true));
88 Path += ".debug";
89 return Path;
90 };
91 if (DebugFileDirectories.empty()) {
92 SmallString<128> Path = GetDebugPath(
93#if defined(__NetBSD__)
94 // Try /usr/libdata/debug/.build-id/../...
95 "/usr/libdata/debug"
96#else
97 // Try /usr/lib/debug/.build-id/../...
98 "/usr/lib/debug"
99#endif
100 );
101 if (llvm::sys::fs::exists(Path))
102 return std::string(Path);
103 } else {
104 for (const auto &Directory : DebugFileDirectories) {
105 // Try <debug-file-directory>/.build-id/../...
106 SmallString<128> Path = GetDebugPath(Directory);
107 if (llvm::sys::fs::exists(Path))
108 return std::string(Path);
109 }
110 }
111 return std::nullopt;
112}
This file declares a library for handling Build IDs and using them to find debug info.
#define P(N)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
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.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
virtual std::optional< std::string > fetch(BuildIDRef BuildID) const
Returns the path to the debug file with the given build ID.
Definition BuildID.cpp:82
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:469
Expected< Elf_Phdr_Range > program_headers() const
Iterate over program header table.
Definition ELF.h:381
Expected< Elf_Shdr_Range > sections() const
Definition ELF.h:932
This class is the base class for all object file types.
Definition ObjectFile.h:231
@ NT_GNU_BUILD_ID
Definition ELF.h:1802
constexpr const char * ELF_NOTE_GNU
Definition ELF.h:1999
@ SHT_NOTE
Definition ELF.h:1149
@ PT_NOTE
Definition ELF.h:1556
SmallVector< uint8_t, 10 > BuildID
A build ID in binary form.
Definition BuildID.h:26
LLVM_ABI BuildIDRef getBuildID(const ObjectFile *Obj)
Returns the build ID, if any, contained in the given object file.
Definition BuildID.cpp:70
LLVM_ABI BuildID parseBuildID(StringRef Str)
Parses a build ID from a hex string.
Definition BuildID.cpp:61
ArrayRef< uint8_t > BuildIDRef
A reference to a BuildID in binary form.
Definition BuildID.h:29
LLVM_ABI bool exists(const basic_file_status &status)
Does file exist?
Definition Path.cpp:1077
LLVM_ABI 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.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:644
bool tryGetFromHex(StringRef Input, std::string &Output)
Convert hexadecimal string Input to its binary representation and store the result in Output....
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
void toHex(ArrayRef< uint8_t > Input, bool LowerCase, SmallVectorImpl< char > &Output)
Convert buffer Input to its hexadecimal representation. The returned string is double the size of Inp...
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1083
#define N