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