Skip to content

Commit 4604393

Browse files
committed
Renamed findSection2 to tryFindSectionHeader
1 parent 1071237 commit 4604393

File tree

3 files changed

+14
-35
lines changed

3 files changed

+14
-35
lines changed

src/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ endif
1919

2020
bin_PROGRAMS = patchelf
2121

22-
patchelf_SOURCES = patchelf.cc elf.h
22+
patchelf_SOURCES = patchelf.cc elf.h patchelf.h

src/patchelf.cc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ std::string ElfFile<ElfFileParamNames>::getSectionName(const Elf_Shdr & shdr) co
471471
template<ElfFileParams>
472472
Elf_Shdr & ElfFile<ElfFileParamNames>::findSectionHeader(const SectionName & sectionName)
473473
{
474-
auto shdr = findSection2(sectionName);
474+
auto shdr = tryFindSectionHeader(sectionName);
475475
if (!shdr) {
476476
std::string extraMsg;
477477
if (sectionName == ".interp" || sectionName == ".dynamic" || sectionName == ".dynstr")
@@ -483,7 +483,7 @@ Elf_Shdr & ElfFile<ElfFileParamNames>::findSectionHeader(const SectionName & sec
483483

484484

485485
template<ElfFileParams>
486-
std::optional<std::reference_wrapper<Elf_Shdr>> ElfFile<ElfFileParamNames>::findSection2(const SectionName & sectionName)
486+
std::optional<std::reference_wrapper<Elf_Shdr>> ElfFile<ElfFileParamNames>::tryFindSectionHeader(const SectionName & sectionName)
487487
{
488488
auto i = getSectionIndex(sectionName);
489489
if (i)
@@ -967,7 +967,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
967967
/* Update all those nasty virtual addresses in the .dynamic
968968
section. Note that not all executables have .dynamic sections
969969
(e.g., those produced by klibc's klcc). */
970-
auto shdrDynamic = findSection2(".dynamic");
970+
auto shdrDynamic = tryFindSectionHeader(".dynamic");
971971
if (shdrDynamic) {
972972
auto dyn_table = (Elf_Dyn *) (fileContents->data() + rdi((*shdrDynamic).get().sh_offset));
973973
unsigned int d_tag;
@@ -981,30 +981,30 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
981981
else if (d_tag == DT_HASH)
982982
dyn->d_un.d_ptr = findSectionHeader(".hash").sh_addr;
983983
else if (d_tag == DT_GNU_HASH) {
984-
auto shdr = findSection2(".gnu.hash");
984+
auto shdr = tryFindSectionHeader(".gnu.hash");
985985
// some binaries might this section stripped
986986
// in which case we just ignore the value.
987987
if (shdr) dyn->d_un.d_ptr = (*shdr).get().sh_addr;
988988
} else if (d_tag == DT_JMPREL) {
989-
auto shdr = findSection2(".rel.plt");
990-
if (!shdr) shdr = findSection2(".rela.plt");
989+
auto shdr = tryFindSectionHeader(".rel.plt");
990+
if (!shdr) shdr = tryFindSectionHeader(".rela.plt");
991991
/* 64-bit Linux, x86-64 */
992-
if (!shdr) shdr = findSection2(".rela.IA_64.pltoff"); /* 64-bit Linux, IA-64 */
992+
if (!shdr) shdr = tryFindSectionHeader(".rela.IA_64.pltoff"); /* 64-bit Linux, IA-64 */
993993
if (!shdr) error("cannot find section corresponding to DT_JMPREL");
994994
dyn->d_un.d_ptr = (*shdr).get().sh_addr;
995995
}
996996
else if (d_tag == DT_REL) { /* !!! hack! */
997-
auto shdr = findSection2(".rel.dyn");
997+
auto shdr = tryFindSectionHeader(".rel.dyn");
998998
/* no idea if this makes sense, but it was needed for some
999999
program */
1000-
if (!shdr) shdr = findSection2(".rel.got");
1000+
if (!shdr) shdr = tryFindSectionHeader(".rel.got");
10011001
/* some programs have neither section, but this doesn't seem
10021002
to be a problem */
10031003
if (!shdr) continue;
10041004
dyn->d_un.d_ptr = (*shdr).get().sh_addr;
10051005
}
10061006
else if (d_tag == DT_RELA) {
1007-
auto shdr = findSection2(".rela.dyn");
1007+
auto shdr = tryFindSectionHeader(".rela.dyn");
10081008
/* some programs lack this section, but it doesn't seem to
10091009
be a problem */
10101010
if (!shdr) continue;
@@ -1017,7 +1017,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
10171017
else if (d_tag == DT_MIPS_RLD_MAP_REL) {
10181018
/* the MIPS_RLD_MAP_REL tag stores the offset to the debug
10191019
pointer, relative to the address of the tag */
1020-
auto shdr = findSection2(".rld_map");
1020+
auto shdr = tryFindSectionHeader(".rld_map");
10211021
if (shdr) {
10221022
auto rld_map_addr = findSectionHeader(".rld_map").sh_addr;
10231023
auto dyn_offset = ((char*)dyn) - ((char*)dyn_table);

src/patchelf.h

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,6 @@ using FileContents = std::shared_ptr<std::vector<unsigned char>>;
66
template<ElfFileParams>
77
class ElfFile
88
{
9-
10-
/**
11-
* @brief The ELF section header
12-
*
13-
*/
14-
class Section {
15-
public:
16-
const std::string & getName() {
17-
return name;
18-
}
19-
const Elf_Shdr * toStruct() {
20-
return (Elf_Shdr *) this->data.data();
21-
}
22-
void resize(unsigned int size) {
23-
data.resize(size, '\0');
24-
}
25-
private:
26-
std::string name;
27-
std::vector<unsigned char> data;
28-
};
29-
309
public:
3110

3211
const FileContents fileContents;
@@ -104,7 +83,7 @@ class ElfFile
10483

10584
Elf_Shdr & findSectionHeader(const SectionName & sectionName);
10685

107-
std::optional<std::reference_wrapper<Elf_Shdr>> findSection2(const SectionName & sectionName);
86+
std::optional<std::reference_wrapper<Elf_Shdr>> tryFindSectionHeader(const SectionName & sectionName);
10887

10988
unsigned int getSectionIndex(const SectionName & sectionName);
11089

@@ -177,4 +156,4 @@ class ElfFile
177156
const Elf_Ehdr *hdr() const {
178157
return (const Elf_Ehdr *)fileContents->data();
179158
}
180-
};
159+
};

0 commit comments

Comments
 (0)