forked from OSchip/llvm-project
COFF: Implement dllimported symbol name mangling.
Symbols exported by DLLs are listed in import library files. Exported names may be mangled by "Import Name Type" field as described in PE/COFF spec 7.3. This patch implements that mangling scheme. llvm-svn: 241719
This commit is contained in:
parent
b1f9ce8fc9
commit
1c79ce9a4c
|
@ -20,12 +20,9 @@
|
|||
#include "llvm/Support/raw_ostream.h"
|
||||
#include <mutex>
|
||||
|
||||
using namespace llvm::COFF;
|
||||
using namespace llvm::object;
|
||||
using namespace llvm::support::endian;
|
||||
using llvm::COFF::ImportHeader;
|
||||
using llvm::COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
|
||||
using llvm::COFF::IMAGE_FILE_MACHINE_AMD64;
|
||||
using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN;
|
||||
using llvm::RoundUpToAlignment;
|
||||
using llvm::sys::fs::identify_magic;
|
||||
using llvm::sys::fs::file_magic;
|
||||
|
@ -260,11 +257,23 @@ std::error_code ImportFile::parse() {
|
|||
StringRef Name = StringAlloc.save(StringRef(Buf + sizeof(*Hdr)));
|
||||
StringRef ImpName = StringAlloc.save(Twine("__imp_") + Name);
|
||||
StringRef DLLName(Buf + sizeof(coff_import_header) + Name.size() + 1);
|
||||
StringRef ExternalName = Name;
|
||||
if (Hdr->getNameType() == llvm::COFF::IMPORT_ORDINAL)
|
||||
ExternalName = "";
|
||||
auto *ImpSym = new (Alloc) DefinedImportData(DLLName, ImpName, ExternalName,
|
||||
Hdr);
|
||||
StringRef ExtName;
|
||||
switch (Hdr->getNameType()) {
|
||||
case IMPORT_ORDINAL:
|
||||
ExtName = "";
|
||||
break;
|
||||
case IMPORT_NAME:
|
||||
ExtName = Name;
|
||||
break;
|
||||
case IMPORT_NAME_NOPREFIX:
|
||||
ExtName = Name.ltrim("?@_");
|
||||
break;
|
||||
case IMPORT_NAME_UNDECORATE:
|
||||
ExtName = Name.ltrim("?@_");
|
||||
ExtName = ExtName.substr(0, ExtName.find('@'));
|
||||
break;
|
||||
}
|
||||
auto *ImpSym = new (Alloc) DefinedImportData(DLLName, ImpName, ExtName, Hdr);
|
||||
SymbolBodies.push_back(ImpSym);
|
||||
|
||||
// If type is function, we need to create a thunk which jump to an
|
||||
|
|
Binary file not shown.
|
@ -0,0 +1,66 @@
|
|||
# RUN: yaml2obj < %s > %t.obj
|
||||
# RUN: lld -flavor link2 /out:%t.exe /opt:noref /entry:main \
|
||||
# RUN: %t.obj %p/Inputs/imports-mangle.lib
|
||||
# RUN: llvm-readobj -coff-imports %t.exe | FileCheck %s
|
||||
|
||||
# CHECK: Import {
|
||||
# CHECK: Symbol: sym4 (0)
|
||||
# CHECK: Symbol: sym3 (1)
|
||||
# CHECK: Symbol: sym1 (2)
|
||||
# CHECK: Symbol: (2)
|
||||
# CHECK: }
|
||||
|
||||
---
|
||||
header:
|
||||
Machine: IMAGE_FILE_MACHINE_AMD64
|
||||
Characteristics: []
|
||||
sections:
|
||||
- Name: .text
|
||||
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
|
||||
Alignment: 4
|
||||
SectionData: 000000000000
|
||||
symbols:
|
||||
- Name: .text
|
||||
Value: 0
|
||||
SectionNumber: 1
|
||||
SimpleType: IMAGE_SYM_TYPE_NULL
|
||||
ComplexType: IMAGE_SYM_DTYPE_NULL
|
||||
StorageClass: IMAGE_SYM_CLASS_STATIC
|
||||
SectionDefinition:
|
||||
Length: 6
|
||||
NumberOfRelocations: 0
|
||||
NumberOfLinenumbers: 0
|
||||
CheckSum: 0
|
||||
Number: 0
|
||||
Selection: IMAGE_COMDAT_SELECT_ANY
|
||||
- Name: main
|
||||
Value: 0
|
||||
SectionNumber: 1
|
||||
SimpleType: IMAGE_SYM_TYPE_NULL
|
||||
ComplexType: IMAGE_SYM_DTYPE_FUNCTION
|
||||
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
|
||||
- Name: sym1
|
||||
Value: 0
|
||||
SectionNumber: 0
|
||||
SimpleType: IMAGE_SYM_TYPE_NULL
|
||||
ComplexType: IMAGE_SYM_DTYPE_FUNCTION
|
||||
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
|
||||
- Name: sym2
|
||||
Value: 0
|
||||
SectionNumber: 0
|
||||
SimpleType: IMAGE_SYM_TYPE_NULL
|
||||
ComplexType: IMAGE_SYM_DTYPE_FUNCTION
|
||||
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
|
||||
- Name: _sym3
|
||||
Value: 0
|
||||
SectionNumber: 0
|
||||
SimpleType: IMAGE_SYM_TYPE_NULL
|
||||
ComplexType: IMAGE_SYM_DTYPE_FUNCTION
|
||||
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
|
||||
- Name: '?sym4@@YAHH@Z'
|
||||
Value: 0
|
||||
SectionNumber: 0
|
||||
SimpleType: IMAGE_SYM_TYPE_NULL
|
||||
ComplexType: IMAGE_SYM_DTYPE_FUNCTION
|
||||
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
|
||||
...
|
Loading…
Reference in New Issue