This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

"Create a bfd and a section at linking time"


Hi

  I wonder what is the right way to create a bfd and its subordinate
sections at the linking time. The following is my sample code:
________________________________________________________________________
#define MY_OBJ          "foo_obj.o"
#define MY_SEC          "foo_sec"
#define SectionFlags    (SEC_HAS_CONTENTS|SEC_ALLOC|SEC_LOAD|SEC_KEEP)
#define SectionSize     4

static bfd *create_dummy_bfd()
{
   bfd       *abfd;
   asection  *sec;
   enum      bfd_architecture abfd_arch;
   long      abfd_mach;

   { // Create a bfd
     abfd = bfd_create(MY_OBJ, output_bfd);
#if 1
     bfd_make_writable(abfd);
#else
     abfd->direction = write_direction;
#endif

     bfd_set_format(abfd, bfd_object);
     abfd_arch = bfd_get_arch(output_bfd);
     abfd_mach = bfd_get_arch(output_bfd);
     bfd_default_set_arch_mach(abfd, abfd_arch, abfd_mach);
   }   { // Create a section
     char *sec_mem;
     sec = bfd_make_section_old_way(abfd, MY_SEC);
     if (!bfd_set_section_flags(abfd, sec, SectionFlags))
       abort();
     if (!bfd_set_section_size(abfd, sec, SectionSize))
       abort();
     if (!bfd_set_section_alignment (abfd, sec, 2))
       abort();

     sec_mem = (char *) xmalloc(SectionSize);
     *((int *) sec_mem) = 0x1234;

     if (!bfd_set_section_contents(abfd, sec, sec_mem, 0, SectionSize))
        abort();

   }
}
________________________________________________________________________

  The above code tries to create a section having a dummy value
'1234' within the newly created bfd. the functions is called
in function after_open_default ().

  The interesting thing is that if the bfd_make_writable() is enabled,
bfd_set_section_contents() will return false, i.e. the last abort will
be executed. Why is that?

  Did I use function bfd_set_section_contents() correctly?

  I tried to set SectionFlags including SEC_IN_MEMORY, the result
is still the same.

Thanks

-- 
Dr. Xinan Tang                    Member of Technical Staff
EMail: xinant@cognigine.com  	  Cognigine Corp.
Voice: 510.743.4930               6120 Stevenson Boulevard
Fax:   510.743.4910               Fremont, CA  94538


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]