BFD how to create relocation entries?

Josef Eisl zapster@zapster.cc
Thu Aug 1 09:36:00 GMT 2013


Hi!

I have a question regarding relocations in the BFD library. Creating 
sections and symbols is straight forward (great documentation on these 
topics, btw!) but adding relocation entries seems to be harder/not that 
well documented.

I've written a minimal example to demonstrate the problem. It is 
targeting x86_64 and elf by I guess my issues are not completely target 
dependent. In my example (source code is attached) I create a .text section
   mov 0x0(rip), %xmm0
   retq
and a .rodata section as well as symbol (foo) pointing to .text.

What I want to do is adding a relocation entry stating that the rip 
offset (.text offset 4, 32bit) should be replaced by the relative 
address of the final position of the .rodata segment (offset 0).

If my understanding of the doc is correct the howto structure is used to 
encode the sort of relocation I want to be performed so the following 
piece should do it in my case, right?

reloc_howto_type* howto = bfd_reloc_type_lookup(abfd, BFD_RELOC_32_PCREL);

The next thing is setting up arecent. What are the correct values? 
address, addend and howto are more or less clear but what about 
sym_ptr_ptr? To my understanding this should be the pointer to the 
pointer of the foo symbol in the symbol table. Can I use the table 
(entry) used for bfd_set_symtab or do I have to create a new table (e.g. 
with bfd_canonicalize_symtbl())?

According to the docs I have to install the relocation using 
bfd_install_relocation(). I've seen many discussions on that function 
and what it is supposed to do. Again, I'm confused by the parameters. 
What is data supposed to point to? I would interpret data_start as the 
offset of the data in the data section (0 in the example case). Is 
input_section the source of the relocation target, e.g. the .rodata section?

Is there still more to be done beside bfd_close(abfd) to get the 
relocation entry into the output file?

Any hints are highly appreciated. Many thanks in advance for sharing 
your insights,

Josef
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bfe-test.c
Type: text/x-csrc
Size: 2053 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20130801/7769c7a1/attachment.bin>


More information about the Binutils mailing list