Skip to content

Commit

Permalink
llvm: update to 19.1.7 and add import libs fix for LLD
Browse files Browse the repository at this point in the history
  • Loading branch information
mati865 committed Jan 18, 2025
1 parent 981f795 commit 6004385
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 18 deletions.
2 changes: 1 addition & 1 deletion mingw-w64-flang/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ _compiler=clang
_realname=flang
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_version=19.1.6
_version=19.1.7
_rc=""
_tag=llvmorg-${_version}${_rc}
pkgver=${_version}${_rc/-/}
Expand Down
2 changes: 1 addition & 1 deletion mingw-w64-libc++/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ _realname=libc++
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
"${MINGW_PACKAGE_PREFIX}-libunwind")
_version=19.1.6
_version=19.1.7
_rc=""
_tag=llvmorg-${_version}${_rc}
pkgver=${_version}${_rc/-/}
Expand Down
2 changes: 1 addition & 1 deletion mingw-w64-libclc/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
_realname=libclc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_version=19.1.6
_version=19.1.7
_rc=""
_tag=llvmorg-${_version}${_rc}
pkgver=${_version}${_rc/-/}
Expand Down
3 changes: 1 addition & 2 deletions mingw-w64-librsvg/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
"${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=2.59.2
pkgrel=1
pkgrel=2
pkgdesc="SVG rendering library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
Expand All @@ -32,7 +32,6 @@ optdepends=("${MINGW_PACKAGE_PREFIX}-gtk3: for rsvg-view-3")
source=("https://download.gnome.org/sources/librsvg/${pkgver%.*}/${_realname}-${pkgver}.tar.xz"
"glib-0.20.0.tar.gz::https://crates.io/api/v1/crates/glib/0.20.0/download"
"glib-rs.patch"
"0002-avoid-link-whole.patch"
"0004-add-option-for-disabling-convert.patch")
sha256sums=('ecd293fb0cc338c170171bbc7bcfbea6725d041c95f31385dc935409933e4597'
'fee90a615ce05be7a32932cfb8adf2c4bbb4700e80d37713c981fb24c0c56238'
Expand Down
2 changes: 1 addition & 1 deletion mingw-w64-lldb/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fi
_realname=lldb
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_version=19.1.6
_version=19.1.7
_rc=""
_tag=llvmorg-${_version}${_rc}
pkgver=${_version}${_rc/-/}
Expand Down
2 changes: 1 addition & 1 deletion mingw-w64-llvm-openmp/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fi
_realname=openmp
pkgbase=mingw-w64-llvm-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-llvm-${_realname}")
_version=19.1.6
_version=19.1.7
_rc=""
_tag=llvmorg-${_version}${_rc}
pkgver=${_version}${_rc/-/}
Expand Down
1 change: 1 addition & 0 deletions mingw-w64-llvm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"0301-wholearchive-import-libs.patch"
210 changes: 210 additions & 0 deletions mingw-w64-llvm/0901-wholearchive-import-libs.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
From 4a4a8a1476b1386b523dc5b292ba9a5a6748a9cf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <martin@martin.st>
Date: Thu, 16 Jan 2025 00:09:09 +0200
Subject: [PATCH] [LLD] [COFF] Fix linking import libraries with -wholearchive:
(#122806)

When LLD links against an import library (for the regular, short import
libraries), it doesn't actually link in the header/trailer object files
at all, but synthesizes new corresponding data structures into the right
sections.

If the whole of such an import library is forced to be linked, e.g. with
the -wholearchive: option, we actually end up linking in those
header/trailer objects. The header objects contain a construct which LLD
fails to handle; previously we'd error out with the error ".idata$4
should not refer to special section 0".

Within the import library header object, in the import directory we have
relocations towards the IAT (.idata$4 and .idata$5), but the header
object itself doesn't contain any data for those sections.

In the case of GNU generated import libraries, the header objects
contain zero length sections .idata$4 and .idata$5, with relocations
against them. However in the case of LLVM generated import libraries,
the sections .idata$4 and .idata$5 are not included in the list of
sections. The symbol table does contain section symbols for these
sections, but without any actual associated section. This can probably
be seen as a declaration of an empty section.

If the header/trailer objects of a short import library are linked
forcibly and we also reference other functions in the library, we end up
with two import directory entries for this DLL, one that gets
synthesized by LLD, and one from the actual header object file. This is
inelegant, but should be acceptable.

While it would seem unusual to link import libraries with the
-wholearchive: option, this can happen in certain scenarios.

Rust builds libraries that contain relevant import libraries bundled
along with compiled Rust code as regular object files, all within one
single archive. Such an archive can then end up linked with the
-wholarchive: option, if build systems decide to use such an option for
including static libraries.

This should fix /~https://github.com/msys2/MINGW-packages/issues/21017.

This works for the header/trailer object files in import libraries
generated by LLVM; import libraries generated by MSVC are vaguely
different. ecb5ea6a266d5cc4e05252f6db4c73613b73cc3b did an attempt at
fixing the issue for MSVC generated libraries, but it's not entirely
correct, and isn't enough for making things work for that case.
---
lld/COFF/InputFiles.cpp | 20 ++++++++++
lld/test/COFF/empty-section-decl.yaml | 56 +++++++++++++++++++++++++++
lld/test/COFF/wholearchive-implib.s | 35 +++++++++++++++++
llvm/include/llvm/Object/COFF.h | 5 +++
4 files changed, 116 insertions(+)
create mode 100644 lld/test/COFF/empty-section-decl.yaml
create mode 100644 lld/test/COFF/wholearchive-implib.s

diff --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp
index e7891d3e67f3e4..66641ff9dcc1f0 100644
--- a/lld/COFF/InputFiles.cpp
+++ b/lld/COFF/InputFiles.cpp
@@ -746,6 +746,26 @@ std::optional<Symbol *> ObjFile::createDefined(
if (sectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
return nullptr;

+ if (sym.isEmptySectionDeclaration()) {
+ // As there is no coff_section in the object file for these, make a
+ // new virtual one, with everything zeroed out (i.e. an empty section),
+ // with only the name and characteristics set.
+ StringRef name = getName();
+ auto *hdr = make<coff_section>();
+ memset(hdr, 0, sizeof(*hdr));
+ strncpy(hdr->Name, name.data(),
+ std::min(name.size(), (size_t)COFF::NameSize));
+ // We have no idea what characteristics should be assumed here; pick
+ // a default. This matches what is used for .idata sections in the regular
+ // object files in import libraries.
+ hdr->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
+ IMAGE_SCN_MEM_WRITE | IMAGE_SCN_ALIGN_4BYTES;
+ auto *sc = make<SectionChunk>(this, hdr);
+ chunks.push_back(sc);
+ return make<DefinedRegular>(this, /*name=*/"", /*isCOMDAT=*/false,
+ /*isExternal=*/false, sym.getGeneric(), sc);
+ }
+
if (llvm::COFF::isReservedSectionNumber(sectionNumber))
Fatal(ctx) << toString(this) << ": " << getName()
<< " should not refer to special section "
diff --git a/lld/test/COFF/empty-section-decl.yaml b/lld/test/COFF/empty-section-decl.yaml
new file mode 100644
index 00000000000000..320df340000289
--- /dev/null
+++ b/lld/test/COFF/empty-section-decl.yaml
@@ -0,0 +1,56 @@
+# REQUIRES: x86
+
+# RUN: yaml2obj %s -o %t.obj
+# RUN: lld-link -dll -out:%t.dll %t.obj -noentry -subsystem:console -lldmap:%t.map
+# RUN: llvm-objdump -s %t.dll | FileCheck %s
+# RUN: FileCheck %s --check-prefix=MAP < %t.map
+
+# CHECK: Contents of section .itest:
+# CHECK-NEXT: 180001000 0c100080 01000000 00000000 01000000
+
+# MAP: 00001000 0000000a 4 {{.*}}:(.itest$2)
+# MAP: 00001000 00000000 0 .itest$2
+# MAP: 0000100c 00000000 4 {{.*}}:(.itest$4)
+# MAP: 0000100c 00000000 0 .itest$4
+# MAP: 0000100c 00000004 2 {{.*}}:(.itest$6)
+# MAP: 0000100c 00000000 0 .itest$6
+
+--- !COFF
+header:
+ Machine: IMAGE_FILE_MACHINE_AMD64
+ Characteristics: [ ]
+sections:
+ - Name: '.itest$2'
+ Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+ Alignment: 4
+ SectionData: '00000000000000000000'
+ SizeOfRawData: 10
+ Relocations:
+ - VirtualAddress: 0
+ SymbolName: '.itest$4'
+ Type: IMAGE_REL_AMD64_ADDR64
+ - Name: '.itest$6'
+ Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+ Alignment: 2
+ SectionData: 01000000
+ SizeOfRawData: 4
+symbols:
+ - Name: '.itest$2'
+ Value: 0
+ SectionNumber: 1
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_SECTION
+ - Name: '.itest$6'
+ Value: 0
+ SectionNumber: 2
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+ - Name: '.itest$4'
+ Value: 0
+ SectionNumber: 0
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_SECTION
+...
diff --git a/lld/test/COFF/wholearchive-implib.s b/lld/test/COFF/wholearchive-implib.s
new file mode 100644
index 00000000000000..0c98ca0ddef072
--- /dev/null
+++ b/lld/test/COFF/wholearchive-implib.s
@@ -0,0 +1,35 @@
+// REQUIRES: x86
+// RUN: split-file %s %t.dir
+// RUN: llvm-lib -machine:amd64 -out:%t.lib -def:%t.dir/lib.def
+// RUN: llvm-mc -filetype=obj -triple=x86_64-windows %t.dir/main.s -o %t.main.obj
+
+// RUN: lld-link -out:%t.exe %t.main.obj -wholearchive:%t.lib -entry:entry -subsystem:console
+// RUN: llvm-readobj --coff-imports %t.exe | FileCheck %s
+
+// As LLD usually doesn't use the header/trailer object files from import
+// libraries, but instead synthesizes those structures, we end up with two
+// import directory entries if we force those objects to be included.
+
+// CHECK: Import {
+// CHECK-NEXT: Name: lib.dll
+// CHECK-NEXT: ImportLookupTableRVA: 0x2050
+// CHECK-NEXT: ImportAddressTableRVA: 0x2068
+// CHECK-NEXT: }
+// CHECK-NEXT: Import {
+// CHECK-NEXT: Name: lib.dll
+// CHECK-NEXT: ImportLookupTableRVA: 0x2058
+// CHECK-NEXT: ImportAddressTableRVA: 0x2070
+// CHECK-NEXT: Symbol: func (0)
+// CHECK-NEXT: }
+
+
+#--- main.s
+.global entry
+entry:
+ call func
+ ret
+
+#--- lib.def
+LIBRARY lib.dll
+EXPORTS
+func
diff --git a/llvm/include/llvm/Object/COFF.h b/llvm/include/llvm/Object/COFF.h
index 05b3587224c296..4de2c680f57b1a 100644
--- a/llvm/include/llvm/Object/COFF.h
+++ b/llvm/include/llvm/Object/COFF.h
@@ -392,6 +392,11 @@ class COFFSymbolRef {
getValue() == 0;
}

+ bool isEmptySectionDeclaration() const {
+ return isSection() && getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED &&
+ getValue() == 0;
+ }
+
bool isWeakExternal() const {
return getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
}
35 changes: 26 additions & 9 deletions mingw-w64-llvm/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pkgname=("${MINGW_PACKAGE_PREFIX}-llvm"
"${MINGW_PACKAGE_PREFIX}-compiler-rt"
$( (( _clangprefix )) && echo "${MINGW_PACKAGE_PREFIX}-gcc-compat" )
"${MINGW_PACKAGE_PREFIX}-lld")
_version=19.1.6
_version=19.1.7
_rc=""
_tag=llvmorg-${_version}${_rc}
pkgver=${_version}${_rc/-/}
Expand Down Expand Up @@ -77,28 +77,31 @@ source=("${_url}/llvm-${pkgver}.src.tar.xz"{,.sig}
# 0301-0399 -> lld
"0303-ignore-new-bfd-options.patch"
# 0401-0499 -> clang-tools-extra
# 9901-9999 -> whole tree
"0901-wholearchive-import-libs.patch::/~https://github.com/llvm/llvm-project/commit/4a4a8a1476b1386b523dc5b292ba9a5a6748a9cf.patch"
)
sha256sums=('ad1a3b125ff014ded290094088de40efb9193ce81a24278184230b7d401f8a3e'
sha256sums=('96f833c6ad99a3e8e1d9aca5f439b8fd2c7efdcf83b664e0af1c0712c5315910'
'SKIP'
'6358cbb3e14687ca2f3465c61cffc65589b448aaa912ec2c163ef9fc046e8a89'
'11e5e4ecab5338b9914de3b83a4622cb200de466b7c56ba675afb72fa7d64675'
'SKIP'
'417a7d0048d8eb62dbaa8461f1fd474eb6493b09e14d050b39cc1f051e8b71d1'
'430b088bf820550090a6cf0eec67183d045273c18b3f1d6c5207e46d93391be7'
'SKIP'
'0d4f312e1419152282c267e6b6a1fa5914a7a0c753a5e926bee1c8c28e614ae4'
'c12b6e764202c615c1a3af9a13d477846878757ae0e29e5f8979215a6958fffc'
'SKIP'
'44fa50556cefbe3796e8256536267ee2f34ced5f3c1492473bccd75449cadbe6'
'630bf0f31b210e89fd7869bdfd6c376d010fc3b72ee42f08058435035c432e42'
'SKIP'
'9c7ec82d9a240dc2287b8de89d6881bb64ceea0dcd6ce133c34ef65bda22d99e'
'11c5a28f90053b0c43d0dec3d0ad579347fc277199c005206b963c19aae514e3'
'SKIP'
'0e8048333bab2ba3607910e5d074259f08dccf00615778d03a2a55416718eb45'
'b96deca1d3097c7ffd4ff2bb904a50bdd56bec7ed1413ffb0d1d01af87b72c12'
'SKIP'
'eb03df53671df6627768141b3aaa76abe176a14e5e47911c97bec544387c4aff'
'ff8bf96fae9531cf6231b502e8c2f38843f826a9dc7e0d98a37cb80fddd36cf4'
'790eb0fccb4ef29c3795bceb8a62c8f4ecd0bdd6c49b2812352b04cfbc552342'
'76d2a88733cebb1f01b766ff2ab91a9e095bae1d2e4cc18c1ffcf21fd095efc4'
'715cb8862753854b2d9256e0b70003e2d1f57083d83eaeaf5a095fc72b8a4e26'
'2770cadf8ccf6b31aece6aee8f76dceb71e6e9d01fdf3be74c3743480ce34899'
'fd0253879cc5e31857f57307e6105e56493358e35d7c4540e0b476be607ab94e')
'fd0253879cc5e31857f57307e6105e56493358e35d7c4540e0b476be607ab94e'
'630806c9f232d7755ea8fb2c9e2110a511676d728a4f39e91e4867d1e460c1da')
validpgpkeys=('B6C8F98282B944E3B0D5C2530FC3042E345AD05D' # Hans Wennborg, Google.
'474E22316ABF4785A88C6E8EA2C794A986419D8A' # Tom Stellard
'D574BD5D1D0E98895E3BF90044F2485E45D59042') # Tobias Hieta
Expand All @@ -112,6 +115,14 @@ apply_patch_with_msg() {
done
}

apply_git_patch_with_msg() {
for _patch in "$@"
do
msg2 "Applying ${_patch}"
patch -Nbp1 -i "${srcdir}/${_patch}"
done
}

revert_patch_with_msg() {
for _patch in "$@"
do
Expand Down Expand Up @@ -161,6 +172,12 @@ prepare() {

# Patch clang-tools-extra
cd "${srcdir}"/clang-tools-extra

# Patch whole tree
cd "${srcdir}"

apply_git_patch_with_msg \
"0901-wholearchive-import-libs.patch"
}

build() {
Expand Down
2 changes: 1 addition & 1 deletion mingw-w64-mlir/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ _compiler=clang
_realname=mlir
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_version=19.1.6
_version=19.1.7
_rc=""
_tag=llvmorg-${_version}${_rc}
pkgver=${_version}${_rc/-/}
Expand Down
2 changes: 1 addition & 1 deletion mingw-w64-polly/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fi
_realname=polly
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_version=19.1.6
_version=19.1.7
_rc=""
_tag=llvmorg-${_version}${_rc}
pkgver=${_version}${_rc/-/}
Expand Down

0 comments on commit 6004385

Please sign in to comment.