Skip to content

Commit 7b275aa

Browse files
authored
[LLD][COFF] Add support for IMPORT_NAME_EXPORTAS import library names. (#83211)
This allows handling importlibs produced by llvm-dlltool in #78772. ARM64EC import libraries use it by default, but it's supported by MSVC link.exe on other platforms too. This also avoids assuming null-terminated input, like in #78769.
1 parent fe1645e commit 7b275aa

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

lld/COFF/InputFiles.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -944,18 +944,20 @@ ImportFile::ImportFile(COFFLinkerContext &ctx, MemoryBufferRef m)
944944
: InputFile(ctx, ImportKind, m), live(!ctx.config.doGC), thunkLive(live) {}
945945

946946
void ImportFile::parse() {
947-
const char *buf = mb.getBufferStart();
948-
const auto *hdr = reinterpret_cast<const coff_import_header *>(buf);
947+
const auto *hdr =
948+
reinterpret_cast<const coff_import_header *>(mb.getBufferStart());
949949

950950
// Check if the total size is valid.
951-
if (mb.getBufferSize() != sizeof(*hdr) + hdr->SizeOfData)
951+
if (mb.getBufferSize() < sizeof(*hdr) ||
952+
mb.getBufferSize() != sizeof(*hdr) + hdr->SizeOfData)
952953
fatal("broken import library");
953954

954955
// Read names and create an __imp_ symbol.
955-
StringRef name = saver().save(StringRef(buf + sizeof(*hdr)));
956+
StringRef buf = mb.getBuffer().substr(sizeof(*hdr));
957+
StringRef name = saver().save(buf.split('\0').first);
956958
StringRef impName = saver().save("__imp_" + name);
957-
const char *nameStart = buf + sizeof(coff_import_header) + name.size() + 1;
958-
dllName = std::string(StringRef(nameStart));
959+
buf = buf.substr(name.size() + 1);
960+
dllName = buf.split('\0').first;
959961
StringRef extName;
960962
switch (hdr->getNameType()) {
961963
case IMPORT_ORDINAL:
@@ -971,6 +973,9 @@ void ImportFile::parse() {
971973
extName = ltrim1(name, "?@_");
972974
extName = extName.substr(0, extName.find('@'));
973975
break;
976+
case IMPORT_NAME_EXPORTAS:
977+
extName = buf.substr(dllName.size() + 1).split('\0').first;
978+
break;
974979
}
975980

976981
this->hdr = hdr;

lld/test/COFF/exportas.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
REQUIRES: x86
2+
RUN: split-file %s %t.dir && cd %t.dir
3+
4+
Link to an import library containing EXPORTAS and verify that we use proper name for the import.
5+
6+
RUN: llvm-mc -filetype=obj -triple=x86_64-windows test.s -o test.obj
7+
RUN: llvm-lib -machine:amd64 -out:test.lib -def:test.def
8+
RUN: lld-link -out:out1.dll -dll -noentry test.obj test.lib
9+
RUN: llvm-readobj --coff-imports out1.dll | FileCheck --check-prefix=IMPORT %s
10+
IMPORT: Symbol: expfunc
11+
12+
#--- test.s
13+
.section ".test", "rd"
14+
.rva __imp_func
15+
16+
#--- test.def
17+
LIBRARY test.dll
18+
EXPORTS
19+
func EXPORTAS expfunc

0 commit comments

Comments
 (0)