0

How to retrieve a block of binary from .text section in an executable?

I know objcopy can help by using:

objcopy --only-section=.text --output-target binary a.out a.out.bin

But it would be much better if I can realize the same goal within a function call using BFD library. Is there any way to call objcopy using function calls?

zhoubot
  • 1
  • 1
  • 1
    What is your definition of "flat binary", that is, what are you going to use it for? – PlasmaHH Feb 04 '14 at 09:19
  • I'm sure this is possible with BFD, but in my quick look at it, I don't think it's a "simple" task. What objcopy does is to copy the section content out. – Mats Petersson Feb 04 '14 at 09:21
  • A flat binary is a binary file without ELF headers. I'm going to perform some static analysis on the binary, which only requires the binary block from .text section. – zhoubot Feb 05 '14 at 09:43

1 Answers1

1

You probably are looking for function in binutils / bfd libs. You can find doc at http://www.delorie.com/gnu/docs/binutils/bfd_toc.html and I think the function you are looking for is:

boolean bfd_get_section_contents (bfd *abfd, asection *section,
    PTR location, file_ptr offset,
    bfd_size_type count);

whose doc can be find at http://www.delorie.com/gnu/docs/binutils/bfd_57.html

hivert
  • 10,579
  • 3
  • 31
  • 56