-
-
Notifications
You must be signed in to change notification settings - Fork 487
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
Introduce Finally to help track section rewrites #363
base: master
Are you sure you want to change the base?
Introduce Finally to help track section rewrites #363
Conversation
Similarly to a concept used in NixOS/nix codebase, introduce the Finally class that can be given a lambda to execute on destructor. The desired effect is to execute some code on a scope leave, in this case the method return. This is used to make sure we only apply sectionRewrite once per modification and can now support early returns in the code paths.
This is desired since I saw some pull-requests may add early returns in the codepaths. Open to discussion on it :) |
#include <string> | ||
#include <functional> | ||
#include <vector> | ||
#include <optional> | ||
#include <map> | ||
#include <set> | ||
#include <memory> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried to make this header file more "correct".
@@ -157,3 +169,15 @@ class ElfFile | |||
return (const Elf_Ehdr *)fileContents->data(); | |||
} | |||
}; | |||
|
|||
/* A trivial class to run a function at the end of a scope. */ | |||
class Finally |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
bool soNameModified = false; | ||
|
||
Finally finally([&]() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would actually prefer the old code in cases where we don't need to handle early returns
to avoid hidden control flow. This one is such a case.
} | ||
|
||
template<ElfFileParams> | ||
void ElfFile<ElfFileParamNames>::setInterpreter(const std::string & newInterpreter) | ||
{ | ||
bool interpreterChanged = false; | ||
|
||
Finally finally([&]() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one as well.
} else if (rdi(dyn->d_tag) == DT_RUNPATH) { | ||
debug("removing DT_RUNPATH entry\n"); | ||
changed = true; | ||
rpathChanged = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one seems fine.
} | ||
|
||
if (rpath && rpath == newRPath) { | ||
return; | ||
} | ||
changed = true; | ||
rpathChanged = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one seems fine.
@@ -1392,6 +1414,14 @@ void ElfFile<ElfFileParamNames>::removeNeeded(const std::set<std::string> & libs | |||
{ | |||
if (libs.empty()) return; | |||
|
|||
bool neededChanged = false; | |||
Finally finally([&]() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one seems fine, but there should be no rewriteSections
at the end.
} | ||
|
||
template<ElfFileParams> | ||
void ElfFile<ElfFileParamNames>::addNeeded(const std::set<std::string> & libs) | ||
{ | ||
if (libs.empty()) return; | ||
|
||
bool neededChanged = false; | ||
Finally finally([&]() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be dropped here.
} | ||
|
||
template<ElfFileParams> | ||
void ElfFile<ElfFileParamNames>::clearSymbolVersions(const std::set<std::string> & syms) | ||
{ | ||
if (syms.empty()) return; | ||
|
||
bool symbolVersionsChanged = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be dropped here.
Similarly to a concept used in NixOS/nix codebase, introduce the
Finally
class that can be given a lambda to execute on destructor.The desired effect is to execute some code on a scope leave, in this case the method return.
This is used to make sure we only apply
sectionRewrite
once per modification and can now support early returns in the code paths.CC @reidpr