[ELF] Emit DT_TEXTREL dynamic table flag.

If one or more dynamic relocation might modify a read-only section,
dynamic table should contain DT_TEXTREL tag.

The patch introduces new `RelocationTable::canModifyReadonlySection()`
method. This method checks through the relocations to see if any modifies
a read-only section. The DynamicTable class calls this method and emits
the DT_TEXTREL tag if necessary.

The patch reviewed by Rui Ueyama and Shankar Easwaran.

llvm-svn: 208670
This commit is contained in:
Simon Atanasyan 2014-05-13 07:37:09 +00:00
parent 97e5d98779
commit 35687a2fd3
2 changed files with 63 additions and 0 deletions

View File

@ -954,6 +954,16 @@ public:
_symbolTable = symbolTable;
}
/// \brief Check if any relocation modifies a read-only section.
bool canModifyReadonlySection() const {
for (const auto &rel : _relocs) {
const DefinedAtom *atom = rel.first;
if ((atom->permissions() & DefinedAtom::permRW_) != DefinedAtom::permRW_)
return true;
}
return false;
}
virtual void finalize() {
this->_link = _symbolTable ? _symbolTable->ordinal() : 0;
if (this->_parent)
@ -1080,6 +1090,11 @@ public:
_dt_relasz = addEntry(dyn);
dyn.d_tag = isRela ? DT_RELAENT : DT_RELENT;
_dt_relaent = addEntry(dyn);
if (_layout.getDynamicRelocationTable()->canModifyReadonlySection()) {
dyn.d_tag = DT_TEXTREL;
_dt_textrel = addEntry(dyn);
}
}
if (_layout.hasPLTRelocationTable()) {
dyn.d_tag = DT_PLTRELSZ;
@ -1165,6 +1180,7 @@ private:
std::size_t _dt_jmprel;
std::size_t _dt_fini_array;
std::size_t _dt_fini_arraysz;
std::size_t _dt_textrel;
TargetLayout<ELFT> &_layout;
DynamicSymbolTable<ELFT> *_dynamicSymbolTable;
HashSection<ELFT> *_hashTable;

View File

@ -0,0 +1,47 @@
# Check that if a dynamic relocation modify a read-only section,
# .dynamic section contains the DT_TEXTREL tag.
# RUN: yaml2obj -format=elf %S/Inputs/pic-obj.yaml > %t-so-obj
# RUN: lld -flavor gnu -target mipsel -shared -o %t-so %t-so-obj
# RUN: yaml2obj -format=elf %s > %t-obj
# RUN: lld -flavor gnu -target mipsel -e T0 -o %t-exe %t-obj %t-so
# RUN: llvm-readobj -dynamic-table %t-exe | FileCheck %s
# CHECK: 0x{{[0-9A-F]+}} TEXTREL
FileHeader:
Class: ELFCLASS32
Data: ELFDATA2LSB
Type: ET_REL
Machine: EM_MIPS
Flags: [ EF_MIPS_NOREORDER, EF_MIPS_PIC, EF_MIPS_CPIC,
EF_MIPS_ABI_O32, EF_MIPS_ARCH_32 ]
Sections:
- Name: .text
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
AddressAlign: 0x04
Content: '00000000'
- Name: .rel.text
Type: SHT_REL
Link: .symtab
Info: .text
AddressAlign: 0x04
Relocations:
- Offset: 0
Symbol: T1
Type: R_MIPS_32
Symbols:
Local:
- Name: .text
Type: STT_SECTION
Section: .text
Global:
- Name: T0
Type: STT_FUNC
Section: .text
Size: 0x04
- Name: T1