From 1ecfbf1fd8c22de5252f4e8b1a2e83199941faf5 Mon Sep 17 00:00:00 2001 From: Romain Thomas Date: Sun, 27 Aug 2023 21:33:23 +0200 Subject: [PATCH] Add new ELF `DT_` enums. This commit resolves the `UNDEFINED` output mentioned in #954, whilst not supporting yet the packed relocations. --- api/python/lief/ELF.pyi | 6 +++++- api/python/lief/MachO.pyi | 3 +++ api/python/lief/PE.pyi | 24 +++++++++++++++++++++--- api/python/src/ELF/enums.cpp | 12 ++++++++---- include/LIEF/ELF/enums.hpp | 12 ++++++++---- src/ELF/EnumToString.cpp | 14 +++++++++----- tests/elf/test_parser.py | 5 +++++ 7 files changed, 59 insertions(+), 17 deletions(-) diff --git a/api/python/lief/ELF.pyi b/api/python/lief/ELF.pyi index 1155bbc796..b88547aacf 100644 --- a/api/python/lief/ELF.pyi +++ b/api/python/lief/ELF.pyi @@ -855,6 +855,10 @@ class DYNAMIC_TAGS: ANDROID_REL: ClassVar[DYNAMIC_TAGS] = ... ANDROID_RELA: ClassVar[DYNAMIC_TAGS] = ... ANDROID_RELASZ: ClassVar[DYNAMIC_TAGS] = ... + ANDROID_RELR: ClassVar[DYNAMIC_TAGS] = ... + ANDROID_RELRCOUNT: ClassVar[DYNAMIC_TAGS] = ... + ANDROID_RELRENT: ClassVar[DYNAMIC_TAGS] = ... + ANDROID_RELRSZ: ClassVar[DYNAMIC_TAGS] = ... ANDROID_RELSZ: ClassVar[DYNAMIC_TAGS] = ... ANDROID_REL_OFFSET: ClassVar[DYNAMIC_TAGS] = ... ANDROID_REL_SIZE: ClassVar[DYNAMIC_TAGS] = ... @@ -931,7 +935,6 @@ class DYNAMIC_TAGS: RELCOUNT: ClassVar[DYNAMIC_TAGS] = ... RELENT: ClassVar[DYNAMIC_TAGS] = ... RELR: ClassVar[DYNAMIC_TAGS] = ... - RELRCOUNT: ClassVar[DYNAMIC_TAGS] = ... RELRENT: ClassVar[DYNAMIC_TAGS] = ... RELRSZ: ClassVar[DYNAMIC_TAGS] = ... RELSZ: ClassVar[DYNAMIC_TAGS] = ... @@ -943,6 +946,7 @@ class DYNAMIC_TAGS: SYMBOLIC: ClassVar[DYNAMIC_TAGS] = ... SYMENT: ClassVar[DYNAMIC_TAGS] = ... SYMTAB: ClassVar[DYNAMIC_TAGS] = ... + SYMTAB_SHNDX: ClassVar[DYNAMIC_TAGS] = ... TEXTREL: ClassVar[DYNAMIC_TAGS] = ... VERDEF: ClassVar[DYNAMIC_TAGS] = ... VERDEFNUM: ClassVar[DYNAMIC_TAGS] = ... diff --git a/api/python/lief/MachO.pyi b/api/python/lief/MachO.pyi index 10d92d2b10..712895ce64 100644 --- a/api/python/lief/MachO.pyi +++ b/api/python/lief/MachO.pyi @@ -919,6 +919,9 @@ class LOAD_COMMAND_TYPES: @property def value(self) -> int: ... +class LinkEdit(SegmentCommand): + def __init__(self, *args, **kwargs) -> None: ... + class LinkerOptHint(LoadCommand): data_offset: int data_size: int diff --git a/api/python/lief/PE.pyi b/api/python/lief/PE.pyi index d26c4d2aee..a4ffabb7da 100644 --- a/api/python/lief/PE.pyi +++ b/api/python/lief/PE.pyi @@ -390,6 +390,8 @@ class Binary(lief.Binary): @property def overlay(self) -> memoryview: ... @property + def overlay_offset(self) -> int: ... + @property def relocations(self) -> lief.PE.Binary.it_relocations: ... @property def resources(self) -> lief.PE.ResourceNode: ... @@ -603,13 +605,17 @@ class CodeViewPDB(CodeView): def parent(self) -> object: ... class ContentInfo(lief.Object): + class Content(lief.Object): + def __init__(self, *args, **kwargs) -> None: ... + def copy(self, *args, **kwargs) -> Any: ... + @property + def content_type(self) -> str: ... def __init__(self, *args, **kwargs) -> None: ... + def copy(self) -> lief.PE.ContentInfo: ... @property def content_type(self) -> str: ... @property - def digest(self) -> bytes: ... - @property - def digest_algorithm(self) -> lief.PE.ALGORITHMS: ... + def value(self) -> lief.PE.ContentInfo.Content: ... class ContentType(Attribute): def __init__(self, *args, **kwargs) -> None: ... @@ -938,6 +944,9 @@ class GUARD_CF_FLAGS: @property def value(self) -> int: ... +class GenericContent(ContentInfo.Content): + def __init__(self, *args, **kwargs) -> None: ... + class GenericType(Attribute): def __init__(self, *args, **kwargs) -> None: ... @property @@ -2477,6 +2486,15 @@ class SignerInfo(lief.Object): @property def version(self) -> int: ... +class SpcIndirectData(ContentInfo.Content): + def __init__(self, *args, **kwargs) -> None: ... + @property + def digest(self) -> memoryview: ... + @property + def digest_algorithm(self) -> lief.PE.ALGORITHMS: ... + @property + def file(self) -> str: ... + class SpcSpOpusInfo(Attribute): def __init__(self, *args, **kwargs) -> None: ... @property diff --git a/api/python/src/ELF/enums.cpp b/api/python/src/ELF/enums.cpp index 6da8cff8ee..edbccdd875 100644 --- a/api/python/src/ELF/enums.cpp +++ b/api/python/src/ELF/enums.cpp @@ -387,6 +387,10 @@ void init_enums(nb::module_& m) { .value(PY_ENUM(DYNAMIC_TAGS::DT_VERDEFNUM)) .value(PY_ENUM(DYNAMIC_TAGS::DT_VERNEED)) .value(PY_ENUM(DYNAMIC_TAGS::DT_VERNEEDNUM)) + .value(PY_ENUM(DYNAMIC_TAGS::DT_SYMTAB_SHNDX)) + .value(PY_ENUM(DYNAMIC_TAGS::DT_RELRSZ)) + .value(PY_ENUM(DYNAMIC_TAGS::DT_RELR)) + .value(PY_ENUM(DYNAMIC_TAGS::DT_RELRENT)) .value(PY_ENUM(DYNAMIC_TAGS::DT_MIPS_RLD_VERSION)) .value(PY_ENUM(DYNAMIC_TAGS::DT_MIPS_TIME_STAMP)) .value(PY_ENUM(DYNAMIC_TAGS::DT_MIPS_ICHECKSUM)) @@ -438,10 +442,10 @@ void init_enums(nb::module_& m) { .value(PY_ENUM(DYNAMIC_TAGS::DT_ANDROID_RELSZ)) .value(PY_ENUM(DYNAMIC_TAGS::DT_ANDROID_RELA)) .value(PY_ENUM(DYNAMIC_TAGS::DT_ANDROID_RELASZ)) - .value(PY_ENUM(DYNAMIC_TAGS::DT_RELR)) - .value(PY_ENUM(DYNAMIC_TAGS::DT_RELRSZ)) - .value(PY_ENUM(DYNAMIC_TAGS::DT_RELRENT)) - .value(PY_ENUM(DYNAMIC_TAGS::DT_RELRCOUNT)); + .value(PY_ENUM(DYNAMIC_TAGS::DT_ANDROID_RELR)) + .value(PY_ENUM(DYNAMIC_TAGS::DT_ANDROID_RELRSZ)) + .value(PY_ENUM(DYNAMIC_TAGS::DT_ANDROID_RELRENT)) + .value(PY_ENUM(DYNAMIC_TAGS::DT_ANDROID_RELRCOUNT)); enum_(m, "SYMBOL_TYPES") diff --git a/include/LIEF/ELF/enums.hpp b/include/LIEF/ELF/enums.hpp index 49fa075380..204567a0f9 100644 --- a/include/LIEF/ELF/enums.hpp +++ b/include/LIEF/ELF/enums.hpp @@ -713,6 +713,10 @@ enum class DYNAMIC_TAGS: size_t { DT_PREINIT_ARRAY = 32, /**< Pointer to array of preinit functions. */ DT_PREINIT_ARRAYSZ = 33, /**< Size of the DT_PREINIT_ARRAY array. */ + DT_SYMTAB_SHNDX = 34, /**< Address of SYMTAB_SHNDX section */ + DT_RELRSZ = 35, /**< Total size of RELR relative relocations */ + DT_RELR = 36, /**< Address of RELR relative relocations */ + DT_RELRENT = 37, /**< Size of one RELR relative relocaction */ DT_LOOS = 0x60000000, /**< Start of environment specific tags. */ DT_HIOS = 0x6FFFFFFF, /**< End of environment specific tags. */ @@ -784,10 +788,10 @@ enum class DYNAMIC_TAGS: size_t { DT_ANDROID_RELSZ = 0x60000010, /**< The size of packed relocation data in bytes (Android specific). */ DT_ANDROID_RELA = 0x60000011, /**< The offset of packed relocation data (Android specific). */ DT_ANDROID_RELASZ = 0x60000012, /**< The size of packed relocation data in bytes (Android specific). */ - DT_RELR = 0x6FFFE000, /**< The offset of new relr relocation data (Android specific). */ - DT_RELRSZ = 0x6FFFE001, /**< The size of nre relr relocation data in bytes (Android specific). */ - DT_RELRENT = 0x6FFFE003, /**< The size of a new relr relocation entry (Android specific). */ - DT_RELRCOUNT = 0x6FFFE005 /**< Specifies the relative count of new relr relocation entries (Android specific). */ + DT_ANDROID_RELR = 0x6FFFE000, /**< The offset of new relr relocation data (Android specific). */ + DT_ANDROID_RELRSZ = 0x6FFFE001, /**< The size of nre relr relocation data in bytes (Android specific). */ + DT_ANDROID_RELRENT = 0x6FFFE003, /**< The size of a new relr relocation entry (Android specific). */ + DT_ANDROID_RELRCOUNT = 0x6FFFE005 /**< Specifies the relative count of new relr relocation entries (Android specific). */ }; /** DT_FLAGS and DT_FLAGS_1 values. */ diff --git a/src/ELF/EnumToString.cpp b/src/ELF/EnumToString.cpp index 6551bd6b70..ea4ae69203 100644 --- a/src/ELF/EnumToString.cpp +++ b/src/ELF/EnumToString.cpp @@ -275,7 +275,7 @@ const char* to_string(SEGMENT_TYPES e) { } const char* to_string(DYNAMIC_TAGS e) { - CONST_MAP(DYNAMIC_TAGS, const char*, 97) enumStrings { + CONST_MAP(DYNAMIC_TAGS, const char*, 101) enumStrings { { DYNAMIC_TAGS::DT_NULL, "NULL"}, { DYNAMIC_TAGS::DT_NEEDED, "NEEDED"}, { DYNAMIC_TAGS::DT_PLTRELSZ, "PLTRELSZ"}, @@ -319,6 +319,10 @@ const char* to_string(DYNAMIC_TAGS e) { { DYNAMIC_TAGS::DT_VERDEFNUM, "VERDEFNUM"}, { DYNAMIC_TAGS::DT_VERNEED, "VERNEED"}, { DYNAMIC_TAGS::DT_VERNEEDNUM, "VERNEEDNUM"}, + { DYNAMIC_TAGS::DT_SYMTAB_SHNDX, "SYMTAB_SHNDX"}, + { DYNAMIC_TAGS::DT_RELRSZ, "RELRSZ"}, + { DYNAMIC_TAGS::DT_RELR, "RELR"}, + { DYNAMIC_TAGS::DT_RELRENT, "RELRENT"}, { DYNAMIC_TAGS::DT_MIPS_RLD_VERSION, "MIPS_RLD_VERSION"}, { DYNAMIC_TAGS::DT_MIPS_TIME_STAMP, "MIPS_TIME_STAMP"}, { DYNAMIC_TAGS::DT_MIPS_ICHECKSUM, "MIPS_ICHECKSUM"}, @@ -370,10 +374,10 @@ const char* to_string(DYNAMIC_TAGS e) { { DYNAMIC_TAGS::DT_ANDROID_RELSZ, "ANDROID_RELSZ"}, { DYNAMIC_TAGS::DT_ANDROID_RELA, "ANDROID_RELA"}, { DYNAMIC_TAGS::DT_ANDROID_RELASZ, "ANDROID_RELASZ"}, - { DYNAMIC_TAGS::DT_RELR, "RELR"}, - { DYNAMIC_TAGS::DT_RELRSZ, "RELRSZ"}, - { DYNAMIC_TAGS::DT_RELRENT, "RELRENT"}, - { DYNAMIC_TAGS::DT_RELRCOUNT, "RELRCOUNT"} + { DYNAMIC_TAGS::DT_ANDROID_RELR, "ANDROID_RELR"}, + { DYNAMIC_TAGS::DT_ANDROID_RELRSZ, "ANDROID_RELRSZ"}, + { DYNAMIC_TAGS::DT_ANDROID_RELRENT, "ANDROID_RELRENT"}, + { DYNAMIC_TAGS::DT_ANDROID_RELRCOUNT, "ANDROID_RELRCOUNT"} }; const auto it = enumStrings.find(e); return it == enumStrings.end() ? "UNDEFINED" : it->second; diff --git a/tests/elf/test_parser.py b/tests/elf/test_parser.py index 802f3615f9..a021c21900 100644 --- a/tests/elf/test_parser.py +++ b/tests/elf/test_parser.py @@ -98,6 +98,11 @@ def test_issue_897(): assert rel2.symbol.name == "__init_array_end" assert rel2.symbol_table.name == ".symtab" +def test_issue_954(): + target = lief.ELF.parse(get_sample('ELF/main.relr.elf')) + assert target.get(lief.ELF.DYNAMIC_TAGS.RELA) is not None + assert target.get(lief.ELF.DYNAMIC_TAGS.RELRSZ) is not None + assert target.get(lief.ELF.DYNAMIC_TAGS.RELRENT) is not None def test_io(): class Wrong: