public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Appending to BFD
@ 2011-11-14 12:00 Paulo J. Matos
  2011-11-15 14:19 ` Paulo J. Matos
  0 siblings, 1 reply; 4+ messages in thread
From: Paulo J. Matos @ 2011-11-14 12:00 UTC (permalink / raw)
  To: binutils

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Appending to BFD
  2011-11-14 12:00 Appending to BFD Paulo J. Matos
@ 2011-11-15 14:19 ` Paulo J. Matos
  2011-11-15 22:50   ` Dave Korn
  0 siblings, 1 reply; 4+ messages in thread
From: Paulo J. Matos @ 2011-11-15 14:19 UTC (permalink / raw)
  To: binutils


Is this even possible?

On 14/11/11 11:42, Paulo J. Matos wrote:
> 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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Appending to BFD
  2011-11-15 14:19 ` Paulo J. Matos
@ 2011-11-15 22:50   ` Dave Korn
  2011-11-16 17:52     ` Paulo J. Matos
  0 siblings, 1 reply; 4+ messages in thread
From: Dave Korn @ 2011-11-15 22:50 UTC (permalink / raw)
  To: Paulo J. Matos; +Cc: binutils

On 15/11/2011 14:18, Paulo J. Matos wrote:
> 
> Is this even possible?

  I don't know, but this code ....

> bfd *
> bfd_fopen (const char *filename, const char *target, const char *mode, int fd)
> {
        [ ... snip ... ]
>   /* Figure out whether the user is opening the file for reading,
>      writing, or both, by looking at the MODE argument.  */
>   if ((mode[0] == 'r' || mode[0] == 'w' || mode[0] == 'a') 
>       && mode[1] == '+')
>     nbfd->direction = both_direction;


... suggests that it ought to be possible.  However you may need to insert
calls to bfd_make_readable/bfd_make_writable at appropriate points, and treat
it as if it was only either readable or writable but not both at any given time.

    cheers,
      DaveK


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Appending to BFD
  2011-11-15 22:50   ` Dave Korn
@ 2011-11-16 17:52     ` Paulo J. Matos
  0 siblings, 0 replies; 4+ messages in thread
From: Paulo J. Matos @ 2011-11-16 17:52 UTC (permalink / raw)
  To: binutils

On 15/11/11 22:49, Dave Korn wrote:
> On 15/11/2011 14:18, Paulo J. Matos wrote:
>>
>> Is this even possible?
>
>    I don't know, but this code ....
>
>> bfd *
>> bfd_fopen (const char *filename, const char *target, const char *mode, int fd)
>> {
>          [ ... snip ... ]
>>    /* Figure out whether the user is opening the file for reading,
>>       writing, or both, by looking at the MODE argument.  */
>>    if ((mode[0] == 'r' || mode[0] == 'w' || mode[0] == 'a')
>>        &&  mode[1] == '+')
>>      nbfd->direction = both_direction;
>
>
> ... suggests that it ought to be possible.  However you may need to insert
> calls to bfd_make_readable/bfd_make_writable at appropriate points, and treat
> it as if it was only either readable or writable but not both at any given time.
>
>      cheers,
>        DaveK
>


Unfortunately this seems impossible indeed. Opening file bfd with "w" 
resets the bfd and I can't find any sections. Opening with "r" and using 
bfd_make_writable fails because this procedure requited abfd->direction 
== no_direction. :-/

I am open to suggestions.

-- 
PMatos

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-11-16 17:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-11-14 12:00 Appending to BFD Paulo J. Matos
2011-11-15 14:19 ` Paulo J. Matos
2011-11-15 22:50   ` Dave Korn
2011-11-16 17:52     ` Paulo J. Matos

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).