60 template <
class T>
static void append(std::vector<uint8_t> &
B,
const T &Data) {
62 B.resize(S +
sizeof(
T));
63 memcpy(&B[S], &Data,
sizeof(
T));
74 size_t Pos = B.size();
81 for (
const auto &S : Strings) {
82 B.resize(Pos + S.length() + 1);
83 strcpy(reinterpret_cast<char *>(&B[Pos]), S.c_str());
84 Pos += S.length() + 1;
111 size_t Pos = S.
find(From);
121 return make_error<StringError>(
123 "' with '" + To +
"' failed").str()), object_error::parse_failed);
130 "__NULL_IMPORT_DESCRIPTOR";
137 class ObjectFactory {
144 std::string ImportDescriptorSymbolName;
145 std::string NullThunkSymbolName;
150 ImportDescriptorSymbolName((
"__IMPORT_DESCRIPTOR_" + Library).str()),
151 NullThunkSymbolName((
"\x7f" + Library +
"_NULL_THUNK_DATA").str()) {}
180 ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) {
181 const uint32_t NumberOfSections = 2;
183 const uint32_t NumberOfRelocations = 3;
188 u16(NumberOfSections),
190 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section)) +
195 (ImportName.
size() + 1)),
196 u32(NumberOfSymbols),
204 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'2'},
212 u16(NumberOfRelocations),
216 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'6'},
219 u32(ImportName.
size() + 1),
230 append(Buffer, SectionTable);
234 u32(0), u32(0), u32(0), u32(0), u32(0),
236 append(Buffer, ImportDescriptor);
246 append(Buffer, RelocationTable);
249 auto S = Buffer.size();
250 Buffer.resize(S + ImportName.
size() + 1);
251 memcpy(&Buffer[S], ImportName.
data(), ImportName.
size());
252 Buffer[S + ImportName.
size()] =
'\0';
256 {{{0, 0, 0, 0, 0, 0, 0, 0}},
262 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'2'}},
268 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'6'}},
274 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'4'}},
280 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'5'}},
286 {{{0, 0, 0, 0, 0, 0, 0, 0}},
292 {{{0, 0, 0, 0, 0, 0, 0, 0}},
304 sizeof(
uint32_t) + ImportDescriptorSymbolName.length() + 1;
306 sizeof(
uint32_t) + ImportDescriptorSymbolName.length() + 1 +
307 NullImportDescriptorSymbolName.length() + 1;
308 append(Buffer, SymbolTable);
313 NullThunkSymbolName});
315 StringRef F{
reinterpret_cast<const char *
>(Buffer.data()), Buffer.size()};
320 ObjectFactory::createNullImportDescriptor(std::vector<uint8_t> &Buffer) {
321 const uint32_t NumberOfSections = 1;
327 u16(NumberOfSections),
329 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section)) +
332 u32(NumberOfSymbols),
340 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'3'},
353 append(Buffer, SectionTable);
357 u32(0), u32(0), u32(0), u32(0), u32(0),
359 append(Buffer, ImportDescriptor);
363 {{{0, 0, 0, 0, 0, 0, 0, 0}},
371 append(Buffer, SymbolTable);
376 StringRef F{
reinterpret_cast<const char *
>(Buffer.data()), Buffer.size()};
380 NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) {
381 const uint32_t NumberOfSections = 2;
388 u16(NumberOfSections),
390 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section)) +
395 u32(NumberOfSymbols),
403 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'5'},
416 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'4'},
431 append(Buffer, SectionTable);
445 {{{0, 0, 0, 0, 0, 0, 0, 0}},
453 append(Buffer, SymbolTable);
458 StringRef F{
reinterpret_cast<const char *
>(Buffer.data()), Buffer.size()};
466 size_t ImpSize = ImportName.
size() + Sym.
size() + 2;
469 memset(Buf, 0, Size);
477 Imp->SizeOfData = ImpSize;
479 Imp->OrdinalHint = Ordinal;
480 Imp->TypeInfo = (NameType << 2) | ImportType;
485 memcpy(P, ImportName.
data(), ImportName.
size());
492 std::vector<uint8_t> Buffer;
493 const uint32_t NumberOfSections = 1;
499 u16(NumberOfSections),
501 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section))),
502 u32(NumberOfSymbols),
510 {{
'.',
'd',
'r',
'e',
'c',
't',
'v',
'e'},
520 append(Buffer, SectionTable);
524 {{{
'@',
'c',
'o',
'm',
'p',
'.',
'i',
'd'}},
530 {{{
'@',
'f',
'e',
'a',
't',
'.',
'0',
'0'}},
536 {{{0, 0, 0, 0, 0, 0, 0, 0}},
542 {{{0, 0, 0, 0, 0, 0, 0, 0}},
561 append(Buffer, SymbolTable);
563 (Prefix + Weak).str()});
566 char *Buf = Alloc.
Allocate<
char>(Buffer.size());
567 memcpy(Buf, Buffer.data(), Buffer.size());
575 std::vector<NewArchiveMember> Members;
578 std::vector<uint8_t> ImportDescriptor;
579 Members.push_back(OF.createImportDescriptor(ImportDescriptor));
581 std::vector<uint8_t> NullImportDescriptor;
582 Members.push_back(OF.createNullImportDescriptor(NullImportDescriptor));
584 std::vector<uint8_t> NullThunk;
585 Members.push_back(OF.createNullThunk(NullThunk));
604 :
replace(SymbolName,
E.Name,
E.ExtName);
609 if (!
E.AliasTarget.empty() && *Name !=
E.AliasTarget) {
610 Members.push_back(OF.createWeakExternal(
E.AliasTarget, *Name,
false));
611 Members.push_back(OF.createWeakExternal(
E.AliasTarget, *Name,
true));
616 OF.createShortImport(*Name,
E.Ordinal, ImportType, NameType));
static const std::string NullImportDescriptorSymbolName
This class represents lattice values for constants.
static void append(std::vector< uint8_t > &B, const T &Data)
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
void replace(Container &Cont, typename Container::iterator ContIt, typename Container::iterator ContEnd, RandomAccessIterator ValIt, RandomAccessIterator ValEnd)
Given a sequence container Cont, replace the range [ContIt, ContEnd) with the range [ValIt...
static ImportNameType getNameType(StringRef Sym, StringRef ExtName, MachineTypes Machine, bool MinGW)
void write32le(void *P, uint32_t V)
Error takeError()
Take ownership of the stored error.
detail::packed_endian_specific_integral< uint16_t, little, unaligned > ulittle16_t
static bool is32bit(MachineTypes Machine)
Error writeArchive(StringRef ArcName, ArrayRef< NewArchiveMember > NewMembers, bool WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin, std::unique_ptr< MemoryBuffer > OldArchiveBuf=nullptr)
Machine is based on a 32bit word architecture.
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Tagged union holding either a T or a Error.
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Error writeImportLibrary(StringRef ImportName, StringRef Path, ArrayRef< COFFShortExport > Exports, COFF::MachineTypes Machine, bool MinGW)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
union llvm::object::coff_symbol::@283 Name
LLVM_NODISCARD size_t size() const
size - Get the string size.
COFF::MachineTypes Machine
static uint16_t getImgRelRelocation(MachineTypes Machine)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
The instances of the Type class are immutable: once they are created, they are never changed...
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
Allocate memory in an ever growing pool, as if by bump-pointer.
Line number, reformatted as symbol.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define offsetof(TYPE, MEMBER)
LLVM_NODISCARD size_t find(char C, size_t From=0) const
Search for the first character C in the string.
LLVM_NODISCARD bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
BlockVerifier::State From
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
The import name is the public symbol name, but skipping the leading ?, @, or optionally _...
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
static void writeStringTable(std::vector< uint8_t > &B, ArrayRef< const std::string > Strings)
The import name is identical to the public symbol name.
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
The import name is the public symbol name, but skipping the leading ?, @, or optionally _...
LLVM_NODISCARD StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
Lightweight error class with error context and mandatory checking.
StringRef - Represent a constant reference to a string, i.e.
support::ulittle32_t Offset