Skip to content

Commit

Permalink
[ELF] Set SHF_INFO_LINK for .rel[a].plt and .rel[a].dyn
Browse files Browse the repository at this point in the history
The ELF spec says

> If the sh_flags field for this section header includes the attribute SHF_INFO_LINK, then this member represents a section header table index.

Set SHF_INFO_LINK so that binary manipulation tools know that sh_info is
a section header table index instead of (the number of local symbols in the case of SHT_SYMTAB/SHT_DYNSYM).
We have already added SHF_INFO_LINK for --emit-relocs retained SHT_REL[A].

For example, we can teach llvm-objcopy to preserve the section index of the sh_info referenced section if
SHF_INFO_LINK is set. (GNU objcopy recognizes .rel[a].plt and updates
sh_info even if SHF_INFO_LINK is not set).

Reviewed By: grimar, psmith

Differential Revision: https://reviews.llvm.org/D89828
  • Loading branch information
MaskRay committed Oct 22, 2020
1 parent 5dc7033 commit a8f9f08
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lld/ELF/SyntheticSections.cpp
Expand Up @@ -1610,10 +1610,14 @@ void RelocationBaseSection::finalizeContents() {
else
getParent()->link = 0;

if (in.relaPlt == this)
if (in.relaPlt == this) {
getParent()->flags |= ELF::SHF_INFO_LINK;
getParent()->info = in.gotPlt->getParent()->sectionIndex;
if (in.relaIplt == this)
}
if (in.relaIplt == this) {
getParent()->flags |= ELF::SHF_INFO_LINK;
getParent()->info = in.igotPlt->getParent()->sectionIndex;
}
}

RelrBaseSection::RelrBaseSection()
Expand Down
1 change: 1 addition & 0 deletions lld/test/ELF/aarch64-gnu-ifunc.s
Expand Up @@ -11,6 +11,7 @@
// CHECK-NEXT: Type: SHT_RELA
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_INFO_LINK
// CHECK-NEXT: ]
// CHECK-NEXT: Address: [[RELA:.*]]
// CHECK-NEXT: Offset: 0x158
Expand Down
1 change: 1 addition & 0 deletions lld/test/ELF/arm-combined-dynrel-ifunc.s
Expand Up @@ -40,6 +40,7 @@ main:
// CHECK-NEXT: Type: SHT_REL
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_INFO_LINK
// CHECK-NEXT: ]
// CHECK-NEXT: Address:
// CHECK-NEXT: Offset:
Expand Down
1 change: 1 addition & 0 deletions lld/test/ELF/arm-gnu-ifunc.s
Expand Up @@ -31,6 +31,7 @@ _start:
// CHECK-NEXT: Type: SHT_REL
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_INFO_LINK
// CHECK-NEXT: ]
// CHECK-NEXT: Address: 0x100F4
// CHECK-NEXT: Offset: 0xF4
Expand Down
1 change: 1 addition & 0 deletions lld/test/ELF/dynamic-reloc.s
Expand Up @@ -13,6 +13,7 @@
// CHECK-NEXT: Type: SHT_RELA
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_INFO_LINK
// CHECK-NEXT: ]
// CHECK-NEXT: Address: [[RELAADDR:.*]]
// CHECK-NEXT: Offset:
Expand Down
1 change: 1 addition & 0 deletions lld/test/ELF/gnu-ifunc-i386.s
Expand Up @@ -11,6 +11,7 @@
// CHECK-NEXT: Type: SHT_REL
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_INFO_LINK
// CHECK-NEXT: ]
// CHECK-NEXT: Address: [[RELA:.*]]
// CHECK-NEXT: Offset: 0xD4
Expand Down
1 change: 1 addition & 0 deletions lld/test/ELF/gnu-ifunc.s
Expand Up @@ -11,6 +11,7 @@
// CHECK-NEXT: Type: SHT_RELA
// CHECK-NEXT: Flags [
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_INFO_LINK
// CHECK-NEXT: ]
// CHECK-NEXT: Address: [[RELA:.*]]
// CHECK-NEXT: Offset: 0x158
Expand Down
1 change: 1 addition & 0 deletions lld/test/ELF/x86-64-combined-dynrel.s
Expand Up @@ -31,6 +31,7 @@ _start:
# CHECK-NEXT: Type: SHT_RELA
# CHECK-NEXT: Flags [
# CHECK-NEXT: SHF_ALLOC
# CHECK-NEXT: SHF_INFO_LINK
# CHECK-NEXT: ]
# CHECK-NEXT: Address:
# CHECK-NEXT: Offset:
Expand Down

0 comments on commit a8f9f08

Please sign in to comment.