Skip to content

Commit 0a8f1fe

Browse files
committedNov 7, 2016
[ELF] Make InputSection<ELFT>::writeTo virtual
Differential revision: https://reviews.llvm.org/D26281 llvm-svn: 286100
1 parent a5672e0 commit 0a8f1fe

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed
 

‎lld/ELF/InputSection.cpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,6 @@ InputSectionBase<ELFT>::InputSectionBase(elf::ObjectFile<ELFT> *File,
7979
this->Offset = Hdr->sh_offset;
8080
}
8181

82-
template <class ELFT> size_t InputSectionBase<ELFT>::getSize() const {
83-
if (auto *D = dyn_cast<InputSection<ELFT>>(this))
84-
if (D->getThunksSize() > 0)
85-
return D->getThunkOff() + D->getThunksSize();
86-
return Data.size();
87-
}
88-
8982
// Returns a string for an error message.
9083
template <class SectionT> static std::string getName(SectionT *Sec) {
9184
return (Sec->getFile()->getName() + "(" + Sec->Name + ")").str();
@@ -207,6 +200,11 @@ bool InputSection<ELFT>::classof(const InputSectionData *S) {
207200
return S->kind() == Base::Regular;
208201
}
209202

203+
template <class ELFT> size_t InputSection<ELFT>::getSize() const {
204+
return getThunksSize() > 0 ? getThunkOff() + getThunksSize()
205+
: this->Data.size();
206+
}
207+
210208
template <class ELFT>
211209
InputSectionBase<ELFT> *InputSection<ELFT>::getRelocatedSection() {
212210
assert(this->Type == SHT_RELA || this->Type == SHT_REL);

‎lld/ELF/InputSection.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class InputSectionData {
5252
unsigned SectionKind : 3;
5353

5454
public:
55+
virtual ~InputSectionData() = default;
56+
InputSectionData(InputSectionData &&) = default;
5557
Kind kind() const { return (Kind)SectionKind; }
5658

5759
unsigned Live : 1; // for garbage collection
@@ -66,6 +68,9 @@ class InputSectionData {
6668
return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T));
6769
}
6870

71+
virtual void writeTo(uint8_t *Buf) {}
72+
virtual size_t getSize() const { return Data.size(); }
73+
6974
// If a section is compressed, this has the uncompressed section data.
7075
std::unique_ptr<uint8_t[]> UncompressedData;
7176

@@ -113,9 +118,6 @@ template <class ELFT> class InputSectionBase : public InputSectionData {
113118
// this but instead this->Repl.
114119
InputSectionBase<ELFT> *Repl;
115120

116-
// Returns the size of this section (even if this is a common or BSS.)
117-
size_t getSize() const;
118-
119121
static InputSectionBase<ELFT> Discarded;
120122

121123
ObjectFile<ELFT> *getFile() const { return File; }
@@ -243,7 +245,7 @@ template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
243245

244246
// Write this section to a mmap'ed file, assuming Buf is pointing to
245247
// beginning of the output section.
246-
void writeTo(uint8_t *Buf);
248+
void writeTo(uint8_t *Buf) override;
247249

248250
// Relocation sections that refer to this one.
249251
llvm::TinyPtrVector<const Elf_Shdr *> RelocSections;
@@ -270,6 +272,9 @@ template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
270272
// Size of chunk with thunks code.
271273
uint64_t getThunksSize() const;
272274

275+
// Size of section in bytes.
276+
size_t getSize() const override;
277+
273278
template <class RelTy>
274279
void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
275280

‎lld/ELF/SyntheticSections.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ BuildIdSection<ELFT>::BuildIdSection(size_t HashSize)
9595
".note.gnu.build-id"),
9696
HashSize(HashSize) {
9797
this->Live = true;
98+
}
9899

99-
Buf.resize(16 + HashSize);
100+
template <class ELFT> void BuildIdSection<ELFT>::writeTo(uint8_t *Buf) {
100101
const endianness E = ELFT::TargetEndianness;
101-
write32<E>(Buf.data(), 4); // Name size
102-
write32<E>(Buf.data() + 4, HashSize); // Content size
103-
write32<E>(Buf.data() + 8, NT_GNU_BUILD_ID); // Type
104-
memcpy(Buf.data() + 12, "GNU", 4); // Name string
105-
this->Data = ArrayRef<uint8_t>(Buf);
102+
write32<E>(Buf, 4); // Name size
103+
write32<E>(Buf + 4, HashSize); // Content size
104+
write32<E>(Buf + 8, NT_GNU_BUILD_ID); // Type
105+
memcpy(Buf + 12, "GNU", 4); // Name string
106106
}
107107

108108
template <class ELFT>

‎lld/ELF/SyntheticSections.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ template <class ELFT> class InterpSection final : public InputSection<ELFT> {
3030
// .note.gnu.build-id section.
3131
template <class ELFT> class BuildIdSection : public InputSection<ELFT> {
3232
public:
33+
void writeTo(uint8_t *Buf) override;
34+
size_t getSize() const override { return 16 + HashSize; }
3335
virtual void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) = 0;
3436
virtual ~BuildIdSection() = default;
3537

3638
uint8_t *getOutputLoc(uint8_t *Start) const;
3739

3840
protected:
3941
BuildIdSection(size_t HashSize);
40-
std::vector<uint8_t> Buf;
4142

4243
void
4344
computeHash(llvm::MutableArrayRef<uint8_t> Buf,

0 commit comments

Comments
 (0)
Please sign in to comment.