-
Notifications
You must be signed in to change notification settings - Fork 569
Expand file tree
/
Copy pathDSOWrapperGenerator.cs
More file actions
168 lines (140 loc) · 6.13 KB
/
DSOWrapperGenerator.cs
File metadata and controls
168 lines (140 loc) · 6.13 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Android.Build.Tasks;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Xamarin.Android.Tools;
namespace Xamarin.Android.Tasks;
/// <summary>
/// <para>
/// Puts passed files inside a real ELF shared library so that they
/// pass scrutiny when examined. The payload is placed inside its own
/// section of a file, so the entire file is a 100% valid ELF image.
/// </para>
///
/// <para>
/// The generated files have their payload section positioned at the offset of
/// 16k (0x4000) from the beginning of file. It's done this way because it not
/// only gives us enough room for the stub part of the ELF image to precede that
/// offset, but it also complies with Google policy of aligning to 16k **and**
/// is still nicely aligned to a 4k boundary on 32-bit systems. This helps mmapping
/// the section on both 64-bit and 32-bit systems.
/// </para>
/// </summary>
/// <remarks>
/// The generated file **MUST NOT** be stripped with `llvm-strip` etc,
/// as it will remove the payload together with other sections it deems
/// unnecessary.
/// </remarks>
class DSOWrapperGenerator
{
internal const string RegisteredConfigKey = ".:!DSOWrapperGeneratorConfig!:.";
public const string StubFileName = "libarchive-dso-stub.so";
internal class Config
{
public Dictionary<AndroidTargetArch, string> DSOStubPaths { get; }
public string AndroidBinUtilsDirectory { get; }
public string BaseOutputDirectory { get; }
public Config (Dictionary<AndroidTargetArch, string> stubPaths, string androidBinUtilsDirectory, string baseOutputDirectory)
{
DSOStubPaths = stubPaths;
AndroidBinUtilsDirectory = androidBinUtilsDirectory;
BaseOutputDirectory = baseOutputDirectory;
}
};
//
// Must be the same avalue as ARCHIVE_DSO_STUB_PAYLOAD_SECTION_ALIGNMENT in src/native/CMakeLists.txt
//
const ulong PayloadSectionAlignment = 0x4000;
public static Config GetConfig (TaskLoggingHelper log, string androidBinUtilsDirectory, ITaskItem[] runtimePackLibraryDirs, string baseOutputDirectory)
{
var stubPaths = new Dictionary<AndroidTargetArch, string> ();
foreach (ITaskItem packLibDir in runtimePackLibraryDirs) {
string ?packRID = packLibDir.GetMetadata ("RuntimeIdentifier");
if (packRID.IsNullOrEmpty ()) {
continue;
}
string stubPath = Path.Combine (packLibDir.ItemSpec, StubFileName);
if (!File.Exists (stubPath)) {
// NativeAOT runtime packs don't include the DSO stub because they don't need
// DSO wrapping. Skip them gracefully instead of failing the build.
string? packageId = packLibDir.GetMetadata ("NuGetPackageId");
if (!String.IsNullOrEmpty (packageId) && packageId.StartsWith ("Microsoft.Android.Runtime.NativeAOT.", StringComparison.OrdinalIgnoreCase)) {
log.LogDebugMessage ($"Skipping NativeAOT runtime pack directory '{packLibDir.ItemSpec}': no DSO stub needed");
continue;
}
throw new InvalidOperationException ($"Internal error: archive DSO stub file '{stubPath}' does not exist in runtime pack at {packLibDir}");
}
AndroidTargetArch arch = MonoAndroidHelper.RidToArch (packRID);
stubPaths.Add (arch, stubPath);
}
return new Config (stubPaths, androidBinUtilsDirectory, baseOutputDirectory);
}
static string GetArchOutputPath (AndroidTargetArch targetArch, Config config)
{
return Path.Combine (config.BaseOutputDirectory, MonoAndroidHelper.ArchToRid (targetArch), "wrapped");
}
/// <summary>
/// Puts the indicated file (<paramref name="payloadFilePath"/>) inside an ELF shared library and returns
/// path to the wrapped file.
/// </summary>
public static string WrapIt (TaskLoggingHelper log, Config config, AndroidTargetArch targetArch, string payloadFilePath, string outputFileName)
{
log.LogDebugMessage ($"[{targetArch}] Putting '{payloadFilePath}' inside ELF shared library '{outputFileName}'");
string outputDir = GetArchOutputPath (targetArch, config);
Directory.CreateDirectory (outputDir);
string outputFile = Path.Combine (outputDir, outputFileName);
log.LogDebugMessage ($" output file path: {outputFile}");
if (!config.DSOStubPaths.TryGetValue (targetArch, out string? stubPath)) {
throw new InvalidOperationException ($"Internal error: archive DSO stub location not known for architecture '{targetArch}'");
}
File.Copy (stubPath, outputFile, overwrite: true);
string quotedOutputFile = MonoAndroidHelper.QuoteFileNameArgument (outputFile);
string objcopy = Path.Combine (config.AndroidBinUtilsDirectory, MonoAndroidHelper.GetExecutablePath (config.AndroidBinUtilsDirectory, "llvm-objcopy"));
var args = new List<string> {
"--add-section",
$"payload={MonoAndroidHelper.QuoteFileNameArgument (payloadFilePath)}",
quotedOutputFile,
};
int ret = MonoAndroidHelper.RunProcess (objcopy, String.Join (" ", args), log);
if (ret != 0) {
return outputFile;
}
args.Clear ();
args.Add ("--set-section-flags");
args.Add ("payload=readonly,data");
args.Add ($"--set-section-alignment payload={PayloadSectionAlignment}");
args.Add (quotedOutputFile);
ret = MonoAndroidHelper.RunProcess (objcopy, String.Join (" ", args), log);
return outputFile;
}
/// <summary>
/// Call when all packaging is done. The method will remove all the wrapper shared libraries that were previously
/// created by this class. The reason to do so is to ensure that we don't package any "stale" content and those
/// wrapper files aren't part of any dependency chain so it's hard to check their up to date state.
/// </summary>
public static void CleanUp (Config config)
{
foreach (var kvp in config.DSOStubPaths) {
string outputDir = GetArchOutputPath (kvp.Key, config);
if (!Directory.Exists (outputDir)) {
continue;
}
Directory.Delete (outputDir, recursive: true);
}
}
public static string [] GetDirectoriesToCleanUp (Config config)
{
var dirs = new List<string> ();
foreach (var kvp in config.DSOStubPaths) {
string outputDir = GetArchOutputPath (kvp.Key, config);
if (!Directory.Exists (outputDir)) {
continue;
}
dirs.Add (outputDir);
}
return dirs.ToArray ();
}
}