From 0a9e84aff00ada432d58515a68b261bff23ea591 Mon Sep 17 00:00:00 2001 From: Brian Gianforcaro Date: Mon, 21 Mar 2022 00:59:45 -0700 Subject: [PATCH] readelf: Port to LibMain --- Userland/Utilities/CMakeLists.txt | 1 + Userland/Utilities/readelf.cpp | 24 +++++++++--------------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index 8b6ac85288..210ea95209 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -168,6 +168,7 @@ target_link_libraries(profile LibMain) target_link_libraries(ps LibMain) target_link_libraries(purge LibMain) target_link_libraries(pwd LibMain) +target_link_libraries(readelf LibMain) target_link_libraries(realpath LibMain) target_link_libraries(reboot LibMain) target_link_libraries(rev LibMain) diff --git a/Userland/Utilities/readelf.cpp b/Userland/Utilities/readelf.cpp index c3183af64c..dc459b564e 100644 --- a/Userland/Utilities/readelf.cpp +++ b/Userland/Utilities/readelf.cpp @@ -10,10 +10,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include @@ -224,12 +226,9 @@ static const char* object_relocation_type_to_string(ElfW(Word) type) } } -int main(int argc, char** argv) +ErrorOr serenity_main(Main::Arguments arguments) { - if (pledge("stdio rpath", nullptr) < 0) { - perror("pledge"); - return 1; - } + TRY(Core::System::pledge("stdio rpath")); const char* path; static bool display_all = false; @@ -261,11 +260,11 @@ int main(int argc, char** argv) args_parser.add_option(display_hardening, "Display security hardening info", "checksec", 'c'); args_parser.add_option(string_dump_section, "Display the contents of a section as strings", "string-dump", 'p', "section-name"); args_parser.add_positional_argument(path, "ELF path", "path"); - args_parser.parse(argc, argv); + args_parser.parse(arguments); - if (argc < 3) { - args_parser.print_usage(stderr, argv[0]); - return -1; + if (arguments.argc < 3) { + args_parser.print_usage(stderr, arguments.argv[0]); + return Error::from_errno(EINVAL); } if (display_headers) { @@ -336,12 +335,7 @@ int main(int argc, char** argv) return -1; } - int fd = open(path, O_RDONLY); - if (fd < 0) { - outln("Unable to open file {}", path); - return 1; - } - + int fd = TRY(Core::System::open(path, O_RDONLY)); auto result = ELF::DynamicLoader::try_create(fd, path); if (result.is_error()) { outln("{}", result.error().text);