Skip to content

Commit 4b645c7

Browse files
committed
Add flag to zero progbits sections
1 parent e37f892 commit 4b645c7

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

patchelf.1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ Set the output file name. If not specified, the input will be modified in place
129129
.IP --debug
130130
Prints details of the changes made to the input file.
131131

132+
.IP --zero-progbits
133+
Clears all SHT_PROGBITS sections.
134+
135+
This can be helpful to generate binaries that can be shared and added to regression
136+
tests.
137+
It will strip most of the sensitive content and dangerous content.
138+
132139
.IP --version
133140
Shows the version of patchelf.
134141

src/patchelf.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,6 +1878,18 @@ void ElfFile<ElfFileParamNames>::noDefaultLib()
18781878
changed = true;
18791879
}
18801880

1881+
template<ElfFileParams>
1882+
void ElfFile<ElfFileParamNames>::zeroProgbits()
1883+
{
1884+
for (auto& shdr : shdrs)
1885+
if (rdi(shdr.sh_type) == SHT_PROGBITS)
1886+
memset(fileContents->data() + rdi(shdr.sh_offset), 0,
1887+
rdi(shdr.sh_size));
1888+
1889+
this->rewriteSections();
1890+
changed = true;
1891+
}
1892+
18811893
template<ElfFileParams>
18821894
void ElfFile<ElfFileParamNames>::addDebugTag()
18831895
{
@@ -2040,6 +2052,7 @@ static std::set<std::string> neededLibsToAdd;
20402052
static std::set<std::string> symbolsToClearVersion;
20412053
static bool printNeeded = false;
20422054
static bool noDefaultLib = false;
2055+
static bool zeroProgbits = false;
20432056
static bool printExecstack = false;
20442057
static bool clearExecstack = false;
20452058
static bool setExecstack = false;
@@ -2097,6 +2110,9 @@ static void patchElf2(ElfFile && elfFile, const FileContents & fileContents, con
20972110
if (addDebugTag)
20982111
elfFile.addDebugTag();
20992112

2113+
if (zeroProgbits)
2114+
elfFile.zeroProgbits();
2115+
21002116
if (elfFile.isChanged()){
21012117
writeFile(fileName, elfFile.fileContents);
21022118
} else if (alwaysWrite) {
@@ -2162,6 +2178,7 @@ static void showHelp(const std::string & progName)
21622178
[--set-execstack]\n\
21632179
[--output FILE]\n\
21642180
[--debug]\n\
2181+
[--zero-progbits]\t\tClear all progbits sections. Helpful to share tests with sensitive content\n\
21652182
[--version]\n\
21662183
FILENAME...\n", progName.c_str());
21672184
}
@@ -2285,6 +2302,9 @@ static int mainWrapped(int argc, char * * argv)
22852302
else if (arg == "--debug") {
22862303
debugMode = true;
22872304
}
2305+
else if (arg == "--zero-progbits") {
2306+
zeroProgbits = true;
2307+
}
22882308
else if (arg == "--no-default-lib") {
22892309
noDefaultLib = true;
22902310
}

src/patchelf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ class ElfFile
149149

150150
void modifyExecstack(ExecstackMode op);
151151

152+
void zeroProgbits();
153+
152154
private:
153155

154156
/* Convert an integer in big or little endian representation (as

0 commit comments

Comments
 (0)