[llvm-objcopy] Replace the size() helper with SectionTableRef::size

Summary:
BTW, STLExtras.h provides llvm::size() which is similar to std::size()
for random access iterators. However, if we prefer qualified
llvm::size(), the member function .size() will be more convenient.

Reviewers: jhenderson, jakehehrlich, rupprecht, grimar, alexshap, espindola

Reviewed By: grimar

Subscribers: emaste, arichardson, jdoerfert, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D60028

llvm-svn: 357347
This commit is contained in:
Fangrui Song 2019-03-30 14:08:59 +00:00
parent 275a81e7ac
commit 45420320ae
2 changed files with 6 additions and 10 deletions

View File

@ -1221,11 +1221,6 @@ template <class ELFT> void ELFBuilder<ELFT>::build() {
" in elf header " + " is not a string table"); " in elf header " + " is not a string table");
} }
// A generic size function which computes sizes of any random access range.
template <class R> size_t size(R &&Range) {
return static_cast<size_t>(std::end(Range) - std::begin(Range));
}
Writer::~Writer() {} Writer::~Writer() {}
Reader::~Reader() {} Reader::~Reader() {}
@ -1282,7 +1277,7 @@ template <class ELFT> void ELFWriter<ELFT>::writeEhdr() {
Ehdr.e_phentsize = (Ehdr.e_phnum != 0) ? sizeof(Elf_Phdr) : 0; Ehdr.e_phentsize = (Ehdr.e_phnum != 0) ? sizeof(Elf_Phdr) : 0;
Ehdr.e_flags = Obj.Flags; Ehdr.e_flags = Obj.Flags;
Ehdr.e_ehsize = sizeof(Elf_Ehdr); Ehdr.e_ehsize = sizeof(Elf_Ehdr);
if (WriteSectionHeaders && size(Obj.sections()) != 0) { if (WriteSectionHeaders && Obj.sections().size() != 0) {
Ehdr.e_shentsize = sizeof(Elf_Shdr); Ehdr.e_shentsize = sizeof(Elf_Shdr);
Ehdr.e_shoff = Obj.SHOffset; Ehdr.e_shoff = Obj.SHOffset;
// """ // """
@ -1291,7 +1286,7 @@ template <class ELFT> void ELFWriter<ELFT>::writeEhdr() {
// number of section header table entries is contained in the sh_size field // number of section header table entries is contained in the sh_size field
// of the section header at index 0. // of the section header at index 0.
// """ // """
auto Shnum = size(Obj.sections()) + 1; auto Shnum = Obj.sections().size() + 1;
if (Shnum >= SHN_LORESERVE) if (Shnum >= SHN_LORESERVE)
Ehdr.e_shnum = 0; Ehdr.e_shnum = 0;
else else
@ -1330,7 +1325,7 @@ template <class ELFT> void ELFWriter<ELFT>::writeShdrs() {
Shdr.sh_addr = 0; Shdr.sh_addr = 0;
Shdr.sh_offset = 0; Shdr.sh_offset = 0;
// See writeEhdr for why we do this. // See writeEhdr for why we do this.
uint64_t Shnum = size(Obj.sections()) + 1; uint64_t Shnum = Obj.sections().size() + 1;
if (Shnum >= SHN_LORESERVE) if (Shnum >= SHN_LORESERVE)
Shdr.sh_size = Shnum; Shdr.sh_size = Shnum;
else else
@ -1564,7 +1559,7 @@ template <class ELFT> size_t ELFWriter<ELFT>::totalSize() const {
// We already have the section header offset so we can calculate the total // We already have the section header offset so we can calculate the total
// size by just adding up the size of each section header. // size by just adding up the size of each section header.
auto NullSectionSize = WriteSectionHeaders ? sizeof(Elf_Shdr) : 0; auto NullSectionSize = WriteSectionHeaders ? sizeof(Elf_Shdr) : 0;
return Obj.SHOffset + size(Obj.sections()) * sizeof(Elf_Shdr) + return Obj.SHOffset + Obj.sections().size() * sizeof(Elf_Shdr) +
NullSectionSize; NullSectionSize;
} }
@ -1595,7 +1590,7 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() {
// if we need large indexes or not. We can assign indexes first and check as // if we need large indexes or not. We can assign indexes first and check as
// we go to see if we will actully need large indexes. // we go to see if we will actully need large indexes.
bool NeedsLargeIndexes = false; bool NeedsLargeIndexes = false;
if (size(Obj.sections()) >= SHN_LORESERVE) { if (Obj.sections().size() >= SHN_LORESERVE) {
auto Sections = Obj.sections(); auto Sections = Obj.sections();
NeedsLargeIndexes = NeedsLargeIndexes =
std::any_of(Sections.begin() + SHN_LORESERVE, Sections.end(), std::any_of(Sections.begin() + SHN_LORESERVE, Sections.end(),

View File

@ -59,6 +59,7 @@ public:
iterator begin() { return iterator(Sections.data()); } iterator begin() { return iterator(Sections.data()); }
iterator end() { return iterator(Sections.data() + Sections.size()); } iterator end() { return iterator(Sections.data() + Sections.size()); }
size_t size() const { return Sections.size(); }
SectionBase *getSection(uint32_t Index, Twine ErrMsg); SectionBase *getSection(uint32_t Index, Twine ErrMsg);