Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/ObjectFile/JIT/ObjectFileJIT.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- ObjectFileJIT.cpp -------------------------------------------------===//
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
#include "llvm/ADT/StringRef.h"
10
11
#include "ObjectFileJIT.h"
12
#include "lldb/Core/Debugger.h"
13
#include "lldb/Core/FileSpecList.h"
14
#include "lldb/Core/Module.h"
15
#include "lldb/Core/ModuleSpec.h"
16
#include "lldb/Core/PluginManager.h"
17
#include "lldb/Core/Section.h"
18
#include "lldb/Core/StreamFile.h"
19
#include "lldb/Host/Host.h"
20
#include "lldb/Symbol/ObjectFile.h"
21
#include "lldb/Target/Platform.h"
22
#include "lldb/Target/Process.h"
23
#include "lldb/Target/SectionLoadList.h"
24
#include "lldb/Target/Target.h"
25
#include "lldb/Utility/ArchSpec.h"
26
#include "lldb/Utility/DataBuffer.h"
27
#include "lldb/Utility/DataBufferHeap.h"
28
#include "lldb/Utility/FileSpec.h"
29
#include "lldb/Utility/Log.h"
30
#include "lldb/Utility/RangeMap.h"
31
#include "lldb/Utility/StreamString.h"
32
#include "lldb/Utility/Timer.h"
33
#include "lldb/Utility/UUID.h"
34
35
#ifndef __APPLE__
36
#include "Utility/UuidCompatibility.h"
37
#endif
38
39
using namespace lldb;
40
using namespace lldb_private;
41
42
LLDB_PLUGIN_DEFINE(ObjectFileJIT)
43
44
char ObjectFileJIT::ID;
45
46
3.44k
void ObjectFileJIT::Initialize() {
47
3.44k
  PluginManager::RegisterPlugin(GetPluginNameStatic(),
48
3.44k
                                GetPluginDescriptionStatic(), CreateInstance,
49
3.44k
                                CreateMemoryInstance, GetModuleSpecifications);
50
3.44k
}
51
52
3.43k
void ObjectFileJIT::Terminate() {
53
3.43k
  PluginManager::UnregisterPlugin(CreateInstance);
54
3.43k
}
55
56
ObjectFile *ObjectFileJIT::CreateInstance(const lldb::ModuleSP &module_sp,
57
                                          DataBufferSP &data_sp,
58
                                          lldb::offset_t data_offset,
59
                                          const FileSpec *file,
60
                                          lldb::offset_t file_offset,
61
139k
                                          lldb::offset_t length) {
62
  // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
63
  // a file
64
139k
  return nullptr;
65
139k
}
66
67
ObjectFile *ObjectFileJIT::CreateMemoryInstance(const lldb::ModuleSP &module_sp,
68
                                                DataBufferSP &data_sp,
69
                                                const ProcessSP &process_sp,
70
4
                                                lldb::addr_t header_addr) {
71
  // JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
72
  // memory
73
4
  return nullptr;
74
4
}
75
76
size_t ObjectFileJIT::GetModuleSpecifications(
77
    const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
78
    lldb::offset_t data_offset, lldb::offset_t file_offset,
79
145k
    lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
80
  // JIT'ed object file can't be read from a file on disk
81
145k
  return 0;
82
145k
}
83
84
ObjectFileJIT::ObjectFileJIT(const lldb::ModuleSP &module_sp,
85
                             const ObjectFileJITDelegateSP &delegate_sp)
86
5.93k
    : ObjectFile(module_sp, nullptr, 0, 0, DataBufferSP(), 0), m_delegate_wp() {
87
5.93k
  if (delegate_sp) {
88
5.93k
    m_delegate_wp = delegate_sp;
89
5.93k
    m_data.SetByteOrder(delegate_sp->GetByteOrder());
90
5.93k
    m_data.SetAddressByteSize(delegate_sp->GetAddressByteSize());
91
5.93k
  }
92
5.93k
}
93
94
5.93k
ObjectFileJIT::~ObjectFileJIT() = default;
95
96
0
bool ObjectFileJIT::ParseHeader() {
97
  // JIT code is never in a file, nor is it required to have any header
98
0
  return false;
99
0
}
100
101
20.1k
ByteOrder ObjectFileJIT::GetByteOrder() const { return m_data.GetByteOrder(); }
102
103
23.7k
bool ObjectFileJIT::IsExecutable() const { return false; }
104
105
20.1k
uint32_t ObjectFileJIT::GetAddressByteSize() const {
106
20.1k
  return m_data.GetAddressByteSize();
107
20.1k
}
108
109
4.84k
void ObjectFileJIT::ParseSymtab(Symtab &symtab) {
110
4.84k
  ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
111
4.84k
  if (delegate_sp)
112
4.84k
    delegate_sp->PopulateSymtab(this, symtab);
113
4.84k
}
114
115
0
bool ObjectFileJIT::IsStripped() {
116
0
  return false; // JIT code that is in a module is never stripped
117
0
}
118
119
5.93k
void ObjectFileJIT::CreateSections(SectionList &unified_section_list) {
120
5.93k
  if (!m_sections_up) {
121
5.93k
    m_sections_up = std::make_unique<SectionList>();
122
5.93k
    ObjectFileJITDelegateSP delegate_sp(m_delegate_wp.lock());
123
5.93k
    if (delegate_sp) {
124
5.93k
      delegate_sp->PopulateSectionList(this, *m_sections_up);
125
5.93k
      unified_section_list = *m_sections_up;
126
5.93k
    }
127
5.93k
  }
128
5.93k
}
129
130
0
void ObjectFileJIT::Dump(Stream *s) {
131
0
  ModuleSP module_sp(GetModule());
132
0
  if (module_sp) {
133
0
    std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
134
0
    s->Printf("%p: ", static_cast<void *>(this));
135
0
    s->Indent();
136
0
    s->PutCString("ObjectFileJIT");
137
138
0
    if (ArchSpec arch = GetArchitecture())
139
0
      *s << ", arch = " << arch.GetArchitectureName();
140
141
0
    s->EOL();
142
143
0
    SectionList *sections = GetSectionList();
144
0
    if (sections)
145
0
      sections->Dump(s->AsRawOstream(), s->GetIndentLevel(), nullptr, true,
146
0
                     UINT32_MAX);
147
148
0
    if (m_symtab_up)
149
0
      m_symtab_up->Dump(s, nullptr, eSortOrderNone);
150
0
  }
151
0
}
152
153
26
UUID ObjectFileJIT::GetUUID() {
154
  // TODO: maybe get from delegate, not needed for first pass
155
26
  return UUID();
156
26
}
157
158
0
uint32_t ObjectFileJIT::GetDependentModules(FileSpecList &files) {
159
  // JIT modules don't have dependencies, but they could
160
  // if external functions are called and we know where they are
161
0
  files.Clear();
162
0
  return 0;
163
0
}
164
165
0
lldb_private::Address ObjectFileJIT::GetEntryPointAddress() {
166
0
  return Address();
167
0
}
168
169
4
lldb_private::Address ObjectFileJIT::GetBaseAddress() { return Address(); }
170
171
2.75k
ObjectFile::Type ObjectFileJIT::CalculateType() { return eTypeJIT; }
172
173
0
ObjectFile::Strata ObjectFileJIT::CalculateStrata() { return eStrataJIT; }
174
175
5.98k
ArchSpec ObjectFileJIT::GetArchitecture() {
176
5.98k
  if (ObjectFileJITDelegateSP delegate_sp = m_delegate_wp.lock())
177
5.98k
    return delegate_sp->GetArchitecture();
178
0
  return ArchSpec();
179
5.98k
}
180
181
bool ObjectFileJIT::SetLoadAddress(Target &target, lldb::addr_t value,
182
5.93k
                                   bool value_is_offset) {
183
5.93k
  size_t num_loaded_sections = 0;
184
5.93k
  SectionList *section_list = GetSectionList();
185
5.93k
  if (section_list) {
186
5.93k
    const size_t num_sections = section_list->GetSize();
187
    // "value" is an offset to apply to each top level segment
188
79.2k
    for (size_t sect_idx = 0; sect_idx < num_sections; 
++sect_idx73.3k
) {
189
      // Iterate through the object file sections to find all of the sections
190
      // that size on disk (to avoid __PAGEZERO) and load them
191
73.3k
      SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
192
73.3k
      if (section_sp && section_sp->GetFileSize() > 0 &&
193
73.3k
          !section_sp->IsThreadSpecific()) {
194
73.3k
        if (target.GetSectionLoadList().SetSectionLoadAddress(
195
73.3k
                section_sp, section_sp->GetFileAddress() + value))
196
73.3k
          ++num_loaded_sections;
197
73.3k
      }
198
73.3k
    }
199
5.93k
  }
200
5.93k
  return num_loaded_sections > 0;
201
5.93k
}
202
203
size_t ObjectFileJIT::ReadSectionData(lldb_private::Section *section,
204
                                      lldb::offset_t section_offset, void *dst,
205
0
                                      size_t dst_len) {
206
0
  lldb::offset_t file_size = section->GetFileSize();
207
0
  if (section_offset < file_size) {
208
0
    size_t src_len = file_size - section_offset;
209
0
    if (src_len > dst_len)
210
0
      src_len = dst_len;
211
0
    const uint8_t *src =
212
0
        ((uint8_t *)(uintptr_t)section->GetFileOffset()) + section_offset;
213
214
0
    memcpy(dst, src, src_len);
215
0
    return src_len;
216
0
  }
217
0
  return 0;
218
0
}
219
220
size_t ObjectFileJIT::ReadSectionData(
221
    lldb_private::Section *section,
222
20.1k
    lldb_private::DataExtractor &section_data) {
223
20.1k
  if (section->GetFileSize()) {
224
20.1k
    const void *src = (void *)(uintptr_t)section->GetFileOffset();
225
226
20.1k
    DataBufferSP data_sp =
227
20.1k
        std::make_shared<DataBufferHeap>(src, section->GetFileSize());
228
20.1k
    section_data.SetData(data_sp, 0, data_sp->GetByteSize());
229
20.1k
    section_data.SetByteOrder(GetByteOrder());
230
20.1k
    section_data.SetAddressByteSize(GetAddressByteSize());
231
20.1k
    return section_data.GetByteSize();
232
20.1k
  }
233
0
  section_data.Clear();
234
0
  return 0;
235
20.1k
}