Skip to content

Commit

Permalink
Add new ELF DT_ enums.
Browse files Browse the repository at this point in the history
This commit resolves the `UNDEFINED` output mentioned in #954, whilst
not supporting yet the packed relocations.
  • Loading branch information
romainthomas committed Aug 27, 2023
1 parent 2ad31be commit 1ecfbf1
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 17 deletions.
6 changes: 5 additions & 1 deletion api/python/lief/ELF.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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] = ...
Expand Down Expand Up @@ -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] = ...
Expand All @@ -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] = ...
Expand Down
3 changes: 3 additions & 0 deletions api/python/lief/MachO.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 21 additions & 3 deletions api/python/lief/PE.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down Expand Up @@ -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: ...
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions api/python/src/ELF/enums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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_<ELF_SYMBOL_TYPES>(m, "SYMBOL_TYPES")
Expand Down
12 changes: 8 additions & 4 deletions include/LIEF/ELF/enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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. */
Expand Down
14 changes: 9 additions & 5 deletions src/ELF/EnumToString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions tests/elf/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 1ecfbf1

Please sign in to comment.