Back to index

cell-binutils  2.17cvs20070401
elf32-avr.c
Go to the documentation of this file.
00001 /* AVR-specific support for 32-bit ELF
00002    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007
00003    Free Software Foundation, Inc.
00004    Contributed by Denis Chertykov <denisc@overta.ru>
00005 
00006    This file is part of BFD, the Binary File Descriptor library.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin Street - Fifth Floor,
00021    Boston, MA 02110-1301, USA.  */
00022 
00023 #include "bfd.h"
00024 #include "sysdep.h"
00025 #include "libbfd.h"
00026 #include "elf-bfd.h"
00027 #include "elf/avr.h"
00028 #include "elf32-avr.h"
00029 
00030 /* Enable debugging printout at stdout with this variable.  */
00031 static bfd_boolean debug_relax = FALSE;
00032 
00033 /* Enable debugging printout at stdout with this variable.  */
00034 static bfd_boolean debug_stubs = FALSE;
00035 
00036 /* Hash table initialization and handling.  Code is taken from the hppa port
00037    and adapted to the needs of AVR.  */
00038 
00039 /* We use two hash tables to hold information for linking avr objects.
00040 
00041    The first is the elf32_avr_link_hash_tablse which is derived from the
00042    stanard ELF linker hash table.  We use this as a place to attach the other
00043    hash table and some static information.
00044 
00045    The second is the stub hash table which is derived from the base BFD
00046    hash table.  The stub hash table holds the information on the linker
00047    stubs.  */
00048 
00049 struct elf32_avr_stub_hash_entry
00050 {
00051   /* Base hash table entry structure.  */
00052   struct bfd_hash_entry bh_root;
00053 
00054   /* Offset within stub_sec of the beginning of this stub.  */
00055   bfd_vma stub_offset;
00056 
00057   /* Given the symbol's value and its section we can determine its final
00058      value when building the stubs (so the stub knows where to jump).  */
00059   bfd_vma target_value;
00060 
00061   /* This way we could mark stubs to be no longer necessary.  */
00062   bfd_boolean is_actually_needed;
00063 };
00064 
00065 struct elf32_avr_link_hash_table
00066 {
00067   /* The main hash table.  */
00068   struct elf_link_hash_table etab;
00069 
00070   /* The stub hash table.  */
00071   struct bfd_hash_table bstab;
00072 
00073   bfd_boolean no_stubs;
00074 
00075   /* Linker stub bfd.  */
00076   bfd *stub_bfd;
00077 
00078   /* The stub section.  */
00079   asection *stub_sec;
00080 
00081   /* Usually 0, unless we are generating code for a bootloader.  Will
00082      be initialized by elf32_avr_size_stubs to the vma offset of the
00083      output section associated with the stub section.  */
00084   bfd_vma vector_base;
00085 
00086   /* Assorted information used by elf32_avr_size_stubs.  */
00087   unsigned int        bfd_count;
00088   int                 top_index;
00089   asection **         input_list;
00090   Elf_Internal_Sym ** all_local_syms;
00091 
00092   /* Tables for mapping vma beyond the 128k boundary to the address of the
00093      corresponding stub.  (AMT)
00094      "amt_max_entry_cnt" reflects the number of entries that memory is allocated
00095      for in the "amt_stub_offsets" and "amt_destination_addr" arrays.
00096      "amt_entry_cnt" informs how many of these entries actually contain
00097      useful data.  */
00098   unsigned int amt_entry_cnt;
00099   unsigned int amt_max_entry_cnt;
00100   bfd_vma *    amt_stub_offsets;
00101   bfd_vma *    amt_destination_addr;
00102 };
00103 
00104 /* Various hash macros and functions.  */
00105 #define avr_link_hash_table(p) \
00106   /* PR 3874: Check that we have an AVR style hash table before using it.  */\
00107   ((p)->hash->table.newfunc != elf32_avr_link_hash_newfunc ? NULL : \
00108    ((struct elf32_avr_link_hash_table *) ((p)->hash)))
00109 
00110 #define avr_stub_hash_entry(ent) \
00111   ((struct elf32_avr_stub_hash_entry *)(ent))
00112 
00113 #define avr_stub_hash_lookup(table, string, create, copy) \
00114   ((struct elf32_avr_stub_hash_entry *) \
00115    bfd_hash_lookup ((table), (string), (create), (copy)))
00116 
00117 static reloc_howto_type elf_avr_howto_table[] =
00118 {
00119   HOWTO (R_AVR_NONE,        /* type */
00120         0,                  /* rightshift */
00121         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00122         32,                 /* bitsize */
00123         FALSE,                     /* pc_relative */
00124         0,                  /* bitpos */
00125         complain_overflow_bitfield, /* complain_on_overflow */
00126         bfd_elf_generic_reloc,     /* special_function */
00127         "R_AVR_NONE",              /* name */
00128         FALSE,                     /* partial_inplace */
00129         0,                  /* src_mask */
00130         0,                  /* dst_mask */
00131         FALSE),             /* pcrel_offset */
00132 
00133   HOWTO (R_AVR_32,          /* type */
00134         0,                  /* rightshift */
00135         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00136         32,                 /* bitsize */
00137         FALSE,                     /* pc_relative */
00138         0,                  /* bitpos */
00139         complain_overflow_bitfield, /* complain_on_overflow */
00140         bfd_elf_generic_reloc,     /* special_function */
00141         "R_AVR_32",         /* name */
00142         FALSE,                     /* partial_inplace */
00143         0xffffffff,         /* src_mask */
00144         0xffffffff,         /* dst_mask */
00145         FALSE),             /* pcrel_offset */
00146 
00147   /* A 7 bit PC relative relocation.  */
00148   HOWTO (R_AVR_7_PCREL,            /* type */
00149         1,                  /* rightshift */
00150         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00151         7,                  /* bitsize */
00152         TRUE,               /* pc_relative */
00153         3,                  /* bitpos */
00154         complain_overflow_bitfield, /* complain_on_overflow */
00155         bfd_elf_generic_reloc, /* special_function */
00156         "R_AVR_7_PCREL",    /* name */
00157         FALSE,                     /* partial_inplace */
00158         0xffff,             /* src_mask */
00159         0xffff,             /* dst_mask */
00160         TRUE),                     /* pcrel_offset */
00161 
00162   /* A 13 bit PC relative relocation.  */
00163   HOWTO (R_AVR_13_PCREL,    /* type */
00164         1,                  /* rightshift */
00165         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00166         13,                 /* bitsize */
00167         TRUE,               /* pc_relative */
00168         0,                  /* bitpos */
00169         complain_overflow_bitfield, /* complain_on_overflow */
00170         bfd_elf_generic_reloc, /* special_function */
00171         "R_AVR_13_PCREL",   /* name */
00172         FALSE,                     /* partial_inplace */
00173         0xfff,                     /* src_mask */
00174         0xfff,                     /* dst_mask */
00175         TRUE),                     /* pcrel_offset */
00176 
00177   /* A 16 bit absolute relocation.  */
00178   HOWTO (R_AVR_16,          /* type */
00179         0,                  /* rightshift */
00180         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00181         16,                 /* bitsize */
00182         FALSE,                     /* pc_relative */
00183         0,                  /* bitpos */
00184         complain_overflow_dont, /* complain_on_overflow */
00185         bfd_elf_generic_reloc,     /* special_function */
00186         "R_AVR_16",         /* name */
00187         FALSE,                     /* partial_inplace */
00188         0xffff,             /* src_mask */
00189         0xffff,             /* dst_mask */
00190         FALSE),             /* pcrel_offset */
00191 
00192   /* A 16 bit absolute relocation for command address
00193      Will be changed when linker stubs are needed.  */
00194   HOWTO (R_AVR_16_PM,              /* type */
00195         1,                  /* rightshift */
00196         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00197         16,                 /* bitsize */
00198         FALSE,                     /* pc_relative */
00199         0,                  /* bitpos */
00200         complain_overflow_bitfield, /* complain_on_overflow */
00201         bfd_elf_generic_reloc,     /* special_function */
00202         "R_AVR_16_PM",             /* name */
00203         FALSE,                     /* partial_inplace */
00204         0xffff,             /* src_mask */
00205         0xffff,             /* dst_mask */
00206         FALSE),             /* pcrel_offset */
00207   /* A low 8 bit absolute relocation of 16 bit address.
00208      For LDI command.  */
00209   HOWTO (R_AVR_LO8_LDI,            /* type */
00210         0,                  /* rightshift */
00211         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00212         8,                  /* bitsize */
00213         FALSE,                     /* pc_relative */
00214         0,                  /* bitpos */
00215         complain_overflow_dont, /* complain_on_overflow */
00216         bfd_elf_generic_reloc,     /* special_function */
00217         "R_AVR_LO8_LDI",    /* name */
00218         FALSE,                     /* partial_inplace */
00219         0xffff,             /* src_mask */
00220         0xffff,             /* dst_mask */
00221         FALSE),             /* pcrel_offset */
00222   /* A high 8 bit absolute relocation of 16 bit address.
00223      For LDI command.  */
00224   HOWTO (R_AVR_HI8_LDI,            /* type */
00225         8,                  /* rightshift */
00226         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00227         8,                  /* bitsize */
00228         FALSE,                     /* pc_relative */
00229         0,                  /* bitpos */
00230         complain_overflow_dont, /* complain_on_overflow */
00231         bfd_elf_generic_reloc,     /* special_function */
00232         "R_AVR_HI8_LDI",    /* name */
00233         FALSE,                     /* partial_inplace */
00234         0xffff,             /* src_mask */
00235         0xffff,             /* dst_mask */
00236         FALSE),             /* pcrel_offset */
00237   /* A high 6 bit absolute relocation of 22 bit address.
00238      For LDI command.  As well second most significant 8 bit value of
00239      a 32 bit link-time constant.  */
00240   HOWTO (R_AVR_HH8_LDI,            /* type */
00241         16,                 /* rightshift */
00242         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00243         8,                  /* bitsize */
00244         FALSE,                     /* pc_relative */
00245         0,                  /* bitpos */
00246         complain_overflow_dont, /* complain_on_overflow */
00247         bfd_elf_generic_reloc,     /* special_function */
00248         "R_AVR_HH8_LDI",    /* name */
00249         FALSE,                     /* partial_inplace */
00250         0xffff,             /* src_mask */
00251         0xffff,             /* dst_mask */
00252         FALSE),             /* pcrel_offset */
00253   /* A negative low 8 bit absolute relocation of 16 bit address.
00254      For LDI command.  */
00255   HOWTO (R_AVR_LO8_LDI_NEG, /* type */
00256         0,                  /* rightshift */
00257         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00258         8,                  /* bitsize */
00259         FALSE,                     /* pc_relative */
00260         0,                  /* bitpos */
00261         complain_overflow_dont, /* complain_on_overflow */
00262         bfd_elf_generic_reloc,     /* special_function */
00263         "R_AVR_LO8_LDI_NEG",       /* name */
00264         FALSE,                     /* partial_inplace */
00265         0xffff,             /* src_mask */
00266         0xffff,             /* dst_mask */
00267         FALSE),             /* pcrel_offset */
00268   /* A negative high 8 bit absolute relocation of 16 bit address.
00269      For LDI command.  */
00270   HOWTO (R_AVR_HI8_LDI_NEG, /* type */
00271         8,                  /* rightshift */
00272         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00273         8,                  /* bitsize */
00274         FALSE,                     /* pc_relative */
00275         0,                  /* bitpos */
00276         complain_overflow_dont, /* complain_on_overflow */
00277         bfd_elf_generic_reloc,     /* special_function */
00278         "R_AVR_HI8_LDI_NEG",       /* name */
00279         FALSE,                     /* partial_inplace */
00280         0xffff,             /* src_mask */
00281         0xffff,             /* dst_mask */
00282         FALSE),             /* pcrel_offset */
00283   /* A negative high 6 bit absolute relocation of 22 bit address.
00284      For LDI command.  */
00285   HOWTO (R_AVR_HH8_LDI_NEG, /* type */
00286         16,                 /* rightshift */
00287         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00288         8,                  /* bitsize */
00289         FALSE,                     /* pc_relative */
00290         0,                  /* bitpos */
00291         complain_overflow_dont, /* complain_on_overflow */
00292         bfd_elf_generic_reloc,     /* special_function */
00293         "R_AVR_HH8_LDI_NEG",       /* name */
00294         FALSE,                     /* partial_inplace */
00295         0xffff,             /* src_mask */
00296         0xffff,             /* dst_mask */
00297         FALSE),             /* pcrel_offset */
00298   /* A low 8 bit absolute relocation of 24 bit program memory address.
00299      For LDI command.  Will not be changed when linker stubs are needed. */
00300   HOWTO (R_AVR_LO8_LDI_PM,  /* type */
00301         1,                  /* rightshift */
00302         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00303         8,                  /* bitsize */
00304         FALSE,                     /* pc_relative */
00305         0,                  /* bitpos */
00306         complain_overflow_dont, /* complain_on_overflow */
00307         bfd_elf_generic_reloc,     /* special_function */
00308         "R_AVR_LO8_LDI_PM", /* name */
00309         FALSE,                     /* partial_inplace */
00310         0xffff,             /* src_mask */
00311         0xffff,             /* dst_mask */
00312         FALSE),             /* pcrel_offset */
00313   /* A low 8 bit absolute relocation of 24 bit program memory address.
00314      For LDI command.  Will not be changed when linker stubs are needed. */
00315   HOWTO (R_AVR_HI8_LDI_PM,  /* type */
00316         9,                  /* rightshift */
00317         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00318         8,                  /* bitsize */
00319         FALSE,                     /* pc_relative */
00320         0,                  /* bitpos */
00321         complain_overflow_dont, /* complain_on_overflow */
00322         bfd_elf_generic_reloc,     /* special_function */
00323         "R_AVR_HI8_LDI_PM", /* name */
00324         FALSE,                     /* partial_inplace */
00325         0xffff,             /* src_mask */
00326         0xffff,             /* dst_mask */
00327         FALSE),             /* pcrel_offset */
00328   /* A low 8 bit absolute relocation of 24 bit program memory address.
00329      For LDI command.  Will not be changed when linker stubs are needed. */
00330   HOWTO (R_AVR_HH8_LDI_PM,  /* type */
00331         17,                 /* rightshift */
00332         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00333         8,                  /* bitsize */
00334         FALSE,                     /* pc_relative */
00335         0,                  /* bitpos */
00336         complain_overflow_dont, /* complain_on_overflow */
00337         bfd_elf_generic_reloc,     /* special_function */
00338         "R_AVR_HH8_LDI_PM", /* name */
00339         FALSE,                     /* partial_inplace */
00340         0xffff,             /* src_mask */
00341         0xffff,             /* dst_mask */
00342         FALSE),             /* pcrel_offset */
00343   /* A low 8 bit absolute relocation of 24 bit program memory address.
00344      For LDI command.  Will not be changed when linker stubs are needed. */
00345   HOWTO (R_AVR_LO8_LDI_PM_NEG,     /* type */
00346         1,                  /* rightshift */
00347         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00348         8,                  /* bitsize */
00349         FALSE,                     /* pc_relative */
00350         0,                  /* bitpos */
00351         complain_overflow_dont, /* complain_on_overflow */
00352         bfd_elf_generic_reloc,     /* special_function */
00353         "R_AVR_LO8_LDI_PM_NEG", /* name */
00354         FALSE,                     /* partial_inplace */
00355         0xffff,             /* src_mask */
00356         0xffff,             /* dst_mask */
00357         FALSE),             /* pcrel_offset */
00358   /* A low 8 bit absolute relocation of 24 bit program memory address.
00359      For LDI command.  Will not be changed when linker stubs are needed. */
00360   HOWTO (R_AVR_HI8_LDI_PM_NEG,     /* type */
00361         9,                  /* rightshift */
00362         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00363         8,                  /* bitsize */
00364         FALSE,                     /* pc_relative */
00365         0,                  /* bitpos */
00366         complain_overflow_dont, /* complain_on_overflow */
00367         bfd_elf_generic_reloc,     /* special_function */
00368         "R_AVR_HI8_LDI_PM_NEG", /* name */
00369         FALSE,                     /* partial_inplace */
00370         0xffff,             /* src_mask */
00371         0xffff,             /* dst_mask */
00372         FALSE),             /* pcrel_offset */
00373   /* A low 8 bit absolute relocation of 24 bit program memory address.
00374      For LDI command.  Will not be changed when linker stubs are needed. */
00375   HOWTO (R_AVR_HH8_LDI_PM_NEG,     /* type */
00376         17,                 /* rightshift */
00377         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00378         8,                  /* bitsize */
00379         FALSE,                     /* pc_relative */
00380         0,                  /* bitpos */
00381         complain_overflow_dont, /* complain_on_overflow */
00382         bfd_elf_generic_reloc,     /* special_function */
00383         "R_AVR_HH8_LDI_PM_NEG", /* name */
00384         FALSE,                     /* partial_inplace */
00385         0xffff,             /* src_mask */
00386         0xffff,             /* dst_mask */
00387         FALSE),             /* pcrel_offset */
00388   /* Relocation for CALL command in ATmega.  */
00389   HOWTO (R_AVR_CALL,        /* type */
00390         1,                  /* rightshift */
00391         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00392         23,                 /* bitsize */
00393         FALSE,                     /* pc_relative */
00394         0,                  /* bitpos */
00395         complain_overflow_dont,/* complain_on_overflow */
00396         bfd_elf_generic_reloc,     /* special_function */
00397         "R_AVR_CALL",              /* name */
00398         FALSE,                     /* partial_inplace */
00399         0xffffffff,         /* src_mask */
00400         0xffffffff,         /* dst_mask */
00401         FALSE),                    /* pcrel_offset */
00402   /* A 16 bit absolute relocation of 16 bit address.
00403      For LDI command.  */
00404   HOWTO (R_AVR_LDI,         /* type */
00405         0,                  /* rightshift */
00406         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00407         16,                 /* bitsize */
00408         FALSE,                     /* pc_relative */
00409         0,                  /* bitpos */
00410         complain_overflow_dont,/* complain_on_overflow */
00411         bfd_elf_generic_reloc,     /* special_function */
00412         "R_AVR_LDI",        /* name */
00413         FALSE,                     /* partial_inplace */
00414         0xffff,             /* src_mask */
00415         0xffff,             /* dst_mask */
00416         FALSE),             /* pcrel_offset */
00417   /* A 6 bit absolute relocation of 6 bit offset.
00418      For ldd/sdd command.  */
00419   HOWTO (R_AVR_6,           /* type */
00420         0,                  /* rightshift */
00421         0,                  /* size (0 = byte, 1 = short, 2 = long) */
00422         6,                  /* bitsize */
00423         FALSE,                     /* pc_relative */
00424         0,                  /* bitpos */
00425         complain_overflow_dont,/* complain_on_overflow */
00426         bfd_elf_generic_reloc,     /* special_function */
00427         "R_AVR_6",          /* name */
00428         FALSE,                     /* partial_inplace */
00429         0xffff,             /* src_mask */
00430         0xffff,             /* dst_mask */
00431         FALSE),             /* pcrel_offset */
00432   /* A 6 bit absolute relocation of 6 bit offset.
00433      For sbiw/adiw command.  */
00434   HOWTO (R_AVR_6_ADIW,             /* type */
00435         0,                  /* rightshift */
00436         0,                  /* size (0 = byte, 1 = short, 2 = long) */
00437         6,                  /* bitsize */
00438         FALSE,                     /* pc_relative */
00439         0,                  /* bitpos */
00440         complain_overflow_dont,/* complain_on_overflow */
00441         bfd_elf_generic_reloc,     /* special_function */
00442         "R_AVR_6_ADIW",     /* name */
00443         FALSE,                     /* partial_inplace */
00444         0xffff,             /* src_mask */
00445         0xffff,             /* dst_mask */
00446         FALSE),             /* pcrel_offset */
00447   /* Most significant 8 bit value of a 32 bit link-time constant.  */
00448   HOWTO (R_AVR_MS8_LDI,            /* type */
00449         24,                 /* rightshift */
00450         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00451         8,                  /* bitsize */
00452         FALSE,                     /* pc_relative */
00453         0,                  /* bitpos */
00454         complain_overflow_dont, /* complain_on_overflow */
00455         bfd_elf_generic_reloc,     /* special_function */
00456         "R_AVR_MS8_LDI",    /* name */
00457         FALSE,                     /* partial_inplace */
00458         0xffff,             /* src_mask */
00459         0xffff,             /* dst_mask */
00460         FALSE),             /* pcrel_offset */
00461   /* Negative most significant 8 bit value of a 32 bit link-time constant.  */
00462   HOWTO (R_AVR_MS8_LDI_NEG, /* type */
00463         24,                 /* rightshift */
00464         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00465         8,                  /* bitsize */
00466         FALSE,                     /* pc_relative */
00467         0,                  /* bitpos */
00468         complain_overflow_dont, /* complain_on_overflow */
00469         bfd_elf_generic_reloc,     /* special_function */
00470         "R_AVR_MS8_LDI_NEG",       /* name */
00471         FALSE,                     /* partial_inplace */
00472         0xffff,             /* src_mask */
00473         0xffff,             /* dst_mask */
00474         FALSE),             /* pcrel_offset */
00475   /* A low 8 bit absolute relocation of 24 bit program memory address.
00476      For LDI command.  Will be changed when linker stubs are needed. */
00477   HOWTO (R_AVR_LO8_LDI_GS,      /* type */
00478          1,                     /* rightshift */
00479          1,                     /* size (0 = byte, 1 = short, 2 = long) */
00480          8,                     /* bitsize */
00481          FALSE,                 /* pc_relative */
00482          0,                     /* bitpos */
00483          complain_overflow_dont, /* complain_on_overflow */
00484          bfd_elf_generic_reloc, /* special_function */
00485          "R_AVR_LO8_LDI_GS",    /* name */
00486          FALSE,                 /* partial_inplace */
00487          0xffff,                /* src_mask */
00488          0xffff,                /* dst_mask */
00489          FALSE),                /* pcrel_offset */
00490   /* A low 8 bit absolute relocation of 24 bit program memory address.
00491      For LDI command.  Will be changed when linker stubs are needed. */
00492   HOWTO (R_AVR_HI8_LDI_GS,      /* type */
00493          9,                     /* rightshift */
00494          1,                     /* size (0 = byte, 1 = short, 2 = long) */
00495          8,                     /* bitsize */
00496          FALSE,                 /* pc_relative */
00497          0,                     /* bitpos */
00498          complain_overflow_dont, /* complain_on_overflow */
00499          bfd_elf_generic_reloc, /* special_function */
00500          "R_AVR_HI8_LDI_GS",    /* name */
00501          FALSE,                 /* partial_inplace */
00502          0xffff,                /* src_mask */
00503          0xffff,                /* dst_mask */
00504          FALSE)                 /* pcrel_offset */
00505 };
00506 
00507 /* Map BFD reloc types to AVR ELF reloc types.  */
00508 
00509 struct avr_reloc_map
00510 {
00511   bfd_reloc_code_real_type bfd_reloc_val;
00512   unsigned int elf_reloc_val;
00513 };
00514 
00515 static const struct avr_reloc_map avr_reloc_map[] =
00516 {
00517   { BFD_RELOC_NONE,                 R_AVR_NONE },
00518   { BFD_RELOC_32,                   R_AVR_32 },
00519   { BFD_RELOC_AVR_7_PCREL,          R_AVR_7_PCREL },
00520   { BFD_RELOC_AVR_13_PCREL,         R_AVR_13_PCREL },
00521   { BFD_RELOC_16,                   R_AVR_16 },
00522   { BFD_RELOC_AVR_16_PM,            R_AVR_16_PM },
00523   { BFD_RELOC_AVR_LO8_LDI,          R_AVR_LO8_LDI},
00524   { BFD_RELOC_AVR_HI8_LDI,          R_AVR_HI8_LDI },
00525   { BFD_RELOC_AVR_HH8_LDI,          R_AVR_HH8_LDI },
00526   { BFD_RELOC_AVR_MS8_LDI,          R_AVR_MS8_LDI },
00527   { BFD_RELOC_AVR_LO8_LDI_NEG,      R_AVR_LO8_LDI_NEG },
00528   { BFD_RELOC_AVR_HI8_LDI_NEG,      R_AVR_HI8_LDI_NEG },
00529   { BFD_RELOC_AVR_HH8_LDI_NEG,      R_AVR_HH8_LDI_NEG },
00530   { BFD_RELOC_AVR_MS8_LDI_NEG,      R_AVR_MS8_LDI_NEG },
00531   { BFD_RELOC_AVR_LO8_LDI_PM,       R_AVR_LO8_LDI_PM },
00532   { BFD_RELOC_AVR_LO8_LDI_GS,       R_AVR_LO8_LDI_GS },
00533   { BFD_RELOC_AVR_HI8_LDI_PM,       R_AVR_HI8_LDI_PM },
00534   { BFD_RELOC_AVR_HI8_LDI_GS,       R_AVR_HI8_LDI_GS },
00535   { BFD_RELOC_AVR_HH8_LDI_PM,       R_AVR_HH8_LDI_PM },
00536   { BFD_RELOC_AVR_LO8_LDI_PM_NEG,   R_AVR_LO8_LDI_PM_NEG },
00537   { BFD_RELOC_AVR_HI8_LDI_PM_NEG,   R_AVR_HI8_LDI_PM_NEG },
00538   { BFD_RELOC_AVR_HH8_LDI_PM_NEG,   R_AVR_HH8_LDI_PM_NEG },
00539   { BFD_RELOC_AVR_CALL,             R_AVR_CALL },
00540   { BFD_RELOC_AVR_LDI,              R_AVR_LDI  },
00541   { BFD_RELOC_AVR_6,                R_AVR_6    },
00542   { BFD_RELOC_AVR_6_ADIW,           R_AVR_6_ADIW }
00543 };
00544 
00545 /* Meant to be filled one day with the wrap around address for the
00546    specific device.  I.e. should get the value 0x4000 for 16k devices,
00547    0x8000 for 32k devices and so on.
00548 
00549    We initialize it here with a value of 0x1000000 resulting in
00550    that we will never suggest a wrap-around jump during relaxation.
00551    The logic of the source code later on assumes that in
00552    avr_pc_wrap_around one single bit is set.  */
00553 static bfd_vma avr_pc_wrap_around = 0x10000000;
00554 
00555 /* If this variable holds a value different from zero, the linker relaxation
00556    machine will try to optimize call/ret sequences by a single jump
00557    instruction. This option could be switched off by a linker switch.  */
00558 static int avr_replace_call_ret_sequences = 1;
00559 
00560 /* Initialize an entry in the stub hash table.  */
00561 
00562 static struct bfd_hash_entry *
00563 stub_hash_newfunc (struct bfd_hash_entry *entry,
00564                    struct bfd_hash_table *table,
00565                    const char *string)
00566 {
00567   /* Allocate the structure if it has not already been allocated by a
00568      subclass.  */
00569   if (entry == NULL)
00570     {
00571       entry = bfd_hash_allocate (table,
00572                                  sizeof (struct elf32_avr_stub_hash_entry));
00573       if (entry == NULL)
00574         return entry;
00575     }
00576 
00577   /* Call the allocation method of the superclass.  */
00578   entry = bfd_hash_newfunc (entry, table, string);
00579   if (entry != NULL)
00580     {
00581       struct elf32_avr_stub_hash_entry *hsh;
00582 
00583       /* Initialize the local fields.  */
00584       hsh = avr_stub_hash_entry (entry);
00585       hsh->stub_offset = 0;
00586       hsh->target_value = 0;
00587     }
00588 
00589   return entry;
00590 }
00591 
00592 /* This function is just a straight passthrough to the real
00593    function in linker.c.  Its prupose is so that its address
00594    can be compared inside the avr_link_hash_table macro.  */
00595 
00596 static struct bfd_hash_entry *
00597 elf32_avr_link_hash_newfunc (struct bfd_hash_entry * entry,
00598                           struct bfd_hash_table * table,
00599                           const char * string)
00600 {
00601   return _bfd_elf_link_hash_newfunc (entry, table, string);
00602 }
00603 
00604 /* Create the derived linker hash table.  The AVR ELF port uses the derived
00605    hash table to keep information specific to the AVR ELF linker (without
00606    using static variables).  */
00607 
00608 static struct bfd_link_hash_table *
00609 elf32_avr_link_hash_table_create (bfd *abfd)
00610 {
00611   struct elf32_avr_link_hash_table *htab;
00612   bfd_size_type amt = sizeof (*htab);
00613 
00614   htab = bfd_malloc (amt);
00615   if (htab == NULL)
00616     return NULL;
00617 
00618   if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd,
00619                                       elf32_avr_link_hash_newfunc,
00620                                       sizeof (struct elf_link_hash_entry)))
00621     {
00622       free (htab);
00623       return NULL;
00624     }
00625 
00626   /* Init the stub hash table too.  */
00627   if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
00628                             sizeof (struct elf32_avr_stub_hash_entry)))
00629     return NULL;
00630 
00631   htab->stub_bfd = NULL;
00632   htab->stub_sec = NULL;
00633 
00634   /* Initialize the address mapping table.  */
00635   htab->amt_stub_offsets = NULL;
00636   htab->amt_destination_addr = NULL;
00637   htab->amt_entry_cnt = 0;
00638   htab->amt_max_entry_cnt = 0;
00639 
00640   return &htab->etab.root;
00641 }
00642 
00643 /* Free the derived linker hash table.  */
00644 
00645 static void
00646 elf32_avr_link_hash_table_free (struct bfd_link_hash_table *btab)
00647 {
00648   struct elf32_avr_link_hash_table *htab
00649     = (struct elf32_avr_link_hash_table *) btab;
00650 
00651   /* Free the address mapping table.  */
00652   if (htab->amt_stub_offsets != NULL)
00653     free (htab->amt_stub_offsets);
00654   if (htab->amt_destination_addr != NULL)
00655     free (htab->amt_destination_addr);
00656 
00657   bfd_hash_table_free (&htab->bstab);
00658   _bfd_generic_link_hash_table_free (btab);
00659 }
00660 
00661 /* Calculates the effective distance of a pc relative jump/call.  */
00662 
00663 static int
00664 avr_relative_distance_considering_wrap_around (unsigned int distance)
00665 {
00666   unsigned int wrap_around_mask = avr_pc_wrap_around - 1;
00667   int dist_with_wrap_around = distance & wrap_around_mask;
00668 
00669   if (dist_with_wrap_around > ((int) (avr_pc_wrap_around >> 1)))
00670     dist_with_wrap_around -= avr_pc_wrap_around;
00671 
00672   return dist_with_wrap_around;
00673 }
00674 
00675 
00676 static reloc_howto_type *
00677 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
00678                              bfd_reloc_code_real_type code)
00679 {
00680   unsigned int i;
00681 
00682   for (i = 0;
00683        i < sizeof (avr_reloc_map) / sizeof (struct avr_reloc_map);
00684        i++)
00685     if (avr_reloc_map[i].bfd_reloc_val == code)
00686       return &elf_avr_howto_table[avr_reloc_map[i].elf_reloc_val];
00687 
00688   return NULL;
00689 }
00690 
00691 static reloc_howto_type *
00692 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
00693                              const char *r_name)
00694 {
00695   unsigned int i;
00696 
00697   for (i = 0;
00698        i < sizeof (elf_avr_howto_table) / sizeof (elf_avr_howto_table[0]);
00699        i++)
00700     if (elf_avr_howto_table[i].name != NULL
00701        && strcasecmp (elf_avr_howto_table[i].name, r_name) == 0)
00702       return &elf_avr_howto_table[i];
00703 
00704   return NULL;
00705 }
00706 
00707 /* Set the howto pointer for an AVR ELF reloc.  */
00708 
00709 static void
00710 avr_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
00711                      arelent *cache_ptr,
00712                      Elf_Internal_Rela *dst)
00713 {
00714   unsigned int r_type;
00715 
00716   r_type = ELF32_R_TYPE (dst->r_info);
00717   BFD_ASSERT (r_type < (unsigned int) R_AVR_max);
00718   cache_ptr->howto = &elf_avr_howto_table[r_type];
00719 }
00720 
00721 /* Look through the relocs for a section during the first phase.
00722    Since we don't do .gots or .plts, we just need to consider the
00723    virtual table relocs for gc.  */
00724 
00725 static bfd_boolean
00726 elf32_avr_check_relocs (bfd *abfd,
00727                      struct bfd_link_info *info,
00728                      asection *sec,
00729                      const Elf_Internal_Rela *relocs)
00730 {
00731   Elf_Internal_Shdr *symtab_hdr;
00732   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
00733   const Elf_Internal_Rela *rel;
00734   const Elf_Internal_Rela *rel_end;
00735 
00736   if (info->relocatable)
00737     return TRUE;
00738 
00739   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
00740   sym_hashes = elf_sym_hashes (abfd);
00741   sym_hashes_end = sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
00742   if (!elf_bad_symtab (abfd))
00743     sym_hashes_end -= symtab_hdr->sh_info;
00744 
00745   rel_end = relocs + sec->reloc_count;
00746   for (rel = relocs; rel < rel_end; rel++)
00747     {
00748       struct elf_link_hash_entry *h;
00749       unsigned long r_symndx;
00750 
00751       r_symndx = ELF32_R_SYM (rel->r_info);
00752       if (r_symndx < symtab_hdr->sh_info)
00753         h = NULL;
00754       else
00755        {
00756          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
00757          while (h->root.type == bfd_link_hash_indirect
00758                || h->root.type == bfd_link_hash_warning)
00759            h = (struct elf_link_hash_entry *) h->root.u.i.link;
00760        }
00761     }
00762 
00763   return TRUE;
00764 }
00765 
00766 static bfd_boolean
00767 avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation)
00768 {
00769   return (relocation >= 0x020000);
00770 }
00771 
00772 /* Returns the address of the corresponding stub if there is one.
00773    Returns otherwise an address above 0x020000.  This function
00774    could also be used, if there is no knowledge on the section where
00775    the destination is found.  */
00776 
00777 static bfd_vma
00778 avr_get_stub_addr (bfd_vma srel,
00779                    struct elf32_avr_link_hash_table *htab)
00780 {
00781   unsigned int index;
00782   bfd_vma stub_sec_addr =
00783               (htab->stub_sec->output_section->vma +
00784               htab->stub_sec->output_offset);
00785 
00786   for (index = 0; index < htab->amt_max_entry_cnt; index ++)
00787     if (htab->amt_destination_addr[index] == srel)
00788       return htab->amt_stub_offsets[index] + stub_sec_addr;
00789 
00790   /* Return an address that could not be reached by 16 bit relocs.  */
00791   return 0x020000;
00792 }
00793 
00794 /* Perform a single relocation.  By default we use the standard BFD
00795    routines, but a few relocs, we have to do them ourselves.  */
00796 
00797 static bfd_reloc_status_type
00798 avr_final_link_relocate (reloc_howto_type *                 howto,
00799                       bfd *                              input_bfd,
00800                       asection *                         input_section,
00801                       bfd_byte *                         contents,
00802                       Elf_Internal_Rela *                rel,
00803                          bfd_vma                            relocation,
00804                          struct elf32_avr_link_hash_table * htab)
00805 {
00806   bfd_reloc_status_type r = bfd_reloc_ok;
00807   bfd_vma               x;
00808   bfd_signed_vma     srel;
00809   bfd_signed_vma     reloc_addr;
00810   bfd_boolean           use_stubs = FALSE;
00811   /* Usually is 0, unless we are generating code for a bootloader.  */
00812   bfd_signed_vma        base_addr = htab->vector_base;
00813 
00814   /* Absolute addr of the reloc in the final excecutable.  */
00815   reloc_addr = rel->r_offset + input_section->output_section->vma
00816               + input_section->output_offset;
00817 
00818   switch (howto->type)
00819     {
00820     case R_AVR_7_PCREL:
00821       contents += rel->r_offset;
00822       srel = (bfd_signed_vma) relocation;
00823       srel += rel->r_addend;
00824       srel -= rel->r_offset;
00825       srel -= 2;     /* Branch instructions add 2 to the PC...  */
00826       srel -= (input_section->output_section->vma +
00827               input_section->output_offset);
00828 
00829       if (srel & 1)
00830        return bfd_reloc_outofrange;
00831       if (srel > ((1 << 7) - 1) || (srel < - (1 << 7)))
00832        return bfd_reloc_overflow;
00833       x = bfd_get_16 (input_bfd, contents);
00834       x = (x & 0xfc07) | (((srel >> 1) << 3) & 0x3f8);
00835       bfd_put_16 (input_bfd, x, contents);
00836       break;
00837 
00838     case R_AVR_13_PCREL:
00839       contents   += rel->r_offset;
00840       srel = (bfd_signed_vma) relocation;
00841       srel += rel->r_addend;
00842       srel -= rel->r_offset;
00843       srel -= 2;     /* Branch instructions add 2 to the PC...  */
00844       srel -= (input_section->output_section->vma +
00845               input_section->output_offset);
00846 
00847       if (srel & 1)
00848        return bfd_reloc_outofrange;
00849 
00850       srel = avr_relative_distance_considering_wrap_around (srel);
00851 
00852       /* AVR addresses commands as words.  */
00853       srel >>= 1;
00854 
00855       /* Check for overflow.  */
00856       if (srel < -2048 || srel > 2047)
00857        {
00858           /* Relative distance is too large.  */
00859 
00860          /* Always apply WRAPAROUND for avr2 and avr4.  */
00861          switch (bfd_get_mach (input_bfd))
00862            {
00863            case bfd_mach_avr2:
00864            case bfd_mach_avr4:
00865              break;
00866 
00867            default:
00868              return bfd_reloc_overflow;
00869            }
00870        }
00871 
00872       x = bfd_get_16 (input_bfd, contents);
00873       x = (x & 0xf000) | (srel & 0xfff);
00874       bfd_put_16 (input_bfd, x, contents);
00875       break;
00876 
00877     case R_AVR_LO8_LDI:
00878       contents += rel->r_offset;
00879       srel = (bfd_signed_vma) relocation + rel->r_addend;
00880       x = bfd_get_16 (input_bfd, contents);
00881       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
00882       bfd_put_16 (input_bfd, x, contents);
00883       break;
00884 
00885     case R_AVR_LDI:
00886       contents += rel->r_offset;
00887       srel = (bfd_signed_vma) relocation + rel->r_addend;
00888       if (((srel > 0) && (srel & 0xffff) > 255)
00889          || ((srel < 0) && ((-srel) & 0xffff) > 128))
00890         /* Remove offset for data/eeprom section.  */
00891         return bfd_reloc_overflow;
00892 
00893       x = bfd_get_16 (input_bfd, contents);
00894       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
00895       bfd_put_16 (input_bfd, x, contents);
00896       break;
00897 
00898     case R_AVR_6:
00899       contents += rel->r_offset;
00900       srel = (bfd_signed_vma) relocation + rel->r_addend;
00901       if (((srel & 0xffff) > 63) || (srel < 0))
00902        /* Remove offset for data/eeprom section.  */
00903        return bfd_reloc_overflow;
00904       x = bfd_get_16 (input_bfd, contents);
00905       x = (x & 0xd3f8) | ((srel & 7) | ((srel & (3 << 3)) << 7)
00906                        | ((srel & (1 << 5)) << 8));
00907       bfd_put_16 (input_bfd, x, contents);
00908       break;
00909 
00910     case R_AVR_6_ADIW:
00911       contents += rel->r_offset;
00912       srel = (bfd_signed_vma) relocation + rel->r_addend;
00913       if (((srel & 0xffff) > 63) || (srel < 0))
00914        /* Remove offset for data/eeprom section.  */
00915        return bfd_reloc_overflow;
00916       x = bfd_get_16 (input_bfd, contents);
00917       x = (x & 0xff30) | (srel & 0xf) | ((srel & 0x30) << 2);
00918       bfd_put_16 (input_bfd, x, contents);
00919       break;
00920 
00921     case R_AVR_HI8_LDI:
00922       contents += rel->r_offset;
00923       srel = (bfd_signed_vma) relocation + rel->r_addend;
00924       srel = (srel >> 8) & 0xff;
00925       x = bfd_get_16 (input_bfd, contents);
00926       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
00927       bfd_put_16 (input_bfd, x, contents);
00928       break;
00929 
00930     case R_AVR_HH8_LDI:
00931       contents += rel->r_offset;
00932       srel = (bfd_signed_vma) relocation + rel->r_addend;
00933       srel = (srel >> 16) & 0xff;
00934       x = bfd_get_16 (input_bfd, contents);
00935       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
00936       bfd_put_16 (input_bfd, x, contents);
00937       break;
00938 
00939     case R_AVR_MS8_LDI:
00940       contents += rel->r_offset;
00941       srel = (bfd_signed_vma) relocation + rel->r_addend;
00942       srel = (srel >> 24) & 0xff;
00943       x = bfd_get_16 (input_bfd, contents);
00944       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
00945       bfd_put_16 (input_bfd, x, contents);
00946       break;
00947 
00948     case R_AVR_LO8_LDI_NEG:
00949       contents += rel->r_offset;
00950       srel = (bfd_signed_vma) relocation + rel->r_addend;
00951       srel = -srel;
00952       x = bfd_get_16 (input_bfd, contents);
00953       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
00954       bfd_put_16 (input_bfd, x, contents);
00955       break;
00956 
00957     case R_AVR_HI8_LDI_NEG:
00958       contents += rel->r_offset;
00959       srel = (bfd_signed_vma) relocation + rel->r_addend;
00960       srel = -srel;
00961       srel = (srel >> 8) & 0xff;
00962       x = bfd_get_16 (input_bfd, contents);
00963       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
00964       bfd_put_16 (input_bfd, x, contents);
00965       break;
00966 
00967     case R_AVR_HH8_LDI_NEG:
00968       contents += rel->r_offset;
00969       srel = (bfd_signed_vma) relocation + rel->r_addend;
00970       srel = -srel;
00971       srel = (srel >> 16) & 0xff;
00972       x = bfd_get_16 (input_bfd, contents);
00973       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
00974       bfd_put_16 (input_bfd, x, contents);
00975       break;
00976 
00977     case R_AVR_MS8_LDI_NEG:
00978       contents += rel->r_offset;
00979       srel = (bfd_signed_vma) relocation + rel->r_addend;
00980       srel = -srel;
00981       srel = (srel >> 24) & 0xff;
00982       x = bfd_get_16 (input_bfd, contents);
00983       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
00984       bfd_put_16 (input_bfd, x, contents);
00985       break;
00986 
00987     case R_AVR_LO8_LDI_GS:
00988       use_stubs = (!htab->no_stubs);
00989       /* Fall through.  */
00990     case R_AVR_LO8_LDI_PM:
00991       contents += rel->r_offset;
00992       srel = (bfd_signed_vma) relocation + rel->r_addend;
00993 
00994       if (use_stubs
00995           && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
00996         {
00997           bfd_vma old_srel = srel;
00998 
00999           /* We need to use the address of the stub instead.  */
01000           srel = avr_get_stub_addr (srel, htab);
01001           if (debug_stubs)
01002             printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
01003                     "reloc at address 0x%x.\n",
01004                     (unsigned int) srel,
01005                     (unsigned int) old_srel,
01006                     (unsigned int) reloc_addr);
01007 
01008          if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
01009            return bfd_reloc_outofrange;
01010         }
01011 
01012       if (srel & 1)
01013        return bfd_reloc_outofrange;
01014       srel = srel >> 1;
01015       x = bfd_get_16 (input_bfd, contents);
01016       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
01017       bfd_put_16 (input_bfd, x, contents);
01018       break;
01019 
01020     case R_AVR_HI8_LDI_GS:
01021       use_stubs = (!htab->no_stubs);
01022       /* Fall through.  */
01023     case R_AVR_HI8_LDI_PM:
01024       contents += rel->r_offset;
01025       srel = (bfd_signed_vma) relocation + rel->r_addend;
01026 
01027       if (use_stubs
01028           && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
01029         {
01030           bfd_vma old_srel = srel;
01031 
01032           /* We need to use the address of the stub instead.  */
01033           srel = avr_get_stub_addr (srel, htab);
01034           if (debug_stubs)
01035             printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
01036                     "reloc at address 0x%x.\n",
01037                     (unsigned int) srel,
01038                     (unsigned int) old_srel,
01039                     (unsigned int) reloc_addr);
01040 
01041          if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
01042            return bfd_reloc_outofrange;
01043         }
01044 
01045       if (srel & 1)
01046        return bfd_reloc_outofrange;
01047       srel = srel >> 1;
01048       srel = (srel >> 8) & 0xff;
01049       x = bfd_get_16 (input_bfd, contents);
01050       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
01051       bfd_put_16 (input_bfd, x, contents);
01052       break;
01053 
01054     case R_AVR_HH8_LDI_PM:
01055       contents += rel->r_offset;
01056       srel = (bfd_signed_vma) relocation + rel->r_addend;
01057       if (srel & 1)
01058        return bfd_reloc_outofrange;
01059       srel = srel >> 1;
01060       srel = (srel >> 16) & 0xff;
01061       x = bfd_get_16 (input_bfd, contents);
01062       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
01063       bfd_put_16 (input_bfd, x, contents);
01064       break;
01065 
01066     case R_AVR_LO8_LDI_PM_NEG:
01067       contents += rel->r_offset;
01068       srel = (bfd_signed_vma) relocation + rel->r_addend;
01069       srel = -srel;
01070       if (srel & 1)
01071        return bfd_reloc_outofrange;
01072       srel = srel >> 1;
01073       x = bfd_get_16 (input_bfd, contents);
01074       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
01075       bfd_put_16 (input_bfd, x, contents);
01076       break;
01077 
01078     case R_AVR_HI8_LDI_PM_NEG:
01079       contents += rel->r_offset;
01080       srel = (bfd_signed_vma) relocation + rel->r_addend;
01081       srel = -srel;
01082       if (srel & 1)
01083        return bfd_reloc_outofrange;
01084       srel = srel >> 1;
01085       srel = (srel >> 8) & 0xff;
01086       x = bfd_get_16 (input_bfd, contents);
01087       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
01088       bfd_put_16 (input_bfd, x, contents);
01089       break;
01090 
01091     case R_AVR_HH8_LDI_PM_NEG:
01092       contents += rel->r_offset;
01093       srel = (bfd_signed_vma) relocation + rel->r_addend;
01094       srel = -srel;
01095       if (srel & 1)
01096        return bfd_reloc_outofrange;
01097       srel = srel >> 1;
01098       srel = (srel >> 16) & 0xff;
01099       x = bfd_get_16 (input_bfd, contents);
01100       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
01101       bfd_put_16 (input_bfd, x, contents);
01102       break;
01103 
01104     case R_AVR_CALL:
01105       contents += rel->r_offset;
01106       srel = (bfd_signed_vma) relocation + rel->r_addend;
01107       if (srel & 1)
01108        return bfd_reloc_outofrange;
01109       srel = srel >> 1;
01110       x = bfd_get_16 (input_bfd, contents);
01111       x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16;
01112       bfd_put_16 (input_bfd, x, contents);
01113       bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2);
01114       break;
01115 
01116     case R_AVR_16_PM:
01117       use_stubs = (!htab->no_stubs);
01118       contents += rel->r_offset;
01119       srel = (bfd_signed_vma) relocation + rel->r_addend;
01120 
01121       if (use_stubs
01122           && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
01123         {
01124           bfd_vma old_srel = srel;
01125 
01126           /* We need to use the address of the stub instead.  */
01127           srel = avr_get_stub_addr (srel,htab);
01128           if (debug_stubs)
01129             printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
01130                     "reloc at address 0x%x.\n",
01131                     (unsigned int) srel,
01132                     (unsigned int) old_srel,
01133                     (unsigned int) reloc_addr);
01134 
01135          if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
01136            return bfd_reloc_outofrange;
01137         }
01138 
01139       if (srel & 1)
01140        return bfd_reloc_outofrange;
01141       srel = srel >> 1;
01142       bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents);
01143       break;
01144 
01145     default:
01146       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
01147                                 contents, rel->r_offset,
01148                                 relocation, rel->r_addend);
01149     }
01150 
01151   return r;
01152 }
01153 
01154 /* Relocate an AVR ELF section.  */
01155 
01156 static bfd_boolean
01157 elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
01158                          struct bfd_link_info *info,
01159                          bfd *input_bfd,
01160                          asection *input_section,
01161                          bfd_byte *contents,
01162                          Elf_Internal_Rela *relocs,
01163                          Elf_Internal_Sym *local_syms,
01164                          asection **local_sections)
01165 {
01166   Elf_Internal_Shdr *           symtab_hdr;
01167   struct elf_link_hash_entry ** sym_hashes;
01168   Elf_Internal_Rela *           rel;
01169   Elf_Internal_Rela *           relend;
01170   struct elf32_avr_link_hash_table * htab = avr_link_hash_table (info);
01171 
01172   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
01173   sym_hashes = elf_sym_hashes (input_bfd);
01174   relend     = relocs + input_section->reloc_count;
01175 
01176   for (rel = relocs; rel < relend; rel ++)
01177     {
01178       reloc_howto_type *           howto;
01179       unsigned long                r_symndx;
01180       Elf_Internal_Sym *           sym;
01181       asection *                   sec;
01182       struct elf_link_hash_entry * h;
01183       bfd_vma                      relocation;
01184       bfd_reloc_status_type        r;
01185       const char *                 name;
01186       int                          r_type;
01187 
01188       r_type = ELF32_R_TYPE (rel->r_info);
01189       r_symndx = ELF32_R_SYM (rel->r_info);
01190       howto  = elf_avr_howto_table + ELF32_R_TYPE (rel->r_info);
01191       h      = NULL;
01192       sym    = NULL;
01193       sec    = NULL;
01194 
01195       if (r_symndx < symtab_hdr->sh_info)
01196        {
01197          sym = local_syms + r_symndx;
01198          sec = local_sections [r_symndx];
01199          relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
01200 
01201          name = bfd_elf_string_from_elf_section
01202            (input_bfd, symtab_hdr->sh_link, sym->st_name);
01203          name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
01204        }
01205       else
01206        {
01207          bfd_boolean unresolved_reloc, warned;
01208 
01209          RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
01210                                r_symndx, symtab_hdr, sym_hashes,
01211                                h, sec, relocation,
01212                                unresolved_reloc, warned);
01213 
01214          name = h->root.root.string;
01215        }
01216 
01217       if (sec != NULL && elf_discarded_section (sec))
01218        {
01219          /* For relocs against symbols from removed linkonce sections,
01220             or sections discarded by a linker script, we just want the
01221             section contents zeroed.  Avoid any special processing.  */
01222          _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
01223          rel->r_info = 0;
01224          rel->r_addend = 0;
01225          continue;
01226        }
01227 
01228       if (info->relocatable)
01229        continue;
01230 
01231       r = avr_final_link_relocate (howto, input_bfd, input_section,
01232                                contents, rel, relocation, htab);
01233 
01234       if (r != bfd_reloc_ok)
01235        {
01236          const char * msg = (const char *) NULL;
01237 
01238          switch (r)
01239            {
01240            case bfd_reloc_overflow:
01241              r = info->callbacks->reloc_overflow
01242               (info, (h ? &h->root : NULL),
01243                name, howto->name, (bfd_vma) 0,
01244                input_bfd, input_section, rel->r_offset);
01245              break;
01246 
01247            case bfd_reloc_undefined:
01248              r = info->callbacks->undefined_symbol
01249               (info, name, input_bfd, input_section, rel->r_offset, TRUE);
01250              break;
01251 
01252            case bfd_reloc_outofrange:
01253              msg = _("internal error: out of range error");
01254              break;
01255 
01256            case bfd_reloc_notsupported:
01257              msg = _("internal error: unsupported relocation error");
01258              break;
01259 
01260            case bfd_reloc_dangerous:
01261              msg = _("internal error: dangerous relocation");
01262              break;
01263 
01264            default:
01265              msg = _("internal error: unknown error");
01266              break;
01267            }
01268 
01269          if (msg)
01270            r = info->callbacks->warning
01271              (info, msg, name, input_bfd, input_section, rel->r_offset);
01272 
01273          if (! r)
01274            return FALSE;
01275        }
01276     }
01277 
01278   return TRUE;
01279 }
01280 
01281 /* The final processing done just before writing out a AVR ELF object
01282    file.  This gets the AVR architecture right based on the machine
01283    number.  */
01284 
01285 static void
01286 bfd_elf_avr_final_write_processing (bfd *abfd,
01287                                 bfd_boolean linker ATTRIBUTE_UNUSED)
01288 {
01289   unsigned long val;
01290 
01291   switch (bfd_get_mach (abfd))
01292     {
01293     default:
01294     case bfd_mach_avr2:
01295       val = E_AVR_MACH_AVR2;
01296       break;
01297 
01298     case bfd_mach_avr1:
01299       val = E_AVR_MACH_AVR1;
01300       break;
01301 
01302     case bfd_mach_avr3:
01303       val = E_AVR_MACH_AVR3;
01304       break;
01305 
01306     case bfd_mach_avr4:
01307       val = E_AVR_MACH_AVR4;
01308       break;
01309 
01310     case bfd_mach_avr5:
01311       val = E_AVR_MACH_AVR5;
01312       break;
01313 
01314     case bfd_mach_avr6:
01315       val = E_AVR_MACH_AVR6;
01316       break;
01317     }
01318 
01319   elf_elfheader (abfd)->e_machine = EM_AVR;
01320   elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH;
01321   elf_elfheader (abfd)->e_flags |= val;
01322   elf_elfheader (abfd)->e_flags |= EF_AVR_LINKRELAX_PREPARED;
01323 }
01324 
01325 /* Set the right machine number.  */
01326 
01327 static bfd_boolean
01328 elf32_avr_object_p (bfd *abfd)
01329 {
01330   unsigned int e_set = bfd_mach_avr2;
01331 
01332   if (elf_elfheader (abfd)->e_machine == EM_AVR
01333       || elf_elfheader (abfd)->e_machine == EM_AVR_OLD)
01334     {
01335       int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH;
01336 
01337       switch (e_mach)
01338        {
01339        default:
01340        case E_AVR_MACH_AVR2:
01341          e_set = bfd_mach_avr2;
01342          break;
01343 
01344        case E_AVR_MACH_AVR1:
01345          e_set = bfd_mach_avr1;
01346          break;
01347 
01348        case E_AVR_MACH_AVR3:
01349          e_set = bfd_mach_avr3;
01350          break;
01351 
01352        case E_AVR_MACH_AVR4:
01353          e_set = bfd_mach_avr4;
01354          break;
01355 
01356        case E_AVR_MACH_AVR5:
01357          e_set = bfd_mach_avr5;
01358          break;
01359 
01360        case E_AVR_MACH_AVR6:
01361          e_set = bfd_mach_avr6;
01362          break;
01363        }
01364     }
01365   return bfd_default_set_arch_mach (abfd, bfd_arch_avr,
01366                                 e_set);
01367 }
01368 
01369 
01370 /* Delete some bytes from a section while changing the size of an instruction.
01371    The parameter "addr" denotes the section-relative offset pointing just
01372    behind the shrinked instruction. "addr+count" point at the first
01373    byte just behind the original unshrinked instruction.  */
01374 
01375 static bfd_boolean
01376 elf32_avr_relax_delete_bytes (bfd *abfd,
01377                               asection *sec,
01378                               bfd_vma addr,
01379                               int count)
01380 {
01381   Elf_Internal_Shdr *symtab_hdr;
01382   unsigned int sec_shndx;
01383   bfd_byte *contents;
01384   Elf_Internal_Rela *irel, *irelend;
01385   Elf_Internal_Rela *irelalign;
01386   Elf_Internal_Sym *isym;
01387   Elf_Internal_Sym *isymbuf = NULL;
01388   Elf_Internal_Sym *isymend;
01389   bfd_vma toaddr;
01390   struct elf_link_hash_entry **sym_hashes;
01391   struct elf_link_hash_entry **end_hashes;
01392   unsigned int symcount;
01393 
01394   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
01395   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
01396   contents = elf_section_data (sec)->this_hdr.contents;
01397 
01398   /* The deletion must stop at the next ALIGN reloc for an aligment
01399      power larger than the number of bytes we are deleting.  */
01400 
01401   irelalign = NULL;
01402   toaddr = sec->size;
01403 
01404   irel = elf_section_data (sec)->relocs;
01405   irelend = irel + sec->reloc_count;
01406 
01407   /* Actually delete the bytes.  */
01408   if (toaddr - addr - count > 0)
01409     memmove (contents + addr, contents + addr + count,
01410              (size_t) (toaddr - addr - count));
01411   sec->size -= count;
01412 
01413   /* Adjust all the reloc addresses.  */
01414   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
01415     {
01416       bfd_vma old_reloc_address;
01417       bfd_vma shrinked_insn_address;
01418 
01419       old_reloc_address = (sec->output_section->vma
01420                            + sec->output_offset + irel->r_offset);
01421       shrinked_insn_address = (sec->output_section->vma
01422                               + sec->output_offset + addr - count);
01423 
01424       /* Get the new reloc address.  */
01425       if ((irel->r_offset > addr
01426            && irel->r_offset < toaddr))
01427         {
01428           if (debug_relax)
01429             printf ("Relocation at address 0x%x needs to be moved.\n"
01430                     "Old section offset: 0x%x, New section offset: 0x%x \n",
01431                     (unsigned int) old_reloc_address,
01432                     (unsigned int) irel->r_offset,
01433                     (unsigned int) ((irel->r_offset) - count));
01434 
01435           irel->r_offset -= count;
01436         }
01437 
01438     }
01439 
01440    /* The reloc's own addresses are now ok. However, we need to readjust
01441       the reloc's addend, i.e. the reloc's value if two conditions are met:
01442       1.) the reloc is relative to a symbol in this section that
01443           is located in front of the shrinked instruction
01444       2.) symbol plus addend end up behind the shrinked instruction.
01445 
01446       The most common case where this happens are relocs relative to
01447       the section-start symbol.
01448 
01449       This step needs to be done for all of the sections of the bfd.  */
01450 
01451   {
01452     struct bfd_section *isec;
01453 
01454     for (isec = abfd->sections; isec; isec = isec->next)
01455      {
01456        bfd_vma symval;
01457        bfd_vma shrinked_insn_address;
01458 
01459        shrinked_insn_address = (sec->output_section->vma
01460                                 + sec->output_offset + addr - count);
01461 
01462        irelend = elf_section_data (isec)->relocs + isec->reloc_count;
01463        for (irel = elf_section_data (isec)->relocs;
01464             irel < irelend;
01465             irel++)
01466          {
01467            /* Read this BFD's local symbols if we haven't done
01468               so already.  */
01469            if (isymbuf == NULL && symtab_hdr->sh_info != 0)
01470              {
01471                isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
01472                if (isymbuf == NULL)
01473                  isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
01474                                                  symtab_hdr->sh_info, 0,
01475                                                  NULL, NULL, NULL);
01476                if (isymbuf == NULL)
01477                  return FALSE;
01478              }
01479 
01480            /* Get the value of the symbol referred to by the reloc.  */
01481            if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
01482              {
01483                /* A local symbol.  */
01484                Elf_Internal_Sym *isym;
01485                asection *sym_sec;
01486 
01487                isym = isymbuf + ELF32_R_SYM (irel->r_info);
01488                sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
01489                symval = isym->st_value;
01490                /* If the reloc is absolute, it will not have
01491                   a symbol or section associated with it.  */
01492                if (sym_sec == sec)
01493                  {
01494                    symval += sym_sec->output_section->vma
01495                              + sym_sec->output_offset;
01496 
01497                    if (debug_relax)
01498                      printf ("Checking if the relocation's "
01499                              "addend needs corrections.\n"
01500                              "Address of anchor symbol: 0x%x \n"
01501                              "Address of relocation target: 0x%x \n"
01502                              "Address of relaxed insn: 0x%x \n",
01503                              (unsigned int) symval,
01504                              (unsigned int) (symval + irel->r_addend),
01505                              (unsigned int) shrinked_insn_address);
01506 
01507                    if (symval <= shrinked_insn_address
01508                        && (symval + irel->r_addend) > shrinked_insn_address)
01509                      {
01510                        irel->r_addend -= count;
01511 
01512                        if (debug_relax)
01513                          printf ("Relocation's addend needed to be fixed \n");
01514                      }
01515                  }
01516               /* else...Reference symbol is absolute.  No adjustment needed.  */
01517             }
01518           /* else...Reference symbol is extern.  No need for adjusting
01519              the addend.  */
01520         }
01521      }
01522   }
01523 
01524   /* Adjust the local symbols defined in this section.  */
01525   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
01526   isymend = isym + symtab_hdr->sh_info;
01527   for (; isym < isymend; isym++)
01528     {
01529       if (isym->st_shndx == sec_shndx
01530           && isym->st_value > addr
01531           && isym->st_value < toaddr)
01532         isym->st_value -= count;
01533     }
01534 
01535   /* Now adjust the global symbols defined in this section.  */
01536   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
01537               - symtab_hdr->sh_info);
01538   sym_hashes = elf_sym_hashes (abfd);
01539   end_hashes = sym_hashes + symcount;
01540   for (; sym_hashes < end_hashes; sym_hashes++)
01541     {
01542       struct elf_link_hash_entry *sym_hash = *sym_hashes;
01543       if ((sym_hash->root.type == bfd_link_hash_defined
01544            || sym_hash->root.type == bfd_link_hash_defweak)
01545           && sym_hash->root.u.def.section == sec
01546           && sym_hash->root.u.def.value > addr
01547           && sym_hash->root.u.def.value < toaddr)
01548         {
01549           sym_hash->root.u.def.value -= count;
01550         }
01551     }
01552 
01553   return TRUE;
01554 }
01555 
01556 /* This function handles relaxing for the avr.
01557    Many important relaxing opportunities within functions are already
01558    realized by the compiler itself.
01559    Here we try to replace  call (4 bytes) ->  rcall (2 bytes)
01560    and jump -> rjmp (safes also 2 bytes).
01561    As well we now optimize seqences of
01562      - call/rcall function
01563      - ret
01564    to yield
01565      - jmp/rjmp function
01566      - ret
01567    . In case that within a sequence
01568      - jmp/rjmp label
01569      - ret
01570    the ret could no longer be reached it is optimized away. In order
01571    to check if the ret is no longer needed, it is checked that the ret's address
01572    is not the target of a branch or jump within the same section, it is checked
01573    that there is no skip instruction before the jmp/rjmp and that there
01574    is no local or global label place at the address of the ret.
01575 
01576    We refrain from relaxing within sections ".vectors" and
01577    ".jumptables" in order to maintain the position of the instructions.
01578    There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
01579    if possible. (In future one could possibly use the space of the nop
01580    for the first instruction of the irq service function.
01581 
01582    The .jumptables sections is meant to be used for a future tablejump variant
01583    for the devices with 3-byte program counter where the table itself
01584    contains 4-byte jump instructions whose relative offset must not
01585    be changed.  */
01586 
01587 static bfd_boolean
01588 elf32_avr_relax_section (bfd *abfd,
01589                       asection *sec,
01590                          struct bfd_link_info *link_info,
01591                          bfd_boolean *again)
01592 {
01593   Elf_Internal_Shdr *symtab_hdr;
01594   Elf_Internal_Rela *internal_relocs;
01595   Elf_Internal_Rela *irel, *irelend;
01596   bfd_byte *contents = NULL;
01597   Elf_Internal_Sym *isymbuf = NULL;
01598   static asection *last_input_section = NULL;
01599   static Elf_Internal_Rela *last_reloc = NULL;
01600   struct elf32_avr_link_hash_table *htab;
01601 
01602   htab = avr_link_hash_table (link_info);
01603   if (htab == NULL)
01604     return FALSE;
01605 
01606   /* Assume nothing changes.  */
01607   *again = FALSE;
01608 
01609   if ((!htab->no_stubs) && (sec == htab->stub_sec))
01610     {
01611       /* We are just relaxing the stub section.
01612         Let's calculate the size needed again.  */
01613       bfd_size_type last_estimated_stub_section_size = htab->stub_sec->size;
01614 
01615       if (debug_relax)
01616         printf ("Relaxing the stub section. Size prior to this pass: %i\n",
01617                 (int) last_estimated_stub_section_size);
01618 
01619       elf32_avr_size_stubs (htab->stub_sec->output_section->owner,
01620                             link_info, FALSE);
01621 
01622       /* Check if the number of trampolines changed.  */
01623       if (last_estimated_stub_section_size != htab->stub_sec->size)
01624         *again = TRUE;
01625 
01626       if (debug_relax)
01627         printf ("Size of stub section after this pass: %i\n",
01628                 (int) htab->stub_sec->size);
01629 
01630       return TRUE;
01631     }
01632 
01633   /* We don't have to do anything for a relocatable link, if
01634      this section does not have relocs, or if this is not a
01635      code section.  */
01636   if (link_info->relocatable
01637       || (sec->flags & SEC_RELOC) == 0
01638       || sec->reloc_count == 0
01639       || (sec->flags & SEC_CODE) == 0)
01640     return TRUE;
01641 
01642   /* Check if the object file to relax uses internal symbols so that we
01643      could fix up the relocations.  */
01644   if (!(elf_elfheader (abfd)->e_flags & EF_AVR_LINKRELAX_PREPARED))
01645     return TRUE;
01646 
01647   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
01648 
01649   /* Get a copy of the native relocations.  */
01650   internal_relocs = (_bfd_elf_link_read_relocs
01651                      (abfd, sec, NULL, NULL, link_info->keep_memory));
01652   if (internal_relocs == NULL)
01653     goto error_return;
01654 
01655   if (sec != last_input_section)
01656     last_reloc = NULL;
01657 
01658   last_input_section = sec;
01659 
01660   /* Walk through the relocs looking for relaxing opportunities.  */
01661   irelend = internal_relocs + sec->reloc_count;
01662   for (irel = internal_relocs; irel < irelend; irel++)
01663     {
01664       bfd_vma symval;
01665 
01666       if (   ELF32_R_TYPE (irel->r_info) != R_AVR_13_PCREL
01667           && ELF32_R_TYPE (irel->r_info) != R_AVR_7_PCREL
01668           && ELF32_R_TYPE (irel->r_info) != R_AVR_CALL)
01669         continue;
01670 
01671       /* Get the section contents if we haven't done so already.  */
01672       if (contents == NULL)
01673         {
01674           /* Get cached copy if it exists.  */
01675           if (elf_section_data (sec)->this_hdr.contents != NULL)
01676             contents = elf_section_data (sec)->this_hdr.contents;
01677           else
01678             {
01679               /* Go get them off disk.  */
01680               if (! bfd_malloc_and_get_section (abfd, sec, &contents))
01681                 goto error_return;
01682             }
01683         }
01684 
01685      /* Read this BFD's local symbols if we haven't done so already.  */
01686       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
01687         {
01688           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
01689           if (isymbuf == NULL)
01690             isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
01691                                             symtab_hdr->sh_info, 0,
01692                                             NULL, NULL, NULL);
01693           if (isymbuf == NULL)
01694             goto error_return;
01695         }
01696 
01697 
01698       /* Get the value of the symbol referred to by the reloc.  */
01699       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
01700         {
01701           /* A local symbol.  */
01702           Elf_Internal_Sym *isym;
01703           asection *sym_sec;
01704 
01705           isym = isymbuf + ELF32_R_SYM (irel->r_info);
01706           sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
01707           symval = isym->st_value;
01708           /* If the reloc is absolute, it will not have
01709              a symbol or section associated with it.  */
01710           if (sym_sec)
01711             symval += sym_sec->output_section->vma
01712               + sym_sec->output_offset;
01713         }
01714       else
01715         {
01716           unsigned long indx;
01717           struct elf_link_hash_entry *h;
01718 
01719           /* An external symbol.  */
01720           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
01721           h = elf_sym_hashes (abfd)[indx];
01722           BFD_ASSERT (h != NULL);
01723           if (h->root.type != bfd_link_hash_defined
01724               && h->root.type != bfd_link_hash_defweak)
01725            /* This appears to be a reference to an undefined
01726               symbol.  Just ignore it--it will be caught by the
01727               regular reloc processing.  */
01728            continue;
01729 
01730           symval = (h->root.u.def.value
01731                     + h->root.u.def.section->output_section->vma
01732                     + h->root.u.def.section->output_offset);
01733         }
01734 
01735       /* For simplicity of coding, we are going to modify the section
01736          contents, the section relocs, and the BFD symbol table.  We
01737          must tell the rest of the code not to free up this
01738          information.  It would be possible to instead create a table
01739          of changes which have to be made, as is done in coff-mips.c;
01740          that would be more work, but would require less memory when
01741          the linker is run.  */
01742       switch (ELF32_R_TYPE (irel->r_info))
01743         {
01744          /* Try to turn a 22-bit absolute call/jump into an 13-bit
01745             pc-relative rcall/rjmp.  */
01746          case R_AVR_CALL:
01747           {
01748             bfd_vma value = symval + irel->r_addend;
01749             bfd_vma dot, gap;
01750             int distance_short_enough = 0;
01751 
01752             /* Get the address of this instruction.  */
01753             dot = (sec->output_section->vma
01754                    + sec->output_offset + irel->r_offset);
01755 
01756             /* Compute the distance from this insn to the branch target.  */
01757             gap = value - dot;
01758 
01759             /* If the distance is within -4094..+4098 inclusive, then we can
01760                relax this jump/call.  +4098 because the call/jump target
01761                will be closer after the relaxation.  */
01762             if ((int) gap >= -4094 && (int) gap <= 4098)
01763               distance_short_enough = 1;
01764 
01765             /* Here we handle the wrap-around case.  E.g. for a 16k device
01766                we could use a rjmp to jump from address 0x100 to 0x3d00!
01767                In order to make this work properly, we need to fill the
01768                vaiable avr_pc_wrap_around with the appropriate value.
01769                I.e. 0x4000 for a 16k device.  */
01770             {
01771                /* Shrinking the code size makes the gaps larger in the
01772                   case of wrap-arounds.  So we use a heuristical safety
01773                   margin to avoid that during relax the distance gets
01774                   again too large for the short jumps.  Let's assume
01775                   a typical code-size reduction due to relax for a
01776                   16k device of 600 bytes.  So let's use twice the
01777                   typical value as safety margin.  */
01778                int rgap;
01779                int safety_margin;
01780 
01781                int assumed_shrink = 600;
01782                if (avr_pc_wrap_around > 0x4000)
01783                  assumed_shrink = 900;
01784 
01785                safety_margin = 2 * assumed_shrink;
01786 
01787                rgap = avr_relative_distance_considering_wrap_around (gap);
01788 
01789                if (rgap >= (-4092 + safety_margin)
01790                    && rgap <= (4094 - safety_margin))
01791                distance_short_enough = 1;
01792             }
01793 
01794             if (distance_short_enough)
01795               {
01796                 unsigned char code_msb;
01797                 unsigned char code_lsb;
01798 
01799                 if (debug_relax)
01800                   printf ("shrinking jump/call instruction at address 0x%x"
01801                           " in section %s\n\n",
01802                           (int) dot, sec->name);
01803 
01804                 /* Note that we've changed the relocs, section contents,
01805                    etc.  */
01806                 elf_section_data (sec)->relocs = internal_relocs;
01807                 elf_section_data (sec)->this_hdr.contents = contents;
01808                 symtab_hdr->contents = (unsigned char *) isymbuf;
01809 
01810                 /* Get the instruction code for relaxing.  */
01811                 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset);
01812                 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
01813 
01814                 /* Mask out the relocation bits.  */
01815                 code_msb &= 0x94;
01816                 code_lsb &= 0x0E;
01817                 if (code_msb == 0x94 && code_lsb == 0x0E)
01818                   {
01819                     /* we are changing call -> rcall .  */
01820                     bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
01821                     bfd_put_8 (abfd, 0xD0, contents + irel->r_offset + 1);
01822                   }
01823                 else if (code_msb == 0x94 && code_lsb == 0x0C)
01824                   {
01825                     /* we are changeing jump -> rjmp.  */
01826                     bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
01827                     bfd_put_8 (abfd, 0xC0, contents + irel->r_offset + 1);
01828                   }
01829                 else
01830                   abort ();
01831 
01832                 /* Fix the relocation's type.  */
01833                 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
01834                                              R_AVR_13_PCREL);
01835 
01836                 /* Check for the vector section. There we don't want to
01837                    modify the ordering!  */
01838 
01839                 if (!strcmp (sec->name,".vectors")
01840                     || !strcmp (sec->name,".jumptables"))
01841                   {
01842                     /* Let's insert a nop.  */
01843                     bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 2);
01844                     bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 3);
01845                   }
01846                 else
01847                   {
01848                     /* Delete two bytes of data.  */
01849                     if (!elf32_avr_relax_delete_bytes (abfd, sec,
01850                                                        irel->r_offset + 2, 2))
01851                       goto error_return;
01852 
01853                     /* That will change things, so, we should relax again.
01854                        Note that this is not required, and it may be slow.  */
01855                     *again = TRUE;
01856                   }
01857               }
01858           }
01859 
01860         default:
01861           {
01862             unsigned char code_msb;
01863             unsigned char code_lsb;
01864             bfd_vma dot;
01865 
01866             code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
01867             code_lsb = bfd_get_8 (abfd, contents + irel->r_offset + 0);
01868 
01869             /* Get the address of this instruction.  */
01870             dot = (sec->output_section->vma
01871                    + sec->output_offset + irel->r_offset);
01872 
01873             /* Here we look for rcall/ret or call/ret sequences that could be
01874                safely replaced by rjmp/ret or jmp/ret.  */
01875             if (((code_msb & 0xf0) == 0xd0)
01876                 && avr_replace_call_ret_sequences)
01877               {
01878                 /* This insn is a rcall.  */
01879                 unsigned char next_insn_msb = 0;
01880                 unsigned char next_insn_lsb = 0;
01881 
01882                 if (irel->r_offset + 3 < sec->size)
01883                   {
01884                     next_insn_msb =
01885                         bfd_get_8 (abfd, contents + irel->r_offset + 3);
01886                     next_insn_lsb =
01887                         bfd_get_8 (abfd, contents + irel->r_offset + 2);
01888                   }
01889 
01890               if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
01891                   {
01892                     /* The next insn is a ret. We now convert the rcall insn
01893                        into a rjmp instruction.  */
01894                     code_msb &= 0xef;
01895                     bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1);
01896                     if (debug_relax)
01897                       printf ("converted rcall/ret sequence at address 0x%x"
01898                               " into rjmp/ret sequence. Section is %s\n\n",
01899                               (int) dot, sec->name);
01900                     *again = TRUE;
01901                     break;
01902                   }
01903               }
01904             else if ((0x94 == (code_msb & 0xfe))
01905                    && (0x0e == (code_lsb & 0x0e))
01906                    && avr_replace_call_ret_sequences)
01907               {
01908                 /* This insn is a call.  */
01909                 unsigned char next_insn_msb = 0;
01910                 unsigned char next_insn_lsb = 0;
01911 
01912                 if (irel->r_offset + 5 < sec->size)
01913                   {
01914                     next_insn_msb =
01915                         bfd_get_8 (abfd, contents + irel->r_offset + 5);
01916                     next_insn_lsb =
01917                         bfd_get_8 (abfd, contents + irel->r_offset + 4);
01918                   }
01919 
01920                 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
01921                   {
01922                     /* The next insn is a ret. We now convert the call insn
01923                        into a jmp instruction.  */
01924 
01925                     code_lsb &= 0xfd;
01926                     bfd_put_8 (abfd, code_lsb, contents + irel->r_offset);
01927                     if (debug_relax)
01928                       printf ("converted call/ret sequence at address 0x%x"
01929                               " into jmp/ret sequence. Section is %s\n\n",
01930                               (int) dot, sec->name);
01931                     *again = TRUE;
01932                     break;
01933                   }
01934               }
01935             else if ((0xc0 == (code_msb & 0xf0))
01936                      || ((0x94 == (code_msb & 0xfe))
01937                          && (0x0c == (code_lsb & 0x0e))))
01938               {
01939                 /* This insn is a rjmp or a jmp.  */
01940                 unsigned char next_insn_msb = 0;
01941                 unsigned char next_insn_lsb = 0;
01942                 int insn_size;
01943 
01944                 if (0xc0 == (code_msb & 0xf0))
01945                   insn_size = 2; /* rjmp insn */
01946                 else
01947                   insn_size = 4; /* jmp insn */
01948 
01949                 if (irel->r_offset + insn_size + 1 < sec->size)
01950                   {
01951                     next_insn_msb =
01952                         bfd_get_8 (abfd, contents + irel->r_offset
01953                                          + insn_size + 1);
01954                     next_insn_lsb =
01955                         bfd_get_8 (abfd, contents + irel->r_offset
01956                                          + insn_size);
01957                   }
01958 
01959                 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
01960                   {
01961                     /* The next insn is a ret. We possibly could delete
01962                        this ret. First we need to check for preceeding
01963                        sbis/sbic/sbrs or cpse "skip" instructions.  */
01964 
01965                     int there_is_preceeding_non_skip_insn = 1;
01966                     bfd_vma address_of_ret;
01967 
01968                     address_of_ret = dot + insn_size;
01969 
01970                     if (debug_relax && (insn_size == 2))
01971                       printf ("found rjmp / ret sequence at address 0x%x\n",
01972                               (int) dot);
01973                     if (debug_relax && (insn_size == 4))
01974                       printf ("found jmp / ret sequence at address 0x%x\n",
01975                               (int) dot);
01976 
01977                     /* We have to make sure that there is a preceeding insn.  */
01978                     if (irel->r_offset >= 2)
01979                       {
01980                         unsigned char preceeding_msb;
01981                         unsigned char preceeding_lsb;
01982                         preceeding_msb =
01983                             bfd_get_8 (abfd, contents + irel->r_offset - 1);
01984                         preceeding_lsb =
01985                             bfd_get_8 (abfd, contents + irel->r_offset - 2);
01986 
01987                         /* sbic.  */
01988                         if (0x99 == preceeding_msb)
01989                           there_is_preceeding_non_skip_insn = 0;
01990 
01991                         /* sbis.  */
01992                         if (0x9b == preceeding_msb)
01993                           there_is_preceeding_non_skip_insn = 0;
01994 
01995                         /* sbrc */
01996                         if ((0xfc == (preceeding_msb & 0xfe)
01997                             && (0x00 == (preceeding_lsb & 0x08))))
01998                           there_is_preceeding_non_skip_insn = 0;
01999 
02000                         /* sbrs */
02001                         if ((0xfe == (preceeding_msb & 0xfe)
02002                             && (0x00 == (preceeding_lsb & 0x08))))
02003                           there_is_preceeding_non_skip_insn = 0;
02004 
02005                         /* cpse */
02006                         if (0x10 == (preceeding_msb & 0xfc))
02007                           there_is_preceeding_non_skip_insn = 0;
02008 
02009                         if (there_is_preceeding_non_skip_insn == 0)
02010                           if (debug_relax)
02011                             printf ("preceeding skip insn prevents deletion of"
02012                                     " ret insn at addr 0x%x in section %s\n",
02013                                     (int) dot + 2, sec->name);
02014                       }
02015                     else
02016                       {
02017                         /* There is no previous instruction.  */
02018                         there_is_preceeding_non_skip_insn = 0;
02019                       }
02020 
02021                     if (there_is_preceeding_non_skip_insn)
02022                       {
02023                         /* We now only have to make sure that there is no
02024                            local label defined at the address of the ret
02025                            instruction and that there is no local relocation
02026                            in this section pointing to the ret.  */
02027 
02028                         int deleting_ret_is_safe = 1;
02029                         unsigned int section_offset_of_ret_insn =
02030                                           irel->r_offset + insn_size;
02031                         Elf_Internal_Sym *isym, *isymend;
02032                         unsigned int sec_shndx;
02033 
02034                         sec_shndx =
02035                        _bfd_elf_section_from_bfd_section (abfd, sec);
02036 
02037                         /* Check for local symbols.  */
02038                         isym = (Elf_Internal_Sym *) symtab_hdr->contents;
02039                         isymend = isym + symtab_hdr->sh_info;
02040                         for (; isym < isymend; isym++)
02041                          {
02042                            if (isym->st_value == section_offset_of_ret_insn
02043                                && isym->st_shndx == sec_shndx)
02044                              {
02045                                deleting_ret_is_safe = 0;
02046                                if (debug_relax)
02047                                  printf ("local label prevents deletion of ret "
02048                                          "insn at address 0x%x\n",
02049                                          (int) dot + insn_size);
02050                              }
02051                          }
02052 
02053                          /* Now check for global symbols.  */
02054                          {
02055                            int symcount;
02056                            struct elf_link_hash_entry **sym_hashes;
02057                            struct elf_link_hash_entry **end_hashes;
02058 
02059                            symcount = (symtab_hdr->sh_size
02060                                        / sizeof (Elf32_External_Sym)
02061                                        - symtab_hdr->sh_info);
02062                            sym_hashes = elf_sym_hashes (abfd);
02063                            end_hashes = sym_hashes + symcount;
02064                            for (; sym_hashes < end_hashes; sym_hashes++)
02065                             {
02066                               struct elf_link_hash_entry *sym_hash =
02067                                                                  *sym_hashes;
02068                               if ((sym_hash->root.type == bfd_link_hash_defined
02069                                   || sym_hash->root.type ==
02070                                bfd_link_hash_defweak)
02071                                   && sym_hash->root.u.def.section == sec
02072                                   && sym_hash->root.u.def.value == section_offset_of_ret_insn)
02073                                 {
02074                                   deleting_ret_is_safe = 0;
02075                                   if (debug_relax)
02076                                     printf ("global label prevents deletion of "
02077                                             "ret insn at address 0x%x\n",
02078                                             (int) dot + insn_size);
02079                                 }
02080                             }
02081                          }
02082                          /* Now we check for relocations pointing to ret.  */
02083                          {
02084                            Elf_Internal_Rela *irel;
02085                            Elf_Internal_Rela *relend;
02086                            Elf_Internal_Shdr *symtab_hdr;
02087 
02088                            symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
02089                            relend = elf_section_data (sec)->relocs
02090                                     + sec->reloc_count;
02091 
02092                            for (irel = elf_section_data (sec)->relocs;
02093                                 irel < relend; irel++)
02094                              {
02095                                bfd_vma reloc_target = 0;
02096                                bfd_vma symval;
02097                                Elf_Internal_Sym *isymbuf = NULL;
02098 
02099                                /* Read this BFD's local symbols if we haven't
02100                                   done so already.  */
02101                                if (isymbuf == NULL && symtab_hdr->sh_info != 0)
02102                                  {
02103                                    isymbuf = (Elf_Internal_Sym *)
02104                                              symtab_hdr->contents;
02105                                    if (isymbuf == NULL)
02106                                      isymbuf = bfd_elf_get_elf_syms
02107                                    (abfd,
02108                                    symtab_hdr,
02109                                    symtab_hdr->sh_info, 0,
02110                                    NULL, NULL, NULL);
02111                                    if (isymbuf == NULL)
02112                                      break;
02113                                   }
02114 
02115                                /* Get the value of the symbol referred to
02116                                   by the reloc.  */
02117                                if (ELF32_R_SYM (irel->r_info)
02118                                    < symtab_hdr->sh_info)
02119                                  {
02120                                    /* A local symbol.  */
02121                                    Elf_Internal_Sym *isym;
02122                                    asection *sym_sec;
02123 
02124                                    isym = isymbuf
02125                                           + ELF32_R_SYM (irel->r_info);
02126                                    sym_sec = bfd_section_from_elf_index
02127                                  (abfd, isym->st_shndx);
02128                                    symval = isym->st_value;
02129 
02130                                    /* If the reloc is absolute, it will not
02131                                       have a symbol or section associated
02132                                       with it.  */
02133 
02134                                    if (sym_sec)
02135                                      {
02136                                        symval +=
02137                                            sym_sec->output_section->vma
02138                                            + sym_sec->output_offset;
02139                                        reloc_target = symval + irel->r_addend;
02140                                      }
02141                                    else
02142                                      {
02143                                        reloc_target = symval + irel->r_addend;
02144                                        /* Reference symbol is absolute.  */
02145                                      }
02146                                  }
02147                             /* else ... reference symbol is extern.  */
02148 
02149                                if (address_of_ret == reloc_target)
02150                                  {
02151                                    deleting_ret_is_safe = 0;
02152                                    if (debug_relax)
02153                                      printf ("ret from "
02154                                              "rjmp/jmp ret sequence at address"
02155                                              " 0x%x could not be deleted. ret"
02156                                              " is target of a relocation.\n",
02157                                              (int) address_of_ret);
02158                                  }
02159                              }
02160                          }
02161 
02162                          if (deleting_ret_is_safe)
02163                            {
02164                              if (debug_relax)
02165                                printf ("unreachable ret instruction "
02166                                        "at address 0x%x deleted.\n",
02167                                        (int) dot + insn_size);
02168 
02169                              /* Delete two bytes of data.  */
02170                              if (!elf32_avr_relax_delete_bytes (abfd, sec,
02171                                         irel->r_offset + insn_size, 2))
02172                                goto error_return;
02173 
02174                              /* That will change things, so, we should relax
02175                                 again. Note that this is not required, and it
02176                                 may be slow.  */
02177                              *again = TRUE;
02178                              break;
02179                            }
02180                       }
02181 
02182                   }
02183               }
02184             break;
02185           }
02186         }
02187     }
02188 
02189   if (contents != NULL
02190       && elf_section_data (sec)->this_hdr.contents != contents)
02191     {
02192       if (! link_info->keep_memory)
02193         free (contents);
02194       else
02195         {
02196           /* Cache the section contents for elf_link_input_bfd.  */
02197           elf_section_data (sec)->this_hdr.contents = contents;
02198         }
02199     }
02200 
02201   if (internal_relocs != NULL
02202       && elf_section_data (sec)->relocs != internal_relocs)
02203     free (internal_relocs);
02204 
02205   return TRUE;
02206 
02207  error_return:
02208   if (isymbuf != NULL
02209       && symtab_hdr->contents != (unsigned char *) isymbuf)
02210     free (isymbuf);
02211   if (contents != NULL
02212       && elf_section_data (sec)->this_hdr.contents != contents)
02213     free (contents);
02214   if (internal_relocs != NULL
02215       && elf_section_data (sec)->relocs != internal_relocs)
02216     free (internal_relocs);
02217 
02218   return FALSE;
02219 }
02220 
02221 /* This is a version of bfd_generic_get_relocated_section_contents
02222    which uses elf32_avr_relocate_section.
02223 
02224    For avr it's essentially a cut and paste taken from the H8300 port.
02225    The author of the relaxation support patch for avr had absolutely no
02226    clue what is happening here but found out that this part of the code
02227    seems to be important.  */
02228 
02229 static bfd_byte *
02230 elf32_avr_get_relocated_section_contents (bfd *output_bfd,
02231                                           struct bfd_link_info *link_info,
02232                                           struct bfd_link_order *link_order,
02233                                           bfd_byte *data,
02234                                           bfd_boolean relocatable,
02235                                           asymbol **symbols)
02236 {
02237   Elf_Internal_Shdr *symtab_hdr;
02238   asection *input_section = link_order->u.indirect.section;
02239   bfd *input_bfd = input_section->owner;
02240   asection **sections = NULL;
02241   Elf_Internal_Rela *internal_relocs = NULL;
02242   Elf_Internal_Sym *isymbuf = NULL;
02243 
02244   /* We only need to handle the case of relaxing, or of having a
02245      particular set of section contents, specially.  */
02246   if (relocatable
02247       || elf_section_data (input_section)->this_hdr.contents == NULL)
02248     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
02249                                                        link_order, data,
02250                                                        relocatable,
02251                                                        symbols);
02252   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
02253 
02254   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
02255           (size_t) input_section->size);
02256 
02257   if ((input_section->flags & SEC_RELOC) != 0
02258       && input_section->reloc_count > 0)
02259     {
02260       asection **secpp;
02261       Elf_Internal_Sym *isym, *isymend;
02262       bfd_size_type amt;
02263 
02264       internal_relocs = (_bfd_elf_link_read_relocs
02265                          (input_bfd, input_section, NULL, NULL, FALSE));
02266       if (internal_relocs == NULL)
02267         goto error_return;
02268 
02269       if (symtab_hdr->sh_info != 0)
02270         {
02271           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
02272           if (isymbuf == NULL)
02273             isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
02274                                             symtab_hdr->sh_info, 0,
02275                                             NULL, NULL, NULL);
02276           if (isymbuf == NULL)
02277             goto error_return;
02278         }
02279 
02280       amt = symtab_hdr->sh_info;
02281       amt *= sizeof (asection *);
02282       sections = bfd_malloc (amt);
02283       if (sections == NULL && amt != 0)
02284         goto error_return;
02285 
02286       isymend = isymbuf + symtab_hdr->sh_info;
02287       for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
02288         {
02289           asection *isec;
02290 
02291           if (isym->st_shndx == SHN_UNDEF)
02292             isec = bfd_und_section_ptr;
02293           else if (isym->st_shndx == SHN_ABS)
02294             isec = bfd_abs_section_ptr;
02295           else if (isym->st_shndx == SHN_COMMON)
02296             isec = bfd_com_section_ptr;
02297           else
02298             isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
02299 
02300           *secpp = isec;
02301         }
02302 
02303       if (! elf32_avr_relocate_section (output_bfd, link_info, input_bfd,
02304                                         input_section, data, internal_relocs,
02305                                         isymbuf, sections))
02306         goto error_return;
02307 
02308       if (sections != NULL)
02309         free (sections);
02310       if (isymbuf != NULL
02311           && symtab_hdr->contents != (unsigned char *) isymbuf)
02312         free (isymbuf);
02313       if (elf_section_data (input_section)->relocs != internal_relocs)
02314         free (internal_relocs);
02315     }
02316 
02317   return data;
02318 
02319  error_return:
02320   if (sections != NULL)
02321     free (sections);
02322   if (isymbuf != NULL
02323       && symtab_hdr->contents != (unsigned char *) isymbuf)
02324     free (isymbuf);
02325   if (internal_relocs != NULL
02326       && elf_section_data (input_section)->relocs != internal_relocs)
02327     free (internal_relocs);
02328   return NULL;
02329 }
02330 
02331 
02332 /* Determines the hash entry name for a particular reloc. It consists of
02333    the identifier of the symbol section and the added reloc addend and
02334    symbol offset relative to the section the symbol is attached to.  */
02335 
02336 static char *
02337 avr_stub_name (const asection *symbol_section,
02338                const bfd_vma symbol_offset,
02339                const Elf_Internal_Rela *rela)
02340 {
02341   char *stub_name;
02342   bfd_size_type len;
02343 
02344   len = 8 + 1 + 8 + 1 + 1;
02345   stub_name = bfd_malloc (len);
02346 
02347   sprintf (stub_name, "%08x+%08x",
02348            symbol_section->id & 0xffffffff,
02349            (unsigned int) ((rela->r_addend & 0xffffffff) + symbol_offset));
02350 
02351   return stub_name;
02352 }
02353 
02354 
02355 /* Add a new stub entry to the stub hash.  Not all fields of the new
02356    stub entry are initialised.  */
02357 
02358 static struct elf32_avr_stub_hash_entry *
02359 avr_add_stub (const char *stub_name,
02360               struct elf32_avr_link_hash_table *htab)
02361 {
02362   struct elf32_avr_stub_hash_entry *hsh;
02363 
02364   /* Enter this entry into the linker stub hash table.  */
02365   hsh = avr_stub_hash_lookup (&htab->bstab, stub_name, TRUE, FALSE);
02366 
02367   if (hsh == NULL)
02368     {
02369       (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
02370                              NULL, stub_name);
02371       return NULL;
02372     }
02373 
02374   hsh->stub_offset = 0;
02375   return hsh;
02376 }
02377 
02378 /* We assume that there is already space allocated for the stub section
02379    contents and that before building the stubs the section size is
02380    initialized to 0.  We assume that within the stub hash table entry,
02381    the absolute position of the jmp target has been written in the
02382    target_value field.  We write here the offset of the generated jmp insn
02383    relative to the trampoline section start to the stub_offset entry in
02384    the stub hash table entry.  */
02385 
02386 static  bfd_boolean
02387 avr_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
02388 {
02389   struct elf32_avr_stub_hash_entry *hsh;
02390   struct bfd_link_info *info;
02391   struct elf32_avr_link_hash_table *htab;
02392   bfd *stub_bfd;
02393   bfd_byte *loc;
02394   bfd_vma target;
02395   bfd_vma starget;
02396 
02397   /* Basic opcode */
02398   bfd_vma jmp_insn = 0x0000940c;
02399 
02400   /* Massage our args to the form they really have.  */
02401   hsh = avr_stub_hash_entry (bh);
02402 
02403   if (!hsh->is_actually_needed)
02404     return TRUE;
02405 
02406   info = (struct bfd_link_info *) in_arg;
02407 
02408   htab = avr_link_hash_table (info);
02409   if (htab == NULL)
02410     return FALSE;
02411 
02412   target = hsh->target_value;
02413 
02414   /* Make a note of the offset within the stubs for this entry.  */
02415   hsh->stub_offset = htab->stub_sec->size;
02416   loc = htab->stub_sec->contents + hsh->stub_offset;
02417 
02418   stub_bfd = htab->stub_sec->owner;
02419 
02420   if (debug_stubs)
02421     printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
02422              (unsigned int) target,
02423              (unsigned int) hsh->stub_offset);
02424 
02425   /* We now have to add the information on the jump target to the bare
02426      opcode bits already set in jmp_insn.  */
02427 
02428   /* Check for the alignment of the address.  */
02429   if (target & 1)
02430      return FALSE;
02431 
02432   starget = target >> 1;
02433   jmp_insn |= ((starget & 0x10000) | ((starget << 3) & 0x1f00000)) >> 16;
02434   bfd_put_16 (stub_bfd, jmp_insn, loc);
02435   bfd_put_16 (stub_bfd, (bfd_vma) starget & 0xffff, loc + 2);
02436 
02437   htab->stub_sec->size += 4;
02438 
02439   /* Now add the entries in the address mapping table if there is still
02440      space left.  */
02441   {
02442     unsigned int nr;
02443 
02444     nr = htab->amt_entry_cnt + 1;
02445     if (nr <= htab->amt_max_entry_cnt)
02446       {
02447         htab->amt_entry_cnt = nr;
02448 
02449         htab->amt_stub_offsets[nr - 1] = hsh->stub_offset;
02450         htab->amt_destination_addr[nr - 1] = target;
02451       }
02452   }
02453 
02454   return TRUE;
02455 }
02456 
02457 static bfd_boolean
02458 avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry *bh,
02459                                    void *in_arg)
02460 {
02461   struct elf32_avr_stub_hash_entry *hsh;
02462   struct elf32_avr_link_hash_table *htab;
02463 
02464   htab = in_arg;
02465   hsh = avr_stub_hash_entry (bh);
02466   hsh->is_actually_needed = FALSE;
02467 
02468   return TRUE;
02469 }
02470 
02471 static bfd_boolean
02472 avr_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
02473 {
02474   struct elf32_avr_stub_hash_entry *hsh;
02475   struct elf32_avr_link_hash_table *htab;
02476   int size;
02477 
02478   /* Massage our args to the form they really have.  */
02479   hsh = avr_stub_hash_entry (bh);
02480   htab = in_arg;
02481 
02482   if (hsh->is_actually_needed)
02483     size = 4;
02484   else
02485     size = 0;
02486 
02487   htab->stub_sec->size += size;
02488   return TRUE;
02489 }
02490 
02491 void
02492 elf32_avr_setup_params (struct bfd_link_info *info,
02493                         bfd *avr_stub_bfd,
02494                         asection *avr_stub_section,
02495                         bfd_boolean no_stubs,
02496                         bfd_boolean deb_stubs,
02497                         bfd_boolean deb_relax,
02498                         bfd_vma pc_wrap_around,
02499                         bfd_boolean call_ret_replacement)
02500 {
02501   struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
02502 
02503   if (htab == NULL)
02504     return;
02505   htab->stub_sec = avr_stub_section;
02506   htab->stub_bfd = avr_stub_bfd;
02507   htab->no_stubs = no_stubs;
02508 
02509   debug_relax = deb_relax;
02510   debug_stubs = deb_stubs;
02511   avr_pc_wrap_around = pc_wrap_around;
02512   avr_replace_call_ret_sequences = call_ret_replacement;
02513 }
02514 
02515 
02516 /* Set up various things so that we can make a list of input sections
02517    for each output section included in the link.  Returns -1 on error,
02518    0 when no stubs will be needed, and 1 on success.  It also sets
02519    information on the stubs bfd and the stub section in the info
02520    struct.  */
02521 
02522 int
02523 elf32_avr_setup_section_lists (bfd *output_bfd,
02524                                struct bfd_link_info *info)
02525 {
02526   bfd *input_bfd;
02527   unsigned int bfd_count;
02528   int top_id, top_index;
02529   asection *section;
02530   asection **input_list, **list;
02531   bfd_size_type amt;
02532   struct elf32_avr_link_hash_table *htab = avr_link_hash_table(info);
02533 
02534   if (htab == NULL || htab->no_stubs)
02535     return 0;
02536 
02537   /* Count the number of input BFDs and find the top input section id.  */
02538   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
02539        input_bfd != NULL;
02540        input_bfd = input_bfd->link_next)
02541     {
02542       bfd_count += 1;
02543       for (section = input_bfd->sections;
02544            section != NULL;
02545            section = section->next)
02546        if (top_id < section->id)
02547          top_id = section->id;
02548     }
02549 
02550   htab->bfd_count = bfd_count;
02551 
02552   /* We can't use output_bfd->section_count here to find the top output
02553      section index as some sections may have been removed, and
02554      strip_excluded_output_sections doesn't renumber the indices.  */
02555   for (section = output_bfd->sections, top_index = 0;
02556        section != NULL;
02557        section = section->next)
02558     if (top_index < section->index)
02559       top_index = section->index;
02560 
02561   htab->top_index = top_index;
02562   amt = sizeof (asection *) * (top_index + 1);
02563   input_list = bfd_malloc (amt);
02564   htab->input_list = input_list;
02565   if (input_list == NULL)
02566     return -1;
02567 
02568   /* For sections we aren't interested in, mark their entries with a
02569      value we can check later.  */
02570   list = input_list + top_index;
02571   do
02572     *list = bfd_abs_section_ptr;
02573   while (list-- != input_list);
02574 
02575   for (section = output_bfd->sections;
02576        section != NULL;
02577        section = section->next)
02578     if ((section->flags & SEC_CODE) != 0)
02579       input_list[section->index] = NULL;
02580 
02581   return 1;
02582 }
02583 
02584 
02585 /* Read in all local syms for all input bfds, and create hash entries
02586    for export stubs if we are building a multi-subspace shared lib.
02587    Returns -1 on error, 0 otherwise.  */
02588 
02589 static int
02590 get_local_syms (bfd *input_bfd, struct bfd_link_info *info)
02591 {
02592   unsigned int bfd_indx;
02593   Elf_Internal_Sym *local_syms, **all_local_syms;
02594   struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
02595 
02596   if (htab == NULL)
02597     return -1;
02598 
02599   /* We want to read in symbol extension records only once.  To do this
02600      we need to read in the local symbols in parallel and save them for
02601      later use; so hold pointers to the local symbols in an array.  */
02602   bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
02603   all_local_syms = bfd_zmalloc (amt);
02604   htab->all_local_syms = all_local_syms;
02605   if (all_local_syms == NULL)
02606     return -1;
02607 
02608   /* Walk over all the input BFDs, swapping in local symbols.
02609      If we are creating a shared library, create hash entries for the
02610      export stubs.  */
02611   for (bfd_indx = 0;
02612        input_bfd != NULL;
02613        input_bfd = input_bfd->link_next, bfd_indx++)
02614     {
02615       Elf_Internal_Shdr *symtab_hdr;
02616 
02617       /* We'll need the symbol table in a second.  */
02618       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
02619       if (symtab_hdr->sh_info == 0)
02620        continue;
02621 
02622       /* We need an array of the local symbols attached to the input bfd.  */
02623       local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
02624       if (local_syms == NULL)
02625        {
02626          local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
02627                                         symtab_hdr->sh_info, 0,
02628                                         NULL, NULL, NULL);
02629          /* Cache them for elf_link_input_bfd.  */
02630          symtab_hdr->contents = (unsigned char *) local_syms;
02631        }
02632       if (local_syms == NULL)
02633        return -1;
02634 
02635       all_local_syms[bfd_indx] = local_syms;
02636     }
02637 
02638   return 0;
02639 }
02640 
02641 #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
02642 
02643 bfd_boolean
02644 elf32_avr_size_stubs (bfd *output_bfd,
02645                       struct bfd_link_info *info,
02646                       bfd_boolean is_prealloc_run)
02647 {
02648   struct elf32_avr_link_hash_table *htab;
02649   int stub_changed = 0;
02650 
02651   htab = avr_link_hash_table (info);
02652   if (htab == NULL)
02653     return FALSE;
02654 
02655   /* At this point we initialize htab->vector_base
02656      To the start of the text output section.  */
02657   htab->vector_base = htab->stub_sec->output_section->vma;
02658 
02659   if (get_local_syms (info->input_bfds, info))
02660     {
02661       if (htab->all_local_syms)
02662        goto error_ret_free_local;
02663       return FALSE;
02664     }
02665 
02666   if (ADD_DUMMY_STUBS_FOR_DEBUGGING)
02667     {
02668       struct elf32_avr_stub_hash_entry *test;
02669 
02670       test = avr_add_stub ("Hugo",htab);
02671       test->target_value = 0x123456;
02672       test->stub_offset = 13;
02673 
02674       test = avr_add_stub ("Hugo2",htab);
02675       test->target_value = 0x84210;
02676       test->stub_offset = 14;
02677     }
02678 
02679   while (1)
02680     {
02681       bfd *input_bfd;
02682       unsigned int bfd_indx;
02683 
02684       /* We will have to re-generate the stub hash table each time anything
02685          in memory has changed.  */
02686 
02687       bfd_hash_traverse (&htab->bstab, avr_mark_stub_not_to_be_necessary, htab);
02688       for (input_bfd = info->input_bfds, bfd_indx = 0;
02689            input_bfd != NULL;
02690            input_bfd = input_bfd->link_next, bfd_indx++)
02691         {
02692           Elf_Internal_Shdr *symtab_hdr;
02693           asection *section;
02694           Elf_Internal_Sym *local_syms;
02695 
02696           /* We'll need the symbol table in a second.  */
02697           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
02698           if (symtab_hdr->sh_info == 0)
02699             continue;
02700 
02701           local_syms = htab->all_local_syms[bfd_indx];
02702 
02703           /* Walk over each section attached to the input bfd.  */
02704           for (section = input_bfd->sections;
02705                section != NULL;
02706                section = section->next)
02707             {
02708               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
02709 
02710               /* If there aren't any relocs, then there's nothing more
02711                  to do.  */
02712               if ((section->flags & SEC_RELOC) == 0
02713                   || section->reloc_count == 0)
02714                 continue;
02715 
02716               /* If this section is a link-once section that will be
02717                  discarded, then don't create any stubs.  */
02718               if (section->output_section == NULL
02719                   || section->output_section->owner != output_bfd)
02720                 continue;
02721 
02722               /* Get the relocs.  */
02723               internal_relocs
02724                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
02725                                              info->keep_memory);
02726               if (internal_relocs == NULL)
02727                 goto error_ret_free_local;
02728 
02729               /* Now examine each relocation.  */
02730               irela = internal_relocs;
02731               irelaend = irela + section->reloc_count;
02732               for (; irela < irelaend; irela++)
02733                 {
02734                   unsigned int r_type, r_indx;
02735                   struct elf32_avr_stub_hash_entry *hsh;
02736                   asection *sym_sec;
02737                   bfd_vma sym_value;
02738                   bfd_vma destination;
02739                   struct elf_link_hash_entry *hh;
02740                   char *stub_name;
02741 
02742                   r_type = ELF32_R_TYPE (irela->r_info);
02743                   r_indx = ELF32_R_SYM (irela->r_info);
02744 
02745                   /* Only look for 16 bit GS relocs. No other reloc will need a
02746                      stub.  */
02747                   if (!((r_type == R_AVR_16_PM)
02748                         || (r_type == R_AVR_LO8_LDI_GS)
02749                         || (r_type == R_AVR_HI8_LDI_GS)))
02750                     continue;
02751 
02752                   /* Now determine the call target, its name, value,
02753                      section.  */
02754                   sym_sec = NULL;
02755                   sym_value = 0;
02756                   destination = 0;
02757                   hh = NULL;
02758                   if (r_indx < symtab_hdr->sh_info)
02759                     {
02760                       /* It's a local symbol.  */
02761                       Elf_Internal_Sym *sym;
02762                       Elf_Internal_Shdr *hdr;
02763 
02764                       sym = local_syms + r_indx;
02765                       hdr = elf_elfsections (input_bfd)[sym->st_shndx];
02766                       sym_sec = hdr->bfd_section;
02767                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
02768                         sym_value = sym->st_value;
02769                       destination = (sym_value + irela->r_addend
02770                                      + sym_sec->output_offset
02771                                      + sym_sec->output_section->vma);
02772                     }
02773                   else
02774                     {
02775                       /* It's an external symbol.  */
02776                       int e_indx;
02777 
02778                       e_indx = r_indx - symtab_hdr->sh_info;
02779                       hh = elf_sym_hashes (input_bfd)[e_indx];
02780 
02781                       while (hh->root.type == bfd_link_hash_indirect
02782                              || hh->root.type == bfd_link_hash_warning)
02783                         hh = (struct elf_link_hash_entry *)
02784                               (hh->root.u.i.link);
02785 
02786                       if (hh->root.type == bfd_link_hash_defined
02787                           || hh->root.type == bfd_link_hash_defweak)
02788                         {
02789                           sym_sec = hh->root.u.def.section;
02790                           sym_value = hh->root.u.def.value;
02791                           if (sym_sec->output_section != NULL)
02792                           destination = (sym_value + irela->r_addend
02793                                          + sym_sec->output_offset
02794                                          + sym_sec->output_section->vma);
02795                         }
02796                       else if (hh->root.type == bfd_link_hash_undefweak)
02797                         {
02798                           if (! info->shared)
02799                             continue;
02800                         }
02801                       else if (hh->root.type == bfd_link_hash_undefined)
02802                         {
02803                           if (! (info->unresolved_syms_in_objects == RM_IGNORE
02804                                  && (ELF_ST_VISIBILITY (hh->other)
02805                                      == STV_DEFAULT)))
02806                              continue;
02807                         }
02808                       else
02809                         {
02810                           bfd_set_error (bfd_error_bad_value);
02811 
02812                           error_ret_free_internal:
02813                           if (elf_section_data (section)->relocs == NULL)
02814                             free (internal_relocs);
02815                           goto error_ret_free_local;
02816                         }
02817                     }
02818 
02819                   if (! avr_stub_is_required_for_16_bit_reloc
02820                     (destination - htab->vector_base))
02821                     {
02822                       if (!is_prealloc_run)
02823                      /* We are having a reloc that does't need a stub.  */
02824                      continue;
02825 
02826                     /* We don't right now know if a stub will be needed.
02827                       Let's rather be on the safe side.  */
02828                     }
02829 
02830                   /* Get the name of this stub.  */
02831                   stub_name = avr_stub_name (sym_sec, sym_value, irela);
02832 
02833                   if (!stub_name)
02834                     goto error_ret_free_internal;
02835 
02836 
02837                   hsh = avr_stub_hash_lookup (&htab->bstab,
02838                                               stub_name,
02839                                               FALSE, FALSE);
02840                   if (hsh != NULL)
02841                     {
02842                       /* The proper stub has already been created.  Mark it
02843                          to be used and write the possibly changed destination
02844                          value.  */
02845                       hsh->is_actually_needed = TRUE;
02846                       hsh->target_value = destination;
02847                       free (stub_name);
02848                       continue;
02849                     }
02850 
02851                   hsh = avr_add_stub (stub_name, htab);
02852                   if (hsh == NULL)
02853                     {
02854                       free (stub_name);
02855                       goto error_ret_free_internal;
02856                     }
02857 
02858                   hsh->is_actually_needed = TRUE;
02859                   hsh->target_value = destination;
02860 
02861                   if (debug_stubs)
02862                     printf ("Adding stub with destination 0x%x to the"
02863                             " hash table.\n", (unsigned int) destination);
02864                   if (debug_stubs)
02865                     printf ("(Pre-Alloc run: %i)\n", is_prealloc_run);
02866 
02867                   stub_changed = TRUE;
02868                 }
02869 
02870               /* We're done with the internal relocs, free them.  */
02871               if (elf_section_data (section)->relocs == NULL)
02872                 free (internal_relocs);
02873             }
02874         }
02875 
02876       /* Re-Calculate the number of needed stubs.  */
02877       htab->stub_sec->size = 0;
02878       bfd_hash_traverse (&htab->bstab, avr_size_one_stub, htab);
02879 
02880       if (!stub_changed)
02881         break;
02882 
02883       stub_changed = FALSE;
02884     }
02885 
02886   free (htab->all_local_syms);
02887   return TRUE;
02888 
02889  error_ret_free_local:
02890   free (htab->all_local_syms);
02891   return FALSE;
02892 }
02893 
02894 
02895 /* Build all the stubs associated with the current output file.  The
02896    stubs are kept in a hash table attached to the main linker hash
02897    table.  We also set up the .plt entries for statically linked PIC
02898    functions here.  This function is called via hppaelf_finish in the
02899    linker.  */
02900 
02901 bfd_boolean
02902 elf32_avr_build_stubs (struct bfd_link_info *info)
02903 {
02904   asection *stub_sec;
02905   struct bfd_hash_table *table;
02906   struct elf32_avr_link_hash_table *htab;
02907   bfd_size_type total_size = 0;
02908 
02909   htab = avr_link_hash_table (info);
02910   if (htab == NULL)
02911     return FALSE;
02912 
02913   /* In case that there were several stub sections:  */
02914   for (stub_sec = htab->stub_bfd->sections;
02915        stub_sec != NULL;
02916        stub_sec = stub_sec->next)
02917     {
02918       bfd_size_type size;
02919 
02920       /* Allocate memory to hold the linker stubs.  */
02921       size = stub_sec->size;
02922       total_size += size;
02923 
02924       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
02925       if (stub_sec->contents == NULL && size != 0)
02926        return FALSE;
02927       stub_sec->size = 0;
02928     }
02929 
02930   /* Allocate memory for the adress mapping table.  */
02931   htab->amt_entry_cnt = 0;
02932   htab->amt_max_entry_cnt = total_size / 4;
02933   htab->amt_stub_offsets = bfd_malloc (sizeof (bfd_vma)
02934                                        * htab->amt_max_entry_cnt);
02935   htab->amt_destination_addr = bfd_malloc (sizeof (bfd_vma)
02936                                       * htab->amt_max_entry_cnt );
02937 
02938   if (debug_stubs)
02939     printf ("Allocating %i entries in the AMT\n", htab->amt_max_entry_cnt);
02940 
02941   /* Build the stubs as directed by the stub hash table.  */
02942   table = &htab->bstab;
02943   bfd_hash_traverse (table, avr_build_one_stub, info);
02944 
02945   if (debug_stubs)
02946     printf ("Final Stub section Size: %i\n", (int) htab->stub_sec->size);
02947 
02948   return TRUE;
02949 }
02950 
02951 #define ELF_ARCH            bfd_arch_avr
02952 #define ELF_MACHINE_CODE    EM_AVR
02953 #define ELF_MACHINE_ALT1    EM_AVR_OLD
02954 #define ELF_MAXPAGESIZE            1
02955 
02956 #define TARGET_LITTLE_SYM       bfd_elf32_avr_vec
02957 #define TARGET_LITTLE_NAME  "elf32-avr"
02958 
02959 #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
02960 #define bfd_elf32_bfd_link_hash_table_free   elf32_avr_link_hash_table_free
02961 
02962 #define elf_info_to_howto                avr_info_to_howto_rela
02963 #define elf_info_to_howto_rel                   NULL
02964 #define elf_backend_relocate_section         elf32_avr_relocate_section
02965 #define elf_backend_check_relocs             elf32_avr_check_relocs
02966 #define elf_backend_can_gc_sections          1
02967 #define elf_backend_rela_normal                1
02968 #define elf_backend_final_write_processing \
02969                                    bfd_elf_avr_final_write_processing
02970 #define elf_backend_object_p              elf32_avr_object_p
02971 
02972 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
02973 #define bfd_elf32_bfd_get_relocated_section_contents \
02974                                         elf32_avr_get_relocated_section_contents
02975 
02976 #include "elf32-target.h"