1*0fca6ea1SDimitry Andric //===--------- SectCreate.cpp - Emulate ld64's -sectcreate option ---------===//
2*0fca6ea1SDimitry Andric //
3*0fca6ea1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0fca6ea1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0fca6ea1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0fca6ea1SDimitry Andric //
7*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===//
8*0fca6ea1SDimitry Andric
9*0fca6ea1SDimitry Andric #include "llvm/ExecutionEngine/Orc/SectCreate.h"
10*0fca6ea1SDimitry Andric
11*0fca6ea1SDimitry Andric #define DEBUG_TYPE "orc"
12*0fca6ea1SDimitry Andric
13*0fca6ea1SDimitry Andric using namespace llvm::jitlink;
14*0fca6ea1SDimitry Andric
15*0fca6ea1SDimitry Andric namespace llvm::orc {
16*0fca6ea1SDimitry Andric
materialize(std::unique_ptr<MaterializationResponsibility> R)17*0fca6ea1SDimitry Andric void SectCreateMaterializationUnit::materialize(
18*0fca6ea1SDimitry Andric std::unique_ptr<MaterializationResponsibility> R) {
19*0fca6ea1SDimitry Andric auto G = std::make_unique<LinkGraph>(
20*0fca6ea1SDimitry Andric "orc_sectcreate_" + SectName,
21*0fca6ea1SDimitry Andric ObjLinkingLayer.getExecutionSession().getTargetTriple(),
22*0fca6ea1SDimitry Andric getGenericEdgeKindName);
23*0fca6ea1SDimitry Andric
24*0fca6ea1SDimitry Andric auto &Sect = G->createSection(SectName, MP);
25*0fca6ea1SDimitry Andric auto Content = G->allocateContent(
26*0fca6ea1SDimitry Andric ArrayRef<char>(Data->getBuffer().data(), Data->getBuffer().size()));
27*0fca6ea1SDimitry Andric auto &B = G->createContentBlock(Sect, Content, ExecutorAddr(), Alignment, 0);
28*0fca6ea1SDimitry Andric
29*0fca6ea1SDimitry Andric for (auto &[Name, Info] : ExtraSymbols) {
30*0fca6ea1SDimitry Andric auto L = Info.Flags.isStrong() ? Linkage::Strong : Linkage::Weak;
31*0fca6ea1SDimitry Andric auto S = Info.Flags.isExported() ? Scope::Default : Scope::Hidden;
32*0fca6ea1SDimitry Andric G->addDefinedSymbol(B, Info.Offset, *Name, 0, L, S, Info.Flags.isCallable(),
33*0fca6ea1SDimitry Andric true);
34*0fca6ea1SDimitry Andric }
35*0fca6ea1SDimitry Andric
36*0fca6ea1SDimitry Andric ObjLinkingLayer.emit(std::move(R), std::move(G));
37*0fca6ea1SDimitry Andric }
38*0fca6ea1SDimitry Andric
discard(const JITDylib & JD,const SymbolStringPtr & Name)39*0fca6ea1SDimitry Andric void SectCreateMaterializationUnit::discard(const JITDylib &JD,
40*0fca6ea1SDimitry Andric const SymbolStringPtr &Name) {
41*0fca6ea1SDimitry Andric ExtraSymbols.erase(Name);
42*0fca6ea1SDimitry Andric }
43*0fca6ea1SDimitry Andric
getInterface(const ExtraSymbolsMap & ExtraSymbols)44*0fca6ea1SDimitry Andric MaterializationUnit::Interface SectCreateMaterializationUnit::getInterface(
45*0fca6ea1SDimitry Andric const ExtraSymbolsMap &ExtraSymbols) {
46*0fca6ea1SDimitry Andric SymbolFlagsMap SymbolFlags;
47*0fca6ea1SDimitry Andric for (auto &[Name, Info] : ExtraSymbols)
48*0fca6ea1SDimitry Andric SymbolFlags[Name] = Info.Flags;
49*0fca6ea1SDimitry Andric return {std::move(SymbolFlags), nullptr};
50*0fca6ea1SDimitry Andric }
51*0fca6ea1SDimitry Andric
52*0fca6ea1SDimitry Andric } // End namespace llvm::orc.
53