简体   繁体   中英

Extracting the .text section of an ELF in Java

I've currently been using the linux command

objcopy -j .text -O binary sample sample.text

to obtain just the .text section of an executable.

I was wondering if there is some Java library/code to provide the same functionality.

Thanks

I don't know why your previous question (note: link only accessible to some users) was put on hold, it seemed perfectly valid to me. Maybe drop the java tag because most Java programmers just don't know what a reasonable question about ELF looks like?

Anyway, here we go again:

You need to read the ELF specification and the psABI supplement for the relevant target (some of which are collected here . It's also helpful to review the definitions in the /usr/include/elf.h header file on an ELF system.

In general, it is fairly straightforward to find the start of a named section. You need to locate the section header table with the help of the elf header (fields e_shoff , e_shentsize , e_shnum ) and parse them (the entry layout is Elf32_Sdhr or Elf64_Shdr ). To find the name of the section, you need to look at the section header table entry with the number e_shstrndx , which gives the table entry with the string table. The sh_name field in the section header is just an offset into that section, and you need to compare the null-terminated string at that location with .text .

Eclipse CDT has an ELF parser:

Maybe that's an option unless you are interested in implementing the actual ELF parsing.

If this would be of any use to anyone in the future:

Although I've implemented means to extract the .text section with the use of the Eclipse file parser, the GNU binutils Java library provides the simplest way to do so, even though I previously overlooked the library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM