[ELF] Set SHF_INFO_LINK for .rel[a].plt and .rel[a].dyn

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
This commit is contained in:
Fangrui Song 2020-10-22 09:48:04 -07:00
parent 5dc70332d5
commit a8f9f08018
8 changed files with 13 additions and 2 deletions

View File

@ -1610,11 +1610,15 @@ 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()
: SyntheticSection(SHF_ALLOC,

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -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: