Skip to content

Add --add-debug option #367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions patchelf.1
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ option can be given multiple times.
Marks the object so that the search for dependencies of this object will ignore any
default library search paths.

.IP "--add-debug-tag"
Adds DT_DEBUG tag to the .dynamic section if not yet present in an ELF
object. A shared library (-shared) by default does not receive DT_DEBUG tag.
This means that when a shared library has an entry point (so that it
can be run as an executable), the debugger does not connect to it correctly and
symbols are not resolved.

.IP "--output FILE"
Set the output file name. If not specified, the input will be modified in place.

Expand Down
40 changes: 40 additions & 0 deletions src/patchelf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,38 @@ void ElfFile<ElfFileParamNames>::noDefaultLib()
changed = true;
}

template<ElfFileParams>
void ElfFile<ElfFileParamNames>::addDebugTag()
{
auto shdrDynamic = findSectionHeader(".dynamic");

auto dyn = (Elf_Dyn *)(fileContents->data() + rdi(shdrDynamic.sh_offset));
for ( ; rdi(dyn->d_tag) != DT_NULL; dyn++) {
if (rdi(dyn->d_tag) == DT_DEBUG) {
return;
}
}
std::string & newDynamic = replaceSection(".dynamic",
rdi(shdrDynamic.sh_size) + sizeof(Elf_Dyn));

unsigned int idx = 0;
for ( ; rdi(((Elf_Dyn *) newDynamic.c_str())[idx].d_tag) != DT_NULL; idx++) ;
debug("DT_NULL index is %d\n", idx);

/* Shift all entries down by one. */
setSubstr(newDynamic, sizeof(Elf_Dyn),
std::string(newDynamic, 0, sizeof(Elf_Dyn) * (idx + 1)));

/* Add the DT_DEBUG entry at the top. */
Elf_Dyn newDyn;
wri(newDyn.d_tag, DT_DEBUG);
newDyn.d_un.d_val = 0;
setSubstr(newDynamic, 0, std::string((char *) &newDyn, sizeof(Elf_Dyn)));

this->rewriteSections();
changed = true;
}

template<ElfFileParams>
void ElfFile<ElfFileParamNames>::clearSymbolVersions(const std::set<std::string> & syms)
{
Expand Down Expand Up @@ -1691,6 +1723,7 @@ static std::vector<std::string> allowedRpathPrefixes;
static bool removeRPath = false;
static bool setRPath = false;
static bool addRPath = false;
static bool addDebugTag = false;
static bool printRPath = false;
static std::string newRPath;
static std::set<std::string> neededLibsToRemove;
Expand Down Expand Up @@ -1737,6 +1770,9 @@ static void patchElf2(ElfFile && elfFile, const FileContents & fileContents, con
if (noDefaultLib)
elfFile.noDefaultLib();

if (addDebugTag)
elfFile.addDebugTag();

if (elfFile.isChanged()){
writeFile(fileName, elfFile.fileContents);
} else if (alwaysWrite) {
Expand Down Expand Up @@ -1793,6 +1829,7 @@ void showHelp(const std::string & progName)
[--print-needed]\n\
[--no-default-lib]\n\
[--clear-symbol-version SYMBOL]\n\
[--add-debug-tag]\n\
[--output FILE]\n\
[--debug]\n\
[--version]\n\
Expand Down Expand Up @@ -1901,6 +1938,9 @@ int mainWrapped(int argc, char * * argv)
else if (arg == "--no-default-lib") {
noDefaultLib = true;
}
else if (arg == "--add-debug-tag") {
addDebugTag = true;
}
else if (arg == "--help" || arg == "-h" ) {
showHelp(argv[0]);
return 0;
Expand Down
2 changes: 2 additions & 0 deletions src/patchelf.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ class ElfFile

void noDefaultLib();

void addDebugTag();

void clearSymbolVersions(const std::set<std::string> & syms);

private:
Expand Down
3 changes: 2 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ src_TESTS = \
set-empty-rpath.sh \
phdr-corruption.sh \
replace-needed.sh \
replace-add-needed.sh
replace-add-needed.sh \
add-debug-tag.sh

build_TESTS = \
$(no_rpath_arch_TESTS)
Expand Down
26 changes: 26 additions & 0 deletions tests/add-debug-tag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#! /bin/sh -e
SCRATCH=scratch/$(basename $0 .sh)

rm -rf ${SCRATCH}
mkdir -p ${SCRATCH}

cp libsimple.so ${SCRATCH}/

# check there is no DT_DEBUG tag
debugTag=$(readelf -d ${SCRATCH}/libsimple.so)
echo ".dynamic before: $debugTag"
if echo "$debugTag" | grep -q DEBUG; then
echo "failed --add-debug-tag test. Expected no line with (DEBUG), got: $debugTag"
exit 1
fi

# set DT_DEBUG
../src/patchelf --add-debug-tag ${SCRATCH}/libsimple.so

# check there is DT_DEBUG tag
debugTag=$(readelf -d ${SCRATCH}/libsimple.so)
echo ".dynamic before: $debugTag"
if ! echo "$debugTag" | grep -q DEBUG; then
echo "failed --add-debug-tag test. Expected line with (DEBUG), got: $debugTag"
exit 1
fi