diff --git a/api/python/lief/MachO.pyi b/api/python/lief/MachO.pyi index 21a95a4015..f569af6867 100644 --- a/api/python/lief/MachO.pyi +++ b/api/python/lief/MachO.pyi @@ -1516,6 +1516,8 @@ class ParserConfig: class RPathCommand(LoadCommand): path: str def __init__(self, *args, **kwargs) -> None: ... + @staticmethod + def create(path: str) -> Optional[lief.MachO.RPathCommand]: ... class Relocation(lief.Relocation): class ORIGIN: diff --git a/api/python/src/MachO/objects/pyRPathCommand.cpp b/api/python/src/MachO/objects/pyRPathCommand.cpp index 2ba62307a7..28c345efc3 100644 --- a/api/python/src/MachO/objects/pyRPathCommand.cpp +++ b/api/python/src/MachO/objects/pyRPathCommand.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include "LIEF/MachO/hash.hpp" #include "LIEF/MachO/RPathCommand.hpp" @@ -28,6 +29,11 @@ template<> void create(nb::module_& m) { nb::class_(m, "RPathCommand") + .def_static("create", &RPathCommand::create, + R"doc(Create a new RPathCommand for the provided ``path``)doc"_doc, + "path"_a + ) + .def_prop_rw("path", nb::overload_cast<>(&RPathCommand::path, nb::const_), nb::overload_cast(&RPathCommand::path), diff --git a/include/LIEF/MachO/RPathCommand.hpp b/include/LIEF/MachO/RPathCommand.hpp index 92958c11d0..10e5026f18 100644 --- a/include/LIEF/MachO/RPathCommand.hpp +++ b/include/LIEF/MachO/RPathCommand.hpp @@ -36,6 +36,7 @@ struct rpath_command; class LIEF_API RPathCommand : public LoadCommand { public: RPathCommand() = default; + RPathCommand(std::string path); RPathCommand(const details::rpath_command& rpathCmd); RPathCommand& operator=(const RPathCommand& copy) = default; @@ -45,6 +46,11 @@ class LIEF_API RPathCommand : public LoadCommand { return std::unique_ptr(new RPathCommand(*this)); } + /// Create a new RPath command for the provided `path` + static std::unique_ptr create(std::string path) { + return std::unique_ptr(new RPathCommand(std::move(path))); + } + ~RPathCommand() override = default; /// The rpath value as a string diff --git a/src/MachO/RPathCommand.cpp b/src/MachO/RPathCommand.cpp index 93329c9b48..000ca44783 100644 --- a/src/MachO/RPathCommand.cpp +++ b/src/MachO/RPathCommand.cpp @@ -13,9 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include - #include "LIEF/Visitor.hpp" +#include "LIEF/utils.hpp" #include "LIEF/MachO/RPathCommand.hpp" #include "MachO/Structures.hpp" @@ -23,6 +22,14 @@ namespace LIEF { namespace MachO { +RPathCommand::RPathCommand(std::string path) : + LoadCommand::LoadCommand(LoadCommand::TYPE::RPATH, 0), + path_(std::move(path)) +{ + size_ = align(sizeof(details::rpath_command) + path.size() + 1, sizeof(uint64_t)); + original_data_.resize(size_); +} + RPathCommand::RPathCommand(const details::rpath_command& rpath) : LoadCommand::LoadCommand{LoadCommand::TYPE(rpath.cmd), rpath.cmdsize} {}