@@ -71,6 +71,12 @@ StringRef COFFImportFile::getExportName() const {
71
71
name = ltrim1 (name, " ?@_" );
72
72
name = name.substr (0 , name.find (' @' ));
73
73
break ;
74
+ case IMPORT_NAME_EXPORTAS: {
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
+ }
74
80
default :
75
81
break ;
76
82
}
@@ -209,6 +215,7 @@ class ObjectFactory {
209
215
// Library Format.
210
216
NewArchiveMember createShortImport (StringRef Sym, uint16_t Ordinal,
211
217
ImportType Type, ImportNameType NameType,
218
+ StringRef ExportName,
212
219
MachineTypes Machine);
213
220
214
221
// Create a weak external file which is described in PE/COFF Aux Format 3.
@@ -500,12 +507,13 @@ NewArchiveMember ObjectFactory::createNullThunk(std::vector<uint8_t> &Buffer) {
500
507
return {MemoryBufferRef{F, ImportName}};
501
508
}
502
509
503
- NewArchiveMember ObjectFactory::createShortImport (StringRef Sym,
504
- uint16_t Ordinal,
505
- ImportType ImportType,
506
- ImportNameType NameType,
507
- MachineTypes Machine) {
510
+ NewArchiveMember
511
+ ObjectFactory::createShortImport (StringRef Sym, uint16_t Ordinal,
512
+ ImportType ImportType, ImportNameType NameType,
513
+ StringRef ExportName, MachineTypes Machine) {
508
514
size_t ImpSize = ImportName.size () + Sym.size () + 2 ; // +2 for NULs
515
+ if (!ExportName.empty ())
516
+ ImpSize += ExportName.size () + 1 ;
509
517
size_t Size = sizeof (coff_import_header) + ImpSize;
510
518
char *Buf = Alloc.Allocate <char >(Size );
511
519
memset (Buf, 0 , Size );
@@ -525,6 +533,10 @@ NewArchiveMember ObjectFactory::createShortImport(StringRef Sym,
525
533
memcpy (P, Sym.data (), Sym.size ());
526
534
P += Sym.size () + 1 ;
527
535
memcpy (P, ImportName.data (), ImportName.size ());
536
+ if (!ExportName.empty ()) {
537
+ P += ImportName.size () + 1 ;
538
+ memcpy (P, ExportName.data (), ExportName.size ());
539
+ }
528
540
529
541
return {MemoryBufferRef (StringRef (Buf, Size ), ImportName)};
530
542
}
@@ -641,27 +653,39 @@ Error writeImportLibrary(StringRef ImportName, StringRef Path,
641
653
ImportType = IMPORT_CONST;
642
654
643
655
StringRef SymbolName = E.SymbolName .empty () ? E.Name : E.SymbolName ;
644
- ImportNameType NameType = E.Noname
645
- ? IMPORT_ORDINAL
646
- : getNameType (SymbolName, E.Name ,
647
- Machine, MinGW);
648
- Expected<std::string> Name = E.ExtName .empty ()
649
- ? std::string (SymbolName)
650
- : replace (SymbolName, E.Name , E.ExtName );
651
-
652
- if (!Name)
653
- return Name.takeError ();
654
-
655
- if (!E.AliasTarget .empty () && *Name != E.AliasTarget ) {
656
+ std::string Name;
657
+
658
+ if (E.ExtName .empty ()) {
659
+ Name = std::string (SymbolName);
660
+ } else {
661
+ Expected<std::string> ReplacedName =
662
+ replace (SymbolName, E.Name , E.ExtName );
663
+ if (!ReplacedName)
664
+ return ReplacedName.takeError ();
665
+ Name.swap (*ReplacedName);
666
+ }
667
+
668
+ if (!E.AliasTarget .empty () && Name != E.AliasTarget ) {
656
669
Members.push_back (
657
- OF.createWeakExternal (E.AliasTarget , * Name, false , Machine));
670
+ OF.createWeakExternal (E.AliasTarget , Name, false , Machine));
658
671
Members.push_back (
659
- OF.createWeakExternal (E.AliasTarget , * Name, true , Machine));
672
+ OF.createWeakExternal (E.AliasTarget , Name, true , Machine));
660
673
continue ;
661
674
}
662
675
663
- Members.push_back (
664
- OF.createShortImport (*Name, E.Ordinal , ImportType, NameType, Machine));
676
+ ImportNameType NameType;
677
+ std::string ExportName;
678
+ if (E.Noname ) {
679
+ NameType = IMPORT_ORDINAL;
680
+ } else if (!E.ExportAs .empty ()) {
681
+ NameType = IMPORT_NAME_EXPORTAS;
682
+ ExportName = E.ExportAs ;
683
+ } else {
684
+ NameType = getNameType (SymbolName, E.Name , Machine, MinGW);
685
+ }
686
+
687
+ Members.push_back (OF.createShortImport (Name, E.Ordinal , ImportType,
688
+ NameType, ExportName, Machine));
665
689
}
666
690
667
691
return writeArchive (Path, Members, SymtabWritingMode::NormalSymtab,
0 commit comments