From 81c64ddc99ad448408689bea0de2f1d22dd10762 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Tue, 21 Feb 2023 19:41:08 +0100 Subject: Declare more read-only functions const --- src/patchelf.cc | 16 ++++++++-------- src/patchelf.h | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/patchelf.cc b/src/patchelf.cc index c10e369..7bfabb4 100644 --- a/src/patchelf.cc +++ b/src/patchelf.cc @@ -535,7 +535,7 @@ std::string ElfFile::getSectionName(const Elf_Shdr & shdr) co template -Elf_Shdr & ElfFile::findSectionHeader(const SectionName & sectionName) +const Elf_Shdr & ElfFile::findSectionHeader(const SectionName & sectionName) const { auto shdr = tryFindSectionHeader(sectionName); if (!shdr) { @@ -549,7 +549,7 @@ Elf_Shdr & ElfFile::findSectionHeader(const SectionName & sec template -std::optional> ElfFile::tryFindSectionHeader(const SectionName & sectionName) +std::optional> ElfFile::tryFindSectionHeader(const SectionName & sectionName) const { auto i = getSectionIndex(sectionName); if (i) @@ -602,7 +602,7 @@ void ElfFile::writeReplacedSections(Elf_Off & curOff, clobbering previously written new section contents. */ for (auto & i : replacedSections) { const std::string & sectionName = i.first; - Elf_Shdr & shdr = findSectionHeader(sectionName); + const Elf_Shdr & shdr = findSectionHeader(sectionName); if (rdi(shdr.sh_type) != SHT_NOBITS) memset(fileContents->data() + rdi(shdr.sh_offset), 'X', rdi(shdr.sh_size)); } @@ -1190,10 +1190,10 @@ static void setSubstr(std::string & s, unsigned int pos, const std::string & t) template -std::string ElfFile::getInterpreter() +std::string ElfFile::getInterpreter() const { auto shdr = findSectionHeader(".interp"); - return std::string((char *) fileContents->data() + rdi(shdr.sh_offset), rdi(shdr.sh_size) - 1); + return std::string((const char *) fileContents->data() + rdi(shdr.sh_offset), rdi(shdr.sh_size) - 1); } template @@ -1776,13 +1776,13 @@ void ElfFile::addNeeded(const std::set & libs) } template -void ElfFile::printNeededLibs() // const +void ElfFile::printNeededLibs() const { const auto shdrDynamic = findSectionHeader(".dynamic"); const auto shdrDynStr = findSectionHeader(".dynstr"); - const char *strTab = (char *)fileContents->data() + rdi(shdrDynStr.sh_offset); + const char *strTab = (const char *)fileContents->data() + rdi(shdrDynStr.sh_offset); - const Elf_Dyn *dyn = (Elf_Dyn *) (fileContents->data() + rdi(shdrDynamic.sh_offset)); + const Elf_Dyn *dyn = (const Elf_Dyn *) (fileContents->data() + rdi(shdrDynamic.sh_offset)); for (; rdi(dyn->d_tag) != DT_NULL; dyn++) { if (rdi(dyn->d_tag) == DT_NEEDED) { diff --git a/src/patchelf.h b/src/patchelf.h index 101b1c9..677462d 100644 --- a/src/patchelf.h +++ b/src/patchelf.h @@ -87,9 +87,9 @@ private: [[nodiscard]] std::string getSectionName(const Elf_Shdr & shdr) const; - Elf_Shdr & findSectionHeader(const SectionName & sectionName); + const Elf_Shdr & findSectionHeader(const SectionName & sectionName) const; - [[nodiscard]] std::optional> tryFindSectionHeader(const SectionName & sectionName); + [[nodiscard]] std::optional> tryFindSectionHeader(const SectionName & sectionName) const; [[nodiscard]] unsigned int getSectionIndex(const SectionName & sectionName) const; @@ -113,7 +113,7 @@ public: void rewriteSections(bool force = false); - [[nodiscard]] std::string getInterpreter(); + [[nodiscard]] std::string getInterpreter() const; typedef enum { printOsAbi, replaceOsAbi } osAbiMode; @@ -137,7 +137,7 @@ public: void replaceNeeded(const std::map & libs); - void printNeededLibs() /* should be const */; + void printNeededLibs() const; void noDefaultLib(); -- cgit v1.2.1