aboutsummaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-objcopy/ELF/Object.cpp
diff options
context:
space:
mode:
authorJames Henderson <james.henderson@sony.com>2021-02-26 13:44:48 +0000
committerJames Henderson <james.henderson@sony.com>2021-03-04 10:23:45 +0000
commit8bb74d16ef04d83f71b1873e7c2e652fb8287b29 (patch)
tree57f47f7f4099596d750bf968f2ace4b32ebab2a8 /llvm/tools/llvm-objcopy/ELF/Object.cpp
parent6e5342a6b0f4172ab6ce49e5d315309ec06b2a27 (diff)
downloadllvm-8bb74d16ef04d83f71b1873e7c2e652fb8287b29.zip
llvm-8bb74d16ef04d83f71b1873e7c2e652fb8287b29.tar.gz
llvm-8bb74d16ef04d83f71b1873e7c2e652fb8287b29.tar.bz2
[llvm-objcopy/strip] Fix off-by-one error in SYMTAB_SHNDX need check
The check for whether an extended symbol index table was required dropped the first SHN_LORESERVE sections from the sections array before checking whether the remaining sections had symbols. Unfortunately, the null section header is not present in this list, so the check was skipping the first section that might be important. If that section contained a symbol, and no subsequent ones did, the .symtab_shndx section would not be emitted, leading to a corrupt object. Also consolidate and expand test coverage in the area to cover this bug and other aspects of the SYMTAB_SHNDX section. Reviewed by: alexshap, MaskRay Differential Revision: https://reviews.llvm.org/D97661
Diffstat (limited to 'llvm/tools/llvm-objcopy/ELF/Object.cpp')
-rw-r--r--llvm/tools/llvm-objcopy/ELF/Object.cpp4
1 files changed, 3 insertions, 1 deletions
diff --git a/llvm/tools/llvm-objcopy/ELF/Object.cpp b/llvm/tools/llvm-objcopy/ELF/Object.cpp
index 7faf415..fe68c51 100644
--- a/llvm/tools/llvm-objcopy/ELF/Object.cpp
+++ b/llvm/tools/llvm-objcopy/ELF/Object.cpp
@@ -2450,8 +2450,10 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() {
bool NeedsLargeIndexes = false;
if (Obj.sections().size() >= SHN_LORESERVE) {
SectionTableRef Sections = Obj.sections();
+ // Sections doesn't include the null section header, so account for this
+ // when skipping the first N sections.
NeedsLargeIndexes =
- any_of(drop_begin(Sections, SHN_LORESERVE),
+ any_of(drop_begin(Sections, SHN_LORESERVE - 1),
[](const SectionBase &Sec) { return Sec.HasSymbol; });
// TODO: handle case where only one section needs the large index table but
// only needs it because the large index table hasn't been removed yet.