Skip to content

Commit 208303b

Browse files
author
Aaron D. Marasco
committed
Some const-correctness and C++11 auto
(cherry picked from commit 76e2cdb)
1 parent d6b2a72 commit 208303b

File tree

1 file changed

+37
-41
lines changed

1 file changed

+37
-41
lines changed

src/patchelf.cc

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class ElfFile
165165

166166
void shiftFile(unsigned int extraPages, Elf_Addr startPage);
167167

168-
std::string getSectionName(const Elf_Shdr & shdr);
168+
std::string getSectionName(const Elf_Shdr & shdr) const;
169169

170170
Elf_Shdr & findSection(const SectionName & sectionName);
171171

@@ -176,7 +176,7 @@ class ElfFile
176176
std::string & replaceSection(const SectionName & sectionName,
177177
unsigned int size);
178178

179-
bool haveReplacedSection(const SectionName & sectionName);
179+
bool haveReplacedSection(const SectionName & sectionName) const;
180180

181181
void writeReplacedSections(Elf_Off & curOff,
182182
Elf_Addr startAddr, Elf_Off startOffset);
@@ -209,7 +209,7 @@ class ElfFile
209209

210210
void replaceNeeded(const std::map<std::string, std::string> & libs);
211211

212-
void printNeededLibs();
212+
void printNeededLibs() /* should be const */;
213213

214214
void noDefaultLib();
215215

@@ -219,11 +219,11 @@ class ElfFile
219219
specified by the ELF header) to this platform's integer
220220
representation. */
221221
template<class I>
222-
I rdi(I i);
222+
I rdi(I i) const;
223223

224224
/* Convert back to the ELF representation. */
225225
template<class I>
226-
I wri(I & t, unsigned long long i)
226+
I wri(I & t, unsigned long long i) const
227227
{
228228
t = rdi((I) i);
229229
return i;
@@ -235,7 +235,7 @@ class ElfFile
235235
why... */
236236
template<ElfFileParams>
237237
template<class I>
238-
I ElfFile<ElfFileParamNames>::rdi(I i)
238+
I ElfFile<ElfFileParamNames>::rdi(I i) const
239239
{
240240
I r = 0;
241241
if (littleEndian) {
@@ -569,7 +569,7 @@ void ElfFile<ElfFileParamNames>::shiftFile(unsigned int extraPages, Elf_Addr sta
569569

570570

571571
template<ElfFileParams>
572-
std::string ElfFile<ElfFileParamNames>::getSectionName(const Elf_Shdr & shdr)
572+
std::string ElfFile<ElfFileParamNames>::getSectionName(const Elf_Shdr & shdr) const
573573
{
574574
return std::string(sectionNames.c_str() + rdi(shdr.sh_name));
575575
}
@@ -578,7 +578,7 @@ std::string ElfFile<ElfFileParamNames>::getSectionName(const Elf_Shdr & shdr)
578578
template<ElfFileParams>
579579
Elf_Shdr & ElfFile<ElfFileParamNames>::findSection(const SectionName & sectionName)
580580
{
581-
Elf_Shdr * shdr = findSection2(sectionName);
581+
auto shdr = findSection2(sectionName);
582582
if (!shdr) {
583583
std::string extraMsg = "";
584584
if (sectionName == ".interp" || sectionName == ".dynamic" || sectionName == ".dynstr")
@@ -592,7 +592,7 @@ Elf_Shdr & ElfFile<ElfFileParamNames>::findSection(const SectionName & sectionNa
592592
template<ElfFileParams>
593593
Elf_Shdr * ElfFile<ElfFileParamNames>::findSection2(const SectionName & sectionName)
594594
{
595-
unsigned int i = findSection3(sectionName);
595+
auto i = findSection3(sectionName);
596596
return i ? &shdrs[i] : 0;
597597
}
598598

@@ -606,13 +606,9 @@ unsigned int ElfFile<ElfFileParamNames>::findSection3(const SectionName & sectio
606606
}
607607

608608
template<ElfFileParams>
609-
bool ElfFile<ElfFileParamNames>::haveReplacedSection(const SectionName & sectionName)
609+
bool ElfFile<ElfFileParamNames>::haveReplacedSection(const SectionName & sectionName) const
610610
{
611-
ReplacedSections::iterator i = replacedSections.find(sectionName);
612-
613-
if (i != replacedSections.end())
614-
return true;
615-
return false;
611+
return (replacedSections.find(sectionName) != replacedSections.end());
616612
}
617613

618614
template<ElfFileParams>
@@ -625,7 +621,7 @@ std::string & ElfFile<ElfFileParamNames>::replaceSection(const SectionName & sec
625621
if (i != replacedSections.end()) {
626622
s = std::string(i->second);
627623
} else {
628-
Elf_Shdr & shdr = findSection(sectionName);
624+
auto shdr = findSection(sectionName);
629625
s = std::string((char *) contents + rdi(shdr.sh_offset), rdi(shdr.sh_size));
630626
}
631627

@@ -652,7 +648,7 @@ void ElfFile<ElfFileParamNames>::writeReplacedSections(Elf_Off & curOff,
652648

653649
for (auto & i : replacedSections) {
654650
std::string sectionName = i.first;
655-
Elf_Shdr & shdr = findSection(sectionName);
651+
auto & shdr = findSection(sectionName);
656652
debug("rewriting section '%s' from offset 0x%x (size %d) to offset 0x%x (size %d)\n",
657653
sectionName.c_str(), rdi(shdr.sh_offset), rdi(shdr.sh_size), curOff, i.second.size());
658654

@@ -947,7 +943,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
947943
/* Update all those nasty virtual addresses in the .dynamic
948944
section. Note that not all executables have .dynamic sections
949945
(e.g., those produced by klibc's klcc). */
950-
Elf_Shdr * shdrDynamic = findSection2(".dynamic");
946+
auto shdrDynamic = findSection2(".dynamic");
951947
if (shdrDynamic) {
952948
Elf_Dyn * dyn = (Elf_Dyn *) (contents + rdi(shdrDynamic->sh_offset));
953949
unsigned int d_tag;
@@ -963,14 +959,14 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
963959
else if (d_tag == DT_GNU_HASH)
964960
dyn->d_un.d_ptr = findSection(".gnu.hash").sh_addr;
965961
else if (d_tag == DT_JMPREL) {
966-
Elf_Shdr * shdr = findSection2(".rel.plt");
962+
auto shdr = findSection2(".rel.plt");
967963
if (!shdr) shdr = findSection2(".rela.plt"); /* 64-bit Linux, x86-64 */
968964
if (!shdr) shdr = findSection2(".rela.IA_64.pltoff"); /* 64-bit Linux, IA-64 */
969965
if (!shdr) error("cannot find section corresponding to DT_JMPREL");
970966
dyn->d_un.d_ptr = shdr->sh_addr;
971967
}
972968
else if (d_tag == DT_REL) { /* !!! hack! */
973-
Elf_Shdr * shdr = findSection2(".rel.dyn");
969+
auto shdr = findSection2(".rel.dyn");
974970
/* no idea if this makes sense, but it was needed for some
975971
program */
976972
if (!shdr) shdr = findSection2(".rel.got");
@@ -980,7 +976,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
980976
dyn->d_un.d_ptr = shdr->sh_addr;
981977
}
982978
else if (d_tag == DT_RELA) {
983-
Elf_Shdr * shdr = findSection2(".rela.dyn");
979+
auto shdr = findSection2(".rela.dyn");
984980
/* some programs lack this section, but it doesn't seem to
985981
be a problem */
986982
if (!shdr) continue;
@@ -1009,7 +1005,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
10091005
}
10101006
std::string section = sectionsByOldIndex.at(shndx);
10111007
assert(!section.empty());
1012-
unsigned int newIndex = findSection3(section); // inefficient
1008+
auto newIndex = findSection3(section); // inefficient
10131009
//debug("rewriting symbol %d: index = %d (%s) -> %d\n", entry, shndx, section.c_str(), newIndex);
10141010
wri(sym->st_shndx, newIndex);
10151011
/* Rewrite st_value. FIXME: we should do this for all
@@ -1033,7 +1029,7 @@ static void setSubstr(std::string & s, unsigned int pos, const std::string & t)
10331029
template<ElfFileParams>
10341030
std::string ElfFile<ElfFileParamNames>::getInterpreter()
10351031
{
1036-
Elf_Shdr & shdr = findSection(".interp");
1032+
auto shdr = findSection(".interp");
10371033
return std::string((char *) contents + rdi(shdr.sh_offset), rdi(shdr.sh_size));
10381034
}
10391035

@@ -1045,8 +1041,8 @@ void ElfFile<ElfFileParamNames>::modifySoname(sonameMode op, const std::string &
10451041
return;
10461042
}
10471043

1048-
Elf_Shdr & shdrDynamic = findSection(".dynamic");
1049-
Elf_Shdr & shdrDynStr = findSection(".dynstr");
1044+
auto shdrDynamic = findSection(".dynamic");
1045+
auto shdrDynStr = findSection(".dynstr");
10501046
char * strTab = (char *) contents + rdi(shdrDynStr.sh_offset);
10511047

10521048
/* Walk through the dynamic section, look for the DT_SONAME entry. */
@@ -1130,11 +1126,11 @@ template<ElfFileParams>
11301126
void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
11311127
const std::vector<std::string> & allowedRpathPrefixes, std::string newRPath)
11321128
{
1133-
Elf_Shdr & shdrDynamic = findSection(".dynamic");
1129+
auto shdrDynamic = findSection(".dynamic");
11341130

11351131
/* !!! We assume that the virtual address in the DT_STRTAB entry
11361132
of the dynamic section corresponds to the .dynstr section. */
1137-
Elf_Shdr & shdrDynStr = findSection(".dynstr");
1133+
auto shdrDynStr = findSection(".dynstr");
11381134
char * strTab = (char *) contents + rdi(shdrDynStr.sh_offset);
11391135

11401136

@@ -1322,8 +1318,8 @@ void ElfFile<ElfFileParamNames>::removeNeeded(const std::set<std::string> & libs
13221318
{
13231319
if (libs.empty()) return;
13241320

1325-
Elf_Shdr & shdrDynamic = findSection(".dynamic");
1326-
Elf_Shdr & shdrDynStr = findSection(".dynstr");
1321+
auto shdrDynamic = findSection(".dynamic");
1322+
auto shdrDynStr = findSection(".dynstr");
13271323
char * strTab = (char *) contents + rdi(shdrDynStr.sh_offset);
13281324

13291325
Elf_Dyn * dyn = (Elf_Dyn *) (contents + rdi(shdrDynamic.sh_offset));
@@ -1350,8 +1346,8 @@ void ElfFile<ElfFileParamNames>::replaceNeeded(const std::map<std::string, std::
13501346
{
13511347
if (libs.empty()) return;
13521348

1353-
Elf_Shdr & shdrDynamic = findSection(".dynamic");
1354-
Elf_Shdr & shdrDynStr = findSection(".dynstr");
1349+
auto shdrDynamic = findSection(".dynamic");
1350+
auto shdrDynStr = findSection(".dynstr");
13551351
char * strTab = (char *) contents + rdi(shdrDynStr.sh_offset);
13561352

13571353
Elf_Dyn * dyn = (Elf_Dyn *) (contents + rdi(shdrDynamic.sh_offset));
@@ -1396,7 +1392,7 @@ void ElfFile<ElfFileParamNames>::replaceNeeded(const std::map<std::string, std::
13961392
// be replaced.
13971393

13981394
if (verNeedNum) {
1399-
Elf_Shdr & shdrVersionR = findSection(".gnu.version_r");
1395+
auto shdrVersionR = findSection(".gnu.version_r");
14001396
// The filename strings in the .gnu.version_r are different from the
14011397
// ones in .dynamic: instead of being in .dynstr, they're in some
14021398
// arbitrary section and we have to look in ->sh_link to figure out
@@ -1446,8 +1442,8 @@ void ElfFile<ElfFileParamNames>::addNeeded(const std::set<std::string> & libs)
14461442
{
14471443
if (libs.empty()) return;
14481444

1449-
Elf_Shdr & shdrDynamic = findSection(".dynamic");
1450-
Elf_Shdr & shdrDynStr = findSection(".dynstr");
1445+
auto shdrDynamic = findSection(".dynamic");
1446+
auto shdrDynStr = findSection(".dynstr");
14511447

14521448
/* add all new libs to the dynstr string table */
14531449
unsigned int length = 0;
@@ -1489,17 +1485,17 @@ void ElfFile<ElfFileParamNames>::addNeeded(const std::set<std::string> & libs)
14891485
}
14901486

14911487
template<ElfFileParams>
1492-
void ElfFile<ElfFileParamNames>::printNeededLibs()
1488+
void ElfFile<ElfFileParamNames>::printNeededLibs() // const
14931489
{
1494-
Elf_Shdr & shdrDynamic = findSection(".dynamic");
1495-
Elf_Shdr & shdrDynStr = findSection(".dynstr");
1496-
char *strTab = (char *)contents + rdi(shdrDynStr.sh_offset);
1490+
const auto shdrDynamic = findSection(".dynamic");
1491+
const auto shdrDynStr = findSection(".dynstr");
1492+
const char *strTab = (char *)contents + rdi(shdrDynStr.sh_offset);
14971493

1498-
Elf_Dyn *dyn = (Elf_Dyn *) (contents + rdi(shdrDynamic.sh_offset));
1494+
const Elf_Dyn *dyn = (Elf_Dyn *) (contents + rdi(shdrDynamic.sh_offset));
14991495

15001496
for (; rdi(dyn->d_tag) != DT_NULL; dyn++) {
15011497
if (rdi(dyn->d_tag) == DT_NEEDED) {
1502-
char *name = strTab + rdi(dyn->d_un.d_val);
1498+
const char *name = strTab + rdi(dyn->d_un.d_val);
15031499
printf("%s\n", name);
15041500
}
15051501
}
@@ -1509,7 +1505,7 @@ void ElfFile<ElfFileParamNames>::printNeededLibs()
15091505
template<ElfFileParams>
15101506
void ElfFile<ElfFileParamNames>::noDefaultLib()
15111507
{
1512-
Elf_Shdr & shdrDynamic = findSection(".dynamic");
1508+
auto shdrDynamic = findSection(".dynamic");
15131509

15141510
Elf_Dyn * dyn = (Elf_Dyn *) (contents + rdi(shdrDynamic.sh_offset));
15151511
Elf_Dyn * dynFlags1 = 0;

0 commit comments

Comments
 (0)