66template <
class T>
static void append(std::vector<uint8_t> &
B,
const T &
Data) {
68 B.resize(S +
sizeof(
T));
69 memcpy(&
B[S], &
Data,
sizeof(
T));
80 size_t Pos =
B.size();
87 for (
const auto &S : Strings) {
88 B.resize(Pos + S.length() + 1);
89 std::copy(S.begin(), S.end(), std::next(
B.begin(), Pos));
90 B[Pos + S.length()] = 0;
91 Pos += S.length() + 1;
128 return make_error<StringError>(
137 "__NULL_IMPORT_DESCRIPTOR";
151 std::string ImportDescriptorSymbolName;
152 std::string NullThunkSymbolName;
156 : Machine(M), ImportName(S), Library(S.
drop_back(4)),
157 ImportDescriptorSymbolName((
"__IMPORT_DESCRIPTOR_" + Library).str()),
158 NullThunkSymbolName((
"\x7f" + Library +
"_NULL_THUNK_DATA").str()) {}
187ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) {
188 const uint32_t NumberOfSections = 2;
190 const uint32_t NumberOfRelocations = 3;
195 u16(NumberOfSections),
197 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section)) +
202 (ImportName.
size() + 1)),
203 u32(NumberOfSymbols),
211 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'2'},
219 u16(NumberOfRelocations),
223 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'6'},
226 u32(ImportName.
size() + 1),
237 append(Buffer, SectionTable);
241 u32(0), u32(0), u32(0), u32(0), u32(0),
243 append(Buffer, ImportDescriptor);
253 append(Buffer, RelocationTable);
256 auto S = Buffer.size();
257 Buffer.resize(S + ImportName.
size() + 1);
258 memcpy(&Buffer[S], ImportName.
data(), ImportName.
size());
259 Buffer[S + ImportName.
size()] =
'\0';
263 {{{0, 0, 0, 0, 0, 0, 0, 0}},
269 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'2'}},
275 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'6'}},
281 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'4'}},
287 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'5'}},
293 {{{0, 0, 0, 0, 0, 0, 0, 0}},
299 {{{0, 0, 0, 0, 0, 0, 0, 0}},
311 sizeof(
uint32_t) + ImportDescriptorSymbolName.length() + 1;
313 sizeof(
uint32_t) + ImportDescriptorSymbolName.length() + 1 +
315 append(Buffer, SymbolTable);
320 NullThunkSymbolName});
322 StringRef F{
reinterpret_cast<const char *
>(Buffer.data()), Buffer.size()};
327ObjectFactory::createNullImportDescriptor(std::vector<uint8_t> &Buffer) {
328 const uint32_t NumberOfSections = 1;
334 u16(NumberOfSections),
336 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section)) +
339 u32(NumberOfSymbols),
347 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'3'},
360 append(Buffer, SectionTable);
364 u32(0), u32(0), u32(0), u32(0), u32(0),
366 append(Buffer, ImportDescriptor);
370 {{{0, 0, 0, 0, 0, 0, 0, 0}},
378 append(Buffer, SymbolTable);
383 StringRef F{
reinterpret_cast<const char *
>(Buffer.data()), Buffer.size()};
387NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) {
388 const uint32_t NumberOfSections = 2;
395 u16(NumberOfSections),
397 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section)) +
402 u32(NumberOfSymbols),
410 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'5'},
423 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'4'},
438 append(Buffer, SectionTable);
452 {{{0, 0, 0, 0, 0, 0, 0, 0}},
460 append(Buffer, SymbolTable);
465 StringRef F{
reinterpret_cast<const char *
>(Buffer.data()), Buffer.size()};
473 size_t ImpSize = ImportName.
size() + Sym.
size() + 2;
476 memset(Buf, 0,
Size);
483 Imp->Machine = Machine;
484 Imp->SizeOfData = ImpSize;
486 Imp->OrdinalHint = Ordinal;
492 memcpy(
P, ImportName.
data(), ImportName.
size());
499 std::vector<uint8_t> Buffer;
500 const uint32_t NumberOfSections = 1;
506 u16(NumberOfSections),
508 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section))),
509 u32(NumberOfSymbols),
517 {{
'.',
'd',
'r',
'e',
'c',
't',
'v',
'e'},
527 append(Buffer, SectionTable);
531 {{{
'@',
'c',
'o',
'm',
'p',
'.',
'i',
'd'}},
537 {{{
'@',
'f',
'e',
'a',
't',
'.',
'0',
'0'}},
543 {{{0, 0, 0, 0, 0, 0, 0, 0}},
549 {{{0, 0, 0, 0, 0, 0, 0, 0}},
568 append(Buffer, SymbolTable);
573 char *Buf =
Alloc.Allocate<
char>(Buffer.size());
574 memcpy(Buf, Buffer.data(), Buffer.size());
582 std::vector<NewArchiveMember> Members;
585 std::vector<uint8_t> ImportDescriptor;
586 Members.push_back(OF.createImportDescriptor(ImportDescriptor));
588 std::vector<uint8_t> NullImportDescriptor;
589 Members.push_back(OF.createNullImportDescriptor(NullImportDescriptor));
591 std::vector<uint8_t> NullThunk;
592 Members.push_back(OF.createNullThunk(NullThunk));
604 StringRef SymbolName =
E.SymbolName.empty() ?
E.Name :
E.SymbolName;
610 ? std::string(SymbolName)
611 :
replace(SymbolName,
E.Name,
E.ExtName);
614 return Name.takeError();
616 if (!
E.AliasTarget.empty() && *
Name !=
E.AliasTarget) {
617 Members.push_back(OF.createWeakExternal(
E.AliasTarget, *
Name,
false));
618 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
StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
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_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 bool is32bit(MachineTypes Machine)
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 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, bool WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin, std::unique_ptr< MemoryBuffer > OldArchiveBuf=nullptr)
support::ulittle32_t Offset
union llvm::object::coff_symbol::@330 Name