Update API for MachO RPath. Related to 27575527db50d8c340d94b01e7501c22273654ee

This commit is contained in:
Romain Thomas 2018-01-18 14:41:48 +01:00
parent 09f4a4019c
commit 9d93cc5ec4
5 changed files with 56 additions and 3 deletions

View File

@ -203,6 +203,17 @@ void init_MachO_Binary_class(py::module& m) {
"Return binary's " RST_CLASS_REF(lief.MachO.ThreadCommand) " if any.",
py::return_value_policy::reference)
.def_property_readonly("has_rpath",
&Binary::has_rpath,
"``True`` if the binary has a " RST_CLASS_REF(lief.MachO.RPathCommand) " command.",
py::return_value_policy::reference_internal)
.def_property_readonly("rpath",
static_cast<no_const_getter<RPathCommand&>>(&Binary::rpath),
"Return binary's " RST_CLASS_REF(lief.MachO.RPathCommand) " if any.",
py::return_value_policy::reference)
.def("virtual_address_to_offset",
&Binary::virtual_address_to_offset,
"Convert the virtual address to an offset in the binary",

View File

@ -268,6 +268,21 @@ def print_thread_command(binary):
print("")
@exceptions_handler(Exception)
def print_rpath_command(binary):
format_str = "{:<13} {:<30}"
format_hex = "{:<13} 0x{:<28x}"
format_dec = "{:<13} {:<30d}"
print("== Rpath Command ==")
cmd = binary.rpath
print("Path: {}".format(cmd.path))
print("")
@exceptions_handler(Exception)
def print_dylinker(binary):
@ -503,6 +518,10 @@ def main():
action='store_true', dest='show_thread_command',
help="Display the 'Thread Command' command")
parser.add_argument('--rpath-command',
action='store_true', dest='show_rpath_command',
help="Display the 'Rpath Command' command")
parser.add_argument("binary",
metavar="<macho-file>",
help='Target Mach-O File')
@ -565,6 +584,9 @@ def main():
if (args.show_thread_command or args.show_all) and binary.has_thread_command:
print_thread_command(binary)
if (args.show_rpath_command or args.show_all) and binary.has_rpath:
print_rpath_command(binary)
if __name__ == "__main__":
main()

View File

@ -268,6 +268,13 @@ class DLL_PUBLIC Binary : public LIEF::Binary {
ThreadCommand& thread_command(void);
const ThreadCommand& thread_command(void) const;
//! @brief ``true`` if the binary has a MachO::RPathCommand command.
bool has_rpath(void) const;
//! @brief Return the MachO::RPathCommand command
RPathCommand& rpath(void);
const RPathCommand& rpath(void) const;
template<class T>
bool has_command(void) const;

View File

@ -755,6 +755,21 @@ const ThreadCommand& Binary::thread_command(void) const {
return this->command<ThreadCommand>();
}
// RPath command
// +++++++++++++
bool Binary::has_rpath(void) const {
return this->has_command<RPathCommand>();
}
RPathCommand& Binary::rpath(void) {
return this->command<RPathCommand>();
}
const RPathCommand& Binary::rpath(void) const {
return this->command<RPathCommand>();
}

View File

@ -64,9 +64,7 @@ class TestMachO(TestCase):
def test_rpath_cmd(self):
rpathmacho = lief.parse(get_sample('MachO/MachO64_x86-64_binary_rpathtest.bin'))
load_cmds = list(rpathmacho.commands)
rpath_cmd = load_cmds[-3]
self.assertEqual(rpath_cmd.path, "@executable_path/../lib")
self.assertEqual(rpathmacho.rpath.path, "@executable_path/../lib")
def test_relocations(self):
helloworld = lief.parse(get_sample('MachO/MachO64_x86-64_object_HelloWorld64.o'))