Home | History | Annotate | Download | only in bfd
      1 /* AVR-specific support for 32-bit ELF
      2    Copyright (C) 1999-2014 Free Software Foundation, Inc.
      3    Contributed by Denis Chertykov <denisc (at) overta.ru>
      4 
      5    This file is part of BFD, the Binary File Descriptor library.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program; if not, write to the Free Software
     19    Foundation, Inc., 51 Franklin Street - Fifth Floor,
     20    Boston, MA 02110-1301, USA.  */
     21 
     22 #include "sysdep.h"
     23 #include "bfd.h"
     24 #include "libbfd.h"
     25 #include "elf-bfd.h"
     26 #include "elf/avr.h"
     27 #include "elf32-avr.h"
     28 
     29 /* Enable debugging printout at stdout with this variable.  */
     30 static bfd_boolean debug_relax = FALSE;
     31 
     32 /* Enable debugging printout at stdout with this variable.  */
     33 static bfd_boolean debug_stubs = FALSE;
     34 
     35 static bfd_reloc_status_type
     36 bfd_elf_avr_diff_reloc (bfd *, arelent *, asymbol *, void *,
     37 			asection *, bfd *, char **);
     38 
     39 /* Hash table initialization and handling.  Code is taken from the hppa port
     40    and adapted to the needs of AVR.  */
     41 
     42 /* We use two hash tables to hold information for linking avr objects.
     43 
     44    The first is the elf32_avr_link_hash_table which is derived from the
     45    stanard ELF linker hash table.  We use this as a place to attach the other
     46    hash table and some static information.
     47 
     48    The second is the stub hash table which is derived from the base BFD
     49    hash table.  The stub hash table holds the information on the linker
     50    stubs.  */
     51 
     52 struct elf32_avr_stub_hash_entry
     53 {
     54   /* Base hash table entry structure.  */
     55   struct bfd_hash_entry bh_root;
     56 
     57   /* Offset within stub_sec of the beginning of this stub.  */
     58   bfd_vma stub_offset;
     59 
     60   /* Given the symbol's value and its section we can determine its final
     61      value when building the stubs (so the stub knows where to jump).  */
     62   bfd_vma target_value;
     63 
     64   /* This way we could mark stubs to be no longer necessary.  */
     65   bfd_boolean is_actually_needed;
     66 };
     67 
     68 struct elf32_avr_link_hash_table
     69 {
     70   /* The main hash table.  */
     71   struct elf_link_hash_table etab;
     72 
     73   /* The stub hash table.  */
     74   struct bfd_hash_table bstab;
     75 
     76   bfd_boolean no_stubs;
     77 
     78   /* Linker stub bfd.  */
     79   bfd *stub_bfd;
     80 
     81   /* The stub section.  */
     82   asection *stub_sec;
     83 
     84   /* Usually 0, unless we are generating code for a bootloader.  Will
     85      be initialized by elf32_avr_size_stubs to the vma offset of the
     86      output section associated with the stub section.  */
     87   bfd_vma vector_base;
     88 
     89   /* Assorted information used by elf32_avr_size_stubs.  */
     90   unsigned int        bfd_count;
     91   int                 top_index;
     92   asection **         input_list;
     93   Elf_Internal_Sym ** all_local_syms;
     94 
     95   /* Tables for mapping vma beyond the 128k boundary to the address of the
     96      corresponding stub.  (AMT)
     97      "amt_max_entry_cnt" reflects the number of entries that memory is allocated
     98      for in the "amt_stub_offsets" and "amt_destination_addr" arrays.
     99      "amt_entry_cnt" informs how many of these entries actually contain
    100      useful data.  */
    101   unsigned int amt_entry_cnt;
    102   unsigned int amt_max_entry_cnt;
    103   bfd_vma *    amt_stub_offsets;
    104   bfd_vma *    amt_destination_addr;
    105 };
    106 
    107 /* Various hash macros and functions.  */
    108 #define avr_link_hash_table(p) \
    109   /* PR 3874: Check that we have an AVR style hash table before using it.  */\
    110   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
    111   == AVR_ELF_DATA ? ((struct elf32_avr_link_hash_table *) ((p)->hash)) : NULL)
    112 
    113 #define avr_stub_hash_entry(ent) \
    114   ((struct elf32_avr_stub_hash_entry *)(ent))
    115 
    116 #define avr_stub_hash_lookup(table, string, create, copy) \
    117   ((struct elf32_avr_stub_hash_entry *) \
    118    bfd_hash_lookup ((table), (string), (create), (copy)))
    119 
    120 static reloc_howto_type elf_avr_howto_table[] =
    121 {
    122   HOWTO (R_AVR_NONE,		/* type */
    123 	 0,			/* rightshift */
    124 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    125 	 32,			/* bitsize */
    126 	 FALSE,			/* pc_relative */
    127 	 0,			/* bitpos */
    128 	 complain_overflow_bitfield, /* complain_on_overflow */
    129 	 bfd_elf_generic_reloc,	/* special_function */
    130 	 "R_AVR_NONE",		/* name */
    131 	 FALSE,			/* partial_inplace */
    132 	 0,			/* src_mask */
    133 	 0,			/* dst_mask */
    134 	 FALSE),		/* pcrel_offset */
    135 
    136   HOWTO (R_AVR_32,		/* type */
    137 	 0,			/* rightshift */
    138 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    139 	 32,			/* bitsize */
    140 	 FALSE,			/* pc_relative */
    141 	 0,			/* bitpos */
    142 	 complain_overflow_bitfield, /* complain_on_overflow */
    143 	 bfd_elf_generic_reloc,	/* special_function */
    144 	 "R_AVR_32",		/* name */
    145 	 FALSE,			/* partial_inplace */
    146 	 0xffffffff,		/* src_mask */
    147 	 0xffffffff,		/* dst_mask */
    148 	 FALSE),		/* pcrel_offset */
    149 
    150   /* A 7 bit PC relative relocation.  */
    151   HOWTO (R_AVR_7_PCREL,		/* type */
    152 	 1,			/* rightshift */
    153 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    154 	 7,			/* bitsize */
    155 	 TRUE,			/* pc_relative */
    156 	 3,			/* bitpos */
    157 	 complain_overflow_bitfield, /* complain_on_overflow */
    158 	 bfd_elf_generic_reloc, /* special_function */
    159 	 "R_AVR_7_PCREL",	/* name */
    160 	 FALSE,			/* partial_inplace */
    161 	 0xffff,		/* src_mask */
    162 	 0xffff,		/* dst_mask */
    163 	 TRUE),			/* pcrel_offset */
    164 
    165   /* A 13 bit PC relative relocation.  */
    166   HOWTO (R_AVR_13_PCREL,	/* type */
    167 	 1,			/* rightshift */
    168 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    169 	 13,			/* bitsize */
    170 	 TRUE,			/* pc_relative */
    171 	 0,			/* bitpos */
    172 	 complain_overflow_bitfield, /* complain_on_overflow */
    173 	 bfd_elf_generic_reloc, /* special_function */
    174 	 "R_AVR_13_PCREL",	/* name */
    175 	 FALSE,			/* partial_inplace */
    176 	 0xfff,			/* src_mask */
    177 	 0xfff,			/* dst_mask */
    178 	 TRUE),			/* pcrel_offset */
    179 
    180   /* A 16 bit absolute relocation.  */
    181   HOWTO (R_AVR_16,		/* type */
    182 	 0,			/* rightshift */
    183 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    184 	 16,			/* bitsize */
    185 	 FALSE,			/* pc_relative */
    186 	 0,			/* bitpos */
    187 	 complain_overflow_dont, /* complain_on_overflow */
    188 	 bfd_elf_generic_reloc,	/* special_function */
    189 	 "R_AVR_16",		/* name */
    190 	 FALSE,			/* partial_inplace */
    191 	 0xffff,		/* src_mask */
    192 	 0xffff,		/* dst_mask */
    193 	 FALSE),		/* pcrel_offset */
    194 
    195   /* A 16 bit absolute relocation for command address
    196      Will be changed when linker stubs are needed.  */
    197   HOWTO (R_AVR_16_PM,		/* type */
    198 	 1,			/* rightshift */
    199 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    200 	 16,			/* bitsize */
    201 	 FALSE,			/* pc_relative */
    202 	 0,			/* bitpos */
    203 	 complain_overflow_bitfield, /* complain_on_overflow */
    204 	 bfd_elf_generic_reloc,	/* special_function */
    205 	 "R_AVR_16_PM",		/* name */
    206 	 FALSE,			/* partial_inplace */
    207 	 0xffff,		/* src_mask */
    208 	 0xffff,		/* dst_mask */
    209 	 FALSE),		/* pcrel_offset */
    210   /* A low 8 bit absolute relocation of 16 bit address.
    211      For LDI command.  */
    212   HOWTO (R_AVR_LO8_LDI,		/* type */
    213 	 0,			/* rightshift */
    214 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    215 	 8,			/* bitsize */
    216 	 FALSE,			/* pc_relative */
    217 	 0,			/* bitpos */
    218 	 complain_overflow_dont, /* complain_on_overflow */
    219 	 bfd_elf_generic_reloc,	/* special_function */
    220 	 "R_AVR_LO8_LDI",	/* name */
    221 	 FALSE,			/* partial_inplace */
    222 	 0xffff,		/* src_mask */
    223 	 0xffff,		/* dst_mask */
    224 	 FALSE),		/* pcrel_offset */
    225   /* A high 8 bit absolute relocation of 16 bit address.
    226      For LDI command.  */
    227   HOWTO (R_AVR_HI8_LDI,		/* type */
    228 	 8,			/* rightshift */
    229 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    230 	 8,			/* bitsize */
    231 	 FALSE,			/* pc_relative */
    232 	 0,			/* bitpos */
    233 	 complain_overflow_dont, /* complain_on_overflow */
    234 	 bfd_elf_generic_reloc,	/* special_function */
    235 	 "R_AVR_HI8_LDI",	/* name */
    236 	 FALSE,			/* partial_inplace */
    237 	 0xffff,		/* src_mask */
    238 	 0xffff,		/* dst_mask */
    239 	 FALSE),		/* pcrel_offset */
    240   /* A high 6 bit absolute relocation of 22 bit address.
    241      For LDI command.  As well second most significant 8 bit value of
    242      a 32 bit link-time constant.  */
    243   HOWTO (R_AVR_HH8_LDI,		/* type */
    244 	 16,			/* rightshift */
    245 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    246 	 8,			/* bitsize */
    247 	 FALSE,			/* pc_relative */
    248 	 0,			/* bitpos */
    249 	 complain_overflow_dont, /* complain_on_overflow */
    250 	 bfd_elf_generic_reloc,	/* special_function */
    251 	 "R_AVR_HH8_LDI",	/* name */
    252 	 FALSE,			/* partial_inplace */
    253 	 0xffff,		/* src_mask */
    254 	 0xffff,		/* dst_mask */
    255 	 FALSE),		/* pcrel_offset */
    256   /* A negative low 8 bit absolute relocation of 16 bit address.
    257      For LDI command.  */
    258   HOWTO (R_AVR_LO8_LDI_NEG,	/* type */
    259 	 0,			/* rightshift */
    260 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    261 	 8,			/* bitsize */
    262 	 FALSE,			/* pc_relative */
    263 	 0,			/* bitpos */
    264 	 complain_overflow_dont, /* complain_on_overflow */
    265 	 bfd_elf_generic_reloc,	/* special_function */
    266 	 "R_AVR_LO8_LDI_NEG",	/* name */
    267 	 FALSE,			/* partial_inplace */
    268 	 0xffff,		/* src_mask */
    269 	 0xffff,		/* dst_mask */
    270 	 FALSE),		/* pcrel_offset */
    271   /* A negative high 8 bit absolute relocation of 16 bit address.
    272      For LDI command.  */
    273   HOWTO (R_AVR_HI8_LDI_NEG,	/* type */
    274 	 8,			/* rightshift */
    275 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    276 	 8,			/* bitsize */
    277 	 FALSE,			/* pc_relative */
    278 	 0,			/* bitpos */
    279 	 complain_overflow_dont, /* complain_on_overflow */
    280 	 bfd_elf_generic_reloc,	/* special_function */
    281 	 "R_AVR_HI8_LDI_NEG",	/* name */
    282 	 FALSE,			/* partial_inplace */
    283 	 0xffff,		/* src_mask */
    284 	 0xffff,		/* dst_mask */
    285 	 FALSE),		/* pcrel_offset */
    286   /* A negative high 6 bit absolute relocation of 22 bit address.
    287      For LDI command.  */
    288   HOWTO (R_AVR_HH8_LDI_NEG,	/* type */
    289 	 16,			/* rightshift */
    290 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    291 	 8,			/* bitsize */
    292 	 FALSE,			/* pc_relative */
    293 	 0,			/* bitpos */
    294 	 complain_overflow_dont, /* complain_on_overflow */
    295 	 bfd_elf_generic_reloc,	/* special_function */
    296 	 "R_AVR_HH8_LDI_NEG",	/* name */
    297 	 FALSE,			/* partial_inplace */
    298 	 0xffff,		/* src_mask */
    299 	 0xffff,		/* dst_mask */
    300 	 FALSE),		/* pcrel_offset */
    301   /* A low 8 bit absolute relocation of 24 bit program memory address.
    302      For LDI command.  Will not be changed when linker stubs are needed. */
    303   HOWTO (R_AVR_LO8_LDI_PM,	/* type */
    304 	 1,			/* rightshift */
    305 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    306 	 8,			/* bitsize */
    307 	 FALSE,			/* pc_relative */
    308 	 0,			/* bitpos */
    309 	 complain_overflow_dont, /* complain_on_overflow */
    310 	 bfd_elf_generic_reloc,	/* special_function */
    311 	 "R_AVR_LO8_LDI_PM",	/* name */
    312 	 FALSE,			/* partial_inplace */
    313 	 0xffff,		/* src_mask */
    314 	 0xffff,		/* dst_mask */
    315 	 FALSE),		/* pcrel_offset */
    316   /* A low 8 bit absolute relocation of 24 bit program memory address.
    317      For LDI command.  Will not be changed when linker stubs are needed. */
    318   HOWTO (R_AVR_HI8_LDI_PM,	/* type */
    319 	 9,			/* rightshift */
    320 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    321 	 8,			/* bitsize */
    322 	 FALSE,			/* pc_relative */
    323 	 0,			/* bitpos */
    324 	 complain_overflow_dont, /* complain_on_overflow */
    325 	 bfd_elf_generic_reloc,	/* special_function */
    326 	 "R_AVR_HI8_LDI_PM",	/* name */
    327 	 FALSE,			/* partial_inplace */
    328 	 0xffff,		/* src_mask */
    329 	 0xffff,		/* dst_mask */
    330 	 FALSE),		/* pcrel_offset */
    331   /* A low 8 bit absolute relocation of 24 bit program memory address.
    332      For LDI command.  Will not be changed when linker stubs are needed. */
    333   HOWTO (R_AVR_HH8_LDI_PM,	/* type */
    334 	 17,			/* rightshift */
    335 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    336 	 8,			/* bitsize */
    337 	 FALSE,			/* pc_relative */
    338 	 0,			/* bitpos */
    339 	 complain_overflow_dont, /* complain_on_overflow */
    340 	 bfd_elf_generic_reloc,	/* special_function */
    341 	 "R_AVR_HH8_LDI_PM",	/* name */
    342 	 FALSE,			/* partial_inplace */
    343 	 0xffff,		/* src_mask */
    344 	 0xffff,		/* dst_mask */
    345 	 FALSE),		/* pcrel_offset */
    346   /* A low 8 bit absolute relocation of 24 bit program memory address.
    347      For LDI command.  Will not be changed when linker stubs are needed. */
    348   HOWTO (R_AVR_LO8_LDI_PM_NEG,	/* type */
    349 	 1,			/* rightshift */
    350 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    351 	 8,			/* bitsize */
    352 	 FALSE,			/* pc_relative */
    353 	 0,			/* bitpos */
    354 	 complain_overflow_dont, /* complain_on_overflow */
    355 	 bfd_elf_generic_reloc,	/* special_function */
    356 	 "R_AVR_LO8_LDI_PM_NEG", /* name */
    357 	 FALSE,			/* partial_inplace */
    358 	 0xffff,		/* src_mask */
    359 	 0xffff,		/* dst_mask */
    360 	 FALSE),		/* pcrel_offset */
    361   /* A low 8 bit absolute relocation of 24 bit program memory address.
    362      For LDI command.  Will not be changed when linker stubs are needed. */
    363   HOWTO (R_AVR_HI8_LDI_PM_NEG,	/* type */
    364 	 9,			/* rightshift */
    365 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    366 	 8,			/* bitsize */
    367 	 FALSE,			/* pc_relative */
    368 	 0,			/* bitpos */
    369 	 complain_overflow_dont, /* complain_on_overflow */
    370 	 bfd_elf_generic_reloc,	/* special_function */
    371 	 "R_AVR_HI8_LDI_PM_NEG", /* name */
    372 	 FALSE,			/* partial_inplace */
    373 	 0xffff,		/* src_mask */
    374 	 0xffff,		/* dst_mask */
    375 	 FALSE),		/* pcrel_offset */
    376   /* A low 8 bit absolute relocation of 24 bit program memory address.
    377      For LDI command.  Will not be changed when linker stubs are needed. */
    378   HOWTO (R_AVR_HH8_LDI_PM_NEG,	/* type */
    379 	 17,			/* rightshift */
    380 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    381 	 8,			/* bitsize */
    382 	 FALSE,			/* pc_relative */
    383 	 0,			/* bitpos */
    384 	 complain_overflow_dont, /* complain_on_overflow */
    385 	 bfd_elf_generic_reloc,	/* special_function */
    386 	 "R_AVR_HH8_LDI_PM_NEG", /* name */
    387 	 FALSE,			/* partial_inplace */
    388 	 0xffff,		/* src_mask */
    389 	 0xffff,		/* dst_mask */
    390 	 FALSE),		/* pcrel_offset */
    391   /* Relocation for CALL command in ATmega.  */
    392   HOWTO (R_AVR_CALL,		/* type */
    393 	 1,			/* rightshift */
    394 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    395 	 23,			/* bitsize */
    396 	 FALSE,			/* pc_relative */
    397 	 0,			/* bitpos */
    398 	 complain_overflow_dont,/* complain_on_overflow */
    399 	 bfd_elf_generic_reloc,	/* special_function */
    400 	 "R_AVR_CALL",		/* name */
    401 	 FALSE,			/* partial_inplace */
    402 	 0xffffffff,		/* src_mask */
    403 	 0xffffffff,		/* dst_mask */
    404 	 FALSE),			/* pcrel_offset */
    405   /* A 16 bit absolute relocation of 16 bit address.
    406      For LDI command.  */
    407   HOWTO (R_AVR_LDI,		/* type */
    408 	 0,			/* rightshift */
    409 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    410 	 16,			/* bitsize */
    411 	 FALSE,			/* pc_relative */
    412 	 0,			/* bitpos */
    413 	 complain_overflow_dont,/* complain_on_overflow */
    414 	 bfd_elf_generic_reloc,	/* special_function */
    415 	 "R_AVR_LDI",		/* name */
    416 	 FALSE,			/* partial_inplace */
    417 	 0xffff,		/* src_mask */
    418 	 0xffff,		/* dst_mask */
    419 	 FALSE),		/* pcrel_offset */
    420   /* A 6 bit absolute relocation of 6 bit offset.
    421      For ldd/sdd command.  */
    422   HOWTO (R_AVR_6,		/* type */
    423 	 0,			/* rightshift */
    424 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    425 	 6,			/* bitsize */
    426 	 FALSE,			/* pc_relative */
    427 	 0,			/* bitpos */
    428 	 complain_overflow_dont,/* complain_on_overflow */
    429 	 bfd_elf_generic_reloc,	/* special_function */
    430 	 "R_AVR_6",		/* name */
    431 	 FALSE,			/* partial_inplace */
    432 	 0xffff,		/* src_mask */
    433 	 0xffff,		/* dst_mask */
    434 	 FALSE),		/* pcrel_offset */
    435   /* A 6 bit absolute relocation of 6 bit offset.
    436      For sbiw/adiw command.  */
    437   HOWTO (R_AVR_6_ADIW,		/* type */
    438 	 0,			/* rightshift */
    439 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    440 	 6,			/* bitsize */
    441 	 FALSE,			/* pc_relative */
    442 	 0,			/* bitpos */
    443 	 complain_overflow_dont,/* complain_on_overflow */
    444 	 bfd_elf_generic_reloc,	/* special_function */
    445 	 "R_AVR_6_ADIW",	/* name */
    446 	 FALSE,			/* partial_inplace */
    447 	 0xffff,		/* src_mask */
    448 	 0xffff,		/* dst_mask */
    449 	 FALSE),		/* pcrel_offset */
    450   /* Most significant 8 bit value of a 32 bit link-time constant.  */
    451   HOWTO (R_AVR_MS8_LDI,		/* type */
    452 	 24,			/* rightshift */
    453 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    454 	 8,			/* bitsize */
    455 	 FALSE,			/* pc_relative */
    456 	 0,			/* bitpos */
    457 	 complain_overflow_dont, /* complain_on_overflow */
    458 	 bfd_elf_generic_reloc,	/* special_function */
    459 	 "R_AVR_MS8_LDI",	/* name */
    460 	 FALSE,			/* partial_inplace */
    461 	 0xffff,		/* src_mask */
    462 	 0xffff,		/* dst_mask */
    463 	 FALSE),		/* pcrel_offset */
    464   /* Negative most significant 8 bit value of a 32 bit link-time constant.  */
    465   HOWTO (R_AVR_MS8_LDI_NEG,	/* type */
    466 	 24,			/* rightshift */
    467 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    468 	 8,			/* bitsize */
    469 	 FALSE,			/* pc_relative */
    470 	 0,			/* bitpos */
    471 	 complain_overflow_dont, /* complain_on_overflow */
    472 	 bfd_elf_generic_reloc,	/* special_function */
    473 	 "R_AVR_MS8_LDI_NEG",	/* name */
    474 	 FALSE,			/* partial_inplace */
    475 	 0xffff,		/* src_mask */
    476 	 0xffff,		/* dst_mask */
    477 	 FALSE), 		/* pcrel_offset */
    478   /* A low 8 bit absolute relocation of 24 bit program memory address.
    479      For LDI command.  Will be changed when linker stubs are needed.  */
    480   HOWTO (R_AVR_LO8_LDI_GS,      /* type */
    481          1,                     /* rightshift */
    482          1,                     /* size (0 = byte, 1 = short, 2 = long) */
    483          8,                     /* bitsize */
    484          FALSE,                 /* pc_relative */
    485          0,                     /* bitpos */
    486          complain_overflow_dont, /* complain_on_overflow */
    487          bfd_elf_generic_reloc, /* special_function */
    488          "R_AVR_LO8_LDI_GS",    /* name */
    489          FALSE,                 /* partial_inplace */
    490          0xffff,                /* src_mask */
    491          0xffff,                /* dst_mask */
    492          FALSE),                /* pcrel_offset */
    493   /* A low 8 bit absolute relocation of 24 bit program memory address.
    494      For LDI command.  Will be changed when linker stubs are needed.  */
    495   HOWTO (R_AVR_HI8_LDI_GS,      /* type */
    496          9,                     /* rightshift */
    497          1,                     /* size (0 = byte, 1 = short, 2 = long) */
    498          8,                     /* bitsize */
    499          FALSE,                 /* pc_relative */
    500          0,                     /* bitpos */
    501          complain_overflow_dont, /* complain_on_overflow */
    502          bfd_elf_generic_reloc, /* special_function */
    503          "R_AVR_HI8_LDI_GS",    /* name */
    504          FALSE,                 /* partial_inplace */
    505          0xffff,                /* src_mask */
    506          0xffff,                /* dst_mask */
    507          FALSE),                /* pcrel_offset */
    508   /* 8 bit offset.  */
    509   HOWTO (R_AVR_8,		/* type */
    510 	 0,			/* rightshift */
    511 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    512 	 8,			/* bitsize */
    513 	 FALSE,			/* pc_relative */
    514 	 0,			/* bitpos */
    515 	 complain_overflow_bitfield,/* complain_on_overflow */
    516 	 bfd_elf_generic_reloc,	/* special_function */
    517 	 "R_AVR_8",		/* name */
    518 	 FALSE,			/* partial_inplace */
    519 	 0x000000ff,		/* src_mask */
    520 	 0x000000ff,		/* dst_mask */
    521 	 FALSE),		/* pcrel_offset */
    522   /* lo8-part to use in  .byte lo8(sym).  */
    523   HOWTO (R_AVR_8_LO8,		/* type */
    524 	 0,			/* rightshift */
    525 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    526 	 8,			/* bitsize */
    527 	 FALSE,			/* pc_relative */
    528 	 0,			/* bitpos */
    529 	 complain_overflow_dont,/* complain_on_overflow */
    530 	 bfd_elf_generic_reloc,	/* special_function */
    531 	 "R_AVR_8_LO8",		/* name */
    532 	 FALSE,			/* partial_inplace */
    533 	 0xffffff,		/* src_mask */
    534 	 0xffffff,		/* dst_mask */
    535 	 FALSE),		/* pcrel_offset */
    536   /* hi8-part to use in  .byte hi8(sym).  */
    537   HOWTO (R_AVR_8_HI8,		/* type */
    538 	 8,			/* rightshift */
    539 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    540 	 8,			/* bitsize */
    541 	 FALSE,			/* pc_relative */
    542 	 0,			/* bitpos */
    543 	 complain_overflow_dont,/* complain_on_overflow */
    544 	 bfd_elf_generic_reloc,	/* special_function */
    545 	 "R_AVR_8_HI8",		/* name */
    546 	 FALSE,			/* partial_inplace */
    547 	 0xffffff,		/* src_mask */
    548 	 0xffffff,		/* dst_mask */
    549 	 FALSE),		/* pcrel_offset */
    550   /* hlo8-part to use in  .byte hlo8(sym).  */
    551   HOWTO (R_AVR_8_HLO8,		/* type */
    552 	 16,			/* rightshift */
    553 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    554 	 8,			/* bitsize */
    555 	 FALSE,			/* pc_relative */
    556 	 0,			/* bitpos */
    557 	 complain_overflow_dont,/* complain_on_overflow */
    558 	 bfd_elf_generic_reloc,	/* special_function */
    559 	 "R_AVR_8_HLO8",	/* name */
    560 	 FALSE,			/* partial_inplace */
    561 	 0xffffff,		/* src_mask */
    562 	 0xffffff,		/* dst_mask */
    563 	 FALSE),		/* pcrel_offset */
    564   HOWTO (R_AVR_DIFF8,		/* type */
    565 	 0,             	/* rightshift */
    566 	 0, 			/* size (0 = byte, 1 = short, 2 = long) */
    567 	 8, 			/* bitsize */
    568 	 FALSE,         	/* pc_relative */
    569 	 0,             	/* bitpos */
    570 	 complain_overflow_bitfield, /* complain_on_overflow */
    571 	 bfd_elf_avr_diff_reloc, /* special_function */
    572 	 "R_AVR_DIFF8",     	/* name */
    573 	 FALSE,         	/* partial_inplace */
    574 	 0,             	/* src_mask */
    575 	 0xff,          	/* dst_mask */
    576 	 FALSE),        	/* pcrel_offset */
    577   HOWTO (R_AVR_DIFF16,  	/* type */
    578 	 0,             	/* rightshift */
    579 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    580 	 16,			/* bitsize */
    581 	 FALSE,         	/* pc_relative */
    582 	 0,             	/* bitpos */
    583 	 complain_overflow_bitfield, /* complain_on_overflow */
    584 	 bfd_elf_avr_diff_reloc,/* special_function */
    585 	 "R_AVR_DIFF16",     	/* name */
    586 	 FALSE,         	/* partial_inplace */
    587 	 0,             	/* src_mask */
    588 	 0xffff,        	/* dst_mask */
    589 	 FALSE),        	/* pcrel_offset */
    590   HOWTO (R_AVR_DIFF32,  	/* type */
    591 	 0,             	/* rightshift */
    592 	 2,         		/* size (0 = byte, 1 = short, 2 = long) */
    593 	 32,        		/* bitsize */
    594 	 FALSE,         	/* pc_relative */
    595 	 0,             	/* bitpos */
    596 	 complain_overflow_bitfield, /* complain_on_overflow */
    597 	 bfd_elf_avr_diff_reloc,/* special_function */
    598 	 "R_AVR_DIFF32",     	/* name */
    599 	 FALSE,         	/* partial_inplace */
    600 	 0,             	/* src_mask */
    601 	 0xffffffff,    	/* dst_mask */
    602 	 FALSE),        	/* pcrel_offset */
    603   /* 7 bit immediate for LDS/STS in Tiny core.  */
    604   HOWTO (R_AVR_LDS_STS_16,  /* type */
    605 	 0,                     /* rightshift */
    606 	 1,                     /* size (0 = byte, 1 = short, 2 = long) */
    607 	 7,                     /* bitsize */
    608 	 FALSE,                 /* pc_relative */
    609 	 0,                     /* bitpos */
    610 	 complain_overflow_dont,/* complain_on_overflow */
    611 	 bfd_elf_generic_reloc, /* special_function */
    612 	 "R_AVR_LDS_STS_16",    /* name */
    613 	 FALSE,                 /* partial_inplace */
    614 	 0xffff,                /* src_mask */
    615 	 0xffff,                /* dst_mask */
    616 	 FALSE),		/* pcrel_offset */
    617 
    618   HOWTO (R_AVR_PORT6,		/* type */
    619 	 0,			/* rightshift */
    620 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    621 	 6,			/* bitsize */
    622 	 FALSE,			/* pc_relative */
    623 	 0,			/* bitpos */
    624 	 complain_overflow_dont,/* complain_on_overflow */
    625 	 bfd_elf_generic_reloc,	/* special_function */
    626 	 "R_AVR_PORT6",		/* name */
    627 	 FALSE,			/* partial_inplace */
    628 	 0xffffff,		/* src_mask */
    629 	 0xffffff,		/* dst_mask */
    630 	 FALSE),		/* pcrel_offset */
    631   HOWTO (R_AVR_PORT5,		/* type */
    632 	 0,			/* rightshift */
    633 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    634 	 5,			/* bitsize */
    635 	 FALSE,			/* pc_relative */
    636 	 0,			/* bitpos */
    637 	 complain_overflow_dont,/* complain_on_overflow */
    638 	 bfd_elf_generic_reloc,	/* special_function */
    639 	 "R_AVR_PORT5",		/* name */
    640 	 FALSE,			/* partial_inplace */
    641 	 0xffffff,		/* src_mask */
    642 	 0xffffff,		/* dst_mask */
    643 	 FALSE) 		/* pcrel_offset */
    644 };
    645 
    646 /* Map BFD reloc types to AVR ELF reloc types.  */
    647 
    648 struct avr_reloc_map
    649 {
    650   bfd_reloc_code_real_type bfd_reloc_val;
    651   unsigned int elf_reloc_val;
    652 };
    653 
    654 static const struct avr_reloc_map avr_reloc_map[] =
    655 {
    656   { BFD_RELOC_NONE,                 R_AVR_NONE },
    657   { BFD_RELOC_32,                   R_AVR_32 },
    658   { BFD_RELOC_AVR_7_PCREL,          R_AVR_7_PCREL },
    659   { BFD_RELOC_AVR_13_PCREL,         R_AVR_13_PCREL },
    660   { BFD_RELOC_16,                   R_AVR_16 },
    661   { BFD_RELOC_AVR_16_PM,            R_AVR_16_PM },
    662   { BFD_RELOC_AVR_LO8_LDI,          R_AVR_LO8_LDI},
    663   { BFD_RELOC_AVR_HI8_LDI,          R_AVR_HI8_LDI },
    664   { BFD_RELOC_AVR_HH8_LDI,          R_AVR_HH8_LDI },
    665   { BFD_RELOC_AVR_MS8_LDI,          R_AVR_MS8_LDI },
    666   { BFD_RELOC_AVR_LO8_LDI_NEG,      R_AVR_LO8_LDI_NEG },
    667   { BFD_RELOC_AVR_HI8_LDI_NEG,      R_AVR_HI8_LDI_NEG },
    668   { BFD_RELOC_AVR_HH8_LDI_NEG,      R_AVR_HH8_LDI_NEG },
    669   { BFD_RELOC_AVR_MS8_LDI_NEG,      R_AVR_MS8_LDI_NEG },
    670   { BFD_RELOC_AVR_LO8_LDI_PM,       R_AVR_LO8_LDI_PM },
    671   { BFD_RELOC_AVR_LO8_LDI_GS,       R_AVR_LO8_LDI_GS },
    672   { BFD_RELOC_AVR_HI8_LDI_PM,       R_AVR_HI8_LDI_PM },
    673   { BFD_RELOC_AVR_HI8_LDI_GS,       R_AVR_HI8_LDI_GS },
    674   { BFD_RELOC_AVR_HH8_LDI_PM,       R_AVR_HH8_LDI_PM },
    675   { BFD_RELOC_AVR_LO8_LDI_PM_NEG,   R_AVR_LO8_LDI_PM_NEG },
    676   { BFD_RELOC_AVR_HI8_LDI_PM_NEG,   R_AVR_HI8_LDI_PM_NEG },
    677   { BFD_RELOC_AVR_HH8_LDI_PM_NEG,   R_AVR_HH8_LDI_PM_NEG },
    678   { BFD_RELOC_AVR_CALL,             R_AVR_CALL },
    679   { BFD_RELOC_AVR_LDI,              R_AVR_LDI  },
    680   { BFD_RELOC_AVR_6,                R_AVR_6    },
    681   { BFD_RELOC_AVR_6_ADIW,           R_AVR_6_ADIW },
    682   { BFD_RELOC_8,                    R_AVR_8 },
    683   { BFD_RELOC_AVR_8_LO,             R_AVR_8_LO8 },
    684   { BFD_RELOC_AVR_8_HI,             R_AVR_8_HI8 },
    685   { BFD_RELOC_AVR_8_HLO,            R_AVR_8_HLO8 },
    686   { BFD_RELOC_AVR_DIFF8,            R_AVR_DIFF8 },
    687   { BFD_RELOC_AVR_DIFF16,           R_AVR_DIFF16 },
    688   { BFD_RELOC_AVR_DIFF32,           R_AVR_DIFF32 },
    689   { BFD_RELOC_AVR_LDS_STS_16,       R_AVR_LDS_STS_16},
    690   { BFD_RELOC_AVR_PORT6,            R_AVR_PORT6},
    691   { BFD_RELOC_AVR_PORT5,            R_AVR_PORT5}
    692 };
    693 
    694 /* Meant to be filled one day with the wrap around address for the
    695    specific device.  I.e. should get the value 0x4000 for 16k devices,
    696    0x8000 for 32k devices and so on.
    697 
    698    We initialize it here with a value of 0x1000000 resulting in
    699    that we will never suggest a wrap-around jump during relaxation.
    700    The logic of the source code later on assumes that in
    701    avr_pc_wrap_around one single bit is set.  */
    702 static bfd_vma avr_pc_wrap_around = 0x10000000;
    703 
    704 /* If this variable holds a value different from zero, the linker relaxation
    705    machine will try to optimize call/ret sequences by a single jump
    706    instruction. This option could be switched off by a linker switch.  */
    707 static int avr_replace_call_ret_sequences = 1;
    708 
    709 /* Initialize an entry in the stub hash table.  */
    711 
    712 static struct bfd_hash_entry *
    713 stub_hash_newfunc (struct bfd_hash_entry *entry,
    714                    struct bfd_hash_table *table,
    715                    const char *string)
    716 {
    717   /* Allocate the structure if it has not already been allocated by a
    718      subclass.  */
    719   if (entry == NULL)
    720     {
    721       entry = bfd_hash_allocate (table,
    722                                  sizeof (struct elf32_avr_stub_hash_entry));
    723       if (entry == NULL)
    724         return entry;
    725     }
    726 
    727   /* Call the allocation method of the superclass.  */
    728   entry = bfd_hash_newfunc (entry, table, string);
    729   if (entry != NULL)
    730     {
    731       struct elf32_avr_stub_hash_entry *hsh;
    732 
    733       /* Initialize the local fields.  */
    734       hsh = avr_stub_hash_entry (entry);
    735       hsh->stub_offset = 0;
    736       hsh->target_value = 0;
    737     }
    738 
    739   return entry;
    740 }
    741 
    742 /* This function is just a straight passthrough to the real
    743    function in linker.c.  Its prupose is so that its address
    744    can be compared inside the avr_link_hash_table macro.  */
    745 
    746 static struct bfd_hash_entry *
    747 elf32_avr_link_hash_newfunc (struct bfd_hash_entry * entry,
    748 			     struct bfd_hash_table * table,
    749 			     const char * string)
    750 {
    751   return _bfd_elf_link_hash_newfunc (entry, table, string);
    752 }
    753 
    754 /* Free the derived linker hash table.  */
    755 
    756 static void
    757 elf32_avr_link_hash_table_free (bfd *obfd)
    758 {
    759   struct elf32_avr_link_hash_table *htab
    760     = (struct elf32_avr_link_hash_table *) obfd->link.hash;
    761 
    762   /* Free the address mapping table.  */
    763   if (htab->amt_stub_offsets != NULL)
    764     free (htab->amt_stub_offsets);
    765   if (htab->amt_destination_addr != NULL)
    766     free (htab->amt_destination_addr);
    767 
    768   bfd_hash_table_free (&htab->bstab);
    769   _bfd_elf_link_hash_table_free (obfd);
    770 }
    771 
    772 /* Create the derived linker hash table.  The AVR ELF port uses the derived
    773    hash table to keep information specific to the AVR ELF linker (without
    774    using static variables).  */
    775 
    776 static struct bfd_link_hash_table *
    777 elf32_avr_link_hash_table_create (bfd *abfd)
    778 {
    779   struct elf32_avr_link_hash_table *htab;
    780   bfd_size_type amt = sizeof (*htab);
    781 
    782   htab = bfd_zmalloc (amt);
    783   if (htab == NULL)
    784     return NULL;
    785 
    786   if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd,
    787                                       elf32_avr_link_hash_newfunc,
    788                                       sizeof (struct elf_link_hash_entry),
    789 				      AVR_ELF_DATA))
    790     {
    791       free (htab);
    792       return NULL;
    793     }
    794 
    795   /* Init the stub hash table too.  */
    796   if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
    797                             sizeof (struct elf32_avr_stub_hash_entry)))
    798     {
    799       _bfd_elf_link_hash_table_free (abfd);
    800       return NULL;
    801     }
    802   htab->etab.root.hash_table_free = elf32_avr_link_hash_table_free;
    803 
    804   return &htab->etab.root;
    805 }
    806 
    807 /* Calculates the effective distance of a pc relative jump/call.  */
    808 
    809 static int
    810 avr_relative_distance_considering_wrap_around (unsigned int distance)
    811 {
    812   unsigned int wrap_around_mask = avr_pc_wrap_around - 1;
    813   int dist_with_wrap_around = distance & wrap_around_mask;
    814 
    815   if (dist_with_wrap_around > ((int) (avr_pc_wrap_around >> 1)))
    816     dist_with_wrap_around -= avr_pc_wrap_around;
    817 
    818   return dist_with_wrap_around;
    819 }
    820 
    821 
    822 static reloc_howto_type *
    823 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    824 				 bfd_reloc_code_real_type code)
    825 {
    826   unsigned int i;
    827 
    828   for (i = 0;
    829        i < sizeof (avr_reloc_map) / sizeof (struct avr_reloc_map);
    830        i++)
    831     if (avr_reloc_map[i].bfd_reloc_val == code)
    832       return &elf_avr_howto_table[avr_reloc_map[i].elf_reloc_val];
    833 
    834   return NULL;
    835 }
    836 
    837 static reloc_howto_type *
    838 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    839 				 const char *r_name)
    840 {
    841   unsigned int i;
    842 
    843   for (i = 0;
    844        i < sizeof (elf_avr_howto_table) / sizeof (elf_avr_howto_table[0]);
    845        i++)
    846     if (elf_avr_howto_table[i].name != NULL
    847 	&& strcasecmp (elf_avr_howto_table[i].name, r_name) == 0)
    848       return &elf_avr_howto_table[i];
    849 
    850   return NULL;
    851 }
    852 
    853 /* Set the howto pointer for an AVR ELF reloc.  */
    854 
    855 static void
    856 avr_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
    857 			arelent *cache_ptr,
    858 			Elf_Internal_Rela *dst)
    859 {
    860   unsigned int r_type;
    861 
    862   r_type = ELF32_R_TYPE (dst->r_info);
    863   BFD_ASSERT (r_type < (unsigned int) R_AVR_max);
    864   cache_ptr->howto = &elf_avr_howto_table[r_type];
    865 }
    866 
    867 static bfd_boolean
    868 avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation)
    869 {
    870   return (relocation >= 0x020000);
    871 }
    872 
    873 /* Returns the address of the corresponding stub if there is one.
    874    Returns otherwise an address above 0x020000.  This function
    875    could also be used, if there is no knowledge on the section where
    876    the destination is found.  */
    877 
    878 static bfd_vma
    879 avr_get_stub_addr (bfd_vma srel,
    880                    struct elf32_avr_link_hash_table *htab)
    881 {
    882   unsigned int sindex;
    883   bfd_vma stub_sec_addr =
    884               (htab->stub_sec->output_section->vma +
    885 	       htab->stub_sec->output_offset);
    886 
    887   for (sindex = 0; sindex < htab->amt_max_entry_cnt; sindex ++)
    888     if (htab->amt_destination_addr[sindex] == srel)
    889       return htab->amt_stub_offsets[sindex] + stub_sec_addr;
    890 
    891   /* Return an address that could not be reached by 16 bit relocs.  */
    892   return 0x020000;
    893 }
    894 
    895 /* Perform a diff relocation. Nothing to do, as the difference value is already
    896    written into the section's contents. */
    897 
    898 static bfd_reloc_status_type
    899 bfd_elf_avr_diff_reloc (bfd *abfd ATTRIBUTE_UNUSED,
    900 		      arelent *reloc_entry ATTRIBUTE_UNUSED,
    901               asymbol *symbol ATTRIBUTE_UNUSED,
    902               void *data ATTRIBUTE_UNUSED,
    903               asection *input_section ATTRIBUTE_UNUSED,
    904               bfd *output_bfd ATTRIBUTE_UNUSED,
    905               char **error_message ATTRIBUTE_UNUSED)
    906 {
    907   return bfd_reloc_ok;
    908 }
    909 
    910 
    911 /* Perform a single relocation.  By default we use the standard BFD
    912    routines, but a few relocs, we have to do them ourselves.  */
    913 
    914 static bfd_reloc_status_type
    915 avr_final_link_relocate (reloc_howto_type *                 howto,
    916 			 bfd *                              input_bfd,
    917 			 asection *                         input_section,
    918 			 bfd_byte *                         contents,
    919 			 Elf_Internal_Rela *                rel,
    920                          bfd_vma                            relocation,
    921                          struct elf32_avr_link_hash_table * htab)
    922 {
    923   bfd_reloc_status_type r = bfd_reloc_ok;
    924   bfd_vma               x;
    925   bfd_signed_vma	srel;
    926   bfd_signed_vma	reloc_addr;
    927   bfd_boolean           use_stubs = FALSE;
    928   /* Usually is 0, unless we are generating code for a bootloader.  */
    929   bfd_signed_vma        base_addr = htab->vector_base;
    930 
    931   /* Absolute addr of the reloc in the final excecutable.  */
    932   reloc_addr = rel->r_offset + input_section->output_section->vma
    933 	       + input_section->output_offset;
    934 
    935   switch (howto->type)
    936     {
    937     case R_AVR_7_PCREL:
    938       contents += rel->r_offset;
    939       srel = (bfd_signed_vma) relocation;
    940       srel += rel->r_addend;
    941       srel -= rel->r_offset;
    942       srel -= 2;	/* Branch instructions add 2 to the PC...  */
    943       srel -= (input_section->output_section->vma +
    944 	       input_section->output_offset);
    945 
    946       if (srel & 1)
    947 	return bfd_reloc_outofrange;
    948       if (srel > ((1 << 7) - 1) || (srel < - (1 << 7)))
    949 	return bfd_reloc_overflow;
    950       x = bfd_get_16 (input_bfd, contents);
    951       x = (x & 0xfc07) | (((srel >> 1) << 3) & 0x3f8);
    952       bfd_put_16 (input_bfd, x, contents);
    953       break;
    954 
    955     case R_AVR_13_PCREL:
    956       contents   += rel->r_offset;
    957       srel = (bfd_signed_vma) relocation;
    958       srel += rel->r_addend;
    959       srel -= rel->r_offset;
    960       srel -= 2;	/* Branch instructions add 2 to the PC...  */
    961       srel -= (input_section->output_section->vma +
    962 	       input_section->output_offset);
    963 
    964       if (srel & 1)
    965 	return bfd_reloc_outofrange;
    966 
    967       srel = avr_relative_distance_considering_wrap_around (srel);
    968 
    969       /* AVR addresses commands as words.  */
    970       srel >>= 1;
    971 
    972       /* Check for overflow.  */
    973       if (srel < -2048 || srel > 2047)
    974 	{
    975           /* Relative distance is too large.  */
    976 
    977 	  /* Always apply WRAPAROUND for avr2, avr25, and avr4.  */
    978 	  switch (bfd_get_mach (input_bfd))
    979 	    {
    980 	    case bfd_mach_avr2:
    981 	    case bfd_mach_avr25:
    982 	    case bfd_mach_avr4:
    983 	      break;
    984 
    985 	    default:
    986 	      return bfd_reloc_overflow;
    987 	    }
    988 	}
    989 
    990       x = bfd_get_16 (input_bfd, contents);
    991       x = (x & 0xf000) | (srel & 0xfff);
    992       bfd_put_16 (input_bfd, x, contents);
    993       break;
    994 
    995     case R_AVR_LO8_LDI:
    996       contents += rel->r_offset;
    997       srel = (bfd_signed_vma) relocation + rel->r_addend;
    998       x = bfd_get_16 (input_bfd, contents);
    999       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
   1000       bfd_put_16 (input_bfd, x, contents);
   1001       break;
   1002 
   1003     case R_AVR_LDI:
   1004       contents += rel->r_offset;
   1005       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1006       if (((srel > 0) && (srel & 0xffff) > 255)
   1007 	  || ((srel < 0) && ((-srel) & 0xffff) > 128))
   1008         /* Remove offset for data/eeprom section.  */
   1009         return bfd_reloc_overflow;
   1010 
   1011       x = bfd_get_16 (input_bfd, contents);
   1012       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
   1013       bfd_put_16 (input_bfd, x, contents);
   1014       break;
   1015 
   1016     case R_AVR_6:
   1017       contents += rel->r_offset;
   1018       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1019       if (((srel & 0xffff) > 63) || (srel < 0))
   1020 	/* Remove offset for data/eeprom section.  */
   1021 	return bfd_reloc_overflow;
   1022       x = bfd_get_16 (input_bfd, contents);
   1023       x = (x & 0xd3f8) | ((srel & 7) | ((srel & (3 << 3)) << 7)
   1024                        | ((srel & (1 << 5)) << 8));
   1025       bfd_put_16 (input_bfd, x, contents);
   1026       break;
   1027 
   1028     case R_AVR_6_ADIW:
   1029       contents += rel->r_offset;
   1030       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1031       if (((srel & 0xffff) > 63) || (srel < 0))
   1032 	/* Remove offset for data/eeprom section.  */
   1033 	return bfd_reloc_overflow;
   1034       x = bfd_get_16 (input_bfd, contents);
   1035       x = (x & 0xff30) | (srel & 0xf) | ((srel & 0x30) << 2);
   1036       bfd_put_16 (input_bfd, x, contents);
   1037       break;
   1038 
   1039     case R_AVR_HI8_LDI:
   1040       contents += rel->r_offset;
   1041       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1042       srel = (srel >> 8) & 0xff;
   1043       x = bfd_get_16 (input_bfd, contents);
   1044       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
   1045       bfd_put_16 (input_bfd, x, contents);
   1046       break;
   1047 
   1048     case R_AVR_HH8_LDI:
   1049       contents += rel->r_offset;
   1050       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1051       srel = (srel >> 16) & 0xff;
   1052       x = bfd_get_16 (input_bfd, contents);
   1053       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
   1054       bfd_put_16 (input_bfd, x, contents);
   1055       break;
   1056 
   1057     case R_AVR_MS8_LDI:
   1058       contents += rel->r_offset;
   1059       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1060       srel = (srel >> 24) & 0xff;
   1061       x = bfd_get_16 (input_bfd, contents);
   1062       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
   1063       bfd_put_16 (input_bfd, x, contents);
   1064       break;
   1065 
   1066     case R_AVR_LO8_LDI_NEG:
   1067       contents += rel->r_offset;
   1068       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1069       srel = -srel;
   1070       x = bfd_get_16 (input_bfd, contents);
   1071       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
   1072       bfd_put_16 (input_bfd, x, contents);
   1073       break;
   1074 
   1075     case R_AVR_HI8_LDI_NEG:
   1076       contents += rel->r_offset;
   1077       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1078       srel = -srel;
   1079       srel = (srel >> 8) & 0xff;
   1080       x = bfd_get_16 (input_bfd, contents);
   1081       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
   1082       bfd_put_16 (input_bfd, x, contents);
   1083       break;
   1084 
   1085     case R_AVR_HH8_LDI_NEG:
   1086       contents += rel->r_offset;
   1087       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1088       srel = -srel;
   1089       srel = (srel >> 16) & 0xff;
   1090       x = bfd_get_16 (input_bfd, contents);
   1091       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
   1092       bfd_put_16 (input_bfd, x, contents);
   1093       break;
   1094 
   1095     case R_AVR_MS8_LDI_NEG:
   1096       contents += rel->r_offset;
   1097       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1098       srel = -srel;
   1099       srel = (srel >> 24) & 0xff;
   1100       x = bfd_get_16 (input_bfd, contents);
   1101       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
   1102       bfd_put_16 (input_bfd, x, contents);
   1103       break;
   1104 
   1105     case R_AVR_LO8_LDI_GS:
   1106       use_stubs = (!htab->no_stubs);
   1107       /* Fall through.  */
   1108     case R_AVR_LO8_LDI_PM:
   1109       contents += rel->r_offset;
   1110       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1111 
   1112       if (use_stubs
   1113           && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
   1114         {
   1115           bfd_vma old_srel = srel;
   1116 
   1117           /* We need to use the address of the stub instead.  */
   1118           srel = avr_get_stub_addr (srel, htab);
   1119           if (debug_stubs)
   1120             printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
   1121                     "reloc at address 0x%x.\n",
   1122                     (unsigned int) srel,
   1123                     (unsigned int) old_srel,
   1124                     (unsigned int) reloc_addr);
   1125 
   1126 	  if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
   1127 	    return bfd_reloc_outofrange;
   1128         }
   1129 
   1130       if (srel & 1)
   1131 	return bfd_reloc_outofrange;
   1132       srel = srel >> 1;
   1133       x = bfd_get_16 (input_bfd, contents);
   1134       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
   1135       bfd_put_16 (input_bfd, x, contents);
   1136       break;
   1137 
   1138     case R_AVR_HI8_LDI_GS:
   1139       use_stubs = (!htab->no_stubs);
   1140       /* Fall through.  */
   1141     case R_AVR_HI8_LDI_PM:
   1142       contents += rel->r_offset;
   1143       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1144 
   1145       if (use_stubs
   1146           && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
   1147         {
   1148           bfd_vma old_srel = srel;
   1149 
   1150           /* We need to use the address of the stub instead.  */
   1151           srel = avr_get_stub_addr (srel, htab);
   1152           if (debug_stubs)
   1153             printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
   1154                     "reloc at address 0x%x.\n",
   1155                     (unsigned int) srel,
   1156                     (unsigned int) old_srel,
   1157                     (unsigned int) reloc_addr);
   1158 
   1159 	  if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
   1160 	    return bfd_reloc_outofrange;
   1161         }
   1162 
   1163       if (srel & 1)
   1164 	return bfd_reloc_outofrange;
   1165       srel = srel >> 1;
   1166       srel = (srel >> 8) & 0xff;
   1167       x = bfd_get_16 (input_bfd, contents);
   1168       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
   1169       bfd_put_16 (input_bfd, x, contents);
   1170       break;
   1171 
   1172     case R_AVR_HH8_LDI_PM:
   1173       contents += rel->r_offset;
   1174       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1175       if (srel & 1)
   1176 	return bfd_reloc_outofrange;
   1177       srel = srel >> 1;
   1178       srel = (srel >> 16) & 0xff;
   1179       x = bfd_get_16 (input_bfd, contents);
   1180       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
   1181       bfd_put_16 (input_bfd, x, contents);
   1182       break;
   1183 
   1184     case R_AVR_LO8_LDI_PM_NEG:
   1185       contents += rel->r_offset;
   1186       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1187       srel = -srel;
   1188       if (srel & 1)
   1189 	return bfd_reloc_outofrange;
   1190       srel = srel >> 1;
   1191       x = bfd_get_16 (input_bfd, contents);
   1192       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
   1193       bfd_put_16 (input_bfd, x, contents);
   1194       break;
   1195 
   1196     case R_AVR_HI8_LDI_PM_NEG:
   1197       contents += rel->r_offset;
   1198       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1199       srel = -srel;
   1200       if (srel & 1)
   1201 	return bfd_reloc_outofrange;
   1202       srel = srel >> 1;
   1203       srel = (srel >> 8) & 0xff;
   1204       x = bfd_get_16 (input_bfd, contents);
   1205       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
   1206       bfd_put_16 (input_bfd, x, contents);
   1207       break;
   1208 
   1209     case R_AVR_HH8_LDI_PM_NEG:
   1210       contents += rel->r_offset;
   1211       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1212       srel = -srel;
   1213       if (srel & 1)
   1214 	return bfd_reloc_outofrange;
   1215       srel = srel >> 1;
   1216       srel = (srel >> 16) & 0xff;
   1217       x = bfd_get_16 (input_bfd, contents);
   1218       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
   1219       bfd_put_16 (input_bfd, x, contents);
   1220       break;
   1221 
   1222     case R_AVR_CALL:
   1223       contents += rel->r_offset;
   1224       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1225       if (srel & 1)
   1226 	return bfd_reloc_outofrange;
   1227       srel = srel >> 1;
   1228       x = bfd_get_16 (input_bfd, contents);
   1229       x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16;
   1230       bfd_put_16 (input_bfd, x, contents);
   1231       bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2);
   1232       break;
   1233 
   1234     case R_AVR_16_PM:
   1235       use_stubs = (!htab->no_stubs);
   1236       contents += rel->r_offset;
   1237       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1238 
   1239       if (use_stubs
   1240           && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
   1241         {
   1242           bfd_vma old_srel = srel;
   1243 
   1244           /* We need to use the address of the stub instead.  */
   1245           srel = avr_get_stub_addr (srel,htab);
   1246           if (debug_stubs)
   1247             printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
   1248                     "reloc at address 0x%x.\n",
   1249                     (unsigned int) srel,
   1250                     (unsigned int) old_srel,
   1251                     (unsigned int) reloc_addr);
   1252 
   1253 	  if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
   1254 	    return bfd_reloc_outofrange;
   1255         }
   1256 
   1257       if (srel & 1)
   1258 	return bfd_reloc_outofrange;
   1259       srel = srel >> 1;
   1260       bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents);
   1261       break;
   1262 
   1263     case R_AVR_DIFF8:
   1264     case R_AVR_DIFF16:
   1265     case R_AVR_DIFF32:
   1266       /* Nothing to do here, as contents already contains the diff value. */
   1267       r = bfd_reloc_ok;
   1268       break;
   1269 
   1270    case R_AVR_LDS_STS_16:
   1271       contents += rel->r_offset;
   1272       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1273       if ((srel & 0xFFFF) < 0x40 || (srel & 0xFFFF) > 0xbf)
   1274         return bfd_reloc_outofrange;
   1275       srel = srel & 0x7f;
   1276       x = bfd_get_16 (input_bfd, contents);
   1277       x |= (srel & 0x0f) | ((srel & 0x30) << 5) | ((srel & 0x40) << 2);
   1278       bfd_put_16 (input_bfd, x, contents);
   1279       break;
   1280 
   1281     case R_AVR_PORT6:
   1282       contents += rel->r_offset;
   1283       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1284       if ((srel & 0xffff) > 0x3f)
   1285         return bfd_reloc_outofrange;
   1286       x = bfd_get_16 (input_bfd, contents);
   1287       x = (x & 0xf9f0) | ((srel & 0x30) << 5) | (srel & 0x0f);
   1288       bfd_put_16 (input_bfd, x, contents);
   1289       break;
   1290 
   1291     case R_AVR_PORT5:
   1292       contents += rel->r_offset;
   1293       srel = (bfd_signed_vma) relocation + rel->r_addend;
   1294       if ((srel & 0xffff) > 0x1f)
   1295         return bfd_reloc_outofrange;
   1296       x = bfd_get_16 (input_bfd, contents);
   1297       x = (x & 0xff07) | ((srel & 0x1f) << 3);
   1298       bfd_put_16 (input_bfd, x, contents);
   1299       break;
   1300 
   1301     default:
   1302       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
   1303 				    contents, rel->r_offset,
   1304 				    relocation, rel->r_addend);
   1305     }
   1306 
   1307   return r;
   1308 }
   1309 
   1310 /* Relocate an AVR ELF section.  */
   1311 
   1312 static bfd_boolean
   1313 elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
   1314 			    struct bfd_link_info *info,
   1315 			    bfd *input_bfd,
   1316 			    asection *input_section,
   1317 			    bfd_byte *contents,
   1318 			    Elf_Internal_Rela *relocs,
   1319 			    Elf_Internal_Sym *local_syms,
   1320 			    asection **local_sections)
   1321 {
   1322   Elf_Internal_Shdr *           symtab_hdr;
   1323   struct elf_link_hash_entry ** sym_hashes;
   1324   Elf_Internal_Rela *           rel;
   1325   Elf_Internal_Rela *           relend;
   1326   struct elf32_avr_link_hash_table * htab = avr_link_hash_table (info);
   1327 
   1328   if (htab == NULL)
   1329     return FALSE;
   1330 
   1331   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
   1332   sym_hashes = elf_sym_hashes (input_bfd);
   1333   relend     = relocs + input_section->reloc_count;
   1334 
   1335   for (rel = relocs; rel < relend; rel ++)
   1336     {
   1337       reloc_howto_type *           howto;
   1338       unsigned long                r_symndx;
   1339       Elf_Internal_Sym *           sym;
   1340       asection *                   sec;
   1341       struct elf_link_hash_entry * h;
   1342       bfd_vma                      relocation;
   1343       bfd_reloc_status_type        r;
   1344       const char *                 name;
   1345       int                          r_type;
   1346 
   1347       r_type = ELF32_R_TYPE (rel->r_info);
   1348       r_symndx = ELF32_R_SYM (rel->r_info);
   1349       howto  = elf_avr_howto_table + r_type;
   1350       h      = NULL;
   1351       sym    = NULL;
   1352       sec    = NULL;
   1353 
   1354       if (r_symndx < symtab_hdr->sh_info)
   1355 	{
   1356 	  sym = local_syms + r_symndx;
   1357 	  sec = local_sections [r_symndx];
   1358 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   1359 
   1360 	  name = bfd_elf_string_from_elf_section
   1361 	    (input_bfd, symtab_hdr->sh_link, sym->st_name);
   1362 	  name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
   1363 	}
   1364       else
   1365 	{
   1366 	  bfd_boolean unresolved_reloc, warned, ignored;
   1367 
   1368 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   1369 				   r_symndx, symtab_hdr, sym_hashes,
   1370 				   h, sec, relocation,
   1371 				   unresolved_reloc, warned, ignored);
   1372 
   1373 	  name = h->root.root.string;
   1374 	}
   1375 
   1376       if (sec != NULL && discarded_section (sec))
   1377 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   1378 					 rel, 1, relend, howto, 0, contents);
   1379 
   1380       if (info->relocatable)
   1381 	continue;
   1382 
   1383       r = avr_final_link_relocate (howto, input_bfd, input_section,
   1384 				   contents, rel, relocation, htab);
   1385 
   1386       if (r != bfd_reloc_ok)
   1387 	{
   1388 	  const char * msg = (const char *) NULL;
   1389 
   1390 	  switch (r)
   1391 	    {
   1392 	    case bfd_reloc_overflow:
   1393 	      r = info->callbacks->reloc_overflow
   1394 		(info, (h ? &h->root : NULL),
   1395 		 name, howto->name, (bfd_vma) 0,
   1396 		 input_bfd, input_section, rel->r_offset);
   1397 	      break;
   1398 
   1399 	    case bfd_reloc_undefined:
   1400 	      r = info->callbacks->undefined_symbol
   1401 		(info, name, input_bfd, input_section, rel->r_offset, TRUE);
   1402 	      break;
   1403 
   1404 	    case bfd_reloc_outofrange:
   1405 	      msg = _("internal error: out of range error");
   1406 	      break;
   1407 
   1408 	    case bfd_reloc_notsupported:
   1409 	      msg = _("internal error: unsupported relocation error");
   1410 	      break;
   1411 
   1412 	    case bfd_reloc_dangerous:
   1413 	      msg = _("internal error: dangerous relocation");
   1414 	      break;
   1415 
   1416 	    default:
   1417 	      msg = _("internal error: unknown error");
   1418 	      break;
   1419 	    }
   1420 
   1421 	  if (msg)
   1422 	    r = info->callbacks->warning
   1423 	      (info, msg, name, input_bfd, input_section, rel->r_offset);
   1424 
   1425 	  if (! r)
   1426 	    return FALSE;
   1427 	}
   1428     }
   1429 
   1430   return TRUE;
   1431 }
   1432 
   1433 /* The final processing done just before writing out a AVR ELF object
   1434    file.  This gets the AVR architecture right based on the machine
   1435    number.  */
   1436 
   1437 static void
   1438 bfd_elf_avr_final_write_processing (bfd *abfd,
   1439 				    bfd_boolean linker ATTRIBUTE_UNUSED)
   1440 {
   1441   unsigned long val;
   1442 
   1443   switch (bfd_get_mach (abfd))
   1444     {
   1445     default:
   1446     case bfd_mach_avr2:
   1447       val = E_AVR_MACH_AVR2;
   1448       break;
   1449 
   1450     case bfd_mach_avr1:
   1451       val = E_AVR_MACH_AVR1;
   1452       break;
   1453 
   1454     case bfd_mach_avr25:
   1455       val = E_AVR_MACH_AVR25;
   1456       break;
   1457 
   1458     case bfd_mach_avr3:
   1459       val = E_AVR_MACH_AVR3;
   1460       break;
   1461 
   1462     case bfd_mach_avr31:
   1463       val = E_AVR_MACH_AVR31;
   1464       break;
   1465 
   1466     case bfd_mach_avr35:
   1467       val = E_AVR_MACH_AVR35;
   1468       break;
   1469 
   1470     case bfd_mach_avr4:
   1471       val = E_AVR_MACH_AVR4;
   1472       break;
   1473 
   1474     case bfd_mach_avr5:
   1475       val = E_AVR_MACH_AVR5;
   1476       break;
   1477 
   1478     case bfd_mach_avr51:
   1479       val = E_AVR_MACH_AVR51;
   1480       break;
   1481 
   1482     case bfd_mach_avr6:
   1483       val = E_AVR_MACH_AVR6;
   1484       break;
   1485 
   1486     case bfd_mach_avrxmega1:
   1487       val = E_AVR_MACH_XMEGA1;
   1488       break;
   1489 
   1490     case bfd_mach_avrxmega2:
   1491       val = E_AVR_MACH_XMEGA2;
   1492       break;
   1493 
   1494     case bfd_mach_avrxmega3:
   1495       val = E_AVR_MACH_XMEGA3;
   1496       break;
   1497 
   1498     case bfd_mach_avrxmega4:
   1499       val = E_AVR_MACH_XMEGA4;
   1500       break;
   1501 
   1502     case bfd_mach_avrxmega5:
   1503       val = E_AVR_MACH_XMEGA5;
   1504       break;
   1505 
   1506     case bfd_mach_avrxmega6:
   1507       val = E_AVR_MACH_XMEGA6;
   1508       break;
   1509 
   1510     case bfd_mach_avrxmega7:
   1511       val = E_AVR_MACH_XMEGA7;
   1512       break;
   1513 
   1514    case bfd_mach_avrtiny:
   1515       val = E_AVR_MACH_AVRTINY;
   1516       break;
   1517     }
   1518 
   1519   elf_elfheader (abfd)->e_machine = EM_AVR;
   1520   elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH;
   1521   elf_elfheader (abfd)->e_flags |= val;
   1522   elf_elfheader (abfd)->e_flags |= EF_AVR_LINKRELAX_PREPARED;
   1523 }
   1524 
   1525 /* Set the right machine number.  */
   1526 
   1527 static bfd_boolean
   1528 elf32_avr_object_p (bfd *abfd)
   1529 {
   1530   unsigned int e_set = bfd_mach_avr2;
   1531 
   1532   if (elf_elfheader (abfd)->e_machine == EM_AVR
   1533       || elf_elfheader (abfd)->e_machine == EM_AVR_OLD)
   1534     {
   1535       int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH;
   1536 
   1537       switch (e_mach)
   1538 	{
   1539 	default:
   1540 	case E_AVR_MACH_AVR2:
   1541 	  e_set = bfd_mach_avr2;
   1542 	  break;
   1543 
   1544 	case E_AVR_MACH_AVR1:
   1545 	  e_set = bfd_mach_avr1;
   1546 	  break;
   1547 
   1548 	case E_AVR_MACH_AVR25:
   1549 	  e_set = bfd_mach_avr25;
   1550 	  break;
   1551 
   1552 	case E_AVR_MACH_AVR3:
   1553 	  e_set = bfd_mach_avr3;
   1554 	  break;
   1555 
   1556 	case E_AVR_MACH_AVR31:
   1557 	  e_set = bfd_mach_avr31;
   1558 	  break;
   1559 
   1560 	case E_AVR_MACH_AVR35:
   1561 	  e_set = bfd_mach_avr35;
   1562 	  break;
   1563 
   1564 	case E_AVR_MACH_AVR4:
   1565 	  e_set = bfd_mach_avr4;
   1566 	  break;
   1567 
   1568 	case E_AVR_MACH_AVR5:
   1569 	  e_set = bfd_mach_avr5;
   1570 	  break;
   1571 
   1572 	case E_AVR_MACH_AVR51:
   1573 	  e_set = bfd_mach_avr51;
   1574 	  break;
   1575 
   1576 	case E_AVR_MACH_AVR6:
   1577 	  e_set = bfd_mach_avr6;
   1578 	  break;
   1579 
   1580 	case E_AVR_MACH_XMEGA1:
   1581 	  e_set = bfd_mach_avrxmega1;
   1582 	  break;
   1583 
   1584 	case E_AVR_MACH_XMEGA2:
   1585 	  e_set = bfd_mach_avrxmega2;
   1586 	  break;
   1587 
   1588 	case E_AVR_MACH_XMEGA3:
   1589 	  e_set = bfd_mach_avrxmega3;
   1590 	  break;
   1591 
   1592 	case E_AVR_MACH_XMEGA4:
   1593 	  e_set = bfd_mach_avrxmega4;
   1594 	  break;
   1595 
   1596 	case E_AVR_MACH_XMEGA5:
   1597 	  e_set = bfd_mach_avrxmega5;
   1598 	  break;
   1599 
   1600 	case E_AVR_MACH_XMEGA6:
   1601 	  e_set = bfd_mach_avrxmega6;
   1602 	  break;
   1603 
   1604 	case E_AVR_MACH_XMEGA7:
   1605 	  e_set = bfd_mach_avrxmega7;
   1606 	  break;
   1607 
   1608     case E_AVR_MACH_AVRTINY:
   1609       e_set = bfd_mach_avrtiny;
   1610       break;
   1611 	}
   1612     }
   1613   return bfd_default_set_arch_mach (abfd, bfd_arch_avr,
   1614 				    e_set);
   1615 }
   1616 
   1617 /* Returns whether the relocation type passed is a diff reloc. */
   1618 
   1619 static bfd_boolean
   1620 elf32_avr_is_diff_reloc (Elf_Internal_Rela *irel)
   1621 {
   1622   return (ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF8
   1623           ||ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF16
   1624           || ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF32);
   1625 }
   1626 
   1627 /* Reduce the diff value written in the section by count if the shrinked
   1628    insn address happens to fall between the two symbols for which this
   1629    diff reloc was emitted.  */
   1630 
   1631 static void
   1632 elf32_avr_adjust_diff_reloc_value (bfd *abfd,
   1633                                    struct bfd_section *isec,
   1634                                    Elf_Internal_Rela *irel,
   1635                                    bfd_vma symval,
   1636                                    bfd_vma shrinked_insn_address,
   1637                                    int count)
   1638 {
   1639   unsigned char *reloc_contents = NULL;
   1640   unsigned char *isec_contents = elf_section_data (isec)->this_hdr.contents;
   1641   if (isec_contents == NULL)
   1642   {
   1643     if (! bfd_malloc_and_get_section (abfd, isec, &isec_contents))
   1644       return;
   1645 
   1646     elf_section_data (isec)->this_hdr.contents = isec_contents;
   1647   }
   1648 
   1649   reloc_contents = isec_contents + irel->r_offset;
   1650 
   1651   /* Read value written in object file. */
   1652  bfd_vma x = 0;
   1653   switch (ELF32_R_TYPE (irel->r_info))
   1654   {
   1655   case R_AVR_DIFF8:
   1656     {
   1657       x = *reloc_contents;
   1658       break;
   1659     }
   1660   case R_AVR_DIFF16:
   1661     {
   1662       x = bfd_get_16 (abfd, reloc_contents);
   1663       break;
   1664     }
   1665   case R_AVR_DIFF32:
   1666     {
   1667       x = bfd_get_32 (abfd, reloc_contents);
   1668       break;
   1669     }
   1670   default:
   1671     {
   1672       BFD_FAIL();
   1673     }
   1674   }
   1675 
   1676   /* For a diff reloc sym1 - sym2 the diff at assembly time (x) is written
   1677      into the object file at the reloc offset. sym2's logical value is
   1678      symval (<start_of_section>) + reloc addend. Compute the start and end
   1679      addresses and check if the shrinked insn falls between sym1 and sym2. */
   1680 
   1681   bfd_vma end_address = symval + irel->r_addend;
   1682   bfd_vma start_address = end_address - x;
   1683 
   1684   /* Reduce the diff value by count bytes and write it back into section
   1685     contents. */
   1686 
   1687   if (shrinked_insn_address >= start_address
   1688       && shrinked_insn_address <= end_address)
   1689   {
   1690     switch (ELF32_R_TYPE (irel->r_info))
   1691     {
   1692     case R_AVR_DIFF8:
   1693       {
   1694         *reloc_contents = (x - count);
   1695         break;
   1696       }
   1697     case R_AVR_DIFF16:
   1698       {
   1699         bfd_put_16 (abfd, (x - count) & 0xFFFF, reloc_contents);
   1700         break;
   1701       }
   1702     case R_AVR_DIFF32:
   1703       {
   1704         bfd_put_32 (abfd, (x - count) & 0xFFFFFFFF, reloc_contents);
   1705         break;
   1706       }
   1707     default:
   1708       {
   1709         BFD_FAIL();
   1710       }
   1711     }
   1712 
   1713   }
   1714 }
   1715 
   1716 /* Delete some bytes from a section while changing the size of an instruction.
   1717    The parameter "addr" denotes the section-relative offset pointing just
   1718    behind the shrinked instruction. "addr+count" point at the first
   1719    byte just behind the original unshrinked instruction.  */
   1720 
   1721 static bfd_boolean
   1722 elf32_avr_relax_delete_bytes (bfd *abfd,
   1723                               asection *sec,
   1724                               bfd_vma addr,
   1725                               int count)
   1726 {
   1727   Elf_Internal_Shdr *symtab_hdr;
   1728   unsigned int sec_shndx;
   1729   bfd_byte *contents;
   1730   Elf_Internal_Rela *irel, *irelend;
   1731   Elf_Internal_Sym *isym;
   1732   Elf_Internal_Sym *isymbuf = NULL;
   1733   bfd_vma toaddr;
   1734   struct elf_link_hash_entry **sym_hashes;
   1735   struct elf_link_hash_entry **end_hashes;
   1736   unsigned int symcount;
   1737 
   1738   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   1739   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   1740   contents = elf_section_data (sec)->this_hdr.contents;
   1741 
   1742   toaddr = sec->size;
   1743 
   1744   irel = elf_section_data (sec)->relocs;
   1745   irelend = irel + sec->reloc_count;
   1746 
   1747   /* Actually delete the bytes.  */
   1748   if (toaddr - addr - count > 0)
   1749     memmove (contents + addr, contents + addr + count,
   1750              (size_t) (toaddr - addr - count));
   1751   sec->size -= count;
   1752 
   1753   /* Adjust all the reloc addresses.  */
   1754   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
   1755     {
   1756       bfd_vma old_reloc_address;
   1757 
   1758       old_reloc_address = (sec->output_section->vma
   1759                            + sec->output_offset + irel->r_offset);
   1760 
   1761       /* Get the new reloc address.  */
   1762       if ((irel->r_offset > addr
   1763            && irel->r_offset < toaddr))
   1764         {
   1765           if (debug_relax)
   1766             printf ("Relocation at address 0x%x needs to be moved.\n"
   1767                     "Old section offset: 0x%x, New section offset: 0x%x \n",
   1768                     (unsigned int) old_reloc_address,
   1769                     (unsigned int) irel->r_offset,
   1770                     (unsigned int) ((irel->r_offset) - count));
   1771 
   1772           irel->r_offset -= count;
   1773         }
   1774 
   1775     }
   1776 
   1777    /* The reloc's own addresses are now ok. However, we need to readjust
   1778       the reloc's addend, i.e. the reloc's value if two conditions are met:
   1779       1.) the reloc is relative to a symbol in this section that
   1780           is located in front of the shrinked instruction
   1781       2.) symbol plus addend end up behind the shrinked instruction.
   1782 
   1783       The most common case where this happens are relocs relative to
   1784       the section-start symbol.
   1785 
   1786       This step needs to be done for all of the sections of the bfd.  */
   1787 
   1788   {
   1789     struct bfd_section *isec;
   1790 
   1791     for (isec = abfd->sections; isec; isec = isec->next)
   1792      {
   1793        bfd_vma symval;
   1794        bfd_vma shrinked_insn_address;
   1795 
   1796        if (isec->reloc_count == 0)
   1797 	 continue;
   1798 
   1799        shrinked_insn_address = (sec->output_section->vma
   1800                                 + sec->output_offset + addr - count);
   1801 
   1802        irel = elf_section_data (isec)->relocs;
   1803        /* PR 12161: Read in the relocs for this section if necessary.  */
   1804        if (irel == NULL)
   1805          irel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE);
   1806 
   1807        for (irelend = irel + isec->reloc_count;
   1808             irel < irelend;
   1809             irel++)
   1810          {
   1811            /* Read this BFD's local symbols if we haven't done
   1812               so already.  */
   1813            if (isymbuf == NULL && symtab_hdr->sh_info != 0)
   1814              {
   1815                isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   1816                if (isymbuf == NULL)
   1817                  isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   1818                                                  symtab_hdr->sh_info, 0,
   1819                                                  NULL, NULL, NULL);
   1820                if (isymbuf == NULL)
   1821                  return FALSE;
   1822              }
   1823 
   1824            /* Get the value of the symbol referred to by the reloc.  */
   1825            if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
   1826              {
   1827                /* A local symbol.  */
   1828                asection *sym_sec;
   1829 
   1830                isym = isymbuf + ELF32_R_SYM (irel->r_info);
   1831                sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   1832                symval = isym->st_value;
   1833                /* If the reloc is absolute, it will not have
   1834                   a symbol or section associated with it.  */
   1835                if (sym_sec == sec)
   1836                  {
   1837                    symval += sym_sec->output_section->vma
   1838                              + sym_sec->output_offset;
   1839 
   1840                    if (debug_relax)
   1841                      printf ("Checking if the relocation's "
   1842                              "addend needs corrections.\n"
   1843                              "Address of anchor symbol: 0x%x \n"
   1844                              "Address of relocation target: 0x%x \n"
   1845                              "Address of relaxed insn: 0x%x \n",
   1846                              (unsigned int) symval,
   1847                              (unsigned int) (symval + irel->r_addend),
   1848                              (unsigned int) shrinked_insn_address);
   1849 
   1850                    if (symval <= shrinked_insn_address
   1851                        && (symval + irel->r_addend) > shrinked_insn_address)
   1852                      {
   1853                        if (elf32_avr_is_diff_reloc (irel))
   1854                          {
   1855                            elf32_avr_adjust_diff_reloc_value (abfd, isec, irel,
   1856                                                          symval,
   1857                                                          shrinked_insn_address,
   1858                                                         count);
   1859                          }
   1860 
   1861                        irel->r_addend -= count;
   1862 
   1863                        if (debug_relax)
   1864                          printf ("Relocation's addend needed to be fixed \n");
   1865                      }
   1866                  }
   1867 	       /* else...Reference symbol is absolute.  No adjustment needed.  */
   1868 	     }
   1869 	   /* else...Reference symbol is extern.  No need for adjusting
   1870 	      the addend.  */
   1871 	 }
   1872      }
   1873   }
   1874 
   1875   /* Adjust the local symbols defined in this section.  */
   1876   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
   1877   /* Fix PR 9841, there may be no local symbols.  */
   1878   if (isym != NULL)
   1879     {
   1880       Elf_Internal_Sym *isymend;
   1881 
   1882       isymend = isym + symtab_hdr->sh_info;
   1883       for (; isym < isymend; isym++)
   1884 	{
   1885 	  if (isym->st_shndx == sec_shndx)
   1886             {
   1887 	      if (isym->st_value > addr
   1888                   && isym->st_value <= toaddr)
   1889                 isym->st_value -= count;
   1890 
   1891               if (isym->st_value <= addr
   1892                   && isym->st_value + isym->st_size > addr)
   1893                 {
   1894                   /* If this assert fires then we have a symbol that ends
   1895                      part way through an instruction.  Does that make
   1896                      sense?  */
   1897                   BFD_ASSERT (isym->st_value + isym->st_size >= addr + count);
   1898                   isym->st_size -= count;
   1899                 }
   1900             }
   1901 	}
   1902     }
   1903 
   1904   /* Now adjust the global symbols defined in this section.  */
   1905   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
   1906               - symtab_hdr->sh_info);
   1907   sym_hashes = elf_sym_hashes (abfd);
   1908   end_hashes = sym_hashes + symcount;
   1909   for (; sym_hashes < end_hashes; sym_hashes++)
   1910     {
   1911       struct elf_link_hash_entry *sym_hash = *sym_hashes;
   1912       if ((sym_hash->root.type == bfd_link_hash_defined
   1913            || sym_hash->root.type == bfd_link_hash_defweak)
   1914           && sym_hash->root.u.def.section == sec)
   1915         {
   1916           if (sym_hash->root.u.def.value > addr
   1917               && sym_hash->root.u.def.value <= toaddr)
   1918             sym_hash->root.u.def.value -= count;
   1919 
   1920           if (sym_hash->root.u.def.value <= addr
   1921               && (sym_hash->root.u.def.value + sym_hash->size > addr))
   1922             {
   1923               /* If this assert fires then we have a symbol that ends
   1924                  part way through an instruction.  Does that make
   1925                  sense?  */
   1926               BFD_ASSERT (sym_hash->root.u.def.value + sym_hash->size
   1927                           >= addr + count);
   1928               sym_hash->size -= count;
   1929             }
   1930         }
   1931     }
   1932 
   1933   return TRUE;
   1934 }
   1935 
   1936 /* This function handles relaxing for the avr.
   1937    Many important relaxing opportunities within functions are already
   1938    realized by the compiler itself.
   1939    Here we try to replace  call (4 bytes) ->  rcall (2 bytes)
   1940    and jump -> rjmp (safes also 2 bytes).
   1941    As well we now optimize seqences of
   1942      - call/rcall function
   1943      - ret
   1944    to yield
   1945      - jmp/rjmp function
   1946      - ret
   1947    . In case that within a sequence
   1948      - jmp/rjmp label
   1949      - ret
   1950    the ret could no longer be reached it is optimized away. In order
   1951    to check if the ret is no longer needed, it is checked that the ret's address
   1952    is not the target of a branch or jump within the same section, it is checked
   1953    that there is no skip instruction before the jmp/rjmp and that there
   1954    is no local or global label place at the address of the ret.
   1955 
   1956    We refrain from relaxing within sections ".vectors" and
   1957    ".jumptables" in order to maintain the position of the instructions.
   1958    There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
   1959    if possible. (In future one could possibly use the space of the nop
   1960    for the first instruction of the irq service function.
   1961 
   1962    The .jumptables sections is meant to be used for a future tablejump variant
   1963    for the devices with 3-byte program counter where the table itself
   1964    contains 4-byte jump instructions whose relative offset must not
   1965    be changed.  */
   1966 
   1967 static bfd_boolean
   1968 elf32_avr_relax_section (bfd *abfd,
   1969 			 asection *sec,
   1970                          struct bfd_link_info *link_info,
   1971                          bfd_boolean *again)
   1972 {
   1973   Elf_Internal_Shdr *symtab_hdr;
   1974   Elf_Internal_Rela *internal_relocs;
   1975   Elf_Internal_Rela *irel, *irelend;
   1976   bfd_byte *contents = NULL;
   1977   Elf_Internal_Sym *isymbuf = NULL;
   1978   struct elf32_avr_link_hash_table *htab;
   1979 
   1980   /* If 'shrinkable' is FALSE, do not shrink by deleting bytes while
   1981      relaxing. Such shrinking can cause issues for the sections such
   1982      as .vectors and .jumptables. Instead the unused bytes should be
   1983      filled with nop instructions. */
   1984   bfd_boolean shrinkable = TRUE;
   1985 
   1986   if (!strcmp (sec->name,".vectors")
   1987       || !strcmp (sec->name,".jumptables"))
   1988     shrinkable = FALSE;
   1989 
   1990   if (link_info->relocatable)
   1991     (*link_info->callbacks->einfo)
   1992       (_("%P%F: --relax and -r may not be used together\n"));
   1993 
   1994   htab = avr_link_hash_table (link_info);
   1995   if (htab == NULL)
   1996     return FALSE;
   1997 
   1998   /* Assume nothing changes.  */
   1999   *again = FALSE;
   2000 
   2001   if ((!htab->no_stubs) && (sec == htab->stub_sec))
   2002     {
   2003       /* We are just relaxing the stub section.
   2004 	 Let's calculate the size needed again.  */
   2005       bfd_size_type last_estimated_stub_section_size = htab->stub_sec->size;
   2006 
   2007       if (debug_relax)
   2008         printf ("Relaxing the stub section. Size prior to this pass: %i\n",
   2009                 (int) last_estimated_stub_section_size);
   2010 
   2011       elf32_avr_size_stubs (htab->stub_sec->output_section->owner,
   2012                             link_info, FALSE);
   2013 
   2014       /* Check if the number of trampolines changed.  */
   2015       if (last_estimated_stub_section_size != htab->stub_sec->size)
   2016         *again = TRUE;
   2017 
   2018       if (debug_relax)
   2019         printf ("Size of stub section after this pass: %i\n",
   2020                 (int) htab->stub_sec->size);
   2021 
   2022       return TRUE;
   2023     }
   2024 
   2025   /* We don't have to do anything for a relocatable link, if
   2026      this section does not have relocs, or if this is not a
   2027      code section.  */
   2028   if (link_info->relocatable
   2029       || (sec->flags & SEC_RELOC) == 0
   2030       || sec->reloc_count == 0
   2031       || (sec->flags & SEC_CODE) == 0)
   2032     return TRUE;
   2033 
   2034   /* Check if the object file to relax uses internal symbols so that we
   2035      could fix up the relocations.  */
   2036   if (!(elf_elfheader (abfd)->e_flags & EF_AVR_LINKRELAX_PREPARED))
   2037     return TRUE;
   2038 
   2039   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   2040 
   2041   /* Get a copy of the native relocations.  */
   2042   internal_relocs = (_bfd_elf_link_read_relocs
   2043                      (abfd, sec, NULL, NULL, link_info->keep_memory));
   2044   if (internal_relocs == NULL)
   2045     goto error_return;
   2046 
   2047   /* Walk through the relocs looking for relaxing opportunities.  */
   2048   irelend = internal_relocs + sec->reloc_count;
   2049   for (irel = internal_relocs; irel < irelend; irel++)
   2050     {
   2051       bfd_vma symval;
   2052 
   2053       if (   ELF32_R_TYPE (irel->r_info) != R_AVR_13_PCREL
   2054 	  && ELF32_R_TYPE (irel->r_info) != R_AVR_7_PCREL
   2055 	  && ELF32_R_TYPE (irel->r_info) != R_AVR_CALL)
   2056         continue;
   2057 
   2058       /* Get the section contents if we haven't done so already.  */
   2059       if (contents == NULL)
   2060         {
   2061           /* Get cached copy if it exists.  */
   2062           if (elf_section_data (sec)->this_hdr.contents != NULL)
   2063             contents = elf_section_data (sec)->this_hdr.contents;
   2064           else
   2065             {
   2066               /* Go get them off disk.  */
   2067               if (! bfd_malloc_and_get_section (abfd, sec, &contents))
   2068                 goto error_return;
   2069             }
   2070         }
   2071 
   2072       /* Read this BFD's local symbols if we haven't done so already.  */
   2073       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
   2074         {
   2075           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   2076           if (isymbuf == NULL)
   2077             isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   2078                                             symtab_hdr->sh_info, 0,
   2079                                             NULL, NULL, NULL);
   2080           if (isymbuf == NULL)
   2081             goto error_return;
   2082         }
   2083 
   2084 
   2085       /* Get the value of the symbol referred to by the reloc.  */
   2086       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
   2087         {
   2088           /* A local symbol.  */
   2089           Elf_Internal_Sym *isym;
   2090           asection *sym_sec;
   2091 
   2092           isym = isymbuf + ELF32_R_SYM (irel->r_info);
   2093           sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   2094           symval = isym->st_value;
   2095           /* If the reloc is absolute, it will not have
   2096              a symbol or section associated with it.  */
   2097           if (sym_sec)
   2098             symval += sym_sec->output_section->vma
   2099               + sym_sec->output_offset;
   2100         }
   2101       else
   2102         {
   2103           unsigned long indx;
   2104           struct elf_link_hash_entry *h;
   2105 
   2106           /* An external symbol.  */
   2107           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
   2108           h = elf_sym_hashes (abfd)[indx];
   2109           BFD_ASSERT (h != NULL);
   2110           if (h->root.type != bfd_link_hash_defined
   2111               && h->root.type != bfd_link_hash_defweak)
   2112 	    /* This appears to be a reference to an undefined
   2113 	       symbol.  Just ignore it--it will be caught by the
   2114 	       regular reloc processing.  */
   2115 	    continue;
   2116 
   2117           symval = (h->root.u.def.value
   2118                     + h->root.u.def.section->output_section->vma
   2119                     + h->root.u.def.section->output_offset);
   2120         }
   2121 
   2122       /* For simplicity of coding, we are going to modify the section
   2123          contents, the section relocs, and the BFD symbol table.  We
   2124          must tell the rest of the code not to free up this
   2125          information.  It would be possible to instead create a table
   2126          of changes which have to be made, as is done in coff-mips.c;
   2127          that would be more work, but would require less memory when
   2128          the linker is run.  */
   2129       switch (ELF32_R_TYPE (irel->r_info))
   2130         {
   2131 	  /* Try to turn a 22-bit absolute call/jump into an 13-bit
   2132 	     pc-relative rcall/rjmp.  */
   2133 	case R_AVR_CALL:
   2134           {
   2135             bfd_vma value = symval + irel->r_addend;
   2136             bfd_vma dot, gap;
   2137             int distance_short_enough = 0;
   2138 
   2139             /* Get the address of this instruction.  */
   2140             dot = (sec->output_section->vma
   2141                    + sec->output_offset + irel->r_offset);
   2142 
   2143             /* Compute the distance from this insn to the branch target.  */
   2144             gap = value - dot;
   2145 
   2146             /* Check if the gap falls in the range that can be accommodated
   2147                in 13bits signed (It is 12bits when encoded, as we deal with
   2148                word addressing). */
   2149             if (!shrinkable && ((int) gap >= -4096 && (int) gap <= 4095))
   2150               distance_short_enough = 1;
   2151             /* If shrinkable, then we can check for a range of distance which
   2152                is two bytes farther on both the directions because the call
   2153                or jump target will be closer by two bytes after the
   2154                relaxation. */
   2155             else if (shrinkable && ((int) gap >= -4094 && (int) gap <= 4097))
   2156               distance_short_enough = 1;
   2157 
   2158             /* Here we handle the wrap-around case.  E.g. for a 16k device
   2159                we could use a rjmp to jump from address 0x100 to 0x3d00!
   2160                In order to make this work properly, we need to fill the
   2161                vaiable avr_pc_wrap_around with the appropriate value.
   2162                I.e. 0x4000 for a 16k device.  */
   2163             {
   2164 	      /* Shrinking the code size makes the gaps larger in the
   2165 		 case of wrap-arounds.  So we use a heuristical safety
   2166 		 margin to avoid that during relax the distance gets
   2167 		 again too large for the short jumps.  Let's assume
   2168 		 a typical code-size reduction due to relax for a
   2169 		 16k device of 600 bytes.  So let's use twice the
   2170 		 typical value as safety margin.  */
   2171 	      int rgap;
   2172 	      int safety_margin;
   2173 
   2174 	      int assumed_shrink = 600;
   2175 	      if (avr_pc_wrap_around > 0x4000)
   2176 		assumed_shrink = 900;
   2177 
   2178 	      safety_margin = 2 * assumed_shrink;
   2179 
   2180 	      rgap = avr_relative_distance_considering_wrap_around (gap);
   2181 
   2182 	      if (rgap >= (-4092 + safety_margin)
   2183 		  && rgap <= (4094 - safety_margin))
   2184 		distance_short_enough = 1;
   2185             }
   2186 
   2187             if (distance_short_enough)
   2188               {
   2189                 unsigned char code_msb;
   2190                 unsigned char code_lsb;
   2191 
   2192                 if (debug_relax)
   2193                   printf ("shrinking jump/call instruction at address 0x%x"
   2194                           " in section %s\n\n",
   2195                           (int) dot, sec->name);
   2196 
   2197                 /* Note that we've changed the relocs, section contents,
   2198                    etc.  */
   2199                 elf_section_data (sec)->relocs = internal_relocs;
   2200                 elf_section_data (sec)->this_hdr.contents = contents;
   2201                 symtab_hdr->contents = (unsigned char *) isymbuf;
   2202 
   2203                 /* Get the instruction code for relaxing.  */
   2204                 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset);
   2205                 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
   2206 
   2207                 /* Mask out the relocation bits.  */
   2208                 code_msb &= 0x94;
   2209                 code_lsb &= 0x0E;
   2210                 if (code_msb == 0x94 && code_lsb == 0x0E)
   2211                   {
   2212                     /* we are changing call -> rcall .  */
   2213                     bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
   2214                     bfd_put_8 (abfd, 0xD0, contents + irel->r_offset + 1);
   2215                   }
   2216                 else if (code_msb == 0x94 && code_lsb == 0x0C)
   2217                   {
   2218                     /* we are changeing jump -> rjmp.  */
   2219                     bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
   2220                     bfd_put_8 (abfd, 0xC0, contents + irel->r_offset + 1);
   2221                   }
   2222                 else
   2223                   abort ();
   2224 
   2225                 /* Fix the relocation's type.  */
   2226                 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   2227                                              R_AVR_13_PCREL);
   2228 
   2229                 /* We should not modify the ordering if 'shrinkable' is
   2230                    FALSE. */
   2231                 if (!shrinkable)
   2232                   {
   2233                     /* Let's insert a nop.  */
   2234                     bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 2);
   2235                     bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 3);
   2236                   }
   2237                 else
   2238                   {
   2239                     /* Delete two bytes of data.  */
   2240                     if (!elf32_avr_relax_delete_bytes (abfd, sec,
   2241                                                        irel->r_offset + 2, 2))
   2242                       goto error_return;
   2243 
   2244                     /* That will change things, so, we should relax again.
   2245                        Note that this is not required, and it may be slow.  */
   2246                     *again = TRUE;
   2247                   }
   2248               }
   2249           }
   2250 
   2251         default:
   2252           {
   2253             unsigned char code_msb;
   2254             unsigned char code_lsb;
   2255             bfd_vma dot;
   2256 
   2257             code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
   2258             code_lsb = bfd_get_8 (abfd, contents + irel->r_offset + 0);
   2259 
   2260             /* Get the address of this instruction.  */
   2261             dot = (sec->output_section->vma
   2262                    + sec->output_offset + irel->r_offset);
   2263 
   2264             /* Here we look for rcall/ret or call/ret sequences that could be
   2265                safely replaced by rjmp/ret or jmp/ret.  */
   2266             if (((code_msb & 0xf0) == 0xd0)
   2267                 && avr_replace_call_ret_sequences)
   2268               {
   2269                 /* This insn is a rcall.  */
   2270                 unsigned char next_insn_msb = 0;
   2271                 unsigned char next_insn_lsb = 0;
   2272 
   2273                 if (irel->r_offset + 3 < sec->size)
   2274                   {
   2275                     next_insn_msb =
   2276 		      bfd_get_8 (abfd, contents + irel->r_offset + 3);
   2277                     next_insn_lsb =
   2278 		      bfd_get_8 (abfd, contents + irel->r_offset + 2);
   2279                   }
   2280 
   2281 		if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
   2282                   {
   2283                     /* The next insn is a ret. We now convert the rcall insn
   2284                        into a rjmp instruction.  */
   2285                     code_msb &= 0xef;
   2286                     bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1);
   2287                     if (debug_relax)
   2288                       printf ("converted rcall/ret sequence at address 0x%x"
   2289                               " into rjmp/ret sequence. Section is %s\n\n",
   2290                               (int) dot, sec->name);
   2291                     *again = TRUE;
   2292                     break;
   2293                   }
   2294               }
   2295             else if ((0x94 == (code_msb & 0xfe))
   2296 		     && (0x0e == (code_lsb & 0x0e))
   2297 		     && avr_replace_call_ret_sequences)
   2298               {
   2299                 /* This insn is a call.  */
   2300                 unsigned char next_insn_msb = 0;
   2301                 unsigned char next_insn_lsb = 0;
   2302 
   2303                 if (irel->r_offset + 5 < sec->size)
   2304                   {
   2305                     next_insn_msb =
   2306 		      bfd_get_8 (abfd, contents + irel->r_offset + 5);
   2307                     next_insn_lsb =
   2308 		      bfd_get_8 (abfd, contents + irel->r_offset + 4);
   2309                   }
   2310 
   2311                 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
   2312                   {
   2313                     /* The next insn is a ret. We now convert the call insn
   2314                        into a jmp instruction.  */
   2315 
   2316                     code_lsb &= 0xfd;
   2317                     bfd_put_8 (abfd, code_lsb, contents + irel->r_offset);
   2318                     if (debug_relax)
   2319                       printf ("converted call/ret sequence at address 0x%x"
   2320                               " into jmp/ret sequence. Section is %s\n\n",
   2321                               (int) dot, sec->name);
   2322                     *again = TRUE;
   2323                     break;
   2324                   }
   2325               }
   2326             else if ((0xc0 == (code_msb & 0xf0))
   2327                      || ((0x94 == (code_msb & 0xfe))
   2328                          && (0x0c == (code_lsb & 0x0e))))
   2329               {
   2330                 /* This insn is a rjmp or a jmp.  */
   2331                 unsigned char next_insn_msb = 0;
   2332                 unsigned char next_insn_lsb = 0;
   2333                 int insn_size;
   2334 
   2335                 if (0xc0 == (code_msb & 0xf0))
   2336                   insn_size = 2; /* rjmp insn */
   2337                 else
   2338                   insn_size = 4; /* jmp insn */
   2339 
   2340                 if (irel->r_offset + insn_size + 1 < sec->size)
   2341                   {
   2342                     next_insn_msb =
   2343 		      bfd_get_8 (abfd, contents + irel->r_offset
   2344 				 + insn_size + 1);
   2345                     next_insn_lsb =
   2346 		      bfd_get_8 (abfd, contents + irel->r_offset
   2347 				 + insn_size);
   2348                   }
   2349 
   2350                 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
   2351                   {
   2352                     /* The next insn is a ret. We possibly could delete
   2353                        this ret. First we need to check for preceding
   2354                        sbis/sbic/sbrs or cpse "skip" instructions.  */
   2355 
   2356                     int there_is_preceding_non_skip_insn = 1;
   2357                     bfd_vma address_of_ret;
   2358 
   2359                     address_of_ret = dot + insn_size;
   2360 
   2361                     if (debug_relax && (insn_size == 2))
   2362                       printf ("found rjmp / ret sequence at address 0x%x\n",
   2363                               (int) dot);
   2364                     if (debug_relax && (insn_size == 4))
   2365                       printf ("found jmp / ret sequence at address 0x%x\n",
   2366                               (int) dot);
   2367 
   2368                     /* We have to make sure that there is a preceding insn.  */
   2369                     if (irel->r_offset >= 2)
   2370                       {
   2371                         unsigned char preceding_msb;
   2372                         unsigned char preceding_lsb;
   2373 
   2374                         preceding_msb =
   2375 			  bfd_get_8 (abfd, contents + irel->r_offset - 1);
   2376                         preceding_lsb =
   2377 			  bfd_get_8 (abfd, contents + irel->r_offset - 2);
   2378 
   2379                         /* sbic.  */
   2380                         if (0x99 == preceding_msb)
   2381                           there_is_preceding_non_skip_insn = 0;
   2382 
   2383                         /* sbis.  */
   2384                         if (0x9b == preceding_msb)
   2385                           there_is_preceding_non_skip_insn = 0;
   2386 
   2387                         /* sbrc */
   2388                         if ((0xfc == (preceding_msb & 0xfe)
   2389 			     && (0x00 == (preceding_lsb & 0x08))))
   2390                           there_is_preceding_non_skip_insn = 0;
   2391 
   2392                         /* sbrs */
   2393                         if ((0xfe == (preceding_msb & 0xfe)
   2394 			     && (0x00 == (preceding_lsb & 0x08))))
   2395                           there_is_preceding_non_skip_insn = 0;
   2396 
   2397                         /* cpse */
   2398                         if (0x10 == (preceding_msb & 0xfc))
   2399                           there_is_preceding_non_skip_insn = 0;
   2400 
   2401                         if (there_is_preceding_non_skip_insn == 0)
   2402                           if (debug_relax)
   2403                             printf ("preceding skip insn prevents deletion of"
   2404                                     " ret insn at Addy 0x%x in section %s\n",
   2405                                     (int) dot + 2, sec->name);
   2406                       }
   2407                     else
   2408                       {
   2409                         /* There is no previous instruction.  */
   2410                         there_is_preceding_non_skip_insn = 0;
   2411                       }
   2412 
   2413                     if (there_is_preceding_non_skip_insn)
   2414                       {
   2415                         /* We now only have to make sure that there is no
   2416                            local label defined at the address of the ret
   2417                            instruction and that there is no local relocation
   2418                            in this section pointing to the ret.  */
   2419 
   2420                         int deleting_ret_is_safe = 1;
   2421                         unsigned int section_offset_of_ret_insn =
   2422 			  irel->r_offset + insn_size;
   2423                         Elf_Internal_Sym *isym, *isymend;
   2424                         unsigned int sec_shndx;
   2425 			struct bfd_section *isec;
   2426 
   2427                         sec_shndx =
   2428 			  _bfd_elf_section_from_bfd_section (abfd, sec);
   2429 
   2430                         /* Check for local symbols.  */
   2431                         isym = (Elf_Internal_Sym *) symtab_hdr->contents;
   2432                         isymend = isym + symtab_hdr->sh_info;
   2433 			/* PR 6019: There may not be any local symbols.  */
   2434                         for (; isym != NULL && isym < isymend; isym++)
   2435 			  {
   2436 			    if (isym->st_value == section_offset_of_ret_insn
   2437 				&& isym->st_shndx == sec_shndx)
   2438 			      {
   2439 				deleting_ret_is_safe = 0;
   2440 				if (debug_relax)
   2441 				  printf ("local label prevents deletion of ret "
   2442 					  "insn at address 0x%x\n",
   2443 					  (int) dot + insn_size);
   2444 			      }
   2445 			  }
   2446 
   2447 			/* Now check for global symbols.  */
   2448 			{
   2449 			  int symcount;
   2450 			  struct elf_link_hash_entry **sym_hashes;
   2451 			  struct elf_link_hash_entry **end_hashes;
   2452 
   2453 			  symcount = (symtab_hdr->sh_size
   2454 				      / sizeof (Elf32_External_Sym)
   2455 				      - symtab_hdr->sh_info);
   2456 			  sym_hashes = elf_sym_hashes (abfd);
   2457 			  end_hashes = sym_hashes + symcount;
   2458 			  for (; sym_hashes < end_hashes; sym_hashes++)
   2459 			    {
   2460 			      struct elf_link_hash_entry *sym_hash =
   2461 				*sym_hashes;
   2462 			      if ((sym_hash->root.type == bfd_link_hash_defined
   2463 				   || sym_hash->root.type ==
   2464 				   bfd_link_hash_defweak)
   2465 				  && sym_hash->root.u.def.section == sec
   2466 				  && sym_hash->root.u.def.value == section_offset_of_ret_insn)
   2467 				{
   2468 				  deleting_ret_is_safe = 0;
   2469 				  if (debug_relax)
   2470 				    printf ("global label prevents deletion of "
   2471 					    "ret insn at address 0x%x\n",
   2472 					    (int) dot + insn_size);
   2473 				}
   2474 			    }
   2475 			}
   2476 
   2477 			/* Now we check for relocations pointing to ret.  */
   2478 			for (isec = abfd->sections; isec && deleting_ret_is_safe; isec = isec->next)
   2479 			  {
   2480 			    Elf_Internal_Rela *rel;
   2481 			    Elf_Internal_Rela *relend;
   2482 
   2483 			    rel = elf_section_data (isec)->relocs;
   2484 			    if (rel == NULL)
   2485 			      rel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE);
   2486 
   2487 			    relend = rel + isec->reloc_count;
   2488 
   2489 			    for (; rel && rel < relend; rel++)
   2490 			      {
   2491 				bfd_vma reloc_target = 0;
   2492 
   2493 				/* Read this BFD's local symbols if we haven't
   2494 				   done so already.  */
   2495 				if (isymbuf == NULL && symtab_hdr->sh_info != 0)
   2496 				  {
   2497 				    isymbuf = (Elf_Internal_Sym *)
   2498 				      symtab_hdr->contents;
   2499 				    if (isymbuf == NULL)
   2500 				      isymbuf = bfd_elf_get_elf_syms
   2501 					(abfd,
   2502 					 symtab_hdr,
   2503 					 symtab_hdr->sh_info, 0,
   2504 					 NULL, NULL, NULL);
   2505 				    if (isymbuf == NULL)
   2506 				      break;
   2507 				  }
   2508 
   2509 				/* Get the value of the symbol referred to
   2510 				   by the reloc.  */
   2511 				if (ELF32_R_SYM (rel->r_info)
   2512 				    < symtab_hdr->sh_info)
   2513 				  {
   2514 				    /* A local symbol.  */
   2515 				    asection *sym_sec;
   2516 
   2517 				    isym = isymbuf
   2518 				      + ELF32_R_SYM (rel->r_info);
   2519 				    sym_sec = bfd_section_from_elf_index
   2520 				      (abfd, isym->st_shndx);
   2521 				    symval = isym->st_value;
   2522 
   2523 				    /* If the reloc is absolute, it will not
   2524 				       have a symbol or section associated
   2525 				       with it.  */
   2526 
   2527 				    if (sym_sec)
   2528 				      {
   2529 					symval +=
   2530 					  sym_sec->output_section->vma
   2531 					  + sym_sec->output_offset;
   2532 					reloc_target = symval + rel->r_addend;
   2533 				      }
   2534 				    else
   2535 				      {
   2536 					reloc_target = symval + rel->r_addend;
   2537 					/* Reference symbol is absolute.  */
   2538 				      }
   2539 				  }
   2540 				/* else ... reference symbol is extern.  */
   2541 
   2542 				if (address_of_ret == reloc_target)
   2543 				  {
   2544 				    deleting_ret_is_safe = 0;
   2545 				    if (debug_relax)
   2546 				      printf ("ret from "
   2547 					      "rjmp/jmp ret sequence at address"
   2548 					      " 0x%x could not be deleted. ret"
   2549 					      " is target of a relocation.\n",
   2550 					      (int) address_of_ret);
   2551 				    break;
   2552 				  }
   2553 			      }
   2554 			  }
   2555 
   2556 			if (deleting_ret_is_safe)
   2557 			  {
   2558 			    if (debug_relax)
   2559 			      printf ("unreachable ret instruction "
   2560 				      "at address 0x%x deleted.\n",
   2561 				      (int) dot + insn_size);
   2562 
   2563 			    /* Delete two bytes of data.  */
   2564 			    if (!elf32_avr_relax_delete_bytes (abfd, sec,
   2565 							       irel->r_offset + insn_size, 2))
   2566 			      goto error_return;
   2567 
   2568 			    /* That will change things, so, we should relax
   2569 			       again. Note that this is not required, and it
   2570 			       may be slow.  */
   2571 			    *again = TRUE;
   2572 			    break;
   2573 			  }
   2574                       }
   2575                   }
   2576               }
   2577             break;
   2578           }
   2579         }
   2580     }
   2581 
   2582   if (contents != NULL
   2583       && elf_section_data (sec)->this_hdr.contents != contents)
   2584     {
   2585       if (! link_info->keep_memory)
   2586         free (contents);
   2587       else
   2588         {
   2589           /* Cache the section contents for elf_link_input_bfd.  */
   2590           elf_section_data (sec)->this_hdr.contents = contents;
   2591         }
   2592     }
   2593 
   2594   if (internal_relocs != NULL
   2595       && elf_section_data (sec)->relocs != internal_relocs)
   2596     free (internal_relocs);
   2597 
   2598   return TRUE;
   2599 
   2600  error_return:
   2601   if (isymbuf != NULL
   2602       && symtab_hdr->contents != (unsigned char *) isymbuf)
   2603     free (isymbuf);
   2604   if (contents != NULL
   2605       && elf_section_data (sec)->this_hdr.contents != contents)
   2606     free (contents);
   2607   if (internal_relocs != NULL
   2608       && elf_section_data (sec)->relocs != internal_relocs)
   2609     free (internal_relocs);
   2610 
   2611   return FALSE;
   2612 }
   2613 
   2614 /* This is a version of bfd_generic_get_relocated_section_contents
   2615    which uses elf32_avr_relocate_section.
   2616 
   2617    For avr it's essentially a cut and paste taken from the H8300 port.
   2618    The author of the relaxation support patch for avr had absolutely no
   2619    clue what is happening here but found out that this part of the code
   2620    seems to be important.  */
   2621 
   2622 static bfd_byte *
   2623 elf32_avr_get_relocated_section_contents (bfd *output_bfd,
   2624                                           struct bfd_link_info *link_info,
   2625                                           struct bfd_link_order *link_order,
   2626                                           bfd_byte *data,
   2627                                           bfd_boolean relocatable,
   2628                                           asymbol **symbols)
   2629 {
   2630   Elf_Internal_Shdr *symtab_hdr;
   2631   asection *input_section = link_order->u.indirect.section;
   2632   bfd *input_bfd = input_section->owner;
   2633   asection **sections = NULL;
   2634   Elf_Internal_Rela *internal_relocs = NULL;
   2635   Elf_Internal_Sym *isymbuf = NULL;
   2636 
   2637   /* We only need to handle the case of relaxing, or of having a
   2638      particular set of section contents, specially.  */
   2639   if (relocatable
   2640       || elf_section_data (input_section)->this_hdr.contents == NULL)
   2641     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
   2642                                                        link_order, data,
   2643                                                        relocatable,
   2644                                                        symbols);
   2645   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   2646 
   2647   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
   2648           (size_t) input_section->size);
   2649 
   2650   if ((input_section->flags & SEC_RELOC) != 0
   2651       && input_section->reloc_count > 0)
   2652     {
   2653       asection **secpp;
   2654       Elf_Internal_Sym *isym, *isymend;
   2655       bfd_size_type amt;
   2656 
   2657       internal_relocs = (_bfd_elf_link_read_relocs
   2658                          (input_bfd, input_section, NULL, NULL, FALSE));
   2659       if (internal_relocs == NULL)
   2660         goto error_return;
   2661 
   2662       if (symtab_hdr->sh_info != 0)
   2663         {
   2664           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
   2665           if (isymbuf == NULL)
   2666             isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
   2667                                             symtab_hdr->sh_info, 0,
   2668                                             NULL, NULL, NULL);
   2669           if (isymbuf == NULL)
   2670             goto error_return;
   2671         }
   2672 
   2673       amt = symtab_hdr->sh_info;
   2674       amt *= sizeof (asection *);
   2675       sections = bfd_malloc (amt);
   2676       if (sections == NULL && amt != 0)
   2677         goto error_return;
   2678 
   2679       isymend = isymbuf + symtab_hdr->sh_info;
   2680       for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
   2681         {
   2682           asection *isec;
   2683 
   2684           if (isym->st_shndx == SHN_UNDEF)
   2685             isec = bfd_und_section_ptr;
   2686           else if (isym->st_shndx == SHN_ABS)
   2687             isec = bfd_abs_section_ptr;
   2688           else if (isym->st_shndx == SHN_COMMON)
   2689             isec = bfd_com_section_ptr;
   2690           else
   2691             isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
   2692 
   2693           *secpp = isec;
   2694         }
   2695 
   2696       if (! elf32_avr_relocate_section (output_bfd, link_info, input_bfd,
   2697                                         input_section, data, internal_relocs,
   2698                                         isymbuf, sections))
   2699         goto error_return;
   2700 
   2701       if (sections != NULL)
   2702         free (sections);
   2703       if (isymbuf != NULL
   2704           && symtab_hdr->contents != (unsigned char *) isymbuf)
   2705         free (isymbuf);
   2706       if (elf_section_data (input_section)->relocs != internal_relocs)
   2707         free (internal_relocs);
   2708     }
   2709 
   2710   return data;
   2711 
   2712  error_return:
   2713   if (sections != NULL)
   2714     free (sections);
   2715   if (isymbuf != NULL
   2716       && symtab_hdr->contents != (unsigned char *) isymbuf)
   2717     free (isymbuf);
   2718   if (internal_relocs != NULL
   2719       && elf_section_data (input_section)->relocs != internal_relocs)
   2720     free (internal_relocs);
   2721   return NULL;
   2722 }
   2723 
   2724 
   2725 /* Determines the hash entry name for a particular reloc. It consists of
   2726    the identifier of the symbol section and the added reloc addend and
   2727    symbol offset relative to the section the symbol is attached to.  */
   2728 
   2729 static char *
   2730 avr_stub_name (const asection *symbol_section,
   2731                const bfd_vma symbol_offset,
   2732                const Elf_Internal_Rela *rela)
   2733 {
   2734   char *stub_name;
   2735   bfd_size_type len;
   2736 
   2737   len = 8 + 1 + 8 + 1 + 1;
   2738   stub_name = bfd_malloc (len);
   2739 
   2740   sprintf (stub_name, "%08x+%08x",
   2741            symbol_section->id & 0xffffffff,
   2742            (unsigned int) ((rela->r_addend & 0xffffffff) + symbol_offset));
   2743 
   2744   return stub_name;
   2745 }
   2746 
   2747 
   2748 /* Add a new stub entry to the stub hash.  Not all fields of the new
   2749    stub entry are initialised.  */
   2750 
   2751 static struct elf32_avr_stub_hash_entry *
   2752 avr_add_stub (const char *stub_name,
   2753               struct elf32_avr_link_hash_table *htab)
   2754 {
   2755   struct elf32_avr_stub_hash_entry *hsh;
   2756 
   2757   /* Enter this entry into the linker stub hash table.  */
   2758   hsh = avr_stub_hash_lookup (&htab->bstab, stub_name, TRUE, FALSE);
   2759 
   2760   if (hsh == NULL)
   2761     {
   2762       (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
   2763                              NULL, stub_name);
   2764       return NULL;
   2765     }
   2766 
   2767   hsh->stub_offset = 0;
   2768   return hsh;
   2769 }
   2770 
   2771 /* We assume that there is already space allocated for the stub section
   2772    contents and that before building the stubs the section size is
   2773    initialized to 0.  We assume that within the stub hash table entry,
   2774    the absolute position of the jmp target has been written in the
   2775    target_value field.  We write here the offset of the generated jmp insn
   2776    relative to the trampoline section start to the stub_offset entry in
   2777    the stub hash table entry.  */
   2778 
   2779 static  bfd_boolean
   2780 avr_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
   2781 {
   2782   struct elf32_avr_stub_hash_entry *hsh;
   2783   struct bfd_link_info *info;
   2784   struct elf32_avr_link_hash_table *htab;
   2785   bfd *stub_bfd;
   2786   bfd_byte *loc;
   2787   bfd_vma target;
   2788   bfd_vma starget;
   2789 
   2790   /* Basic opcode */
   2791   bfd_vma jmp_insn = 0x0000940c;
   2792 
   2793   /* Massage our args to the form they really have.  */
   2794   hsh = avr_stub_hash_entry (bh);
   2795 
   2796   if (!hsh->is_actually_needed)
   2797     return TRUE;
   2798 
   2799   info = (struct bfd_link_info *) in_arg;
   2800 
   2801   htab = avr_link_hash_table (info);
   2802   if (htab == NULL)
   2803     return FALSE;
   2804 
   2805   target = hsh->target_value;
   2806 
   2807   /* Make a note of the offset within the stubs for this entry.  */
   2808   hsh->stub_offset = htab->stub_sec->size;
   2809   loc = htab->stub_sec->contents + hsh->stub_offset;
   2810 
   2811   stub_bfd = htab->stub_sec->owner;
   2812 
   2813   if (debug_stubs)
   2814     printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
   2815              (unsigned int) target,
   2816              (unsigned int) hsh->stub_offset);
   2817 
   2818   /* We now have to add the information on the jump target to the bare
   2819      opcode bits already set in jmp_insn.  */
   2820 
   2821   /* Check for the alignment of the address.  */
   2822   if (target & 1)
   2823      return FALSE;
   2824 
   2825   starget = target >> 1;
   2826   jmp_insn |= ((starget & 0x10000) | ((starget << 3) & 0x1f00000)) >> 16;
   2827   bfd_put_16 (stub_bfd, jmp_insn, loc);
   2828   bfd_put_16 (stub_bfd, (bfd_vma) starget & 0xffff, loc + 2);
   2829 
   2830   htab->stub_sec->size += 4;
   2831 
   2832   /* Now add the entries in the address mapping table if there is still
   2833      space left.  */
   2834   {
   2835     unsigned int nr;
   2836 
   2837     nr = htab->amt_entry_cnt + 1;
   2838     if (nr <= htab->amt_max_entry_cnt)
   2839       {
   2840         htab->amt_entry_cnt = nr;
   2841 
   2842         htab->amt_stub_offsets[nr - 1] = hsh->stub_offset;
   2843         htab->amt_destination_addr[nr - 1] = target;
   2844       }
   2845   }
   2846 
   2847   return TRUE;
   2848 }
   2849 
   2850 static bfd_boolean
   2851 avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry *bh,
   2852                                    void *in_arg ATTRIBUTE_UNUSED)
   2853 {
   2854   struct elf32_avr_stub_hash_entry *hsh;
   2855 
   2856   hsh = avr_stub_hash_entry (bh);
   2857   hsh->is_actually_needed = FALSE;
   2858 
   2859   return TRUE;
   2860 }
   2861 
   2862 static bfd_boolean
   2863 avr_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
   2864 {
   2865   struct elf32_avr_stub_hash_entry *hsh;
   2866   struct elf32_avr_link_hash_table *htab;
   2867   int size;
   2868 
   2869   /* Massage our args to the form they really have.  */
   2870   hsh = avr_stub_hash_entry (bh);
   2871   htab = in_arg;
   2872 
   2873   if (hsh->is_actually_needed)
   2874     size = 4;
   2875   else
   2876     size = 0;
   2877 
   2878   htab->stub_sec->size += size;
   2879   return TRUE;
   2880 }
   2881 
   2882 void
   2883 elf32_avr_setup_params (struct bfd_link_info *info,
   2884                         bfd *avr_stub_bfd,
   2885                         asection *avr_stub_section,
   2886                         bfd_boolean no_stubs,
   2887                         bfd_boolean deb_stubs,
   2888                         bfd_boolean deb_relax,
   2889                         bfd_vma pc_wrap_around,
   2890                         bfd_boolean call_ret_replacement)
   2891 {
   2892   struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
   2893 
   2894   if (htab == NULL)
   2895     return;
   2896   htab->stub_sec = avr_stub_section;
   2897   htab->stub_bfd = avr_stub_bfd;
   2898   htab->no_stubs = no_stubs;
   2899 
   2900   debug_relax = deb_relax;
   2901   debug_stubs = deb_stubs;
   2902   avr_pc_wrap_around = pc_wrap_around;
   2903   avr_replace_call_ret_sequences = call_ret_replacement;
   2904 }
   2905 
   2906 
   2907 /* Set up various things so that we can make a list of input sections
   2908    for each output section included in the link.  Returns -1 on error,
   2909    0 when no stubs will be needed, and 1 on success.  It also sets
   2910    information on the stubs bfd and the stub section in the info
   2911    struct.  */
   2912 
   2913 int
   2914 elf32_avr_setup_section_lists (bfd *output_bfd,
   2915                                struct bfd_link_info *info)
   2916 {
   2917   bfd *input_bfd;
   2918   unsigned int bfd_count;
   2919   int top_id, top_index;
   2920   asection *section;
   2921   asection **input_list, **list;
   2922   bfd_size_type amt;
   2923   struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
   2924 
   2925   if (htab == NULL || htab->no_stubs)
   2926     return 0;
   2927 
   2928   /* Count the number of input BFDs and find the top input section id.  */
   2929   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
   2930        input_bfd != NULL;
   2931        input_bfd = input_bfd->link.next)
   2932     {
   2933       bfd_count += 1;
   2934       for (section = input_bfd->sections;
   2935            section != NULL;
   2936            section = section->next)
   2937 	if (top_id < section->id)
   2938 	  top_id = section->id;
   2939     }
   2940 
   2941   htab->bfd_count = bfd_count;
   2942 
   2943   /* We can't use output_bfd->section_count here to find the top output
   2944      section index as some sections may have been removed, and
   2945      strip_excluded_output_sections doesn't renumber the indices.  */
   2946   for (section = output_bfd->sections, top_index = 0;
   2947        section != NULL;
   2948        section = section->next)
   2949     if (top_index < section->index)
   2950       top_index = section->index;
   2951 
   2952   htab->top_index = top_index;
   2953   amt = sizeof (asection *) * (top_index + 1);
   2954   input_list = bfd_malloc (amt);
   2955   htab->input_list = input_list;
   2956   if (input_list == NULL)
   2957     return -1;
   2958 
   2959   /* For sections we aren't interested in, mark their entries with a
   2960      value we can check later.  */
   2961   list = input_list + top_index;
   2962   do
   2963     *list = bfd_abs_section_ptr;
   2964   while (list-- != input_list);
   2965 
   2966   for (section = output_bfd->sections;
   2967        section != NULL;
   2968        section = section->next)
   2969     if ((section->flags & SEC_CODE) != 0)
   2970       input_list[section->index] = NULL;
   2971 
   2972   return 1;
   2973 }
   2974 
   2975 
   2976 /* Read in all local syms for all input bfds, and create hash entries
   2977    for export stubs if we are building a multi-subspace shared lib.
   2978    Returns -1 on error, 0 otherwise.  */
   2979 
   2980 static int
   2981 get_local_syms (bfd *input_bfd, struct bfd_link_info *info)
   2982 {
   2983   unsigned int bfd_indx;
   2984   Elf_Internal_Sym *local_syms, **all_local_syms;
   2985   struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
   2986   bfd_size_type amt;
   2987 
   2988   if (htab == NULL)
   2989     return -1;
   2990 
   2991   /* We want to read in symbol extension records only once.  To do this
   2992      we need to read in the local symbols in parallel and save them for
   2993      later use; so hold pointers to the local symbols in an array.  */
   2994   amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
   2995   all_local_syms = bfd_zmalloc (amt);
   2996   htab->all_local_syms = all_local_syms;
   2997   if (all_local_syms == NULL)
   2998     return -1;
   2999 
   3000   /* Walk over all the input BFDs, swapping in local symbols.
   3001      If we are creating a shared library, create hash entries for the
   3002      export stubs.  */
   3003   for (bfd_indx = 0;
   3004        input_bfd != NULL;
   3005        input_bfd = input_bfd->link.next, bfd_indx++)
   3006     {
   3007       Elf_Internal_Shdr *symtab_hdr;
   3008 
   3009       /* We'll need the symbol table in a second.  */
   3010       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   3011       if (symtab_hdr->sh_info == 0)
   3012 	continue;
   3013 
   3014       /* We need an array of the local symbols attached to the input bfd.  */
   3015       local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
   3016       if (local_syms == NULL)
   3017 	{
   3018 	  local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
   3019 					     symtab_hdr->sh_info, 0,
   3020 					     NULL, NULL, NULL);
   3021 	  /* Cache them for elf_link_input_bfd.  */
   3022 	  symtab_hdr->contents = (unsigned char *) local_syms;
   3023 	}
   3024       if (local_syms == NULL)
   3025 	return -1;
   3026 
   3027       all_local_syms[bfd_indx] = local_syms;
   3028     }
   3029 
   3030   return 0;
   3031 }
   3032 
   3033 #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
   3034 
   3035 bfd_boolean
   3036 elf32_avr_size_stubs (bfd *output_bfd,
   3037                       struct bfd_link_info *info,
   3038                       bfd_boolean is_prealloc_run)
   3039 {
   3040   struct elf32_avr_link_hash_table *htab;
   3041   int stub_changed = 0;
   3042 
   3043   htab = avr_link_hash_table (info);
   3044   if (htab == NULL)
   3045     return FALSE;
   3046 
   3047   /* At this point we initialize htab->vector_base
   3048      To the start of the text output section.  */
   3049   htab->vector_base = htab->stub_sec->output_section->vma;
   3050 
   3051   if (get_local_syms (info->input_bfds, info))
   3052     {
   3053       if (htab->all_local_syms)
   3054 	goto error_ret_free_local;
   3055       return FALSE;
   3056     }
   3057 
   3058   if (ADD_DUMMY_STUBS_FOR_DEBUGGING)
   3059     {
   3060       struct elf32_avr_stub_hash_entry *test;
   3061 
   3062       test = avr_add_stub ("Hugo",htab);
   3063       test->target_value = 0x123456;
   3064       test->stub_offset = 13;
   3065 
   3066       test = avr_add_stub ("Hugo2",htab);
   3067       test->target_value = 0x84210;
   3068       test->stub_offset = 14;
   3069     }
   3070 
   3071   while (1)
   3072     {
   3073       bfd *input_bfd;
   3074       unsigned int bfd_indx;
   3075 
   3076       /* We will have to re-generate the stub hash table each time anything
   3077          in memory has changed.  */
   3078 
   3079       bfd_hash_traverse (&htab->bstab, avr_mark_stub_not_to_be_necessary, htab);
   3080       for (input_bfd = info->input_bfds, bfd_indx = 0;
   3081            input_bfd != NULL;
   3082            input_bfd = input_bfd->link.next, bfd_indx++)
   3083         {
   3084           Elf_Internal_Shdr *symtab_hdr;
   3085           asection *section;
   3086           Elf_Internal_Sym *local_syms;
   3087 
   3088           /* We'll need the symbol table in a second.  */
   3089           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   3090           if (symtab_hdr->sh_info == 0)
   3091             continue;
   3092 
   3093           local_syms = htab->all_local_syms[bfd_indx];
   3094 
   3095           /* Walk over each section attached to the input bfd.  */
   3096           for (section = input_bfd->sections;
   3097                section != NULL;
   3098                section = section->next)
   3099             {
   3100               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
   3101 
   3102               /* If there aren't any relocs, then there's nothing more
   3103                  to do.  */
   3104               if ((section->flags & SEC_RELOC) == 0
   3105                   || section->reloc_count == 0)
   3106                 continue;
   3107 
   3108               /* If this section is a link-once section that will be
   3109                  discarded, then don't create any stubs.  */
   3110               if (section->output_section == NULL
   3111                   || section->output_section->owner != output_bfd)
   3112                 continue;
   3113 
   3114               /* Get the relocs.  */
   3115               internal_relocs
   3116                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
   3117                                              info->keep_memory);
   3118               if (internal_relocs == NULL)
   3119                 goto error_ret_free_local;
   3120 
   3121               /* Now examine each relocation.  */
   3122               irela = internal_relocs;
   3123               irelaend = irela + section->reloc_count;
   3124               for (; irela < irelaend; irela++)
   3125                 {
   3126                   unsigned int r_type, r_indx;
   3127                   struct elf32_avr_stub_hash_entry *hsh;
   3128                   asection *sym_sec;
   3129                   bfd_vma sym_value;
   3130                   bfd_vma destination;
   3131                   struct elf_link_hash_entry *hh;
   3132                   char *stub_name;
   3133 
   3134                   r_type = ELF32_R_TYPE (irela->r_info);
   3135                   r_indx = ELF32_R_SYM (irela->r_info);
   3136 
   3137                   /* Only look for 16 bit GS relocs. No other reloc will need a
   3138                      stub.  */
   3139                   if (!((r_type == R_AVR_16_PM)
   3140                         || (r_type == R_AVR_LO8_LDI_GS)
   3141                         || (r_type == R_AVR_HI8_LDI_GS)))
   3142                     continue;
   3143 
   3144                   /* Now determine the call target, its name, value,
   3145                      section.  */
   3146                   sym_sec = NULL;
   3147                   sym_value = 0;
   3148                   destination = 0;
   3149                   hh = NULL;
   3150                   if (r_indx < symtab_hdr->sh_info)
   3151                     {
   3152                       /* It's a local symbol.  */
   3153                       Elf_Internal_Sym *sym;
   3154                       Elf_Internal_Shdr *hdr;
   3155 		      unsigned int shndx;
   3156 
   3157                       sym = local_syms + r_indx;
   3158                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
   3159                         sym_value = sym->st_value;
   3160 		      shndx = sym->st_shndx;
   3161 		      if (shndx < elf_numsections (input_bfd))
   3162 			{
   3163 			  hdr = elf_elfsections (input_bfd)[shndx];
   3164 			  sym_sec = hdr->bfd_section;
   3165 			  destination = (sym_value + irela->r_addend
   3166 					 + sym_sec->output_offset
   3167 					 + sym_sec->output_section->vma);
   3168 			}
   3169                     }
   3170                   else
   3171                     {
   3172                       /* It's an external symbol.  */
   3173                       int e_indx;
   3174 
   3175                       e_indx = r_indx - symtab_hdr->sh_info;
   3176                       hh = elf_sym_hashes (input_bfd)[e_indx];
   3177 
   3178                       while (hh->root.type == bfd_link_hash_indirect
   3179                              || hh->root.type == bfd_link_hash_warning)
   3180                         hh = (struct elf_link_hash_entry *)
   3181                               (hh->root.u.i.link);
   3182 
   3183                       if (hh->root.type == bfd_link_hash_defined
   3184                           || hh->root.type == bfd_link_hash_defweak)
   3185                         {
   3186                           sym_sec = hh->root.u.def.section;
   3187                           sym_value = hh->root.u.def.value;
   3188                           if (sym_sec->output_section != NULL)
   3189                           destination = (sym_value + irela->r_addend
   3190                                          + sym_sec->output_offset
   3191                                          + sym_sec->output_section->vma);
   3192                         }
   3193                       else if (hh->root.type == bfd_link_hash_undefweak)
   3194                         {
   3195                           if (! info->shared)
   3196                             continue;
   3197                         }
   3198                       else if (hh->root.type == bfd_link_hash_undefined)
   3199                         {
   3200                           if (! (info->unresolved_syms_in_objects == RM_IGNORE
   3201                                  && (ELF_ST_VISIBILITY (hh->other)
   3202                                      == STV_DEFAULT)))
   3203                              continue;
   3204                         }
   3205                       else
   3206                         {
   3207                           bfd_set_error (bfd_error_bad_value);
   3208 
   3209                           error_ret_free_internal:
   3210                           if (elf_section_data (section)->relocs == NULL)
   3211                             free (internal_relocs);
   3212                           goto error_ret_free_local;
   3213                         }
   3214                     }
   3215 
   3216                   if (! avr_stub_is_required_for_16_bit_reloc
   3217 		      (destination - htab->vector_base))
   3218                     {
   3219                       if (!is_prealloc_run)
   3220 			/* We are having a reloc that does't need a stub.  */
   3221 			continue;
   3222 
   3223 		      /* We don't right now know if a stub will be needed.
   3224 			 Let's rather be on the safe side.  */
   3225                     }
   3226 
   3227                   /* Get the name of this stub.  */
   3228                   stub_name = avr_stub_name (sym_sec, sym_value, irela);
   3229 
   3230                   if (!stub_name)
   3231                     goto error_ret_free_internal;
   3232 
   3233 
   3234                   hsh = avr_stub_hash_lookup (&htab->bstab,
   3235                                               stub_name,
   3236                                               FALSE, FALSE);
   3237                   if (hsh != NULL)
   3238                     {
   3239                       /* The proper stub has already been created.  Mark it
   3240                          to be used and write the possibly changed destination
   3241                          value.  */
   3242                       hsh->is_actually_needed = TRUE;
   3243                       hsh->target_value = destination;
   3244                       free (stub_name);
   3245                       continue;
   3246                     }
   3247 
   3248                   hsh = avr_add_stub (stub_name, htab);
   3249                   if (hsh == NULL)
   3250                     {
   3251                       free (stub_name);
   3252                       goto error_ret_free_internal;
   3253                     }
   3254 
   3255                   hsh->is_actually_needed = TRUE;
   3256                   hsh->target_value = destination;
   3257 
   3258                   if (debug_stubs)
   3259                     printf ("Adding stub with destination 0x%x to the"
   3260                             " hash table.\n", (unsigned int) destination);
   3261                   if (debug_stubs)
   3262                     printf ("(Pre-Alloc run: %i)\n", is_prealloc_run);
   3263 
   3264                   stub_changed = TRUE;
   3265                 }
   3266 
   3267               /* We're done with the internal relocs, free them.  */
   3268               if (elf_section_data (section)->relocs == NULL)
   3269                 free (internal_relocs);
   3270             }
   3271         }
   3272 
   3273       /* Re-Calculate the number of needed stubs.  */
   3274       htab->stub_sec->size = 0;
   3275       bfd_hash_traverse (&htab->bstab, avr_size_one_stub, htab);
   3276 
   3277       if (!stub_changed)
   3278         break;
   3279 
   3280       stub_changed = FALSE;
   3281     }
   3282 
   3283   free (htab->all_local_syms);
   3284   return TRUE;
   3285 
   3286  error_ret_free_local:
   3287   free (htab->all_local_syms);
   3288   return FALSE;
   3289 }
   3290 
   3291 
   3292 /* Build all the stubs associated with the current output file.  The
   3293    stubs are kept in a hash table attached to the main linker hash
   3294    table.  We also set up the .plt entries for statically linked PIC
   3295    functions here.  This function is called via hppaelf_finish in the
   3296    linker.  */
   3297 
   3298 bfd_boolean
   3299 elf32_avr_build_stubs (struct bfd_link_info *info)
   3300 {
   3301   asection *stub_sec;
   3302   struct bfd_hash_table *table;
   3303   struct elf32_avr_link_hash_table *htab;
   3304   bfd_size_type total_size = 0;
   3305 
   3306   htab = avr_link_hash_table (info);
   3307   if (htab == NULL)
   3308     return FALSE;
   3309 
   3310   /* In case that there were several stub sections:  */
   3311   for (stub_sec = htab->stub_bfd->sections;
   3312        stub_sec != NULL;
   3313        stub_sec = stub_sec->next)
   3314     {
   3315       bfd_size_type size;
   3316 
   3317       /* Allocate memory to hold the linker stubs.  */
   3318       size = stub_sec->size;
   3319       total_size += size;
   3320 
   3321       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
   3322       if (stub_sec->contents == NULL && size != 0)
   3323 	return FALSE;
   3324       stub_sec->size = 0;
   3325     }
   3326 
   3327   /* Allocate memory for the adress mapping table.  */
   3328   htab->amt_entry_cnt = 0;
   3329   htab->amt_max_entry_cnt = total_size / 4;
   3330   htab->amt_stub_offsets = bfd_malloc (sizeof (bfd_vma)
   3331                                        * htab->amt_max_entry_cnt);
   3332   htab->amt_destination_addr = bfd_malloc (sizeof (bfd_vma)
   3333 					   * htab->amt_max_entry_cnt );
   3334 
   3335   if (debug_stubs)
   3336     printf ("Allocating %i entries in the AMT\n", htab->amt_max_entry_cnt);
   3337 
   3338   /* Build the stubs as directed by the stub hash table.  */
   3339   table = &htab->bstab;
   3340   bfd_hash_traverse (table, avr_build_one_stub, info);
   3341 
   3342   if (debug_stubs)
   3343     printf ("Final Stub section Size: %i\n", (int) htab->stub_sec->size);
   3344 
   3345   return TRUE;
   3346 }
   3347 
   3348 #define ELF_ARCH		bfd_arch_avr
   3349 #define ELF_TARGET_ID		AVR_ELF_DATA
   3350 #define ELF_MACHINE_CODE	EM_AVR
   3351 #define ELF_MACHINE_ALT1	EM_AVR_OLD
   3352 #define ELF_MAXPAGESIZE		1
   3353 
   3354 #define TARGET_LITTLE_SYM       avr_elf32_vec
   3355 #define TARGET_LITTLE_NAME	"elf32-avr"
   3356 
   3357 #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
   3358 
   3359 #define elf_info_to_howto	             avr_info_to_howto_rela
   3360 #define elf_info_to_howto_rel	             NULL
   3361 #define elf_backend_relocate_section         elf32_avr_relocate_section
   3362 #define elf_backend_can_gc_sections          1
   3363 #define elf_backend_rela_normal		     1
   3364 #define elf_backend_final_write_processing \
   3365 					bfd_elf_avr_final_write_processing
   3366 #define elf_backend_object_p		elf32_avr_object_p
   3367 
   3368 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
   3369 #define bfd_elf32_bfd_get_relocated_section_contents \
   3370                                         elf32_avr_get_relocated_section_contents
   3371 
   3372 #include "elf32-target.h"
   3373