Skip to content

Commit

Permalink
Support new Dynamic tags for X86_64 (#1081)
Browse files Browse the repository at this point in the history
* Add new X86_64 specific Dynamic Tag

See https://sourceware.org/git/?p=glibc.git;a=commit;h=848746e88ec2aa22e8dea25f2110e2b2c59c712e

* Add missing Dynamic Tag in C api

---------

Co-authored-by: Romain Thomas <me@romainthomas.fr>
Co-authored-by: Romain Thomas <7450402+romainthomas@users.noreply.github.com>
  • Loading branch information
3 people committed Jul 27, 2024
1 parent f954c50 commit f4bf30d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 6 deletions.
40 changes: 36 additions & 4 deletions api/c/include/LIEF/ELF/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,11 @@ enum LIEF_DYNAMIC_TAGS {
LIEF_DT_PREINIT_ARRAY = 32, /**< Pointer to array of preinit functions. */
LIEF_DT_PREINIT_ARRAYSZ = 33, /**< Size of the DT_PREINIT_ARRAY array. */

LIEF_DT_SYMTAB_SHNDX = 34, /**< Address of SYMTAB_SHNDX section */
LIEF_DT_RELRSZ = 35, /**< Total size of RELR relative relocations */
LIEF_DT_RELR = 36, /**< Address of RELR relative relocations */
LIEF_DT_RELRENT = 37, /**< Size of one RELR relative relocaction */

LIEF_DT_LOOS = 0x60000000, /**< Start of environment specific tags. */
LIEF_DT_HIOS = 0x6FFFFFFF, /**< End of environment specific tags. */
LIEF_DT_LOPROC = 0x70000000, /**< Start of processor specific tags. */
Expand Down Expand Up @@ -757,6 +762,8 @@ enum LIEF_DYNAMIC_TAGS {
LIEF_DT_MIPS_AUX_DYNAMIC = 0x70000031, /**< Address of auxiliary .dynamic. */
LIEF_DT_MIPS_PLTGOT = 0x70000032, /**< Address of the base of the PLTGOT. */
LIEF_DT_MIPS_RWPLT = 0x70000034, /**< Points to the base of a writable PLT. */
LIEF_DT_MIPS_RLD_MAP_REL = 0x70000035,
LIEF_DT_MIPS_XHASH = 0x70000036,

/* Android specific dynamic table entry tags. */
LIEF_DT_ANDROID_REL_OFFSET = 0x6000000D, /**< The offset of packed relocation data (older version < M) (Android specific. */
Expand All @@ -765,10 +772,35 @@ enum LIEF_DYNAMIC_TAGS {
LIEF_DT_ANDROID_RELSZ = 0x60000010, /**< The size of packed relocation data in bytes (Android specific. */
LIEF_DT_ANDROID_RELA = 0x60000011, /**< The offset of packed relocation data (Android specific. */
LIEF_DT_ANDROID_RELASZ = 0x60000012, /**< The size of packed relocation data in bytes (Android specific. */
LIEF_DT_RELR = 0x6FFFE000, /**< The offset of new relr relocation data (Android specific. */
LIEF_DT_RELRSZ = 0x6FFFE001, /**< The size of nre relr relocation data in bytes (Android specific. */
LIEF_DT_RELRENT = 0x6FFFE003, /**< The size of a new relr relocation entry (Android specific. */
LIEF_DT_RELRCOUNT = 0x6FFFE005 /**< Specifies the relative count of new relr relocation entries (Android specific. */
LIEF_DT_ANDROID_RELR = 0x6FFFE000, /**< The offset of new relr relocation data (Android specific. */
LIEF_DT_ANDROID_RELRSZ = 0x6FFFE001, /**< The size of nre relr relocation data in bytes (Android specific. */
LIEF_DT_ANDROID_RELRENT = 0x6FFFE003, /**< The size of a new relr relocation entry (Android specific. */
LIEF_DT_ANDROID_RELRCOUNT = 0x6FFFE005, /**< Specifies the relative count of new relr relocation entries (Android specific. */

LIEF_DT_AARCH64_BTI_PLT = 0x70000001,
LIEF_DT_AARCH64_PAC_PLT = 0x70000003,
LIEF_DT_AARCH64_VARIANT_PCS = 0x70000005,
LIEF_DT_AARCH64_MEMTAG_MODE = 0x70000009,
LIEF_DT_AARCH64_MEMTAG_HEAP = 0x7000000b,
LIEF_DT_AARCH64_MEMTAG_STACK = 0x7000000c,
LIEF_DT_AARCH64_MEMTAG_GLOBALS = 0x7000000d,
LIEF_DT_AARCH64_MEMTAG_GLOBALSSZ = 0x7000000f,

LIEF_DT_HEXAGON_SYMSZ = 0x70000000,
LIEF_DT_HEXAGON_VER = 0x70000001,
LIEF_DT_HEXAGON_PLT = 0x70000002,

LIEF_DT_PPC_GOT = 0x70000000,
LIEF_DT_PPC_OPT = 0x70000001,

LIEF_DT_PPC64_GLINK = 0x70000000,
LIEF_DT_PPC64_OPT = 0x70000003,

LIEF_DT_RISCV_VARIANT_CC = 0x70000003,

LIEF_DT_X86_64_PLT = 0x70000000,
LIEF_DT_X86_64_PLTSZ = 0x70000001,
LIEF_DT_X86_64_PLTENT = 0x70000003
};

/** DT_FLAGS and DT_FLAGS_1 values. */
Expand Down
3 changes: 3 additions & 0 deletions api/python/lief/ELF.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,9 @@ class DynamicEntry(lief.Object):
VERNEED: ClassVar[DynamicEntry.TAG] = ...
VERNEEDNUM: ClassVar[DynamicEntry.TAG] = ...
VERSYM: ClassVar[DynamicEntry.TAG] = ...
X86_64_PLT: ClassVar[DynamicEntry.TAG] = ...
X86_64_PLTSZ: ClassVar[DynamicEntry.TAG] = ...
X86_64_PLTENT: ClassVar[DynamicEntry.TAG] = ...
__name__: str
def __init__(self, *args, **kwargs) -> None: ...
@staticmethod
Expand Down
4 changes: 4 additions & 0 deletions api/python/src/ELF/objects/pyDynamicEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ void create<DynamicEntry>(nb::module_& m) {
ENTRY(PPC64_OPT)

ENTRY(RISCV_VARIANT_CC)

ENTRY(X86_64_PLT)
ENTRY(X86_64_PLTSZ)
ENTRY(X86_64_PLTENT)
;
#undef ENTRY

Expand Down
9 changes: 9 additions & 0 deletions api/rust/cargo/lief/src/elf/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ pub enum Tag {
PPC64_GLINK,
PPC64_OPT,
RISCV_VARIANT_CC,
X86_64_PLT,
X86_64_PLTSZ,
X86_64_PLTENT,
UNKNOWN(u64),
}

Expand Down Expand Up @@ -251,6 +254,9 @@ impl From<u64> for Tag {
0x570000000 => Tag::PPC64_GLINK,
0x570000003 => Tag::PPC64_OPT,
0x670000003 => Tag::RISCV_VARIANT_CC,
0x770000000 => Tag::X86_64_PLT,
0x770000001 => Tag::X86_64_PLTSZ,
0x770000003 => Tag::X86_64_PLTENT,
_ => Tag::UNKNOWN(value),

}
Expand Down Expand Up @@ -378,6 +384,9 @@ impl From<Tag> for u64 {
Tag::PPC64_GLINK => 0x570000000,
Tag::PPC64_OPT => 0x570000003,
Tag::RISCV_VARIANT_CC => 0x670000003,
Tag::X86_64_PLT => 0x770000000,
Tag::X86_64_PLTSZ => 0x770000001,
Tag::X86_64_PLTENT => 0x770000003,
Tag::UNKNOWN(value) => value,
}
}
Expand Down
1 change: 0 additions & 1 deletion doc/sphinx/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Changelog
* Add support for the ``LC_SUBCLIENT`` command: :class:`lief.MachO.SubClient`
* Add support for the ``LC_ROUTINE/LC_ROUTINE64`` command: :class:`lief.MachO.Routine`


0.15.1 - July 23th, 2024
------------------------

Expand Down
5 changes: 5 additions & 0 deletions include/LIEF/ELF/DynamicEntry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class LIEF_API DynamicEntry : public Object {
static constexpr uint64_t PPC_DISC = 0x400000000;
static constexpr uint64_t PPC64_DISC = 0x500000000;
static constexpr uint64_t RISCV_DISC = 0x600000000;
static constexpr uint64_t X86_64_DISC = 0x700000000;

enum class TAG : uint64_t {
UNKNOWN = uint64_t(-1),
Expand Down Expand Up @@ -174,6 +175,10 @@ class LIEF_API DynamicEntry : public Object {
PPC64_OPT = PPC64_DISC + 0x70000003,

RISCV_VARIANT_CC = RISCV_DISC + 0x70000003,

X86_64_PLT = X86_64_DISC + 0x70000000,
X86_64_PLTSZ = X86_64_DISC + 0x70000001,
X86_64_PLTENT = X86_64_DISC + 0x70000003,
};

static TAG from_value(uint64_t value, ARCH arch);
Expand Down
12 changes: 11 additions & 1 deletion src/ELF/DynamicEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ DynamicEntry::TAG DynamicEntry::from_value(uint64_t value, ARCH arch) {
case ARCH::RISCV:
return TAG(RISCV_DISC + value);

case ARCH::X86_64:
return TAG(X86_64_DISC + value);

default:
LIEF_WARN("Dynamic tag: 0x{:04x} is not supported for the "
"current architecture", value);
Expand Down Expand Up @@ -83,9 +86,12 @@ uint64_t DynamicEntry::to_value(DynamicEntry::TAG tag) {
return raw_value - PPC64_DISC;
}

if (RISCV_DISC <= raw_value) {
if (RISCV_DISC <= raw_value && raw_value < X86_64_DISC) {
return raw_value - RISCV_DISC;
}
if (X86_64_DISC <= raw_value) {
return raw_value - X86_64_DISC;
}

return raw_value;
}
Expand Down Expand Up @@ -237,6 +243,10 @@ const char* to_string(DynamicEntry::TAG tag) {
ENTRY(PPC64_OPT),

ENTRY(RISCV_VARIANT_CC),

ENTRY(X86_64_PLT),
ENTRY(X86_64_PLTSZ),
ENTRY(X86_64_PLTENT),
};
#undef ENTRY

Expand Down

0 comments on commit f4bf30d

Please sign in to comment.