Skip to content

Commit a1352e5

Browse files
committed
remove raw pointer from findSection2
1 parent 3cfd9a6 commit a1352e5

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
AM_CXXFLAGS = -Wall -std=c++11 -D_FILE_OFFSET_BITS=64
1+
AM_CXXFLAGS = -Wall -std=c++17 -D_FILE_OFFSET_BITS=64
22

33
if WITH_ASAN
44
AM_CXXFLAGS += -fsanitize=address -fsanitize-address-use-after-scope

src/patchelf.cc

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class ElfFile
160160

161161
Elf_Shdr & findSection(const SectionName & sectionName);
162162

163-
Elf_Shdr * findSection2(const SectionName & sectionName);
163+
std::optional<std::reference_wrapper<Elf_Shdr>> findSection2(const SectionName & sectionName);
164164

165165
unsigned int findSection3(const SectionName & sectionName);
166166

@@ -637,10 +637,12 @@ Elf_Shdr & ElfFile<ElfFileParamNames>::findSection(const SectionName & sectionNa
637637

638638

639639
template<ElfFileParams>
640-
Elf_Shdr * ElfFile<ElfFileParamNames>::findSection2(const SectionName & sectionName)
640+
std::optional<std::reference_wrapper<Elf_Shdr>> ElfFile<ElfFileParamNames>::findSection2(const SectionName & sectionName)
641641
{
642642
auto i = findSection3(sectionName);
643-
return i ? &shdrs[i] : nullptr;
643+
if (i)
644+
return shdrs[i];
645+
return {};
644646
}
645647

646648

@@ -1121,7 +1123,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
11211123
(e.g., those produced by klibc's klcc). */
11221124
auto shdrDynamic = findSection2(".dynamic");
11231125
if (shdrDynamic) {
1124-
auto dyn_table = (Elf_Dyn *) (fileContents->data() + rdi(shdrDynamic->sh_offset));
1126+
auto dyn_table = (Elf_Dyn *) (fileContents->data() + rdi((*shdrDynamic).get().sh_offset));
11251127
unsigned int d_tag;
11261128
for (auto dyn = dyn_table; (d_tag = rdi(dyn->d_tag)) != DT_NULL; dyn++)
11271129
if (d_tag == DT_STRTAB)
@@ -1136,13 +1138,14 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
11361138
auto shdr = findSection2(".gnu.hash");
11371139
// some binaries might this section stripped
11381140
// in which case we just ignore the value.
1139-
if (shdr) dyn->d_un.d_ptr = shdr->sh_addr;
1141+
if (shdr) dyn->d_un.d_ptr = (*shdr).get().sh_addr;
11401142
} else if (d_tag == DT_JMPREL) {
11411143
auto shdr = findSection2(".rel.plt");
1142-
if (!shdr) shdr = findSection2(".rela.plt"); /* 64-bit Linux, x86-64 */
1144+
if (!shdr) shdr = findSection2(".rela.plt");
1145+
/* 64-bit Linux, x86-64 */
11431146
if (!shdr) shdr = findSection2(".rela.IA_64.pltoff"); /* 64-bit Linux, IA-64 */
11441147
if (!shdr) error("cannot find section corresponding to DT_JMPREL");
1145-
dyn->d_un.d_ptr = shdr->sh_addr;
1148+
dyn->d_un.d_ptr = (*shdr).get().sh_addr;
11461149
}
11471150
else if (d_tag == DT_REL) { /* !!! hack! */
11481151
auto shdr = findSection2(".rel.dyn");
@@ -1152,14 +1155,14 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
11521155
/* some programs have neither section, but this doesn't seem
11531156
to be a problem */
11541157
if (!shdr) continue;
1155-
dyn->d_un.d_ptr = shdr->sh_addr;
1158+
dyn->d_un.d_ptr = (*shdr).get().sh_addr;
11561159
}
11571160
else if (d_tag == DT_RELA) {
11581161
auto shdr = findSection2(".rela.dyn");
11591162
/* some programs lack this section, but it doesn't seem to
11601163
be a problem */
11611164
if (!shdr) continue;
1162-
dyn->d_un.d_ptr = shdr->sh_addr;
1165+
dyn->d_un.d_ptr = (*shdr).get().sh_addr;
11631166
}
11641167
else if (d_tag == DT_VERNEED)
11651168
dyn->d_un.d_ptr = findSection(".gnu.version_r").sh_addr;
@@ -1172,7 +1175,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
11721175
if (shdr) {
11731176
auto rld_map_addr = findSection(".rld_map").sh_addr;
11741177
auto dyn_offset = ((char*)dyn) - ((char*)dyn_table);
1175-
dyn->d_un.d_ptr = rld_map_addr + dyn_offset - shdrDynamic->sh_addr;
1178+
dyn->d_un.d_ptr = rld_map_addr + dyn_offset - (*shdrDynamic).get().sh_addr;
11761179
} else {
11771180
/* ELF file with DT_MIPS_RLD_MAP_REL but without .rld_map
11781181
is broken, and it's not our job to fix it; yet, we have

0 commit comments

Comments
 (0)