summaryrefslogtreecommitdiffstats
path: root/src/patchelf.cc
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2012-08-21 17:40:43 (GMT)
committerEelco Dolstra <eelco.dolstra@logicblox.com>2012-08-21 17:40:43 (GMT)
commit9b794ce69129340e1b7160b9aca2910d09e34693 (patch)
treee15921dc9dcc3477140088c705433db631c00765 /src/patchelf.cc
parenta1ddbd47d3836a5912a05439f4321db0e329fbbf (diff)
downloadpatchelf-9b794ce69129340e1b7160b9aca2910d09e34693.zip
patchelf-9b794ce69129340e1b7160b9aca2910d09e34693.tar.gz
patchelf-9b794ce69129340e1b7160b9aca2910d09e34693.tar.bz2
Remove trailing whitespace
Diffstat (limited to 'src/patchelf.cc')
-rw-r--r--src/patchelf.cc104
1 files changed, 52 insertions, 52 deletions
diff --git a/src/patchelf.cc b/src/patchelf.cc
index 6fd7f45..cf79310 100644
--- a/src/patchelf.cc
+++ b/src/patchelf.cc
@@ -70,7 +70,7 @@ class ElfFile
public:
- ElfFile()
+ ElfFile()
{
changed = false;
sectionAlignment = sizeof(Elf_Off);
@@ -80,7 +80,7 @@ public:
{
return changed;
}
-
+
void parse();
private:
@@ -100,7 +100,7 @@ private:
void sortPhdrs();
- struct CompShdr
+ struct CompShdr
{
ElfFile * elfFile;
bool operator ()(const Elf_Shdr & x, const Elf_Shdr & y)
@@ -108,7 +108,7 @@ private:
return elfFile->rdi(x.sh_offset) < elfFile->rdi(y.sh_offset);
}
};
-
+
friend struct CompShdr;
void sortShdrs();
@@ -118,7 +118,7 @@ private:
string getSectionName(const Elf_Shdr & shdr);
Elf_Shdr & findSection(const SectionName & sectionName);
-
+
Elf_Shdr * findSection2(const SectionName & sectionName);
unsigned int findSection3(const SectionName & sectionName);
@@ -128,11 +128,11 @@ private:
void writeReplacedSections(Elf_Off & curOff,
Elf_Addr startAddr, Elf_Off startOffset);
-
+
void rewriteHeaders(Elf_Addr phdrAddress);
void rewriteSectionsLibrary();
-
+
void rewriteSectionsExecutable();
public:
@@ -148,7 +148,7 @@ public:
void modifyRPath(RPathOp op, string newRPath);
private:
-
+
/* Convert an integer in big or little endian representation (as
specified by the ELF header) to this platform's integer
representation. */
@@ -157,7 +157,7 @@ private:
/* Convert back to the ELF representation. */
template<class I>
- I wri(I & t, unsigned long long i)
+ I wri(I & t, unsigned long long i)
{
t = rdi((I) i);
return i;
@@ -169,7 +169,7 @@ private:
why... */
template<ElfFileParams>
template<class I>
-I ElfFile<ElfFileParamNames>::rdi(I i)
+I ElfFile<ElfFileParamNames>::rdi(I i)
{
I r = 0;
if (littleEndian) {
@@ -224,7 +224,7 @@ static void readFile(string fileName, mode_t * fileMode)
fileSize = st.st_size;
*fileMode = st.st_mode;
maxSize = fileSize + 8 * 1024 * 1024;
-
+
contents = (unsigned char *) malloc(fileSize + maxSize);
if (!contents) abort();
@@ -232,7 +232,7 @@ static void readFile(string fileName, mode_t * fileMode)
if (fd == -1) error("open");
if (read(fd, contents, fileSize) != fileSize) error("read");
-
+
close(fd);
}
@@ -248,7 +248,7 @@ template<ElfFileParams>
void ElfFile<ElfFileParamNames>::parse()
{
isExecutable = false;
-
+
/* Check the ELF header for basic validity. */
if (fileSize < (off_t) sizeof(Elf_Ehdr)) error("missing ELF header");
@@ -258,13 +258,13 @@ void ElfFile<ElfFileParamNames>::parse()
error("not an ELF executable");
littleEndian = contents[EI_DATA] == ELFDATA2LSB;
-
+
if (rdi(hdr->e_type) != ET_EXEC && rdi(hdr->e_type) != ET_DYN)
error("wrong ELF type");
if ((off_t) (rdi(hdr->e_phoff) + rdi(hdr->e_phnum) * rdi(hdr->e_phentsize)) > fileSize)
error("missing program headers");
-
+
if ((off_t) (rdi(hdr->e_shoff) + rdi(hdr->e_shnum) * rdi(hdr->e_shentsize)) > fileSize)
error("missing section headers");
@@ -276,7 +276,7 @@ void ElfFile<ElfFileParamNames>::parse()
phdrs.push_back(* ((Elf_Phdr *) (contents + rdi(hdr->e_phoff)) + i));
if (rdi(phdrs[i].p_type) == PT_INTERP) isExecutable = true;
}
-
+
for (int i = 0; i < rdi(hdr->e_shnum); ++i)
shdrs.push_back(* ((Elf_Shdr *) (contents + rdi(hdr->e_shoff)) + i));
@@ -325,7 +325,7 @@ void ElfFile<ElfFileParamNames>::sortShdrs()
/* Idem for the index of the .shstrtab section in the ELF header. */
SectionName shstrtabName = getSectionName(shdrs[rdi(hdr->e_shstrndx)]);
-
+
/* Sort the sections by offset. */
CompShdr comp;
comp.elfFile = this;
@@ -352,13 +352,13 @@ void ElfFile<ElfFileParamNames>::sortShdrs()
static void writeFile(string fileName, mode_t fileMode)
{
string fileName2 = fileName + "_patchelf_tmp";
-
+
int fd = open(fileName2.c_str(),
O_CREAT | O_TRUNC | O_WRONLY, 0700);
if (fd == -1) error("open");
if (write(fd, contents, fileSize) != fileSize) error("write");
-
+
if (close(fd) != 0) error("close");
if (chmod(fileName2.c_str(), fileMode) != 0) error("chmod");
@@ -387,11 +387,11 @@ void ElfFile<ElfFileParamNames>::shiftFile(unsigned int extraPages, Elf_Addr sta
/* Adjust the ELF header. */
wri(hdr->e_phoff, sizeof(Elf_Ehdr));
wri(hdr->e_shoff, rdi(hdr->e_shoff) + shift);
-
+
/* Update the offsets in the section headers. */
for (int i = 1; i < rdi(hdr->e_shnum); ++i)
wri(shdrs[i].sh_offset, rdi(shdrs[i].sh_offset) + shift);
-
+
/* Update the offsets in the program headers. */
for (int i = 0; i < rdi(hdr->e_phnum); ++i) {
wri(phdrs[i].p_offset, rdi(phdrs[i].p_offset) + shift);
@@ -411,7 +411,7 @@ void ElfFile<ElfFileParamNames>::shiftFile(unsigned int extraPages, Elf_Addr sta
wri(phdr.p_type, PT_LOAD);
wri(phdr.p_offset, 0);
wri(phdr.p_vaddr, wri(phdr.p_paddr, startPage));
- wri(phdr.p_filesz, wri(phdr.p_memsz, shift));
+ wri(phdr.p_filesz, wri(phdr.p_memsz, shift));
wri(phdr.p_flags, PF_R | PF_W);
wri(phdr.p_align, pageSize);
}
@@ -457,14 +457,14 @@ string & ElfFile<ElfFileParamNames>::replaceSection(const SectionName & sectionN
{
ReplacedSections::iterator i = replacedSections.find(sectionName);
string s;
-
+
if (i != replacedSections.end()) {
s = string(i->second);
} else {
Elf_Shdr & shdr = findSection(sectionName);
s = string((char *) contents + rdi(shdr.sh_offset), rdi(shdr.sh_size));
}
-
+
s.resize(size);
replacedSections[sectionName] = s;
@@ -486,7 +486,7 @@ void ElfFile<ElfFileParamNames>::writeReplacedSections(Elf_Off & curOff,
Elf_Shdr & shdr = findSection(sectionName);
memset(contents + rdi(shdr.sh_offset), 'X', rdi(shdr.sh_size));
}
-
+
for (ReplacedSections::iterator i = replacedSections.begin();
i != replacedSections.end(); ++i)
{
@@ -591,7 +591,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsLibrary()
startPage = startOffset;
}
-
+
/* Add a segment that maps the replaced sections and program
headers into memory. */
phdrs.resize(rdi(hdr->e_phnum) + 1);
@@ -639,7 +639,7 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
assert(lastReplaced != 0);
debug("last replaced is %d\n", lastReplaced);
-
+
/* Try to replace all sections before that, as far as possible.
Stop when we reach an irreplacable section (such as one of type
SHT_PROGBITS). These cannot be moved in virtual address space
@@ -672,18 +672,18 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
debug("first reserved offset/addr is 0x%x/0x%llx\n",
startOffset, (unsigned long long) startAddr);
-
+
assert(startAddr % pageSize == startOffset % pageSize);
Elf_Addr firstPage = startAddr - startOffset;
debug("first page is 0x%llx\n", (unsigned long long) firstPage);
-
+
/* Right now we assume that the section headers are somewhere near
the end, which appears to be the case most of the time.
Therefore they're not accidentally overwritten by the replaced
sections. !!! Fix this. */
assert((off_t) rdi(hdr->e_shoff) >= startOffset);
-
+
/* Compute the total space needed for the replaced sections, the
ELF header, and the program headers. */
size_t neededSpace = sizeof(Elf_Ehdr) + phdrs.size() * sizeof(Elf_Phdr);
@@ -701,12 +701,12 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
/* We also need an additional program header, so adjust for that. */
neededSpace += sizeof(Elf_Phdr);
debug("needed space is %d\n", neededSpace);
-
+
unsigned int neededPages = roundUp(neededSpace - startOffset, pageSize) / pageSize;
debug("needed pages is %d\n", neededPages);
if (neededPages * pageSize > firstPage)
error("virtual address space underrun!");
-
+
firstPage -= neededPages * pageSize;
startOffset += neededPages * pageSize;
@@ -718,17 +718,17 @@ void ElfFile<ElfFileParamNames>::rewriteSectionsExecutable()
Elf_Off curOff = sizeof(Elf_Ehdr) + phdrs.size() * sizeof(Elf_Phdr);
debug("clearing first %d bytes\n", startOffset - curOff);
memset(contents + curOff, 0, startOffset - curOff);
-
+
/* Write out the replaced sections. */
writeReplacedSections(curOff, firstPage, 0);
assert((off_t) curOff == neededSpace);
-
+
rewriteHeaders(firstPage + rdi(hdr->e_phoff));
}
-
+
template<ElfFileParams>
void ElfFile<ElfFileParamNames>::rewriteSections()
{
@@ -767,7 +767,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
for (unsigned int i = 0; i < phdrs.size(); ++i)
* ((Elf_Phdr *) (contents + rdi(hdr->e_phoff)) + i) = phdrs[i];
-
+
/* Rewrite the section header table. For neatness, keep the
sections sorted. */
assert(rdi(hdr->e_shnum) == shdrs.size());
@@ -775,7 +775,7 @@ void ElfFile<ElfFileParamNames>::rewriteHeaders(Elf_Addr phdrAddress)
for (unsigned int i = 1; i < rdi(hdr->e_shnum); ++i)
* ((Elf_Shdr *) (contents + rdi(hdr->e_shoff)) + i) = shdrs[i];
-
+
/* Update all those nasty virtual addresses in the .dynamic
section. Note that not all executables have .dynamic sections
(e.g., those produced by klibc's klcc). */
@@ -857,7 +857,7 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op, string newRPath)
Elf_Shdr & shdrDynamic = findSection(".dynamic");
/* !!! We assume that the virtual address in the DT_STRTAB entry
- of the dynamic section corresponds to the .dynstr section. */
+ of the dynamic section corresponds to the .dynstr section. */
Elf_Shdr & shdrDynStr = findSection(".dynstr");
char * strTab = (char *) contents + rdi(shdrDynStr.sh_offset);
@@ -869,8 +869,8 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op, string newRPath)
if (!strTabAddr) error("strange: no string table");
assert(strTabAddr == rdi(shdrDynStr.sh_addr));
-
-
+
+
/* Walk through the dynamic section, look for the RPATH/RUNPATH
entry.
@@ -891,7 +891,7 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op, string newRPath)
if (rdi(dyn->d_tag) == DT_RPATH) {
dynRPath = dyn;
/* Only use DT_RPATH if there is no DT_RUNPATH. */
- if (!dynRunPath)
+ if (!dynRunPath)
rpath = strTab + rdi(dyn->d_un.d_val);
}
else if (rdi(dyn->d_tag) == DT_RUNPATH) {
@@ -906,13 +906,13 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op, string newRPath)
printf("%s\n", rpath ? rpath : "");
return;
}
-
+
if (op == rpShrink && !rpath) {
debug("no RPATH to shrink\n");
return;
}
-
+
/* For each directory in the RPATH, check if it contains any
needed library. */
if (op == rpShrink) {
@@ -957,11 +957,11 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op, string newRPath)
}
}
-
+
if (string(rpath ? rpath : "") == newRPath) return;
changed = true;
-
+
/* Zero out the previous rpath to prevent retained dependencies in
Nix. */
unsigned int rpathSize = 0;
@@ -981,7 +981,7 @@ void ElfFile<ElfFileParamNames>::modifyRPath(RPathOp op, string newRPath)
if (forceRPath && dynRPath && dynRunPath) { /* convert DT_RUNPATH to DT_RPATH */
dynRunPath->d_tag = DT_IGNORE;
}
-
+
if (newRPath.size() <= rpathSize) {
strcpy(rpath, newRPath.c_str());
return;
@@ -1040,7 +1040,7 @@ static void patchElf2(ElfFile & elfFile, mode_t fileMode)
if (printInterpreter)
printf("%s\n", elfFile.getInterpreter().c_str());
-
+
if (newInterpreter != "")
elfFile.setInterpreter(newInterpreter);
@@ -1051,22 +1051,22 @@ static void patchElf2(ElfFile & elfFile, mode_t fileMode)
elfFile.modifyRPath(elfFile.rpShrink, "");
else if (setRPath)
elfFile.modifyRPath(elfFile.rpSet, newRPath);
-
-
+
+
if (elfFile.isChanged()){
elfFile.rewriteSections();
writeFile(fileName, fileMode);
}
}
-
+
static void patchElf()
{
if (!printInterpreter && !printRPath)
debug("patching ELF file `%s'\n", fileName.c_str());
mode_t fileMode;
-
+
readFile(fileName, &fileMode);
@@ -1075,7 +1075,7 @@ static void patchElf()
if (memcmp(contents, ELFMAG, SELFMAG) != 0)
error("not an ELF executable");
-
+
if (contents[EI_CLASS] == ELFCLASS32 &&
contents[EI_VERSION] == EV_CURRENT)
{