Skip to content

Commit 7013e59

Browse files
authored
Merge pull request #210 from AaronDMarasco/const_cleanup
Some const-correctness and C++11 auto
2 parents 4dc6e9e + 208303b commit 7013e59

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) {
@@ -568,7 +568,7 @@ void ElfFile<ElfFileParamNames>::shiftFile(unsigned int extraPages, Elf_Addr sta
568568

569569

570570
template<ElfFileParams>
571-
std::string ElfFile<ElfFileParamNames>::getSectionName(const Elf_Shdr & shdr)
571+
std::string ElfFile<ElfFileParamNames>::getSectionName(const Elf_Shdr & shdr) const
572572
{
573573
return std::string(sectionNames.c_str() + rdi(shdr.sh_name));
574574
}
@@ -577,7 +577,7 @@ std::string ElfFile<ElfFileParamNames>::getSectionName(const Elf_Shdr & shdr)
577577
template<ElfFileParams>
578578
Elf_Shdr & ElfFile<ElfFileParamNames>::findSection(const SectionName & sectionName)
579579
{
580-
Elf_Shdr * shdr = findSection2(sectionName);
580+
auto shdr = findSection2(sectionName);
581581
if (!shdr) {
582582
std::string extraMsg = "";
583583
if (sectionName == ".interp" || sectionName == ".dynamic" || sectionName == ".dynstr")
@@ -591,7 +591,7 @@ Elf_Shdr & ElfFile<ElfFileParamNames>::findSection(const SectionName & sectionNa
591591
template<ElfFileParams>
592592
Elf_Shdr * ElfFile<ElfFileParamNames>::findSection2(const SectionName & sectionName)
593593
{
594-
unsigned int i = findSection3(sectionName);
594+
auto i = findSection3(sectionName);
595595
return i ? &shdrs[i] : 0;
596596
}
597597

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

607607
template<ElfFileParams>
608-
bool ElfFile<ElfFileParamNames>::haveReplacedSection(const SectionName & sectionName)
608+
bool ElfFile<ElfFileParamNames>::haveReplacedSection(const SectionName & sectionName) const
609609
{
610-
ReplacedSections::iterator i = replacedSections.find(sectionName);
611-
612-
if (i != replacedSections.end())
613-
return true;
614-
return false;
610+
return (replacedSections.find(sectionName) != replacedSections.end());
615611
}
616612

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

@@ -651,7 +647,7 @@ void ElfFile<ElfFileParamNames>::writeReplacedSections(Elf_Off & curOff,
651647

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

@@ -946,7 +942,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
946942
/* Update all those nasty virtual addresses in the .dynamic
947943
section. Note that not all executables have .dynamic sections
948944
(e.g., those produced by klibc's klcc). */
949-
Elf_Shdr * shdrDynamic = findSection2(".dynamic");
945+
auto shdrDynamic = findSection2(".dynamic");
950946
if (shdrDynamic) {
951947
Elf_Dyn * dyn = (Elf_Dyn *) (contents + rdi(shdrDynamic->sh_offset));
952948
unsigned int d_tag;
@@ -962,14 +958,14 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
962958
else if (d_tag == DT_GNU_HASH)
963959
dyn->d_un.d_ptr = findSection(".gnu.hash").sh_addr;
964960
else if (d_tag == DT_JMPREL) {
965-
Elf_Shdr * shdr = findSection2(".rel.plt");
961+
auto shdr = findSection2(".rel.plt");
966962
if (!shdr) shdr = findSection2(".rela.plt"); /* 64-bit Linux, x86-64 */
967963
if (!shdr) shdr = findSection2(".rela.IA_64.pltoff"); /* 64-bit Linux, IA-64 */
968964
if (!shdr) error("cannot find section corresponding to DT_JMPREL");
969965
dyn->d_un.d_ptr = shdr->sh_addr;
970966
}
971967
else if (d_tag == DT_REL) { /* !!! hack! */
972-
Elf_Shdr * shdr = findSection2(".rel.dyn");
968+
auto shdr = findSection2(".rel.dyn");
973969
/* no idea if this makes sense, but it was needed for some
974970
program */
975971
if (!shdr) shdr = findSection2(".rel.got");
@@ -979,7 +975,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
979975
dyn->d_un.d_ptr = shdr->sh_addr;
980976
}
981977
else if (d_tag == DT_RELA) {
982-
Elf_Shdr * shdr = findSection2(".rela.dyn");
978+
auto shdr = findSection2(".rela.dyn");
983979
/* some programs lack this section, but it doesn't seem to
984980
be a problem */
985981
if (!shdr) continue;
@@ -1008,7 +1004,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
10081004
}
10091005
std::string section = sectionsByOldIndex.at(shndx);
10101006
assert(!section.empty());
1011-
unsigned int newIndex = findSection3(section); // inefficient
1007+
auto newIndex = findSection3(section); // inefficient
10121008
//debug("rewriting symbol %d: index = %d (%s) -> %d\n", entry, shndx, section.c_str(), newIndex);
10131009
wri(sym->st_shndx, newIndex);
10141010
/* Rewrite st_value. FIXME: we should do this for all
@@ -1032,7 +1028,7 @@ static void setSubstr(std::string & s, unsigned int pos, const std::string & t)
10321028
template<ElfFileParams>
10331029
std::string ElfFile<ElfFileParamNames>::getInterpreter()
10341030
{
1035-
Elf_Shdr & shdr = findSection(".interp");
1031+
auto shdr = findSection(".interp");
10361032
return std::string((char *) contents + rdi(shdr.sh_offset), rdi(shdr.sh_size));
10371033
}
10381034

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

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

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

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

11391135

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

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

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

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

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

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

1448-
Elf_Shdr & shdrDynamic = findSection(".dynamic");
1449-
Elf_Shdr & shdrDynStr = findSection(".dynstr");
1444+
auto shdrDynamic = findSection(".dynamic");
1445+
auto shdrDynStr = findSection(".dynstr");
14501446

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

14901486
template<ElfFileParams>
1491-
void ElfFile<ElfFileParamNames>::printNeededLibs()
1487+
void ElfFile<ElfFileParamNames>::printNeededLibs() // const
14921488
{
1493-
Elf_Shdr & shdrDynamic = findSection(".dynamic");
1494-
Elf_Shdr & shdrDynStr = findSection(".dynstr");
1495-
char *strTab = (char *)contents + rdi(shdrDynStr.sh_offset);
1489+
const auto shdrDynamic = findSection(".dynamic");
1490+
const auto shdrDynStr = findSection(".dynstr");
1491+
const char *strTab = (char *)contents + rdi(shdrDynStr.sh_offset);
14961492

1497-
Elf_Dyn *dyn = (Elf_Dyn *) (contents + rdi(shdrDynamic.sh_offset));
1493+
const Elf_Dyn *dyn = (Elf_Dyn *) (contents + rdi(shdrDynamic.sh_offset));
14981494

14991495
for (; rdi(dyn->d_tag) != DT_NULL; dyn++) {
15001496
if (rdi(dyn->d_tag) == DT_NEEDED) {
1501-
char *name = strTab + rdi(dyn->d_un.d_val);
1497+
const char *name = strTab + rdi(dyn->d_un.d_val);
15021498
printf("%s\n", name);
15031499
}
15041500
}
@@ -1508,7 +1504,7 @@ void ElfFile<ElfFileParamNames>::printNeededLibs()
15081504
template<ElfFileParams>
15091505
void ElfFile<ElfFileParamNames>::noDefaultLib()
15101506
{
1511-
Elf_Shdr & shdrDynamic = findSection(".dynamic");
1507+
auto shdrDynamic = findSection(".dynamic");
15121508

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

0 commit comments

Comments
 (0)