Closed Evian-Zhang closed 2 months ago
See if #729 meets your need. Note that even without that PR, I think you could still call Object::dynamic_relocations
and compare the relocation address to the section you are interested in. This is possibly more reliable in general, since ObjectSection::dynamic_relocations
requires sh_info
to be set, and I haven't checked if all linkers do that (it may not be required because the dynamic loader doesn't use the sections).
Thank you for your advice. #729 does solve my problem:)
Sorry for reopening this issue. I found that the solution could not solve my problem after some investigation. The GOT entries in .got section are relocated by relocation records in several sections, such as .rela.plt
and .rela.dyn
. However, only .rela.plt
relocates the GOT entry that PLT stub references. So I need to determine whether a relocation is generated by .rela.plt
, which I cannot come up with a solution.
You're doing something that is very specific to ELF, so use the lower level ELF API to read the relocations in .rela.plt
.
I guess another option would be to check the relocation type (such as R_X86_64_JUMP_SLOT
).
However, only .rela.plt relocates the GOT entry that PLT stub references.
Looking some more, I don't think that's true. .rela.plt
contains the relocations for .plt
, but there are also PLT entries in .plt.got
, and the relocations for those are mixed in with others in .rela.dyn
. So you'll have to handle those anyway.
OK, so I think this is a very specific question that isn't related to object
crate. Thank you for your advice. I will investigate more.
For anyone interested in this question, the following is my implementation of retrieving PLT stub symbols (do not rely on it for robustness).
extend_plt_symbols_for_elf
This is roughly corresponds to objdump's general implementation in this function. However, there is a more specific function to deal with PLT relocation symbols for x86_64, which is in this function. This solution needs disassembly and is able to handle more PLT categories such as .plt.sec
which is much more common in modern libc (even llvm-objdump
cannot deal with it for now), but this solution is too complex to implement by myself.
For ELF binaries, we can use
Object::dynamic_relocations
orObjectSection::relocations
to getRelocation
type. However, asObject::dynamic_relocations
are meant to collect all relocations that appeared in this binary,Object::Section::relocations
does not designed to treat specific section's data as relocations entries. Instead, it usesRelocationSections::Get
in this line to find a corresponding relocation section for given section, and parse such section for relocation entries.However, in some binaries, the
RelocationSections
is empty, while we can still get relocation entries throughObject::dynamic_relocations
. In such binaries, there is no way to get relocation entries of specific sections.Why I want this feature
I want to get name of plt entries in an ELF binary, and following discussions in gimli-rs/object#227, I found that the ddbug uses disassembly to get the corresponding got entry of one PLT stub. However, as stated in this SO,
objdump
assumes that PLT stubs and GOT entries are increasing coordinately. As a result, we can only parse the got relocation entries and then use the offset to locate corresponding PLT stubs, which is much light-weight than the disassemble-approach.