forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elf_loader.cc
137 lines (113 loc) · 4.19 KB
/
elf_loader.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/testing/elf_loader.h"
#include <utility>
#include "flutter/fml/file.h"
#include "flutter/fml/paths.h"
#include "flutter/runtime/dart_vm.h"
#include "flutter/testing/testing.h"
namespace flutter {
namespace testing {
ELFAOTSymbols LoadELFSymbolFromFixturesIfNeccessary(std::string elf_filename) {
if (!DartVM::IsRunningPrecompiledCode()) {
return {};
}
const auto elf_path =
fml::paths::JoinPaths({GetFixturesPath(), std::move(elf_filename)});
if (!fml::IsFile(elf_path)) {
FML_LOG(ERROR) << "App AOT file does not exist for this fixture. Attempts "
"to launch the Dart VM with these AOT symbols will fail.";
return {};
}
ELFAOTSymbols symbols;
// Must not be freed.
const char* error = nullptr;
#if OS_FUCHSIA
// TODO(gw280): https://github.com/flutter/flutter/issues/50285
// Dart doesn't implement Dart_LoadELF on Fuchsia
auto loaded_elf = nullptr;
#else
auto loaded_elf =
Dart_LoadELF(elf_path.c_str(), // file path
0, // file offset
&error, // error (out)
&symbols.vm_snapshot_data, // vm snapshot data (out)
&symbols.vm_snapshot_instrs, // vm snapshot instrs (out)
&symbols.vm_isolate_data, // vm isolate data (out)
&symbols.vm_isolate_instrs // vm isolate instr (out)
);
#endif
if (loaded_elf == nullptr) {
FML_LOG(ERROR)
<< "Could not fetch AOT symbols from loaded ELF. Attempts "
"to launch the Dart VM with these AOT symbols will fail. Error: "
<< error;
return {};
}
symbols.loaded_elf.reset(loaded_elf);
return symbols;
}
ELFAOTSymbols LoadELFSplitSymbolFromFixturesIfNeccessary(
std::string elf_split_filename) {
if (!DartVM::IsRunningPrecompiledCode()) {
return {};
}
const auto elf_path =
fml::paths::JoinPaths({GetFixturesPath(), std::move(elf_split_filename)});
if (!fml::IsFile(elf_path)) {
// We do not log here, as there is no expectation for a split library to
// exist.
return {};
}
ELFAOTSymbols symbols;
// Must not be freed.
const char* error = nullptr;
#if OS_FUCHSIA
// TODO(gw280): https://github.com/flutter/flutter/issues/50285
// Dart doesn't implement Dart_LoadELF on Fuchsia
auto loaded_elf = nullptr;
#else
auto loaded_elf =
Dart_LoadELF(elf_path.c_str(), // file path
0, // file offset
&error, // error (out)
&symbols.vm_snapshot_data, // vm snapshot data (out)
&symbols.vm_snapshot_instrs, // vm snapshot instrs (out)
&symbols.vm_isolate_data, // vm isolate data (out)
&symbols.vm_isolate_instrs // vm isolate instr (out)
);
#endif
if (loaded_elf == nullptr) {
FML_LOG(ERROR)
<< "Could not fetch AOT symbols from loaded ELF. Attempts "
"to launch the Dart VM with these AOT symbols will fail. Error: "
<< error;
return {};
}
symbols.loaded_elf.reset(loaded_elf);
return symbols;
}
bool PrepareSettingsForAOTWithSymbols(Settings& settings,
const ELFAOTSymbols& symbols) {
if (!DartVM::IsRunningPrecompiledCode()) {
return false;
}
settings.vm_snapshot_data = [&]() {
return std::make_unique<fml::NonOwnedMapping>(symbols.vm_snapshot_data, 0u);
};
settings.isolate_snapshot_data = [&]() {
return std::make_unique<fml::NonOwnedMapping>(symbols.vm_isolate_data, 0u);
};
settings.vm_snapshot_instr = [&]() {
return std::make_unique<fml::NonOwnedMapping>(symbols.vm_snapshot_instrs,
0u);
};
settings.isolate_snapshot_instr = [&]() {
return std::make_unique<fml::NonOwnedMapping>(symbols.vm_isolate_instrs,
0u);
};
return true;
}
} // namespace testing
} // namespace flutter