From 41c06d176db62940e0f9fbced5bb4e87c5494ded Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Tue, 3 Dec 2024 19:14:40 -0800 Subject: [PATCH 1/3] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?= =?UTF-8?q?itial=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Created using spr 1.3.6-beta.1 --- lld/ELF/Relocations.cpp | 20 +++++++++------- lld/ELF/Relocations.h | 3 +++ lld/ELF/Writer.cpp | 14 +++++++---- lld/test/ELF/pack-dyn-relocs-ifunc-static.s | 26 +++++++++++++++++++++ 4 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 lld/test/ELF/pack-dyn-relocs-ifunc-static.s diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 4aa27b0a71bc1..204599c544bc4 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1687,6 +1687,16 @@ template void elf::scanRelocations(Ctx &ctx) { outerFn(); } +RelocationBaseSection &elf::getIRelativeSection(Ctx &ctx) { + // Prior to Android V, there was a bug that caused RELR relocations to be + // applied after packed relocations. This meant that resolvers referenced by + // IRELATIVE relocations in the packed relocation section would read + // unrelocated globals with RELR relocations when + // --pack-relative-relocs=android+relr is enabled. Work around this by placing + // IRELATIVE in .rela.plt. + return ctx.arg.androidPackDynRelocs ? *ctx.in.relaPlt : *ctx.mainPart->relaDyn; +} + static bool handleNonPreemptibleIfunc(Ctx &ctx, Symbol &sym, uint16_t flags) { // Handle a reference to a non-preemptible ifunc. These are special in a // few ways: @@ -1736,17 +1746,9 @@ static bool handleNonPreemptibleIfunc(Ctx &ctx, Symbol &sym, uint16_t flags) { // original section/value pairs. For non-GOT non-PLT relocation case below, we // may alter section/value, so create a copy of the symbol to make // section/value fixed. - // - // Prior to Android V, there was a bug that caused RELR relocations to be - // applied after packed relocations. This meant that resolvers referenced by - // IRELATIVE relocations in the packed relocation section would read - // unrelocated globals with RELR relocations when - // --pack-relative-relocs=android+relr is enabled. Work around this by placing - // IRELATIVE in .rela.plt. auto *directSym = makeDefined(cast(sym)); directSym->allocateAux(ctx); - auto &dyn = - ctx.arg.androidPackDynRelocs ? *ctx.in.relaPlt : *ctx.mainPart->relaDyn; + auto &dyn = getIRelativeSection(ctx); addPltEntry(ctx, *ctx.in.iplt, *ctx.in.igotPlt, dyn, ctx.target->iRelativeRel, *directSym); sym.allocateAux(ctx); diff --git a/lld/ELF/Relocations.h b/lld/ELF/Relocations.h index 71cea0220e04c..7ca203257ea87 100644 --- a/lld/ELF/Relocations.h +++ b/lld/ELF/Relocations.h @@ -22,6 +22,7 @@ class Symbol; class InputSection; class InputSectionBase; class OutputSection; +class RelocationBaseSection; class SectionBase; // Represents a relocation type, such as R_X86_64_PC32 or R_ARM_THM_CALL. @@ -356,6 +357,8 @@ sortRels(Relocs> rels, return {}; } +RelocationBaseSection &getIRelativeSection(Ctx &ctx); + // Returns true if Expr refers a GOT entry. Note that this function returns // false for TLS variables even though they need GOT, because TLS variables uses // GOT differently than the regular variables. diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index f10cc54c05a0c..aa7c36bc9ac13 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -844,11 +844,15 @@ template void Writer::setReservedSymbolSections() { ctx.sym.globalOffsetTable->section = sec; } - // .rela_iplt_{start,end} mark the start and the end of .rel[a].dyn. - if (ctx.sym.relaIpltStart && ctx.mainPart->relaDyn->isNeeded()) { - ctx.sym.relaIpltStart->section = ctx.mainPart->relaDyn.get(); - ctx.sym.relaIpltEnd->section = ctx.mainPart->relaDyn.get(); - ctx.sym.relaIpltEnd->value = ctx.mainPart->relaDyn->getSize(); + // .rela_iplt_{start,end} mark the start and the end of the section containing + // IRELATIVE relocations. + if (ctx.sym.relaIpltStart) { + auto &dyn = getIRelativeSection(ctx); + if (dyn.isNeeded()) { + ctx.sym.relaIpltStart->section = &dyn; + ctx.sym.relaIpltEnd->section = &dyn; + ctx.sym.relaIpltEnd->value = dyn.getSize(); + } } PhdrEntry *last = nullptr; diff --git a/lld/test/ELF/pack-dyn-relocs-ifunc-static.s b/lld/test/ELF/pack-dyn-relocs-ifunc-static.s new file mode 100644 index 0000000000000..0f86a6db785b6 --- /dev/null +++ b/lld/test/ELF/pack-dyn-relocs-ifunc-static.s @@ -0,0 +1,26 @@ +# REQUIRES: aarch64 +## __rela_iplt_start and __rela_iplt_end must surround the IRELATIVE relocation +## list even if moved to .rel[a].plt because of packed relocation sections. + +# RUN: llvm-mc -filetype=obj -triple=aarch64-linux-android %s -o %t.o +# RUN: ld.lld --pack-dyn-relocs=android %t.o -o %t +# RUN: llvm-readelf -sS %t | FileCheck %s + +# CHECK: .rela.plt RELA 0000000000200158 000158 000018 18 AI 0 5 8 +# CHECK: 0000000000200158 0 NOTYPE LOCAL HIDDEN 1 __rela_iplt_start +# CHECK: 0000000000200170 0 NOTYPE LOCAL HIDDEN 1 __rela_iplt_end + +.text +.type foo, %gnu_indirect_function +.globl foo +foo: + ret + +.globl _start +_start: + bl foo + +.data +.balign 8 +.quad __rela_iplt_start +.quad __rela_iplt_end From ade32a0d39ef4158a10edd875d075010ad1c864c Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Tue, 3 Dec 2024 19:20:09 -0800 Subject: [PATCH 2/3] Format Created using spr 1.3.6-beta.1 --- lld/ELF/Relocations.cpp | 3 ++- lld/ELF/Writer.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index 204599c544bc4..2268121827f64 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -1694,7 +1694,8 @@ RelocationBaseSection &elf::getIRelativeSection(Ctx &ctx) { // unrelocated globals with RELR relocations when // --pack-relative-relocs=android+relr is enabled. Work around this by placing // IRELATIVE in .rela.plt. - return ctx.arg.androidPackDynRelocs ? *ctx.in.relaPlt : *ctx.mainPart->relaDyn; + return ctx.arg.androidPackDynRelocs ? *ctx.in.relaPlt + : *ctx.mainPart->relaDyn; } static bool handleNonPreemptibleIfunc(Ctx &ctx, Symbol &sym, uint16_t flags) { diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index aa7c36bc9ac13..49616fa03e63c 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -847,7 +847,7 @@ template void Writer::setReservedSymbolSections() { // .rela_iplt_{start,end} mark the start and the end of the section containing // IRELATIVE relocations. if (ctx.sym.relaIpltStart) { - auto &dyn = getIRelativeSection(ctx); + auto &dyn = getIRelativeSection(ctx); if (dyn.isNeeded()) { ctx.sym.relaIpltStart->section = &dyn; ctx.sym.relaIpltEnd->section = &dyn; From 63a6d17f5f57e8e09a1b13d5b9117b6dc31894ad Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Wed, 4 Dec 2024 17:19:38 -0800 Subject: [PATCH 3/3] Move test to pack-dyn-relocs-ifunc.s Created using spr 1.3.6-beta.1 --- lld/test/ELF/pack-dyn-relocs-ifunc-static.s | 26 --------------------- lld/test/ELF/pack-dyn-relocs-ifunc.s | 25 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 26 deletions(-) delete mode 100644 lld/test/ELF/pack-dyn-relocs-ifunc-static.s diff --git a/lld/test/ELF/pack-dyn-relocs-ifunc-static.s b/lld/test/ELF/pack-dyn-relocs-ifunc-static.s deleted file mode 100644 index 0f86a6db785b6..0000000000000 --- a/lld/test/ELF/pack-dyn-relocs-ifunc-static.s +++ /dev/null @@ -1,26 +0,0 @@ -# REQUIRES: aarch64 -## __rela_iplt_start and __rela_iplt_end must surround the IRELATIVE relocation -## list even if moved to .rel[a].plt because of packed relocation sections. - -# RUN: llvm-mc -filetype=obj -triple=aarch64-linux-android %s -o %t.o -# RUN: ld.lld --pack-dyn-relocs=android %t.o -o %t -# RUN: llvm-readelf -sS %t | FileCheck %s - -# CHECK: .rela.plt RELA 0000000000200158 000158 000018 18 AI 0 5 8 -# CHECK: 0000000000200158 0 NOTYPE LOCAL HIDDEN 1 __rela_iplt_start -# CHECK: 0000000000200170 0 NOTYPE LOCAL HIDDEN 1 __rela_iplt_end - -.text -.type foo, %gnu_indirect_function -.globl foo -foo: - ret - -.globl _start -_start: - bl foo - -.data -.balign 8 -.quad __rela_iplt_start -.quad __rela_iplt_end diff --git a/lld/test/ELF/pack-dyn-relocs-ifunc.s b/lld/test/ELF/pack-dyn-relocs-ifunc.s index 6168d06f99d9e..b6000df3d6160 100644 --- a/lld/test/ELF/pack-dyn-relocs-ifunc.s +++ b/lld/test/ELF/pack-dyn-relocs-ifunc.s @@ -47,3 +47,28 @@ _start: .globl bar bar: ret + +#--- c.s + +# RUN: llvm-mc -filetype=obj -triple=aarch64-linux-android c.s -o c.o +# RUN: ld.lld --pack-dyn-relocs=android c.o -o c +# RUN: llvm-readelf -sS c | FileCheck --check-prefix=STATIC %s + +# STATIC: .rela.plt RELA 0000000000200158 000158 000018 18 AI 0 5 8 +# STATIC: 0000000000200158 0 NOTYPE LOCAL HIDDEN 1 __rela_iplt_start +# STATIC: 0000000000200170 0 NOTYPE LOCAL HIDDEN 1 __rela_iplt_end + +.text +.type foo, %gnu_indirect_function +.globl foo +foo: + ret + +.globl _start +_start: + bl foo + +.data +.balign 8 +.quad __rela_iplt_start +.quad __rela_iplt_end