From bf77be3ba57a5f915d344f2651852fc2b30356b7 Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Tue, 29 Jan 2013 01:07:47 +0000 Subject: [PATCH] [ELF] Give Chunk a ELFTargetInfo. llvm-svn: 173743 --- lld/lib/ReaderWriter/ELF/DefaultELFLayout.h | 8 ++-- lld/lib/ReaderWriter/ELF/ELFChunk.h | 15 ++---- lld/lib/ReaderWriter/ELF/ELFHeaderChunks.h | 20 ++++---- lld/lib/ReaderWriter/ELF/ELFSectionChunks.h | 53 ++++++++------------- lld/lib/ReaderWriter/ELF/ELFSegmentChunks.h | 30 +++++------- lld/lib/ReaderWriter/ELF/WriterELF.cpp | 18 +++---- 6 files changed, 59 insertions(+), 85 deletions(-) diff --git a/lld/lib/ReaderWriter/ELF/DefaultELFLayout.h b/lld/lib/ReaderWriter/ELF/DefaultELFLayout.h index f7525322cabd..1a73aaa0515f 100644 --- a/lld/lib/ReaderWriter/ELF/DefaultELFLayout.h +++ b/lld/lib/ReaderWriter/ELF/DefaultELFLayout.h @@ -406,8 +406,8 @@ DefaultELFLayout::addAtom(const Atom *atom) { if (_sectionMap.find(sectionKey) == _sectionMap.end()) { SectionOrder section_order = getSectionOrder(sectionName, contentType, permissions); - section = new (_allocator.Allocate >()) - Section(sectionName, contentType, permissions, section_order); + section = new (_allocator) Section( + _targetInfo, sectionName, contentType, permissions, section_order); section->setOrder(section_order); _sections.push_back(section); _sectionMap.insert(std::make_pair(sectionKey, section)); @@ -487,8 +487,8 @@ DefaultELFLayout::assignSectionsToSegments() { if (!segmentInsert.second) { segment = segmentInsert.first->second; } else { - segment = new (_allocator.Allocate>()) Segment( - segmentName, getSegmentType(section), _targetInfo); + segment = new (_allocator) + Segment(_targetInfo, segmentName, getSegmentType(section)); segmentInsert.first->second = segment; _segments.push_back(segment); } diff --git a/lld/lib/ReaderWriter/ELF/ELFChunk.h b/lld/lib/ReaderWriter/ELF/ELFChunk.h index 5045a91a1ee4..771646aa3f6a 100644 --- a/lld/lib/ReaderWriter/ELF/ELFChunk.h +++ b/lld/lib/ReaderWriter/ELF/ELFChunk.h @@ -37,16 +37,10 @@ public: K_ELFSection, // Section K_ELFSectionHeader // Section header }; - Chunk(llvm::StringRef name, Kind kind) - : _name(name) - , _kind(kind) - , _fsize(0) - , _msize(0) - , _align2(0) - , _order(0) - , _ordinal(1) - , _start(0) - , _fileoffset(0) {} + Chunk(llvm::StringRef name, Kind kind, const ELFTargetInfo &ti) + : _name(name), _kind(kind), _fsize(0), _msize(0), _align2(0), _order(0), + _ordinal(1), _start(0), _fileoffset(0), _targetInfo(ti) { + } virtual ~Chunk() {} // Does the chunk occupy disk space virtual bool occupiesNoDiskSpace() const { @@ -90,6 +84,7 @@ protected: uint64_t _ordinal; uint64_t _start; uint64_t _fileoffset; + const ELFTargetInfo &_targetInfo; }; } // elf diff --git a/lld/lib/ReaderWriter/ELF/ELFHeaderChunks.h b/lld/lib/ReaderWriter/ELF/ELFHeaderChunks.h index f93e398e5e5e..a749055c44a3 100644 --- a/lld/lib/ReaderWriter/ELF/ELFHeaderChunks.h +++ b/lld/lib/ReaderWriter/ELF/ELFHeaderChunks.h @@ -30,7 +30,7 @@ class ELFHeader : public Chunk { public: typedef llvm::object::Elf_Ehdr_Impl Elf_Ehdr; - ELFHeader(); + ELFHeader(const ELFTargetInfo &); void e_ident(int I, unsigned char C) { _eh.e_ident[I] = C; } void e_type(uint16_t type) { _eh.e_type = type; } @@ -60,9 +60,9 @@ private: Elf_Ehdr _eh; }; -template -ELFHeader::ELFHeader() -: Chunk("elfhdr", Chunk::K_ELFHeader) { +template +ELFHeader::ELFHeader(const ELFTargetInfo &ti) + : Chunk("elfhdr", Chunk::K_ELFHeader, ti) { this->_align2 = ELFT::Is64Bits ? 8 : 4; this->_fsize = sizeof(Elf_Ehdr); this->_msize = sizeof(Elf_Ehdr); @@ -111,8 +111,8 @@ public: uint64_t _flagsClear; }; - ELFProgramHeader() - : Chunk("elfphdr", Chunk::K_ELFProgramHeader) { + ELFProgramHeader(const ELFTargetInfo &ti) + : Chunk("elfphdr", Chunk::K_ELFProgramHeader, ti) { this->_align2 = ELFT::Is64Bits ? 8 : 4; resetProgramHeaders(); } @@ -213,7 +213,7 @@ class ELFSectionHeader : public Chunk { public: typedef llvm::object::Elf_Shdr_Impl Elf_Shdr; - ELFSectionHeader(int32_t order); + ELFSectionHeader(const ELFTargetInfo &, int32_t order); void appendSection(MergedSections *section); @@ -249,9 +249,9 @@ private: llvm::BumpPtrAllocator _sectionAllocate; }; -template -ELFSectionHeader::ELFSectionHeader(int32_t order) - : Chunk("shdr", Chunk::K_ELFSectionHeader) { +template +ELFSectionHeader::ELFSectionHeader(const ELFTargetInfo &ti, int32_t order) + : Chunk("shdr", Chunk::K_ELFSectionHeader, ti) { this->_fsize = 0; this->_align2 = 8; this->setOrder(order); diff --git a/lld/lib/ReaderWriter/ELF/ELFSectionChunks.h b/lld/lib/ReaderWriter/ELF/ELFSectionChunks.h index 8318787abb00..2dea2c6850cf 100644 --- a/lld/lib/ReaderWriter/ELF/ELFSectionChunks.h +++ b/lld/lib/ReaderWriter/ELF/ELFSectionChunks.h @@ -46,11 +46,9 @@ public: }; // Create a section object, the section is set to the default type if the // caller doesnot set it - Section(const llvm::StringRef sectionName, - const int32_t contentType, - const int32_t contentPermissions, - const int32_t order, - const SectionKind kind = K_Default); + Section(const ELFTargetInfo &, const llvm::StringRef sectionName, + const int32_t contentType, const int32_t contentPermissions, + const int32_t order, const SectionKind kind = K_Default); /// return the section kind inline SectionKind sectionKind() const { @@ -179,19 +177,14 @@ protected: // Create a section object, the section is set to the default type if the // caller doesnot set it -template -Section::Section(const StringRef sectionName, +template +Section::Section(const ELFTargetInfo &ti, const StringRef sectionName, const int32_t contentType, - const int32_t contentPermissions, - const int32_t order, + const int32_t contentPermissions, const int32_t order, const SectionKind kind) - : Chunk(sectionName, Chunk::K_ELFSection) - , _contentType(contentType) - , _contentPermissions(contentPermissions) - , _sectionKind(kind) - , _entSize(0) - , _shInfo(0) - , _link(0) { + : Chunk(sectionName, Chunk::K_ELFSection, ti), + _contentType(contentType), _contentPermissions(contentPermissions), + _sectionKind(kind), _entSize(0), _shInfo(0), _link(0) { this->setOrder(order); } @@ -498,7 +491,7 @@ MergedSections::appendSection(Chunk *c) { template class ELFStringTable : public Section { public: - ELFStringTable(const char *str, int32_t order); + ELFStringTable(const ELFTargetInfo &, const char *str, int32_t order); static inline bool classof(const Chunk *c) { return c->kind() == Section::K_StringTable; @@ -514,15 +507,11 @@ private: std::vector _strings; }; -template -ELFStringTable::ELFStringTable(const char *str, +template +ELFStringTable::ELFStringTable(const ELFTargetInfo &ti, const char *str, int32_t order) - : Section( - str, - llvm::ELF::SHT_STRTAB, - DefinedAtom::perm___, - order, - Section::K_StringTable) { + : Section(ti, str, llvm::ELF::SHT_STRTAB, DefinedAtom::perm___, order, + Section::K_StringTable) { // the string table has a NULL entry for which // add an empty string _strings.push_back(""); @@ -559,7 +548,7 @@ class ELFSymbolTable : public Section { public: typedef llvm::object::Elf_Sym_Impl Elf_Sym; - ELFSymbolTable(const char *str, int32_t order); + ELFSymbolTable(const ELFTargetInfo &ti, const char *str, int32_t order); void addSymbol(const Atom *atom, int32_t sectionIndex, uint64_t addr = 0); @@ -583,15 +572,11 @@ private: }; /// ELF Symbol Table -template -ELFSymbolTable::ELFSymbolTable(const char *str, +template +ELFSymbolTable::ELFSymbolTable(const ELFTargetInfo &ti, const char *str, int32_t order) - : Section( - str, - llvm::ELF::SHT_SYMTAB, - 0, - order, - Section::K_SymbolTable) { + : Section(ti, str, llvm::ELF::SHT_SYMTAB, 0, order, + Section::K_SymbolTable) { this->setOrder(order); Elf_Sym *symbol = new (_symbolAllocate.Allocate()) Elf_Sym; memset((void *)symbol, 0, sizeof(Elf_Sym)); diff --git a/lld/lib/ReaderWriter/ELF/ELFSegmentChunks.h b/lld/lib/ReaderWriter/ELF/ELFSegmentChunks.h index 75ad7019dfe6..611af34627ee 100644 --- a/lld/lib/ReaderWriter/ELF/ELFSegmentChunks.h +++ b/lld/lib/ReaderWriter/ELF/ELFSegmentChunks.h @@ -111,9 +111,8 @@ public: typedef typename std::vector *>::iterator SliceIter; typedef typename std::vector *>::iterator SectionIter; - Segment(const StringRef name, - const ELFLayout::SegmentType type, - const ELFTargetInfo &ti); + Segment(const ELFTargetInfo &ti, const StringRef name, + const ELFLayout::SegmentType type); /// append a section to a segment void append(Section *section); @@ -168,7 +167,7 @@ public: inline ELFLayout::SegmentType segmentType() { return _segmentType; } - inline int pageSize() const { return _targetInfo.getPageSize(); } + inline int pageSize() const { return this->_targetInfo.getPageSize(); } inline int64_t atomflags() const { return _atomflags; } @@ -195,19 +194,14 @@ protected: ELFLayout::SegmentType _segmentType; int64_t _flags; int64_t _atomflags; - const ELFTargetInfo &_targetInfo; llvm::BumpPtrAllocator _segmentAllocate; }; -template -Segment::Segment(const StringRef name, - const ELFLayout::SegmentType type, - const ELFTargetInfo &ti) - : Chunk(name, Chunk::K_ELFSegment) - , _segmentType(type) - , _flags(0) - , _atomflags(0) - , _targetInfo(ti) { +template +Segment::Segment(const ELFTargetInfo &ti, const StringRef name, + const ELFLayout::SegmentType type) + : Chunk(name, Chunk::K_ELFSegment, ti), _segmentType(type), + _flags(0), _atomflags(0) { this->_align2 = 0; this->_fsize = 0; } @@ -268,7 +262,7 @@ Segment::assignOffsets(uint64_t startOffset) { SegmentSlice *slice = nullptr; // If the newOffset computed is more than a page away, lets create // a seperate segment, so that memory is not used up while running - if ((newOffset - curOffset) > _targetInfo.getPageSize()) { + if ((newOffset - curOffset) > this->_targetInfo.getPageSize()) { // TODO: use std::find here for (auto s : slices()) { if (s->startSection() == startSection) { @@ -285,8 +279,8 @@ Segment::assignOffsets(uint64_t startOffset) { slice->setSections(make_range(startSectionIter, endSectionIter)); slice->setSize(curSliceSize); slice->setAlign(sliceAlign); - uint64_t newPageOffset = - llvm::RoundUpToAlignment(curOffset, _targetInfo.getPageSize()); + uint64_t newPageOffset = llvm::RoundUpToAlignment( + curOffset, this->_targetInfo.getPageSize()); newOffset = llvm::RoundUpToAlignment(newPageOffset, (*si)->align2()); curSliceFileOffset = newOffset; startSectionIter = endSectionIter; @@ -332,7 +326,7 @@ void Segment::assignVirtualAddress(uint64_t &addr) { for (auto slice : slices()) { // Align to a page - addr = llvm::RoundUpToAlignment(addr, _targetInfo.getPageSize()); + addr = llvm::RoundUpToAlignment(addr, this->_targetInfo.getPageSize()); // Align to the slice alignment addr = llvm::RoundUpToAlignment(addr, slice->align2()); diff --git a/lld/lib/ReaderWriter/ELF/WriterELF.cpp b/lld/lib/ReaderWriter/ELF/WriterELF.cpp index fd7baca08112..d43ebfba7eac 100644 --- a/lld/lib/ReaderWriter/ELF/WriterELF.cpp +++ b/lld/lib/ReaderWriter/ELF/WriterELF.cpp @@ -285,19 +285,19 @@ ELFExecutableWriter::writeFile(const lld::File &file, StringRef path) { template void ELFExecutableWriter::createDefaultSections() { - _elfHeader = new ELFHeader(); - _programHeader = new ELFProgramHeader(); + _elfHeader = new ELFHeader(_targetInfo); + _programHeader = new ELFProgramHeader(_targetInfo); _layout->setELFHeader(_elfHeader); _layout->setProgramHeader(_programHeader); - _symtab = new ELFSymbolTable( - ".symtab", DefaultELFLayout::ORDER_SYMBOL_TABLE); - _strtab = new ELFStringTable( - ".strtab", DefaultELFLayout::ORDER_STRING_TABLE); + _symtab = new ELFSymbolTable< + ELFT>(_targetInfo, ".symtab", DefaultELFLayout::ORDER_SYMBOL_TABLE); + _strtab = new ELFStringTable< + ELFT>(_targetInfo, ".strtab", DefaultELFLayout::ORDER_STRING_TABLE); _shstrtab = new ELFStringTable( - ".shstrtab", DefaultELFLayout::ORDER_SECTION_STRINGS); - _shdrtab = new ELFSectionHeader( - DefaultELFLayout::ORDER_SECTION_HEADERS); + _targetInfo, ".shstrtab", DefaultELFLayout::ORDER_SECTION_STRINGS); + _shdrtab = new ELFSectionHeader< + ELFT>(_targetInfo, DefaultELFLayout::ORDER_SECTION_HEADERS); _layout->addSection(_symtab); _layout->addSection(_strtab); _layout->addSection(_shstrtab);