LLVM 20.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"
16#include "llvm/ADT/StringMap.h"
17#include "llvm/ADT/Twine.h"
18#include "llvm/Object/Archive.h"
20#include "llvm/Object/COFF.h"
22#include "llvm/Support/Endian.h"
23#include "llvm/Support/Error.h"
25#include "llvm/Support/Path.h"
26
27#include <cstdint>
28#include <string>
29#include <vector>
30
31using namespace llvm::COFF;
32using namespace llvm::object;
33using namespace llvm;
34
35namespace llvm {
36namespace object {
37
39 switch (getMachine()) {
41 return "COFF-import-file-i386";
43 return "COFF-import-file-x86-64";
45 return "COFF-import-file-ARM";
47 return "COFF-import-file-ARM64";
49 return "COFF-import-file-ARM64EC";
51 return "COFF-import-file-ARM64X";
52 default:
53 return "COFF-import-file-<unknown arch>";
54 }
55}
56
58 auto ltrim1 = [](StringRef s, StringRef chars) {
59 return !s.empty() && chars.contains(s[0]) ? s.substr(1) : s;
60 };
61
62 switch (Type) {
64 name = ltrim1(name, "?@_");
65 break;
67 name = ltrim1(name, "?@_");
68 name = name.substr(0, name.find('@'));
69 break;
70 default:
71 break;
72 }
73 return name;
74}
75
78 StringRef name = Data.getBuffer().substr(sizeof(*hdr)).split('\0').first;
79
80 switch (hdr->getNameType()) {
81 case IMPORT_ORDINAL:
82 name = "";
83 break;
86 name = applyNameType(static_cast<ImportNameType>(hdr->getNameType()), name);
87 break;
89 // Skip DLL name
90 name = Data.getBuffer().substr(sizeof(*hdr) + name.size() + 1);
91 name = name.split('\0').second.split('\0').first;
92 break;
93 }
94 default:
95 break;
96 }
97
98 return name;
99}
100
102 switch (Symb.p) {
103 case ImpSymbol:
104 OS << "__imp_";
105 break;
106 case ECAuxSymbol:
107 OS << "__imp_aux_";
108 break;
109 }
110 const char *Name = Data.getBufferStart() + sizeof(coff_import_header);
111 if (Symb.p != ECThunkSymbol && COFF::isArm64EC(getMachine())) {
112 if (std::optional<std::string> DemangledName =
114 OS << StringRef(*DemangledName);
115 return Error::success();
116 }
117 }
118 OS << StringRef(Name);
119 return Error::success();
120}
121
123 switch (Machine) {
124 default:
125 llvm_unreachable("unsupported machine");
136 }
137}
138
139template <class T> static void append(std::vector<uint8_t> &B, const T &Data) {
140 size_t S = B.size();
141 B.resize(S + sizeof(T));
142 memcpy(&B[S], &Data, sizeof(T));
143}
144
145static void writeStringTable(std::vector<uint8_t> &B,
147 // The COFF string table consists of a 4-byte value which is the size of the
148 // table, including the length field itself. This value is followed by the
149 // string content itself, which is an array of null-terminated C-style
150 // strings. The termination is important as they are referenced to by offset
151 // by the symbol entity in the file format.
152
153 size_t Pos = B.size();
154 size_t Offset = B.size();
155
156 // Skip over the length field, we will fill it in later as we will have
157 // computed the length while emitting the string content itself.
158 Pos += sizeof(uint32_t);
159
160 for (const auto &S : Strings) {
161 B.resize(Pos + S.length() + 1);
162 std::copy(S.begin(), S.end(), std::next(B.begin(), Pos));
163 B[Pos + S.length()] = 0;
164 Pos += S.length() + 1;
165 }
166
167 // Backfill the length of the table now that it has been computed.
170}
171
173 MachineTypes Machine, bool MinGW) {
174 // A decorated stdcall function in MSVC is exported with the
175 // type IMPORT_NAME, and the exported function name includes the
176 // the leading underscore. In MinGW on the other hand, a decorated
177 // stdcall function still omits the underscore (IMPORT_NAME_NOPREFIX).
178 // See the comment in isDecorated in COFFModuleDefinition.cpp for more
179 // details.
180 if (ExtName.starts_with("_") && ExtName.contains('@') && !MinGW)
181 return IMPORT_NAME;
182 if (Sym != ExtName)
184 if (Machine == IMAGE_FILE_MACHINE_I386 && Sym.starts_with("_"))
186 return IMPORT_NAME;
187}
188
190 StringRef To) {
191 size_t Pos = S.find(From);
192
193 // From and To may be mangled, but substrings in S may not.
194 if (Pos == StringRef::npos && From.starts_with("_") && To.starts_with("_")) {
195 From = From.substr(1);
196 To = To.substr(1);
197 Pos = S.find(From);
198 }
199
200 if (Pos == StringRef::npos) {
201 return make_error<StringError>(
202 StringRef(Twine(S + ": replacing '" + From +
203 "' with '" + To + "' failed").str()), object_error::parse_failed);
204 }
205
206 return (Twine(S.substr(0, Pos)) + To + S.substr(Pos + From.size())).str();
207}
208
209namespace {
210// This class constructs various small object files necessary to support linking
211// symbols imported from a DLL. The contents are pretty strictly defined and
212// nearly entirely static. The details of the structures files are defined in
213// WINNT.h and the PE/COFF specification.
214class ObjectFactory {
215 using u16 = support::ulittle16_t;
216 using u32 = support::ulittle32_t;
217 MachineTypes NativeMachine;
218 BumpPtrAllocator Alloc;
219 StringRef ImportName;
220 StringRef Library;
221 std::string ImportDescriptorSymbolName;
222 std::string NullThunkSymbolName;
223
224public:
225 ObjectFactory(StringRef S, MachineTypes M)
226 : NativeMachine(M), ImportName(S), Library(llvm::sys::path::stem(S)),
227 ImportDescriptorSymbolName((ImportDescriptorPrefix + Library).str()),
228 NullThunkSymbolName(
229 (NullThunkDataPrefix + Library + NullThunkDataSuffix).str()) {}
230
231 // Creates an Import Descriptor. This is a small object file which contains a
232 // reference to the terminators and contains the library name (entry) for the
233 // import name table. It will force the linker to construct the necessary
234 // structure to import symbols from the DLL.
235 NewArchiveMember createImportDescriptor(std::vector<uint8_t> &Buffer);
236
237 // Creates a NULL import descriptor. This is a small object file whcih
238 // contains a NULL import descriptor. It is used to terminate the imports
239 // from a specific DLL.
240 NewArchiveMember createNullImportDescriptor(std::vector<uint8_t> &Buffer);
241
242 // Create a NULL Thunk Entry. This is a small object file which contains a
243 // NULL Import Address Table entry and a NULL Import Lookup Table Entry. It
244 // is used to terminate the IAT and ILT.
245 NewArchiveMember createNullThunk(std::vector<uint8_t> &Buffer);
246
247 // Create a short import file which is described in PE/COFF spec 7. Import
248 // Library Format.
249 NewArchiveMember createShortImport(StringRef Sym, uint16_t Ordinal,
251 StringRef ExportName,
253
254 // Create a weak external file which is described in PE/COFF Aux Format 3.
255 NewArchiveMember createWeakExternal(StringRef Sym, StringRef Weak, bool Imp,
257
258 bool is64Bit() const { return COFF::is64Bit(NativeMachine); }
259};
260} // namespace
261
263ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) {
264 const uint32_t NumberOfSections = 2;
265 const uint32_t NumberOfSymbols = 7;
266 const uint32_t NumberOfRelocations = 3;
267
268 // COFF Header
269 coff_file_header Header{
270 u16(NativeMachine),
271 u16(NumberOfSections),
272 u32(0),
273 u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) +
274 // .idata$2
276 NumberOfRelocations * sizeof(coff_relocation) +
277 // .idata$4
278 (ImportName.size() + 1)),
279 u32(NumberOfSymbols),
280 u16(0),
282 };
283 append(Buffer, Header);
284
285 // Section Header Table
286 const coff_section SectionTable[NumberOfSections] = {
287 {{'.', 'i', 'd', 'a', 't', 'a', '$', '2'},
288 u32(0),
289 u32(0),
291 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section)),
292 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section) +
294 u32(0),
295 u16(NumberOfRelocations),
296 u16(0),
299 {{'.', 'i', 'd', 'a', 't', 'a', '$', '6'},
300 u32(0),
301 u32(0),
302 u32(ImportName.size() + 1),
303 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section) +
305 NumberOfRelocations * sizeof(coff_relocation)),
306 u32(0),
307 u32(0),
308 u16(0),
309 u16(0),
312 };
313 append(Buffer, SectionTable);
314
315 // .idata$2
316 const coff_import_directory_table_entry ImportDescriptor{
317 u32(0), u32(0), u32(0), u32(0), u32(0),
318 };
319 append(Buffer, ImportDescriptor);
320
321 const coff_relocation RelocationTable[NumberOfRelocations] = {
322 {u32(offsetof(coff_import_directory_table_entry, NameRVA)), u32(2),
323 u16(getImgRelRelocation(NativeMachine))},
324 {u32(offsetof(coff_import_directory_table_entry, ImportLookupTableRVA)),
325 u32(3), u16(getImgRelRelocation(NativeMachine))},
326 {u32(offsetof(coff_import_directory_table_entry, ImportAddressTableRVA)),
327 u32(4), u16(getImgRelRelocation(NativeMachine))},
328 };
329 append(Buffer, RelocationTable);
330
331 // .idata$6
332 auto S = Buffer.size();
333 Buffer.resize(S + ImportName.size() + 1);
334 memcpy(&Buffer[S], ImportName.data(), ImportName.size());
335 Buffer[S + ImportName.size()] = '\0';
336
337 // Symbol Table
338 coff_symbol16 SymbolTable[NumberOfSymbols] = {
339 {{{0, 0, 0, 0, 0, 0, 0, 0}},
340 u32(0),
341 u16(1),
342 u16(0),
344 0},
345 {{{'.', 'i', 'd', 'a', 't', 'a', '$', '2'}},
346 u32(0),
347 u16(1),
348 u16(0),
350 0},
351 {{{'.', 'i', 'd', 'a', 't', 'a', '$', '6'}},
352 u32(0),
353 u16(2),
354 u16(0),
356 0},
357 {{{'.', 'i', 'd', 'a', 't', 'a', '$', '4'}},
358 u32(0),
359 u16(0),
360 u16(0),
362 0},
363 {{{'.', 'i', 'd', 'a', 't', 'a', '$', '5'}},
364 u32(0),
365 u16(0),
366 u16(0),
368 0},
369 {{{0, 0, 0, 0, 0, 0, 0, 0}},
370 u32(0),
371 u16(0),
372 u16(0),
374 0},
375 {{{0, 0, 0, 0, 0, 0, 0, 0}},
376 u32(0),
377 u16(0),
378 u16(0),
380 0},
381 };
382 // TODO: Name.Offset.Offset here and in the all similar places below
383 // suggests a names refactoring. Maybe StringTableOffset.Value?
384 SymbolTable[0].Name.Offset.Offset =
385 sizeof(uint32_t);
386 SymbolTable[5].Name.Offset.Offset =
387 sizeof(uint32_t) + ImportDescriptorSymbolName.length() + 1;
388 SymbolTable[6].Name.Offset.Offset =
389 sizeof(uint32_t) + ImportDescriptorSymbolName.length() + 1 +
391 append(Buffer, SymbolTable);
392
393 // String Table
394 writeStringTable(Buffer,
395 {ImportDescriptorSymbolName, NullImportDescriptorSymbolName,
396 NullThunkSymbolName});
397
398 StringRef F{reinterpret_cast<const char *>(Buffer.data()), Buffer.size()};
399 return {MemoryBufferRef(F, ImportName)};
400}
401
403ObjectFactory::createNullImportDescriptor(std::vector<uint8_t> &Buffer) {
404 const uint32_t NumberOfSections = 1;
405 const uint32_t NumberOfSymbols = 1;
406
407 // COFF Header
408 coff_file_header Header{
409 u16(NativeMachine),
410 u16(NumberOfSections),
411 u32(0),
412 u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) +
413 // .idata$3
415 u32(NumberOfSymbols),
416 u16(0),
418 };
419 append(Buffer, Header);
420
421 // Section Header Table
422 const coff_section SectionTable[NumberOfSections] = {
423 {{'.', 'i', 'd', 'a', 't', 'a', '$', '3'},
424 u32(0),
425 u32(0),
427 u32(sizeof(coff_file_header) +
428 (NumberOfSections * sizeof(coff_section))),
429 u32(0),
430 u32(0),
431 u16(0),
432 u16(0),
435 };
436 append(Buffer, SectionTable);
437
438 // .idata$3
439 const coff_import_directory_table_entry ImportDescriptor{
440 u32(0), u32(0), u32(0), u32(0), u32(0),
441 };
442 append(Buffer, ImportDescriptor);
443
444 // Symbol Table
445 coff_symbol16 SymbolTable[NumberOfSymbols] = {
446 {{{0, 0, 0, 0, 0, 0, 0, 0}},
447 u32(0),
448 u16(1),
449 u16(0),
451 0},
452 };
453 SymbolTable[0].Name.Offset.Offset = sizeof(uint32_t);
454 append(Buffer, SymbolTable);
455
456 // String Table
458
459 StringRef F{reinterpret_cast<const char *>(Buffer.data()), Buffer.size()};
460 return {MemoryBufferRef(F, ImportName)};
461}
462
463NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) {
464 const uint32_t NumberOfSections = 2;
465 const uint32_t NumberOfSymbols = 1;
466 uint32_t VASize = is64Bit() ? 8 : 4;
467
468 // COFF Header
469 coff_file_header Header{
470 u16(NativeMachine),
471 u16(NumberOfSections),
472 u32(0),
473 u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) +
474 // .idata$5
475 VASize +
476 // .idata$4
477 VASize),
478 u32(NumberOfSymbols),
479 u16(0),
481 };
482 append(Buffer, Header);
483
484 // Section Header Table
485 const coff_section SectionTable[NumberOfSections] = {
486 {{'.', 'i', 'd', 'a', 't', 'a', '$', '5'},
487 u32(0),
488 u32(0),
489 u32(VASize),
490 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section)),
491 u32(0),
492 u32(0),
493 u16(0),
494 u16(0),
498 {{'.', 'i', 'd', 'a', 't', 'a', '$', '4'},
499 u32(0),
500 u32(0),
501 u32(VASize),
502 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section) +
503 VASize),
504 u32(0),
505 u32(0),
506 u16(0),
507 u16(0),
511 };
512 append(Buffer, SectionTable);
513
514 // .idata$5, ILT
515 append(Buffer, u32(0));
516 if (is64Bit())
517 append(Buffer, u32(0));
518
519 // .idata$4, IAT
520 append(Buffer, u32(0));
521 if (is64Bit())
522 append(Buffer, u32(0));
523
524 // Symbol Table
525 coff_symbol16 SymbolTable[NumberOfSymbols] = {
526 {{{0, 0, 0, 0, 0, 0, 0, 0}},
527 u32(0),
528 u16(1),
529 u16(0),
531 0},
532 };
533 SymbolTable[0].Name.Offset.Offset = sizeof(uint32_t);
534 append(Buffer, SymbolTable);
535
536 // String Table
537 writeStringTable(Buffer, {NullThunkSymbolName});
538
539 StringRef F{reinterpret_cast<const char *>(Buffer.data()), Buffer.size()};
540 return {MemoryBufferRef{F, ImportName}};
541}
542
544ObjectFactory::createShortImport(StringRef Sym, uint16_t Ordinal,
546 StringRef ExportName, MachineTypes Machine) {
547 size_t ImpSize = ImportName.size() + Sym.size() + 2; // +2 for NULs
548 if (!ExportName.empty())
549 ImpSize += ExportName.size() + 1;
550 size_t Size = sizeof(coff_import_header) + ImpSize;
551 char *Buf = Alloc.Allocate<char>(Size);
552 memset(Buf, 0, Size);
553 char *P = Buf;
554
555 // Write short import library.
556 auto *Imp = reinterpret_cast<coff_import_header *>(P);
557 P += sizeof(*Imp);
558 Imp->Sig2 = 0xFFFF;
559 Imp->Machine = Machine;
560 Imp->SizeOfData = ImpSize;
561 if (Ordinal > 0)
562 Imp->OrdinalHint = Ordinal;
563 Imp->TypeInfo = (NameType << 2) | ImportType;
564
565 // Write symbol name and DLL name.
566 memcpy(P, Sym.data(), Sym.size());
567 P += Sym.size() + 1;
568 memcpy(P, ImportName.data(), ImportName.size());
569 if (!ExportName.empty()) {
570 P += ImportName.size() + 1;
571 memcpy(P, ExportName.data(), ExportName.size());
572 }
573
574 return {MemoryBufferRef(StringRef(Buf, Size), ImportName)};
575}
576
577NewArchiveMember ObjectFactory::createWeakExternal(StringRef Sym,
578 StringRef Weak, bool Imp,
580 std::vector<uint8_t> Buffer;
581 const uint32_t NumberOfSections = 1;
582 const uint32_t NumberOfSymbols = 5;
583
584 // COFF Header
585 coff_file_header Header{
586 u16(Machine),
587 u16(NumberOfSections),
588 u32(0),
589 u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section))),
590 u32(NumberOfSymbols),
591 u16(0),
592 u16(0),
593 };
594 append(Buffer, Header);
595
596 // Section Header Table
597 const coff_section SectionTable[NumberOfSections] = {
598 {{'.', 'd', 'r', 'e', 'c', 't', 'v', 'e'},
599 u32(0),
600 u32(0),
601 u32(0),
602 u32(0),
603 u32(0),
604 u32(0),
605 u16(0),
606 u16(0),
608 append(Buffer, SectionTable);
609
610 // Symbol Table
611 coff_symbol16 SymbolTable[NumberOfSymbols] = {
612 {{{'@', 'c', 'o', 'm', 'p', '.', 'i', 'd'}},
613 u32(0),
614 u16(0xFFFF),
615 u16(0),
617 0},
618 {{{'@', 'f', 'e', 'a', 't', '.', '0', '0'}},
619 u32(0),
620 u16(0xFFFF),
621 u16(0),
623 0},
624 {{{0, 0, 0, 0, 0, 0, 0, 0}},
625 u32(0),
626 u16(0),
627 u16(0),
629 0},
630 {{{0, 0, 0, 0, 0, 0, 0, 0}},
631 u32(0),
632 u16(0),
633 u16(0),
635 1},
636 {{{2, 0, 0, 0, IMAGE_WEAK_EXTERN_SEARCH_ALIAS, 0, 0, 0}},
637 u32(0),
638 u16(0),
639 u16(0),
641 0},
642 };
643 SymbolTable[2].Name.Offset.Offset = sizeof(uint32_t);
644
645 //__imp_ String Table
646 StringRef Prefix = Imp ? "__imp_" : "";
647 SymbolTable[3].Name.Offset.Offset =
648 sizeof(uint32_t) + Sym.size() + Prefix.size() + 1;
649 append(Buffer, SymbolTable);
650 writeStringTable(Buffer, {(Prefix + Sym).str(),
651 (Prefix + Weak).str()});
652
653 // Copied here so we can still use writeStringTable
654 char *Buf = Alloc.Allocate<char>(Buffer.size());
655 memcpy(Buf, Buffer.data(), Buffer.size());
656 return {MemoryBufferRef(StringRef(Buf, Buffer.size()), ImportName)};
657}
658
661 MachineTypes Machine, bool MinGW,
662 ArrayRef<COFFShortExport> NativeExports) {
663
664 MachineTypes NativeMachine = Machine;
665 if (isArm64EC(Machine)) {
666 NativeMachine = IMAGE_FILE_MACHINE_ARM64;
668 }
669
670 std::vector<NewArchiveMember> Members;
671 ObjectFactory OF(llvm::sys::path::filename(ImportName), NativeMachine);
672
673 std::vector<uint8_t> ImportDescriptor;
674 Members.push_back(OF.createImportDescriptor(ImportDescriptor));
675
676 std::vector<uint8_t> NullImportDescriptor;
677 Members.push_back(OF.createNullImportDescriptor(NullImportDescriptor));
678
679 std::vector<uint8_t> NullThunk;
680 Members.push_back(OF.createNullThunk(NullThunk));
681
682 auto addExports = [&](ArrayRef<COFFShortExport> Exp,
683 MachineTypes M) -> Error {
684 StringMap<std::string> RegularImports;
685 struct Deferred {
686 std::string Name;
687 ImportType ImpType;
688 const COFFShortExport *Export;
689 };
691 for (const COFFShortExport &E : Exp) {
692 if (E.Private)
693 continue;
694
696 if (E.Data)
698 if (E.Constant)
700
701 StringRef SymbolName = E.SymbolName.empty() ? E.Name : E.SymbolName;
702 std::string Name;
703
704 if (E.ExtName.empty()) {
705 Name = std::string(SymbolName);
706 } else {
707 Expected<std::string> ReplacedName =
708 replace(SymbolName, E.Name, E.ExtName);
709 if (!ReplacedName)
710 return ReplacedName.takeError();
711 Name.swap(*ReplacedName);
712 }
713
715 std::string ExportName;
716 if (E.Noname) {
718 } else if (!E.ExportAs.empty()) {
720 ExportName = E.ExportAs;
721 } else if (!E.ImportName.empty()) {
722 // If we need to import from a specific ImportName, we may need to use
723 // a weak alias (which needs another import to point at). But if we can
724 // express ImportName based on the symbol name and a specific NameType,
725 // prefer that over an alias.
727 applyNameType(IMPORT_NAME_UNDECORATE, Name) == E.ImportName)
729 else if (Machine == IMAGE_FILE_MACHINE_I386 &&
730 applyNameType(IMPORT_NAME_NOPREFIX, Name) == E.ImportName)
732 else if (isArm64EC(M)) {
734 ExportName = E.ImportName;
735 } else if (Name == E.ImportName)
737 else {
738 Deferred D;
739 D.Name = Name;
740 D.ImpType = ImportType;
741 D.Export = &E;
742 Renames.push_back(D);
743 continue;
744 }
745 } else {
746 NameType = getNameType(SymbolName, E.Name, M, MinGW);
747 }
748
749 // On ARM64EC, use EXPORTAS to import demangled name for mangled symbols.
750 if (ImportType == IMPORT_CODE && isArm64EC(M)) {
751 if (std::optional<std::string> MangledName =
753 if (!E.Noname && ExportName.empty()) {
755 ExportName.swap(Name);
756 }
757 Name = std::move(*MangledName);
758 } else if (!E.Noname && ExportName.empty()) {
760 ExportName = std::move(*getArm64ECDemangledFunctionName(Name));
761 }
762 }
763
764 RegularImports[applyNameType(NameType, Name)] = Name;
765 Members.push_back(OF.createShortImport(Name, E.Ordinal, ImportType,
766 NameType, ExportName, M));
767 }
768 for (const auto &D : Renames) {
769 auto It = RegularImports.find(D.Export->ImportName);
770 if (It != RegularImports.end()) {
771 // We have a regular import entry for a symbol with the name we
772 // want to reference; produce an alias pointing at that.
773 StringRef Symbol = It->second;
774 if (D.ImpType == IMPORT_CODE)
775 Members.push_back(OF.createWeakExternal(Symbol, D.Name, false, M));
776 Members.push_back(OF.createWeakExternal(Symbol, D.Name, true, M));
777 } else {
778 Members.push_back(OF.createShortImport(D.Name, D.Export->Ordinal,
779 D.ImpType, IMPORT_NAME_EXPORTAS,
780 D.Export->ImportName, M));
781 }
782 }
783 return Error::success();
784 };
785
786 if (Error e = addExports(Exports, Machine))
787 return e;
788 if (Error e = addExports(NativeExports, NativeMachine))
789 return e;
790
791 return writeArchive(Path, Members, SymtabWritingMode::NormalSymtab,
793 /*Deterministic*/ true, /*Thin*/ false,
794 /*OldArchiveBuf*/ nullptr, isArm64EC(Machine));
795}
796
797} // namespace object
798} // namespace llvm
This file defines the StringMap class.
#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< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
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:50
raw_pwrite_stream & OS
This file defines the SmallVector class.
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:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
const char * getBufferStart() const
StringRef getBuffer() const
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
iterator end()
Definition: StringMap.h:220
iterator find(StringRef Key)
Definition: StringMap.h:233
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:685
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:556
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:250
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:409
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:282
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 StringRef applyNameType(ImportNameType Type, StringRef name)
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:468
detail::packed_endian_specific_integral< uint16_t, llvm::endianness::little, unaligned > ulittle16_t
Definition: Endian.h:282
detail::packed_endian_specific_integral< uint32_t, llvm::endianness::little, unaligned > ulittle32_t
Definition: Endian.h:285
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:480
@ Length
Definition: DWP.cpp:480
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, function_ref< void(Error)> Warn=warnToStderr)
@ Export
Export information to summary.
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::@353 Name