28 using namespace llvm::COFF;
29 using namespace llvm::object;
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;
103 size_t Pos = S.
find(From);
113 return make_error<StringError>(
115 "' with '" + To +
"' failed").str()), object_error::parse_failed);
122 "__NULL_IMPORT_DESCRIPTOR";
129 class ObjectFactory {
172 ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) {
173 const uint32_t NumberOfSections = 2;
175 const uint32_t NumberOfRelocations = 3;
180 u16(NumberOfSections),
182 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section)) +
188 u32(NumberOfSymbols),
196 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'2'},
204 u16(NumberOfRelocations),
208 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'6'},
222 append(Buffer, SectionTable);
226 u32(0), u32(0), u32(0), u32(0), u32(0),
228 append(Buffer, ImportDescriptor);
238 append(Buffer, RelocationTable);
241 auto S = Buffer.size();
248 {{{0, 0, 0, 0, 0, 0, 0, 0}},
254 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'2'}},
260 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'6'}},
266 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'4'}},
272 {{{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'5'}},
278 {{{0, 0, 0, 0, 0, 0, 0, 0}},
284 {{{0, 0, 0, 0, 0, 0, 0, 0}},
300 append(Buffer, SymbolTable);
307 StringRef F{
reinterpret_cast<const char *
>(Buffer.data()), Buffer.size()};
312 ObjectFactory::createNullImportDescriptor(std::vector<uint8_t> &Buffer) {
313 const uint32_t NumberOfSections = 1;
319 u16(NumberOfSections),
321 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section)) +
324 u32(NumberOfSymbols),
332 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'3'},
345 append(Buffer, SectionTable);
349 u32(0), u32(0), u32(0), u32(0), u32(0),
351 append(Buffer, ImportDescriptor);
355 {{{0, 0, 0, 0, 0, 0, 0, 0}},
363 append(Buffer, SymbolTable);
368 StringRef F{
reinterpret_cast<const char *
>(Buffer.data()), Buffer.size()};
372 NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) {
373 const uint32_t NumberOfSections = 2;
380 u16(NumberOfSections),
382 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section)) +
387 u32(NumberOfSymbols),
395 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'5'},
408 {{
'.',
'i',
'd',
'a',
't',
'a',
'$',
'4'},
423 append(Buffer, SectionTable);
437 {{{0, 0, 0, 0, 0, 0, 0, 0}},
445 append(Buffer, SymbolTable);
450 StringRef F{
reinterpret_cast<const char *
>(Buffer.data()), Buffer.size()};
461 memset(Buf, 0, Size);
469 Imp->SizeOfData = ImpSize;
471 Imp->OrdinalHint = Ordinal;
472 Imp->TypeInfo = (NameType << 2) | ImportType;
484 std::vector<uint8_t> Buffer;
485 const uint32_t NumberOfSections = 1;
491 u16(NumberOfSections),
493 u32(
sizeof(Header) + (NumberOfSections *
sizeof(
coff_section))),
494 u32(NumberOfSymbols),
502 {{
'.',
'd',
'r',
'e',
'c',
't',
'v',
'e'},
512 append(Buffer, SectionTable);
516 {{{
'@',
'c',
'o',
'm',
'p',
'.',
'i',
'd'}},
522 {{{
'@',
'f',
'e',
'a',
't',
'.',
'0',
'0'}},
528 {{{0, 0, 0, 0, 0, 0, 0, 0}},
534 {{{0, 0, 0, 0, 0, 0, 0, 0}},
540 {{{2, 0, 0, 0, 3, 0, 0, 0}}, u32(0), u16(0), u16(0), uint8_t(0), 0},
548 append(Buffer, SymbolTable);
550 (Prefix + Weak).str()});
554 memcpy(Buf, Buffer.data(), Buffer.size());
562 std::vector<NewArchiveMember> Members;
565 std::vector<uint8_t> ImportDescriptor;
566 Members.push_back(OF.createImportDescriptor(ImportDescriptor));
568 std::vector<uint8_t> NullImportDescriptor;
569 Members.push_back(OF.createNullImportDescriptor(NullImportDescriptor));
571 std::vector<uint8_t> NullThunk;
572 Members.push_back(OF.createNullThunk(NullThunk));
578 if (
E.isWeak() && MakeWeakAliases) {
579 Members.push_back(OF.createWeakExternal(
E.Name,
E.ExtName,
false));
580 Members.push_back(OF.createWeakExternal(
E.Name,
E.ExtName,
true));
590 StringRef SymbolName =
E.SymbolName.empty() ?
E.Name :
E.SymbolName;
594 :
replace(SymbolName,
E.Name,
E.ExtName);
601 OF.createShortImport(*Name,
E.Ordinal, ImportType, NameType));
604 std::pair<StringRef, std::error_code> Result =
608 return Result.second;
static const std::string NullImportDescriptorSymbolName
static ImportNameType getNameType(StringRef Sym, StringRef ExtName, MachineTypes Machine)
static void append(std::vector< uint8_t > &B, const T &Data)
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)
The import name is the public symbol name, but skipping the leading ?, @, or optionally _...
std::pair< StringRef, std::error_code > writeArchive(StringRef ArcName, std::vector< NewArchiveMember > &NewMembers, bool WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin, std::unique_ptr< MemoryBuffer > OldArchiveBuf=nullptr)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The import name is identical to the public symbol name.
Tagged union holding either a T or a Error.
union llvm::object::coff_symbol::@221 Name
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Machine is based on a 32bit word architecture.
static uint16_t getImgRelRelocation(MachineTypes Machine)
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.
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.
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
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).
std::error_code writeImportLibrary(StringRef ImportName, StringRef Path, ArrayRef< COFFShortExport > Exports, COFF::MachineTypes Machine, bool MakeWeakAliases)
std::string ImportDescriptorSymbolName
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
llvm_unreachable("Unknown RegBank!")
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
static void writeStringTable(raw_fd_ostream &Out, StringRef ArcName, ArrayRef< NewArchiveMember > Members, std::vector< unsigned > &StringMapIndexes, bool Thin)
std::string NullThunkSymbolName
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).
StringRef - Represent a constant reference to a string, i.e.
static Expected< std::string > replace(StringRef S, StringRef From, StringRef To)
The import name is the public symbol name, but skipping the leading ?, @, or optionally _...
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
support::ulittle32_t Offset