[ELF][Writer] Add dynamic table.

llvm-svn: 175654
This commit is contained in:
Michael J. Spencer 2013-02-20 19:46:12 +00:00
parent 294c5e1cc3
commit b71ce655a8
6 changed files with 71 additions and 2 deletions

View File

@ -49,6 +49,12 @@ public:
return false;
}
/// \brief Does the output have dynamic sections.
bool isDynamic() const {
return _options._outputKind == OutputKind::DynamicExecutable ||
_options._outputKind == OutputKind::Shared;
}
virtual ErrorOr<Reader &> getReader(const LinkerInput &input) const;
virtual ErrorOr<Writer &> getWriter() const;

View File

@ -182,7 +182,8 @@ bool ProgramHeader<ELFT>::addSegment(Segment<ELFT> *segment) {
bool allocatedNew = false;
for (auto slice : segment->slices()) {
// If we have a TLS segment, emit a LOAD first.
if (segment->segmentType() == llvm::ELF::PT_TLS) {
if (segment->segmentType() == llvm::ELF::PT_TLS ||
segment->segmentType() == llvm::ELF::PT_DYNAMIC) {
auto phdr = allocateProgramHeader();
if (phdr.second)
allocatedNew = true;
@ -193,7 +194,7 @@ bool ProgramHeader<ELFT>::addSegment(Segment<ELFT> *segment) {
phdr.first->p_filesz = slice->fileSize();
phdr.first->p_memsz = slice->memSize();
phdr.first->p_flags = segment->flags();
phdr.first->p_align = slice->align2();
phdr.first->p_align = segment->pageSize();
}
auto phdr = allocateProgramHeader();
if (phdr.second)

View File

@ -716,6 +716,48 @@ public:
private:
std::vector<std::pair<const DefinedAtom *, const Reference *>> _relocs;
};
template <class ELFT> class DynamicTable : public Section<ELFT> {
typedef llvm::object::Elf_Dyn_Impl<ELFT> Elf_Dyn;
typedef std::vector<Elf_Dyn> EntriesT;
public:
DynamicTable(const ELFTargetInfo &ti, StringRef str, int32_t order)
: Section<ELFT>(ti, str) {
this->setOrder(order);
this->_entSize = sizeof(Elf_Dyn);
this->_align2 = llvm::alignOf<Elf_Dyn>();
// Reserve space for the DT_NULL entry.
this->_fsize = sizeof(Elf_Dyn);
this->_msize = sizeof(Elf_Dyn);
this->_type = SHT_DYNAMIC;
this->_flags = SHF_ALLOC;
}
range<typename EntriesT::iterator> entries() { return _entries; }
/// \returns the index of the entry.
std::size_t addEntry(Elf_Dyn e) {
_entries.push_back(e);
this->_fsize = (_entries.size() * sizeof(Elf_Dyn)) + sizeof(Elf_Dyn);
this->_msize = this->_fsize;
return _entries.size() - 1;
}
void write(ELFWriter *writer, llvm::FileOutputBuffer &buffer) {
uint8_t *chunkBuffer = buffer.getBufferStart();
uint8_t *dest = chunkBuffer + this->fileOffset();
// Add the null entry.
Elf_Dyn d;
d.d_tag = 0;
d.d_un.d_val = 0;
_entries.push_back(d);
std::memcpy(dest, _entries.data(), this->_fsize);
}
private:
EntriesT _entries;
};
} // end namespace elf
} // end namespace lld

View File

@ -66,6 +66,10 @@ private:
LLD_UNIQUE_BUMP_PTR(StringTable<ELFT>) _strtab;
LLD_UNIQUE_BUMP_PTR(StringTable<ELFT>) _shstrtab;
LLD_UNIQUE_BUMP_PTR(SectionHeader<ELFT>) _shdrtab;
/// \name Dynamic sections.
/// @{
LLD_UNIQUE_BUMP_PTR(DynamicTable<ELFT>) _dynamicTable;
/// @}
CRuntimeFile<ELFT> _runtimeFile;
};
@ -330,6 +334,12 @@ void ExecutableWriter<ELFT>::createDefaultSections() {
_symtab->setStringSection(_strtab.get());
_layout->addSection(_shdrtab.get());
if (_targetInfo.isDynamic()) {
_dynamicTable.reset(new (_alloc) DynamicTable<ELFT>(
_targetInfo, ".dynamic", DefaultLayout<ELFT>::ORDER_DYNAMIC));
_layout->addSection(_dynamicTable.get());
}
// give a chance for the target to add sections
_targetHandler.createDefaultSections();
}

View File

@ -107,6 +107,7 @@ ErrorOr<void> X86_64TargetRelocationHandler::applyRelocation(
break;
}
// Runtime only relocations. Ignore here.
case R_X86_64_RELATIVE:
case R_X86_64_IRELATIVE:
break;

View File

@ -0,0 +1,9 @@
RUN: lld -core -target x86_64-linux %p/Inputs/use-shared.x86-64 \
RUN: %p/Inputs/shared.so-x86-64 -output=%t -entry=main \
RUN: -output-type=dynamic
RUN: llvm-readobj %t | FileCheck %s
CHECK: Dynamic section contains 1 entries
CHECK: Tag Type Name/Value
CHECK: 0x0000000000000000 (NULL) 0x0
CHECK: Total: 1