Appending to BFD

Paulo J. Matos paulo@matos-sorge.com
Mon Nov 14 12:00:00 GMT 2011


Hello,

I am trying to open an elf file and appending a string to the 
.debug_comment section. I am doing:

void record_version_in_section(bfd *abfd, asection *sec, void *data)
{

     if(strcmp(sec->name, ".debug_comment"))
         return;

     // Get version into a buffer 
 
 

     char buf[128];
     const char *argv0 = strrchr(program_name, '/');
     if(!argv0)
         argv0 = strrchr(program_name, '\\');
     if(!argv0)
         argv0 = program_name;
     else
         argv0++;
     snprintf(buf, 128, "%s %s\n", argv0, BFD_VERSION_STRING);

     unsigned int size = bfd_get_section_size(sec);
     bfd_boolean size_ok = bfd_set_section_size(abfd, sec, size + 
strlen(buf) + 1);
     if(!size_ok)
         return;

     bfd_boolean contents_ok = bfd_set_section_contents(abfd, sec, buf, 
size, strlen(buf) + 1);
     if(!contents_ok)
     {
         // OOPS failed to set contents 
 
 

     }

}

void record_version(const std::string &file)
{
     // Open BFD and get section size 
 

     bfd *abfd = bfd_fopen(file.c_str(), NULL, "a+", -1);
     if(abfd == NULL)
     {
         std::cerr << "warning: can't open elf `" << file<< "' to write 
klink information\n";
         return;
     }

     char **matching;
     bfd_check_format_matches(abfd, bfd_object, &matching);
     bfd_map_over_sections(abfd, record_version_in_section, NULL);
     bfd_close(abfd);
}


This however fails when trying to set the new section size.

Any suggestions on how to do this?

Cheers,

-- 
PMatos



More information about the Binutils mailing list