Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting the .text section of an ELF in Java

Tags:

java

parsing

elf

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


1 Answers

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:

  • org.eclipse.cdt.utils.elf

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

like image 136
Florian Weimer Avatar answered Oct 28 '25 12:10

Florian Weimer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!