LLVM 19.0.0git
COFFImportFile.cpp
Go to the documentation of this file.
1//===- COFFImportFile.cpp - COFF short import file implementation ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the writeImportLibrary function.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/Twine.h"
16#include "llvm/Object/Archive.h"
18#include "llvm/Object/COFF.h"
20#include "llvm/Support/Endian.h"
21#include "llvm/Support/Error.h"
23#include "llvm/Support/Path.h"
24
25#include <cstdint>
26#include <string>
27#include <vector>
28
29using namespace llvm::COFF;
30using namespace llvm::object;
31using namespace llvm;
32
33namespace llvm {
34namespace object {
35
37 switch (getMachine()) {
39 return "COFF-import-file-i386";
41 return "COFF-import-file-x86-64";
43 return "COFF-import-file-ARM";
45 return "COFF-import-file-ARM64";
47 return "COFF-import-file-ARM64EC";
49 return "COFF-import-file-ARM64X";
50 default:
51 return "COFF-import-file-<unknown arch>";
52 }
53}
54
57 StringRef name = Data.getBuffer().substr(sizeof(*hdr)).split('\0').first;
58
59 auto ltrim1 = [](StringRef s, StringRef chars) {
60 return !s.empty() && chars.contains(s[0]) ? s.substr(1) : s;
61 };
62
63 switch (hdr->getNameType()) {
64 case IMPORT_ORDINAL:
65 name = "";
66 break;
68 name = ltrim1(name, "?@_");
69 break;
71 name = ltrim1(name, "?@_");
72 name = name.substr(0, name.find('@'));
73 break;
75 // Skip DLL name
76 name = Data.getBuffer().substr(sizeof(*hdr) + name.size() + 1);
77 name = name.split('\0').second.split('\0').first;
78 break;
79 }
80 default:
81 break;
82 }
83
84 return name;
85}
86
88 switch (Symb.p) {
89 case ImpSymbol:
90 OS << "__imp_";
91 break;
92 case ECAuxSymbol:
93 OS << "__imp_aux_";
94 break;
95 }
96 const char *Name = Data.getBufferStart() + sizeof(coff_import_header);
97 if (Symb.p != ECThunkSymbol && COFF::isArm64EC(getMachine())) {
98 if (std::optional<std::string> DemangledName =
100 OS << StringRef(*DemangledName);
101 return Error::success();
102 }
103 }
104 OS << StringRef(Name);
105 return Error::success();
106}
107
109 switch (Machine) {
110 default:
111 llvm_unreachable("unsupported machine");
122 }
123}
124
125template <class T> static void append(std::vector<uint8_t> &B, const T &Data) {
126 size_t S = B.size();
127 B.resize(S + sizeof(T));
128 memcpy(&B[S], &Data, sizeof(T));
129}
130
131static void writeStringTable(std::vector<uint8_t> &B,
133 // The COFF string table consists of a 4-byte value which is the size of the
134 // table, including the length field itself. This value is followed by the
135 // string content itself, which is an array of null-terminated C-style
136 // strings. The termination is important as they are referenced to by offset
137 // by the symbol entity in the file format.
138
139 size_t Pos = B.size();
140 size_t Offset = B.size();
141
142 // Skip over the length field, we will fill it in later as we will have
143 // computed the length while emitting the string content itself.
144 Pos += sizeof(uint32_t);
145
146 for (const auto &S : Strings) {
147 B.resize(Pos + S.length() + 1);
148 std::copy(S.begin(), S.end(), std::next(B.begin(), Pos));
149 B[Pos + S.length()] = 0;
150 Pos += S.length() + 1;
151 }
152
153 // Backfill the length of the table now that it has been computed.
156}
157
159 MachineTypes Machine, bool MinGW) {
160 // A decorated stdcall function in MSVC is exported with the
161 // type IMPORT_NAME, and the exported function name includes the
162 // the leading underscore. In MinGW on the other hand, a decorated
163 // stdcall function still omits the underscore (IMPORT_NAME_NOPREFIX).
164 // See the comment in isDecorated in COFFModuleDefinition.cpp for more
165 // details.
166 if (ExtName.starts_with("_") && ExtName.contains('@') && !MinGW)
167 return IMPORT_NAME;
168 if (Sym != ExtName)
170 if (Machine == IMAGE_FILE_MACHINE_I386 && Sym.starts_with("_"))
172 return IMPORT_NAME;
173}
174
176 StringRef To) {
177 size_t Pos = S.find(From);
178
179 // From and To may be mangled, but substrings in S may not.
180 if (Pos == StringRef::npos && From.starts_with("_") && To.starts_with("_")) {
181 From = From.substr(1);
182 To = To.substr(1);
183 Pos = S.find(From);
184 }
185
186 if (Pos == StringRef::npos) {
187 return make_error<StringError>(
188 StringRef(Twine(S + ": replacing '" + From +
189 "' with '" + To + "' failed").str()), object_error::parse_failed);
190 }
191
192 return (Twine(S.substr(0, Pos)) + To + S.substr(Pos + From.size())).str();
193}
194
195namespace {
196// This class constructs various small object files necessary to support linking
197// symbols imported from a DLL. The contents are pretty strictly defined and
198// nearly entirely static. The details of the structures files are defined in
199// WINNT.h and the PE/COFF specification.
200class ObjectFactory {
201 using u16 = support::ulittle16_t;
202 using u32 = support::ulittle32_t;
203 MachineTypes NativeMachine;
204 BumpPtrAllocator Alloc;
205 StringRef ImportName;
206 StringRef Library;
207 std::string ImportDescriptorSymbolName;
208 std::string NullThunkSymbolName;
209
210public:
211 ObjectFactory(StringRef S, MachineTypes M)
212 : NativeMachine(M), ImportName(S), Library(llvm::sys::path::stem(S)),
213 ImportDescriptorSymbolName((ImportDescriptorPrefix + Library).str()),
214 NullThunkSymbolName(
215 (NullThunkDataPrefix + Library + NullThunkDataSuffix).str()) {}
216
217 // Creates an Import Descriptor. This is a small object file which contains a
218 // reference to the terminators and contains the library name (entry) for the
219 // import name table. It will force the linker to construct the necessary
220 // structure to import symbols from the DLL.
221 NewArchiveMember createImportDescriptor(std::vector<uint8_t> &Buffer);
222
223 // Creates a NULL import descriptor. This is a small object file whcih
224 // contains a NULL import descriptor. It is used to terminate the imports
225 // from a specific DLL.
226 NewArchiveMember createNullImportDescriptor(std::vector<uint8_t> &Buffer);
227
228 // Create a NULL Thunk Entry. This is a small object file which contains a
229 // NULL Import Address Table entry and a NULL Import Lookup Table Entry. It
230 // is used to terminate the IAT and ILT.
231 NewArchiveMember createNullThunk(std::vector<uint8_t> &Buffer);
232
233 // Create a short import file which is described in PE/COFF spec 7. Import
234 // Library Format.
235 NewArchiveMember createShortImport(StringRef Sym, uint16_t Ordinal,
237 StringRef ExportName,
239
240 // Create a weak external file which is described in PE/COFF Aux Format 3.
241 NewArchiveMember createWeakExternal(StringRef Sym, StringRef Weak, bool Imp,
243
244 bool is64Bit() const { return COFF::is64Bit(NativeMachine); }
245};
246} // namespace
247
249ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) {
250 const uint32_t NumberOfSections = 2;
251 const uint32_t NumberOfSymbols = 7;
252 const uint32_t NumberOfRelocations = 3;
253
254 // COFF Header
255 coff_file_header Header{
256 u16(NativeMachine),
257 u16(NumberOfSections),
258 u32(0),
259 u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) +
260 // .idata$2
262 NumberOfRelocations * sizeof(coff_relocation) +
263 // .idata$4
264 (ImportName.size() + 1)),
265 u32(NumberOfSymbols),
266 u16(0),
268 };
269 append(Buffer, Header);
270
271 // Section Header Table
272 const coff_section SectionTable[NumberOfSections] = {
273 {{'.', 'i', 'd', 'a', 't', 'a', '$', '2'},
274 u32(0),
275 u32(0),
277 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section)),
278 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section) +
280 u32(0),
281 u16(NumberOfRelocations),
282 u16(0),
285 {{'.', 'i', 'd', 'a', 't', 'a', '$', '6'},
286 u32(0),
287 u32(0),
288 u32(ImportName.size() + 1),
289 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section) +
291 NumberOfRelocations * sizeof(coff_relocation)),
292 u32(0),
293 u32(0),
294 u16(0),
295 u16(0),
298 };
299 append(Buffer, SectionTable);
300
301 // .idata$2
302 const coff_import_directory_table_entry ImportDescriptor{
303 u32(0), u32(0), u32(0), u32(0), u32(0),
304 };
305 append(Buffer, ImportDescriptor);
306
307 const coff_relocation RelocationTable[NumberOfRelocations] = {
308 {u32(offsetof(coff_import_directory_table_entry, NameRVA)), u32(2),
309 u16(getImgRelRelocation(NativeMachine))},
310 {u32(offsetof(coff_import_directory_table_entry, ImportLookupTableRVA)),
311 u32(3), u16(getImgRelRelocation(NativeMachine))},
312 {u32(offsetof(coff_import_directory_table_entry, ImportAddressTableRVA)),
313 u32(4), u16(getImgRelRelocation(NativeMachine))},
314 };
315 append(Buffer, RelocationTable);
316
317 // .idata$6
318 auto S = Buffer.size();
319 Buffer.resize(S + ImportName.size() + 1);
320 memcpy(&Buffer[S], ImportName.data(), ImportName.size());
321 Buffer[S + ImportName.size()] = '\0';
322
323 // Symbol Table
324 coff_symbol16 SymbolTable[NumberOfSymbols] = {
325 {{{0, 0, 0, 0, 0, 0, 0, 0}},
326 u32(0),
327 u16(1),
328 u16(0),
330 0},
331 {{{'.', 'i', 'd', 'a', 't', 'a', '$', '2'}},
332 u32(0),
333 u16(1),
334 u16(0),
336 0},
337 {{{'.', 'i', 'd', 'a', 't', 'a', '$', '6'}},
338 u32(0),
339 u16(2),
340 u16(0),
342 0},
343 {{{'.', 'i', 'd', 'a', 't', 'a', '$', '4'}},
344 u32(0),
345 u16(0),
346 u16(0),
348 0},
349 {{{'.', 'i', 'd', 'a', 't', 'a', '$', '5'}},
350 u32(0),
351 u16(0),
352 u16(0),
354 0},
355 {{{0, 0, 0, 0, 0, 0, 0, 0}},
356 u32(0),
357 u16(0),
358 u16(0),
360 0},
361 {{{0, 0, 0, 0, 0, 0, 0, 0}},
362 u32(0),
363 u16(0),
364 u16(0),
366 0},
367 };
368 // TODO: Name.Offset.Offset here and in the all similar places below
369 // suggests a names refactoring. Maybe StringTableOffset.Value?
370 SymbolTable[0].Name.Offset.Offset =
371 sizeof(uint32_t);
372 SymbolTable[5].Name.Offset.Offset =
373 sizeof(uint32_t) + ImportDescriptorSymbolName.length() + 1;
374 SymbolTable[6].Name.Offset.Offset =
375 sizeof(uint32_t) + ImportDescriptorSymbolName.length() + 1 +
377 append(Buffer, SymbolTable);
378
379 // String Table
380 writeStringTable(Buffer,
381 {ImportDescriptorSymbolName, NullImportDescriptorSymbolName,
382 NullThunkSymbolName});
383
384 StringRef F{reinterpret_cast<const char *>(Buffer.data()), Buffer.size()};
385 return {MemoryBufferRef(F, ImportName)};
386}
387
389ObjectFactory::createNullImportDescriptor(std::vector<uint8_t> &Buffer) {
390 const uint32_t NumberOfSections = 1;
391 const uint32_t NumberOfSymbols = 1;
392
393 // COFF Header
394 coff_file_header Header{
395 u16(NativeMachine),
396 u16(NumberOfSections),
397 u32(0),
398 u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) +
399 // .idata$3
401 u32(NumberOfSymbols),
402 u16(0),
404 };
405 append(Buffer, Header);
406
407 // Section Header Table
408 const coff_section SectionTable[NumberOfSections] = {
409 {{'.', 'i', 'd', 'a', 't', 'a', '$', '3'},
410 u32(0),
411 u32(0),
413 u32(sizeof(coff_file_header) +
414 (NumberOfSections * sizeof(coff_section))),
415 u32(0),
416 u32(0),
417 u16(0),
418 u16(0),
421 };
422 append(Buffer, SectionTable);
423
424 // .idata$3
425 const coff_import_directory_table_entry ImportDescriptor{
426 u32(0), u32(0), u32(0), u32(0), u32(0),
427 };
428 append(Buffer, ImportDescriptor);
429
430 // Symbol Table
431 coff_symbol16 SymbolTable[NumberOfSymbols] = {
432 {{{0, 0, 0, 0, 0, 0, 0, 0}},
433 u32(0),
434 u16(1),
435 u16(0),
437 0},
438 };
439 SymbolTable[0].Name.Offset.Offset = sizeof(uint32_t);
440 append(Buffer, SymbolTable);
441
442 // String Table
444
445 StringRef F{reinterpret_cast<const char *>(Buffer.data()), Buffer.size()};
446 return {MemoryBufferRef(F, ImportName)};
447}
448
449NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) {
450 const uint32_t NumberOfSections = 2;
451 const uint32_t NumberOfSymbols = 1;
452 uint32_t VASize = is64Bit() ? 8 : 4;
453
454 // COFF Header
455 coff_file_header Header{
456 u16(NativeMachine),
457 u16(NumberOfSections),
458 u32(0),
459 u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) +
460 // .idata$5
461 VASize +
462 // .idata$4
463 VASize),
464 u32(NumberOfSymbols),
465 u16(0),
467 };
468 append(Buffer, Header);
469
470 // Section Header Table
471 const coff_section SectionTable[NumberOfSections] = {
472 {{'.', 'i', 'd', 'a', 't', 'a', '$', '5'},
473 u32(0),
474 u32(0),
475 u32(VASize),
476 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section)),
477 u32(0),
478 u32(0),
479 u16(0),
480 u16(0),
484 {{'.', 'i', 'd', 'a', 't', 'a', '$', '4'},
485 u32(0),
486 u32(0),
487 u32(VASize),
488 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section) +
489 VASize),
490 u32(0),
491 u32(0),
492 u16(0),
493 u16(0),
497 };
498 append(Buffer, SectionTable);
499
500 // .idata$5, ILT
501 append(Buffer, u32(0));
502 if (is64Bit())
503 append(Buffer, u32(0));
504
505 // .idata$4, IAT
506 append(Buffer, u32(0));
507 if (is64Bit())
508 append(Buffer, u32(0));
509
510 // Symbol Table
511 coff_symbol16 SymbolTable[NumberOfSymbols] = {
512 {{{0, 0, 0, 0, 0, 0, 0, 0}},
513 u32(0),
514 u16(1),
515 u16(0),
517 0},
518 };
519 SymbolTable[0].Name.Offset.Offset = sizeof(uint32_t);
520 append(Buffer, SymbolTable);
521
522 // String Table
523 writeStringTable(Buffer, {NullThunkSymbolName});
524
525 StringRef F{reinterpret_cast<const char *>(Buffer.data()), Buffer.size()};
526 return {MemoryBufferRef{F, ImportName}};
527}
528
530ObjectFactory::createShortImport(StringRef Sym, uint16_t Ordinal,
532 StringRef ExportName, MachineTypes Machine) {
533 size_t ImpSize = ImportName.size() + Sym.size() + 2; // +2 for NULs
534 if (!ExportName.empty())
535 ImpSize += ExportName.size() + 1;
536 size_t Size = sizeof(coff_import_header) + ImpSize;
537 char *Buf = Alloc.Allocate<char>(Size);
538 memset(Buf, 0, Size);
539 char *P = Buf;
540
541 // Write short import library.
542 auto *Imp = reinterpret_cast<coff_import_header *>(P);
543 P += sizeof(*Imp);
544 Imp->Sig2 = 0xFFFF;
545 Imp->Machine = Machine;
546 Imp->SizeOfData = ImpSize;
547 if (Ordinal > 0)
548 Imp->OrdinalHint = Ordinal;
549 Imp->TypeInfo = (NameType << 2) | ImportType;
550
551 // Write symbol name and DLL name.
552 memcpy(P, Sym.data(), Sym.size());
553 P += Sym.size() + 1;
554 memcpy(P, ImportName.data(), ImportName.size());
555 if (!ExportName.empty()) {
556 P += ImportName.size() + 1;
557 memcpy(P, ExportName.data(), ExportName.size());
558 }
559
560 return {MemoryBufferRef(StringRef(Buf, Size), ImportName)};
561}
562
563NewArchiveMember ObjectFactory::createWeakExternal(StringRef Sym,
564 StringRef Weak, bool Imp,
566 std::vector<uint8_t> Buffer;
567 const uint32_t NumberOfSections = 1;
568 const uint32_t NumberOfSymbols = 5;
569
570 // COFF Header
571 coff_file_header Header{
572 u16(Machine),
573 u16(NumberOfSections),
574 u32(0),
575 u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section))),
576 u32(NumberOfSymbols),
577 u16(0),
578 u16(0),
579 };
580 append(Buffer, Header);
581
582 // Section Header Table
583 const coff_section SectionTable[NumberOfSections] = {
584 {{'.', 'd', 'r', 'e', 'c', 't', 'v', 'e'},
585 u32(0),
586 u32(0),
587 u32(0),
588 u32(0),
589 u32(0),
590 u32(0),
591 u16(0),
592 u16(0),
594 append(Buffer, SectionTable);
595
596 // Symbol Table
597 coff_symbol16 SymbolTable[NumberOfSymbols] = {
598 {{{'@', 'c', 'o', 'm', 'p', '.', 'i', 'd'}},
599 u32(0),
600 u16(0xFFFF),
601 u16(0),
603 0},
604 {{{'@', 'f', 'e', 'a', 't', '.', '0', '0'}},
605 u32(0),
606 u16(0xFFFF),
607 u16(0),
609 0},
610 {{{0, 0, 0, 0, 0, 0, 0, 0}},
611 u32(0),
612 u16(0),
613 u16(0),
615 0},
616 {{{0, 0, 0, 0, 0, 0, 0, 0}},
617 u32(0),
618 u16(0),
619 u16(0),
621 1},
622 {{{2, 0, 0, 0, IMAGE_WEAK_EXTERN_SEARCH_ALIAS, 0, 0, 0}},
623 u32(0),
624 u16(0),
625 u16(0),
627 0},
628 };
629 SymbolTable[2].Name.Offset.Offset = sizeof(uint32_t);
630
631 //__imp_ String Table
632 StringRef Prefix = Imp ? "__imp_" : "";
633 SymbolTable[3].Name.Offset.Offset =
634 sizeof(uint32_t) + Sym.size() + Prefix.size() + 1;
635 append(Buffer, SymbolTable);
636 writeStringTable(Buffer, {(Prefix + Sym).str(),
637 (Prefix + Weak).str()});
638
639 // Copied here so we can still use writeStringTable
640 char *Buf = Alloc.Allocate<char>(Buffer.size());
641 memcpy(Buf, Buffer.data(), Buffer.size());
642 return {MemoryBufferRef(StringRef(Buf, Buffer.size()), ImportName)};
643}
644
647 MachineTypes Machine, bool MinGW,
648 ArrayRef<COFFShortExport> NativeExports) {
649
650 MachineTypes NativeMachine = Machine;
651 if (isArm64EC(Machine)) {
652 NativeMachine = IMAGE_FILE_MACHINE_ARM64;
654 }
655
656 std::vector<NewArchiveMember> Members;
657 ObjectFactory OF(llvm::sys::path::filename(ImportName), NativeMachine);
658
659 std::vector<uint8_t> ImportDescriptor;
660 Members.push_back(OF.createImportDescriptor(ImportDescriptor));
661
662 std::vector<uint8_t> NullImportDescriptor;
663 Members.push_back(OF.createNullImportDescriptor(NullImportDescriptor));
664
665 std::vector<uint8_t> NullThunk;
666 Members.push_back(OF.createNullThunk(NullThunk));
667
668 auto addExports = [&](ArrayRef<COFFShortExport> Exp,
669 MachineTypes M) -> Error {
670 for (const COFFShortExport &E : Exp) {
671 if (E.Private)
672 continue;
673
675 if (E.Data)
677 if (E.Constant)
679
680 StringRef SymbolName = E.SymbolName.empty() ? E.Name : E.SymbolName;
681 std::string Name;
682
683 if (E.ExtName.empty()) {
684 Name = std::string(SymbolName);
685 } else {
686 Expected<std::string> ReplacedName =
687 replace(SymbolName, E.Name, E.ExtName);
688 if (!ReplacedName)
689 return ReplacedName.takeError();
690 Name.swap(*ReplacedName);
691 }
692
693 if (!E.AliasTarget.empty() && Name != E.AliasTarget) {
694 Members.push_back(OF.createWeakExternal(E.AliasTarget, Name, false, M));
695 Members.push_back(OF.createWeakExternal(E.AliasTarget, Name, true, M));
696 continue;
697 }
698
700 std::string ExportName;
701 if (E.Noname) {
703 } else if (!E.ExportAs.empty()) {
705 ExportName = E.ExportAs;
706 } else {
707 NameType = getNameType(SymbolName, E.Name, M, MinGW);
708 }
709
710 // On ARM64EC, use EXPORTAS to import demangled name for mangled symbols.
711 if (ImportType == IMPORT_CODE && isArm64EC(M)) {
712 if (std::optional<std::string> MangledName =
714 if (!E.Noname && ExportName.empty()) {
716 ExportName.swap(Name);
717 }
718 Name = std::move(*MangledName);
719 } else if (!E.Noname && ExportName.empty()) {
721 ExportName = std::move(*getArm64ECDemangledFunctionName(Name));
722 }
723 }
724
725 Members.push_back(OF.createShortImport(Name, E.Ordinal, ImportType,
726 NameType, ExportName, M));
727 }
728 return Error::success();
729 };
730
731 if (Error e = addExports(Exports, Machine))
732 return e;
733 if (Error e = addExports(NativeExports, NativeMachine))
734 return e;
735
736 return writeArchive(Path, Members, SymtabWritingMode::NormalSymtab,
738 /*Deterministic*/ true, /*Thin*/ false,
739 /*OldArchiveBuf*/ nullptr, isArm64EC(Machine));
740}
741
742} // namespace object
743} // namespace llvm
#define offsetof(TYPE, MEMBER)
This file defines the BumpPtrAllocator interface.
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:371
std::string Name
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define F(x, y, z)
Definition: MD5.cpp:55
#define P(N)
static const char * name
Definition: SMEABIPass.cpp:49
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
const char * getBufferStart() const
StringRef getBuffer() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:696
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:567
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:257
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:420
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:293
static constexpr size_t npos
Definition: StringRef.h:52
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
MemoryBufferRef Data
Definition: Binary.h:37
const coff_import_header * getCOFFImportHeader() const
StringRef getFileFormatName() const
StringRef getExportName() const
Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
MachineTypes
Definition: COFF.h:92
@ IMAGE_FILE_MACHINE_ARM64
Definition: COFF.h:100
@ IMAGE_FILE_MACHINE_AMD64
Definition: COFF.h:97
@ IMAGE_FILE_MACHINE_ARM64EC
Definition: COFF.h:101
@ IMAGE_FILE_MACHINE_I386
Definition: COFF.h:104
@ IMAGE_FILE_MACHINE_ARM64X
Definition: COFF.h:102
@ IMAGE_FILE_MACHINE_ARMNT
Definition: COFF.h:99
@ IMAGE_SCN_LNK_REMOVE
Definition: COFF.h:307
@ IMAGE_SCN_MEM_READ
Definition: COFF.h:335
@ IMAGE_SCN_LNK_INFO
Definition: COFF.h:306
@ IMAGE_SCN_CNT_INITIALIZED_DATA
Definition: COFF.h:303
@ IMAGE_SCN_ALIGN_8BYTES
Definition: COFF.h:317
@ IMAGE_SCN_ALIGN_4BYTES
Definition: COFF.h:316
@ IMAGE_SCN_ALIGN_2BYTES
Definition: COFF.h:315
@ IMAGE_SCN_MEM_WRITE
Definition: COFF.h:336
ImportType
Definition: COFF.h:700
@ IMPORT_CONST
Definition: COFF.h:703
@ IMPORT_CODE
Definition: COFF.h:701
@ IMPORT_DATA
Definition: COFF.h:702
@ IMAGE_REL_ARM64_ADDR32NB
Definition: COFF.h:402
@ IMAGE_REL_AMD64_ADDR32NB
Definition: COFF.h:363
@ IMAGE_SYM_CLASS_SECTION
Line number, reformatted as symbol.
Definition: COFF.h:247
@ IMAGE_SYM_CLASS_EXTERNAL
External symbol.
Definition: COFF.h:223
@ IMAGE_SYM_CLASS_NULL
No symbol.
Definition: COFF.h:221
@ IMAGE_SYM_CLASS_WEAK_EXTERNAL
Duplicate tag.
Definition: COFF.h:248
@ IMAGE_SYM_CLASS_STATIC
Static.
Definition: COFF.h:224
@ IMAGE_WEAK_EXTERN_SEARCH_ALIAS
Definition: COFF.h:456
bool is64Bit(T Machine)
Definition: COFF.h:133
@ IMAGE_REL_ARM_ADDR32NB
Definition: COFF.h:382
bool isArm64EC(T Machine)
Definition: COFF.h:124
ImportNameType
Definition: COFF.h:706
@ IMPORT_ORDINAL
Import is by ordinal.
Definition: COFF.h:711
@ IMPORT_NAME_EXPORTAS
The import name is specified as a separate string in the import library object file.
Definition: COFF.h:722
@ IMPORT_NAME
The import name is identical to the public symbol name.
Definition: COFF.h:713
@ IMPORT_NAME_UNDECORATE
The import name is the public symbol name, but skipping the leading ?, @, or optionally _,...
Definition: COFF.h:719
@ IMPORT_NAME_NOPREFIX
The import name is the public symbol name, but skipping the leading ?, @, or optionally _.
Definition: COFF.h:716
@ IMAGE_REL_I386_DIR32NB
Definition: COFF.h:350
@ C_Invalid
Definition: COFF.h:138
@ IMAGE_FILE_32BIT_MACHINE
Machine is based on a 32bit word architecture.
Definition: COFF.h:159
static void append(std::vector< uint8_t > &B, const T &Data)
constexpr std::string_view NullImportDescriptorSymbolName
Error writeImportLibrary(StringRef ImportName, StringRef Path, ArrayRef< COFFShortExport > Exports, COFF::MachineTypes Machine, bool MinGW, ArrayRef< COFFShortExport > NativeExports=std::nullopt)
Writes a COFF import library containing entries described by the Exports array.
static Expected< std::string > replace(StringRef S, StringRef From, StringRef To)
static uint16_t getImgRelRelocation(MachineTypes Machine)
constexpr std::string_view NullThunkDataPrefix
constexpr std::string_view NullThunkDataSuffix
static ImportNameType getNameType(StringRef Sym, StringRef ExtName, MachineTypes Machine, bool MinGW)
static void writeStringTable(std::vector< uint8_t > &B, ArrayRef< const std::string_view > Strings)
constexpr std::string_view ImportDescriptorPrefix
void write32le(void *P, uint32_t V)
Definition: Endian.h:452
detail::packed_endian_specific_integral< uint16_t, llvm::endianness::little, unaligned > ulittle16_t
Definition: Endian.h:266
detail::packed_endian_specific_integral< uint32_t, llvm::endianness::little, unaligned > ulittle32_t
Definition: Endian.h:269
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:578
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
@ Length
Definition: DWP.cpp:456
std::optional< std::string > getArm64ECMangledFunctionName(StringRef Name)
Definition: Mangler.cpp:293
Error writeArchive(StringRef ArcName, ArrayRef< NewArchiveMember > NewMembers, SymtabWritingMode WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin, std::unique_ptr< MemoryBuffer > OldArchiveBuf=nullptr, std::optional< bool > IsEC=std::nullopt)
std::optional< std::string > getArm64ECDemangledFunctionName(StringRef Name)
Definition: Mangler.cpp:321
support::ulittle32_t Offset
Definition: COFF.h:246
Definition: COFF.h:555
StringTableOffset Offset
Definition: COFF.h:253
union llvm::object::coff_symbol::@346 Name