61 template <
class T>
static void append(std::vector<uint8_t> &
B,
const T &Data) {
63 B.resize(S +
sizeof(
T));
64 memcpy(&B[S], &Data,
sizeof(
T));
75 size_t Pos = B.size();
82 for (
const auto &S : Strings) {
83 B.resize(Pos + S.length() + 1);
84 strcpy(reinterpret_cast<char *>(&B[Pos]), S.c_str());
85 Pos += S.length() + 1;
112 size_t Pos = S.
find(From);
122 return make_error<StringError>(
124 "' with '" + To +
"' failed").str()), object_error::parse_failed);
131 "__NULL_IMPORT_DESCRIPTOR";
138 class ObjectFactory {
145 std::string ImportDescriptorSymbolName;
146 std::string NullThunkSymbolName;
151 ImportDescriptorSymbolName((
"__IMPORT_DESCRIPTOR_" + Library).str()),
152 NullThunkSymbolName((
"\x7f" + Library +
"_NULL_THUNK_DATA").str()) {}
181 ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) {
182 const uint32_t NumberOfSections = 2;
184 const uint32_t NumberOfRelocations = 3;
189 u16(NumberOfSections),
191 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section)) +
196 (ImportName.
size() + 1)),
197 u32(NumberOfSymbols),
205 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'2'},
213 u16(NumberOfRelocations),
217 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'6'},
220 u32(ImportName.
size() + 1),
231 append(Buffer, SectionTable);
235 u32(0), u32(0), u32(0), u32(0), u32(0),
237 append(Buffer, ImportDescriptor);
247 append(Buffer, RelocationTable);
250 auto S = Buffer.size();
251 Buffer.resize(S + ImportName.
size() + 1);
253 Buffer[S + ImportName.
size()] =
'\0';
257 {{{0, 0, 0, 0, 0, 0, 0, 0}},
263 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'2'}},
269 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'6'}},
275 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'4'}},
281 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'5'}},
287 {{{0, 0, 0, 0, 0, 0, 0, 0}},
293 {{{0, 0, 0, 0, 0, 0, 0, 0}},
305 sizeof(
uint32_t) + ImportDescriptorSymbolName.length() + 1;
307 sizeof(
uint32_t) + ImportDescriptorSymbolName.length() + 1 +
308 NullImportDescriptorSymbolName.length() + 1;
309 append(Buffer, SymbolTable);
314 NullThunkSymbolName});
316 StringRef F{
reinterpret_cast<const char *
>(Buffer.data()), Buffer.size()};
321 ObjectFactory::createNullImportDescriptor(std::vector<uint8_t> &Buffer) {
322 const uint32_t NumberOfSections = 1;
328 u16(NumberOfSections),
330 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section)) +
333 u32(NumberOfSymbols),
341 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'3'},
354 append(Buffer, SectionTable);
358 u32(0), u32(0), u32(0), u32(0), u32(0),
360 append(Buffer, ImportDescriptor);
364 {{{0, 0, 0, 0, 0, 0, 0, 0}},
372 append(Buffer, SymbolTable);
377 StringRef F{
reinterpret_cast<const char *
>(Buffer.data()), Buffer.size()};
381 NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) {
382 const uint32_t NumberOfSections = 2;
389 u16(NumberOfSections),
391 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section)) +
396 u32(NumberOfSymbols),
404 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'5'},
417 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'4'},
432 append(Buffer, SectionTable);
446 {{{0, 0, 0, 0, 0, 0, 0, 0}},
454 append(Buffer, SymbolTable);
459 StringRef F{
reinterpret_cast<const char *
>(Buffer.data()), Buffer.size()};
467 size_t ImpSize = ImportName.
size() + Sym.
size() + 2;
478 Imp->SizeOfData = ImpSize;
480 Imp->OrdinalHint = Ordinal;
481 Imp->TypeInfo = (NameType << 2) | ImportType;
493 std::vector<uint8_t> Buffer;
494 const uint32_t NumberOfSections = 1;
500 u16(NumberOfSections),
502 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section))),
503 u32(NumberOfSymbols),
511 {{
'.',
'd',
'r',
'e',
'c',
't',
'v',
'e'},
521 append(Buffer, SectionTable);
525 {{{
'@',
'c',
'o',
'm',
'p',
'.',
'i',
'd'}},
531 {{{
'@',
'f',
'e',
'a',
't',
'.',
'0',
'0'}},
537 {{{0, 0, 0, 0, 0, 0, 0, 0}},
543 {{{0, 0, 0, 0, 0, 0, 0, 0}},
562 append(Buffer, SymbolTable);
564 (Prefix + Weak).str()});
567 char *Buf = Alloc.
Allocate<
char>(Buffer.size());
568 memcpy(Buf, Buffer.data(), Buffer.size());
576 std::vector<NewArchiveMember> Members;
579 std::vector<uint8_t> ImportDescriptor;
580 Members.push_back(OF.createImportDescriptor(ImportDescriptor));
582 std::vector<uint8_t> NullImportDescriptor;
583 Members.push_back(OF.createNullImportDescriptor(NullImportDescriptor));
585 std::vector<uint8_t> NullThunk;
586 Members.push_back(OF.createNullThunk(NullThunk));
602 :
replace(SymbolName,
E.Name,
E.ExtName);
607 if (!
E.AliasTarget.empty() && *Name !=
E.AliasTarget) {
608 Members.push_back(OF.createWeakExternal(
E.AliasTarget, *Name,
false));
609 Members.push_back(OF.createWeakExternal(
E.AliasTarget, *Name,
true));
614 OF.createShortImport(*Name,
E.Ordinal, ImportType, NameType));
static const std::string NullImportDescriptorSymbolName
This class represents lattice values for constants.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
union llvm::object::coff_symbol::@273 Name
static void append(std::vector< uint8_t > &B, const T &Data)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
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)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Error writeArchive(StringRef ArcName, ArrayRef< NewArchiveMember > NewMembers, bool WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin, std::unique_ptr< MemoryBuffer > OldArchiveBuf=nullptr)
amdgpu Simplify well known AMD library false Value Value const Twine & Name
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 LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
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)...
COFF::MachineTypes Machine
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
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")
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
#define offsetof(TYPE, MEMBER)
static void replace(Module &M, GlobalVariable *Old, GlobalVariable *New)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
BlockVerifier::State From
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.
The import name is the public symbol name, but skipping the leading ?, @, or optionally _...
Lightweight error class with error context and mandatory checking.
StringRef - Represent a constant reference to a string, i.e.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find(char C, size_t From=0) const
Search for the first character C in the string.
support::ulittle32_t Offset