xref: /aosp_15_r20/external/stg/elf_dwarf_handle.cc (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright 2022-2024 Google LLC
5 //
6 // Licensed under the Apache License v2.0 with LLVM Exceptions (the
7 // "License"); you may not use this file except in compliance with the
8 // License.  You may obtain a copy of the License at
9 //
10 //     https://llvm.org/LICENSE.txt
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18 // Author: Aleksei Vetrov
19 
20 #include "elf_dwarf_handle.h"
21 
22 #include <elfutils/libdw.h>
23 #include <elfutils/libdwfl.h>
24 #include <gelf.h>
25 #include <libelf.h>
26 
27 #include <cstddef>
28 #include <functional>
29 #include <sstream>
30 #include <string>
31 
32 #include "error.h"
33 #include "hex.h"
34 
35 namespace stg {
36 
37 namespace {
38 
39 const Dwfl_Callbacks kDwflCallbacks = {
40     .find_elf = nullptr,
41     .find_debuginfo = dwfl_standard_find_debuginfo,
42     .section_address = dwfl_offline_section_address,
43     .debuginfo_path = nullptr};
44 
45 constexpr int kReturnOk = 0;
46 
GetDwflError(const char * caller)47 std::string GetDwflError(const char* caller) {
48   std::ostringstream result;
49   const int dwfl_error = dwfl_errno();
50   const char* errmsg = dwfl_errmsg(dwfl_error);
51   if (errmsg == nullptr) {
52     // There are some cases when DWFL fails to produce an error message.
53     result << caller << " returned error code " << Hex(dwfl_error);
54   } else {
55     result << caller << " returned error: " << errmsg;
56   }
57   return result.str();
58 }
59 
CheckOrDwflError(bool condition,const char * caller)60 void CheckOrDwflError(bool condition, const char* caller) {
61   if (!condition) {
62     Die() << GetDwflError(caller);
63   }
64 }
65 
66 }  // namespace
67 
ElfDwarfHandle(const char * module_name,const std::function<Dwfl_Module * ()> & add_module)68 ElfDwarfHandle::ElfDwarfHandle(
69     const char* module_name, const std::function<Dwfl_Module*()>& add_module) {
70   dwfl_ = DwflUniquePtr(dwfl_begin(&kDwflCallbacks));
71   CheckOrDwflError(dwfl_ != nullptr, "dwfl_begin");
72   // Add data to process to dwfl
73   dwfl_module_ = add_module();
74   CheckOrDwflError(dwfl_module_ != nullptr, module_name);
75   // Finish adding files to dwfl and process them
76   CheckOrDwflError(dwfl_report_end(dwfl_.get(), nullptr, nullptr) == kReturnOk,
77                    "dwfl_report_end");
78 }
79 
ElfDwarfHandle(const std::string & path)80 ElfDwarfHandle::ElfDwarfHandle(const std::string& path)
81     : ElfDwarfHandle("dwfl_report_offline", [&] {
82         return dwfl_report_offline(dwfl_.get(), path.c_str(), path.c_str(), -1);
83       }) {}
84 
ElfDwarfHandle(char * data,size_t size)85 ElfDwarfHandle::ElfDwarfHandle(char* data, size_t size)
86     : ElfDwarfHandle("dwfl_report_offline_memory", [&] {
87         return dwfl_report_offline_memory(dwfl_.get(), "<memory>", "<memory>",
88                                           data, size);
89       }) {}
90 
GetElf()91 Elf& ElfDwarfHandle::GetElf() {
92   GElf_Addr loadbase = 0;  // output argument for dwfl, unused by us
93   Elf* elf = dwfl_module_getelf(dwfl_module_, &loadbase);
94   CheckOrDwflError(elf != nullptr, "dwfl_module_getelf");
95   return *elf;
96 }
97 
GetDwarf()98 Dwarf* ElfDwarfHandle::GetDwarf() {
99   GElf_Addr loadbase = 0;  // output argument for dwfl, unused by us
100   Dwarf* dwarf = dwfl_module_getdwarf(dwfl_module_, &loadbase);
101   if (dwarf == nullptr) {
102     Warn() << "No DWARF found: " << GetDwflError("dwfl_module_getdwarf");
103   }
104   return dwarf;
105 }
106 
107 }  // namespace stg
108