Skip to content

Commit 004e75e

Browse files
authored
Pack relocations for Android API >= 28 (llvm#117624)
Patch copied from: android/ndk#909 (comment) Fixes: android/ndk#909
1 parent 261d4bb commit 004e75e

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

clang/lib/Driver/ToolChains/Linux.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,24 @@ Linux::Linux(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
256256
ExtraOpts.push_back("-z");
257257
ExtraOpts.push_back("max-page-size=16384");
258258
}
259+
if (Triple.isAndroidVersionLT(29)) {
260+
// https://github.com/android/ndk/issues/1196
261+
// The unwinder used by the crash handler on versions of Android prior to
262+
// API 29 did not correctly handle binaries built with rosegment, which is
263+
// enabled by default for LLD. Android only supports LLD, so it's not an
264+
// issue that this flag is not accepted by other linkers.
265+
ExtraOpts.push_back("--no-rosegment");
266+
}
267+
if (!Triple.isAndroidVersionLT(28)) {
268+
// Android supports relr packing starting with API 28 and had its own
269+
// flavor (--pack-dyn-relocs=android) starting in API 23.
270+
// TODO: It's possible to use both with --pack-dyn-relocs=android+relr,
271+
// but we need to gather some data on the impact of that form before we
272+
// can know if it's a good default.
273+
// On the other hand, relr should always be an improvement.
274+
ExtraOpts.push_back("--use-android-relr-tags");
275+
ExtraOpts.push_back("--pack-dyn-relocs=relr");
276+
}
259277
}
260278

261279
if (GCCInstallation.getParentLibPath().contains("opt/rh/"))

clang/test/Driver/linux-ld.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,36 @@
940940
// CHECK-ANDROID-HASH-STYLE-M: "{{.*}}ld{{(.exe)?}}"
941941
// CHECK-ANDROID-HASH-STYLE-M: "--hash-style=gnu"
942942

943+
// Check that we pass --no-rosegment for pre-29 Android versions and do not for
944+
// 29+.
945+
// RUN: %clang %s -### -o %t.o 2>&1 \
946+
// RUN: --target=armv7-linux-android28 \
947+
// RUN: | FileCheck --check-prefix=CHECK-ANDROID-ROSEGMENT-28 %s
948+
// CHECK-ANDROID-ROSEGMENT-28: "{{.*}}ld{{(.exe)?}}"
949+
// CHECK-ANDROID-ROSEGMENT-28: "--no-rosegment"
950+
//
951+
// RUN: %clang %s -### -o %t.o 2>&1 \
952+
// RUN: --target=armv7-linux-android29 \
953+
// RUN: | FileCheck --check-prefix=CHECK-ANDROID-ROSEGMENT-29 %s
954+
// CHECK-ANDROID-ROSEGMENT-29: "{{.*}}ld{{(.exe)?}}"
955+
// CHECK-ANDROID-ROSEGMENT-29-NOT: "--no-rosegment"
956+
957+
// Check that we pass --pack-dyn-relocs=relr for API 28+ and not before.
958+
// RUN: %clang %s -### -o %t.o 2>&1 \
959+
// RUN: --target=armv7-linux-android27 \
960+
// RUN: | FileCheck --check-prefix=CHECK-ANDROID-RELR-27 %s
961+
// CHECK-ANDROID-RELR-27: "{{.*}}ld{{(.exe)?}}"
962+
// CHECK-ANDROID-RELR-27-NOT: "--pack-dyn-relocs=relr"
963+
// CHECK-ANDROID-RELR-27-NOT: "--pack-dyn-relocs=android+relr"
964+
//
965+
// RUN: %clang %s -### -o %t.o 2>&1 \
966+
// RUN: --target=armv7-linux-android28 \
967+
// RUN: | FileCheck --check-prefix=CHECK-ANDROID-RELR-28 %s
968+
// CHECK-ANDROID-RELR-28: "{{.*}}ld{{(.exe)?}}"
969+
// CHECK-ANDROID-RELR-28: "--use-android-relr-tags"
970+
// CHECK-ANDROID-RELR-28: "--pack-dyn-relocs=relr"
971+
// CHECK-ANDROID-RELR-28-NOT: "--pack-dyn-relocs=android+relr"
972+
943973
// RUN: %clang -### %s -no-pie 2>&1 --target=mips64-linux-gnuabin32 \
944974
// RUN: | FileCheck --check-prefix=CHECK-MIPS64EL-GNUABIN32 %s
945975
// CHECK-MIPS64EL-GNUABIN32: "{{.*}}ld{{(.exe)?}}"

0 commit comments

Comments
 (0)