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]
Other format: [Raw text]

BFD - Mingw32 - Need experienced eyes and/or example.


Hi all.

Below is an example BFD test program abstracted from the source of Gnu
Common Lisp (GCL).  I am trying to fix GCL so that the BFD object file
loader works when built with Mingw32 gcc.

The example when compiled as follows:

gcc bfdtest.c -g -lbfd -liberty -o bfdtest.exe

crashes when it tries to execute bfd_get_relocated_section_contents () for
the first time.

I would like someone to please point me to some examples of how to use this
function on Win32/Mingw32 and/or point out the errors in initialisation
which presumably exist.

I can send the offending object module "t.o" if necessary, but it is
apparently not the problem as it can be linked and used by other means
without trouble.

Cheers

Mike Thomas.



======================= bfdtest.c =============
#include <bfd.h>
#include <bfdlink.h>
#include <stdio.h>

static boolean
madd_archive_element (struct bfd_link_info * link_info,
         bfd *abfd,
         const char *name) {

return false;

}

static boolean
mmultiple_definition (struct bfd_link_info * link_info,
        const char *name,
        bfd *obfd,
        asection *osec,
        bfd_vma oval,
        bfd *nbfd,
        asection *nsec,
        bfd_vma nval) {

return false;

}

static boolean
mmultiple_common (struct bfd_link_info * link_info,
    const char *name,
    bfd *obfd,
    enum bfd_link_hash_type otype,
    bfd_vma osize,
    bfd *nbfd,
    enum bfd_link_hash_type ntype,
    bfd_vma nsize) {

return false;

}

static boolean
madd_to_set (struct bfd_link_info * link_info,
      struct bfd_link_hash_entry *entry,
      bfd_reloc_code_real_type reloc,
      bfd *abfd, asection *sec, bfd_vma value) {

return false;

}

static  boolean
mconstructor (struct bfd_link_info * link_info,boolean constructor,
       const char *name, bfd *abfd, asection *sec,
       bfd_vma value) {

return false;

}

static boolean
mwarning (struct bfd_link_info * link_info,
   const char *warning, const char *symbol,
   bfd *abfd, asection *section,
   bfd_vma address) {

return false;

}

static boolean
mundefined_symbol (struct bfd_link_info * link_info,
     const char *name, bfd *abfd,
     asection *section,
     bfd_vma address,
     boolean fatal) {

  fprintf(stderr, "%s is undefined\n",name);
  return false;
}

static boolean
mreloc_overflow (struct bfd_link_info * link_info,
   const char *name,
   const char *reloc_name, bfd_vma addend,
   bfd *abfd, asection *section,
   bfd_vma address) {

  fprintf( stderr, "reloc for %s is overflowing\n",name);
  return false;

}

static boolean
mreloc_dangerous (struct bfd_link_info * link_info,
    const char *message,
    bfd *abfd, asection *section,
    bfd_vma address) {

  fprintf( stderr, "reloc is dangerous %s\n",message);
  return false;

}

static boolean
munattached_reloc (struct bfd_link_info * link_info,
     const char *name,
     bfd *abfd, asection *section,
     bfd_vma address) {

  fprintf(stderr, "%s is unattached\n",name);
return false;

}

static boolean
mnotice (struct bfd_link_info * link_info, const char *name,
  bfd *abfd, asection *section, bfd_vma address) {

return false;

}

int main(int argc, char **argv)
{

    bfd *b;

    unsigned u, v;

    asymbol **q;
    asection *s;

    struct bfd_link_callbacks link_callbacks;
    struct bfd_link_order link_order;
    struct bfd_link_info link_info;
#if 0
    if ( argc < 2 ) {
        fprintf ( stderr, "Need an object file argument for input\n" );
    } else {
#else

#endif
        bfd_init();

        link_callbacks.add_archive_element=madd_archive_element;
        link_callbacks.multiple_definition=mmultiple_definition;
        link_callbacks.multiple_common=mmultiple_common;
        link_callbacks.add_to_set=madd_to_set;
        link_callbacks.constructor=mconstructor;
        link_callbacks.warning=mwarning;
        link_callbacks.undefined_symbol=mundefined_symbol;
        link_callbacks.reloc_overflow=mreloc_overflow;
        link_callbacks.reloc_dangerous=mreloc_dangerous;
        link_callbacks.unattached_reloc=munattached_reloc;
        link_callbacks.notice=mnotice;

#if 0
        if ( ! ( b = bfd_openr ( argv[1], 0 ) ) ) {
#endif
        if ( ! ( b = bfd_openr ( "t.o", 0 ) ) ) {
            fprintf ( stderr, "Cannot open bfd");
        }
        if ( ! bfd_check_format ( b, bfd_object ) ) {
            fprintf ( stderr, "Unknown bfd format");
        }

        link_info.hash = bfd_link_hash_table_create (b);
        link_info.callbacks=&link_callbacks;

        link_order.type=bfd_indirect_link_order;

        for ( s = b->sections; s ; s=s->next ) {
            s->owner=b;
            s->output_section=(s->flags & SEC_ALLOC) ? s : b->sections;
            s->output_offset=0;
        }

        if ( ( u = bfd_get_symtab_upper_bound ( b ) ) < 0 ) {
            fprintf ( stderr, "Cannot get symtab upper bound");
        }

        q = (asymbol **) malloc ( u );

        if ( ( v = bfd_canonicalize_symtab ( b, q ) ) < 0 ) {
            fprintf ( stderr, "cannot canonicalize symtab");
        }

        for ( u=0; u<v; u++ ) {

            struct bfd_link_hash_entry *h;

            fprintf ( stderr, "q[%d]->name = %s\n", u, q[u]->name );
            if ( ! ( h = bfd_link_hash_lookup ( link_info.hash, q[u]->name,
false, false, true ) ) ) continue;

            if (h->type!=bfd_link_hash_defined) {
                fprintf ( stderr, "Undefined symbol");
            }
            if ( h->u.def.section ) {
                q[u]->value = h->u.def.value + h->u.def.section->vma;
                q[u]->flags |= BSF_WEAK;
            } else  {
                fprintf ( stderr, "Symbol without section");
            }
        }

        for ( s = b->sections; s; s = s->next ) {
            fprintf ( stderr, "s->name %s, s->flags = %x\n", s->name,
s->flags );
            if ( ! ( s->flags & SEC_LOAD ) ) continue;

            link_order.u.indirect.section = s;

            fprintf ( stderr, "About to get reloc section contents\n" );
            if ( ! bfd_get_relocated_section_contents (b, &link_info,
&link_order,
                                                        (void *)
s->output_section->vma, 0, q ) ) {
                fprintf ( stderr, "Cannot get relocated section
contents\n");
            }
        }

        free (q);
        bfd_close(b);
        exit (0);
#if 0
    }
#endif
}




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