Updating an object file using the BFD interface

I need to add a symbol to an existing object file. I am currently trying to use the elf32-i386 target. I tried to open the object file in read / write mode:

abfd = bfd_fopen ("test.o", "elf32-i386", "r+", -1);


I have no problem reading the existing symbol table. I compared it with the output of objdump and that's ok. But when I add a new symbol to the existing asymmetric list ** and try to set a new symbol table by calling `bfd_set_symtab (abfd, newsymtab, newsymtab_count), it fails.

Looking at the syms.c file where it is bfd_set_symtab()

defined, it seems that the bfd object created using write-only mode can set the symbol.

if (abfd->format != bfd_object || bfd_read_p (abfd))
      bfd_set_error (bfd_error_invalid_operation);
      return FALSE;

bfd_read_p (abfd) expands to : 
((abfd)->direction == read_direction || (abfd)->direction == both_direction)



"+" is both directions.

I cannot open the object file in write mode as it will destroy the existing data in the file. I was left with the only option to copy a BFD object created using read mode to another created using write mode of the new output object file. Looked at the BFD interface and I don't see any api to copy / clone an existing BFD object. There is a sample program in the BFD documentation for creating a symbol table, but it is in a new output object file. I want to update an existing object file.

Can anyone please explain how we can edit an object file using the BFD interface for any simple use.

Many thanks!


source to share

1 answer

I thought about it myself. I mentioned the objcopy utility code. The copy_object () function does the task that I wanted. It uses a lot of additional stuff based on the command line option. I had to throw away all those codes and keep only the basic copy functionality like setting up partitions, copying symbol table, copying repos, copying partition contents and personal data. I can post the code in case anyone is interested.



All Articles