@@ -52,25 +52,37 @@ StringRef COFFImportFile::getFileFormatName() const {
52
52
}
53
53
}
54
54
55
- StringRef COFFImportFile::getExportName () const {
56
- const coff_import_header *hdr = getCOFFImportHeader ();
57
- StringRef name = Data.getBuffer ().substr (sizeof (*hdr)).split (' \0 ' ).first ;
58
-
55
+ static StringRef applyNameType (ImportNameType Type, StringRef name) {
59
56
auto ltrim1 = [](StringRef s, StringRef chars) {
60
57
return !s.empty () && chars.contains (s[0 ]) ? s.substr (1 ) : s;
61
58
};
62
59
63
- switch (hdr->getNameType ()) {
64
- case IMPORT_ORDINAL:
65
- name = " " ;
66
- break ;
60
+ switch (Type) {
67
61
case IMPORT_NAME_NOPREFIX:
68
62
name = ltrim1 (name, " ?@_" );
69
63
break ;
70
64
case IMPORT_NAME_UNDECORATE:
71
65
name = ltrim1 (name, " ?@_" );
72
66
name = name.substr (0 , name.find (' @' ));
73
67
break ;
68
+ default :
69
+ break ;
70
+ }
71
+ return name;
72
+ }
73
+
74
+ StringRef COFFImportFile::getExportName () const {
75
+ const coff_import_header *hdr = getCOFFImportHeader ();
76
+ StringRef name = Data.getBuffer ().substr (sizeof (*hdr)).split (' \0 ' ).first ;
77
+
78
+ switch (hdr->getNameType ()) {
79
+ case IMPORT_ORDINAL:
80
+ name = " " ;
81
+ break ;
82
+ case IMPORT_NAME_NOPREFIX:
83
+ case IMPORT_NAME_UNDECORATE:
84
+ name = applyNameType (static_cast <ImportNameType>(hdr->getNameType ()), name);
85
+ break ;
74
86
case IMPORT_NAME_EXPORTAS: {
75
87
// Skip DLL name
76
88
name = Data.getBuffer ().substr (sizeof (*hdr) + name.size () + 1 );
@@ -691,26 +703,38 @@ Error writeImportLibrary(StringRef ImportName, StringRef Path,
691
703
Name.swap (*ReplacedName);
692
704
}
693
705
694
- if (!E.ImportName .empty () && Name != E.ImportName ) {
695
- StringRef Prefix = " " ;
696
- if (Machine == IMAGE_FILE_MACHINE_I386 && AddUnderscores)
697
- Prefix = " _" ;
698
-
699
- if (ImportType == IMPORT_CODE)
700
- Members.push_back (OF.createWeakExternal ((Prefix + E.ImportName ).str (),
701
- Name, false , M));
702
- Members.push_back (OF.createWeakExternal ((Prefix + E.ImportName ).str (),
703
- Name, true , M));
704
- continue ;
705
- }
706
-
707
706
ImportNameType NameType;
708
707
std::string ExportName;
709
708
if (E.Noname ) {
710
709
NameType = IMPORT_ORDINAL;
711
710
} else if (!E.ExportAs .empty ()) {
712
711
NameType = IMPORT_NAME_EXPORTAS;
713
712
ExportName = E.ExportAs ;
713
+ } else if (!E.ImportName .empty ()) {
714
+ // If we need to import from a specific ImportName, we may need to use
715
+ // a weak alias (which needs another import to point at). But if we can
716
+ // express ImportName based on the symbol name and a specific NameType,
717
+ // prefer that over an alias.
718
+ if (Machine == IMAGE_FILE_MACHINE_I386 &&
719
+ applyNameType (IMPORT_NAME_UNDECORATE, Name) == E.ImportName )
720
+ NameType = IMPORT_NAME_UNDECORATE;
721
+ else if (Machine == IMAGE_FILE_MACHINE_I386 &&
722
+ applyNameType (IMPORT_NAME_NOPREFIX, Name) == E.ImportName )
723
+ NameType = IMPORT_NAME_NOPREFIX;
724
+ else if (Name == E.ImportName )
725
+ NameType = IMPORT_NAME;
726
+ else {
727
+ StringRef Prefix = " " ;
728
+ if (Machine == IMAGE_FILE_MACHINE_I386 && AddUnderscores)
729
+ Prefix = " _" ;
730
+
731
+ if (ImportType == IMPORT_CODE)
732
+ Members.push_back (OF.createWeakExternal (
733
+ (Prefix + E.ImportName ).str (), Name, false , M));
734
+ Members.push_back (OF.createWeakExternal ((Prefix + E.ImportName ).str (),
735
+ Name, true , M));
736
+ continue ;
737
+ }
714
738
} else {
715
739
NameType = getNameType (SymbolName, E.Name , M, MinGW);
716
740
}
0 commit comments