Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/DynamicLoader/Static/DynamicLoaderStatic.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- DynamicLoaderStatic.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 "lldb/Core/Module.h"
10
#include "lldb/Core/PluginManager.h"
11
#include "lldb/Core/Section.h"
12
#include "lldb/Symbol/ObjectFile.h"
13
#include "lldb/Target/SectionLoadList.h"
14
#include "lldb/Target/Target.h"
15
16
#include "DynamicLoaderStatic.h"
17
18
using namespace lldb;
19
using namespace lldb_private;
20
21
LLDB_PLUGIN_DEFINE(DynamicLoaderStatic)
22
23
// Create an instance of this class. This function is filled into the plugin
24
// info class that gets handed out by the plugin factory and allows the lldb to
25
// instantiate an instance of this class.
26
DynamicLoader *DynamicLoaderStatic::CreateInstance(Process *process,
27
65
                                                   bool force) {
28
65
  bool create = force;
29
65
  if (!create) {
30
58
    const llvm::Triple &triple_ref =
31
58
        process->GetTarget().GetArchitecture().GetTriple();
32
58
    const llvm::Triple::OSType os_type = triple_ref.getOS();
33
58
    const llvm::Triple::ArchType arch_type = triple_ref.getArch();
34
58
    if (os_type == llvm::Triple::UnknownOS) {
35
      // The WASM and Hexagon plugin check the ArchType rather than the OSType,
36
      // so explicitly reject those here.
37
57
      switch(arch_type) {
38
0
        case llvm::Triple::hexagon:
39
3
        case llvm::Triple::wasm32:
40
3
        case llvm::Triple::wasm64:
41
3
          break;
42
54
        default:
43
54
          create = true;
44
57
      }
45
57
    }
46
58
  }
47
48
65
  if (!create) {
49
4
    Module *exe_module = process->GetTarget().GetExecutableModulePointer();
50
4
    if (exe_module) {
51
1
      ObjectFile *object_file = exe_module->GetObjectFile();
52
1
      if (object_file) {
53
1
        create = (object_file->GetStrata() == ObjectFile::eStrataRawImage);
54
1
      }
55
1
    }
56
4
  }
57
58
65
  if (create)
59
62
    return new DynamicLoaderStatic(process);
60
3
  return nullptr;
61
65
}
62
63
// Constructor
64
DynamicLoaderStatic::DynamicLoaderStatic(Process *process)
65
62
    : DynamicLoader(process) {}
66
67
/// Called after attaching a process.
68
///
69
/// Allow DynamicLoader plug-ins to execute some code after
70
/// attaching to a process.
71
60
void DynamicLoaderStatic::DidAttach() { LoadAllImagesAtFileAddresses(); }
72
73
/// Called after attaching a process.
74
///
75
/// Allow DynamicLoader plug-ins to execute some code after
76
/// attaching to a process.
77
2
void DynamicLoaderStatic::DidLaunch() { LoadAllImagesAtFileAddresses(); }
78
79
62
void DynamicLoaderStatic::LoadAllImagesAtFileAddresses() {
80
62
  const ModuleList &module_list = m_process->GetTarget().GetImages();
81
82
62
  ModuleList loaded_module_list;
83
84
  // Disable JIT for static dynamic loader targets
85
62
  m_process->SetCanJIT(false);
86
87
62
  for (ModuleSP module_sp : module_list.Modules()) {
88
33
    if (module_sp) {
89
33
      bool changed = false;
90
33
      ObjectFile *image_object_file = module_sp->GetObjectFile();
91
33
      if (image_object_file) {
92
33
        SectionList *section_list = image_object_file->GetSectionList();
93
33
        if (section_list) {
94
          // All sections listed in the dyld image info structure will all
95
          // either be fixed up already, or they will all be off by a single
96
          // slide amount that is determined by finding the first segment that
97
          // is at file offset zero which also has bytes (a file size that is
98
          // greater than zero) in the object file.
99
100
          // Determine the slide amount (if any)
101
33
          const size_t num_sections = section_list->GetSize();
102
33
          size_t sect_idx = 0;
103
183
          for (sect_idx = 0; sect_idx < num_sections; 
++sect_idx150
) {
104
            // Iterate through the object file sections to find the first
105
            // section that starts of file offset zero and that has bytes in
106
            // the file...
107
150
            SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx));
108
150
            if (section_sp) {
109
              // If this section already has a load address set in the target,
110
              // don't re-set it to the file address.  Something may have
111
              // set it to a more correct value already.
112
150
              if (m_process->GetTarget()
113
150
                      .GetSectionLoadList()
114
150
                      .GetSectionLoadAddress(section_sp) !=
115
150
                  LLDB_INVALID_ADDRESS) {
116
21
                continue;
117
21
              }
118
129
              if (m_process->GetTarget().SetSectionLoadAddress(
119
129
                      section_sp, section_sp->GetFileAddress()))
120
67
                changed = true;
121
129
            }
122
150
          }
123
33
        }
124
33
      }
125
126
33
      if (changed)
127
33
        loaded_module_list.AppendIfNeeded(module_sp);
128
33
    }
129
33
  }
130
131
62
  m_process->GetTarget().ModulesDidLoad(loaded_module_list);
132
62
}
133
134
ThreadPlanSP
135
DynamicLoaderStatic::GetStepThroughTrampolinePlan(Thread &thread,
136
0
                                                  bool stop_others) {
137
0
  return ThreadPlanSP();
138
0
}
139
140
0
Status DynamicLoaderStatic::CanLoadImage() {
141
0
  Status error;
142
0
  error.SetErrorString("can't load images on with a static debug session");
143
0
  return error;
144
0
}
145
146
3.44k
void DynamicLoaderStatic::Initialize() {
147
3.44k
  PluginManager::RegisterPlugin(GetPluginNameStatic(),
148
3.44k
                                GetPluginDescriptionStatic(), CreateInstance);
149
3.44k
}
150
151
3.43k
void DynamicLoaderStatic::Terminate() {
152
3.43k
  PluginManager::UnregisterPlugin(CreateInstance);
153
3.43k
}
154
155
3.44k
llvm::StringRef DynamicLoaderStatic::GetPluginDescriptionStatic() {
156
3.44k
  return "Dynamic loader plug-in that will load any images at the static "
157
3.44k
         "addresses contained in each image.";
158
3.44k
}