Skip to content

Commit add92c1

Browse files
authored
Merge pull request #235 from emlix/cleanups
minor improvements
2 parents 1e1544a + acb4665 commit add92c1

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

src/elf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,9 @@ typedef struct
366366
required */
367367
#define SHF_GROUP (1 << 9) /* Section is member of a group. */
368368
#define SHF_TLS (1 << 10) /* Section hold thread-local data. */
369+
#define SHF_COMPRESSED (1 << 11) /* Section with compressed data */
369370
#define SHF_MASKOS 0x0ff00000 /* OS-specific. */
371+
#define SHF_GNU_BUILD_NOTE (1 << 20) /* Section contains GNU BUILD ATTRIBUTE notes. */
370372
#define SHF_MASKPROC 0xf0000000 /* Processor-specific */
371373
#define SHF_ORDERED (1 << 30) /* Special ordering requirement
372374
(Solaris). */

src/patchelf.cc

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,8 @@ static void checkPointer(const FileContents & contents, void * p, unsigned int s
380380

381381

382382
template<ElfFileParams>
383-
ElfFile<ElfFileParamNames>::ElfFile(FileContents fileContents)
384-
: fileContents(fileContents)
383+
ElfFile<ElfFileParamNames>::ElfFile(FileContents fContents)
384+
: fileContents(fContents)
385385
, contents(fileContents->data())
386386
{
387387
/* Check the ELF header for basic validity. */
@@ -673,15 +673,15 @@ void ElfFile<ElfFileParamNames>::writeReplacedSections(Elf_Off & curOff,
673673
*before* writing the new section contents (below) to prevent
674674
clobbering previously written new section contents. */
675675
for (auto & i : replacedSections) {
676-
std::string sectionName = i.first;
676+
const std::string & sectionName = i.first;
677677
Elf_Shdr & shdr = findSection(sectionName);
678678
if (rdi(shdr.sh_type) != SHT_NOBITS)
679679
memset(contents + rdi(shdr.sh_offset), 'X', rdi(shdr.sh_size));
680680
}
681681

682682
std::set<unsigned int> noted_phdrs = {};
683683
for (auto & i : replacedSections) {
684-
std::string sectionName = i.first;
684+
const std::string & sectionName = i.first;
685685
auto & shdr = findSection(sectionName);
686686
Elf_Shdr orig_shdr = shdr;
687687
debug("rewriting section '%s' from offset 0x%x (size %d) to offset 0x%x (size %d)\n",
@@ -710,7 +710,7 @@ void ElfFile<ElfFileParamNames>::writeReplacedSections(Elf_Off & curOff,
710710

711711
/* If this is the .dynamic section, then the PT_DYNAMIC segment
712712
must be sync'ed with it. */
713-
if (sectionName == ".dynamic") {
713+
else if (sectionName == ".dynamic") {
714714
for (auto & phdr : phdrs) {
715715
if (rdi(phdr.p_type) == PT_DYNAMIC) {
716716
phdr.p_offset = shdr.sh_offset;
@@ -802,11 +802,11 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
802802

803803
/* Compute the total space needed for the replaced sections */
804804
off_t neededSpace = 0;
805-
for (auto & i : replacedSections)
806-
neededSpace += roundUp(i.second.size(), sectionAlignment);
805+
for (auto & s : replacedSections)
806+
neededSpace += roundUp(s.second.size(), sectionAlignment);
807807
debug("needed space is %d\n", neededSpace);
808808

809-
size_t startOffset = roundUp(fileContents->size(), getPageSize());
809+
Elf_Off startOffset = roundUp(fileContents->size(), getPageSize());
810810

811811
growFile(fileContents, startOffset + neededSpace);
812812

@@ -903,7 +903,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
903903
debug("replacing section '%s' which is in the way\n", sectionName.c_str());
904904
replaceSection(sectionName, rdi(shdr.sh_size));
905905
}
906-
prevSection = sectionName;
906+
prevSection = std::move(sectionName);
907907
}
908908

909909
debug("first reserved offset/addr is 0x%x/0x%llx\n",
@@ -1147,7 +1147,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
11471147
fprintf(stderr, "warning: entry %d in symbol table refers to a non-existent section, skipping\n", shndx);
11481148
continue;
11491149
}
1150-
std::string section = sectionsByOldIndex.at(shndx);
1150+
const std::string & section = sectionsByOldIndex.at(shndx);
11511151
assert(!section.empty());
11521152
auto newIndex = findSection3(section); // inefficient
11531153
//debug("rewriting symbol %d: index = %d (%s) -> %d\n", entry, shndx, section.c_str(), newIndex);
@@ -1202,7 +1202,7 @@ void ElfFile<ElfFileParamNames>::modifySoname(sonameMode op, const std::string &
12021202

12031203
if (op == printSoname) {
12041204
if (soname) {
1205-
if (std::string(soname ? soname : "").empty())
1205+
if (strlen(soname) == 0)
12061206
debug("DT_SONAME is empty\n");
12071207
else
12081208
printf("%s\n", soname);
@@ -1212,7 +1212,7 @@ void ElfFile<ElfFileParamNames>::modifySoname(sonameMode op, const std::string &
12121212
return;
12131213
}
12141214

1215-
if (std::string(soname ? soname : "") == newSoname) {
1215+
if (soname && soname == newSoname) {
12161216
debug("current and proposed new SONAMEs are equal keeping DT_SONAME entry\n");
12171217
return;
12181218
}
@@ -1373,7 +1373,7 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
13731373
return;
13741374
}
13751375

1376-
auto dyn = (Elf_Dyn *)(contents + rdi(shdrDynamic.sh_offset));
1376+
dyn = (Elf_Dyn *)(contents + rdi(shdrDynamic.sh_offset));
13771377
Elf_Dyn * last = dyn;
13781378
for ( ; rdi(dyn->d_tag) != DT_NULL; dyn++) {
13791379
if (rdi(dyn->d_tag) == DT_RPATH) {
@@ -1403,7 +1403,7 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op,
14031403
changed = true;
14041404
}
14051405

1406-
if (std::string(rpath ? rpath : "") == newRPath) {
1406+
if (rpath && rpath == newRPath) {
14071407
return;
14081408
}
14091409

@@ -1508,7 +1508,7 @@ void ElfFile<ElfFileParamNames>::replaceNeeded(const std::map<std::string, std::
15081508
if (rdi(dyn->d_tag) == DT_NEEDED) {
15091509
char * name = strTab + rdi(dyn->d_un.d_val);
15101510
auto i = libs.find(name);
1511-
if (i != libs.end()) {
1511+
if (i != libs.end() && name != i->second) {
15121512
auto replacement = i->second;
15131513

15141514
debug("replacing DT_NEEDED entry '%s' with '%s'\n", name, replacement.c_str());
@@ -1560,7 +1560,7 @@ void ElfFile<ElfFileParamNames>::replaceNeeded(const std::map<std::string, std::
15601560
while (verNeedNum > 0) {
15611561
char * file = verStrTab + rdi(need->vn_file);
15621562
auto i = libs.find(file);
1563-
if (i != libs.end()) {
1563+
if (i != libs.end() && file != i->second) {
15641564
auto replacement = i->second;
15651565

15661566
debug("replacing .gnu.version_r entry '%s' with '%s'\n", file, replacement.c_str());
@@ -1739,7 +1739,7 @@ static bool printNeeded = false;
17391739
static bool noDefaultLib = false;
17401740

17411741
template<class ElfFile>
1742-
static void patchElf2(ElfFile && elfFile, const FileContents & fileContents, std::string fileName)
1742+
static void patchElf2(ElfFile && elfFile, const FileContents & fileContents, const std::string & fileName)
17431743
{
17441744
if (printInterpreter)
17451745
printf("%s\n", elfFile.getInterpreter().c_str());
@@ -1790,7 +1790,7 @@ static void patchElf()
17901790
debug("patching ELF file '%s'\n", fileName.c_str());
17911791

17921792
auto fileContents = readFile(fileName);
1793-
std::string outputFileName2 = outputFileName.empty() ? fileName : outputFileName;
1793+
const std::string & outputFileName2 = outputFileName.empty() ? fileName : outputFileName;
17941794

17951795
if (getElfType(fileContents).is32Bit)
17961796
patchElf2(ElfFile<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Addr, Elf32_Off, Elf32_Dyn, Elf32_Sym, Elf32_Verneed, Elf32_Versym>(fileContents), fileContents, outputFileName2);

0 commit comments

Comments
 (0)