LLVM 23.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
18#include "llvm/Support/Error.h"
20#include "llvm/Support/Path.h"
21
22using namespace llvm;
23using namespace llvm::object;
24
25namespace {
26
27template <typename ELFT> BuildIDRef getBuildID(const ELFFile<ELFT> &Obj) {
28 auto findBuildID = [&Obj](const auto &ShdrOrPhdr,
29 uint64_t Alignment) -> std::optional<BuildIDRef> {
30 Error Err = Error::success();
31 for (auto N : Obj.notes(ShdrOrPhdr, Err))
32 if (N.getType() == ELF::NT_GNU_BUILD_ID &&
33 N.getName() == ELF::ELF_NOTE_GNU)
34 return N.getDesc(Alignment);
35 consumeError(std::move(Err));
36 return std::nullopt;
37 };
38
39 auto Sections = cantFail(Obj.sections());
40 for (const auto &S : Sections) {
41 if (S.sh_type != ELF::SHT_NOTE)
42 continue;
43 if (std::optional<BuildIDRef> ShdrRes = findBuildID(S, S.sh_addralign))
44 return ShdrRes.value();
45 }
46 auto PhdrsOrErr = Obj.program_headers();
47 if (!PhdrsOrErr) {
48 consumeError(PhdrsOrErr.takeError());
49 return {};
50 }
51 for (const auto &P : *PhdrsOrErr) {
52 if (P.p_type != ELF::PT_NOTE)
53 continue;
54 if (std::optional<BuildIDRef> PhdrRes = findBuildID(P, P.p_align))
55 return PhdrRes.value();
56 }
57 return {};
58}
59
60} // namespace
61
63 std::string Bytes;
64 if (!tryGetFromHex(Str, Bytes))
65 return {};
66 ArrayRef<uint8_t> BuildID(reinterpret_cast<const uint8_t *>(Bytes.data()),
67 Bytes.size());
69}
70
72 if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(Obj))
73 return ::getBuildID(O->getELFFile());
74 if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(Obj))
75 return ::getBuildID(O->getELFFile());
76 if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(Obj))
77 return ::getBuildID(O->getELFFile());
78 if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(Obj))
79 return ::getBuildID(O->getELFFile());
80 return {};
81}
82
84 auto GetDebugPath = [&](StringRef Directory) {
85 SmallString<128> Path{Directory};
86 sys::path::append(Path, ".build-id",
87 llvm::toHex(BuildID[0], /*LowerCase=*/true),
88 llvm::toHex(BuildID.slice(1), /*LowerCase=*/true));
89 Path += ".debug";
90 return Path;
91 };
92 if (DebugFileDirectories.empty()) {
93 SmallString<128> Path = GetDebugPath(
94#if defined(__NetBSD__)
95 // Try /usr/libdata/debug/.build-id/../...
96 "/usr/libdata/debug"
97#else
98 // Try /usr/lib/debug/.build-id/../...
99 "/usr/lib/debug"
100#endif
101 );
102 if (llvm::sys::fs::exists(Path))
103 return std::string(Path);
104 } else {
105 for (const auto &Directory : DebugFileDirectories) {
106 // Try <debug-file-directory>/.build-id/../...
107 SmallString<128> Path = GetDebugPath(Directory);
108 if (llvm::sys::fs::exists(Path))
109 return std::string(Path);
110 }
111 }
112 return createStringError(
113 make_error_code(std::errc::no_such_file_or_directory),
114 "could not find debug file for build ID '" +
115 llvm::toHex(BuildID, /*LowerCase=*/true) + "'");
116}
This file declares a library for handling Build IDs and using them to find debug info.
#define P(N)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
virtual Expected< std::string > fetch(BuildIDRef BuildID) const
Returns the path to the debug file with the given build ID.
Definition BuildID.cpp:83
This class is the base class for all object file types.
Definition ObjectFile.h:231
@ NT_GNU_BUILD_ID
Definition ELF.h:1816
constexpr const char * ELF_NOTE_GNU
Definition ELF.h:2013
@ SHT_NOTE
Definition ELF.h:1161
@ PT_NOTE
Definition ELF.h:1569
SmallVector< uint8_t, 10 > BuildID
A build ID in binary form.
Definition BuildID.h:27
std::error_code make_error_code(object_error e)
Definition Error.h:41
LLVM_ABI BuildIDRef getBuildID(const ObjectFile *Obj)
Returns the build ID, if any, contained in the given object file.
Definition BuildID.cpp:71
LLVM_ABI BuildID parseBuildID(StringRef Str)
Parses a build ID from a hex string.
Definition BuildID.cpp:62
ArrayRef< uint8_t > BuildIDRef
A reference to a BuildID in binary form.
Definition BuildID.h:30
LLVM_ABI bool exists(const basic_file_status &status)
Does file exist?
Definition Path.cpp:1107
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:467
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:643
bool tryGetFromHex(StringRef Input, std::string &Output)
Convert hexadecimal string Input to its binary representation and store the result in Output....
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
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:1106
#define N