53template <
class T>
static void append(std::vector<uint8_t> &
B,
const T &
Data) {
55 B.resize(S +
sizeof(
T));
56 memcpy(&
B[S], &
Data,
sizeof(
T));
67 size_t Pos =
B.size();
74 for (
const auto &S : Strings) {
75 B.resize(Pos + S.length() + 1);
76 std::copy(S.begin(), S.end(), std::next(
B.begin(), Pos));
77 B[Pos + S.length()] = 0;
78 Pos += S.length() + 1;
115 return make_error<StringError>(
124 "__NULL_IMPORT_DESCRIPTOR";
138 std::string ImportDescriptorSymbolName;
139 std::string NullThunkSymbolName;
144 ImportDescriptorSymbolName((
"__IMPORT_DESCRIPTOR_" + Library).str()),
145 NullThunkSymbolName((
"\x7f" + Library +
"_NULL_THUNK_DATA").str()) {}
174ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) {
175 const uint32_t NumberOfSections = 2;
177 const uint32_t NumberOfRelocations = 3;
182 u16(NumberOfSections),
184 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section)) +
189 (ImportName.
size() + 1)),
190 u32(NumberOfSymbols),
198 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'2'},
206 u16(NumberOfRelocations),
210 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'6'},
213 u32(ImportName.
size() + 1),
224 append(Buffer, SectionTable);
228 u32(0), u32(0), u32(0), u32(0), u32(0),
230 append(Buffer, ImportDescriptor);
240 append(Buffer, RelocationTable);
243 auto S = Buffer.size();
244 Buffer.resize(S + ImportName.
size() + 1);
245 memcpy(&Buffer[S], ImportName.
data(), ImportName.
size());
246 Buffer[S + ImportName.
size()] =
'\0';
250 {{{0, 0, 0, 0, 0, 0, 0, 0}},
256 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'2'}},
262 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'6'}},
268 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'4'}},
274 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'5'}},
280 {{{0, 0, 0, 0, 0, 0, 0, 0}},
286 {{{0, 0, 0, 0, 0, 0, 0, 0}},
298 sizeof(
uint32_t) + ImportDescriptorSymbolName.length() + 1;
300 sizeof(
uint32_t) + ImportDescriptorSymbolName.length() + 1 +
302 append(Buffer, SymbolTable);
307 NullThunkSymbolName});
309 StringRef F{
reinterpret_cast<const char *
>(Buffer.data()), Buffer.size()};
314ObjectFactory::createNullImportDescriptor(std::vector<uint8_t> &Buffer) {
315 const uint32_t NumberOfSections = 1;
321 u16(NumberOfSections),
323 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section)) +
326 u32(NumberOfSymbols),
334 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'3'},
347 append(Buffer, SectionTable);
351 u32(0), u32(0), u32(0), u32(0), u32(0),
353 append(Buffer, ImportDescriptor);
357 {{{0, 0, 0, 0, 0, 0, 0, 0}},
365 append(Buffer, SymbolTable);
370 StringRef F{
reinterpret_cast<const char *
>(Buffer.data()), Buffer.size()};
374NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) {
375 const uint32_t NumberOfSections = 2;
382 u16(NumberOfSections),
384 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section)) +
389 u32(NumberOfSymbols),
397 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'5'},
410 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'4'},
425 append(Buffer, SectionTable);
439 {{{0, 0, 0, 0, 0, 0, 0, 0}},
447 append(Buffer, SymbolTable);
452 StringRef F{
reinterpret_cast<const char *
>(Buffer.data()), Buffer.size()};
460 size_t ImpSize = ImportName.
size() +
Sym.size() + 2;
463 memset(Buf, 0,
Size);
470 Imp->Machine = Machine;
471 Imp->SizeOfData = ImpSize;
473 Imp->OrdinalHint = Ordinal;
477 memcpy(
P,
Sym.data(),
Sym.size());
479 memcpy(
P, ImportName.
data(), ImportName.
size());
486 std::vector<uint8_t> Buffer;
487 const uint32_t NumberOfSections = 1;
493 u16(NumberOfSections),
495 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section))),
496 u32(NumberOfSymbols),
504 {{
'.',
'd',
'r',
'e',
'c',
't',
'v',
'e'},
514 append(Buffer, SectionTable);
518 {{{
'@',
'c',
'o',
'm',
'p',
'.',
'i',
'd'}},
524 {{{
'@',
'f',
'e',
'a',
't',
'.',
'0',
'0'}},
530 {{{0, 0, 0, 0, 0, 0, 0, 0}},
536 {{{0, 0, 0, 0, 0, 0, 0, 0}},
555 append(Buffer, SymbolTable);
560 char *Buf =
Alloc.Allocate<
char>(Buffer.size());
561 memcpy(Buf, Buffer.data(), Buffer.size());
569 std::vector<NewArchiveMember> Members;
572 std::vector<uint8_t> ImportDescriptor;
573 Members.push_back(OF.createImportDescriptor(ImportDescriptor));
575 std::vector<uint8_t> NullImportDescriptor;
576 Members.push_back(OF.createNullImportDescriptor(NullImportDescriptor));
578 std::vector<uint8_t> NullThunk;
579 Members.push_back(OF.createNullThunk(NullThunk));
591 StringRef SymbolName =
E.SymbolName.empty() ?
E.Name :
E.SymbolName;
597 ? std::string(SymbolName)
598 :
replace(SymbolName,
E.Name,
E.ExtName);
601 return Name.takeError();
603 if (!
E.AliasTarget.empty() && *
Name !=
E.AliasTarget) {
604 Members.push_back(OF.createWeakExternal(
E.AliasTarget, *
Name,
false));
605 Members.push_back(OF.createWeakExternal(
E.AliasTarget, *
Name,
true));
#define offsetof(TYPE, MEMBER)
This file defines the BumpPtrAllocator interface.
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Allocate memory in an ever growing pool, as if by bump-pointer.
Lightweight error class with error context and mandatory checking.
Tagged union holding either a T or a Error.
StringRef - Represent a constant reference to a string, i.e.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
constexpr size_t size() const
size - Get the string size.
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
bool startswith(StringRef Prefix) const
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
static constexpr size_t npos
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ IMAGE_FILE_MACHINE_ARM64
@ IMAGE_FILE_MACHINE_AMD64
@ IMAGE_FILE_MACHINE_ARM64EC
@ IMAGE_FILE_MACHINE_I386
@ IMAGE_FILE_MACHINE_ARM64X
@ IMAGE_FILE_MACHINE_ARMNT
@ IMAGE_SCN_CNT_INITIALIZED_DATA
@ IMAGE_REL_ARM64_ADDR32NB
@ IMAGE_REL_AMD64_ADDR32NB
@ IMAGE_SYM_CLASS_SECTION
Line number, reformatted as symbol.
@ IMAGE_SYM_CLASS_EXTERNAL
External symbol.
@ IMAGE_SYM_CLASS_NULL
No symbol.
@ IMAGE_SYM_CLASS_WEAK_EXTERNAL
Duplicate tag.
@ IMAGE_SYM_CLASS_STATIC
Static.
@ IMAGE_WEAK_EXTERN_SEARCH_ALIAS
@ IMPORT_ORDINAL
Import is by ordinal.
@ IMPORT_NAME
The import name is identical to the public symbol name.
@ IMPORT_NAME_UNDECORATE
The import name is the public symbol name, but skipping the leading ?, @, or optionally _,...
@ IMPORT_NAME_NOPREFIX
The import name is the public symbol name, but skipping the leading ?, @, or optionally _.
@ IMAGE_FILE_32BIT_MACHINE
Machine is based on a 32bit word architecture.
static void append(std::vector< uint8_t > &B, const T &Data)
static Expected< std::string > replace(StringRef S, StringRef From, StringRef To)
static uint16_t getImgRelRelocation(MachineTypes Machine)
static void writeStringTable(std::vector< uint8_t > &B, ArrayRef< const std::string > Strings)
static const std::string NullImportDescriptorSymbolName
static ImportNameType getNameType(StringRef Sym, StringRef ExtName, MachineTypes Machine, bool MinGW)
Error writeImportLibrary(StringRef ImportName, StringRef Path, ArrayRef< COFFShortExport > Exports, COFF::MachineTypes Machine, bool MinGW)
void write32le(void *P, uint32_t V)
detail::packed_endian_specific_integral< uint16_t, little, unaligned > ulittle16_t
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
StringRef stem(StringRef path, Style style=Style::native)
Get stem.
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
This is an optimization pass for GlobalISel generic memory operations.
Error writeArchive(StringRef ArcName, ArrayRef< NewArchiveMember > NewMembers, SymtabWritingMode WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin, std::unique_ptr< MemoryBuffer > OldArchiveBuf=nullptr, bool IsEC=false)
support::ulittle32_t Offset
union llvm::object::coff_symbol::@342 Name