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");
138 }
139}
140
141template <class T> static void append(std::vector<uint8_t> &B, const T &Data) {
142 size_t S = B.size();
143 B.resize(S + sizeof(T));
144 memcpy(&B[S], &Data, sizeof(T));
145}
146
147static void writeStringTable(std::vector<uint8_t> &B,
149 // The COFF string table consists of a 4-byte value which is the size of the
150 // table, including the length field itself. This value is followed by the
151 // string content itself, which is an array of null-terminated C-style
152 // strings. The termination is important as they are referenced to by offset
153 // by the symbol entity in the file format.
154
155 size_t Pos = B.size();
156 size_t Offset = B.size();
157
158 // Skip over the length field, we will fill it in later as we will have
159 // computed the length while emitting the string content itself.
160 Pos += sizeof(uint32_t);
161
162 for (const auto &S : Strings) {
163 B.resize(Pos + S.length() + 1);
164 std::copy(S.begin(), S.end(), std::next(B.begin(), Pos));
165 B[Pos + S.length()] = 0;
166 Pos += S.length() + 1;
167 }
168
169 // Backfill the length of the table now that it has been computed.
172}
173
175 MachineTypes Machine, bool MinGW) {
176 // A decorated stdcall function in MSVC is exported with the
177 // type IMPORT_NAME, and the exported function name includes the
178 // the leading underscore. In MinGW on the other hand, a decorated
179 // stdcall function still omits the underscore (IMPORT_NAME_NOPREFIX).
180 // See the comment in isDecorated in COFFModuleDefinition.cpp for more
181 // details.
182 if (ExtName.starts_with("_") && ExtName.contains('@') && !MinGW)
183 return IMPORT_NAME;
184 if (Sym != ExtName)
186 if (Machine == IMAGE_FILE_MACHINE_I386 && Sym.starts_with("_"))
188 return IMPORT_NAME;
189}
190
192 StringRef To) {
193 size_t Pos = S.find(From);
194
195 // From and To may be mangled, but substrings in S may not.
196 if (Pos == StringRef::npos && From.starts_with("_") && To.starts_with("_")) {
197 From = From.substr(1);
198 To = To.substr(1);
199 Pos = S.find(From);
200 }
201
202 if (Pos == StringRef::npos) {
203 return make_error<StringError>(
204 StringRef(Twine(S + ": replacing '" + From +
205 "' with '" + To + "' failed").str()), object_error::parse_failed);
206 }
207
208 return (Twine(S.substr(0, Pos)) + To + S.substr(Pos + From.size())).str();
209}
210
211namespace {
212// This class constructs various small object files necessary to support linking
213// symbols imported from a DLL. The contents are pretty strictly defined and
214// nearly entirely static. The details of the structures files are defined in
215// WINNT.h and the PE/COFF specification.
216class ObjectFactory {
217 using u16 = support::ulittle16_t;
218 using u32 = support::ulittle32_t;
219 MachineTypes NativeMachine;
220 BumpPtrAllocator Alloc;
221 StringRef ImportName;
222 StringRef Library;
223 std::string ImportDescriptorSymbolName;
224 std::string NullThunkSymbolName;
225
226public:
227 ObjectFactory(StringRef S, MachineTypes M)
228 : NativeMachine(M), ImportName(S), Library(llvm::sys::path::stem(S)),
229 ImportDescriptorSymbolName((ImportDescriptorPrefix + Library).str()),
230 NullThunkSymbolName(
231 (NullThunkDataPrefix + Library + NullThunkDataSuffix).str()) {}
232
233 // Creates an Import Descriptor. This is a small object file which contains a
234 // reference to the terminators and contains the library name (entry) for the
235 // import name table. It will force the linker to construct the necessary
236 // structure to import symbols from the DLL.
237 NewArchiveMember createImportDescriptor(std::vector<uint8_t> &Buffer);
238
239 // Creates a NULL import descriptor. This is a small object file whcih
240 // contains a NULL import descriptor. It is used to terminate the imports
241 // from a specific DLL.
242 NewArchiveMember createNullImportDescriptor(std::vector<uint8_t> &Buffer);
243
244 // Create a NULL Thunk Entry. This is a small object file which contains a
245 // NULL Import Address Table entry and a NULL Import Lookup Table Entry. It
246 // is used to terminate the IAT and ILT.
247 NewArchiveMember createNullThunk(std::vector<uint8_t> &Buffer);
248
249 // Create a short import file which is described in PE/COFF spec 7. Import
250 // Library Format.
251 NewArchiveMember createShortImport(StringRef Sym, uint16_t Ordinal,
253 StringRef ExportName,
255
256 // Create a weak external file which is described in PE/COFF Aux Format 3.
257 NewArchiveMember createWeakExternal(StringRef Sym, StringRef Weak, bool Imp,
259
260 bool is64Bit() const { return COFF::is64Bit(NativeMachine); }
261};
262} // namespace
263
265ObjectFactory::createImportDescriptor(std::vector<uint8_t> &Buffer) {
266 const uint32_t NumberOfSections = 2;
267 const uint32_t NumberOfSymbols = 7;
268 const uint32_t NumberOfRelocations = 3;
269
270 // COFF Header
271 coff_file_header Header{
272 u16(NativeMachine),
273 u16(NumberOfSections),
274 u32(0),
275 u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) +
276 // .idata$2
278 NumberOfRelocations * sizeof(coff_relocation) +
279 // .idata$4
280 (ImportName.size() + 1)),
281 u32(NumberOfSymbols),
282 u16(0),
284 };
285 append(Buffer, Header);
286
287 // Section Header Table
288 const coff_section SectionTable[NumberOfSections] = {
289 {{'.', 'i', 'd', 'a', 't', 'a', '$', '2'},
290 u32(0),
291 u32(0),
293 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section)),
294 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section) +
296 u32(0),
297 u16(NumberOfRelocations),
298 u16(0),
301 {{'.', 'i', 'd', 'a', 't', 'a', '$', '6'},
302 u32(0),
303 u32(0),
304 u32(ImportName.size() + 1),
305 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section) +
307 NumberOfRelocations * sizeof(coff_relocation)),
308 u32(0),
309 u32(0),
310 u16(0),
311 u16(0),
314 };
315 append(Buffer, SectionTable);
316
317 // .idata$2
318 const coff_import_directory_table_entry ImportDescriptor{
319 u32(0), u32(0), u32(0), u32(0), u32(0),
320 };
321 append(Buffer, ImportDescriptor);
322
323 const coff_relocation RelocationTable[NumberOfRelocations] = {
324 {u32(offsetof(coff_import_directory_table_entry, NameRVA)), u32(2),
325 u16(getImgRelRelocation(NativeMachine))},
326 {u32(offsetof(coff_import_directory_table_entry, ImportLookupTableRVA)),
327 u32(3), u16(getImgRelRelocation(NativeMachine))},
328 {u32(offsetof(coff_import_directory_table_entry, ImportAddressTableRVA)),
329 u32(4), u16(getImgRelRelocation(NativeMachine))},
330 };
331 append(Buffer, RelocationTable);
332
333 // .idata$6
334 auto S = Buffer.size();
335 Buffer.resize(S + ImportName.size() + 1);
336 memcpy(&Buffer[S], ImportName.data(), ImportName.size());
337 Buffer[S + ImportName.size()] = '\0';
338
339 // Symbol Table
340 coff_symbol16 SymbolTable[NumberOfSymbols] = {
341 {{{0, 0, 0, 0, 0, 0, 0, 0}},
342 u32(0),
343 u16(1),
344 u16(0),
346 0},
347 {{{'.', 'i', 'd', 'a', 't', 'a', '$', '2'}},
348 u32(0),
349 u16(1),
350 u16(0),
352 0},
353 {{{'.', 'i', 'd', 'a', 't', 'a', '$', '6'}},
354 u32(0),
355 u16(2),
356 u16(0),
358 0},
359 {{{'.', 'i', 'd', 'a', 't', 'a', '$', '4'}},
360 u32(0),
361 u16(0),
362 u16(0),
364 0},
365 {{{'.', 'i', 'd', 'a', 't', 'a', '$', '5'}},
366 u32(0),
367 u16(0),
368 u16(0),
370 0},
371 {{{0, 0, 0, 0, 0, 0, 0, 0}},
372 u32(0),
373 u16(0),
374 u16(0),
376 0},
377 {{{0, 0, 0, 0, 0, 0, 0, 0}},
378 u32(0),
379 u16(0),
380 u16(0),
382 0},
383 };
384 // TODO: Name.Offset.Offset here and in the all similar places below
385 // suggests a names refactoring. Maybe StringTableOffset.Value?
386 SymbolTable[0].Name.Offset.Offset =
387 sizeof(uint32_t);
388 SymbolTable[5].Name.Offset.Offset =
389 sizeof(uint32_t) + ImportDescriptorSymbolName.length() + 1;
390 SymbolTable[6].Name.Offset.Offset =
391 sizeof(uint32_t) + ImportDescriptorSymbolName.length() + 1 +
393 append(Buffer, SymbolTable);
394
395 // String Table
396 writeStringTable(Buffer,
397 {ImportDescriptorSymbolName, NullImportDescriptorSymbolName,
398 NullThunkSymbolName});
399
400 StringRef F{reinterpret_cast<const char *>(Buffer.data()), Buffer.size()};
401 return {MemoryBufferRef(F, ImportName)};
402}
403
405ObjectFactory::createNullImportDescriptor(std::vector<uint8_t> &Buffer) {
406 const uint32_t NumberOfSections = 1;
407 const uint32_t NumberOfSymbols = 1;
408
409 // COFF Header
410 coff_file_header Header{
411 u16(NativeMachine),
412 u16(NumberOfSections),
413 u32(0),
414 u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) +
415 // .idata$3
417 u32(NumberOfSymbols),
418 u16(0),
420 };
421 append(Buffer, Header);
422
423 // Section Header Table
424 const coff_section SectionTable[NumberOfSections] = {
425 {{'.', 'i', 'd', 'a', 't', 'a', '$', '3'},
426 u32(0),
427 u32(0),
429 u32(sizeof(coff_file_header) +
430 (NumberOfSections * sizeof(coff_section))),
431 u32(0),
432 u32(0),
433 u16(0),
434 u16(0),
437 };
438 append(Buffer, SectionTable);
439
440 // .idata$3
441 const coff_import_directory_table_entry ImportDescriptor{
442 u32(0), u32(0), u32(0), u32(0), u32(0),
443 };
444 append(Buffer, ImportDescriptor);
445
446 // Symbol Table
447 coff_symbol16 SymbolTable[NumberOfSymbols] = {
448 {{{0, 0, 0, 0, 0, 0, 0, 0}},
449 u32(0),
450 u16(1),
451 u16(0),
453 0},
454 };
455 SymbolTable[0].Name.Offset.Offset = sizeof(uint32_t);
456 append(Buffer, SymbolTable);
457
458 // String Table
460
461 StringRef F{reinterpret_cast<const char *>(Buffer.data()), Buffer.size()};
462 return {MemoryBufferRef(F, ImportName)};
463}
464
465NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) {
466 const uint32_t NumberOfSections = 2;
467 const uint32_t NumberOfSymbols = 1;
468 uint32_t VASize = is64Bit() ? 8 : 4;
469
470 // COFF Header
471 coff_file_header Header{
472 u16(NativeMachine),
473 u16(NumberOfSections),
474 u32(0),
475 u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section)) +
476 // .idata$5
477 VASize +
478 // .idata$4
479 VASize),
480 u32(NumberOfSymbols),
481 u16(0),
483 };
484 append(Buffer, Header);
485
486 // Section Header Table
487 const coff_section SectionTable[NumberOfSections] = {
488 {{'.', 'i', 'd', 'a', 't', 'a', '$', '5'},
489 u32(0),
490 u32(0),
491 u32(VASize),
492 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section)),
493 u32(0),
494 u32(0),
495 u16(0),
496 u16(0),
500 {{'.', 'i', 'd', 'a', 't', 'a', '$', '4'},
501 u32(0),
502 u32(0),
503 u32(VASize),
504 u32(sizeof(coff_file_header) + NumberOfSections * sizeof(coff_section) +
505 VASize),
506 u32(0),
507 u32(0),
508 u16(0),
509 u16(0),
513 };
514 append(Buffer, SectionTable);
515
516 // .idata$5, ILT
517 append(Buffer, u32(0));
518 if (is64Bit())
519 append(Buffer, u32(0));
520
521 // .idata$4, IAT
522 append(Buffer, u32(0));
523 if (is64Bit())
524 append(Buffer, u32(0));
525
526 // Symbol Table
527 coff_symbol16 SymbolTable[NumberOfSymbols] = {
528 {{{0, 0, 0, 0, 0, 0, 0, 0}},
529 u32(0),
530 u16(1),
531 u16(0),
533 0},
534 };
535 SymbolTable[0].Name.Offset.Offset = sizeof(uint32_t);
536 append(Buffer, SymbolTable);
537
538 // String Table
539 writeStringTable(Buffer, {NullThunkSymbolName});
540
541 StringRef F{reinterpret_cast<const char *>(Buffer.data()), Buffer.size()};
542 return {MemoryBufferRef{F, ImportName}};
543}
544
546ObjectFactory::createShortImport(StringRef Sym, uint16_t Ordinal,
548 StringRef ExportName, MachineTypes Machine) {
549 size_t ImpSize = ImportName.size() + Sym.size() + 2; // +2 for NULs
550 if (!ExportName.empty())
551 ImpSize += ExportName.size() + 1;
552 size_t Size = sizeof(coff_import_header) + ImpSize;
553 char *Buf = Alloc.Allocate<char>(Size);
554 memset(Buf, 0, Size);
555 char *P = Buf;
556
557 // Write short import library.
558 auto *Imp = reinterpret_cast<coff_import_header *>(P);
559 P += sizeof(*Imp);
560 Imp->Sig2 = 0xFFFF;
561 Imp->Machine = Machine;
562 Imp->SizeOfData = ImpSize;
563 if (Ordinal > 0)
564 Imp->OrdinalHint = Ordinal;
565 Imp->TypeInfo = (NameType << 2) | ImportType;
566
567 // Write symbol name and DLL name.
568 memcpy(P, Sym.data(), Sym.size());
569 P += Sym.size() + 1;
570 memcpy(P, ImportName.data(), ImportName.size());
571 if (!ExportName.empty()) {
572 P += ImportName.size() + 1;
573 memcpy(P, ExportName.data(), ExportName.size());
574 }
575
576 return {MemoryBufferRef(StringRef(Buf, Size), ImportName)};
577}
578
579NewArchiveMember ObjectFactory::createWeakExternal(StringRef Sym,
580 StringRef Weak, bool Imp,
582 std::vector<uint8_t> Buffer;
583 const uint32_t NumberOfSections = 1;
584 const uint32_t NumberOfSymbols = 5;
585
586 // COFF Header
587 coff_file_header Header{
588 u16(Machine),
589 u16(NumberOfSections),
590 u32(0),
591 u32(sizeof(Header) + (NumberOfSections * sizeof(coff_section))),
592 u32(NumberOfSymbols),
593 u16(0),
594 u16(0),
595 };
596 append(Buffer, Header);
597
598 // Section Header Table
599 const coff_section SectionTable[NumberOfSections] = {
600 {{'.', 'd', 'r', 'e', 'c', 't', 'v', 'e'},
601 u32(0),
602 u32(0),
603 u32(0),
604 u32(0),
605 u32(0),
606 u32(0),
607 u16(0),
608 u16(0),
610 append(Buffer, SectionTable);
611
612 // Symbol Table
613 coff_symbol16 SymbolTable[NumberOfSymbols] = {
614 {{{'@', 'c', 'o', 'm', 'p', '.', 'i', 'd'}},
615 u32(0),
616 u16(0xFFFF),
617 u16(0),
619 0},
620 {{{'@', 'f', 'e', 'a', 't', '.', '0', '0'}},
621 u32(0),
622 u16(0xFFFF),
623 u16(0),
625 0},
626 {{{0, 0, 0, 0, 0, 0, 0, 0}},
627 u32(0),
628 u16(0),
629 u16(0),
631 0},
632 {{{0, 0, 0, 0, 0, 0, 0, 0}},
633 u32(0),
634 u16(0),
635 u16(0),
637 1},
638 {{{2, 0, 0, 0, IMAGE_WEAK_EXTERN_SEARCH_ALIAS, 0, 0, 0}},
639 u32(0),
640 u16(0),
641 u16(0),
643 0},
644 };
645 SymbolTable[2].Name.Offset.Offset = sizeof(uint32_t);
646
647 //__imp_ String Table
648 StringRef Prefix = Imp ? "__imp_" : "";
649 SymbolTable[3].Name.Offset.Offset =
650 sizeof(uint32_t) + Sym.size() + Prefix.size() + 1;
651 append(Buffer, SymbolTable);
652 writeStringTable(Buffer, {(Prefix + Sym).str(),
653 (Prefix + Weak).str()});
654
655 // Copied here so we can still use writeStringTable
656 char *Buf = Alloc.Allocate<char>(Buffer.size());
657 memcpy(Buf, Buffer.data(), Buffer.size());
658 return {MemoryBufferRef(StringRef(Buf, Buffer.size()), ImportName)};
659}
660
663 MachineTypes Machine, bool MinGW,
664 ArrayRef<COFFShortExport> NativeExports) {
665
666 MachineTypes NativeMachine = Machine;
667 if (isArm64EC(Machine)) {
668 NativeMachine = IMAGE_FILE_MACHINE_ARM64;
670 }
671
672 std::vector<NewArchiveMember> Members;
673 ObjectFactory OF(llvm::sys::path::filename(ImportName), NativeMachine);
674
675 std::vector<uint8_t> ImportDescriptor;
676 Members.push_back(OF.createImportDescriptor(ImportDescriptor));
677
678 std::vector<uint8_t> NullImportDescriptor;
679 Members.push_back(OF.createNullImportDescriptor(NullImportDescriptor));
680
681 std::vector<uint8_t> NullThunk;
682 Members.push_back(OF.createNullThunk(NullThunk));
683
684 auto addExports = [&](ArrayRef<COFFShortExport> Exp,
685 MachineTypes M) -> Error {
686 StringMap<std::string> RegularImports;
687 struct Deferred {
688 std::string Name;
689 ImportType ImpType;
690 const COFFShortExport *Export;
691 };
693 for (const COFFShortExport &E : Exp) {
694 if (E.Private)
695 continue;
696
698 if (E.Data)
700 if (E.Constant)
702
703 StringRef SymbolName = E.SymbolName.empty() ? E.Name : E.SymbolName;
704 std::string Name;
705
706 if (E.ExtName.empty()) {
707 Name = std::string(SymbolName);
708 } else {
709 Expected<std::string> ReplacedName =
710 object::replace(SymbolName, E.Name, E.ExtName);
711 if (!ReplacedName)
712 return ReplacedName.takeError();
713 Name.swap(*ReplacedName);
714 }
715
717 std::string ExportName;
718 if (E.Noname) {
720 } else if (!E.ExportAs.empty()) {
722 ExportName = E.ExportAs;
723 } else if (!E.ImportName.empty()) {
724 // If we need to import from a specific ImportName, we may need to use
725 // a weak alias (which needs another import to point at). But if we can
726 // express ImportName based on the symbol name and a specific NameType,
727 // prefer that over an alias.
729 applyNameType(IMPORT_NAME_UNDECORATE, Name) == E.ImportName)
731 else if (Machine == IMAGE_FILE_MACHINE_I386 &&
732 applyNameType(IMPORT_NAME_NOPREFIX, Name) == E.ImportName)
734 else if (isArm64EC(M)) {
736 ExportName = E.ImportName;
737 } else if (Name == E.ImportName)
739 else {
740 Deferred D;
741 D.Name = Name;
742 D.ImpType = ImportType;
743 D.Export = &E;
744 Renames.push_back(D);
745 continue;
746 }
747 } else {
748 NameType = getNameType(SymbolName, E.Name, M, MinGW);
749 }
750
751 // On ARM64EC, use EXPORTAS to import demangled name for mangled symbols.
752 if (ImportType == IMPORT_CODE && isArm64EC(M)) {
753 if (std::optional<std::string> MangledName =
755 if (!E.Noname && ExportName.empty()) {
757 ExportName.swap(Name);
758 }
759 Name = std::move(*MangledName);
760 } else if (!E.Noname && ExportName.empty()) {
761 std::optional<std::string> DemangledName =
763 if (!DemangledName)
764 return make_error<StringError>(
765 StringRef(Twine("Invalid ARM64EC function name '" + Name + "'")
766 .str()),
769 ExportName = std::move(*DemangledName);
770 }
771 }
772
773 RegularImports[applyNameType(NameType, Name)] = Name;
774 Members.push_back(OF.createShortImport(Name, E.Ordinal, ImportType,
775 NameType, ExportName, M));
776 }
777 for (const auto &D : Renames) {
778 auto It = RegularImports.find(D.Export->ImportName);
779 if (It != RegularImports.end()) {
780 // We have a regular import entry for a symbol with the name we
781 // want to reference; produce an alias pointing at that.
782 StringRef Symbol = It->second;
783 if (D.ImpType == IMPORT_CODE)
784 Members.push_back(OF.createWeakExternal(Symbol, D.Name, false, M));
785 Members.push_back(OF.createWeakExternal(Symbol, D.Name, true, M));
786 } else {
787 Members.push_back(OF.createShortImport(D.Name, D.Export->Ordinal,
788 D.ImpType, IMPORT_NAME_EXPORTAS,
789 D.Export->ImportName, M));
790 }
791 }
792 return Error::success();
793 };
794
795 if (Error e = addExports(Exports, Machine))
796 return e;
797 if (Error e = addExports(NativeExports, NativeMachine))
798 return e;
799
800 return writeArchive(Path, Members, SymtabWritingMode::NormalSymtab,
802 /*Deterministic*/ true, /*Thin*/ false,
803 /*OldArchiveBuf*/ nullptr, isArm64EC(Machine));
804}
805
806} // namespace object
807} // 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:390
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:46
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:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
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:51
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:700
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:571
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:265
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:144
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:424
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:297
static constexpr size_t npos
Definition: StringRef.h:53
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.
@ IMAGE_REL_MIPS_REFWORDNB
Definition: COFF.h:434
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_R4000
Definition: COFF.h:112
@ 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:733
@ IMPORT_CONST
Definition: COFF.h:736
@ IMPORT_CODE
Definition: COFF.h:734
@ IMPORT_DATA
Definition: COFF.h:735
@ 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:489
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:739
@ IMPORT_ORDINAL
Import is by ordinal.
Definition: COFF.h:744
@ IMPORT_NAME_EXPORTAS
The import name is specified as a separate string in the import library object file.
Definition: COFF.h:755
@ IMPORT_NAME
The import name is identical to the public symbol name.
Definition: COFF.h:746
@ IMPORT_NAME_UNDECORATE
The import name is the public symbol name, but skipping the leading ?, @, or optionally _,...
Definition: COFF.h:752
@ IMPORT_NAME_NOPREFIX
The import name is the public symbol name, but skipping the leading ?, @, or optionally _.
Definition: COFF.h:749
@ 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
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
Error writeImportLibrary(StringRef ImportName, StringRef Path, ArrayRef< COFFShortExport > Exports, COFF::MachineTypes Machine, bool MinGW, ArrayRef< COFFShortExport > NativeExports={})
Writes a COFF import library containing entries described by the Exports array.
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:577
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)
Returns the ARM64EC mangled function name unless the input is already mangled.
Definition: Mangler.cpp:294
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)
Returns the ARM64EC demangled function name, unless the input is not mangled.
Definition: Mangler.cpp:317
support::ulittle32_t Offset
Definition: COFF.h:250
Definition: COFF.h:559
StringTableOffset Offset
Definition: COFF.h:257
union llvm::object::coff_symbol::@362 Name