Adding new architecture to libbfd

Intro

Few notes on my target, before we start. This is very simple 16bit more-or-less risc like special-purpose processor. It is not intended to be used as CPU, but rather as coprocessor for use in compression/decompression applications. Thus my target doesn't need anything fancy, such as dynamic linking. I was at first thinking of coming up with my own format, that would handle some requirements specific to this arch, such as overlays, but then thought tha it will take a while before I actually need it. If I ever get to doing that, I'll modify this guide accordingly. For now, I chose aout format, as it seemes to be simplest one to write.

To do this port I found an inital patch set for some other port ( PDP11 actually) and followed it. You can get mine if you want to look at it from here.

To add new arch to BFD, you will need to modify following files:

Files

Makefile.am

  1. Add cpu-<arch>.lo to ALL_MACHINES
  2. Add <format>-<arch>.lo to BFD32_BACKENDS
  3. Add dependancy for your cpu-xxx.lo file: cpu-<arch>.lo: cpu-<arch<.c $(INCDIR)/filenames.h
  4. Add dep for <format>-<arch>.lo. This sample is for a.out format. Elf format targets would have different deps.
    <format>-<arch>.lo: aout-<arch>.c $(INCDIR)/filenames.h libaout.h $(INCDIR)/bfdlink.h \
        aout-target.h $(INCDIR)/aout/aout64.h $(INCDIR)/aout/stab_gnu.h \
        $(INCDIR)/aout/stab.def $(INCDIR)/aout/ar.h $(INCDIR)/safe-ctype.h
    

configure.in

  1. Add your _vec declaration to list: <my_arch>_vec) tb="$tb aout-bsp.lo" ;;

config.bfd

  1. Add your _vec here as well:
    <arch>-*-*)
    	targ_defvec=<my_arch>_vec
    	;;
    

targets.c

  1. Add extern declarateion of your _vec
  2. Add pointer to it to _bfd_target_vector[] Make sure you don't stick it in #ifdef BFD64 section somewhere if it's not 64bit-only target.

archures.c

  1. Add declaration for your bfd_arch_info_type structure:
    extern const bfd_arch_info_type bfd_<arch>_arch;
  2. Add pointer to your this structure to the bfd_archures_list array.
  3. Add comments about your arch to enum in beginning of file:
    .  bfd_arch_<arch>,	{* Describtion of your arch *}
    	
    Note the dot in the beginning of line. Also note those curly braces ({}) - they are used instead of forward slash (/) for comments.

reloc.c

Add your elocs to howto manager. I don't need any, so I skip this step

cpu-<arch>.c

This file should (at minimum) contain your bfd_arch_info_type struct definition:
const bfd_arch_info_type bfd_<arch>arch =
{
    16, /* 16 bits in a word */
    8,  /* 8 bits in an address */
    8,  /* 8 bits in a byte */
    your_arch_enum_name, /* from enum in archures.c, remember? */
    0,  /* only 1 machine */
    "your_arch_name",
    "printable name",
    1,          /* aligment = 16 bit */
    true, /* the one and only */
    bfd_default_compatible,
    bfd_default_scan,
    0,
};
    
Look at cpu-mmix.c for reference.

your-arch.c

OK, this is where all the rel work gets done. However, for my case, I got away wtih simply defining BYTES_IN_WORD, BYTES_IN_LONG, ARCH_SIZE, TARGET_IS_BIG_ENDIAN_P, TARGET_PAGE_SIZE, DEFAULT_ARCH, and few other macros. last lines were #include "aoutx.h" and #include "aout-target.h"

libbfd.h, bfd-in2.h

This file is not modified directly. Instead, you have to run make headers to rgenerate is, after you are done with all other files. It is important to stress: run "make headers" in your $BUILD_DIR/bfd directory right after you run configure for the first time