Skip to content

Commit

Permalink
Renamed findSection2 to tryFindSectionHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
fzakaria committed Dec 21, 2021
1 parent 1071237 commit 4604393
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ endif

bin_PROGRAMS = patchelf

patchelf_SOURCES = patchelf.cc elf.h
patchelf_SOURCES = patchelf.cc elf.h patchelf.h
22 changes: 11 additions & 11 deletions src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ std::string ElfFile<ElfFileParamNames>::getSectionName(const Elf_Shdr & shdr) co
template<ElfFileParams>
Elf_Shdr & ElfFile<ElfFileParamNames>::findSectionHeader(const SectionName & sectionName)
{
auto shdr = findSection2(sectionName);
auto shdr = tryFindSectionHeader(sectionName);
if (!shdr) {
std::string extraMsg;
if (sectionName == ".interp" || sectionName == ".dynamic" || sectionName == ".dynstr")
Expand All @@ -483,7 +483,7 @@ Elf_Shdr & ElfFile<ElfFileParamNames>::findSectionHeader(const SectionName & sec


template<ElfFileParams>
std::optional<std::reference_wrapper<Elf_Shdr>> ElfFile<ElfFileParamNames>::findSection2(const SectionName & sectionName)
std::optional<std::reference_wrapper<Elf_Shdr>> ElfFile<ElfFileParamNames>::tryFindSectionHeader(const SectionName & sectionName)
{
auto i = getSectionIndex(sectionName);
if (i)
Expand Down Expand Up @@ -967,7 +967,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
/* Update all those nasty virtual addresses in the .dynamic
section. Note that not all executables have .dynamic sections
(e.g., those produced by klibc's klcc). */
auto shdrDynamic = findSection2(".dynamic");
auto shdrDynamic = tryFindSectionHeader(".dynamic");
if (shdrDynamic) {
auto dyn_table = (Elf_Dyn *) (fileContents->data() + rdi((*shdrDynamic).get().sh_offset));
unsigned int d_tag;
Expand All @@ -981,30 +981,30 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
else if (d_tag == DT_HASH)
dyn->d_un.d_ptr = findSectionHeader(".hash").sh_addr;
else if (d_tag == DT_GNU_HASH) {
auto shdr = findSection2(".gnu.hash");
auto shdr = tryFindSectionHeader(".gnu.hash");
// some binaries might this section stripped
// in which case we just ignore the value.
if (shdr) dyn->d_un.d_ptr = (*shdr).get().sh_addr;
} else if (d_tag == DT_JMPREL) {
auto shdr = findSection2(".rel.plt");
if (!shdr) shdr = findSection2(".rela.plt");
auto shdr = tryFindSectionHeader(".rel.plt");
if (!shdr) shdr = tryFindSectionHeader(".rela.plt");
/* 64-bit Linux, x86-64 */
if (!shdr) shdr = findSection2(".rela.IA_64.pltoff"); /* 64-bit Linux, IA-64 */
if (!shdr) shdr = tryFindSectionHeader(".rela.IA_64.pltoff"); /* 64-bit Linux, IA-64 */
if (!shdr) error("cannot find section corresponding to DT_JMPREL");
dyn->d_un.d_ptr = (*shdr).get().sh_addr;
}
else if (d_tag == DT_REL) { /* !!! hack! */
auto shdr = findSection2(".rel.dyn");
auto shdr = tryFindSectionHeader(".rel.dyn");
/* no idea if this makes sense, but it was needed for some
program */
if (!shdr) shdr = findSection2(".rel.got");
if (!shdr) shdr = tryFindSectionHeader(".rel.got");
/* some programs have neither section, but this doesn't seem
to be a problem */
if (!shdr) continue;
dyn->d_un.d_ptr = (*shdr).get().sh_addr;
}
else if (d_tag == DT_RELA) {
auto shdr = findSection2(".rela.dyn");
auto shdr = tryFindSectionHeader(".rela.dyn");
/* some programs lack this section, but it doesn't seem to
be a problem */
if (!shdr) continue;
Expand All @@ -1017,7 +1017,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
else if (d_tag == DT_MIPS_RLD_MAP_REL) {
/* the MIPS_RLD_MAP_REL tag stores the offset to the debug
pointer, relative to the address of the tag */
auto shdr = findSection2(".rld_map");
auto shdr = tryFindSectionHeader(".rld_map");
if (shdr) {
auto rld_map_addr = findSectionHeader(".rld_map").sh_addr;
auto dyn_offset = ((char*)dyn) - ((char*)dyn_table);
Expand Down
25 changes: 2 additions & 23 deletions src/patchelf.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,6 @@ using FileContents = std::shared_ptr<std::vector<unsigned char>>;
template<ElfFileParams>
class ElfFile
{

/**
* @brief The ELF section header
*
*/
class Section {
public:
const std::string & getName() {
return name;
}
const Elf_Shdr * toStruct() {
return (Elf_Shdr *) this->data.data();
}
void resize(unsigned int size) {
data.resize(size, '\0');
}
private:
std::string name;
std::vector<unsigned char> data;
};

public:

const FileContents fileContents;
Expand Down Expand Up @@ -104,7 +83,7 @@ class ElfFile

Elf_Shdr & findSectionHeader(const SectionName & sectionName);

std::optional<std::reference_wrapper<Elf_Shdr>> findSection2(const SectionName & sectionName);
std::optional<std::reference_wrapper<Elf_Shdr>> tryFindSectionHeader(const SectionName & sectionName);

unsigned int getSectionIndex(const SectionName & sectionName);

Expand Down Expand Up @@ -177,4 +156,4 @@ class ElfFile
const Elf_Ehdr *hdr() const {
return (const Elf_Ehdr *)fileContents->data();
}
};
};

0 comments on commit 4604393

Please sign in to comment.