Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

fzakaria
Copy link
Contributor

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

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.
@fzakaria
Copy link
Contributor Author

This is desired since I saw some pull-requests may add early returns in the codepaths.
Rather than trying to track this across all pull-requests, I thought this approach similar to what Nix does might be sensible.

Open to discussion on it :)

Comment on lines +3 to +9
#include <string>
#include <functional>
#include <vector>
#include <optional>
#include <map>
#include <set>
#include <memory>
Copy link
Contributor Author

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
Copy link
Contributor Author

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([&]() {
Copy link
Member

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([&]() {
Copy link
Member

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;
Copy link
Member

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;
Copy link
Member

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([&]() {
Copy link
Member

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([&]() {
Copy link
Member

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be dropped here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants