Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crossgen2: Add --imagebase option #65567

Merged
merged 1 commit into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,15 @@ public void EmitPortableExecutable()

if (_nodeFactory.CompilationModuleGroup.IsCompositeBuildMode && _componentModule == null)
{
headerBuilder = PEHeaderProvider.Create(Subsystem.Unknown, _nodeFactory.Target);
headerBuilder = PEHeaderProvider.Create(Subsystem.Unknown, _nodeFactory.Target, _nodeFactory.ImageBase);
peIdProvider = new Func<IEnumerable<Blob>, BlobContentId>(content => BlobContentId.FromHash(CryptographicHashProvider.ComputeSourceHash(content)));
timeDateStamp = null;
r2rHeaderExportSymbol = _nodeFactory.Header;
}
else
{
PEReader inputPeReader = (_componentModule != null ? _componentModule.PEReader : _nodeFactory.CompilationModuleGroup.CompilationModuleSet.First().PEReader);
headerBuilder = PEHeaderProvider.Create(inputPeReader.PEHeaders.PEHeader.Subsystem, _nodeFactory.Target);
headerBuilder = PEHeaderProvider.Create(inputPeReader.PEHeaders.PEHeader.Subsystem, _nodeFactory.Target, _nodeFactory.ImageBase);
timeDateStamp = inputPeReader.PEHeaders.CoffHeader.TimeDateStamp;
r2rHeaderExportSymbol = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public sealed class NodeFactory

public CompositeImageSettings CompositeImageSettings { get; set; }

public ulong ImageBase;

public bool MarkingComplete => _markingComplete;

public void SetMarkingComplete()
Expand Down Expand Up @@ -155,7 +157,8 @@ public NodeFactory(
CopiedCorHeaderNode corHeaderNode,
DebugDirectoryNode debugDirectoryNode,
ResourceData win32Resources,
ReadyToRunFlags flags)
ReadyToRunFlags flags,
ulong imageBase)
{
TypeSystemContext = context;
CompilationModuleGroup = compilationModuleGroup;
Expand All @@ -167,6 +170,7 @@ public NodeFactory(
DebugDirectoryNode = debugDirectoryNode;
Resolver = compilationModuleGroup.Resolver;
Header = new GlobalHeaderNode(Target, flags);
ImageBase = imageBase;
if (!win32Resources.IsEmpty)
Win32ResourcesNode = new Win32ResourcesNode(win32Resources);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,8 @@ private void RewriteComponentFile(string inputFile, string outputFile, string ow
copiedCorHeader,
debugDirectory,
win32Resources: new Win32Resources.ResourceData(inputModule),
flags);
flags,
_nodeFactory.ImageBase);

IComparer<DependencyNodeCore<NodeFactory>> comparer = new SortableDependencyNode.ObjectNodeComparer(new CompilerComparer());
DependencyAnalyzerBase<NodeFactory> componentGraph = new DependencyAnalyzer<NoLogStrategy<NodeFactory>, NodeFactory>(componentFactory, comparer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public sealed class ReadyToRunCodegenCompilationBuilder : CompilationBuilder
private int _customPESectionAlignment;
private bool _verifyTypeAndFieldLayout;
private CompositeImageSettings _compositeImageSettings;
private ulong _imageBase;

private string _jitPath;
private string _outputFile;
Expand Down Expand Up @@ -198,6 +199,12 @@ public ReadyToRunCodegenCompilationBuilder UseCompositeImageSettings(CompositeIm
return this;
}

public ReadyToRunCodegenCompilationBuilder UseImageBase(ulong imageBase)
{
_imageBase = imageBase;
return this;
}

public override ICompilation ToCompilation()
{
// TODO: only copy COR headers for single-assembly build and for composite build with embedded MSIL
Expand Down Expand Up @@ -240,7 +247,9 @@ public override ICompilation ToCompilation()
corHeaderNode,
debugDirectoryNode,
win32Resources,
flags);
flags,
_imageBase
);

factory.CompositeImageSettings = _compositeImageSettings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,15 +673,13 @@ static class PEHeaderProvider
/// </summary>
/// <param name="subsystem">Targeting subsystem</param>
/// <param name="target">Target architecture to set in the header</param>
public static PEHeaderBuilder Create(Subsystem subsystem, TargetDetails target)
public static PEHeaderBuilder Create(Subsystem subsystem, TargetDetails target, ulong imageBase)
{
bool is64BitTarget = target.PointerSize == sizeof(long);

Characteristics imageCharacteristics = Characteristics.ExecutableImage | Characteristics.Dll;
imageCharacteristics |= is64BitTarget ? Characteristics.LargeAddressAware : Characteristics.Bit32Machine;

ulong imageBase = is64BitTarget ? PE64HeaderConstants.DllImageBase : PE32HeaderConstants.ImageBase;

int fileAlignment = 0x200;
bool isWindowsOr32bit = target.IsWindows || !is64BitTarget;
if (isWindowsOr32bit)
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/tools/aot/crossgen2/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ internal class CommandLineOptions
public string FileLayout;
public bool VerifyTypeAndFieldLayout;
public string CallChainProfileFile;
public string ImageBase;

public string SingleMethodTypeName;
public string SingleMethodName;
Expand Down Expand Up @@ -143,6 +144,7 @@ public CommandLineOptions(string[] args)
syntax.DefineOption("waitfordebugger", ref WaitForDebugger, SR.WaitForDebuggerOption);
syntax.DefineOptionList("codegenopt|codegen-options", ref CodegenOptions, SR.CodeGenOptions);
syntax.DefineOption("resilient", ref Resilient, SR.ResilientOption);
syntax.DefineOption("imagebase", ref ImageBase, SR.ImageBase);

syntax.DefineOption("targetarch", ref TargetArch, SR.TargetArchOption);
syntax.DefineOption("targetos", ref TargetOS, SR.TargetOSOption);
Expand Down
14 changes: 14 additions & 0 deletions src/coreclr/tools/aot/crossgen2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal class Program
public TargetArchitecture _targetArchitecture;
private bool _armelAbi = false;
public OptimizationMode _optimizationMode;
private ulong _imageBase;

// File names as strings in args
private Dictionary<string, string> _inputFilePaths = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Expand Down Expand Up @@ -318,6 +319,16 @@ private InstructionSetSupport ConfigureInstructionSetSupport()
_targetArchitecture);
}

private void ConfigureImageBase(TargetDetails targetDetails)
{
bool is64BitTarget = targetDetails.PointerSize == sizeof(long);

if (_commandLineOptions.ImageBase != null)
_imageBase = is64BitTarget ? Convert.ToUInt64(_commandLineOptions.ImageBase, 16) : Convert.ToUInt32(_commandLineOptions.ImageBase, 16);
else
_imageBase = is64BitTarget ? PEWriter.PE64HeaderConstants.DllImageBase : PEWriter.PE32HeaderConstants.ImageBase;
}

private int Run(string[] args)
{
InitializeDefaultOptions();
Expand All @@ -343,6 +354,8 @@ private int Run(string[] args)

var targetDetails = new TargetDetails(_targetArchitecture, _targetOS, _armelAbi ? TargetAbi.CoreRTArmel : TargetAbi.CoreRT, instructionSetSupport.GetVectorTSimdVector());

ConfigureImageBase(targetDetails);

bool versionBubbleIncludesCoreLib = false;
if (_commandLineOptions.InputBubble)
{
Expand Down Expand Up @@ -743,6 +756,7 @@ private void RunSingleCompilation(Dictionary<string, string> inFilePaths, Instru
.UseCustomPESectionAlignment(_commandLineOptions.CustomPESectionAlignment)
.UseVerifyTypeAndFieldLayout(_commandLineOptions.VerifyTypeAndFieldLayout)
.GenerateOutputFile(outFile)
.UseImageBase(_imageBase)
.UseILProvider(ilProvider)
.UseBackendOptions(_commandLineOptions.CodegenOptions)
.UseLogger(logger)
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/tools/aot/crossgen2/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,7 @@
<data name="UnsupportedInputFileExtension" xml:space="preserve">
<value>Input file with '{0}' extension not supported</value>
</data>
<data name="ImageBase" xml:space="preserve">
<value>Hexademical value to set target PE-file ImageBase field</value>
</data>
</root>