This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Newbie questions
- From: Bernhard Damberger <bernied at gmail dot com>
- To: binutils at sourceware dot org
- Date: Thu, 4 Sep 2014 08:22:05 -0700
- Subject: Newbie questions
- Authentication-results: sourceware.org; auth=none
I have been trying to learn the bfd (and binutils) libraries. I
thought as an exercise I would try and create a program that would
insert nops between each instruction in an ARM (little) obj file. I
have run into some issues and have questions. Hopefully this is the
correct forum for this. If not, please let me know.
1. If I call bfd_fopen() on an elf file and then call
bfd_map_over_sections(), nothing happens (even though I know there are
sections in my file as per objdump). I did read that bfd will not load
sections into memory until required in some instances.
I figured out if I call bfd_check_format() it will force the sections
to be loaded into memory, but now the file is marked as
output_has_begun so I can't modify it.
Is there a standard way to force the sections to be loaded so you can
iterate over them (and modify)?
2. To add nop instructions in between every instruction in a code
block (ARM), this requires doubling the section size (fixed sized
instructions) and copying the modified instruction stream into the
section. (I am currently ignoring offset issues; I can deal with that
once I get this working.)
So I allocate a memory block (sized bfd_get_section_size()*2), call
bfd_get_section_contents(), iterate over its contents, and interleave
nops w/ the instructions in the new buffer. This is now the new
contents of the section that I want to substitute the old contents
for.
Calling bfd_set_section_contents fails because the sizes don't match.
Calling bfd_set_section_size() fails because output_has_begun == 1 (as
per issue #1). If I manually change output_has_begun to 0 (just seeing
what happens here), it fails in bfd_set_section_contents() I get
"`.ARM.exidx' points to removed section `.text' of foo.elf".
My question's are: can you modify sections inline? If you want to
replace the contents of a section, what are the required steps?
I did notice the docs about Section output. It mentions modifying
output_section etc. I didn't fully understand whats required of me
here.
I am thinking you can't actually modify a section in place. But rather
I have to create a new section that is a duplicate of the original,
but w/ modifications, and then delete the old section. Is that
correct? I feel like I am missing something here about sections and
how bfd models them.
3. Would a tool that converts bfd (internal structures) to json and
back be useful? My thought is it might, in that you could dump a obj
file into json, use jq (or some other tools) to manipulate it, and
then use that to create a new obj file. If I can figure out bfd, then
maybe its an itch I will scratch.
Any help greatly appreciated. Thanks.
_bernhard