This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
"Create a bfd and a section at linking time"
- To: binutils at sources dot redhat dot com, DJ Delorie <dj at delorie dot com>
- Subject: "Create a bfd and a section at linking time"
- From: xinant at cognigine dot com (Xinan Tang)
- Date: Fri, 08 Jun 2001 17:06:34 -0700
- Organization: Cognigine Corporation
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