This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH series] AVR: add support for new controller family to binutils, gcc and avr-libc


Hi,

Here is a patch series that adds support for a new controller family avr6 to 
the AVR ports of the gnu toolchain. This mail is cross-posted on the lists 
for the three affected projects since in their isolated form the patches are 
not really usefull:

binutils, gcc and avr-libc.

The attached patches are against current head of binutils, gccs and avr-libc. 

I'd like to classify them now to be "beta" versions rather than "alpha" and, 
IMO, they now should be compliant with the coding requirements for gcc and 
binutils for the respective head branches.

The basic problem for adding support for devices with > 64k Words of program
memory was that gcc would need more than the 2-Bytes that are currently used 
for program memory pointers. Using 4-Byte pointers would have resulted in 
very inefficient code and many changes in the avr backend of gcc.

The used method follows Marek's suggestions concerning the insertion of 
trampolines in the lower memory, if the target address happens not to be
reachable with 16 bit relocs. Most of the work, thus is at the binutils
side. GCC itself had mainly only to be teached to use different frame offset 
for the different devices. (The other changes were only necessary in order to 
enable bootloader support).


Two other general issues are fixed with gcc part of the present patch family:

1.) citing from the gcc info pages:

`-fdelete-null-pointer-checks'
     Use global dataflow analysis to identify and eliminate useless
     checks for null pointers.  The compiler assumes that dereferencing
     a null pointer would have halted the program.  If a pointer is
     checked after it has already been dereferenced, it cannot be null.

     In some environments, this assumption is not true, and programs can
     safely dereference null pointers.  Use
     `-fno-delete-null-pointer-checks' to disable this optimization for
     programs which depend on that behavior.

The AVR HW does not implement null-pointer-checks and this option now is
allways used for the avr target.

2.) make main a normal function

As discussed on the avr-libc-dev list, it is desirable that main() is just
an ordinary function like any other. The stack pointer initialization is
now only located in the gcrt1.S file of the avr-libc.


In comparison to the earlier patches posted on the avr-libc-dev list,
some bugs of the binutils part for that I had received private mail have now 
been fixed and Mareks suggestions concerning new relocs and bootloader 
support have been realized.

Note that concerning the avr-libc part the device specific header files for 
mega2560 and mega2561 still need some checking!

Yours,

Bjoern.


Summary of the changes to the binutils:

The template used for implementing the linker stubs for avr was the hppa
target. The difference here is, that we are having only one single
stub section that is created at the very beginning in the linker emulation
template. The placement of this stub section is controlled by it's name that
is included in the scripttemplate. It will be linked right after the 
section used for initializing the contents of the .data section.

In order to implement this, it was necessary to add support for a derived
linker hash table and an additional stub hash table.

avr-ld now is aware of new target specific options (documented in the
avr-ld --help output):

The two options

 --no-call-ret-replacement
 --pmem-wrap-around

control the linker relaxation optimizations. When defining 
--pm-wrap-around=16k,
the linker now tries to shorten jumps from absolute 14k to absolute 1k by 
using
the fact that the PC wraps around when using a shorter relative jump.
This is, IMO, useful only for the 16k and 32k devices. 

When using --no-stubs, the linker will not generate the jump stubs even when
using an avr6 device.

By use of the --debug-stubs and --debug-relax switches one could activate
debugging printout to stdout that is meant to be used when debugging avr-ld 
itself. It's not meant to be used by the user, but documented never the less.

In order to distinguish relocs where stubs should be generated and relocs 
where
no stubs should be generated, there are now two new ldi-type PM relocs that
carry the GS suffix instead of PM. Gas now knows of the directives gs() that
has the same functionality as pm(), only that it generates the GS relocs that
force the linker to generate stubs.


Change log for the binutils part:

2006-05-10  Bjoern Haase  <bjoern.m.haase@web.de> 

        * bfd/archures.c
	bfd_mach_avr6: add.
	
	* bfd/bfd-in2.h
	bfd_mach_avr6, BFD_RELOC_AVR_LO8_LDI_GS, BFD_RELOC_AVR_HI8_LDI_GS: add.

	* bfd/libbfd.h
	BFD_RELOC_AVR_LO8_LDI_GS, BFD_RELOC_AVR_HI8_LDI_GS: add.

	* bfd/reloc.c
	BFD_RELOC_AVR_LO8_LDI_GS, BFD_RELOC_AVR_HI8_LDI_GS: add.
	
	* bfd/cpu-avr.c
	bfd_mach_avr6: add.
	
	* bfd/elf32-avr.h: new file
	
	* bfd/elf32-avr.c
	Add elf32-avr.h to include list.
	elf32_avr_stub_hash_entry: add.
	elf32_avr_link_hash_table: add.
	avr_link_hash_table: add.
	avr_stub_hash_entry: add.
	avr_stub_hash_lookup: add.
	stub_hash_newfunc: add.
	elf32_avr_link_hash_table_create: add.
	elf32_avr_link_hash_table_free: add.
	avr_get_stub_addr: add.
	debug_relax, debug_stubs: add.
	avr_pc_wrap_around: Change type to bfd_vma.
	avr_replace_call_ret_sequences: add.
	avr_stub_is_required_for_16_bit_reloc: add.
	avr_final_link_relocate: 
	Add linker hashtable to parameter list. Use stub addr. if necessary.
	elf32_avr_relocate_section:
	Add linker hashtable to parameter list of avr_final_link_relocate.
	bfd_elf_avr_final_write_processing:
	Add handling for bfd_mach_avr6.
	elf32_avr_object_p:
	Add support for E_AVR_MACH_AVR6.
	elf32_avr_relax_delete_bytes:
	Iterate over all sections of a bfd when adjusting relocs addends.
	elf32_avr_relax_section:
	Use avr_replace_call_ret_sequences, recalculate size of stub section.
	avr_stub_name: add.
	avr_get_stub_addr: add.
	avr_get_stub_entry: add.
	avr_add_stub: add.
	avr_build_one_stub: add.
	avr_size_one_stub: add.
	elf32_avr_setup_params: add.
	elf32_avr_setup_section_lists: add.
	get_local_syms: add.
	elf32_avr_size_stubs: add.
	bfd_elf32_bfd_link_hash_table_create: add.
	bfd_elf32_bfd_link_hash_table_free: add
	
	* gas/config/tc-avr.c: add avr6, avr2560 and avr2561
	
	* gas/config/tc-avr.h:
	TC_VALIDATE_FIX: Don't generate fixups for PM type relocs
	
	* include/elf/avr.h
	E_AVR_MACH_AVR6, R_AVR_LO8_LDI_GS, R_AVR_HI8_LDI_GS: add.
	
	* ld/configure.tgt: Add avr6 to emulation list.
	
	* ld/Makefile.am, ld/Makefile.in:
	Add eavr6.o and corresponding rule.
	
	* ld/emulparams/avr6.sh: add
	
	* ld/emulparams/avrX.sh:
	TEMPLATE_NAME: Change to elf32 instead of generic.
	EXTRA_EM_FILE: add reference to new avrelf.
	
	* ld/scripttempl/avr.sc: Add ".trampolines" section.
	
	* ld/emultampl/avrelf.em: New file.


	Summary of the changes to gcc:

There is now a new variable holding the size of the PC and the frame
offsets are adjusted accordingly.
The tablejump insn now use eijmp instead of ijmp for the
3-Byte PC devices and so does libgcc.S. 
New builtin defines for __AVR_3_BYTE_PC__ and _AVR_2_BYTE_PC__ are now
available.
The option -fno-delete-null-pointer-checks has been added to the option
list, since 0 is a perfectly legal address for some avr devices.
main() now is a normal function without any special handling.


Change log for the gcc part:

2006-05-10  Bjoern Haase  <bjoern.m.haase@web.de> 

	* gcc/gcc/config/avr/avr.c:
	avr_pc_size: add
	struct base_arch: add three_byte_pc to struct.
	struct base_arch_s: add new struct member and add avr6 entry.
	struct mcu_type_s: add atmega2560 and atmega2561.
        avr_override_options: set avr_pc_size to 3 if necessary.
	initial_elimination_offset: use (avr_pc_size) instead of fixed value 2.
	avr_output_function_prologue: use gs() asm specifier instead of pm()
	print_operand_address: ditto.
	avr_assemble_integer: ditto.
	avr_output_addr_vec_elt: ditto.
	avr_output_function_prologue: handle main() like any normal function.
	avr_output_function_epilogue: ditto.

	* gcc/gcc/config/avr/avr.h:
        CC1_SPEC: add -fno-delete-null-pointer-checks to list.
        CC1PLUS_SPEC: ditto.
	LINK_SPEC: add atmega2560 and atmega2561
	CRT_BINUTILS_SPEC: 
	add atmega2560 (crtm2560.o) and atmega2561 (crtm2561.o).
	TARGET_CPU_CPP_BUILTINS: add __AVR_3_BYTE_PC__ and __AVR_2_BYTE_PC__
	AVR_3_BYTE_PC: add.
	AVR_2_BYTE_PC: add.
	
	* gcc/gcc/config/avr/libgcc.S
	
	* gcc/gcc/config/avr/avr.md
	call_insn: use eicall instead of icall for 3 byte PC devices.
	call_value_insn: ditto.
	indirect_jump: use only if AVR_2_BYTE_PC
	tablejump_enh: ditto.
	tablejump: ditto.
	indirect_jump_avr6: add.
	tablejump_avr6: add.	

	* gcc/gcc/config/avr/t-avr:
	MULITLIB_OPTIONS: add avr6 architecture to list
	MULITLIB_DIRNAMES: ditto. 
	MULTILIB_MATCHES: add atmega2560 and atmega2561 to list.


Changes in avr-libc:

The build script and bootstrap scripts have been extended to generate the 
required directories and files for the avr6 family devices.
For the avr6 devices EIND is now initialized in the .init2 step of gcrt1.S and
gcrt1.S is now aware of main() being a normal function.


Change Log for avr-libc:

2006-05-10  Bjoern Haase  <bjoern.m.haase@web.de> 

	* devtools/gen-avr-lib-tree.sh:
	AVR6_DEV_INFO: add.
	AVR_ARH_INFO: add AVR6_DEV_INFO.

	* configure.ac:
	AC_CONFIG_FILES: add avr6/Makefile, atmega2560 and atmega2561.

	* include/avr/io.h:
	add "#include"s for atmega2560 and atmega2561.

	* include/avr/iom2560.h: New file. Register locations are untested.
	* include/avr/iom2561.h: New file. Register locations are untested.

	* crt1/gcrt1.S:
	init2: add EIND initialization to hh8(pm(__vectors))
	init9: use XCALL main instead of XJMP main. Add XJMP exit
? build
? genpatch
? crt1/.gcrt1.S.swp
? include/avr/iom2560.h
? include/avr/iom2561.h
Index: configure.ac
===================================================================
RCS file: /sources/avr-libc/avr-libc/configure.ac,v
retrieving revision 1.32
diff -U12 -r1.32 configure.ac
--- configure.ac	22 Apr 2006 08:41:44 -0000	1.32
+++ configure.ac	12 May 2006 21:25:51 -0000
@@ -531,24 +531,36 @@
 CHECK_AVR_DEVICE(at90usb646)
 AM_CONDITIONAL(HAS_at90usb646, test "x$HAS_at90usb646" = "xyes")
 
 CHECK_AVR_DEVICE(at90usb647)
 AM_CONDITIONAL(HAS_at90usb647, test "x$HAS_at90usb647" = "xyes")
 
 CHECK_AVR_DEVICE(at90usb1286)
 AM_CONDITIONAL(HAS_at90usb1286, test "x$HAS_at90usb1286" = "xyes")
 
 CHECK_AVR_DEVICE(at90usb1287)
 AM_CONDITIONAL(HAS_at90usb1287, test "x$HAS_at90usb1287" = "xyes")
 
+# avr6
+AM_CONDITIONAL(HAS_avr6, true)
+AM_CONDITIONAL(HAS_atmega2560, true)
+AM_CONDITIONAL(HAS_atmega2561, true)
+
+CHECK_AVR_DEVICE(atmega2560)
+AM_CONDITIONAL(HAS_atmega2560, test "x$HAS_atmega2560" = "xyes")
+
+CHECK_AVR_DEVICE(atmega2561)
+AM_CONDITIONAL(HAS_atmega2561, test "x$HAS_atmega2561" = "xyes")
+
+
 # Generate all files from *.in sources.
 
 AC_CONFIG_FILES([
 	Makefile
 	avr-libc.spec
 	avr/Makefile
 	avr/lib/Makefile
 	common/Makefile
 	crt1/Makefile
 	doc/Makefile
 	doc/api/Makefile
 	doc/examples/Makefile
@@ -656,13 +668,19 @@
 	avr/lib/avr5/atmega1280/Makefile
 	avr/lib/avr5/atmega1281/Makefile
 	avr/lib/avr5/at90can32/Makefile
 	avr/lib/avr5/at90can64/Makefile
 	avr/lib/avr5/at90can128/Makefile
 	avr/lib/avr5/at90usb646/Makefile
 	avr/lib/avr5/at90usb647/Makefile
 	avr/lib/avr5/at90usb1286/Makefile
 	avr/lib/avr5/at90usb1287/Makefile
 	avr/lib/avr5/at94k/Makefile
 ])
 
+#avr6
+AC_CONFIG_FILES([
+	avr/lib/avr6/Makefile
+	avr/lib/avr6/atmega2560/Makefile
+	avr/lib/avr6/atmega2561/Makefile
+])
 AC_OUTPUT
Index: crt1/gcrt1.S
===================================================================
RCS file: /sources/avr-libc/avr-libc/crt1/gcrt1.S,v
retrieving revision 1.8
diff -U12 -r1.8 gcrt1.S
--- crt1/gcrt1.S	10 Nov 2005 21:12:09 -0000	1.8
+++ crt1/gcrt1.S	12 May 2006 21:25:52 -0000
@@ -141,24 +141,29 @@
 	.set	__heap_end, 0
 
 	.section .init2,"ax",@progbits
 	clr	__zero_reg__
 	out	_SFR_IO_ADDR(SREG), __zero_reg__
 	ldi	r28,lo8(__stack)
 #ifdef SPH
 	ldi	r29,hi8(__stack)
 	out	_SFR_IO_ADDR(SPH), r29
 #endif
 	out	_SFR_IO_ADDR(SPL), r28
 
+#ifdef __AVR_3_BYTE_PC__
+        ldi  r16,hh8(pm(__vectors))
+	out	_SFR_IO_ADDR(EIND),r16
+#endif
+
 #if BIG_CODE
 	/* Only for >64K devices with RAMPZ, replaces the default code
 	   provided by libgcc.S which is only linked in if necessary.  */
 
 	.section .init4,"ax",@progbits
 	.global __do_copy_data
 __do_copy_data:
 	ldi	r17, hi8(__data_end)
 	ldi	r26, lo8(__data_start)
 	ldi	r27, hi8(__data_start)
 	ldi	r30, lo8(__data_load_start)
 	ldi	r31, hi8(__data_load_start)
@@ -187,15 +192,16 @@
 	brcs	.L__do_copy_data_carry
 #endif
 .L__do_copy_data_start:
 	cpi	r26, lo8(__data_end)
 	cpc	r27, r17
 	brne	.L__do_copy_data_loop
 #endif /* BIG_CODE */
 
 	.set	__stack, RAMEND
 #endif /* !__AVR_ASM_ONLY__ */
 
 	.section .init9,"ax",@progbits
-	XJMP	main
+	XCALL   main
+	XJMP    exit
 ;	.endfunc
 
Index: devtools/gen-avr-lib-tree.sh
===================================================================
RCS file: /sources/avr-libc/avr-libc/devtools/gen-avr-lib-tree.sh,v
retrieving revision 1.18
diff -U12 -r1.18 gen-avr-lib-tree.sh
--- devtools/gen-avr-lib-tree.sh	22 Apr 2006 08:41:44 -0000	1.18
+++ devtools/gen-avr-lib-tree.sh	12 May 2006 21:25:53 -0000
@@ -204,31 +204,37 @@
 atmega1280:crtm1280.o:${DEV_DEFS}:${CFLAGS_SPEED}:${DEV_ASFLAGS};\
 atmega1281:crtm1281.o:${DEV_DEFS}:${CFLAGS_SPEED}:${DEV_ASFLAGS};\
 at90can32:crtcan32.o:${DEV_DEFS}:${CFLAGS_SPEED}:${DEV_ASFLAGS};\
 at90can64:crtcan64.o:${DEV_DEFS}:${CFLAGS_SPEED}:${DEV_ASFLAGS};\
 at90can128:crtcan128.o:${DEV_DEFS}:${CFLAGS_SPEED}:${DEV_ASFLAGS};\
 at90usb646:crtusb646.o:${DEV_DEFS}:${CFLAGS_SPEED}:${DEV_ASFLAGS};\
 at90usb647:crtusb647.o:${DEV_DEFS}:${CFLAGS_SPEED}:${DEV_ASFLAGS};\
 at90usb1286:crtusb1286.o:${DEV_DEFS}:${CFLAGS_SPEED}:${DEV_ASFLAGS};\
 at90usb1287:crtusb1287.o:${DEV_DEFS}:${CFLAGS_SPEED}:${DEV_ASFLAGS};\
 at94k:crtat94k.o:${DEV_DEFS}:${CFLAGS_SPEED}:${DEV_ASFLAGS}\
 "
 
+AVR6_DEV_INFO="\
+atmega2560:crtm2560.o:${DEV_DEFS}:${CFLAGS_SPEED}:${DEV_ASFLAGS};\
+atmega2561:crtm2561.o:${DEV_DEFS}:${CFLAGS_SPEED}:${DEV_ASFLAGS}\
+"
+
 LIB_DEFS="-D__COMPILING_AVR_LIBC__"
 
 AVR_ARH_INFO="\
 avr2:AVR12_DEV_INFO:${LIB_DEFS}:${CFLAGS_SPACE}:${DEV_ASFLAGS};\
 avr3:AVR3_DEV_INFO:${LIB_DEFS}:${CFLAGS_SPEED}:${ASFLAGS_SPEED};\
 avr4:AVR4_DEV_INFO:${LIB_DEFS}:${CFLAGS_SPACE}:${DEV_ASFLAGS};\
-avr5:AVR5_DEV_INFO:${LIB_DEFS}:${CFLAGS_SPEED}:${ASFLAGS_SPEED}\
+avr5:AVR5_DEV_INFO:${LIB_DEFS}:${CFLAGS_SPEED}:${ASFLAGS_SPEED};\
+avr6:AVR6_DEV_INFO:${LIB_DEFS}:${CFLAGS_SPEED}:${ASFLAGS_SPEED}\
 "
 
 echo "Generating source directories:"
 
 IFS=';'
 ARH_SUBDIRS="SUBDIRS ="
 
 for ath_lib in $AVR_ARH_INFO
 do
 	arh=$(echo $ath_lib | awk -F: '{print $1}')
 	dev_info=$(echo $ath_lib | awk -F: '{print $2}')
 	lib_defs=$(echo $ath_lib | awk -F: '{print $3}')
Index: include/avr/io.h
===================================================================
RCS file: /sources/avr-libc/avr-libc/include/avr/io.h,v
retrieving revision 1.32
diff -U12 -r1.32 io.h
--- include/avr/io.h	22 Apr 2006 08:41:44 -0000	1.32
+++ include/avr/io.h	12 May 2006 21:25:54 -0000
@@ -175,24 +175,28 @@
 #elif defined (__AVR_AT43USB355__)
 #  include <avr/io43u35x.h>
 #elif defined (__AVR_AT76C711__)
 #  include <avr/io76c711.h>
 #elif defined (__AVR_AT86RF401__)
 #  include <avr/io86r401.h>
 #elif defined (__AVR_AT90PWM2__)
 #  include <avr/io90pwmx.h>
 #elif defined (__AVR_AT90PWM3__)
 #  include <avr/io90pwmx.h>
 #elif defined (__AVR_ATmega128__)
 #  include <avr/iom128.h>
+#elif defined (__AVR_ATmega2561__)
+#  include <avr/iom2561.h>
+#elif defined (__AVR_ATmega2560__)
+#  include <avr/iom2560.h>
 #elif defined (__AVR_ATmega1280__)
 #  include <avr/iom1280.h>
 #elif defined (__AVR_ATmega1281__)
 #  include <avr/iom1281.h>
 #elif defined (__AVR_AT90CAN32__)
 #  include <avr/iocan32.h>
 #elif defined (__AVR_AT90CAN64__)
 #  include <avr/iocan64.h>
 #elif defined (__AVR_AT90CAN128__)
 #  include <avr/iocan128.h>
 #elif defined (__AVR_AT90USB646__)
 #  include <avr/iousb646.h>
Index: libgcc.S
===================================================================
--- libgcc.S	(revision 113395)
+++ libgcc.S	(working copy)
@@ -593,7 +593,12 @@
 	out	__SP_H__,r29
 	out	__SREG__,__tmp_reg__
 	out	__SP_L__,r28
+#if defined (__AVR_3_BYTE_PC__)
+	eijmp
+#else
 	ijmp
+#endif
+
 .endfunc
 #endif /* defined (L_prologue) */
 
@@ -672,13 +677,22 @@
 	lpm	__tmp_reg__, Z+
 	lpm	r31, Z
 	mov	r30, __tmp_reg__
+
+#if defined (__AVR_3_BYTE_PC__)
+	eijmp
+#else
 	ijmp
+#endif
+
 #else
 	lpm
 	adiw	r30, 1
 	push	r0
 	lpm
 	push	r0
+#if defined (__AVR_3_BYTE_PC__)
+        push    __zero_reg__
+#endif
 	ret
 #endif
 	.endfunc
Index: avr.md
===================================================================
--- avr.md	(revision 113395)
+++ avr.md	(working copy)
@@ -2101,22 +2101,47 @@
   "(register_operand (operands[0], HImode) || CONSTANT_P (operands[0]))"
   "*{
   if (which_alternative==0)
-     return \"icall\";
+    {
+      if (AVR_3_BYTE_PC)
+        return \"eicall\";
+      else
+        return \"icall\";
+    }
   else if (which_alternative==1)
     {
       if (AVR_ENHANCED)
-	return (AS2 (movw, r30, %0) CR_TAB
-		\"icall\");
+        {
+          if (AVR_3_BYTE_PC)
+            return (AS2 (movw, r30, %0) CR_TAB
+                    \"eicall\");
+          else
+            return (AS2 (movw, r30, %0) CR_TAB
+                    \"icall\");
+        }
       else
-	return (AS2 (mov, r30, %A0) CR_TAB
-		AS2 (mov, r31, %B0) CR_TAB
-		\"icall\");
+        {
+          if (AVR_3_BYTE_PC)
+	    return (AS2 (mov, r30, %A0) CR_TAB
+                    AS2 (mov, r31, %B0) CR_TAB
+                    \"eicall\");
+          else
+	    return (AS2 (mov, r30, %A0) CR_TAB
+                    AS2 (mov, r31, %B0) CR_TAB
+                    \"icall\");
+        }
     }
   else if (which_alternative==2)
     return AS1(%~call,%c0);
-  return (AS2 (ldi,r30,lo8(%0)) CR_TAB
-          AS2 (ldi,r31,hi8(%0)) CR_TAB
-          \"icall\");
+
+
+  if (AVR_3_BYTE_PC)
+    return (AS2 (ldi,r30,lo8(%0)) CR_TAB
+            AS2 (ldi,r31,hi8(%0)) CR_TAB
+            \"eicall\");
+  else
+    return (AS2 (ldi,r30,lo8(%0)) CR_TAB
+            AS2 (ldi,r31,hi8(%0)) CR_TAB
+            \"icall\");
 }"
   [(set_attr "cc" "clobber,clobber,clobber,clobber")
    (set_attr_alternative "length"
@@ -2138,22 +2163,46 @@
   "(register_operand (operands[0], VOIDmode) || CONSTANT_P (operands[0]))"
   "*{
   if (which_alternative==0)
-     return \"icall\";
+    {
+      if (AVR_3_BYTE_PC)
+        return \"eicall\";
+      else
+        return \"icall\";
+    }
   else if (which_alternative==1)
     {
       if (AVR_ENHANCED)
-	return (AS2 (movw, r30, %1) CR_TAB
-		\"icall\");
+        {
+          if (AVR_3_BYTE_PC)
+            return (AS2 (movw, r30, %1) CR_TAB
+                    \"eicall\");
+          else
+            return (AS2 (movw, r30, %1) CR_TAB
+                    \"icall\");
+        }
       else
-	return (AS2 (mov, r30, %A1) CR_TAB
-		AS2 (mov, r31, %B1) CR_TAB
-		\"icall\");
+        {
+          if (AVR_3_BYTE_PC)
+            return (AS2 (mov, r30, %A1) CR_TAB
+                    AS2 (mov, r31, %B1) CR_TAB
+                    \"eicall\");
+          else
+            return (AS2 (mov, r30, %A1) CR_TAB
+                    AS2 (mov, r31, %B1) CR_TAB
+                    \"icall\");
+        }
     }
   else if (which_alternative==2)
     return AS1(%~call,%c1);
-  return (AS2 (ldi, r30, lo8(%1)) CR_TAB
-          AS2 (ldi, r31, hi8(%1)) CR_TAB
-          \"icall\");
+
+  if (AVR_3_BYTE_PC)
+    return (AS2 (ldi, r30, lo8(%1)) CR_TAB
+            AS2 (ldi, r31, hi8(%1)) CR_TAB
+            \"eicall\");
+  else
+    return (AS2 (ldi, r30, lo8(%1)) CR_TAB
+            AS2 (ldi, r31, hi8(%1)) CR_TAB
+            \"icall\");
 }"
   [(set_attr "cc" "clobber,clobber,clobber,clobber")
    (set_attr_alternative "length"
@@ -2183,13 +2232,20 @@
 ; indirect jump
 (define_insn "indirect_jump"
   [(set (pc) (match_operand:HI 0 "register_operand" "!z,*r"))]
-  ""
+  "AVR_2_BYTE_PC"
   "@
 	ijmp
 	push %A0\;push %B0\;ret"
   [(set_attr "length" "1,3")
    (set_attr "cc" "none,none")])
 
+(define_insn "*indirect_jump_avr6"
+  [(set (pc) (match_operand:HI 0 "register_operand" "z"))]
+  "AVR_3_BYTE_PC"
+  "eijmp"
+  [(set_attr "length" "1")
+   (set_attr "cc" "none")])
+
 ;; table jump
 
 ;; Table made from "rjmp" instructions for <=8K devices.
@@ -2198,7 +2254,7 @@
 			UNSPEC_INDEX_JMP))
    (use (label_ref (match_operand 1 "" "")))
    (clobber (match_dup 0))]
-  "!AVR_MEGA"
+  "(!AVR_MEGA) && (AVR_2_BYTE_PC)"
   "@
 	ijmp
 	push %A0\;push %B0\;ret"
@@ -2216,12 +2272,27 @@
   [(set_attr "length" "2")
    (set_attr "cc" "clobber")])
 
+(define_insn "*tablejump_avr6"
+  [(set (pc) (unspec:HI [(match_operand:HI 0 "register_operand" "z")]
+                        UNSPEC_INDEX_JMP))
+   (use (label_ref (match_operand 1 "" "")))
+   (clobber (match_dup 0))]
+  "AVR_MEGA && AVR_ENHANCED && AVR_3_BYTE_PC"
+  "lsl r30
+        rol r31
+        lpm __tmp_reg__,Z+
+        lpm r31,Z
+        mov r30,__tmp_reg__
+        eijmp"
+  [(set_attr "length" "6")
+   (set_attr "cc" "clobber")])
+
 (define_insn "*tablejump_enh"
   [(set (pc) (unspec:HI [(match_operand:HI 0 "register_operand" "z")]
 			UNSPEC_INDEX_JMP))
    (use (label_ref (match_operand 1 "" "")))
    (clobber (match_dup 0))]
-  "AVR_MEGA && AVR_ENHANCED"
+  "AVR_MEGA && AVR_ENHANCED && AVR_2_BYTE_PC"
   "lsl r30
 	rol r31
 	lpm __tmp_reg__,Z+
@@ -2236,7 +2307,7 @@
 			UNSPEC_INDEX_JMP))
    (use (label_ref (match_operand 1 "" "")))
    (clobber (match_dup 0))]
-  "AVR_MEGA"
+  "AVR_MEGA && AVR_2_BYTE_PC"
   "lsl r30
 	rol r31
 	lpm
Index: avr.c
===================================================================
--- avr.c	(revision 113395)
+++ avr.c	(working copy)
@@ -126,20 +126,26 @@
 /* Assembler only.  */
 int avr_asm_only_p = 0;
 
+/* Usually holds the value 2, but could be 3 for the larger devices with a
+   3-byte program counter.  */
+int avr_pc_size = 2;
+
 struct base_arch_s {
   int asm_only;
   int enhanced;
   int mega;
+  int three_byte_pc;
   const char *const macro;
 };
 
 static const struct base_arch_s avr_arch_types[] = {
-  { 1, 0, 0, NULL },  /* unknown device specified */
-  { 1, 0, 0, "__AVR_ARCH__=1" },
-  { 0, 0, 0, "__AVR_ARCH__=2" },
-  { 0, 0, 1, "__AVR_ARCH__=3" },
-  { 0, 1, 0, "__AVR_ARCH__=4" },
-  { 0, 1, 1, "__AVR_ARCH__=5" }
+  { 1, 0, 0, 0, NULL },  /* unknown device specified */
+  { 1, 0, 0, 0, "__AVR_ARCH__=1" },
+  { 0, 0, 0, 0, "__AVR_ARCH__=2" },
+  { 0, 0, 1, 0, "__AVR_ARCH__=3" },
+  { 0, 1, 0, 0, "__AVR_ARCH__=4" },
+  { 0, 1, 1, 0, "__AVR_ARCH__=5" },
+  { 0, 1, 1, 1, "__AVR_ARCH__=6" }
 };
 
 struct mcu_type_s {
@@ -209,6 +215,10 @@
   { "atmega128", 5, "__AVR_ATmega128__" },
   { "at90can128", 5, "__AVR_AT90CAN128__" },
   { "at94k",     5, "__AVR_AT94K__" },
+    /* 3-Byte PC */
+  { "avr6",      6, NULL },
+  { "atmega2560",     6, "__AVR_ATmega2560__" },
+  { "atmega2561",     6, "__AVR_ATmega2561__" },
     /* Assembler only.  */
   { "avr1",      1, NULL },
   { "at90s1200", 1, "__AVR_AT90S1200__" },
@@ -291,6 +301,11 @@
   avr_base_arch_macro = base->macro;
   avr_extra_arch_macro = t->macro;
 
+  if (base->three_byte_pc)
+    avr_pc_size = 3;
+  else
+    avr_pc_size = 2;
+
   if (optimize && !TARGET_NO_TABLEJUMP)
     avr_case_values_threshold = (!AVR_MEGA || TARGET_CALL_PROLOGUES) ? 8 : 17;
 
@@ -416,10 +431,10 @@
     return 0;
   else
     {
-      int offset = frame_pointer_needed ? 2 : 0;
+      int offset = frame_pointer_needed ? (avr_pc_size) : 0;
 
       offset += avr_regs_to_save (NULL);
-      return get_frame_size () + 2 + 1 + offset;
+      return get_frame_size () + (avr_pc_size) + 1 + offset;
     }
 }
 
@@ -603,7 +618,6 @@
   int reg;
   int interrupt_func_p;
   int signal_func_p;
-  int main_p;
   int live_seq;
   int minimize;
 
@@ -621,7 +635,6 @@
 
   interrupt_func_p = interrupt_function_p (current_function_decl);
   signal_func_p = signal_function_p (current_function_decl);
-  main_p = MAIN_NAME_P (DECL_NAME (current_function_decl));
   live_seq = sequent_regs_live ();
   minimize = (TARGET_CALL_PROLOGUES
 	      && !interrupt_func_p && !signal_func_p && live_seq);
@@ -641,25 +654,14 @@
 	       AS1 (clr,__zero_reg__)    "\n");
       prologue_size += 5;
     }
-  if (main_p)
+  if (minimize && (frame_pointer_needed || live_seq > 6)) 
     {
-      fprintf (file, ("\t" 
-		      AS1 (ldi,r28) ",lo8(%s - " HOST_WIDE_INT_PRINT_DEC ")" CR_TAB
-		      AS1 (ldi,r29) ",hi8(%s - " HOST_WIDE_INT_PRINT_DEC ")" CR_TAB
-		      AS2 (out,__SP_H__,r29)     CR_TAB
-		      AS2 (out,__SP_L__,r28) "\n"),
-	       avr_init_stack, size, avr_init_stack, size);
-      
-      prologue_size += 4;
-    }
-  else if (minimize && (frame_pointer_needed || live_seq > 6)) 
-    {
       fprintf (file, ("\t"
 		      AS1 (ldi, r26) ",lo8(" HOST_WIDE_INT_PRINT_DEC ")" CR_TAB
 		      AS1 (ldi, r27) ",hi8(" HOST_WIDE_INT_PRINT_DEC ")" CR_TAB), size, size);
 
-      fputs ((AS2 (ldi,r30,pm_lo8(1f)) CR_TAB
-	      AS2 (ldi,r31,pm_hi8(1f)) CR_TAB), file);
+      fputs ((AS2 (ldi,r30,lo8(gs(1f))) CR_TAB
+	      AS2 (ldi,r31,lo8(gs(1f))) CR_TAB), file);
       
       prologue_size += 4;
       
@@ -730,7 +732,6 @@
   int reg;
   int interrupt_func_p;
   int signal_func_p;
-  int main_p;
   int function_size;
   int live_seq;
   int minimize;
@@ -762,28 +763,13 @@
 
   interrupt_func_p = interrupt_function_p (current_function_decl);
   signal_func_p = signal_function_p (current_function_decl);
-  main_p = MAIN_NAME_P (DECL_NAME (current_function_decl));
+
   live_seq = sequent_regs_live ();
   minimize = (TARGET_CALL_PROLOGUES
 	      && !interrupt_func_p && !signal_func_p && live_seq);
   
-  if (main_p)
+  if (minimize && (frame_pointer_needed || live_seq > 4))
     {
-      /* Return value from main() is already in the correct registers
-	 (r25:r24) as the exit() argument.  */
-      if (AVR_MEGA)
-	{
-	  fputs ("\t" AS1 (jmp,exit) "\n", file);
-	  epilogue_size += 2;
-	}
-      else
-	{
-	  fputs ("\t" AS1 (rjmp,exit) "\n", file);
-	  ++epilogue_size;
-	}
-    }
-  else if (minimize && (frame_pointer_needed || live_seq > 4))
-    {
       fprintf (file, ("\t" AS2 (ldi, r30, %d) CR_TAB), live_seq);
       ++epilogue_size;
       if (frame_pointer_needed)
@@ -1040,7 +1026,7 @@
 	  && ((GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_FUNCTION_P (addr))
 	      || GET_CODE (addr) == LABEL_REF))
 	{
-	  fprintf (file, "pm(");
+	  fprintf (file, "gs(");
 	  output_addr_const (file,addr);
 	  fprintf (file ,")");
 	}
@@ -4461,7 +4447,7 @@
       && ((GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_FUNCTION_P (x))
 	  || GET_CODE (x) == LABEL_REF))
     {
-      fputs ("\t.word\tpm(", asm_out_file);
+      fputs ("\t.word\tgs(", asm_out_file);
       output_addr_const (asm_out_file, x);
       fputs (")\n", asm_out_file);
       return true;
@@ -5833,7 +5819,7 @@
 {
   switch_to_section (progmem_section);
   if (AVR_MEGA)
-    fprintf (stream, "\t.word pm(.L%d)\n", value);
+    fprintf (stream, "\t.word gs(.L%d)\n", value);
   else
     fprintf (stream, "\trjmp .L%d\n", value);
 
Index: t-avr
===================================================================
--- t-avr	(revision 113395)
+++ t-avr	(working copy)
@@ -37,8 +37,8 @@
 
 FPBIT = fp-bit.c
 
-MULTILIB_OPTIONS = mmcu=avr2/mmcu=avr3/mmcu=avr4/mmcu=avr5
-MULTILIB_DIRNAMES = avr2 avr3 avr4 avr5
+MULTILIB_OPTIONS = mmcu=avr2/mmcu=avr3/mmcu=avr4/mmcu=avr5/mmcu=avr6
+MULTILIB_DIRNAMES = avr2 avr3 avr4 avr5 avr6
 
 # The many avr2 matches are not listed here - this is the default.
 MULTILIB_MATCHES = \
@@ -56,7 +56,8 @@
 	mmcu?avr5=mmcu?atmega325 mmcu?avr5=mmcu?atmega3250 \
 	mmcu?avr5=mmcu?atmega64  mmcu?avr5=mmcu?atmega128 \
 	mmcu?avr5=mmcu?atmega645 mmcu?avr5=mmcu?atmega6450 \
-	mmcu?avr5=mmcu?at94k mmcu?avr5=mmcu?at90can128
+	mmcu?avr5=mmcu?at94k mmcu?avr5=mmcu?at90can128 \
+	mmcu?avr6=mmcu?atmega2560 mmcu?avr6=mmcu?atmega2561
 
 MULTILIB_EXCEPTIONS =
 
Index: avr.h
===================================================================
--- avr.h	(revision 113395)
+++ avr.h	(working copy)
@@ -33,6 +33,10 @@
 	builtin_define (avr_extra_arch_macro);	\
       if (avr_asm_only_p)			\
 	builtin_define ("__AVR_ASM_ONLY__");	\
+      if (avr_pc_size == 2)			\
+	builtin_define ("__AVR_2_BYTE_PC__");	\
+      if (avr_pc_size == 3)			\
+	builtin_define ("__AVR_3_BYTE_PC__");	\
       if (avr_enhanced_p)			\
 	builtin_define ("__AVR_ENHANCED__");	\
       if (avr_mega_p)				\
@@ -47,6 +51,8 @@
 extern int avr_mega_p;
 extern int avr_enhanced_p;
 extern int avr_asm_only_p;
+extern int avr_pc_size;
+
 #ifndef IN_LIBGCC2
 extern GTY(()) section *progmem_section;
 #endif
@@ -54,6 +60,9 @@
 #define AVR_MEGA (avr_mega_p && !TARGET_SHORT_CALLS)
 #define AVR_ENHANCED (avr_enhanced_p)
 
+#define AVR_3_BYTE_PC (avr_pc_size == 3)
+#define AVR_2_BYTE_PC (avr_pc_size == 2)
+
 #define TARGET_VERSION fprintf (stderr, " (GNU assembler syntax)");
 
 #define OVERRIDE_OPTIONS avr_override_options ()
@@ -708,12 +717,13 @@
 
 #define CPP_SPEC "%{posix:-D_POSIX_SOURCE}"
 
-#define CC1_SPEC "%{profile:-p}"
+#define CC1_SPEC "%{profile:-p} -fno-delete-null-pointer-checks"
 
 #define CC1PLUS_SPEC "%{!frtti:-fno-rtti} \
     %{!fenforce-eh-specs:-fno-enforce-eh-specs} \
-    %{!fexceptions:-fno-exceptions}"
-/* A C string constant that tells the GCC drvier program options to
+    %{!fexceptions:-fno-exceptions} \
+    -fno-delete-null-pointer-checks"
+/* A C string constant that tells the GCC driver program options to
    pass to `cc1plus'.  */
 
 #define ASM_SPEC "%{mmcu=*:-mmcu=%*}"
@@ -723,6 +733,8 @@
 %{mmcu=attiny22|mmcu=attiny26|mmcu=at90s2*|mmcu=at90s4*|mmcu=at90s8*|mmcu=at90c8*|mmcu=at86rf401|mmcu=attiny13|mmcu=attiny2313:-m avr2}\
 %{mmcu=atmega103|mmcu=atmega603|mmcu=at43*|mmcu=at76*:-m avr3}\
 %{mmcu=atmega8*|mmcu=atmega48:-m avr4}\
+%{mmcu=atmega2560|mmcu=atmega2561:-m avr6}\
+%{mmcu=atmega2560|mmcu=atmega2561: -Tdata 0x800200}\
 %{mmcu=atmega16*|mmcu=atmega32*|mmcu=atmega64*|mmcu=atmega128|mmcu=at90can128|mmcu=at94k:-m avr5}\
 %{mmcu=atmega325|mmcu=atmega3250|mmcu=atmega48|mmcu=atmega88|mmcu=atmega64|mmcu=atmega645|mmcu=atmega6450|mmcu=atmega128|mmcu=at90can128|mmcu=at90can128|mmcu=atmega162|mmcu=atmega165|mmcu=atmega168|mmcu=atmega169: -Tdata 0x800100} "
 
@@ -786,7 +798,9 @@
 %{mmcu=atmega6450:crtm6450.o%s} \
 %{mmcu=atmega128:crtm128.o%s} \
 %{mmcu=at90can128:crtcan128.o%s} \
-%{mmcu=at94k:crtat94k.o%s}"
+%{mmcu=at94k:crtat94k.o%s}\
+%{mmcu=atmega2560:crtm2560.o%s} \
+%{mmcu=atmega2561:crtm2561.o%s}"
 
 #define EXTRA_SPECS {"crt_binutils", CRT_BINUTILS_SPECS},
 
? avr_linkrelax_followup.patch
? avr_linkrelax_patch.rev5
? avr_linkrelax_patch.rev6
? genpatch
? unters
? unterschied2
? bfd/elf32-avr.h
? bfd/untersch
? bfd/doc/bfd.info
? bfd/doc/bfd.info-1
? bfd/doc/bfdint.info
? bfd/doc/bfdint.info-1
? bfd/doc/bfdint.info-2
? binutils/doc/binutils.info
? binutils/doc/binutils.info-1
? gas/config/tc-avr.h.alt
? gas/config/tc.avr.h
? gas/config/unters
? gas/doc/as.info
? gas/doc/as.info-1
? ld/ld.info
? ld/ld.info-1
? ld/stamp-h1.in
? ld/stamp-h2.in
? ld/emulparams/avr6.sh
? ld/emultempl/avrelf.em
Index: bfd/archures.c
===================================================================
RCS file: /cvs/src/src/bfd/archures.c,v
retrieving revision 1.116
diff -U62 -r1.116 archures.c
--- bfd/archures.c	6 Mar 2006 13:42:03 -0000	1.116
+++ bfd/archures.c	12 May 2006 21:43:04 -0000
@@ -275,124 +275,125 @@
 .#define bfd_mach_arm_iWMMXt	12
 .  bfd_arch_ns32k,     {* National Semiconductors ns32000 *}
 .  bfd_arch_w65,       {* WDC 65816 *}
 .  bfd_arch_tic30,     {* Texas Instruments TMS320C30 *}
 .  bfd_arch_tic4x,     {* Texas Instruments TMS320C3X/4X *}
 .#define bfd_mach_tic3x         30
 .#define bfd_mach_tic4x         40
 .  bfd_arch_tic54x,    {* Texas Instruments TMS320C54X *}
 .  bfd_arch_tic80,     {* TI TMS320c80 (MVP) *}
 .  bfd_arch_v850,      {* NEC V850 *}
 .#define bfd_mach_v850          1
 .#define bfd_mach_v850e 	'E'
 .#define bfd_mach_v850e1	'1'
 .  bfd_arch_arc,       {* ARC Cores *}
 .#define bfd_mach_arc_5         5
 .#define bfd_mach_arc_6         6
 .#define bfd_mach_arc_7         7
 .#define bfd_mach_arc_8         8
 . bfd_arch_m32c,     {* Renesas M16C/M32C.  *}
 .#define bfd_mach_m16c        0x75
 .#define bfd_mach_m32c        0x78
 .  bfd_arch_m32r,      {* Renesas M32R (formerly Mitsubishi M32R/D) *}
 .#define bfd_mach_m32r		1 {* For backwards compatibility.  *}
 .#define bfd_mach_m32rx		'x'
 .#define bfd_mach_m32r2		'2'
 .  bfd_arch_mn10200,   {* Matsushita MN10200 *}
 .  bfd_arch_mn10300,   {* Matsushita MN10300 *}
 .#define bfd_mach_mn10300		300
 .#define bfd_mach_am33		330
 .#define bfd_mach_am33_2	332
 .  bfd_arch_fr30,
 .#define bfd_mach_fr30		0x46523330
 .  bfd_arch_frv,
 .#define bfd_mach_frv		1
 .#define bfd_mach_frvsimple	2
 .#define bfd_mach_fr300		300
 .#define bfd_mach_fr400		400
 .#define bfd_mach_fr450		450
 .#define bfd_mach_frvtomcat	499	{* fr500 prototype *}
 .#define bfd_mach_fr500		500
 .#define bfd_mach_fr550		550
 .  bfd_arch_mcore,
 .  bfd_arch_ia64,      {* HP/Intel ia64 *}
 .#define bfd_mach_ia64_elf64	64
 .#define bfd_mach_ia64_elf32	32
 .  bfd_arch_ip2k,      {* Ubicom IP2K microcontrollers. *}
 .#define bfd_mach_ip2022	1
 .#define bfd_mach_ip2022ext	2
 . bfd_arch_iq2000,     {* Vitesse IQ2000.  *}
 .#define bfd_mach_iq2000        1
 .#define bfd_mach_iq10          2
 .  bfd_arch_mt,
 .#define bfd_mach_ms1           1
 .#define bfd_mach_mrisc2        2
 .#define bfd_mach_ms2           3
 .  bfd_arch_pj,
 .  bfd_arch_avr,       {* Atmel AVR microcontrollers.  *}
 .#define bfd_mach_avr1		1
 .#define bfd_mach_avr2		2
 .#define bfd_mach_avr3		3
 .#define bfd_mach_avr4		4
 .#define bfd_mach_avr5		5
+.#define bfd_mach_avr6		6
 .  bfd_arch_bfin,        {* ADI Blackfin *}
 .#define bfd_mach_bfin          1
 .  bfd_arch_cr16c,       {* National Semiconductor CompactRISC. *}
 .#define bfd_mach_cr16c		1
 .  bfd_arch_crx,       {*  National Semiconductor CRX.  *}
 .#define bfd_mach_crx		1
 .  bfd_arch_cris,      {* Axis CRIS *}
 .#define bfd_mach_cris_v0_v10	255
 .#define bfd_mach_cris_v32	32
 .#define bfd_mach_cris_v10_v32	1032
 .  bfd_arch_s390,      {* IBM s390 *}
 .#define bfd_mach_s390_31       31
 .#define bfd_mach_s390_64       64
 .  bfd_arch_openrisc,  {* OpenRISC *}
 .  bfd_arch_mmix,      {* Donald Knuth's educational processor.  *}
 .  bfd_arch_xstormy16,
 .#define bfd_mach_xstormy16	1
 .  bfd_arch_msp430,    {* Texas Instruments MSP430 architecture.  *}
 .#define bfd_mach_msp11          11
 .#define bfd_mach_msp110         110
 .#define bfd_mach_msp12          12
 .#define bfd_mach_msp13          13
 .#define bfd_mach_msp14          14
 .#define bfd_mach_msp15          15
 .#define bfd_mach_msp16          16
 .#define bfd_mach_msp21          21
 .#define bfd_mach_msp31          31
 .#define bfd_mach_msp32          32
 .#define bfd_mach_msp33          33
 .#define bfd_mach_msp41          41
 .#define bfd_mach_msp42          42
 .#define bfd_mach_msp43          43
 .#define bfd_mach_msp44          44
 .  bfd_arch_xc16x,     {* Infineon's XC16X Series.               *}
 .#define bfd_mach_xc16x         1
 .#define bfd_mach_xc16xl        2
 .#define bfd_mach_xc16xs         3
 .  bfd_arch_xtensa,    {* Tensilica's Xtensa cores.  *}
 .#define bfd_mach_xtensa	1
 .   bfd_arch_maxq,     {* Dallas MAXQ 10/20 *}
 .#define bfd_mach_maxq10    10
 .#define bfd_mach_maxq20    20
 .  bfd_arch_z80,
 .#define bfd_mach_z80strict      1 {* No undocumented opcodes.  *}
 .#define bfd_mach_z80            3 {* With ixl, ixh, iyl, and iyh.  *}
 .#define bfd_mach_z80full        7 {* All undocumented instructions.  *}
 .#define bfd_mach_r800           11 {* R800: successor with multiplication.  *}
 .  bfd_arch_last
 .  };
 */
 
 /*
 SUBSECTION
 	bfd_arch_info
 
 DESCRIPTION
 	This structure contains information on architectures for use
 	within BFD.
 
 .
 .typedef struct bfd_arch_info
 .{
Index: bfd/bfd-in2.h
===================================================================
RCS file: /cvs/src/src/bfd/bfd-in2.h,v
retrieving revision 1.390
diff -U62 -r1.390 bfd-in2.h
--- bfd/bfd-in2.h	11 May 2006 15:17:34 -0000	1.390
+++ bfd/bfd-in2.h	12 May 2006 21:43:07 -0000
@@ -1866,124 +1866,125 @@
 #define bfd_mach_arm_iWMMXt    12
   bfd_arch_ns32k,     /* National Semiconductors ns32000 */
   bfd_arch_w65,       /* WDC 65816 */
   bfd_arch_tic30,     /* Texas Instruments TMS320C30 */
   bfd_arch_tic4x,     /* Texas Instruments TMS320C3X/4X */
 #define bfd_mach_tic3x         30
 #define bfd_mach_tic4x         40
   bfd_arch_tic54x,    /* Texas Instruments TMS320C54X */
   bfd_arch_tic80,     /* TI TMS320c80 (MVP) */
   bfd_arch_v850,      /* NEC V850 */
 #define bfd_mach_v850          1
 #define bfd_mach_v850e         'E'
 #define bfd_mach_v850e1        '1'
   bfd_arch_arc,       /* ARC Cores */
 #define bfd_mach_arc_5         5
 #define bfd_mach_arc_6         6
 #define bfd_mach_arc_7         7
 #define bfd_mach_arc_8         8
  bfd_arch_m32c,     /* Renesas M16C/M32C.  */
 #define bfd_mach_m16c        0x75
 #define bfd_mach_m32c        0x78
   bfd_arch_m32r,      /* Renesas M32R (formerly Mitsubishi M32R/D) */
 #define bfd_mach_m32r          1 /* For backwards compatibility.  */
 #define bfd_mach_m32rx         'x'
 #define bfd_mach_m32r2         '2'
   bfd_arch_mn10200,   /* Matsushita MN10200 */
   bfd_arch_mn10300,   /* Matsushita MN10300 */
 #define bfd_mach_mn10300               300
 #define bfd_mach_am33          330
 #define bfd_mach_am33_2        332
   bfd_arch_fr30,
 #define bfd_mach_fr30          0x46523330
   bfd_arch_frv,
 #define bfd_mach_frv           1
 #define bfd_mach_frvsimple     2
 #define bfd_mach_fr300         300
 #define bfd_mach_fr400         400
 #define bfd_mach_fr450         450
 #define bfd_mach_frvtomcat     499     /* fr500 prototype */
 #define bfd_mach_fr500         500
 #define bfd_mach_fr550         550
   bfd_arch_mcore,
   bfd_arch_ia64,      /* HP/Intel ia64 */
 #define bfd_mach_ia64_elf64    64
 #define bfd_mach_ia64_elf32    32
   bfd_arch_ip2k,      /* Ubicom IP2K microcontrollers. */
 #define bfd_mach_ip2022        1
 #define bfd_mach_ip2022ext     2
  bfd_arch_iq2000,     /* Vitesse IQ2000.  */
 #define bfd_mach_iq2000        1
 #define bfd_mach_iq10          2
   bfd_arch_mt,
 #define bfd_mach_ms1           1
 #define bfd_mach_mrisc2        2
 #define bfd_mach_ms2           3
   bfd_arch_pj,
   bfd_arch_avr,       /* Atmel AVR microcontrollers.  */
 #define bfd_mach_avr1          1
 #define bfd_mach_avr2          2
 #define bfd_mach_avr3          3
 #define bfd_mach_avr4          4
 #define bfd_mach_avr5          5
+#define bfd_mach_avr6          6
   bfd_arch_bfin,        /* ADI Blackfin */
 #define bfd_mach_bfin          1
   bfd_arch_cr16c,       /* National Semiconductor CompactRISC. */
 #define bfd_mach_cr16c         1
   bfd_arch_crx,       /*  National Semiconductor CRX.  */
 #define bfd_mach_crx           1
   bfd_arch_cris,      /* Axis CRIS */
 #define bfd_mach_cris_v0_v10   255
 #define bfd_mach_cris_v32      32
 #define bfd_mach_cris_v10_v32  1032
   bfd_arch_s390,      /* IBM s390 */
 #define bfd_mach_s390_31       31
 #define bfd_mach_s390_64       64
   bfd_arch_openrisc,  /* OpenRISC */
   bfd_arch_mmix,      /* Donald Knuth's educational processor.  */
   bfd_arch_xstormy16,
 #define bfd_mach_xstormy16     1
   bfd_arch_msp430,    /* Texas Instruments MSP430 architecture.  */
 #define bfd_mach_msp11          11
 #define bfd_mach_msp110         110
 #define bfd_mach_msp12          12
 #define bfd_mach_msp13          13
 #define bfd_mach_msp14          14
 #define bfd_mach_msp15          15
 #define bfd_mach_msp16          16
 #define bfd_mach_msp21          21
 #define bfd_mach_msp31          31
 #define bfd_mach_msp32          32
 #define bfd_mach_msp33          33
 #define bfd_mach_msp41          41
 #define bfd_mach_msp42          42
 #define bfd_mach_msp43          43
 #define bfd_mach_msp44          44
   bfd_arch_xc16x,     /* Infineon's XC16X Series.               */
 #define bfd_mach_xc16x         1
 #define bfd_mach_xc16xl        2
 #define bfd_mach_xc16xs         3
   bfd_arch_xtensa,    /* Tensilica's Xtensa cores.  */
 #define bfd_mach_xtensa        1
    bfd_arch_maxq,     /* Dallas MAXQ 10/20 */
 #define bfd_mach_maxq10    10
 #define bfd_mach_maxq20    20
   bfd_arch_z80,
 #define bfd_mach_z80strict      1 /* No undocumented opcodes.  */
 #define bfd_mach_z80            3 /* With ixl, ixh, iyl, and iyh.  */
 #define bfd_mach_z80full        7 /* All undocumented instructions.  */
 #define bfd_mach_r800           11 /* R800: successor with multiplication.  */
   bfd_arch_last
   };
 
 typedef struct bfd_arch_info
 {
   int bits_per_word;
   int bits_per_address;
   int bits_per_byte;
   enum bfd_architecture arch;
   unsigned long mach;
   const char *arch_name;
   const char *printable_name;
   unsigned int section_align_power;
   /* TRUE if this is the default machine for the architecture.
      The default arch should be the first entry for an arch so that
@@ -3443,169 +3444,182 @@
   BFD_RELOC_MCORE_PCREL_32,
   BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2,
   BFD_RELOC_MCORE_RVA,
 
 /* These are relocations for the GETA instruction.  */
   BFD_RELOC_MMIX_GETA,
   BFD_RELOC_MMIX_GETA_1,
   BFD_RELOC_MMIX_GETA_2,
   BFD_RELOC_MMIX_GETA_3,
 
 /* These are relocations for a conditional branch instruction.  */
   BFD_RELOC_MMIX_CBRANCH,
   BFD_RELOC_MMIX_CBRANCH_J,
   BFD_RELOC_MMIX_CBRANCH_1,
   BFD_RELOC_MMIX_CBRANCH_2,
   BFD_RELOC_MMIX_CBRANCH_3,
 
 /* These are relocations for the PUSHJ instruction.  */
   BFD_RELOC_MMIX_PUSHJ,
   BFD_RELOC_MMIX_PUSHJ_1,
   BFD_RELOC_MMIX_PUSHJ_2,
   BFD_RELOC_MMIX_PUSHJ_3,
   BFD_RELOC_MMIX_PUSHJ_STUBBABLE,
 
 /* These are relocations for the JMP instruction.  */
   BFD_RELOC_MMIX_JMP,
   BFD_RELOC_MMIX_JMP_1,
   BFD_RELOC_MMIX_JMP_2,
   BFD_RELOC_MMIX_JMP_3,
 
 /* This is a relocation for a relative address as in a GETA instruction or
 a branch.  */
   BFD_RELOC_MMIX_ADDR19,
 
 /* This is a relocation for a relative address as in a JMP instruction.  */
   BFD_RELOC_MMIX_ADDR27,
 
 /* This is a relocation for an instruction field that may be a general
 register or a value 0..255.  */
   BFD_RELOC_MMIX_REG_OR_BYTE,
 
 /* This is a relocation for an instruction field that may be a general
 register.  */
   BFD_RELOC_MMIX_REG,
 
 /* This is a relocation for two instruction fields holding a register and
 an offset, the equivalent of the relocation.  */
   BFD_RELOC_MMIX_BASE_PLUS_OFFSET,
 
 /* This relocation is an assertion that the expression is not allocated as
 a global register.  It does not modify contents.  */
   BFD_RELOC_MMIX_LOCAL,
 
 /* This is a 16 bit reloc for the AVR that stores 8 bit pc relative
 short offset into 7 bits.  */
   BFD_RELOC_AVR_7_PCREL,
 
 /* This is a 16 bit reloc for the AVR that stores 13 bit pc relative
 short offset into 12 bits.  */
   BFD_RELOC_AVR_13_PCREL,
 
 /* This is a 16 bit reloc for the AVR that stores 17 bit value (usually
-program memory address) into 16 bits.  */
+program memory address) into 16 bits.  If the address ends up behind the
+17 bit boundary, the linker will add a trampoline below 128k.  */
   BFD_RELOC_AVR_16_PM,
 
 /* This is a 16 bit reloc for the AVR that stores 8 bit value (usually
 data memory address) into 8 bit immediate value of LDI insn.  */
   BFD_RELOC_AVR_LO8_LDI,
 
 /* This is a 16 bit reloc for the AVR that stores 8 bit value (high 8 bit
 of data memory address) into 8 bit immediate value of LDI insn.  */
   BFD_RELOC_AVR_HI8_LDI,
 
 /* This is a 16 bit reloc for the AVR that stores 8 bit value (most high 8 bit
 of program memory address) into 8 bit immediate value of LDI insn.  */
   BFD_RELOC_AVR_HH8_LDI,
 
 /* This is a 16 bit reloc for the AVR that stores 8 bit value (most high 8 bit
 of 32 bit value) into 8 bit immediate value of LDI insn.  */
   BFD_RELOC_AVR_MS8_LDI,
 
 /* This is a 16 bit reloc for the AVR that stores negated 8 bit value
 (usually data memory address) into 8 bit immediate value of SUBI insn.  */
   BFD_RELOC_AVR_LO8_LDI_NEG,
 
 /* This is a 16 bit reloc for the AVR that stores negated 8 bit value
 (high 8 bit of data memory address) into 8 bit immediate value of
 SUBI insn.  */
   BFD_RELOC_AVR_HI8_LDI_NEG,
 
 /* This is a 16 bit reloc for the AVR that stores negated 8 bit value
 (most high 8 bit of program memory address) into 8 bit immediate value
 of LDI or SUBI insn.  */
   BFD_RELOC_AVR_HH8_LDI_NEG,
 
 /* This is a 16 bit reloc for the AVR that stores negated 8 bit value (msb
 of 32 bit value) into 8 bit immediate value of LDI insn.  */
   BFD_RELOC_AVR_MS8_LDI_NEG,
 
 /* This is a 16 bit reloc for the AVR that stores 8 bit value (usually
 command address) into 8 bit immediate value of LDI insn.  */
   BFD_RELOC_AVR_LO8_LDI_PM,
 
+/* This is a 16 bit reloc for the AVR that stores 8 bit value (command
+address) into 8 bit immediate value of LDI insn.  If the address is
+located behind the 128k boundary, the linker inserts a jump stub for
+this reloc.  */
+  BFD_RELOC_AVR_LO8_LDI_GS,
+
 /* This is a 16 bit reloc for the AVR that stores 8 bit value (high 8 bit
 of command address) into 8 bit immediate value of LDI insn.  */
   BFD_RELOC_AVR_HI8_LDI_PM,
 
+/* This is a 16 bit reloc for the AVR that stores 8 bit value (command
+address) into 8 bit immediate value of LDI insn.  If the address is
+located behind the 128k boundary, the linker inserts a jump stub for
+this reloc.  */
+  BFD_RELOC_AVR_HI8_LDI_GS,
+
 /* This is a 16 bit reloc for the AVR that stores 8 bit value (most high 8 bit
 of command address) into 8 bit immediate value of LDI insn.  */
   BFD_RELOC_AVR_HH8_LDI_PM,
 
 /* This is a 16 bit reloc for the AVR that stores negated 8 bit value
 (usually command address) into 8 bit immediate value of SUBI insn.  */
   BFD_RELOC_AVR_LO8_LDI_PM_NEG,
 
 /* This is a 16 bit reloc for the AVR that stores negated 8 bit value
 (high 8 bit of 16 bit command address) into 8 bit immediate value
 of SUBI insn.  */
   BFD_RELOC_AVR_HI8_LDI_PM_NEG,
 
 /* This is a 16 bit reloc for the AVR that stores negated 8 bit value
 (high 6 bit of 22 bit command address) into 8 bit immediate
 value of SUBI insn.  */
   BFD_RELOC_AVR_HH8_LDI_PM_NEG,
 
 /* This is a 32 bit reloc for the AVR that stores 23 bit value
 into 22 bits.  */
   BFD_RELOC_AVR_CALL,
 
 /* This is a 16 bit reloc for the AVR that stores all needed bits
 for absolute addressing with ldi with overflow check to linktime  */
   BFD_RELOC_AVR_LDI,
 
 /* This is a 6 bit reloc for the AVR that stores offset for ldd/std
 instructions  */
   BFD_RELOC_AVR_6,
 
 /* This is a 6 bit reloc for the AVR that stores offset for adiw/sbiw
 instructions  */
   BFD_RELOC_AVR_6_ADIW,
 
 /* Direct 12 bit.  */
   BFD_RELOC_390_12,
 
 /* 12 bit GOT offset.  */
   BFD_RELOC_390_GOT12,
 
 /* 32 bit PC relative PLT address.  */
   BFD_RELOC_390_PLT32,
 
 /* Copy symbol at runtime.  */
   BFD_RELOC_390_COPY,
 
 /* Create GOT entry.  */
   BFD_RELOC_390_GLOB_DAT,
 
 /* Create PLT entry.  */
   BFD_RELOC_390_JMP_SLOT,
 
 /* Adjust by program base.  */
   BFD_RELOC_390_RELATIVE,
 
 /* 32 bit PC relative offset to GOT.  */
   BFD_RELOC_390_GOTPC,
 
 /* 16 bit GOT offset.  */
   BFD_RELOC_390_GOT16,
 
 /* PC relative 16 bit shifted by 1.  */
Index: bfd/cpu-avr.c
===================================================================
RCS file: /cvs/src/src/bfd/cpu-avr.c,v
retrieving revision 1.9
diff -U62 -r1.9 cpu-avr.c
--- bfd/cpu-avr.c	3 Mar 2006 15:54:23 -0000	1.9
+++ bfd/cpu-avr.c	12 May 2006 21:43:07 -0000
@@ -22,68 +22,71 @@
 #include "sysdep.h"
 #include "libbfd.h"
 
 /* This routine is provided two arch_infos and works out which AVR
    machine which would be compatible with both and returns a pointer
    to its info structure.  */
 
 static const bfd_arch_info_type *
 compatible (const bfd_arch_info_type * a,
 	    const bfd_arch_info_type * b)
 {
   /* If a & b are for different architectures we can do nothing.  */
   if (a->arch != b->arch)
     return NULL;
 
   /* Special case for ATmega[16]03 (avr:3) and ATmega83 (avr:4).  */
   if ((a->mach == bfd_mach_avr3 && b->mach == bfd_mach_avr4)
       || (a->mach == bfd_mach_avr4 && b->mach == bfd_mach_avr3))
     return NULL;
 
   /* So far all newer AVR architecture cores are supersets of previous
      cores.  */
   if (a->mach <= b->mach)
     return b;
 
   if (a->mach >= b->mach)
     return a;
 
   /* Never reached!  */
   return NULL;
 }
 
 #define N(addr_bits, machine, print, default, next)		\
 {								\
   8,				/* 8 bits in a word.  */	\
   addr_bits,			/* bits in an address.  */	\
   8,				/* 8 bits in a byte.  */	\
   bfd_arch_avr,							\
   machine,			/* Machine number.  */		\
   "avr",			/* Architecture name.   */	\
   print,			/* Printable name.  */		\
   1,				/* Section align power.  */	\
   default,			/* Is this the default ?  */	\
   compatible,							\
   bfd_default_scan,						\
   next								\
 }
 
 static const bfd_arch_info_type arch_info_struct[] =
 {
   /* AT90S1200, ATtiny1x, ATtiny28.  */
   N (16, bfd_mach_avr1, "avr:1", FALSE, & arch_info_struct[1]),
 
   /* AT90S2xxx, AT90S4xxx, AT90S8xxx, ATtiny22.  */
   N (16, bfd_mach_avr2, "avr:2", FALSE, & arch_info_struct[2]),
 
   /* ATmega103, ATmega603.  */
   N (22, bfd_mach_avr3, "avr:3", FALSE, & arch_info_struct[3]),
 
   /* ATmega83, ATmega85.  */
   N (16, bfd_mach_avr4, "avr:4", FALSE, & arch_info_struct[4]),
 
-  /* ATmega161, ATmega163, ATmega32, AT94K.  */
-  N (22, bfd_mach_avr5, "avr:5", FALSE, NULL)
+  /* ATmegaXXX.  */
+  N (22, bfd_mach_avr5, "avr:5", FALSE, & arch_info_struct[5]),
+
+  /* ATmega256x.  */
+  N (22, bfd_mach_avr6, "avr:6", FALSE, NULL)
 };
 
 const bfd_arch_info_type bfd_avr_arch =
   N (16, bfd_mach_avr2, "avr", TRUE, & arch_info_struct[0]);
Index: bfd/elf32-avr.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-avr.c,v
retrieving revision 1.25
diff -U62 -r1.25 elf32-avr.c
--- bfd/elf32-avr.c	5 May 2006 17:46:47 -0000	1.25
+++ bfd/elf32-avr.c	12 May 2006 21:43:09 -0000
@@ -1,641 +1,880 @@
 /* AVR-specific support for 32-bit ELF
    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2006
    Free Software Foundation, Inc.
    Contributed by Denis Chertykov <denisc@overta.ru>
 
    This file is part of BFD, the Binary File Descriptor library.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street - Fifth Floor,
    Boston, MA 02110-1301, USA.  */
 
 #include "bfd.h"
 #include "sysdep.h"
 #include "libbfd.h"
 #include "elf-bfd.h"
 #include "elf/avr.h"
+#include "elf32-avr.h"
 
+/* Enable debugging printout at stdout with this variable.  */
+bfd_boolean debug_relax = FALSE;
+
+/* Enable debugging printout at stdout with this variable.  */
+bfd_boolean debug_stubs = FALSE;
+
+/* Hash table initialization and handling.  Code is taken from the hppa port
+   and adapted to the needs of avr.  */
+
+/* We use two hash tables to hold information for linking avr objects.
+
+   The first is the elf32_avr_link_hash_tablse which is derived from the
+   stanard ELF linker hash table.  We use this as a place to attach the other
+   hash table and some static information.
+
+   The second is the stub hash table which is derived from the base BFD
+   hash table.  The stub hash table holds the information on the linker
+   stubs.  */ 
+
+struct elf32_avr_stub_hash_entry {
+  /* Base hash table entry structure.  */
+  struct bfd_hash_entry bh_root;
+
+  /* Offset within stub_sec of the beginning of this stub.  */
+  bfd_vma stub_offset;
+
+  /* Given the symbol's value and its section we can determine its final
+     value when building the stubs (so the stub knows where to jump).  */
+  bfd_vma target_value;
+
+  /* This way we could mark stubs to be no longer necessary.  */
+  bfd_boolean is_actually_needed;
+};
+
+struct elf32_avr_link_hash_table {
+
+  /* The main hash table.  */
+  struct elf_link_hash_table etab;
+
+  /* The stub hash table.  */
+  struct bfd_hash_table bstab;
+
+  bfd_boolean no_stubs;
+
+  /* Linker stub bfd.  */
+  bfd *stub_bfd;
+  /* The stub section.  */
+  asection *stub_sec;
+
+  /* Usually 0, unless we are generating code for a bootloader.  Will
+     be initialized by elf32_avr_size_stubs to the vma offset of the
+     output section associated with the stub section.  */
+  bfd_vma vector_base;
+
+  /* Assorted information used by elf32_avr_size_stubs.  */
+  unsigned int bfd_count;
+  int top_index;
+  asection **input_list;
+  Elf_Internal_Sym **all_local_syms;
+
+  /* Tables for mapping vma beyond the 128k boundary to the address of the
+     corresponding stub.  (AMT) 
+     "amt_max_entry_cnt" reflects the number of entries that memory is allocated
+     for in the "amt_stub_offsets" and "amt_destination_addr" arrays. 
+     "amt_entry_cnt" informs how many of these entries actually contain
+     useful data.  */
+  unsigned int amt_entry_cnt;
+  unsigned int amt_max_entry_cnt;
+  bfd_vma *amt_stub_offsets;
+  bfd_vma *amt_destination_addr;
+};
+
+/* Various hash macros and functions.  */
+#define avr_link_hash_table(p) \
+  ((struct elf32_avr_link_hash_table *) ((p)->hash))
+
+#define avr_stub_hash_entry(ent) \
+  ((struct elf32_avr_stub_hash_entry *)(ent))
+
+#define avr_stub_hash_lookup(table, string, create, copy) \
+  ((struct elf32_avr_stub_hash_entry *) \
+   bfd_hash_lookup ((table), (string), (create), (copy)))   
+   
+/* Initialize an entry in the stub hash table.  */
+
+static struct bfd_hash_entry * 
+stub_hash_newfunc (struct bfd_hash_entry *entry,
+                   struct bfd_hash_table *table,
+                   const char *string)
+{
+  /* Allocate the structure if it has not already been allocated by a
+     subclass.  */
+  if (entry == NULL)
+    {
+      entry = bfd_hash_allocate (table,
+                                 sizeof (struct elf32_avr_stub_hash_entry));
+      if (entry == NULL)
+        return entry;
+    }
+
+  /* Call the allocation method of the superclass.  */
+  entry = bfd_hash_newfunc (entry, table, string);
+  if (entry != NULL)
+    {
+      struct elf32_avr_stub_hash_entry *hsh;
+
+      /* Initialize the local fields.  */
+      hsh = avr_stub_hash_entry (entry);
+      hsh->stub_offset = 0;
+      hsh->target_value = 0;
+    }
+
+  return entry;
+}
+
+/* Create the derived linker hash table.  The AVR ELF port uses the derived
+   hash table to keep information specific to the AVR ELF linker (without
+   using static variables).  */
+
+static struct bfd_link_hash_table *
+elf32_avr_link_hash_table_create (bfd *abfd)
+{
+  struct elf32_avr_link_hash_table *htab;
+  bfd_size_type amt = sizeof (*htab);
+
+  htab = (struct elf32_avr_link_hash_table *) bfd_malloc (amt);
+  if (htab == NULL)
+    return NULL;
+
+  if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd, 
+                                      _bfd_elf_link_hash_newfunc,
+                                      sizeof (struct elf_link_hash_entry)))
+    {
+      free (htab);
+      return NULL;
+    }
+
+  /* Init the stub hash table too.  */
+  if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
+                            sizeof (struct elf32_avr_stub_hash_entry)))
+    return NULL;
+
+  htab->stub_bfd = NULL;
+  htab->stub_sec = NULL;
+
+  /* Initialize the address mapping table.  */
+  htab->amt_stub_offsets = NULL;
+  htab->amt_destination_addr = NULL;
+  htab->amt_entry_cnt = 0;
+  htab->amt_max_entry_cnt = 0;
+
+  return &htab->etab.root;
+}
+
+/* Free the derived linker hash table.  */
+
+static void
+elf32_avr_link_hash_table_free (struct bfd_link_hash_table *btab)
+{
+  struct elf32_avr_link_hash_table *htab
+    = (struct elf32_avr_link_hash_table *) btab;
+
+  /* Free the address mapping table.  */
+  if (htab->amt_stub_offsets != NULL)
+    free (htab->amt_stub_offsets);
+  if (htab->amt_destination_addr != NULL)
+    free (htab->amt_destination_addr);
+
+  bfd_hash_table_free (&htab->bstab);
+  _bfd_generic_link_hash_table_free (btab);
+}
+
+/* Returns the address of the corresponding stub if there is one.  
+   Returns otherwise an address higher or equal to 0x020000.  */
+static bfd_vma
+avr_get_stub_addr (bfd_vma srel, 
+                   struct elf32_avr_link_hash_table *htab);
+
+
+/* OK, at this point we are through with handling of the hash
+   tables.  */   
+   
+   
 static reloc_howto_type elf_avr_howto_table[] =
 {
   HOWTO (R_AVR_NONE,		/* type */
 	 0,			/* rightshift */
 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
 	 32,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_bitfield, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_NONE",		/* name */
 	 FALSE,			/* partial_inplace */
 	 0,			/* src_mask */
 	 0,			/* dst_mask */
 	 FALSE),		/* pcrel_offset */
 
   HOWTO (R_AVR_32,		/* type */
 	 0,			/* rightshift */
 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
 	 32,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_bitfield, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_32",		/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffffffff,		/* src_mask */
 	 0xffffffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
 
   /* A 7 bit PC relative relocation.  */
   HOWTO (R_AVR_7_PCREL,		/* type */
 	 1,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 7,			/* bitsize */
 	 TRUE,			/* pc_relative */
 	 3,			/* bitpos */
 	 complain_overflow_bitfield, /* complain_on_overflow */
 	 bfd_elf_generic_reloc, /* special_function */
 	 "R_AVR_7_PCREL",	/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 TRUE),			/* pcrel_offset */
 
   /* A 13 bit PC relative relocation.  */
   HOWTO (R_AVR_13_PCREL,	/* type */
 	 1,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 13,			/* bitsize */
 	 TRUE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_bitfield, /* complain_on_overflow */
 	 bfd_elf_generic_reloc, /* special_function */
 	 "R_AVR_13_PCREL",	/* name */
 	 FALSE,			/* partial_inplace */
 	 0xfff,			/* src_mask */
 	 0xfff,			/* dst_mask */
 	 TRUE),			/* pcrel_offset */
 
   /* A 16 bit absolute relocation.  */
   HOWTO (R_AVR_16,		/* type */
 	 0,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 16,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_16",		/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
-
-  /* A 16 bit absolute relocation for command address.  */
+  /* A 16 bit absolute relocation for command address.  Will be
+     changed when linker stubs are needed.  */
   HOWTO (R_AVR_16_PM,		/* type */
 	 1,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 16,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_bitfield, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_16_PM",		/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
   /* A low 8 bit absolute relocation of 16 bit address.
      For LDI command.  */
   HOWTO (R_AVR_LO8_LDI,		/* type */
 	 0,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 8,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_LO8_LDI",	/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
   /* A high 8 bit absolute relocation of 16 bit address.
      For LDI command.  */
   HOWTO (R_AVR_HI8_LDI,		/* type */
 	 8,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 8,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_HI8_LDI",	/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
   /* A high 6 bit absolute relocation of 22 bit address.
      For LDI command.  As well second most significant 8 bit value of
      a 32 bit link-time constant.  */
   HOWTO (R_AVR_HH8_LDI,		/* type */
 	 16,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 8,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_HH8_LDI",	/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
   /* A negative low 8 bit absolute relocation of 16 bit address.
      For LDI command.  */
   HOWTO (R_AVR_LO8_LDI_NEG,	/* type */
 	 0,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 8,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_LO8_LDI_NEG",	/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
   /* A negative high 8 bit absolute relocation of 16 bit address.
      For LDI command.  */
   HOWTO (R_AVR_HI8_LDI_NEG,	/* type */
 	 8,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 8,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_HI8_LDI_NEG",	/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
   /* A negative high 6 bit absolute relocation of 22 bit address.
      For LDI command.  */
   HOWTO (R_AVR_HH8_LDI_NEG,	/* type */
 	 16,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 8,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_HH8_LDI_NEG",	/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
   /* A low 8 bit absolute relocation of 24 bit program memory address.
-     For LDI command.  */
+     For LDI command.  Will not be changed when linker stubs are needed. */
   HOWTO (R_AVR_LO8_LDI_PM,	/* type */
 	 1,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 8,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_LO8_LDI_PM",	/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
-  /* A high 8 bit absolute relocation of 16 bit program memory address.
-     For LDI command.  */
+  /* A low 8 bit absolute relocation of 24 bit program memory address.
+     For LDI command.  Will not be changed when linker stubs are needed. */
   HOWTO (R_AVR_HI8_LDI_PM,	/* type */
 	 9,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 8,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_HI8_LDI_PM",	/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
-  /* A high 8 bit absolute relocation of 24 bit program memory address.
-     For LDI command.  */
+  /* A low 8 bit absolute relocation of 24 bit program memory address.
+     For LDI command.  Will not be changed when linker stubs are needed. */
   HOWTO (R_AVR_HH8_LDI_PM,	/* type */
 	 17,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 8,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_HH8_LDI_PM",	/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
-  /* A low 8 bit absolute relocation of a negative 24 bit
-     program memory address.  For LDI command.  */
+  /* A low 8 bit absolute relocation of 24 bit program memory address.
+     For LDI command.  Will not be changed when linker stubs are needed. */
   HOWTO (R_AVR_LO8_LDI_PM_NEG,	/* type */
 	 1,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 8,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_LO8_LDI_PM_NEG", /* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
-  /* A high 8 bit absolute relocation of a negative 16 bit
-     program memory address.  For LDI command.  */
+  /* A low 8 bit absolute relocation of 24 bit program memory address.
+     For LDI command.  Will not be changed when linker stubs are needed. */
   HOWTO (R_AVR_HI8_LDI_PM_NEG,	/* type */
 	 9,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 8,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_HI8_LDI_PM_NEG", /* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
-  /* A high 8 bit absolute relocation of a negative 24 bit
-     program memory address.  For LDI command.  */
+  /* A low 8 bit absolute relocation of 24 bit program memory address.
+     For LDI command.  Will not be changed when linker stubs are needed. */
   HOWTO (R_AVR_HH8_LDI_PM_NEG,	/* type */
 	 17,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 8,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_HH8_LDI_PM_NEG", /* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
   /* Relocation for CALL command in ATmega.  */
   HOWTO (R_AVR_CALL,		/* type */
 	 1,			/* rightshift */
 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
 	 23,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont,/* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_CALL",		/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffffffff,		/* src_mask */
 	 0xffffffff,		/* dst_mask */
 	 FALSE),			/* pcrel_offset */
   /* A 16 bit absolute relocation of 16 bit address.
      For LDI command.  */
   HOWTO (R_AVR_LDI,		/* type */
 	 0,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 16,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont,/* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_LDI",		/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
   /* A 6 bit absolute relocation of 6 bit offset.
      For ldd/sdd command.  */
   HOWTO (R_AVR_6,		/* type */
 	 0,			/* rightshift */
 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
 	 6,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont,/* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_6",		/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
   /* A 6 bit absolute relocation of 6 bit offset.
      For sbiw/adiw command.  */
   HOWTO (R_AVR_6_ADIW,		/* type */
 	 0,			/* rightshift */
 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
 	 6,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont,/* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_6_ADIW",	/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
   /* Most significant 8 bit value of a 32 bit link-time constant.  */
   HOWTO (R_AVR_MS8_LDI,		/* type */
 	 24,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 8,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_MS8_LDI",	/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
 	 FALSE),		/* pcrel_offset */
   /* Negative most significant 8 bit value of a 32 bit link-time constant.  */
   HOWTO (R_AVR_MS8_LDI_NEG,	/* type */
 	 24,			/* rightshift */
 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
 	 8,			/* bitsize */
 	 FALSE,			/* pc_relative */
 	 0,			/* bitpos */
 	 complain_overflow_dont, /* complain_on_overflow */
 	 bfd_elf_generic_reloc,	/* special_function */
 	 "R_AVR_MS8_LDI_NEG",	/* name */
 	 FALSE,			/* partial_inplace */
 	 0xffff,		/* src_mask */
 	 0xffff,		/* dst_mask */
-	 FALSE) 		/* pcrel_offset */
+	 FALSE), 		/* pcrel_offset */
+  /* A low 8 bit absolute relocation of 24 bit program memory address.
+     For LDI command.  Will be changed when linker stubs are needed. */
+  HOWTO (R_AVR_LO8_LDI_GS,      /* type */
+         1,                     /* rightshift */
+         1,                     /* size (0 = byte, 1 = short, 2 = long) */
+         8,                     /* bitsize */
+         FALSE,                 /* pc_relative */
+         0,                     /* bitpos */
+         complain_overflow_dont, /* complain_on_overflow */
+         bfd_elf_generic_reloc, /* special_function */
+         "R_AVR_LO8_LDI_GS",    /* name */
+         FALSE,                 /* partial_inplace */
+         0xffff,                /* src_mask */
+         0xffff,                /* dst_mask */
+         FALSE),                /* pcrel_offset */
+  /* A low 8 bit absolute relocation of 24 bit program memory address.
+     For LDI command.  Will be changed when linker stubs are needed. */
+  HOWTO (R_AVR_HI8_LDI_GS,      /* type */ 
+         9,                     /* rightshift */
+         1,                     /* size (0 = byte, 1 = short, 2 = long) */
+         8,                     /* bitsize */
+         FALSE,                 /* pc_relative */
+         0,                     /* bitpos */
+         complain_overflow_dont, /* complain_on_overflow */
+         bfd_elf_generic_reloc, /* special_function */
+         "R_AVR_HI8_LDI_GS",    /* name */
+         FALSE,                 /* partial_inplace */
+         0xffff,                /* src_mask */
+         0xffff,                /* dst_mask */
+         FALSE)                 /* pcrel_offset */
 };
 
 /* Map BFD reloc types to AVR ELF reloc types.  */
 
 struct avr_reloc_map
 {
   bfd_reloc_code_real_type bfd_reloc_val;
   unsigned int elf_reloc_val;
 };
 
  static const struct avr_reloc_map avr_reloc_map[] =
 {
   { BFD_RELOC_NONE,                 R_AVR_NONE },
   { BFD_RELOC_32,                   R_AVR_32 },
   { BFD_RELOC_AVR_7_PCREL,          R_AVR_7_PCREL },
   { BFD_RELOC_AVR_13_PCREL,         R_AVR_13_PCREL },
   { BFD_RELOC_16,                   R_AVR_16 },
   { BFD_RELOC_AVR_16_PM,            R_AVR_16_PM },
   { BFD_RELOC_AVR_LO8_LDI,          R_AVR_LO8_LDI},
   { BFD_RELOC_AVR_HI8_LDI,          R_AVR_HI8_LDI },
   { BFD_RELOC_AVR_HH8_LDI,          R_AVR_HH8_LDI },
   { BFD_RELOC_AVR_MS8_LDI,          R_AVR_MS8_LDI },
   { BFD_RELOC_AVR_LO8_LDI_NEG,      R_AVR_LO8_LDI_NEG },
   { BFD_RELOC_AVR_HI8_LDI_NEG,      R_AVR_HI8_LDI_NEG },
   { BFD_RELOC_AVR_HH8_LDI_NEG,      R_AVR_HH8_LDI_NEG },
   { BFD_RELOC_AVR_MS8_LDI_NEG,      R_AVR_MS8_LDI_NEG },
   { BFD_RELOC_AVR_LO8_LDI_PM,       R_AVR_LO8_LDI_PM },
+  { BFD_RELOC_AVR_LO8_LDI_GS,       R_AVR_LO8_LDI_GS },
   { BFD_RELOC_AVR_HI8_LDI_PM,       R_AVR_HI8_LDI_PM },
+  { BFD_RELOC_AVR_HI8_LDI_GS,       R_AVR_HI8_LDI_GS },
   { BFD_RELOC_AVR_HH8_LDI_PM,       R_AVR_HH8_LDI_PM },
   { BFD_RELOC_AVR_LO8_LDI_PM_NEG,   R_AVR_LO8_LDI_PM_NEG },
   { BFD_RELOC_AVR_HI8_LDI_PM_NEG,   R_AVR_HI8_LDI_PM_NEG },
   { BFD_RELOC_AVR_HH8_LDI_PM_NEG,   R_AVR_HH8_LDI_PM_NEG },
   { BFD_RELOC_AVR_CALL,             R_AVR_CALL },
   { BFD_RELOC_AVR_LDI,              R_AVR_LDI  },
   { BFD_RELOC_AVR_6,                R_AVR_6    },
   { BFD_RELOC_AVR_6_ADIW,           R_AVR_6_ADIW }
 };
 
+
+
 /* Meant to be filled one day with the wrap around address for the
    specific device.  I.e. should get the value 0x4000 for 16k devices,
    0x8000 for 32k devices and so on.
 
    We initialize it here with a value of 0x1000000 resulting in
    that we will never suggest a wrap-around jump during relaxation.
    The logic of the source code later on assumes that in
    avr_pc_wrap_around one single bit is set.  */
 
-unsigned int avr_pc_wrap_around = 0x10000000;
+bfd_vma avr_pc_wrap_around = 0x10000000;
+
+/* If this variable holds a value different from zero, the linker relaxation
+   machine will try to optimize call/ret sequences by a single jump
+   instruction. This option could be switched off by a linker switch*/
+int avr_replace_call_ret_sequences = 1;
 
 /* Calculates the effective distance of a pc relative jump/call.  */
 
 static int
 avr_relative_distance_considering_wrap_around (unsigned int distance)
 {
   unsigned int wrap_around_mask = avr_pc_wrap_around - 1;
   int dist_with_wrap_around = distance & wrap_around_mask;
 
   if (dist_with_wrap_around > ((int) (avr_pc_wrap_around >> 1)))
     dist_with_wrap_around -= avr_pc_wrap_around;
 
   return dist_with_wrap_around;
 }
 
-
 static reloc_howto_type *
 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
 				 bfd_reloc_code_real_type code)
 {
   unsigned int i;
 
   for (i = 0;
        i < sizeof (avr_reloc_map) / sizeof (struct avr_reloc_map);
        i++)
     if (avr_reloc_map[i].bfd_reloc_val == code)
       return &elf_avr_howto_table[avr_reloc_map[i].elf_reloc_val];
 
   return NULL;
 }
 
 /* Set the howto pointer for an AVR ELF reloc.  */
 
 static void
 avr_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
 			arelent *cache_ptr,
 			Elf_Internal_Rela *dst)
 {
   unsigned int r_type;
 
   r_type = ELF32_R_TYPE (dst->r_info);
   BFD_ASSERT (r_type < (unsigned int) R_AVR_max);
   cache_ptr->howto = &elf_avr_howto_table[r_type];
 }
 
 static asection *
 elf32_avr_gc_mark_hook (asection *sec,
 			struct bfd_link_info *info ATTRIBUTE_UNUSED,
 			Elf_Internal_Rela *rel,
 			struct elf_link_hash_entry *h,
 			Elf_Internal_Sym *sym)
 {
   if (h != NULL)
     {
       switch (ELF32_R_TYPE (rel->r_info))
 	{
 	default:
 	  switch (h->root.type)
 	    {
 	    case bfd_link_hash_defined:
 	    case bfd_link_hash_defweak:
 	      return h->root.u.def.section;
 
 	    case bfd_link_hash_common:
 	      return h->root.u.c.p->section;
 
 	    default:
 	      break;
 	    }
 	}
     }
   else
     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
 
   return NULL;
 }
 
 static bfd_boolean
 elf32_avr_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
 			 struct bfd_link_info *info ATTRIBUTE_UNUSED,
 			 asection *sec ATTRIBUTE_UNUSED,
 			 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
 {
   /* We don't use got and plt entries for avr.  */
   return TRUE;
 }
 
 /* Look through the relocs for a section during the first phase.
    Since we don't do .gots or .plts, we just need to consider the
    virtual table relocs for gc.  */
 
 static bfd_boolean
 elf32_avr_check_relocs (bfd *abfd,
 			struct bfd_link_info *info,
 			asection *sec,
 			const Elf_Internal_Rela *relocs)
 {
   Elf_Internal_Shdr *symtab_hdr;
   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
   const Elf_Internal_Rela *rel;
   const Elf_Internal_Rela *rel_end;
 
   if (info->relocatable)
     return TRUE;
 
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
   sym_hashes_end = sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
   if (!elf_bad_symtab (abfd))
     sym_hashes_end -= symtab_hdr->sh_info;
 
   rel_end = relocs + sec->reloc_count;
   for (rel = relocs; rel < rel_end; rel++)
     {
       struct elf_link_hash_entry *h;
       unsigned long r_symndx;
 
       r_symndx = ELF32_R_SYM (rel->r_info);
       if (r_symndx < symtab_hdr->sh_info)
         h = NULL;
       else
 	{
 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
 	  while (h->root.type == bfd_link_hash_indirect
 		 || h->root.type == bfd_link_hash_warning)
 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
 	}
     }
 
   return TRUE;
 }
 
+static bfd_boolean
+avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation)
+{
+  return (relocation >= 0x020000);
+}
+
 /* Perform a single relocation.  By default we use the standard BFD
    routines, but a few relocs, we have to do them ourselves.  */
 
 static bfd_reloc_status_type
 avr_final_link_relocate (reloc_howto_type *  howto,
-			 bfd *               input_bfd,
-			 asection *          input_section,
-			 bfd_byte *          contents,
-			 Elf_Internal_Rela * rel,
-			 bfd_vma             relocation)
+                         bfd *               input_bfd,
+                         asection *          input_section,
+                         bfd_byte *          contents,
+                         Elf_Internal_Rela * rel,
+                         bfd_vma             relocation,
+                         struct elf32_avr_link_hash_table *htab)
 {
   bfd_reloc_status_type r = bfd_reloc_ok;
   bfd_vma               x;
   bfd_signed_vma	srel;
+  bfd_signed_vma	reloc_addr;
+  
+  bfd_boolean use_stubs = FALSE;
+
+  /* Usually is 0, unless we are generating code for a bootloader.  */ 
+  bfd_signed_vma        base_addr = htab->vector_base;
+
+  /* Absolute addr of the reloc in the final excecutable.  */
+  reloc_addr = rel->r_offset + input_section->output_section->vma
+	       + input_section->output_offset;
 
   switch (howto->type)
     {
     case R_AVR_7_PCREL:
       contents += rel->r_offset;
       srel = (bfd_signed_vma) relocation;
       srel += rel->r_addend;
       srel -= rel->r_offset;
       srel -= 2;	/* Branch instructions add 2 to the PC...  */
       srel -= (input_section->output_section->vma +
 	       input_section->output_offset);
 
       if (srel & 1)
 	return bfd_reloc_outofrange;
       if (srel > ((1 << 7) - 1) || (srel < - (1 << 7)))
 	return bfd_reloc_overflow;
       x = bfd_get_16 (input_bfd, contents);
       x = (x & 0xfc07) | (((srel >> 1) << 3) & 0x3f8);
       bfd_put_16 (input_bfd, x, contents);
       break;
 
     case R_AVR_13_PCREL:
       contents   += rel->r_offset;
       srel = (bfd_signed_vma) relocation;
       srel += rel->r_addend;
       srel -= rel->r_offset;
       srel -= 2;	/* Branch instructions add 2 to the PC...  */
       srel -= (input_section->output_section->vma +
 	       input_section->output_offset);
 
       if (srel & 1)
 	return bfd_reloc_outofrange;
 
       srel = avr_relative_distance_considering_wrap_around (srel);
 
       /* AVR addresses commands as words.  */
       srel >>= 1;
 
       /* Check for overflow.  */
       if (srel < -2048 || srel > 2047)
 	{
           /* Relative distance is too large.  */
 
 	  /* Always apply WRAPAROUND for avr2 and avr4.  */
 	  switch (bfd_get_mach (input_bfd))
 	    {
 	    case bfd_mach_avr2:
 	    case bfd_mach_avr4:
 	      break;
 
 	    default:
 	      return bfd_reloc_overflow;
 	    }
 	}
 
       x = bfd_get_16 (input_bfd, contents);
       x = (x & 0xf000) | (srel & 0xfff);
       bfd_put_16 (input_bfd, x, contents);
       break;
 
     case R_AVR_LO8_LDI:
       contents += rel->r_offset;
@@ -688,652 +927,760 @@
       x = bfd_get_16 (input_bfd, contents);
       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
       bfd_put_16 (input_bfd, x, contents);
       break;
 
     case R_AVR_HH8_LDI:
       contents += rel->r_offset;
       srel = (bfd_signed_vma) relocation + rel->r_addend;
       srel = (srel >> 16) & 0xff;
       x = bfd_get_16 (input_bfd, contents);
       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
       bfd_put_16 (input_bfd, x, contents);
       break;
 
     case R_AVR_MS8_LDI:
       contents += rel->r_offset;
       srel = (bfd_signed_vma) relocation + rel->r_addend;
       srel = (srel >> 24) & 0xff;
       x = bfd_get_16 (input_bfd, contents);
       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
       bfd_put_16 (input_bfd, x, contents);
       break;
 
     case R_AVR_LO8_LDI_NEG:
       contents += rel->r_offset;
       srel = (bfd_signed_vma) relocation + rel->r_addend;
       srel = -srel;
       x = bfd_get_16 (input_bfd, contents);
       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
       bfd_put_16 (input_bfd, x, contents);
       break;
 
     case R_AVR_HI8_LDI_NEG:
       contents += rel->r_offset;
       srel = (bfd_signed_vma) relocation + rel->r_addend;
       srel = -srel;
       srel = (srel >> 8) & 0xff;
       x = bfd_get_16 (input_bfd, contents);
       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
       bfd_put_16 (input_bfd, x, contents);
       break;
 
     case R_AVR_HH8_LDI_NEG:
       contents += rel->r_offset;
       srel = (bfd_signed_vma) relocation + rel->r_addend;
       srel = -srel;
       srel = (srel >> 16) & 0xff;
       x = bfd_get_16 (input_bfd, contents);
       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
       bfd_put_16 (input_bfd, x, contents);
       break;
 
     case R_AVR_MS8_LDI_NEG:
       contents += rel->r_offset;
       srel = (bfd_signed_vma) relocation + rel->r_addend;
       srel = -srel;
       srel = (srel >> 24) & 0xff;
       x = bfd_get_16 (input_bfd, contents);
       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
       bfd_put_16 (input_bfd, x, contents);
       break;
 
+    case R_AVR_LO8_LDI_GS:
+      use_stubs = (!htab->no_stubs);
     case R_AVR_LO8_LDI_PM:
+
       contents += rel->r_offset;
       srel = (bfd_signed_vma) relocation + rel->r_addend;
+
+      if (use_stubs
+          && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
+        { 
+          bfd_vma old_srel = srel;
+          /* We need to use the address of the stub instead.  */
+          srel = avr_get_stub_addr (srel,htab);
+          if (debug_stubs)
+            printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
+                    "reloc at address 0x%x.\n",
+                    (unsigned int) srel,
+                    (unsigned int) old_srel,
+                    (unsigned int) reloc_addr);
+        }
+      if (use_stubs
+          && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
+	return bfd_reloc_outofrange;
       if (srel & 1)
 	return bfd_reloc_outofrange;
+
       srel = srel >> 1;
       x = bfd_get_16 (input_bfd, contents);
       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
       bfd_put_16 (input_bfd, x, contents);
       break;
 
+    case R_AVR_HI8_LDI_GS:
+      use_stubs = (!htab->no_stubs);
     case R_AVR_HI8_LDI_PM:
       contents += rel->r_offset;
       srel = (bfd_signed_vma) relocation + rel->r_addend;
+
+      if (use_stubs
+          && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
+        { 
+          bfd_vma old_srel = srel;
+          /* We need to use the address of the stub instead.  */
+          srel = avr_get_stub_addr (srel,htab);
+          if (debug_stubs)
+            printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
+                    "reloc at address 0x%x.\n",
+                    (unsigned int) srel,
+                    (unsigned int) old_srel,
+                    (unsigned int) reloc_addr);
+        }
+      if (use_stubs
+          && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
+	return bfd_reloc_outofrange;
       if (srel & 1)
 	return bfd_reloc_outofrange;
+
       srel = srel >> 1;
       srel = (srel >> 8) & 0xff;
       x = bfd_get_16 (input_bfd, contents);
       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
       bfd_put_16 (input_bfd, x, contents);
       break;
 
     case R_AVR_HH8_LDI_PM:
       contents += rel->r_offset;
       srel = (bfd_signed_vma) relocation + rel->r_addend;
       if (srel & 1)
 	return bfd_reloc_outofrange;
       srel = srel >> 1;
       srel = (srel >> 16) & 0xff;
       x = bfd_get_16 (input_bfd, contents);
       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
       bfd_put_16 (input_bfd, x, contents);
       break;
 
     case R_AVR_LO8_LDI_PM_NEG:
       contents += rel->r_offset;
       srel = (bfd_signed_vma) relocation + rel->r_addend;
       srel = -srel;
       if (srel & 1)
 	return bfd_reloc_outofrange;
       srel = srel >> 1;
       x = bfd_get_16 (input_bfd, contents);
       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
       bfd_put_16 (input_bfd, x, contents);
       break;
 
     case R_AVR_HI8_LDI_PM_NEG:
       contents += rel->r_offset;
       srel = (bfd_signed_vma) relocation + rel->r_addend;
       srel = -srel;
       if (srel & 1)
 	return bfd_reloc_outofrange;
       srel = srel >> 1;
       srel = (srel >> 8) & 0xff;
       x = bfd_get_16 (input_bfd, contents);
       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
       bfd_put_16 (input_bfd, x, contents);
       break;
 
     case R_AVR_HH8_LDI_PM_NEG:
       contents += rel->r_offset;
       srel = (bfd_signed_vma) relocation + rel->r_addend;
       srel = -srel;
       if (srel & 1)
 	return bfd_reloc_outofrange;
       srel = srel >> 1;
       srel = (srel >> 16) & 0xff;
       x = bfd_get_16 (input_bfd, contents);
       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
       bfd_put_16 (input_bfd, x, contents);
       break;
 
     case R_AVR_CALL:
       contents += rel->r_offset;
       srel = (bfd_signed_vma) relocation + rel->r_addend;
       if (srel & 1)
 	return bfd_reloc_outofrange;
       srel = srel >> 1;
       x = bfd_get_16 (input_bfd, contents);
       x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16;
       bfd_put_16 (input_bfd, x, contents);
       bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2);
       break;
 
+    case R_AVR_16_PM:
+      use_stubs = (!htab->no_stubs);
+      contents += rel->r_offset;
+      srel = (bfd_signed_vma) relocation + rel->r_addend;
+
+      if (use_stubs 
+          && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
+        { 
+          bfd_vma old_srel = srel;
+          /* We need to use the address of the stub instead.  */
+          srel = avr_get_stub_addr (srel,htab);
+          if (debug_stubs)
+            printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
+                    "reloc at address 0x%x.\n",
+                    (unsigned int) srel,
+                    (unsigned int) old_srel,
+                    (unsigned int) reloc_addr);
+        }
+      if (use_stubs
+          && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
+	return bfd_reloc_outofrange;
+      if (srel & 1)
+	return bfd_reloc_outofrange;
+
+      srel = srel >> 1;
+      bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents);
+      break;
+
     default:
       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
 				    contents, rel->r_offset,
 				    relocation, rel->r_addend);
     }
 
   return r;
 }
 
 /* Relocate an AVR ELF section.  */
 
 static bfd_boolean
 elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
 			    struct bfd_link_info *info,
 			    bfd *input_bfd,
 			    asection *input_section,
 			    bfd_byte *contents,
 			    Elf_Internal_Rela *relocs,
 			    Elf_Internal_Sym *local_syms,
 			    asection **local_sections)
 {
   Elf_Internal_Shdr *           symtab_hdr;
   struct elf_link_hash_entry ** sym_hashes;
   Elf_Internal_Rela *           rel;
   Elf_Internal_Rela *           relend;
+  struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info); 
 
   if (info->relocatable)
     return TRUE;
 
   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (input_bfd);
   relend     = relocs + input_section->reloc_count;
 
   for (rel = relocs; rel < relend; rel ++)
     {
       reloc_howto_type *           howto;
       unsigned long                r_symndx;
       Elf_Internal_Sym *           sym;
       asection *                   sec;
       struct elf_link_hash_entry * h;
       bfd_vma                      relocation;
       bfd_reloc_status_type        r;
       const char *                 name;
       int                          r_type;
 
       /* This is a final link.  */
       r_type = ELF32_R_TYPE (rel->r_info);
       r_symndx = ELF32_R_SYM (rel->r_info);
       howto  = elf_avr_howto_table + ELF32_R_TYPE (rel->r_info);
       h      = NULL;
       sym    = NULL;
       sec    = NULL;
 
       if (r_symndx < symtab_hdr->sh_info)
 	{
 	  sym = local_syms + r_symndx;
 	  sec = local_sections [r_symndx];
 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
 
 	  name = bfd_elf_string_from_elf_section
 	    (input_bfd, symtab_hdr->sh_link, sym->st_name);
 	  name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
 	}
       else
 	{
 	  bfd_boolean unresolved_reloc, warned;
 
 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
 				   r_symndx, symtab_hdr, sym_hashes,
 				   h, sec, relocation,
 				   unresolved_reloc, warned);
 
 	  name = h->root.root.string;
 	}
 
       r = avr_final_link_relocate (howto, input_bfd, input_section,
-				   contents, rel, relocation);
+				   contents, rel, relocation, htab);
 
       if (r != bfd_reloc_ok)
 	{
 	  const char * msg = (const char *) NULL;
 
 	  switch (r)
 	    {
 	    case bfd_reloc_overflow:
 	      r = info->callbacks->reloc_overflow
 		(info, (h ? &h->root : NULL),
 		 name, howto->name, (bfd_vma) 0,
 		 input_bfd, input_section, rel->r_offset);
 	      break;
 
 	    case bfd_reloc_undefined:
 	      r = info->callbacks->undefined_symbol
 		(info, name, input_bfd, input_section, rel->r_offset, TRUE);
 	      break;
 
 	    case bfd_reloc_outofrange:
 	      msg = _("internal error: out of range error");
 	      break;
 
 	    case bfd_reloc_notsupported:
 	      msg = _("internal error: unsupported relocation error");
 	      break;
 
 	    case bfd_reloc_dangerous:
 	      msg = _("internal error: dangerous relocation");
 	      break;
 
 	    default:
 	      msg = _("internal error: unknown error");
 	      break;
 	    }
 
 	  if (msg)
 	    r = info->callbacks->warning
 	      (info, msg, name, input_bfd, input_section, rel->r_offset);
 
 	  if (! r)
 	    return FALSE;
 	}
     }
 
   return TRUE;
 }
 
 /* The final processing done just before writing out a AVR ELF object
    file.  This gets the AVR architecture right based on the machine
    number.  */
 
 static void
 bfd_elf_avr_final_write_processing (bfd *abfd,
 				    bfd_boolean linker ATTRIBUTE_UNUSED)
 {
   unsigned long val;
 
   switch (bfd_get_mach (abfd))
     {
     default:
     case bfd_mach_avr2:
       val = E_AVR_MACH_AVR2;
       break;
 
     case bfd_mach_avr1:
       val = E_AVR_MACH_AVR1;
       break;
 
     case bfd_mach_avr3:
       val = E_AVR_MACH_AVR3;
       break;
 
     case bfd_mach_avr4:
       val = E_AVR_MACH_AVR4;
       break;
 
     case bfd_mach_avr5:
       val = E_AVR_MACH_AVR5;
       break;
+
+    case bfd_mach_avr6:
+      val = E_AVR_MACH_AVR6;
+      break;
     }
 
   elf_elfheader (abfd)->e_machine = EM_AVR;
   elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH;
   elf_elfheader (abfd)->e_flags |= val;
   elf_elfheader (abfd)->e_flags |= EF_AVR_LINKRELAX_PREPARED;
 }
 
 /* Set the right machine number.  */
 
 static bfd_boolean
 elf32_avr_object_p (bfd *abfd)
 {
   unsigned int e_set = bfd_mach_avr2;
 
   if (elf_elfheader (abfd)->e_machine == EM_AVR
       || elf_elfheader (abfd)->e_machine == EM_AVR_OLD)
     {
       int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH;
 
       switch (e_mach)
 	{
 	default:
 	case E_AVR_MACH_AVR2:
 	  e_set = bfd_mach_avr2;
 	  break;
 
 	case E_AVR_MACH_AVR1:
 	  e_set = bfd_mach_avr1;
 	  break;
 
 	case E_AVR_MACH_AVR3:
 	  e_set = bfd_mach_avr3;
 	  break;
 
 	case E_AVR_MACH_AVR4:
 	  e_set = bfd_mach_avr4;
 	  break;
 
 	case E_AVR_MACH_AVR5:
 	  e_set = bfd_mach_avr5;
 	  break;
+
+	case E_AVR_MACH_AVR6:
+	  e_set = bfd_mach_avr6;
+	  break;
 	}
     }
   return bfd_default_set_arch_mach (abfd, bfd_arch_avr,
 				    e_set);
 }
 
 
-/* Enable debugging printout at stdout with a value of 1.  */
-#define DEBUG_RELAX 0
-
 /* Delete some bytes from a section while changing the size of an instruction.
    The parameter "addr" denotes the section-relative offset pointing just
    behind the shrinked instruction. "addr+count" point at the first
    byte just behind the original unshrinked instruction.  */
 
 static bfd_boolean
 elf32_avr_relax_delete_bytes (bfd *abfd,
                               asection *sec,
                               bfd_vma addr,
                               int count)
 {
   Elf_Internal_Shdr *symtab_hdr;
   unsigned int sec_shndx;
   bfd_byte *contents;
   Elf_Internal_Rela *irel, *irelend;
   Elf_Internal_Rela *irelalign;
   Elf_Internal_Sym *isym;
   Elf_Internal_Sym *isymbuf = NULL;
   Elf_Internal_Sym *isymend;
   bfd_vma toaddr;
   struct elf_link_hash_entry **sym_hashes;
   struct elf_link_hash_entry **end_hashes;
   unsigned int symcount;
 
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   contents = elf_section_data (sec)->this_hdr.contents;
 
   /* The deletion must stop at the next ALIGN reloc for an aligment
      power larger than the number of bytes we are deleting.  */
 
   irelalign = NULL;
   toaddr = sec->size;
 
   irel = elf_section_data (sec)->relocs;
   irelend = irel + sec->reloc_count;
 
   /* Actually delete the bytes.  */
   if (toaddr - addr - count > 0)
     memmove (contents + addr, contents + addr + count,
              (size_t) (toaddr - addr - count));
   sec->size -= count;
 
   /* Adjust all the reloc addresses.  */
   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
     {
       bfd_vma old_reloc_address;
       bfd_vma shrinked_insn_address;
 
       old_reloc_address = (sec->output_section->vma
                            + sec->output_offset + irel->r_offset);
       shrinked_insn_address = (sec->output_section->vma
                               + sec->output_offset + addr - count);
 
       /* Get the new reloc address.  */
       if ((irel->r_offset > addr
            && irel->r_offset < toaddr))
         {
-          if (DEBUG_RELAX)
+          if (debug_relax)
             printf ("Relocation at address 0x%x needs to be moved.\n"
                     "Old section offset: 0x%x, New section offset: 0x%x \n",
                     (unsigned int) old_reloc_address,
                     (unsigned int) irel->r_offset,
                     (unsigned int) ((irel->r_offset) - count));
 
           irel->r_offset -= count;
         }
 
     }
 
    /* The reloc's own addresses are now ok. However, we need to readjust
       the reloc's addend, i.e. the reloc's value if two conditions are met:
       1.) the reloc is relative to a symbol in this section that
           is located in front of the shrinked instruction
       2.) symbol plus addend end up behind the shrinked instruction.  
       
       The most common case where this happens are relocs relative to
       the section-start symbol.
          
       This step needs to be done for all of the sections of the bfd.  */
 
   {
     struct bfd_section *isec;
-
     for (isec = abfd->sections; isec; isec = isec->next)
      {
        bfd_vma symval;
        bfd_vma shrinked_insn_address;
 
        shrinked_insn_address = (sec->output_section->vma
                                 + sec->output_offset + addr - count);
 
        irelend = elf_section_data (isec)->relocs + isec->reloc_count;
        for (irel = elf_section_data (isec)->relocs; 
             irel < irelend;
             irel++)
          {
            /* Read this BFD's local symbols if we haven't done 
               so already.  */
            if (isymbuf == NULL && symtab_hdr->sh_info != 0)
              {
                isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
                if (isymbuf == NULL)
                  isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
                                                  symtab_hdr->sh_info, 0,
                                                  NULL, NULL, NULL);
                if (isymbuf == NULL)
                  return FALSE;
              }
 
            /* Get the value of the symbol referred to by the reloc.  */
            if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
              {
                /* A local symbol.  */
                Elf_Internal_Sym *isym;
                asection *sym_sec;
 
                isym = isymbuf + ELF32_R_SYM (irel->r_info);
                sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
                symval = isym->st_value;
                /* If the reloc is absolute, it will not have
                   a symbol or section associated with it.  */
                if (sym_sec == sec)
                  { 
                    symval += sym_sec->output_section->vma
                              + sym_sec->output_offset;
 
-                   if (DEBUG_RELAX)
+                   if (debug_relax)
                      printf ("Checking if the relocation's "
                              "addend needs corrections.\n"
                              "Address of anchor symbol: 0x%x \n"
                              "Address of relocation target: 0x%x \n"
                              "Address of relaxed insn: 0x%x \n",
                              (unsigned int) symval,
                              (unsigned int) (symval + irel->r_addend),
                              (unsigned int) shrinked_insn_address);
 
                    if (symval <= shrinked_insn_address
                        && (symval + irel->r_addend) > shrinked_insn_address)
                      {
                        irel->r_addend -= count;
 
-                       if (DEBUG_RELAX)
+                       if (debug_relax)
                          printf ("Relocation's addend needed to be fixed \n");
                      }
                  }
-	       /* else...Reference symbol is absolute.  No adjustment needed.  */
+               else
+                 {
+                   /* Reference symbol is absolute.  No adjustment needed.  */
+                 }
+              }
+            else
+              {
+                /* Reference symbol is extern. No need for adjusting 
+                   the addend.  */
               }
-	   /* else...Reference symbol is extern. No need for adjusting 
-	      the addend.  */
           }
      }
   }
 
   /* Adjust the local symbols defined in this section.  */
   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
   isymend = isym + symtab_hdr->sh_info;
   for (; isym < isymend; isym++)
     {
       if (isym->st_shndx == sec_shndx
           && isym->st_value > addr
           && isym->st_value < toaddr)
         isym->st_value -= count;
     }
 
   /* Now adjust the global symbols defined in this section.  */
   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
               - symtab_hdr->sh_info);
   sym_hashes = elf_sym_hashes (abfd);
   end_hashes = sym_hashes + symcount;
   for (; sym_hashes < end_hashes; sym_hashes++)
     {
       struct elf_link_hash_entry *sym_hash = *sym_hashes;
       if ((sym_hash->root.type == bfd_link_hash_defined
            || sym_hash->root.type == bfd_link_hash_defweak)
           && sym_hash->root.u.def.section == sec
           && sym_hash->root.u.def.value > addr
           && sym_hash->root.u.def.value < toaddr)
         {
           sym_hash->root.u.def.value -= count;
         }
     }
 
   return TRUE;
 }
 
 /* This function handles relaxing for the avr.
    Many important relaxing opportunities within functions are already
    realized by the compiler itself.
    Here we try to replace  call (4 bytes) ->  rcall (2 bytes)
    and jump -> rjmp (safes also 2 bytes).
    As well we now optimize seqences of
      - call/rcall function
      - ret
    to yield
      - jmp/rjmp function
      - ret
    . In case that within a sequence
      - jmp/rjmp label
      - ret
    the ret could no longer be reached it is optimized away. In order
    to check if the ret is no longer needed, it is checked that the ret's address
    is not the target of a branch or jump within the same section, it is checked
    that there is no skip instruction before the jmp/rjmp and that there
    is no local or global label place at the address of the ret.
 
    We refrain from relaxing within sections ".vectors" and
    ".jumptables" in order to maintain the position of the instructions.
    There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
    if possible. (In future one could possibly use the space of the nop
    for the first instruction of the irq service function.
 
    The .jumptables sections is meant to be used for a future tablejump variant
    for the devices with 3-byte program counter where the table itself
    contains 4-byte jump instructions whose relative offset must not
    be changed.  */
 
-static  bfd_boolean
+static bfd_boolean
 elf32_avr_relax_section (bfd *abfd,
 			 asection *sec,
                          struct bfd_link_info *link_info,
                          bfd_boolean *again)
 {
   Elf_Internal_Shdr *symtab_hdr;
   Elf_Internal_Rela *internal_relocs;
   Elf_Internal_Rela *irel, *irelend;
   bfd_byte *contents = NULL;
   Elf_Internal_Sym *isymbuf = NULL;
   static asection *last_input_section = NULL;
   static Elf_Internal_Rela *last_reloc = NULL;
+  struct elf32_avr_link_hash_table *htab;
+
+  htab = avr_link_hash_table (link_info);
 
   /* Assume nothing changes.  */
   *again = FALSE;
 
+  if (sec == htab->stub_sec)
+    {
+
+      /* We are just relaxing the stub section.  Let's again calculate
+         the size needed.  */
+      bfd_size_type last_estimated_stub_section_size = htab->stub_sec->size;
+
+      if (debug_relax)
+        printf ("Relaxing the stub section. Size prior to this pass: %i\n",
+                (int) last_estimated_stub_section_size); 
+          
+      elf32_avr_size_stubs (htab->stub_sec->output_section->owner, 
+                            link_info, FALSE);
+     
+      /* Check if the number of trampolines changed.  */ 
+      if (last_estimated_stub_section_size != htab->stub_sec->size)
+        *again = TRUE;
+
+      if (debug_relax)
+        printf ("Size of stub section after this pass: %i\n",
+                (int) htab->stub_sec->size);
+
+      return TRUE; 
+    }
+
   /* We don't have to do anything for a relocatable link, if
      this section does not have relocs, or if this is not a
      code section.  */
   if (link_info->relocatable
       || (sec->flags & SEC_RELOC) == 0
       || sec->reloc_count == 0
       || (sec->flags & SEC_CODE) == 0)
     return TRUE;
 
   /* Check if the object file to relax uses internal symbols so that we
      could fix up the relocations.  */
   if (!(elf_elfheader (abfd)->e_flags & EF_AVR_LINKRELAX_PREPARED))
     return TRUE;
 
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
 
   /* Get a copy of the native relocations.  */
   internal_relocs = (_bfd_elf_link_read_relocs
                      (abfd, sec, NULL, NULL, link_info->keep_memory));
   if (internal_relocs == NULL)
     goto error_return;
 
   if (sec != last_input_section)
     last_reloc = NULL;
 
   last_input_section = sec;
 
   /* Walk through the relocs looking for relaxing opportunities.  */
   irelend = internal_relocs + sec->reloc_count;
   for (irel = internal_relocs; irel < irelend; irel++)
     {
       bfd_vma symval;
 
       if (   ELF32_R_TYPE (irel->r_info) != R_AVR_13_PCREL
           && ELF32_R_TYPE (irel->r_info) != R_AVR_7_PCREL
           && ELF32_R_TYPE (irel->r_info) != R_AVR_CALL)
         continue;
 
       /* Get the section contents if we haven't done so already.  */
       if (contents == NULL)
         {
           /* Get cached copy if it exists.  */
           if (elf_section_data (sec)->this_hdr.contents != NULL)
             contents = elf_section_data (sec)->this_hdr.contents;
           else
             {
               /* Go get them off disk.  */
               if (! bfd_malloc_and_get_section (abfd, sec, &contents))
                 goto error_return;
             }
         }
 
      /* Read this BFD's local symbols if we haven't done so already.  */
       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
         {
           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
           if (isymbuf == NULL)
             isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
                                             symtab_hdr->sh_info, 0,
                                             NULL, NULL, NULL);
           if (isymbuf == NULL)
             goto error_return;
@@ -1382,488 +1729,491 @@
          must tell the rest of the code not to free up this
          information.  It would be possible to instead create a table
          of changes which have to be made, as is done in coff-mips.c;
          that would be more work, but would require less memory when
          the linker is run.  */
       switch (ELF32_R_TYPE (irel->r_info))
         {
          /* Try to turn a 22-bit absolute call/jump into an 13-bit
             pc-relative rcall/rjmp.  */
          case R_AVR_CALL:
           {
             bfd_vma value = symval + irel->r_addend;
             bfd_vma dot, gap;
             int distance_short_enough = 0;
 
             /* Get the address of this instruction.  */
             dot = (sec->output_section->vma
                    + sec->output_offset + irel->r_offset);
 
             /* Compute the distance from this insn to the branch target.  */
             gap = value - dot;
 
             /* If the distance is within -4094..+4098 inclusive, then we can
                relax this jump/call.  +4098 because the call/jump target
                will be closer after the relaxation.  */
             if ((int) gap >= -4094 && (int) gap <= 4098)
               distance_short_enough = 1;
 
             /* Here we handle the wrap-around case.  E.g. for a 16k device
                we could use a rjmp to jump from address 0x100 to 0x3d00!
                In order to make this work properly, we need to fill the
                vaiable avr_pc_wrap_around with the appropriate value.
                I.e. 0x4000 for a 16k device.  */
             {
                /* Shrinking the code size makes the gaps larger in the
                   case of wrap-arounds.  So we use a heuristical safety
                   margin to avoid that during relax the distance gets
                   again too large for the short jumps.  Let's assume
                   a typical code-size reduction due to relax for a
                   16k device of 600 bytes.  So let's use twice the
                   typical value as safety margin.  */
                int rgap;
                int safety_margin;
 
                int assumed_shrink = 600;
                if (avr_pc_wrap_around > 0x4000)
                  assumed_shrink = 900;
 
                safety_margin = 2 * assumed_shrink;
 
                rgap = avr_relative_distance_considering_wrap_around (gap);
 
                if (rgap >= (-4092 + safety_margin)
                    && rgap <= (4094 - safety_margin))
 		 distance_short_enough = 1;
             }
 
             if (distance_short_enough)
               {
                 unsigned char code_msb;
                 unsigned char code_lsb;
 
-                if (DEBUG_RELAX)
+                if (debug_relax)
                   printf ("shrinking jump/call instruction at address 0x%x"
                           " in section %s\n\n",
                           (int) dot, sec->name);
 
                 /* Note that we've changed the relocs, section contents,
                    etc.  */
                 elf_section_data (sec)->relocs = internal_relocs;
                 elf_section_data (sec)->this_hdr.contents = contents;
                 symtab_hdr->contents = (unsigned char *) isymbuf;
 
                 /* Get the instruction code for relaxing.  */
                 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset);
                 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
 
                 /* Mask out the relocation bits.  */
                 code_msb &= 0x94;
                 code_lsb &= 0x0E;
                 if (code_msb == 0x94 && code_lsb == 0x0E)
                   {
                     /* we are changing call -> rcall .  */
                     bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
                     bfd_put_8 (abfd, 0xD0, contents + irel->r_offset + 1);
                   }
                 else if (code_msb == 0x94 && code_lsb == 0x0C)
                   {
                     /* we are changeing jump -> rjmp.  */
                     bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
                     bfd_put_8 (abfd, 0xC0, contents + irel->r_offset + 1);
                   }
                 else
                   abort ();
 
                 /* Fix the relocation's type.  */
                 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
                                              R_AVR_13_PCREL);
 
                 /* Check for the vector section. There we don't want to
                    modify the ordering!  */
 
                 if (!strcmp (sec->name,".vectors")
                     || !strcmp (sec->name,".jumptables"))
                   {
                     /* Let's insert a nop.  */
                     bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 2);
                     bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 3);
                   }
                 else
                   {
                     /* Delete two bytes of data.  */
                     if (!elf32_avr_relax_delete_bytes (abfd, sec,
                                                        irel->r_offset + 2, 2))
                       goto error_return;
 
                     /* That will change things, so, we should relax again.
                        Note that this is not required, and it may be slow.  */
                     *again = TRUE;
                   }
               }
           }
 
         default:
           {
             unsigned char code_msb;
             unsigned char code_lsb;
             bfd_vma dot;
 
             code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
             code_lsb = bfd_get_8 (abfd, contents + irel->r_offset + 0);
 
             /* Get the address of this instruction.  */
             dot = (sec->output_section->vma
                    + sec->output_offset + irel->r_offset);
 
             /* Here we look for rcall/ret or call/ret sequences that could be
                safely replaced by rjmp/ret or jmp/ret */
-            if (0xd0 == (code_msb & 0xf0))
+            if ((0xd0 == (code_msb & 0xf0))
+                && avr_replace_call_ret_sequences)
               {
                 /* This insn is a rcall.  */
                 unsigned char next_insn_msb = 0;
                 unsigned char next_insn_lsb = 0;
 
                 if (irel->r_offset + 3 < sec->size)
                   {
                     next_insn_msb =
                         bfd_get_8 (abfd, contents + irel->r_offset + 3);
                     next_insn_lsb =
                         bfd_get_8 (abfd, contents + irel->r_offset + 2);
                   }
 
 		if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
                   {
                     /* The next insn is a ret. We now convert the rcall insn
                        into a rjmp instruction.  */
                     code_msb &= 0xef;
                     bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1);
-                    if (DEBUG_RELAX)
+                    if (debug_relax)
                       printf ("converted rcall/ret sequence at address 0x%x"
                               " into rjmp/ret sequence. Section is %s\n\n",
                               (int) dot, sec->name);
                     *again = TRUE;
                     break;
                   }
               }
             else if ((0x94 == (code_msb & 0xfe))
-                      && (0x0e == (code_lsb & 0x0e)))
+                      && (0x0e == (code_lsb & 0x0e))
+                      && avr_replace_call_ret_sequences)
               {
                 /* This insn is a call.  */
                 unsigned char next_insn_msb = 0;
                 unsigned char next_insn_lsb = 0;
 
                 if (irel->r_offset + 5 < sec->size)
                   {
                     next_insn_msb =
                         bfd_get_8 (abfd, contents + irel->r_offset + 5);
                     next_insn_lsb =
                         bfd_get_8 (abfd, contents + irel->r_offset + 4);
                   }
 
                 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
                   {
                     /* The next insn is a ret. We now convert the call insn
                        into a jmp instruction.  */
 
                     code_lsb &= 0xfd;
                     bfd_put_8 (abfd, code_lsb, contents + irel->r_offset);
-                    if (DEBUG_RELAX)
+                    if (debug_relax)
                       printf ("converted call/ret sequence at address 0x%x"
                               " into jmp/ret sequence. Section is %s\n\n",
                               (int) dot, sec->name);
                     *again = TRUE;
                     break;
                   }
               }
             else if ((0xc0 == (code_msb & 0xf0))
                      || ((0x94 == (code_msb & 0xfe))
                          && (0x0c == (code_lsb & 0x0e))))
               {
                 /* This insn is a rjmp or a jmp.  */
                 unsigned char next_insn_msb = 0;
                 unsigned char next_insn_lsb = 0;
                 int insn_size;
 
                 if (0xc0 == (code_msb & 0xf0))
                   insn_size = 2; /* rjmp insn */
                 else
                   insn_size = 4; /* jmp insn */
 
                 if (irel->r_offset + insn_size + 1 < sec->size)
                   {
                     next_insn_msb =
                         bfd_get_8 (abfd, contents + irel->r_offset
                                          + insn_size + 1);
                     next_insn_lsb =
                         bfd_get_8 (abfd, contents + irel->r_offset
                                          + insn_size);
                   }
 
                 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
                   {
                     /* The next insn is a ret. We possibly could delete
                        this ret. First we need to check for preceeding
                        sbis/sbic/sbrs or cpse "skip" instructions.  */
 
                     int there_is_preceeding_non_skip_insn = 1;
                     bfd_vma address_of_ret;
 
                     address_of_ret = dot + insn_size;
 
-                    if (DEBUG_RELAX && (insn_size == 2))
+                    if (debug_relax && (insn_size == 2))
                       printf ("found rjmp / ret sequence at address 0x%x\n",
                               (int) dot);
-                    if (DEBUG_RELAX && (insn_size == 4))
+                    if (debug_relax && (insn_size == 4))
                       printf ("found jmp / ret sequence at address 0x%x\n",
                               (int) dot);
 
                     /* We have to make sure that there is a preceeding insn.  */
                     if (irel->r_offset >= 2)
                       {
                         unsigned char preceeding_msb;
                         unsigned char preceeding_lsb;
                         preceeding_msb =
                             bfd_get_8 (abfd, contents + irel->r_offset - 1);
                         preceeding_lsb =
                             bfd_get_8 (abfd, contents + irel->r_offset - 2);
 
                         /* sbic.  */
                         if (0x99 == preceeding_msb)
                           there_is_preceeding_non_skip_insn = 0;
 
                         /* sbis.  */
                         if (0x9b == preceeding_msb)
                           there_is_preceeding_non_skip_insn = 0;
 
                         /* sbrc */
                         if ((0xfc == (preceeding_msb & 0xfe)
                             && (0x00 == (preceeding_lsb & 0x08))))
                           there_is_preceeding_non_skip_insn = 0;
 
                         /* sbrs */
                         if ((0xfe == (preceeding_msb & 0xfe)
                             && (0x00 == (preceeding_lsb & 0x08))))
                           there_is_preceeding_non_skip_insn = 0;
 
                         /* cpse */
                         if (0x10 == (preceeding_msb & 0xfc))
                           there_is_preceeding_non_skip_insn = 0;
 
                         if (there_is_preceeding_non_skip_insn == 0)
-                          if (DEBUG_RELAX)
+                          if (debug_relax)
                             printf ("preceeding skip insn prevents deletion of"
                                     " ret insn at addr 0x%x in section %s\n",
                                     (int) dot + 2, sec->name);
                       }
                     else
                       {
                         /* There is no previous instruction.  */
                         there_is_preceeding_non_skip_insn = 0;
                       }
 
                     if (there_is_preceeding_non_skip_insn)
                       {
                         /* We now only have to make sure that there is no
                            local label defined at the address of the ret
                            instruction and that there is no local relocation
                            in this section pointing to the ret.  */
 
                         int deleting_ret_is_safe = 1;
                         unsigned int section_offset_of_ret_insn =
                                           irel->r_offset + insn_size;
                         Elf_Internal_Sym *isym, *isymend;
                         unsigned int sec_shndx;
 
                         sec_shndx =
 			  _bfd_elf_section_from_bfd_section (abfd, sec);
 
                         /* Check for local symbols.  */
                         isym = (Elf_Internal_Sym *) symtab_hdr->contents;
                         isymend = isym + symtab_hdr->sh_info;
                         for (; isym < isymend; isym++)
                          {
                            if (isym->st_value == section_offset_of_ret_insn
                                && isym->st_shndx == sec_shndx)
                              {
                                deleting_ret_is_safe = 0;
-                               if (DEBUG_RELAX)
+                               if (debug_relax)
                                  printf ("local label prevents deletion of ret "
                                          "insn at address 0x%x\n",
                                          (int) dot + insn_size);
                              }
                          }
 
                          /* Now check for global symbols.  */
                          {
                            int symcount;
                            struct elf_link_hash_entry **sym_hashes;
                            struct elf_link_hash_entry **end_hashes;
 
                            symcount = (symtab_hdr->sh_size
                                        / sizeof (Elf32_External_Sym)
                                        - symtab_hdr->sh_info);
                            sym_hashes = elf_sym_hashes (abfd);
                            end_hashes = sym_hashes + symcount;
                            for (; sym_hashes < end_hashes; sym_hashes++)
                             {
                               struct elf_link_hash_entry *sym_hash =
                                                                  *sym_hashes;
                               if ((sym_hash->root.type == bfd_link_hash_defined
                                   || sym_hash->root.type ==
 				   bfd_link_hash_defweak)
                                   && sym_hash->root.u.def.section == sec
-                                  && sym_hash->root.u.def.value == section_offset_of_ret_insn)
+                                  && sym_hash->root.u.def.value == 
+                                                    section_offset_of_ret_insn)
                                 {
                                   deleting_ret_is_safe = 0;
-                                  if (DEBUG_RELAX)
+                                  if (debug_relax)
                                     printf ("global label prevents deletion of "
                                             "ret insn at address 0x%x\n",
                                             (int) dot + insn_size);
                                 }
                             }
                          }
                          /* Now we check for relocations pointing to ret.  */
                          {
                            Elf_Internal_Rela *irel;
                            Elf_Internal_Rela *relend;
                            Elf_Internal_Shdr *symtab_hdr;
 
                            symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
                            relend = elf_section_data (sec)->relocs
                                     + sec->reloc_count;
 
                            for (irel = elf_section_data (sec)->relocs;
                                 irel < relend; irel++)
                              {
                                bfd_vma reloc_target = 0;
                                bfd_vma symval;
                                Elf_Internal_Sym *isymbuf = NULL;
 
                                /* Read this BFD's local symbols if we haven't
                                   done so already.  */
                                if (isymbuf == NULL && symtab_hdr->sh_info != 0)
                                  {
                                    isymbuf = (Elf_Internal_Sym *)
                                              symtab_hdr->contents;
                                    if (isymbuf == NULL)
                                      isymbuf = bfd_elf_get_elf_syms
 				       (abfd,
 					symtab_hdr,
 					symtab_hdr->sh_info, 0,
 					NULL, NULL, NULL);
                                    if (isymbuf == NULL)
                                      break;
                                   }
 
                                /* Get the value of the symbol referred to
                                   by the reloc.  */
                                if (ELF32_R_SYM (irel->r_info)
                                    < symtab_hdr->sh_info)
                                  {
                                    /* A local symbol.  */
                                    Elf_Internal_Sym *isym;
                                    asection *sym_sec;
 
                                    isym = isymbuf
                                           + ELF32_R_SYM (irel->r_info);
                                    sym_sec = bfd_section_from_elf_index
 				     (abfd, isym->st_shndx);
                                    symval = isym->st_value;
 
                                    /* If the reloc is absolute, it will not
                                       have a symbol or section associated
                                       with it.  */
 
                                    if (sym_sec)
                                      {
                                        symval +=
                                            sym_sec->output_section->vma
                                            + sym_sec->output_offset;
                                        reloc_target = symval + irel->r_addend;
                                      }
                                    else
                                      {
                                        reloc_target = symval + irel->r_addend;
                                        /* Reference symbol is absolute.  */
                                      }
                                  }
 			       /* else ... reference symbol is extern.  */
 
                                if (address_of_ret == reloc_target)
                                  {
                                    deleting_ret_is_safe = 0;
-                                   if (DEBUG_RELAX)
+                                   if (debug_relax)
                                      printf ("ret from "
                                              "rjmp/jmp ret sequence at address"
                                              " 0x%x could not be deleted. ret"
                                              " is target of a relocation.\n",
                                              (int) address_of_ret);
                                  }
                              }
                          }
 
                          if (deleting_ret_is_safe)
                            {
-                             if (DEBUG_RELAX)
+                             if (debug_relax)
                                printf ("unreachable ret instruction "
                                        "at address 0x%x deleted.\n",
                                        (int) dot + insn_size);
 
                              /* Delete two bytes of data.  */
                              if (!elf32_avr_relax_delete_bytes (abfd, sec,
                                         irel->r_offset + insn_size, 2))
                                goto error_return;
 
                              /* That will change things, so, we should relax
                                 again. Note that this is not required, and it
                                 may be slow.  */
                              *again = TRUE;
                              break;
                            }
                       }
 
                   }
               }
             break;
           }
         }
     }
 
   if (contents != NULL
       && elf_section_data (sec)->this_hdr.contents != contents)
     {
       if (! link_info->keep_memory)
         free (contents);
       else
         {
           /* Cache the section contents for elf_link_input_bfd.  */
           elf_section_data (sec)->this_hdr.contents = contents;
         }
     }
 
   if (internal_relocs != NULL
       && elf_section_data (sec)->relocs != internal_relocs)
     free (internal_relocs);
 
   return TRUE;
 
  error_return:
   if (isymbuf != NULL
       && symtab_hdr->contents != (unsigned char *) isymbuf)
     free (isymbuf);
   if (contents != NULL
       && elf_section_data (sec)->this_hdr.contents != contents)
     free (contents);
   if (internal_relocs != NULL
       && elf_section_data (sec)->relocs != internal_relocs)
     free (internal_relocs);
 
   return FALSE;
 }
 
 /* This is a version of bfd_generic_get_relocated_section_contents
    which uses elf32_avr_relocate_section.
 
    For avr it's essentially a cut and paste taken from the H8300 port.
    The author of the relaxation support patch for avr had absolutely no
    clue what is happening here but found out that this part of the code
@@ -1912,88 +2262,735 @@
       if (symtab_hdr->sh_info != 0)
         {
           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
           if (isymbuf == NULL)
             isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
                                             symtab_hdr->sh_info, 0,
                                             NULL, NULL, NULL);
           if (isymbuf == NULL)
             goto error_return;
         }
 
       amt = symtab_hdr->sh_info;
       amt *= sizeof (asection *);
       sections = bfd_malloc (amt);
       if (sections == NULL && amt != 0)
         goto error_return;
 
       isymend = isymbuf + symtab_hdr->sh_info;
       for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
         {
           asection *isec;
 
           if (isym->st_shndx == SHN_UNDEF)
             isec = bfd_und_section_ptr;
           else if (isym->st_shndx == SHN_ABS)
             isec = bfd_abs_section_ptr;
           else if (isym->st_shndx == SHN_COMMON)
             isec = bfd_com_section_ptr;
           else
             isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
 
           *secpp = isec;
         }
 
       if (! elf32_avr_relocate_section (output_bfd, link_info, input_bfd,
                                         input_section, data, internal_relocs,
                                         isymbuf, sections))
         goto error_return;
 
       if (sections != NULL)
         free (sections);
       if (isymbuf != NULL
           && symtab_hdr->contents != (unsigned char *) isymbuf)
         free (isymbuf);
       if (elf_section_data (input_section)->relocs != internal_relocs)
         free (internal_relocs);
     }
 
   return data;
 
  error_return:
   if (sections != NULL)
     free (sections);
   if (isymbuf != NULL
       && symtab_hdr->contents != (unsigned char *) isymbuf)
     free (isymbuf);
   if (internal_relocs != NULL
       && elf_section_data (input_section)->relocs != internal_relocs)
     free (internal_relocs);
   return NULL;
 }
 
+/* Determines the hash entry name for a particular reloc. It consists of
+   the identifier of the symbol section and the added reloc addend and
+   symbol offset relative to the section the symbol is attached to.  */
+static char *
+avr_stub_name (const asection *symbol_section,
+               const bfd_vma symbol_offset,
+               const Elf_Internal_Rela *rela)
+{
+  char *stub_name;
+  bfd_size_type len;
+
+  len = 8 + 1 + 8 + 1 + 1;
+  stub_name = bfd_malloc (len);
+
+  sprintf (stub_name, "%08x+%08x",
+           symbol_section->id & 0xffffffff,
+           (unsigned int) ((rela->r_addend & 0xffffffff) + symbol_offset));
+
+  return stub_name;
+}
+
+
+/* Returns the address of the corresponding stub if there is one.  
+   Returns otherwise an address above 0x020000.  This function 
+   could also be used, if there is no knowledge on the section where
+   the destination is found.  */ 
+static bfd_vma
+avr_get_stub_addr (bfd_vma srel, 
+                   struct elf32_avr_link_hash_table *htab)
+{
+  bfd_vma stub_sec_addr = 
+              (htab->stub_sec->output_section->vma +
+	       htab->stub_sec->output_offset);
+
+  unsigned int index;
+  for (index = 0; index < htab->amt_max_entry_cnt; index ++)
+   {
+     if (htab->amt_destination_addr[index] == srel)
+       {
+         return htab->amt_stub_offsets[index] + stub_sec_addr;
+       }
+   }     
+
+  /* Return an address that could not be reached by 16 bit relocs.  */
+  return 0x020000;
+}
+
+/* Add a new stub entry to the stub hash.  Not all fields of the new
+   stub entry are initialised.  */
+
+static struct elf32_avr_stub_hash_entry *
+avr_add_stub (const char *stub_name,
+              struct elf32_avr_link_hash_table *htab)
+{
+  struct elf32_avr_stub_hash_entry *hsh;
+
+  /* Enter this entry into the linker stub hash table.  */
+  hsh = avr_stub_hash_lookup (&htab->bstab, stub_name, TRUE, FALSE);
+                               
+  if (hsh == NULL)
+    {
+      (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
+                             NULL,
+                             stub_name);
+      return NULL;
+    }
+
+  hsh->stub_offset = 0;
+  return hsh;
+}
+
+/* We assume that there is already space allocated for the stub section
+   contents and that before building the stubs the section size is
+   initialized to 0.  We assume that within the stub hash table entry,
+   the absolute position of the jmp target has been written in the
+   target_value field.  We write here the offset of the generated jmp insn
+   relative to the trampoline section start to the stub_offset entry in
+   the stub hash table entry.  */
+
+static  bfd_boolean
+avr_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
+{
+  struct elf32_avr_stub_hash_entry *hsh;
+  struct bfd_link_info *info;
+  struct elf32_avr_link_hash_table *htab;
+  bfd *stub_bfd;
+  bfd_byte *loc;
+  bfd_vma target;
+  bfd_vma starget;
+
+  /* Basic opcode */
+  bfd_vma jmp_insn = 0x0000940c;
+
+  /* Massage our args to the form they really have.  */
+  hsh = avr_stub_hash_entry (bh);
+
+  if (!hsh->is_actually_needed)
+    return TRUE;
+
+  info = (struct bfd_link_info *)in_arg;
+
+  htab = avr_link_hash_table (info);
+
+  target = hsh->target_value;
+
+  /* Make a note of the offset within the stubs for this entry.  */
+  hsh->stub_offset = htab->stub_sec->size;
+  loc = htab->stub_sec->contents + hsh->stub_offset;
+
+  stub_bfd = htab->stub_sec->owner;
+
+  if (debug_stubs)
+    printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
+             (unsigned int) target,
+             (unsigned int) hsh->stub_offset);
+
+  /* We now have to add the information on the jump target to the bare
+     opcode bits already set in jmp_insn.  */
+
+  /* Check for the alignment of the address.  */
+  if (target & 1)
+     return FALSE;
+
+  starget = target >> 1;
+  jmp_insn |= ((starget & 0x10000) | ((starget << 3) & 0x1f00000)) >> 16;
+  bfd_put_16 (stub_bfd, jmp_insn, loc);
+  bfd_put_16 (stub_bfd, (bfd_vma) starget & 0xffff, loc+2);
+
+  htab->stub_sec->size += 4;
+
+  /* Now add the entries in the address mapping table if there is still
+     space left.  */
+  { 
+    unsigned int nr;
+    nr = htab->amt_entry_cnt + 1;
+    if (nr <= htab->amt_max_entry_cnt)
+      {
+        htab->amt_entry_cnt = nr;
+   
+        htab->amt_stub_offsets[nr - 1] = hsh->stub_offset;
+        htab->amt_destination_addr[nr - 1] = target;
+      }
+  }
+
+  return TRUE;
+}
+
+static bfd_boolean
+avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry *bh, 
+                                   void *in_arg)
+{
+  struct elf32_avr_stub_hash_entry *hsh;
+  struct elf32_avr_link_hash_table *htab;
+  htab = in_arg;
+
+  hsh = avr_stub_hash_entry (bh);
+  hsh->is_actually_needed = FALSE;
+
+  return TRUE;
+}
+
+static bfd_boolean
+avr_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
+{
+  struct elf32_avr_stub_hash_entry *hsh;
+  struct elf32_avr_link_hash_table *htab;
+  int size;
+
+  /* Massage our args to the form they really have.  */
+  hsh = avr_stub_hash_entry (bh);
+  htab = in_arg;
+
+  if (hsh->is_actually_needed)
+    size = 4;
+  else 
+    size = 0;
+
+  htab->stub_sec->size += size;
+  return TRUE;
+}
+
+void
+elf32_avr_setup_params (struct bfd_link_info *info,
+                        bfd *avr_stub_bfd,
+                        asection *avr_stub_section,
+                        bfd_boolean no_stubs,
+                        bfd_boolean deb_stubs,
+                        bfd_boolean deb_relax,
+                        bfd_vma pc_wrap_around,
+                        bfd_boolean call_ret_replacement)
+{
+  struct elf32_avr_link_hash_table *htab = avr_link_hash_table(info);
+  htab->stub_sec = avr_stub_section;
+  htab->stub_bfd = avr_stub_bfd;
+  htab->no_stubs = no_stubs;
+ 
+  debug_relax = deb_relax; 
+  debug_stubs = deb_stubs; 
+  avr_pc_wrap_around = pc_wrap_around;
+  avr_replace_call_ret_sequences = call_ret_replacement;
+}
+
+
+/* Set up various things so that we can make a list of input sections
+   for each output section included in the link.  Returns -1 on error,
+   0 when no stubs will be needed, and 1 on success.  It also sets 
+   information on the stubs bfd and the stub section in the info
+   struct.  */
+   
+int
+elf32_avr_setup_section_lists (bfd *output_bfd, 
+                               struct bfd_link_info *info)
+{  
+  bfd *input_bfd;
+  unsigned int bfd_count;
+  int top_id, top_index;
+  asection *section;
+  asection **input_list, **list;
+  bfd_size_type amt;
+  struct elf32_avr_link_hash_table *htab = avr_link_hash_table(info);
+
+  if (htab->no_stubs)
+    return 0;
+  
+  /* Count the number of input BFDs and find the top input section id.  */
+  for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
+       input_bfd != NULL;
+       input_bfd = input_bfd->link_next)
+    {
+      bfd_count += 1;
+      for (section = input_bfd->sections;
+           section != NULL;
+           section = section->next)
+        {
+         if (top_id < section->id)
+             top_id = section->id;
+        }
+    }
+  htab->bfd_count = bfd_count;
+
+  /* We can't use output_bfd->section_count here to find the top output
+     section index as some sections may have been removed, and
+     strip_excluded_output_sections doesn't renumber the indices.  */
+  for (section = output_bfd->sections, top_index = 0;
+       section != NULL;
+       section = section->next)
+    {
+      if (top_index < section->index)
+        top_index = section->index;
+    }
+
+  htab->top_index = top_index;
+  amt = sizeof (asection *) * (top_index + 1);
+  input_list = bfd_malloc (amt);
+  htab->input_list = input_list;
+  if (input_list == NULL)
+    return -1;
+
+  /* For sections we aren't interested in, mark their entries with a
+     value we can check later.  */
+  list = input_list + top_index;
+  do
+    *list = bfd_abs_section_ptr;
+  while (list-- != input_list);
+
+  for (section = output_bfd->sections;
+       section != NULL;
+       section = section->next)
+    {
+      if ((section->flags & SEC_CODE) != 0)
+        input_list[section->index] = NULL;
+    }
+ 
+  return 1;
+}
+
+
+/* Read in all local syms for all input bfds, and create hash entries
+   for export stubs if we are building a multi-subspace shared lib.
+   Returns -1 on error, 0 otherwise.  */
+
+static int
+get_local_syms (bfd *input_bfd, struct bfd_link_info *info)
+{
+  unsigned int bfd_indx;
+  Elf_Internal_Sym *local_syms, **all_local_syms;
+  struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
+
+  /* We want to read in symbol extension records only once.  To do this
+     we need to read in the local symbols in parallel and save them for
+     later use; so hold pointers to the local symbols in an array.  */
+  bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
+  all_local_syms = bfd_zmalloc (amt);
+  htab->all_local_syms = all_local_syms;
+  if (all_local_syms == NULL)
+    return -1;
+
+  /* Walk over all the input BFDs, swapping in local symbols.
+     If we are creating a shared library, create hash entries for the
+     export stubs.  */
+  for (bfd_indx = 0;
+       input_bfd != NULL;
+       input_bfd = input_bfd->link_next, bfd_indx++)
+    {
+      Elf_Internal_Shdr *symtab_hdr;
+
+      /* We'll need the symbol table in a second.  */
+      symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
+      if (symtab_hdr->sh_info == 0)
+	continue;
+
+      /* We need an array of the local symbols attached to the input bfd.  */
+      local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
+      if (local_syms == NULL)
+	{
+	  local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
+					     symtab_hdr->sh_info, 0,
+					     NULL, NULL, NULL);
+	  /* Cache them for elf_link_input_bfd.  */
+	  symtab_hdr->contents = (unsigned char *) local_syms;
+	}
+      if (local_syms == NULL)
+	return -1;
+
+      all_local_syms[bfd_indx] = local_syms;
+    }
+
+  return 0;
+}
+
+#define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
+
+bfd_boolean 
+elf32_avr_size_stubs (bfd *output_bfd, 
+                      struct bfd_link_info *info,
+                      bfd_boolean is_prealloc_run)
+{
+ struct elf32_avr_link_hash_table *htab;
+ int stub_changed = 0;
+
+ htab = avr_link_hash_table (info);
+
+ /* At this point we initialize
+    htab->vector_base 
+    To the start of the text output section.  */
+ htab->vector_base = htab->stub_sec->output_section->vma;
+                    
+ if (get_local_syms (info->input_bfds, info))
+   {
+     if (htab->all_local_syms)
+       goto error_ret_free_local;
+     return FALSE;
+   }   
+   
+  if (ADD_DUMMY_STUBS_FOR_DEBUGGING)
+    {
+      struct elf32_avr_stub_hash_entry *test;
+      test = avr_add_stub ("Hugo",htab);
+      test->target_value = 0x123456;
+      test->stub_offset = 13;
+
+      test = avr_add_stub ("Hugo2",htab);
+      test->target_value = 0x84210;
+      test->stub_offset = 14;
+    }       
+
+  while (1)
+    {
+      bfd *input_bfd;
+      unsigned int bfd_indx;
+ 
+      /* We will have to re-generate the stub hash table each time anything
+         in memory has changed.  */
+    
+      bfd_hash_traverse (&htab->bstab, avr_mark_stub_not_to_be_necessary, htab); 
+      for (input_bfd = info->input_bfds, bfd_indx = 0;
+           input_bfd != NULL;
+           input_bfd = input_bfd->link_next, bfd_indx++)
+        {
+          Elf_Internal_Shdr *symtab_hdr;
+          asection *section;
+          Elf_Internal_Sym *local_syms;
+
+          /* We'll need the symbol table in a second.  */
+          symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
+          if (symtab_hdr->sh_info == 0)
+            continue;
+
+          local_syms = htab->all_local_syms[bfd_indx];
+
+          /* Walk over each section attached to the input bfd.  */
+          for (section = input_bfd->sections;
+               section != NULL;
+               section = section->next)
+            {
+              Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
+
+              /* If there aren't any relocs, then there's nothing more
+                 to do.  */
+              if ((section->flags & SEC_RELOC) == 0
+                  || section->reloc_count == 0)
+                continue;
+
+              /* If this section is a link-once section that will be
+                 discarded, then don't create any stubs.  */
+              if (section->output_section == NULL
+                  || section->output_section->owner != output_bfd)
+                continue;
+
+              /* Get the relocs.  */
+              internal_relocs
+                = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
+                                             info->keep_memory);
+              if (internal_relocs == NULL)
+                goto error_ret_free_local;
+
+              /* Now examine each relocation.  */
+              irela = internal_relocs;
+              irelaend = irela + section->reloc_count;
+              for (; irela < irelaend; irela++)
+                {
+                  unsigned int r_type, r_indx;
+                  struct elf32_avr_stub_hash_entry *hsh;
+                  asection *sym_sec;
+                  bfd_vma sym_value;
+                  bfd_vma destination;
+                  struct elf_link_hash_entry *hh;
+                  char *stub_name;
+
+                  r_type = ELF32_R_TYPE (irela->r_info);
+                  r_indx = ELF32_R_SYM (irela->r_info);
+
+                  /* Only look for 16 bit GS relocs. No other reloc will need a 
+                     stub.  */
+                  if (!((r_type == R_AVR_16_PM) 
+                        || (r_type == R_AVR_LO8_LDI_GS) 
+                        || (r_type == R_AVR_HI8_LDI_GS)))
+                    continue;
+
+                  /* Now determine the call target, its name, value,
+                     section.  */
+                  sym_sec = NULL;
+                  sym_value = 0;
+                  destination = 0;
+                  hh = NULL;
+                  if (r_indx < symtab_hdr->sh_info)
+                    {
+                      /* It's a local symbol.  */
+                      Elf_Internal_Sym *sym;
+                      Elf_Internal_Shdr *hdr;
+
+                      sym = local_syms + r_indx;
+                      hdr = elf_elfsections (input_bfd)[sym->st_shndx];
+                      sym_sec = hdr->bfd_section;
+                      if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
+                        sym_value = sym->st_value;
+                      destination = (sym_value + irela->r_addend
+                                     + sym_sec->output_offset
+                                     + sym_sec->output_section->vma);
+                    }
+                  else
+                    {
+                      /* It's an external symbol.  */
+                      int e_indx;
+
+                      e_indx = r_indx - symtab_hdr->sh_info;
+                      hh = elf_sym_hashes (input_bfd)[e_indx];
+
+                      while (hh->root.type == bfd_link_hash_indirect
+                             || hh->root.type == bfd_link_hash_warning)
+                        hh = (struct elf_link_hash_entry *) 
+                              (hh->root.u.i.link);
+
+                      if (hh->root.type == bfd_link_hash_defined
+                          || hh->root.type == bfd_link_hash_defweak)
+                        {
+                          sym_sec = hh->root.u.def.section;
+                          sym_value = hh->root.u.def.value;
+                          if (sym_sec->output_section != NULL)
+                          destination = (sym_value + irela->r_addend
+                                         + sym_sec->output_offset
+                                         + sym_sec->output_section->vma);
+                        }
+                      else if (hh->root.type == bfd_link_hash_undefweak)
+                        {
+                          if (! info->shared)
+                            continue;
+                        }
+                      else if (hh->root.type == bfd_link_hash_undefined)
+                        {
+                          if (! (info->unresolved_syms_in_objects == RM_IGNORE
+                                 && (ELF_ST_VISIBILITY (hh->other)
+                                     == STV_DEFAULT)))
+                             continue;
+                        }
+                      else
+                        {
+                          bfd_set_error (bfd_error_bad_value);
+
+                          error_ret_free_internal:
+                          if (elf_section_data (section)->relocs == NULL)
+                            free (internal_relocs);
+                          goto error_ret_free_local;
+                        }
+                    }
+
+                  if (!avr_stub_is_required_for_16_bit_reloc (
+                                             destination - htab->vector_base))
+                                      
+                    {
+                      if (!is_prealloc_run)
+                        {
+                          /* We are having a reloc that does't need a stub.  */
+                          continue;
+                        }
+                      else
+                        {
+                          /* We don't right now know if a stub will be needed.
+                             Let's rather be on the safe side.  */
+                        }
+                    }
+
+                  /* Get the name of this stub.  */
+                  stub_name = avr_stub_name (sym_sec, sym_value, irela);
+
+                  if (!stub_name)
+                    goto error_ret_free_internal;
+
+
+                  hsh = avr_stub_hash_lookup (&htab->bstab,
+                                              stub_name,
+                                              FALSE, FALSE);
+                  if (hsh != NULL)
+                    {
+                      /* The proper stub has already been created.  Mark it
+                         to be used and write the possibly changed destination
+                         value.  */
+                      hsh->is_actually_needed = TRUE;
+                      hsh->target_value = destination;
+                      free (stub_name);
+                      continue;
+                    }
+
+                  hsh = avr_add_stub (stub_name, htab);
+                  if (hsh == NULL)
+                    {
+                      free (stub_name);
+                      goto error_ret_free_internal;
+                    }
+
+                  hsh->is_actually_needed = TRUE;
+                  hsh->target_value = destination;
+
+                  if (debug_stubs)
+                    printf ("Adding stub with destination 0x%x to the"
+                            " hash table.\n", (unsigned int) destination); 
+                  if (debug_stubs)
+                    printf ("(Pre-Alloc run: %i)\n", is_prealloc_run);
+ 
+                  stub_changed = TRUE;
+                }
+
+              /* We're done with the internal relocs, free them.  */
+              if (elf_section_data (section)->relocs == NULL)
+                free (internal_relocs);
+            }
+        }
+
+      /* Re-Calculate the number of needed stubs.  */
+      htab->stub_sec->size = 0;
+      bfd_hash_traverse (&htab->bstab, avr_size_one_stub, htab); 
+
+      if (!stub_changed)
+        break;
+
+      stub_changed = FALSE;
+    }
+
+  free (htab->all_local_syms);
+  return TRUE;
+
+ return 1;
+
+ error_ret_free_local:
+  free (htab->all_local_syms);
+  return FALSE; 
+}
+
+
+/* Build all the stubs associated with the current output file.  The
+   stubs are kept in a hash table attached to the main linker hash
+   table.  We also set up the .plt entries for statically linked PIC
+   functions here.  This function is called via hppaelf_finish in the
+   linker.  */
+
+bfd_boolean
+elf32_avr_build_stubs (struct bfd_link_info *info)
+{
+  asection *stub_sec;
+  struct bfd_hash_table *table;
+  struct elf32_avr_link_hash_table *htab;
+  bfd_size_type total_size = 0;
+
+  htab = avr_link_hash_table (info);
+ 
+  /* In case that there were several stub sections:  */
+  for (stub_sec = htab->stub_bfd->sections;
+       stub_sec != NULL;
+       stub_sec = stub_sec->next)
+    {
+      bfd_size_type size;
+
+      /* Allocate memory to hold the linker stubs.  */
+      size = stub_sec->size;
+      total_size += size;
+
+      stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
+      if (stub_sec->contents == NULL && size != 0)
+	return FALSE;
+      stub_sec->size = 0;
+    }
+
+  /* Allocate memory for the adress mapping table.  */
+  htab->amt_entry_cnt = 0;
+  htab->amt_max_entry_cnt = total_size / 4;
+  htab->amt_stub_offsets = bfd_malloc (sizeof (bfd_vma) 
+                                       * htab->amt_max_entry_cnt);
+  htab->amt_destination_addr = 
+                        bfd_malloc (sizeof (bfd_vma) 
+                                    * htab->amt_max_entry_cnt );
+
+  if (debug_stubs)
+    printf ("Allocating %i entries in the AMT\n", htab->amt_max_entry_cnt);
+
+  /* Build the stubs as directed by the stub hash table.  */
+  table = &htab->bstab;
+  bfd_hash_traverse (table, avr_build_one_stub, info);
+
+  if (debug_stubs)
+    printf ("Final Stub section Size: %i\n", (int) htab->stub_sec->size);
+
+  return TRUE;
+}
+
 
 #define ELF_ARCH		bfd_arch_avr
 #define ELF_MACHINE_CODE	EM_AVR
 #define ELF_MACHINE_ALT1	EM_AVR_OLD
 #define ELF_MAXPAGESIZE		1
 
 #define TARGET_LITTLE_SYM       bfd_elf32_avr_vec
 #define TARGET_LITTLE_NAME	"elf32-avr"
 
+#define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
+#define bfd_elf32_bfd_link_hash_table_free   elf32_avr_link_hash_table_free
+
 #define elf_info_to_howto	             avr_info_to_howto_rela
 #define elf_info_to_howto_rel	             NULL
 #define elf_backend_relocate_section         elf32_avr_relocate_section
 #define elf_backend_gc_mark_hook             elf32_avr_gc_mark_hook
 #define elf_backend_gc_sweep_hook            elf32_avr_gc_sweep_hook
 #define elf_backend_check_relocs             elf32_avr_check_relocs
 #define elf_backend_can_gc_sections          1
 #define elf_backend_rela_normal		     1
 #define elf_backend_final_write_processing \
 					bfd_elf_avr_final_write_processing
 #define elf_backend_object_p		elf32_avr_object_p
 
 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
 #define bfd_elf32_bfd_get_relocated_section_contents \
                                         elf32_avr_get_relocated_section_contents
 
 #include "elf32-target.h"
Index: bfd/libbfd.h
===================================================================
RCS file: /cvs/src/src/bfd/libbfd.h,v
retrieving revision 1.181
diff -U62 -r1.181 libbfd.h
--- bfd/libbfd.h	11 May 2006 15:17:34 -0000	1.181
+++ bfd/libbfd.h	12 May 2006 21:43:09 -0000
@@ -1460,125 +1460,127 @@
   "BFD_RELOC_V850_LONGCALL",
   "BFD_RELOC_V850_LONGJUMP",
   "BFD_RELOC_V850_ALIGN",
   "BFD_RELOC_V850_LO16_SPLIT_OFFSET",
   "BFD_RELOC_MN10300_32_PCREL",
   "BFD_RELOC_MN10300_16_PCREL",
   "BFD_RELOC_TIC30_LDP",
   "BFD_RELOC_TIC54X_PARTLS7",
   "BFD_RELOC_TIC54X_PARTMS9",
   "BFD_RELOC_TIC54X_23",
   "BFD_RELOC_TIC54X_16_OF_23",
   "BFD_RELOC_TIC54X_MS7_OF_23",
   "BFD_RELOC_FR30_48",
   "BFD_RELOC_FR30_20",
   "BFD_RELOC_FR30_6_IN_4",
   "BFD_RELOC_FR30_8_IN_8",
   "BFD_RELOC_FR30_9_IN_8",
   "BFD_RELOC_FR30_10_IN_8",
   "BFD_RELOC_FR30_9_PCREL",
   "BFD_RELOC_FR30_12_PCREL",
   "BFD_RELOC_MCORE_PCREL_IMM8BY4",
   "BFD_RELOC_MCORE_PCREL_IMM11BY2",
   "BFD_RELOC_MCORE_PCREL_IMM4BY2",
   "BFD_RELOC_MCORE_PCREL_32",
   "BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2",
   "BFD_RELOC_MCORE_RVA",
   "BFD_RELOC_MMIX_GETA",
   "BFD_RELOC_MMIX_GETA_1",
   "BFD_RELOC_MMIX_GETA_2",
   "BFD_RELOC_MMIX_GETA_3",
   "BFD_RELOC_MMIX_CBRANCH",
   "BFD_RELOC_MMIX_CBRANCH_J",
   "BFD_RELOC_MMIX_CBRANCH_1",
   "BFD_RELOC_MMIX_CBRANCH_2",
   "BFD_RELOC_MMIX_CBRANCH_3",
   "BFD_RELOC_MMIX_PUSHJ",
   "BFD_RELOC_MMIX_PUSHJ_1",
   "BFD_RELOC_MMIX_PUSHJ_2",
   "BFD_RELOC_MMIX_PUSHJ_3",
   "BFD_RELOC_MMIX_PUSHJ_STUBBABLE",
   "BFD_RELOC_MMIX_JMP",
   "BFD_RELOC_MMIX_JMP_1",
   "BFD_RELOC_MMIX_JMP_2",
   "BFD_RELOC_MMIX_JMP_3",
   "BFD_RELOC_MMIX_ADDR19",
   "BFD_RELOC_MMIX_ADDR27",
   "BFD_RELOC_MMIX_REG_OR_BYTE",
   "BFD_RELOC_MMIX_REG",
   "BFD_RELOC_MMIX_BASE_PLUS_OFFSET",
   "BFD_RELOC_MMIX_LOCAL",
   "BFD_RELOC_AVR_7_PCREL",
   "BFD_RELOC_AVR_13_PCREL",
   "BFD_RELOC_AVR_16_PM",
   "BFD_RELOC_AVR_LO8_LDI",
   "BFD_RELOC_AVR_HI8_LDI",
   "BFD_RELOC_AVR_HH8_LDI",
   "BFD_RELOC_AVR_MS8_LDI",
   "BFD_RELOC_AVR_LO8_LDI_NEG",
   "BFD_RELOC_AVR_HI8_LDI_NEG",
   "BFD_RELOC_AVR_HH8_LDI_NEG",
   "BFD_RELOC_AVR_MS8_LDI_NEG",
   "BFD_RELOC_AVR_LO8_LDI_PM",
+  "BFD_RELOC_AVR_LO8_LDI_GS",
   "BFD_RELOC_AVR_HI8_LDI_PM",
+  "BFD_RELOC_AVR_HI8_LDI_GS",
   "BFD_RELOC_AVR_HH8_LDI_PM",
   "BFD_RELOC_AVR_LO8_LDI_PM_NEG",
   "BFD_RELOC_AVR_HI8_LDI_PM_NEG",
   "BFD_RELOC_AVR_HH8_LDI_PM_NEG",
   "BFD_RELOC_AVR_CALL",
   "BFD_RELOC_AVR_LDI",
   "BFD_RELOC_AVR_6",
   "BFD_RELOC_AVR_6_ADIW",
   "BFD_RELOC_390_12",
   "BFD_RELOC_390_GOT12",
   "BFD_RELOC_390_PLT32",
   "BFD_RELOC_390_COPY",
   "BFD_RELOC_390_GLOB_DAT",
   "BFD_RELOC_390_JMP_SLOT",
   "BFD_RELOC_390_RELATIVE",
   "BFD_RELOC_390_GOTPC",
   "BFD_RELOC_390_GOT16",
   "BFD_RELOC_390_PC16DBL",
   "BFD_RELOC_390_PLT16DBL",
   "BFD_RELOC_390_PC32DBL",
   "BFD_RELOC_390_PLT32DBL",
   "BFD_RELOC_390_GOTPCDBL",
   "BFD_RELOC_390_GOT64",
   "BFD_RELOC_390_PLT64",
   "BFD_RELOC_390_GOTENT",
   "BFD_RELOC_390_GOTOFF64",
   "BFD_RELOC_390_GOTPLT12",
   "BFD_RELOC_390_GOTPLT16",
   "BFD_RELOC_390_GOTPLT32",
   "BFD_RELOC_390_GOTPLT64",
   "BFD_RELOC_390_GOTPLTENT",
   "BFD_RELOC_390_PLTOFF16",
   "BFD_RELOC_390_PLTOFF32",
   "BFD_RELOC_390_PLTOFF64",
   "BFD_RELOC_390_TLS_LOAD",
   "BFD_RELOC_390_TLS_GDCALL",
   "BFD_RELOC_390_TLS_LDCALL",
   "BFD_RELOC_390_TLS_GD32",
   "BFD_RELOC_390_TLS_GD64",
   "BFD_RELOC_390_TLS_GOTIE12",
   "BFD_RELOC_390_TLS_GOTIE32",
   "BFD_RELOC_390_TLS_GOTIE64",
   "BFD_RELOC_390_TLS_LDM32",
   "BFD_RELOC_390_TLS_LDM64",
   "BFD_RELOC_390_TLS_IE32",
   "BFD_RELOC_390_TLS_IE64",
   "BFD_RELOC_390_TLS_IEENT",
   "BFD_RELOC_390_TLS_LE32",
   "BFD_RELOC_390_TLS_LE64",
   "BFD_RELOC_390_TLS_LDO32",
   "BFD_RELOC_390_TLS_LDO64",
   "BFD_RELOC_390_TLS_DTPMOD",
   "BFD_RELOC_390_TLS_DTPOFF",
   "BFD_RELOC_390_TLS_TPOFF",
   "BFD_RELOC_390_20",
   "BFD_RELOC_390_GOT20",
   "BFD_RELOC_390_GOTPLT20",
   "BFD_RELOC_390_TLS_GOTIE20",
   "BFD_RELOC_IP2K_FR9",
   "BFD_RELOC_IP2K_BANK",
   "BFD_RELOC_IP2K_ADDR16CJP",
   "BFD_RELOC_IP2K_PAGE3",
Index: bfd/reloc.c
===================================================================
RCS file: /cvs/src/src/bfd/reloc.c,v
retrieving revision 1.153
diff -U62 -r1.153 reloc.c
--- bfd/reloc.c	11 May 2006 15:17:34 -0000	1.153
+++ bfd/reloc.c	12 May 2006 21:43:10 -0000
@@ -3625,129 +3625,143 @@
   BFD_RELOC_AVR_7_PCREL
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores 8 bit pc relative
   short offset into 7 bits.
 ENUM
   BFD_RELOC_AVR_13_PCREL
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores 13 bit pc relative
   short offset into 12 bits.
 ENUM
   BFD_RELOC_AVR_16_PM
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores 17 bit value (usually
   program memory address) into 16 bits.
 ENUM
   BFD_RELOC_AVR_LO8_LDI
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores 8 bit value (usually
   data memory address) into 8 bit immediate value of LDI insn.
 ENUM
   BFD_RELOC_AVR_HI8_LDI
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores 8 bit value (high 8 bit
   of data memory address) into 8 bit immediate value of LDI insn.
 ENUM
   BFD_RELOC_AVR_HH8_LDI
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores 8 bit value (most high 8 bit
   of program memory address) into 8 bit immediate value of LDI insn.
 ENUM
   BFD_RELOC_AVR_MS8_LDI
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores 8 bit value (most high 8 bit
   of 32 bit value) into 8 bit immediate value of LDI insn.
 ENUM
   BFD_RELOC_AVR_LO8_LDI_NEG
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores negated 8 bit value
   (usually data memory address) into 8 bit immediate value of SUBI insn.
 ENUM
   BFD_RELOC_AVR_HI8_LDI_NEG
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores negated 8 bit value
   (high 8 bit of data memory address) into 8 bit immediate value of
   SUBI insn.
 ENUM
   BFD_RELOC_AVR_HH8_LDI_NEG
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores negated 8 bit value
   (most high 8 bit of program memory address) into 8 bit immediate value
   of LDI or SUBI insn.
 ENUM
   BFD_RELOC_AVR_MS8_LDI_NEG
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores negated 8 bit value (msb
   of 32 bit value) into 8 bit immediate value of LDI insn.
 ENUM
   BFD_RELOC_AVR_LO8_LDI_PM
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores 8 bit value (usually
   command address) into 8 bit immediate value of LDI insn.
 ENUM
+  BFD_RELOC_AVR_LO8_LDI_GS
+ENUMDOC
+  This is a 16 bit reloc for the AVR that stores 8 bit value 
+  (command address) into 8 bit immediate value of LDI insn. If the address
+  is beyond the 128k boundary, the linker inserts a jump stub for this reloc
+  in the lower 128k.
+ENUM
   BFD_RELOC_AVR_HI8_LDI_PM
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores 8 bit value (high 8 bit
   of command address) into 8 bit immediate value of LDI insn.
 ENUM
+  BFD_RELOC_AVR_HI8_LDI_GS
+ENUMDOC
+  This is a 16 bit reloc for the AVR that stores 8 bit value (high 8 bit
+  of command address) into 8 bit immediate value of LDI insn.  If the address
+  is beyond the 128k boundary, the linker inserts a jump stub for this reloc
+  below 128k.
+ENUM
   BFD_RELOC_AVR_HH8_LDI_PM
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores 8 bit value (most high 8 bit
   of command address) into 8 bit immediate value of LDI insn.
 ENUM
   BFD_RELOC_AVR_LO8_LDI_PM_NEG
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores negated 8 bit value
   (usually command address) into 8 bit immediate value of SUBI insn.
 ENUM
   BFD_RELOC_AVR_HI8_LDI_PM_NEG
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores negated 8 bit value
   (high 8 bit of 16 bit command address) into 8 bit immediate value
   of SUBI insn.
 ENUM
   BFD_RELOC_AVR_HH8_LDI_PM_NEG
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores negated 8 bit value
   (high 6 bit of 22 bit command address) into 8 bit immediate
   value of SUBI insn.
 ENUM
   BFD_RELOC_AVR_CALL
 ENUMDOC
   This is a 32 bit reloc for the AVR that stores 23 bit value
   into 22 bits.
 ENUM
   BFD_RELOC_AVR_LDI
 ENUMDOC
   This is a 16 bit reloc for the AVR that stores all needed bits
   for absolute addressing with ldi with overflow check to linktime
 ENUM
   BFD_RELOC_AVR_6
 ENUMDOC
   This is a 6 bit reloc for the AVR that stores offset for ldd/std
   instructions
 ENUM
   BFD_RELOC_AVR_6_ADIW
 ENUMDOC
   This is a 6 bit reloc for the AVR that stores offset for adiw/sbiw
   instructions
 
 ENUM
   BFD_RELOC_390_12
 ENUMDOC
    Direct 12 bit.
 ENUM
   BFD_RELOC_390_GOT12
 ENUMDOC
   12 bit GOT offset.
 ENUM
   BFD_RELOC_390_PLT32
 ENUMDOC
   32 bit PC relative PLT address.
 ENUM
   BFD_RELOC_390_COPY
 ENUMDOC
   Copy symbol at runtime.
 ENUM
   BFD_RELOC_390_GLOB_DAT
 ENUMDOC
   Create GOT entry.
Index: gas/config/tc-avr.c
===================================================================
RCS file: /cvs/src/src/gas/config/tc-avr.c,v
retrieving revision 1.32
diff -U62 -r1.32 tc-avr.c
--- gas/config/tc-avr.c	7 Apr 2006 15:18:08 -0000	1.32
+++ gas/config/tc-avr.c	12 May 2006 21:43:11 -0000
@@ -9,202 +9,205 @@
    GAS is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2, or (at your option)
    any later version.
 
    GAS is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with GAS; see the file COPYING.  If not, write to
    the Free Software Foundation, 51 Franklin Street - Fifth Floor,
    Boston, MA 02110-1301, USA.  */
 
 #include <stdio.h>
 #include "as.h"
 #include "safe-ctype.h"
 #include "subsegs.h"
 #include "libiberty.h"
 
 struct avr_opcodes_s
 {
   char *        name;
   char *        constraints;
   int           insn_size;		/* In words.  */
   int           isa;
   unsigned int  bin_opcode;
 };
 
 #define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \
 {#NAME, CONSTR, SIZE, ISA, BIN},
 
 struct avr_opcodes_s avr_opcodes[] =
 {
   #include "opcode/avr.h"
   {NULL, NULL, 0, 0, 0}
 };
 
 const char comment_chars[] = ";";
 const char line_comment_chars[] = "#";
 const char line_separator_chars[] = "$";
 
 const char *md_shortopts = "m:";
 struct mcu_type_s
 {
   char *name;
   int isa;
   int mach;
 };
 
 /* XXX - devices that don't seem to exist (renamed, replaced with larger
    ones, or planned but never produced), left here for compatibility.
    TODO: hide them in show_mcu_list output?  */
 
 static struct mcu_type_s mcu_types[] =
 {
   {"avr1",      AVR_ISA_TINY1,    bfd_mach_avr1},
   {"avr2",      AVR_ISA_TINY2,    bfd_mach_avr2},
   {"avr3",      AVR_ISA_M103,     bfd_mach_avr3},
   {"avr4",      AVR_ISA_M8,       bfd_mach_avr4},
   {"avr5",      AVR_ISA_ALL,      bfd_mach_avr5},
+  {"avr6",      AVR_ISA_ALL,      bfd_mach_avr6},
   {"at90s1200", AVR_ISA_1200,     bfd_mach_avr1},
   {"attiny10",  AVR_ISA_TINY1,    bfd_mach_avr1}, /* XXX -> tn11 */
   {"attiny11",  AVR_ISA_TINY1,    bfd_mach_avr1},
   {"attiny12",  AVR_ISA_TINY1,    bfd_mach_avr1},
   {"attiny15",  AVR_ISA_TINY1,    bfd_mach_avr1},
   {"attiny28",  AVR_ISA_TINY1,    bfd_mach_avr1},
   {"at90s2313", AVR_ISA_2xxx,     bfd_mach_avr2},
   {"at90s2323", AVR_ISA_2xxx,     bfd_mach_avr2},
   {"at90s2333", AVR_ISA_2xxx,     bfd_mach_avr2}, /* XXX -> 4433 */
   {"at90s2343", AVR_ISA_2xxx,     bfd_mach_avr2},
   {"attiny22",  AVR_ISA_2xxx,     bfd_mach_avr2}, /* XXX -> 2343 */
   {"attiny26",  AVR_ISA_2xxx,     bfd_mach_avr2},
   {"at90s4433", AVR_ISA_2xxx,     bfd_mach_avr2},
   {"at90s4414", AVR_ISA_2xxx,     bfd_mach_avr2}, /* XXX -> 8515 */
   {"at90s4434", AVR_ISA_2xxx,     bfd_mach_avr2}, /* XXX -> 8535 */
   {"at90s8515", AVR_ISA_2xxx,     bfd_mach_avr2},
   {"at90s8535", AVR_ISA_2xxx,     bfd_mach_avr2},
   {"at90c8534", AVR_ISA_2xxx,     bfd_mach_avr2},
   {"at86rf401", AVR_ISA_2xxx,     bfd_mach_avr2},
   {"attiny13",  AVR_ISA_TINY2,    bfd_mach_avr2},
   {"attiny2313",AVR_ISA_TINY2,    bfd_mach_avr2},
   {"attiny261", AVR_ISA_TINY2,    bfd_mach_avr2},
   {"attiny461", AVR_ISA_TINY2,    bfd_mach_avr2},
   {"attiny861", AVR_ISA_TINY2,    bfd_mach_avr2},
   {"attiny24",  AVR_ISA_TINY2,    bfd_mach_avr2},
   {"attiny44",  AVR_ISA_TINY2,    bfd_mach_avr2},
   {"attiny84",  AVR_ISA_TINY2,    bfd_mach_avr2},
   {"attiny25",  AVR_ISA_TINY2,    bfd_mach_avr2},
   {"attiny45",  AVR_ISA_TINY2,    bfd_mach_avr2},
   {"attiny85",  AVR_ISA_TINY2,    bfd_mach_avr2},
   {"atmega603", AVR_ISA_M603,     bfd_mach_avr3}, /* XXX -> m103 */
   {"atmega103", AVR_ISA_M103,     bfd_mach_avr3},
   {"at43usb320",AVR_ISA_M103,     bfd_mach_avr3},
   {"at43usb355",AVR_ISA_M603,     bfd_mach_avr3},
   {"at76c711",  AVR_ISA_M603,     bfd_mach_avr3},
   {"atmega48",  AVR_ISA_PWMx,     bfd_mach_avr4},
   {"atmega8",   AVR_ISA_M8,       bfd_mach_avr4},
   {"atmega83",  AVR_ISA_M8,       bfd_mach_avr4}, /* XXX -> m8535 */
   {"atmega85",  AVR_ISA_M8,       bfd_mach_avr4}, /* XXX -> m8 */
   {"atmega88",  AVR_ISA_PWMx,     bfd_mach_avr4},
   {"atmega8515",AVR_ISA_M8,       bfd_mach_avr4},
   {"atmega8535",AVR_ISA_M8,       bfd_mach_avr4},
   {"at90pwm2",  AVR_ISA_PWMx,     bfd_mach_avr4},
   {"at90pwm3",  AVR_ISA_PWMx,     bfd_mach_avr4},
   {"atmega16",  AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega161", AVR_ISA_M161,     bfd_mach_avr5},
   {"atmega162", AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega163", AVR_ISA_M161,     bfd_mach_avr5},
   {"atmega164", AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega165", AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega168", AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega169", AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega32",  AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega323", AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega324", AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega325", AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega329", AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega3250",AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega3290",AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega406", AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega64",  AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega640", AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega644", AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega128", AVR_ISA_M128,     bfd_mach_avr5},
   {"atmega1280",AVR_ISA_M128,     bfd_mach_avr5},
   {"atmega1281",AVR_ISA_M128,     bfd_mach_avr5},
   {"atmega645", AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega649", AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega6450",AVR_ISA_M323,     bfd_mach_avr5},
   {"atmega6490",AVR_ISA_M323,     bfd_mach_avr5},
   {"at90can32" ,AVR_ISA_M323,     bfd_mach_avr5},
   {"at90can64" ,AVR_ISA_M323,     bfd_mach_avr5},
   {"at90can128",AVR_ISA_M128,     bfd_mach_avr5},
   {"at90usb646", AVR_ISA_M323,    bfd_mach_avr5},
   {"at90usb647", AVR_ISA_M323,    bfd_mach_avr5},
   {"at90usb1286",AVR_ISA_M128,    bfd_mach_avr5},
   {"at90usb1287",AVR_ISA_M128,    bfd_mach_avr5},
   {"at94k",     AVR_ISA_94K,      bfd_mach_avr5},
+  {"atmega2560", AVR_ISA_ALL,     bfd_mach_avr6},
+  {"atmega2561", AVR_ISA_ALL,     bfd_mach_avr6},
   {NULL, 0, 0}
 };
 
 /* Current MCU type.  */
 static struct mcu_type_s   default_mcu = {"avr2", AVR_ISA_2xxx,bfd_mach_avr2};
 static struct mcu_type_s * avr_mcu = & default_mcu;
 
 /* AVR target-specific switches.  */
 struct avr_opt_s
 {
   int all_opcodes;  /* -mall-opcodes: accept all known AVR opcodes.  */
   int no_skip_bug;  /* -mno-skip-bug: no warnings for skipping 2-word insns.  */
   int no_wrap;      /* -mno-wrap: reject rjmp/rcall with 8K wrap-around.  */
 };
 
 static struct avr_opt_s avr_opt = { 0, 0, 0 };
 
 const char EXP_CHARS[] = "eE";
 const char FLT_CHARS[] = "dD";
 
 static void avr_set_arch (int);
 
 /* The target specific pseudo-ops which we support.  */
 const pseudo_typeS md_pseudo_table[] =
 {
   {"arch", avr_set_arch,	0},
   { NULL,	NULL,		0}
 };
 
 #define LDI_IMMEDIATE(x) (((x) & 0xf) | (((x) << 4) & 0xf00))
 
 #define EXP_MOD_NAME(i)       exp_mod[i].name
 #define EXP_MOD_RELOC(i)      exp_mod[i].reloc
 #define EXP_MOD_NEG_RELOC(i)  exp_mod[i].neg_reloc
 #define HAVE_PM_P(i)          exp_mod[i].have_pm
 
 struct exp_mod_s
 {
   char *                    name;
   bfd_reloc_code_real_type  reloc;
   bfd_reloc_code_real_type  neg_reloc;
   int                       have_pm;
 };
 
 static struct exp_mod_s exp_mod[] =
 {
   {"hh8",    BFD_RELOC_AVR_HH8_LDI,    BFD_RELOC_AVR_HH8_LDI_NEG,    1},
   {"pm_hh8", BFD_RELOC_AVR_HH8_LDI_PM, BFD_RELOC_AVR_HH8_LDI_PM_NEG, 0},
   {"hi8",    BFD_RELOC_AVR_HI8_LDI,    BFD_RELOC_AVR_HI8_LDI_NEG,    1},
   {"pm_hi8", BFD_RELOC_AVR_HI8_LDI_PM, BFD_RELOC_AVR_HI8_LDI_PM_NEG, 0},
   {"lo8",    BFD_RELOC_AVR_LO8_LDI,    BFD_RELOC_AVR_LO8_LDI_NEG,    1},
   {"pm_lo8", BFD_RELOC_AVR_LO8_LDI_PM, BFD_RELOC_AVR_LO8_LDI_PM_NEG, 0},
   {"hlo8",   BFD_RELOC_AVR_HH8_LDI,    BFD_RELOC_AVR_HH8_LDI_NEG,    0},
   {"hhi8",   BFD_RELOC_AVR_MS8_LDI,    BFD_RELOC_AVR_MS8_LDI_NEG,    0},
 };
 
 /* A union used to store indicies into the exp_mod[] array
    in a hash table which expects void * data types.  */
 typedef union
 {
   void * ptr;
   int    index;
@@ -487,189 +490,217 @@
   if (ex.X_op != O_constant)
     as_bad (_("constant value required"));
 
   if (ex.X_add_number > max || ex.X_add_number < 0)
     as_bad (_("number must be less than %d"), max + 1);
 
   return ex.X_add_number;
 }
 
 /* Parse for ldd/std offset.  */
 
 static void
 avr_offset_expression (expressionS *exp)
 {
   char *str = input_line_pointer;
   char *tmp;
   char op[8];
 
   tmp = str;
   str = extract_word (str, op, sizeof (op));
 
   input_line_pointer = tmp;
   expression (exp);
 
   /* Warn about expressions that fail to use lo8 ().  */
   if (exp->X_op == O_constant)
     {
       int x = exp->X_add_number;
       
       if (x < -255 || x > 255)
 	as_warn (_("constant out of 8-bit range: %d"), x);
     }
 }
 
 /* Parse ordinary expression.  */
 
 static char *
 parse_exp (char *s, expressionS *op)
 {
   input_line_pointer = s;
   expression (op);
   if (op->X_op == O_absent)
     as_bad (_("missing operand"));
   return input_line_pointer;
 }
 
 /* Parse special expressions (needed for LDI command):
    xx8 (address)
    xx8 (-address)
    pm_xx8 (address)
    pm_xx8 (-address)
    where xx is: hh, hi, lo.  */
 
 static bfd_reloc_code_real_type
 avr_ldi_expression (expressionS *exp)
 {
   char *str = input_line_pointer;
   char *tmp;
   char op[8];
   int mod;
   tmp = str;
 
+  int linker_stubs_should_be_generated = 0;
+
   str = extract_word (str, op, sizeof (op));
 
   if (op[0])
     {
       mod_index m;
       
       m.ptr = hash_find (avr_mod_hash, op);
       mod = m.index;
 
       if (mod)
 	{
 	  int closes = 0;
 
 	  mod -= 10;
 	  str = skip_space (str);
 
 	  if (*str == '(')
 	    {
 	      int neg_p = 0;
 
 	      ++str;
 
 	      if (strncmp ("pm(", str, 3) == 0
+                  || strncmp ("gs(",str,3) == 0
+                  || strncmp ("-(gs(",str,5) == 0
 		  || strncmp ("-(pm(", str, 5) == 0)
 		{
 		  if (HAVE_PM_P (mod))
 		    {
 		      ++mod;
 		      ++closes;
 		    }
 		  else
 		    as_bad (_("illegal expression"));
 
+                  if (str[0] == 'g'
+                      || str[2] == 'g')
+                    linker_stubs_should_be_generated = 1;
+
 		  if (*str == '-')
 		    {
 		      neg_p = 1;
 		      ++closes;
 		      str += 5;
 		    }
 		  else
 		    str += 3;
 		}
 
 	      if (*str == '-' && *(str + 1) == '(')
 		{
 		  neg_p ^= 1;
 		  ++closes;
 		  str += 2;
 		}
 
 	      input_line_pointer = str;
 	      expression (exp);
 
 	      do
 		{
 		  if (*input_line_pointer != ')')
 		    {
 		      as_bad (_("`)' required"));
 		      break;
 		    }
 		  input_line_pointer++;
 		}
 	      while (closes--);
 
-	      return neg_p ? EXP_MOD_NEG_RELOC (mod) : EXP_MOD_RELOC (mod);
+              { 
+                 bfd_reloc_code_real_type  reloc_to_return;
+	         reloc_to_return = 
+                    neg_p ? EXP_MOD_NEG_RELOC (mod) : EXP_MOD_RELOC (mod);
+                 if (linker_stubs_should_be_generated)
+                   {
+                     switch (reloc_to_return)
+                      {
+                        case BFD_RELOC_AVR_LO8_LDI_PM:
+                          reloc_to_return = BFD_RELOC_AVR_LO8_LDI_GS;
+                          break;
+                        case BFD_RELOC_AVR_HI8_LDI_PM:
+                          reloc_to_return = BFD_RELOC_AVR_HI8_LDI_GS;
+                          break;
+
+                        default:
+		          as_warn (_("expression dangerous with linker stubs"));
+                      }
+                   } 
+                 return reloc_to_return;
+              }
 	    }
 	}
     }
 
   input_line_pointer = tmp;
   expression (exp);
 
   /* Warn about expressions that fail to use lo8 ().  */
   if (exp->X_op == O_constant)
     {
       int x = exp->X_add_number;
 
       if (x < -255 || x > 255)
 	as_warn (_("constant out of 8-bit range: %d"), x);
     }
 
   return BFD_RELOC_AVR_LDI;
 }
 
 /* Parse one instruction operand.
    Return operand bitmask.  Also fixups can be generated.  */
 
 static unsigned int
 avr_operand (struct avr_opcodes_s *opcode,
 	     int where,
 	     char *op,
 	     char **line)
 {
   expressionS op_expr;
   unsigned int op_mask = 0;
   char *str = skip_space (*line);
 
   switch (*op)
     {
       /* Any register operand.  */
     case 'w':
     case 'd':
     case 'r':
     case 'a':
     case 'v':
       if (*str == 'r' || *str == 'R')
 	{
 	  char r_name[20];
 
 	  str = extract_word (str, r_name, sizeof (r_name));
 	  op_mask = 0xff;
 	  if (ISDIGIT (r_name[1]))
 	    {
 	      if (r_name[2] == '\0')
 		op_mask = r_name[1] - '0';
 	      else if (r_name[1] != '0'
 		       && ISDIGIT (r_name[2])
 		       && r_name[3] == '\0')
 		op_mask = (r_name[1] - '0') * 10 + r_name[2] - '0';
 	    }
 	}
       else
 	{
 	  op_mask = avr_get_constant (str, 31);
 	  str = input_line_pointer;
 	}
 
@@ -1251,131 +1282,135 @@
   if (reloc->howto == (reloc_howto_type *) NULL)
     {
       as_bad_where (fixp->fx_file, fixp->fx_line,
 		    _("reloc %d not supported by object file format"),
 		    (int) fixp->fx_r_type);
       return NULL;
     }
 
   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
     reloc->address = fixp->fx_offset;
 
   reloc->addend = fixp->fx_offset;
 
   return reloc;
 }
 
 void
 md_assemble (char *str)
 {
   struct avr_opcodes_s *opcode;
   char op[11];
 
   str = skip_space (extract_word (str, op, sizeof (op)));
 
   if (!op[0])
     as_bad (_("can't find opcode "));
 
   opcode = (struct avr_opcodes_s *) hash_find (avr_hash, op);
 
   if (opcode == NULL)
     {
       as_bad (_("unknown opcode `%s'"), op);
       return;
     }
 
   /* Special case for opcodes with optional operands (lpm, elpm) -
      version with operands exists in avr_opcodes[] in the next entry.  */
 
   if (*str && *opcode->constraints == '?')
     ++opcode;
 
   if (!avr_opt.all_opcodes && (opcode->isa & avr_mcu->isa) != opcode->isa)
     as_bad (_("illegal opcode %s for mcu %s"), opcode->name, avr_mcu->name);
 
   /* We used to set input_line_pointer to the result of get_operands,
      but that is wrong.  Our caller assumes we don't change it.  */
   {
     char *t = input_line_pointer;
 
     avr_operands (opcode, &str);
     if (*skip_space (str))
       as_bad (_("garbage at end of line"));
     input_line_pointer = t;
   }
 }
 
 /* Flag to pass `pm' mode between `avr_parse_cons_expression' and
    `avr_cons_fix_new'.  */
 static int exp_mod_pm = 0;
 
 /* Parse special CONS expression: pm (expression)
+   or alternatively gs (expression).
    which is used for addressing to a program memory.
    Relocation: BFD_RELOC_AVR_16_PM.  */
 
 void
 avr_parse_cons_expression (expressionS *exp, int nbytes)
 {
   char *tmp;
 
   exp_mod_pm = 0;
 
   tmp = input_line_pointer = skip_space (input_line_pointer);
 
   if (nbytes == 2)
     {
-      char *pm_name = "pm";
-      int len = strlen (pm_name);
+      char *pm_name1 = "pm";
+      char *pm_name2 = "gs";
+      int len = strlen (pm_name1);
+      /* len must be the same for both pm identifiers.  */
 
-      if (strncasecmp (input_line_pointer, pm_name, len) == 0)
+      if (strncasecmp (input_line_pointer, pm_name1, len) == 0
+          || strncasecmp (input_line_pointer, pm_name2, len) == 0)
 	{
 	  input_line_pointer = skip_space (input_line_pointer + len);
 
 	  if (*input_line_pointer == '(')
 	    {
 	      input_line_pointer = skip_space (input_line_pointer + 1);
 	      exp_mod_pm = 1;
 	      expression (exp);
 
 	      if (*input_line_pointer == ')')
 		++input_line_pointer;
 	      else
 		{
 		  as_bad (_("`)' required"));
 		  exp_mod_pm = 0;
 		}
 
 	      return;
 	    }
 
 	  input_line_pointer = tmp;
 	}
     }
 
   expression (exp);
 }
 
 void
 avr_cons_fix_new (fragS *frag,
 		  int where,
 		  int nbytes,
 		  expressionS *exp)
 {
   if (exp_mod_pm == 0)
     {
       if (nbytes == 2)
 	fix_new_exp (frag, where, nbytes, exp, FALSE, BFD_RELOC_16);
       else if (nbytes == 4)
 	fix_new_exp (frag, where, nbytes, exp, FALSE, BFD_RELOC_32);
       else
 	as_bad (_("illegal %srelocation size: %d"), "", nbytes);
     }
   else
     {
       if (nbytes == 2)
 	fix_new_exp (frag, where, nbytes, exp, FALSE, BFD_RELOC_AVR_16_PM);
       else
 	as_bad (_("illegal %srelocation size: %d"), "`pm' ", nbytes);
       exp_mod_pm = 0;
     }
 }
Index: gas/config/tc-avr.h
===================================================================
RCS file: /cvs/src/src/gas/config/tc-avr.h,v
retrieving revision 1.12
diff -U62 -r1.12 tc-avr.h
--- gas/config/tc-avr.h	5 May 2006 17:46:47 -0000	1.12
+++ gas/config/tc-avr.h	12 May 2006 21:43:11 -0000
@@ -66,74 +66,81 @@
    targets like the MIPS which support options to change the
    endianness, which function to call is a runtime decision.  On
    other targets, `md_number_to_chars' can be a simple macro.  */
 #define md_number_to_chars number_to_chars_littleendian
 
 /* `md_short_jump_size'
    `md_long_jump_size'
    `md_create_short_jump'
    `md_create_long_jump'
    If `WORKING_DOT_WORD' is defined, GAS will not do broken word
    processing (*note Broken words::.).  Otherwise, you should set
    `md_short_jump_size' to the size of a short jump (a jump that is
    just long enough to jump around a long jmp) and
    `md_long_jump_size' to the size of a long jump (a jump that can go
    anywhere in the function), You should define
    `md_create_short_jump' to create a short jump around a long jump,
    and define `md_create_long_jump' to create a long jump.  */
 #define WORKING_DOT_WORD
 
 /* If you define this macro, it means that `tc_gen_reloc' may return
    multiple relocation entries for a single fixup.  In this case, the
    return value of `tc_gen_reloc' is a pointer to a null terminated
    array.  */
 #undef RELOC_EXPANSION_POSSIBLE
 
 /* No shared lib support, so we don't need to ensure externally
    visible symbols can be overridden.  */
 #define EXTERN_FORCE_RELOC 0
 
 /* Values passed to md_apply_fix don't include the symbol value.  */
 #define MD_APPLY_SYM_VALUE(FIX) 0
 
 /* If you define this macro, it should return the offset between the
    address of a PC relative fixup and the position from which the PC
    relative adjustment should be made.  On many processors, the base
    of a PC relative instruction is the next instruction, so this
    macro would return the length of an instruction.  */
 #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from_section (FIX, SEC)
 extern long md_pcrel_from_section (struct fix *, segT);
 
 /* The number of bytes to put into a word in a listing.  This affects
    the way the bytes are clumped together in the listing.  For
    example, a value of 2 might print `1234 5678' where a value of 1
    would print `12 34 56 78'.  The default value is 4.  */
 #define LISTING_WORD_SIZE 2
 
 /* AVR port uses `$' as a logical line separator.  */
 #define LEX_DOLLAR 0
 
 /* An `.lcomm' directive with no explicit alignment parameter will
    use this macro to set P2VAR to the alignment that a request for
    SIZE bytes will have.  The alignment is expressed as a power of
    two.  If no alignment should take place, the macro definition
    should do nothing.  Some targets define a `.bss' directive that is
    also affected by this macro.  The default definition will set
    P2VAR to the truncated power of two of sizes up to eight bytes.  */
 #define TC_IMPLICIT_LCOMM_ALIGNMENT(SIZE, P2VAR) (P2VAR) = 0
 
 /* We don't want gas to fixup the following program memory related relocations.
    We will need them in case that we want to do linker relaxation.
    We could in principle keep these fixups in gas when not relaxing.
    However, there is no serious performance penilty when making the linker
-   make the fixup work.  */
-#define TC_VALIDATE_FIX(FIXP,SEG,SKIP)                      \
-  if ( FIXP->fx_r_type == BFD_RELOC_AVR_7_PCREL             \
-    || FIXP->fx_r_type == BFD_RELOC_AVR_13_PCREL            \
-    || FIXP->fx_r_type == BFD_RELOC_AVR_LO8_LDI_PM          \
-    || FIXP->fx_r_type == BFD_RELOC_AVR_HI8_LDI_PM          \
-    || FIXP->fx_r_type == BFD_RELOC_AVR_HH8_LDI_PM          \
-    || FIXP->fx_r_type == BFD_RELOC_AVR_16_PM)              \
-    {                                                       \
-      goto SKIP;                                            \
+   make the fixup work.  Check also that fx_addsy is not NULL, in order to make
+   shure that the fixup refers to some sort of lable.  */
+#define TC_VALIDATE_FIX(FIXP,SEG,SKIP)                       \
+  if ((FIXP->fx_r_type == BFD_RELOC_AVR_7_PCREL              \
+       || FIXP->fx_r_type == BFD_RELOC_AVR_13_PCREL          \
+       || FIXP->fx_r_type == BFD_RELOC_AVR_LO8_LDI_PM        \
+       || FIXP->fx_r_type == BFD_RELOC_AVR_LO8_LDI_GS        \
+       || FIXP->fx_r_type == BFD_RELOC_AVR_HI8_LDI_PM        \
+       || FIXP->fx_r_type == BFD_RELOC_AVR_HI8_LDI_GS        \
+       || FIXP->fx_r_type == BFD_RELOC_AVR_HH8_LDI_PM        \
+       || FIXP->fx_r_type == BFD_RELOC_AVR_LO8_LDI_PM_NEG    \
+       || FIXP->fx_r_type == BFD_RELOC_AVR_HI8_LDI_PM_NEG    \
+       || FIXP->fx_r_type == BFD_RELOC_AVR_HH8_LDI_PM_NEG    \
+       || FIXP->fx_r_type == BFD_RELOC_AVR_16_PM)            \
+      && (FIXP->fx_addsy))			             \
+    {                                                        \
+      goto SKIP;                                             \
     }
 
Index: include/elf/avr.h
===================================================================
RCS file: /cvs/src/src/include/elf/avr.h,v
retrieving revision 1.7
diff -U62 -r1.7 avr.h
--- include/elf/avr.h	3 Mar 2006 15:25:30 -0000	1.7
+++ include/elf/avr.h	12 May 2006 21:43:13 -0000
@@ -1,67 +1,70 @@
 /* AVR ELF support for BFD.
    Copyright 1999, 2000, 2004 Free Software Foundation, Inc.
    Contributed by Denis Chertykov <denisc@overta.ru>
 
    This file is part of BFD, the Binary File Descriptor library.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 #ifndef _ELF_AVR_H
 #define _ELF_AVR_H
 
 #include "elf/reloc-macros.h"
 
 /* Processor specific flags for the ELF header e_flags field.  */
 #define EF_AVR_MACH 0xf
 
 /* If bit #7 is set, it is assumed that the elf file uses local symbols
    as reference for the relocations so that linker relaxation is possible.  */
 #define EF_AVR_LINKRELAX_PREPARED 0x80
 
 #define E_AVR_MACH_AVR1 1
 #define E_AVR_MACH_AVR2 2
 #define E_AVR_MACH_AVR3 3
 #define E_AVR_MACH_AVR4 4
 #define E_AVR_MACH_AVR5 5
+#define E_AVR_MACH_AVR6 6 
 
 /* Relocations.  */
 START_RELOC_NUMBERS (elf_avr_reloc_type)
      RELOC_NUMBER (R_AVR_NONE,			0)
      RELOC_NUMBER (R_AVR_32,			1)
      RELOC_NUMBER (R_AVR_7_PCREL,		2)
      RELOC_NUMBER (R_AVR_13_PCREL,		3)
      RELOC_NUMBER (R_AVR_16, 			4)
      RELOC_NUMBER (R_AVR_16_PM, 		5)
      RELOC_NUMBER (R_AVR_LO8_LDI,		6)
      RELOC_NUMBER (R_AVR_HI8_LDI,		7)
      RELOC_NUMBER (R_AVR_HH8_LDI,		8)
      RELOC_NUMBER (R_AVR_LO8_LDI_NEG,		9)
      RELOC_NUMBER (R_AVR_HI8_LDI_NEG,	       10)
      RELOC_NUMBER (R_AVR_HH8_LDI_NEG,	       11)
      RELOC_NUMBER (R_AVR_LO8_LDI_PM,	       12)
      RELOC_NUMBER (R_AVR_HI8_LDI_PM,	       13)
      RELOC_NUMBER (R_AVR_HH8_LDI_PM,	       14)
      RELOC_NUMBER (R_AVR_LO8_LDI_PM_NEG,       15)
      RELOC_NUMBER (R_AVR_HI8_LDI_PM_NEG,       16)
      RELOC_NUMBER (R_AVR_HH8_LDI_PM_NEG,       17)
      RELOC_NUMBER (R_AVR_CALL,		       18)
      RELOC_NUMBER (R_AVR_LDI,                  19)
      RELOC_NUMBER (R_AVR_6,                    20)
      RELOC_NUMBER (R_AVR_6_ADIW,               21)
      RELOC_NUMBER (R_AVR_MS8_LDI,              22)
      RELOC_NUMBER (R_AVR_MS8_LDI_NEG,          23)
+     RELOC_NUMBER (R_AVR_LO8_LDI_GS,	       24)
+     RELOC_NUMBER (R_AVR_HI8_LDI_GS,	       25)
 END_RELOC_NUMBERS (R_AVR_max)
 
 #endif /* _ELF_AVR_H */
Index: ld/Makefile.am
===================================================================
RCS file: /cvs/src/src/ld/Makefile.am,v
retrieving revision 1.210
diff -U62 -r1.210 Makefile.am
--- ld/Makefile.am	2 May 2006 14:20:46 -0000	1.210
+++ ld/Makefile.am	12 May 2006 21:43:14 -0000
@@ -74,124 +74,125 @@
     else \
       echo gcc | sed '$(transform)'; \
     fi; \
   fi`
 
 CXX = gcc
 CXX_FOR_TARGET = ` \
   if [ -f $$r/../gcc/xgcc ] ; then \
     if [ -f $$r/../newlib/Makefile ] ; then \
       echo $$r/../gcc/xgcc -B$$r/../gcc/ -idirafter $$r/../newlib/targ-include -idirafter $${srcroot}/../newlib/libc/include -nostdinc; \
     else \
       echo $$r/../gcc/xgcc -B$$r/../gcc/; \
     fi; \
   else \
     if [ "@host@" = "@target@" ] ; then \
       echo $(CXX); \
     else \
       echo gcc | sed '$(transform)'; \
     fi; \
   fi`
 
 noinst_PROGRAMS = ld-new
 info_TEXINFOS = ld.texinfo
 noinst_TEXINFOS = ldint.texinfo
 man_MANS = ld.1
 
 AM_MAKEINFOFLAGS = -I $(srcdir) -I $(BFDDIR)/doc -I $(top_srcdir)/../libiberty
 TEXI2DVI = texi2dvi -I $(srcdir) -I $(BFDDIR)/doc
 
 INCLUDES = -D_GNU_SOURCE -I. -I$(srcdir) -I../bfd -I$(BFDDIR) -I$(INCDIR) -I$(top_srcdir)/../intl -I../intl $(HDEFINES) $(CFLAGS) -DLOCALEDIR="\"$(datadir)/locale\""
 
 BFDLIB = ../bfd/libbfd.la
 LIBIBERTY = ../libiberty/libiberty.a
 
 ALL_EMULATIONS = \
 	eaixppc.o \
 	eaixrs6.o \
 	ealpha.o \
 	earcelf.o \
 	earm_epoc_pe.o \
 	earmaoutb.o \
 	earmaoutl.o \
 	earmcoff.o \
 	earmelf.o \
 	earmelfb.o \
 	earmelf_fbsd.o \
 	earmelf_linux.o \
 	earmelf_linux_eabi.o \
 	earmelfb_linux.o \
 	earmelfb_linux_eabi.o \
 	earmelf_nbsd.o \
 	earmelfb_nbsd.o \
 	earmelf_vxworks.o \
 	earmnto.o \
 	earmnbsd.o \
 	earmpe.o \
 	earmsymbian.o \
 	eavr2.o \
 	eavr1.o \
 	eavr3.o \
 	eavr4.o \
 	eavr5.o \
+	eavr6.o \
 	ecoff_i860.o \
 	ecoff_sparc.o \
 	ecrisaout.o \
 	ecriself.o \
 	ecrislinux.o \
 	ed10velf.o \
 	ed30v_e.o \
 	ed30v_o.o \
 	ed30velf.o \
 	edelta68.o \
 	eelf32_dlx.o \
 	eelf32_i960.o \
 	eelf32_i860.o \
 	eelf32_sparc.o \
 	eelf32_sparc_vxworks.o \
 	eelf32b4300.o \
 	eelf32bfin.o \
 	eelf32bfinfd.o \
 	eelf32cr16c.o \
 	eelf32bmip.o \
 	eelf32bmipn32.o \
 	eelf32btsmip.o \
 	eelf32crx.o \
 	eelf32btsmipn32.o \
 	eelf32ltsmip.o \
 	eelf32ltsmipn32.o \
 	eelf32ebmip.o \
 	eelf32ebmipvxworks.o \
 	eelf32elmip.o \
 	eelf32elmipvxworks.o \
 	eelf32fr30.o \
 	eelf32frv.o \
 	eelf32i370.o \
 	eelf32ip2k.o \
 	eelf32iq2000.o \
 	eelf32iq10.o \
 	eelf32l4300.o \
 	eelf32lmip.o \
 	eelf32lppc.o \
 	eelf32lppcnto.o \
 	eelf32lppcsim.o \
 	eelf32m32c.o \
 	eelf32mcore.o \
 	eelf32mipswindiss.o \
 	eelf32mt.o \
 	eelf32openrisc.o \
 	eelf32ppc.o \
 	eelf32ppc_fbsd.o \
 	eelf32ppclinux.o \
 	eelf32ppcnto.o \
 	eelf32ppcsim.o \
 	eelf32ppcwindiss.o \
 	eelf32ppcvxworks.o \
 	eelf32vax.o \
         eelf32xc16x.o \
         eelf32xc16xl.o \
         eelf32xc16xs.o \
 	eelf32xstormy16.o \
 	eelf32xtensa.o \
 	eelf_i386.o \
 	eelf_i386_be.o \
 	eelf_i386_chaos.o \
@@ -536,124 +537,128 @@
   $(srcdir)/emultempl/elf32.em $(srcdir)/emultempl/armelf.em \
   $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} armelf_nbsd "$(tdir_armelf_nbsd)"
 earmelfb_nbsd.c: $(srcdir)/emulparams/armelfb_nbsd.sh \
   $(srcdir)/emulparams/armelf_nbsd.sh \
   $(srcdir)/emulparams/armelf.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/emultempl/armelf.em \
   $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} armelfb_nbsd "$(tdir_armelfb_nbsd)"
 earmelf_vxworks.c: $(srcdir)/emulparams/armelf_vxworks.sh \
   $(srcdir)/emulparams/vxworks.sh $(srcdir)/emulparams/armelf.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/emultempl/vxworks.em \
   $(srcdir)/emultempl/armelf.em $(srcdir)/scripttempl/elf.sc \
   ${GEN_DEPENDS}
 	${GENSCRIPTS} armelf_vxworks "$(tdir_armelf)"
 earmaoutb.c:	$(srcdir)/emulparams/armaoutb.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/armaout.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} armaoutb "$(tdir_armaoutb)"
 earmaoutl.c:	$(srcdir)/emulparams/armaoutl.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/armaout.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} armaoutl "$(tdir_armaoutl)"
 earmcoff.c:	$(srcdir)/emulparams/armcoff.sh \
   $(srcdir)/emultempl/armcoff.em $(srcdir)/scripttempl/armcoff.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} armcoff "$(tdir_armcoff)"
 earmnbsd.c:	$(srcdir)/emulparams/armnbsd.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/aout.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} armnbsd "$(tdir_armnbsd)"
 earmnto.c: $(srcdir)/emulparams/armnto.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/emultempl/armelf.em \
   $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} armnto "$(tdir_armnto)"
 earm_epoc_pe.c: $(srcdir)/emulparams/arm_epoc_pe.sh \
   $(srcdir)/emultempl/pe.em $(srcdir)/scripttempl/epocpe.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} arm_epoc_pe "$(tdir_armpe)"
 earmpe.c: $(srcdir)/emulparams/armpe.sh \
   $(srcdir)/emultempl/pe.em $(srcdir)/scripttempl/pe.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} armpe "$(tdir_armpe)"
 earmsymbian.c: $(srcdir)/emulparams/armsymbian.sh \
   $(srcdir)/emulparams/armelf.sh $(srcdir)/emultempl/elf32.em \
   $(srcdir)/emultempl/armelf.em $(srcdir)/scripttempl/armbpabi.sc \
   ${GEN_DEPENDS}
 	${GENSCRIPTS} armsymbian "$(tdir_armelf)"
 eavr2.c: $(srcdir)/emulparams/avr2.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/avr.sc \
   ${GEN_DEPENDS}
 	${GENSCRIPTS} avr2 "$(tdir_avr2)"
 eavr1.c: $(srcdir)/emulparams/avr1.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/avr.sc \
   ${GEN_DEPENDS}
 	${GENSCRIPTS} avr1 "$(tdir_avr2)"
 eavr3.c: $(srcdir)/emulparams/avr3.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/avr.sc \
   ${GEN_DEPENDS}
 	${GENSCRIPTS} avr3 "$(tdir_avr2)"
 eavr4.c: $(srcdir)/emulparams/avr4.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/avr.sc \
   ${GEN_DEPENDS}
 	${GENSCRIPTS} avr4 "$(tdir_avr2)"
 eavr5.c: $(srcdir)/emulparams/avr5.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/avr.sc \
   ${GEN_DEPENDS}
 	${GENSCRIPTS} avr5 "$(tdir_avr2)"
+eavr6.c: $(srcdir)/emulparams/avr6.sh \
+  $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/avr.sc \
+  ${GEN_DEPENDS}
+	${GENSCRIPTS} avr6 "$(tdir_avr2)"
 ecoff_i860.c: $(srcdir)/emulparams/coff_i860.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/i860coff.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} coff_i860 "$(tdir_coff_i860)"
 ecoff_sparc.c: $(srcdir)/emulparams/coff_sparc.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/sparccoff.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} coff_sparc "$(tdir_coff_sparc)"
 ecrisaout.c: $(srcdir)/emulparams/crisaout.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/crisaout.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} crisaout "$(tdir_cris)"
 ecriself.c: $(srcdir)/emulparams/criself.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} criself "$(tdir_cris)"
 ecrislinux.c: $(srcdir)/emulparams/crislinux.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} crislinux "$(tdir_cris)"
 ed10velf.c: $(srcdir)/emulparams/d10velf.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/scripttempl/elfd10v.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} d10velf "$(tdir_d10v)"
 ed30velf.c: $(srcdir)/emulparams/d30velf.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/elfd30v.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} d30velf "$(tdir_d30v)"
 ed30v_o.c: $(srcdir)/emulparams/d30v_o.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/elfd30v.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} d30v_o "$(tdir_d30v)"
 ed30v_e.c: $(srcdir)/emulparams/d30v_e.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/elfd30v.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} d30v_e "$(tdir_d30v)"
 edelta68.c: $(srcdir)/emulparams/delta68.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/delta68.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} delta68 "$(tdir_delta68)"
 eelf32bfin.c: $(srcdir)/emulparams/bfin.sh \
   $(srcdir)/emultempl/elf32.em \
   $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} elf32bfin "$(tdir_elf32bfin)" bfin
 eelf32bfinfd.c: $(srcdir)/emulparams/elf32bfinfd.sh $(srcdir)/emulparams/bfin.sh \
   $(srcdir)/emultempl/elf32.em \
   $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} elf32bfinfd "$(tdir_elf32bfinfd)" elf32bfinfd
 eelf32_dlx.c: $(srcdir)/emulparams/elf32_dlx.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/scripttempl/dlx.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} elf32_dlx "$(tdir_elf32_dlx)"
 eelf32xc16x.c: $(srcdir)/emulparams/elf32xc16x.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/emultempl/needrelax.em \
   $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} elf32xc16x "$(tdir_xc16x)"
 eelf32xc16xl.c: $(srcdir)/emulparams/elf32xc16xl.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/emultempl/needrelax.em \
   $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} elf32xc16xl "$(tdir_xc16xl)"
 eelf32xc16xs.c: $(srcdir)/emulparams/elf32xc16xs.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/emultempl/needrelax.em \
   $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} elf32xc16xs "$(tdir_xc16xs)"		
 eelf32xstormy16.c: $(srcdir)/emulparams/elf32xstormy16.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/emultempl/needrelax.em \
   $(srcdir)/scripttempl/xstormy16.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} elf32xstormy16 "$(tdir_xstormy16)"
 eelf32am33lin.c: $(srcdir)/emulparams/elf32am33lin.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} elf32am33lin "$(tdir_mn10300)"
 eelf32vax.c: $(srcdir)/emulparams/elf32vax.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
Index: ld/Makefile.in
===================================================================
RCS file: /cvs/src/src/ld/Makefile.in,v
retrieving revision 1.224
diff -U62 -r1.224 Makefile.in
--- ld/Makefile.in	2 May 2006 14:20:46 -0000	1.224
+++ ld/Makefile.in	12 May 2006 21:43:15 -0000
@@ -298,124 +298,125 @@
       echo $$r/../gcc/xgcc -B$$r/../gcc/; \
     fi; \
   else \
     if [ "@host@" = "@target@" ] ; then \
       echo $(CC); \
     else \
       echo gcc | sed '$(transform)'; \
     fi; \
   fi`
 
 CXX = gcc
 CXX_FOR_TARGET = ` \
   if [ -f $$r/../gcc/xgcc ] ; then \
     if [ -f $$r/../newlib/Makefile ] ; then \
       echo $$r/../gcc/xgcc -B$$r/../gcc/ -idirafter $$r/../newlib/targ-include -idirafter $${srcroot}/../newlib/libc/include -nostdinc; \
     else \
       echo $$r/../gcc/xgcc -B$$r/../gcc/; \
     fi; \
   else \
     if [ "@host@" = "@target@" ] ; then \
       echo $(CXX); \
     else \
       echo gcc | sed '$(transform)'; \
     fi; \
   fi`
 
 info_TEXINFOS = ld.texinfo
 noinst_TEXINFOS = ldint.texinfo
 man_MANS = ld.1
 AM_MAKEINFOFLAGS = -I $(srcdir) -I $(BFDDIR)/doc -I $(top_srcdir)/../libiberty
 TEXI2DVI = texi2dvi -I $(srcdir) -I $(BFDDIR)/doc
 INCLUDES = -D_GNU_SOURCE -I. -I$(srcdir) -I../bfd -I$(BFDDIR) -I$(INCDIR) -I$(top_srcdir)/../intl -I../intl $(HDEFINES) $(CFLAGS) -DLOCALEDIR="\"$(datadir)/locale\""
 BFDLIB = ../bfd/libbfd.la
 LIBIBERTY = ../libiberty/libiberty.a
 ALL_EMULATIONS = \
 	eaixppc.o \
 	eaixrs6.o \
 	ealpha.o \
 	earcelf.o \
 	earm_epoc_pe.o \
 	earmaoutb.o \
 	earmaoutl.o \
 	earmcoff.o \
 	earmelf.o \
 	earmelfb.o \
 	earmelf_fbsd.o \
 	earmelf_linux.o \
 	earmelf_linux_eabi.o \
 	earmelfb_linux.o \
 	earmelfb_linux_eabi.o \
 	earmelf_nbsd.o \
 	earmelfb_nbsd.o \
 	earmelf_vxworks.o \
 	earmnto.o \
 	earmnbsd.o \
 	earmpe.o \
 	earmsymbian.o \
 	eavr2.o \
 	eavr1.o \
 	eavr3.o \
 	eavr4.o \
 	eavr5.o \
+	eavr6.o \
 	ecoff_i860.o \
 	ecoff_sparc.o \
 	ecrisaout.o \
 	ecriself.o \
 	ecrislinux.o \
 	ed10velf.o \
 	ed30v_e.o \
 	ed30v_o.o \
 	ed30velf.o \
 	edelta68.o \
 	eelf32_dlx.o \
 	eelf32_i960.o \
 	eelf32_i860.o \
 	eelf32_sparc.o \
 	eelf32_sparc_vxworks.o \
 	eelf32b4300.o \
 	eelf32bfin.o \
 	eelf32bfinfd.o \
 	eelf32cr16c.o \
 	eelf32bmip.o \
 	eelf32bmipn32.o \
 	eelf32btsmip.o \
 	eelf32crx.o \
 	eelf32btsmipn32.o \
 	eelf32ltsmip.o \
 	eelf32ltsmipn32.o \
 	eelf32ebmip.o \
 	eelf32ebmipvxworks.o \
 	eelf32elmip.o \
 	eelf32elmipvxworks.o \
 	eelf32fr30.o \
 	eelf32frv.o \
 	eelf32i370.o \
 	eelf32ip2k.o \
 	eelf32iq2000.o \
 	eelf32iq10.o \
 	eelf32l4300.o \
 	eelf32lmip.o \
 	eelf32lppc.o \
 	eelf32lppcnto.o \
 	eelf32lppcsim.o \
 	eelf32m32c.o \
 	eelf32mcore.o \
 	eelf32mipswindiss.o \
 	eelf32mt.o \
 	eelf32openrisc.o \
 	eelf32ppc.o \
 	eelf32ppc_fbsd.o \
 	eelf32ppclinux.o \
 	eelf32ppcnto.o \
 	eelf32ppcsim.o \
 	eelf32ppcwindiss.o \
 	eelf32ppcvxworks.o \
 	eelf32vax.o \
         eelf32xc16x.o \
         eelf32xc16xl.o \
         eelf32xc16xs.o \
 	eelf32xstormy16.o \
 	eelf32xtensa.o \
 	eelf_i386.o \
 	eelf_i386_be.o \
 	eelf_i386_chaos.o \
@@ -1350,124 +1351,128 @@
   $(srcdir)/emultempl/elf32.em $(srcdir)/emultempl/armelf.em \
   $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} armelf_nbsd "$(tdir_armelf_nbsd)"
 earmelfb_nbsd.c: $(srcdir)/emulparams/armelfb_nbsd.sh \
   $(srcdir)/emulparams/armelf_nbsd.sh \
   $(srcdir)/emulparams/armelf.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/emultempl/armelf.em \
   $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} armelfb_nbsd "$(tdir_armelfb_nbsd)"
 earmelf_vxworks.c: $(srcdir)/emulparams/armelf_vxworks.sh \
   $(srcdir)/emulparams/vxworks.sh $(srcdir)/emulparams/armelf.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/emultempl/vxworks.em \
   $(srcdir)/emultempl/armelf.em $(srcdir)/scripttempl/elf.sc \
   ${GEN_DEPENDS}
 	${GENSCRIPTS} armelf_vxworks "$(tdir_armelf)"
 earmaoutb.c:	$(srcdir)/emulparams/armaoutb.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/armaout.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} armaoutb "$(tdir_armaoutb)"
 earmaoutl.c:	$(srcdir)/emulparams/armaoutl.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/armaout.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} armaoutl "$(tdir_armaoutl)"
 earmcoff.c:	$(srcdir)/emulparams/armcoff.sh \
   $(srcdir)/emultempl/armcoff.em $(srcdir)/scripttempl/armcoff.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} armcoff "$(tdir_armcoff)"
 earmnbsd.c:	$(srcdir)/emulparams/armnbsd.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/aout.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} armnbsd "$(tdir_armnbsd)"
 earmnto.c: $(srcdir)/emulparams/armnto.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/emultempl/armelf.em \
   $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} armnto "$(tdir_armnto)"
 earm_epoc_pe.c: $(srcdir)/emulparams/arm_epoc_pe.sh \
   $(srcdir)/emultempl/pe.em $(srcdir)/scripttempl/epocpe.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} arm_epoc_pe "$(tdir_armpe)"
 earmpe.c: $(srcdir)/emulparams/armpe.sh \
   $(srcdir)/emultempl/pe.em $(srcdir)/scripttempl/pe.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} armpe "$(tdir_armpe)"
 earmsymbian.c: $(srcdir)/emulparams/armsymbian.sh \
   $(srcdir)/emulparams/armelf.sh $(srcdir)/emultempl/elf32.em \
   $(srcdir)/emultempl/armelf.em $(srcdir)/scripttempl/armbpabi.sc \
   ${GEN_DEPENDS}
 	${GENSCRIPTS} armsymbian "$(tdir_armelf)"
 eavr2.c: $(srcdir)/emulparams/avr2.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/avr.sc \
   ${GEN_DEPENDS}
 	${GENSCRIPTS} avr2 "$(tdir_avr2)"
 eavr1.c: $(srcdir)/emulparams/avr1.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/avr.sc \
   ${GEN_DEPENDS}
 	${GENSCRIPTS} avr1 "$(tdir_avr2)"
 eavr3.c: $(srcdir)/emulparams/avr3.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/avr.sc \
   ${GEN_DEPENDS}
 	${GENSCRIPTS} avr3 "$(tdir_avr2)"
 eavr4.c: $(srcdir)/emulparams/avr4.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/avr.sc \
   ${GEN_DEPENDS}
 	${GENSCRIPTS} avr4 "$(tdir_avr2)"
 eavr5.c: $(srcdir)/emulparams/avr5.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/avr.sc \
   ${GEN_DEPENDS}
 	${GENSCRIPTS} avr5 "$(tdir_avr2)"
+eavr6.c: $(srcdir)/emulparams/avr6.sh \
+  $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/avr.sc \
+  ${GEN_DEPENDS}
+	${GENSCRIPTS} avr6 "$(tdir_avr2)"
 ecoff_i860.c: $(srcdir)/emulparams/coff_i860.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/i860coff.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} coff_i860 "$(tdir_coff_i860)"
 ecoff_sparc.c: $(srcdir)/emulparams/coff_sparc.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/sparccoff.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} coff_sparc "$(tdir_coff_sparc)"
 ecrisaout.c: $(srcdir)/emulparams/crisaout.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/crisaout.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} crisaout "$(tdir_cris)"
 ecriself.c: $(srcdir)/emulparams/criself.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} criself "$(tdir_cris)"
 ecrislinux.c: $(srcdir)/emulparams/crislinux.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} crislinux "$(tdir_cris)"
 ed10velf.c: $(srcdir)/emulparams/d10velf.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/scripttempl/elfd10v.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} d10velf "$(tdir_d10v)"
 ed30velf.c: $(srcdir)/emulparams/d30velf.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/elfd30v.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} d30velf "$(tdir_d30v)"
 ed30v_o.c: $(srcdir)/emulparams/d30v_o.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/elfd30v.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} d30v_o "$(tdir_d30v)"
 ed30v_e.c: $(srcdir)/emulparams/d30v_e.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/elfd30v.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} d30v_e "$(tdir_d30v)"
 edelta68.c: $(srcdir)/emulparams/delta68.sh \
   $(srcdir)/emultempl/generic.em $(srcdir)/scripttempl/delta68.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} delta68 "$(tdir_delta68)"
 eelf32bfin.c: $(srcdir)/emulparams/bfin.sh \
   $(srcdir)/emultempl/elf32.em \
   $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} elf32bfin "$(tdir_elf32bfin)" bfin
 eelf32bfinfd.c: $(srcdir)/emulparams/elf32bfinfd.sh $(srcdir)/emulparams/bfin.sh \
   $(srcdir)/emultempl/elf32.em \
   $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} elf32bfinfd "$(tdir_elf32bfinfd)" elf32bfinfd
 eelf32_dlx.c: $(srcdir)/emulparams/elf32_dlx.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/scripttempl/dlx.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} elf32_dlx "$(tdir_elf32_dlx)"
 eelf32xc16x.c: $(srcdir)/emulparams/elf32xc16x.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/emultempl/needrelax.em \
   $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} elf32xc16x "$(tdir_xc16x)"
 eelf32xc16xl.c: $(srcdir)/emulparams/elf32xc16xl.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/emultempl/needrelax.em \
   $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} elf32xc16xl "$(tdir_xc16xl)"
 eelf32xc16xs.c: $(srcdir)/emulparams/elf32xc16xs.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/emultempl/needrelax.em \
   $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} elf32xc16xs "$(tdir_xc16xs)"		
 eelf32xstormy16.c: $(srcdir)/emulparams/elf32xstormy16.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/emultempl/needrelax.em \
   $(srcdir)/scripttempl/xstormy16.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} elf32xstormy16 "$(tdir_xstormy16)"
 eelf32am33lin.c: $(srcdir)/emulparams/elf32am33lin.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
 	${GENSCRIPTS} elf32am33lin "$(tdir_mn10300)"
 eelf32vax.c: $(srcdir)/emulparams/elf32vax.sh \
   $(srcdir)/emultempl/elf32.em $(srcdir)/scripttempl/elf.sc ${GEN_DEPENDS}
Index: ld/configure.tgt
===================================================================
RCS file: /cvs/src/src/ld/configure.tgt,v
retrieving revision 1.187
diff -U62 -r1.187 configure.tgt
--- ld/configure.tgt	5 Apr 2006 12:41:57 -0000	1.187
+++ ld/configure.tgt	12 May 2006 21:43:15 -0000
@@ -22,125 +22,125 @@
 case "${targ}" in
 alpha*-*-freebsd* | alpha*-*-kfreebsd*-gnu)
 		        targ_emul=elf64alpha_fbsd
 			targ_extra_emuls="elf64alpha alpha"
 			tdir_alpha=`echo ${targ_alias} | sed -e 's/freebsd/freebsdecoff/'` ;;
 alpha*-*-linuxecoff*)	targ_emul=alpha targ_extra_emuls=elf64alpha
 			tdir_elf64alpha=`echo ${targ_alias} | sed -e 's/ecoff//'` ;;
 alpha*-*-linux-*)	targ_emul=elf64alpha targ_extra_emuls=alpha
 			tdir_alpha=`echo ${targ_alias} | sed -e 's/linux/linuxecoff/'` ;;
 alpha*-*-osf*)		targ_emul=alpha ;;
 alpha*-*-gnu*)		targ_emul=elf64alpha ;;
 alpha*-*-netware*)	targ_emul=alpha ;;
 alpha*-*-netbsd*)	targ_emul=elf64alpha_nbsd ;;
 alpha*-*-openbsd*)	targ_emul=elf64alpha
 			;;
 arc-*-elf*)		targ_emul=arcelf
 			;;
 arm-epoc-pe)		targ_emul=arm_epoc_pe ;	targ_extra_ofiles="deffilep.o pe-dll.o" ;;
 arm-*-wince)		targ_emul=armpe ; targ_extra_ofiles="deffilep.o pe-dll.o" ;;
 arm-*-pe)		targ_emul=armpe ; targ_extra_ofiles="deffilep.o pe-dll.o" ;;
 arm-*-aout | armel-*-aout) targ_emul=armaoutl ;;
 armeb-*-aout)		targ_emul=armaoutb ;;
 arm-*-coff)		targ_emul=armcoff ;;
 arm-*-freebsd* | arm-*-kfreebsd*-gnu)
 	       		targ_emul=armelf_fbsd
 			targ_extra_emuls="armelf" ;;
 armeb-*-netbsdelf*)	targ_emul=armelfb_nbsd;
 			targ_extra_emuls="armelf_nbsd armelf armnbsd" ;;
 arm-*-netbsdelf*)	targ_emul=armelf_nbsd;
 			targ_extra_emuls="armelfb_nbsd armelf armnbsd" ;;
 arm-*-netbsd*)		targ_emul=armnbsd;
 			targ_extra_emuls="armelf armelf_nbsd armelfb_nbsd"  ;;
 arm-*-nto*)		targ_emul=armnto ;;
 arm-*-openbsd*)		targ_emul=armnbsd ;;
 arm-*-rtems*)		targ_emul=armelf ;;
 armeb-*-elf)		targ_emul=armelfb ;;
 arm-*-elf | arm*-*-eabi*)
 	  		targ_emul=armelf ;;
 arm*-*-symbianelf*)     targ_emul=armsymbian;;
 arm-*-kaos*)		targ_emul=armelf ;;
 arm9e-*-elf)		targ_emul=armelf ;;
 arm*b-*-linux-*eabi)	targ_emul=armelfb_linux_eabi ;;
 arm*b-*-linux-*)	targ_emul=armelfb_linux; targ_extra_emuls=armelfb ;;
 arm*-*-linux-*eabi)	targ_emul=armelf_linux_eabi ;;
 arm*-*-linux-*)	        targ_emul=armelf_linux; targ_extra_emuls=armelf ;;
 arm*-*-uclinux*)	targ_emul=armelf_linux; targ_extra_emuls=armelf ;;
 arm-*-vxworks)		targ_emul=armelf_vxworks ;;
 arm*-*-conix*)		targ_emul=armelf ;;
 thumb-*-linux-* | thumb-*-uclinux*)	targ_emul=armelf_linux; targ_extra_emuls=armelf ;;
 strongarm-*-coff)	targ_emul=armcoff ;;
 strongarm-*-elf)	targ_emul=armelf ;;
 strongarm-*-kaos*)	targ_emul=armelf ;;
 thumb-*-coff)		targ_emul=armcoff ;;
 thumb-*-elf)		targ_emul=armelf ;;
 thumb-epoc-pe)		targ_emul=arm_epoc_pe ;
 			targ_extra_ofiles="deffilep.o pe-dll.o" ;;
 thumb-*-pe)		targ_emul=armpe ;
 			targ_extra_ofiles="deffilep.o pe-dll.o" ;;
 xscale-*-coff)		targ_emul=armcoff ;;
 xscale-*-elf)		targ_emul=armelf
 			;;
 avr-*-*)		targ_emul=avr2
-			targ_extra_emuls="avr1 avr3 avr4 avr5"
+			targ_extra_emuls="avr1 avr3 avr4 avr5 avr6"
 			;;
 bfin-*-elf)		targ_emul=elf32bfin; targ_extra_emuls="elf32bfinfd" ;;
 bfin-*-uclinux*)	targ_emul=elf32bfin; targ_extra_emuls="elf32bfinfd" ;;
 cr16c-*-elf*)           targ_emul=elf32cr16c
 			;;
 cris-*-*aout*)		targ_emul=crisaout
 			targ_extra_emuls="criself crislinux"
 			targ_extra_libpath=$targ_extra_emuls ;;
 cris-*-linux-* | crisv32-*-linux-*)
 			targ_emul=crislinux ;;
 cris-*-* | crisv32-*-*)	targ_emul=criself
 			targ_extra_emuls="crisaout crislinux"
 			targ_extra_libpath=$targ_extra_emuls
 			;;
 crx-*-elf*)		targ_emul=elf32crx
 			;;
 d10v-*-*)		targ_emul=d10velf ;;
 d30v-*-*ext*)		targ_emul=d30v_e; targ_extra_emuls="d30velf d30v_o" ;;
 d30v-*-*onchip*)	targ_emul=d30v_o; targ_extra_emuls="d30velf d30v_e" ;;
 d30v-*-*)		targ_emul=d30velf; targ_extra_emuls="d30v_e d30v_o"
 			;;
 dlx-*-elf*)		targ_emul=elf32_dlx
 			;;
 fr30-*-*)		targ_emul=elf32fr30
 			;;
 frv-*-*linux*)		targ_emul=elf32frvfd ;;
 frv-*-*)		targ_emul=elf32frv ; targ_extra_emuls="elf32frvfd"
 			;;
 h8300-*-hms* | h8300-*-coff* | h8300-*-rtemscoff*)
 			targ_emul=h8300; targ_extra_emuls="h8300h h8300s h8300hn h8300sn h8300sx h8300sxn" ;;
 h8300-*-elf* | h8300-*-rtems*)
 			targ_emul=h8300elf;
 			targ_extra_emuls="h8300helf h8300self h8300hnelf h8300snelf h8300sxelf h8300sxnelf" ;;
 h8500-*-hms* | h8500-*-coff* | h8500-*-rtems*)
 			targ_emul=h8500
 			targ_extra_emuls="h8500s h8500b h8500m h8500c"
 			;;
 hppa*64*-*-linux-*)	targ_emul=hppa64linux ;;
 hppa*64*-*)		targ_emul=elf64hppa ;;
 hppa*-*-linux-*)	targ_emul=hppalinux ;;
 hppa*-*-*elf*)		targ_emul=hppaelf ;;
 hppa*-*-lites*)		targ_emul=hppaelf ;;
 hppa*-*-netbsd*)	targ_emul=hppanbsd ;;
 hppa*-*-openbsd*)	targ_emul=hppaobsd
 			;;
 i370-*-elf* | i370-*-linux-*) targ_emul=elf32i370
 			;;
 i[3-7]86-*-nto-qnx*)	targ_emul=i386nto ;;
 i[3-7]86-*-vsta)	targ_emul=vsta ;;
 i[3-7]86-*-go32)	targ_emul=i386go32 ;;
 i[3-7]86-*-msdosdjgpp*) targ_emul=i386go32 ;;
 i[3-7]86-*-aix*)	targ_emul=i386coff ;;
 i[3-7]86-*-sco*)	targ_emul=i386coff ;;
 i[3-7]86-*-isc*)	targ_emul=i386coff ;;
 i[3-7]86-*-lynxos*)	targ_emul=i386lynx ;;
 i[3-7]86-*-coff)	targ_emul=i386coff ;;
 i[3-7]86-*-rtems*)	targ_emul=elf_i386 ;;
 i[3-7]86-*-aros*)	targ_emul=elf_i386 ;;
 i[3-7]86-*-rdos*)	targ_emul=elf_i386 ;;
 i[3-7]86-*-bsd)		targ_emul=i386bsd ;;
 i[3-7]86-*-bsd386)	targ_emul=i386bsd ;;
 i[3-7]86-*-bsdi*)	targ_emul=i386bsd ;;
Index: ld/emulparams/avr1.sh
===================================================================
RCS file: /cvs/src/src/ld/emulparams/avr1.sh,v
retrieving revision 1.1
diff -U62 -r1.1 avr1.sh
--- ld/emulparams/avr1.sh	16 May 2002 19:51:08 -0000	1.1
+++ ld/emulparams/avr1.sh	12 May 2006 21:43:15 -0000
@@ -1,10 +1,12 @@
 ARCH=avr:1
 MACHINE=
 SCRIPT_NAME=avr
 OUTPUT_FORMAT="elf32-avr"
 MAXPAGESIZE=1
 EMBEDDED=yes
-TEMPLATE_NAME=generic
+TEMPLATE_NAME=elf32
 
 TEXT_LENGTH=8K
 DATA_LENGTH=0
+EXTRA_EM_FILE=avrelf
+
Index: ld/emulparams/avr2.sh
===================================================================
RCS file: /cvs/src/src/ld/emulparams/avr2.sh,v
retrieving revision 1.1
diff -U62 -r1.1 avr2.sh
--- ld/emulparams/avr2.sh	16 May 2002 19:51:08 -0000	1.1
+++ ld/emulparams/avr2.sh	12 May 2006 21:43:15 -0000
@@ -1,10 +1,12 @@
 ARCH=avr:2
 MACHINE=
 SCRIPT_NAME=avr
 OUTPUT_FORMAT="elf32-avr"
 MAXPAGESIZE=1
 EMBEDDED=yes
-TEMPLATE_NAME=generic
+TEMPLATE_NAME=elf32
 
 TEXT_LENGTH=8K
 DATA_LENGTH=0xffa0
+EXTRA_EM_FILE=avrelf
+
Index: ld/emulparams/avr3.sh
===================================================================
RCS file: /cvs/src/src/ld/emulparams/avr3.sh,v
retrieving revision 1.1
diff -U62 -r1.1 avr3.sh
--- ld/emulparams/avr3.sh	16 May 2002 19:51:08 -0000	1.1
+++ ld/emulparams/avr3.sh	12 May 2006 21:43:15 -0000
@@ -1,10 +1,13 @@
 ARCH=avr:3
 MACHINE=
 SCRIPT_NAME=avr
 OUTPUT_FORMAT="elf32-avr"
 MAXPAGESIZE=1
 EMBEDDED=yes
-TEMPLATE_NAME=generic
+TEMPLATE_NAME=elf32
 
 TEXT_LENGTH=128K
 DATA_LENGTH=0xffa0
+
+EXTRA_EM_FILE=avrelf
+
Index: ld/emulparams/avr4.sh
===================================================================
RCS file: /cvs/src/src/ld/emulparams/avr4.sh,v
retrieving revision 1.1
diff -U62 -r1.1 avr4.sh
--- ld/emulparams/avr4.sh	16 May 2002 19:51:08 -0000	1.1
+++ ld/emulparams/avr4.sh	12 May 2006 21:43:15 -0000
@@ -1,10 +1,12 @@
 ARCH=avr:4
 MACHINE=
 SCRIPT_NAME=avr
 OUTPUT_FORMAT="elf32-avr"
 MAXPAGESIZE=1
 EMBEDDED=yes
-TEMPLATE_NAME=generic
+TEMPLATE_NAME=elf32
 
 TEXT_LENGTH=8K
 DATA_LENGTH=0xffa0
+
+EXTRA_EM_FILE=avrelf
Index: ld/emulparams/avr5.sh
===================================================================
RCS file: /cvs/src/src/ld/emulparams/avr5.sh,v
retrieving revision 1.1
diff -U62 -r1.1 avr5.sh
--- ld/emulparams/avr5.sh	16 May 2002 19:51:08 -0000	1.1
+++ ld/emulparams/avr5.sh	12 May 2006 21:43:15 -0000
@@ -1,10 +1,11 @@
 ARCH=avr:5
 MACHINE=
 SCRIPT_NAME=avr
 OUTPUT_FORMAT="elf32-avr"
 MAXPAGESIZE=1
 EMBEDDED=yes
-TEMPLATE_NAME=generic
+TEMPLATE_NAME=elf32
 
 TEXT_LENGTH=128K
 DATA_LENGTH=0xffa0
+EXTRA_EM_FILE=avrelf
Index: ld/scripttempl/avr.sc
===================================================================
RCS file: /cvs/src/src/ld/scripttempl/avr.sc,v
retrieving revision 1.4
diff -U62 -r1.4 avr.sc
--- ld/scripttempl/avr.sc	3 Mar 2006 15:25:31 -0000	1.4
+++ ld/scripttempl/avr.sc	12 May 2006 21:43:15 -0000
@@ -18,144 +18,155 @@
   .dynstr      ${RELOCATING-0} : { *(.dynstr)		}
   .gnu.version ${RELOCATING-0} : { *(.gnu.version)	}
   .gnu.version_d ${RELOCATING-0} : { *(.gnu.version_d)	}
   .gnu.version_r ${RELOCATING-0} : { *(.gnu.version_r)	}
 
   .rel.init    ${RELOCATING-0} : { *(.rel.init)		}
   .rela.init   ${RELOCATING-0} : { *(.rela.init)	}
   .rel.text    ${RELOCATING-0} :
     {
       *(.rel.text)
       ${RELOCATING+*(.rel.text.*)}
       ${RELOCATING+*(.rel.gnu.linkonce.t*)}
     }
   .rela.text   ${RELOCATING-0} :
     {
       *(.rela.text)
       ${RELOCATING+*(.rela.text.*)}
       ${RELOCATING+*(.rela.gnu.linkonce.t*)}
     }
   .rel.fini    ${RELOCATING-0} : { *(.rel.fini)		}
   .rela.fini   ${RELOCATING-0} : { *(.rela.fini)	}
   .rel.rodata  ${RELOCATING-0} :
     {
       *(.rel.rodata)
       ${RELOCATING+*(.rel.rodata.*)}
       ${RELOCATING+*(.rel.gnu.linkonce.r*)}
     }
   .rela.rodata ${RELOCATING-0} :
     {
       *(.rela.rodata)
       ${RELOCATING+*(.rela.rodata.*)}
       ${RELOCATING+*(.rela.gnu.linkonce.r*)}
     }
   .rel.data    ${RELOCATING-0} :
     {
       *(.rel.data)
       ${RELOCATING+*(.rel.data.*)}
       ${RELOCATING+*(.rel.gnu.linkonce.d*)}
     }
   .rela.data   ${RELOCATING-0} :
     {
       *(.rela.data)
       ${RELOCATING+*(.rela.data.*)}
       ${RELOCATING+*(.rela.gnu.linkonce.d*)}
     }
   .rel.ctors   ${RELOCATING-0} : { *(.rel.ctors)	}
   .rela.ctors  ${RELOCATING-0} : { *(.rela.ctors)	}
   .rel.dtors   ${RELOCATING-0} : { *(.rel.dtors)	}
   .rela.dtors  ${RELOCATING-0} : { *(.rela.dtors)	}
   .rel.got     ${RELOCATING-0} : { *(.rel.got)		}
   .rela.got    ${RELOCATING-0} : { *(.rela.got)		}
   .rel.bss     ${RELOCATING-0} : { *(.rel.bss)		}
   .rela.bss    ${RELOCATING-0} : { *(.rela.bss)		}
   .rel.plt     ${RELOCATING-0} : { *(.rel.plt)		}
   .rela.plt    ${RELOCATING-0} : { *(.rela.plt)		}
 
   /* Internal text space or external memory */
   .text :
   {
     *(.vectors)
     KEEP(*(.vectors))
 
+    /* For data that needs to reside in the lower 64k of progmem */
+    *(.progmem.gcc*)
+    *(.progmem*)
+    ${RELOCATING+. = ALIGN(2);}
+
+    ${CONSTRUCTING+ __trampolines_start = . ; }
+    /* The jump trampolines for the 16-bit limited relocs will reside here.  */
+    *(.trampolines)
+    *(.trampolines*)
+    ${CONSTRUCTING+ __trampolines_end = . ; }
+
+    /* for future tablejump instruction arrays for 3 byte pc devices.
+       we don't relax jump/call instructions within these sections.  */
+    *(.jumptables) 
+    *(.jumptables*) 
+
+    /* for code that needs to reside in the lower 128k progmem */
+    *(.lowtext)
+    *(.lowtext*)
+
     ${CONSTRUCTING+ __ctors_start = . ; }
     ${CONSTRUCTING+ *(.ctors) }
     ${CONSTRUCTING+ __ctors_end = . ; }
     ${CONSTRUCTING+ __dtors_start = . ; }
     ${CONSTRUCTING+ *(.dtors) }
     ${CONSTRUCTING+ __dtors_end = . ; }
     KEEP(SORT(*)(.ctors))
     KEEP(SORT(*)(.dtors))
 
-    /* For data that needs to reside in the lower 64k of progmem */
-    *(.progmem.gcc*)
-    *(.progmem*)
-    ${RELOCATING+. = ALIGN(2);}
-    
-    /* for future tablejump instruction arrays for 3 byte pc devices */
-    *(.jumptables) 
-    *(.jumptables*) 
-    /* for code that needs to reside in the lower 128k progmem */
-    *(.lowtext)
-    *(.lowtext*)  
+    /* From this point on, we don't bother about wether the insns are
+       below or above the 16 bits boundary.  */
 
     *(.init0)  /* Start here after reset.  */
     KEEP (*(.init0))
     *(.init1)
     KEEP (*(.init1))
     *(.init2)  /* Clear __zero_reg__, set up stack pointer.  */
     KEEP (*(.init2))
     *(.init3)
     KEEP (*(.init3))
     *(.init4)  /* Initialize data and BSS.  */
     KEEP (*(.init4))
     *(.init5)
     KEEP (*(.init5))
     *(.init6)  /* C++ constructors.  */
     KEEP (*(.init6))
     *(.init7)
     KEEP (*(.init7))
     *(.init8)
     KEEP (*(.init8))
     *(.init9)  /* Call main().  */
     KEEP (*(.init9))
     *(.text)
     ${RELOCATING+. = ALIGN(2);}
     *(.text.*)
     ${RELOCATING+. = ALIGN(2);}
     *(.fini9)  /* _exit() starts here.  */
     KEEP (*(.fini9))
     *(.fini8)
     KEEP (*(.fini8))
     *(.fini7)
     KEEP (*(.fini7))
     *(.fini6)  /* C++ destructors.  */
     KEEP (*(.fini6))
     *(.fini5)
     KEEP (*(.fini5))
     *(.fini4)
     KEEP (*(.fini4))
     *(.fini3)
     KEEP (*(.fini3))
     *(.fini2)
     KEEP (*(.fini2))
     *(.fini1)
     KEEP (*(.fini1))
     *(.fini0)  /* Infinite loop after program termination.  */
     KEEP (*(.fini0))
     ${RELOCATING+ _etext = . ; }
   } ${RELOCATING+ > text}
 
   .data	${RELOCATING-0} : ${RELOCATING+AT (ADDR (.text) + SIZEOF (.text))}
   {
     ${RELOCATING+ PROVIDE (__data_start = .) ; }
     *(.data)
     *(.data*)
     *(.rodata)  /* We need to include .rodata here if gcc is used */
     *(.rodata*) /* with -fdata-sections.  */
     *(.gnu.linkonce.d*)
     ${RELOCATING+. = ALIGN(2);}
     ${RELOCATING+ _edata = . ; }
     ${RELOCATING+ PROVIDE (__data_end = .) ; }
   } ${RELOCATING+ > data}
 
   .bss ${RELOCATING+ SIZEOF(.data) + ADDR(.data)} :
--- /dev/null	2004-10-02 05:17:35.000000000 +0200
+++ bfd/elf32-avr.h	2006-04-30 11:03:04.000000000 +0200
@@ -0,0 +1,48 @@
+/* AVR-specific support for 32-bit ELF.
+   Copyright 2006 Free Software Foundation, Inc.
+
+   Written by Bjoern Haase <bjoern.m.haase@web.de>
+
+This file is part of BFD, the Binary File Descriptor library.
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street - Fifth Floor, 
+Boston, MA 02110-1301, USA.  */
+
+
+/* These four functions will be called from the ld back-end.  */
+
+void
+elf32_avr_setup_params (struct bfd_link_info *info,
+                        bfd *avr_stub_bfd,
+                        asection *avr_stub_section,
+                        bfd_boolean no_stubs,
+                        bfd_boolean deb_stubs,
+                        bfd_boolean deb_relax,
+                        bfd_vma pc_wrap_around,
+                        bfd_boolean call_ret_replacement);
+
+int
+elf32_avr_setup_section_lists (bfd *abfd, 
+                               struct bfd_link_info *info);
+
+bfd_boolean
+elf32_avr_size_stubs (bfd *output_bfd,
+                      struct bfd_link_info *info,
+                      bfd_boolean is_prealloc_run);
+
+bfd_boolean
+elf32_avr_build_stubs (struct bfd_link_info *info);
+                       
+
--- /dev/null	2004-10-02 05:17:35.000000000 +0200
+++ ld/emulparams/avr6.sh	2006-04-23 06:31:39.000000000 +0200
@@ -0,0 +1,11 @@
+ARCH=avr:6
+MACHINE=
+SCRIPT_NAME=avr
+OUTPUT_FORMAT="elf32-avr"
+MAXPAGESIZE=1
+EMBEDDED=yes
+TEMPLATE_NAME=elf32
+
+TEXT_LENGTH=1024K
+DATA_LENGTH=0xffa0
+EXTRA_EM_FILE=avrelf
--- /dev/null	2004-10-02 05:17:35.000000000 +0200
+++ ld/emultempl/avrelf.em	2006-05-12 13:13:34.464590264 +0200
@@ -0,0 +1,277 @@
+# This shell script emits a C file. -*- C -*-
+#   Copyright 2006
+#   Free Software Foundation, Inc.
+#
+# This file is part of GLD, the Gnu Linker.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 
+# MA 02110-1301 USA.
+
+# This file is sourced from elf32.em, and defines extra avr-elf
+# specific routines.  It is used to generate the trampolines for the avr6
+# family devices where one needs to address the issue that it is not possible
+# to reach the whole program memory by using 16 bit pointers.
+
+cat >>e${EMULATION_NAME}.c <<EOF
+
+#include "elf32-avr.h"
+#include "ldctor.h"
+
+/* The fake file and it's corresponding section meant to hold 
+   the linker stubs if needed.  */
+static lang_input_statement_type *stub_file;
+static asection *avr_stub_section;
+
+/* Variables set by the command-line parameters and transfered to 
+   the bfd without use of global shared variables.  */
+static bfd_boolean avr_no_stubs = FALSE;
+static bfd_boolean avr_debug_relax = FALSE;
+static bfd_boolean avr_debug_stubs = FALSE;
+static bfd_boolean avr_replace_call_ret_sequences = TRUE;
+static bfd_vma avr_pc_wrap_around = 0x10000000;
+
+/* Transfers information to the bfd frontend.  */
+static void
+avr_elf_set_global_bfd_parameters (void)
+{
+  elf32_avr_setup_params (&link_info,
+                          stub_file->the_bfd,
+                          avr_stub_section,
+                          avr_no_stubs,
+                          avr_debug_stubs,
+                          avr_debug_relax,
+                          avr_pc_wrap_around,
+                          avr_replace_call_ret_sequences);
+}
+
+
+/* Makes a conservative estimate of the trampoline section size that could
+   be corrected later on.  */
+static void
+avr_elf_${EMULATION_NAME}_before_allocation (void)
+{
+  int ret;
+
+  gld${EMULATION_NAME}_before_allocation ();
+
+  avr_elf_set_global_bfd_parameters ();
+
+  /* If generating a relocatable output file, then we don't
+     have to generate the trampolines.  */
+  if (link_info.relocatable)
+    {
+      avr_no_stubs = TRUE;
+      return;
+    }
+
+  /* We only need stubs for the avr6 family.  */
+  if (strcmp ("${EMULATION_NAME}","avr6"))
+    {
+      avr_no_stubs = TRUE;
+      return;
+    }
+
+  if (avr_no_stubs)
+    ret = 0;
+  else
+    ret = elf32_avr_setup_section_lists (output_bfd, &link_info);
+
+  if (ret != 0)
+    {
+      if (ret < 0)
+        {
+          einfo ("%X%P: can not setup the input section list: %E\n");
+          return;
+        }
+
+      /* Call into the BFD backend to do the real "stub"-work.  */
+      if (!elf32_avr_size_stubs (output_bfd,&link_info,TRUE))
+        {
+          einfo ("%X%P: can not size stub section: %E\n");
+          return;
+        }
+    }
+}
+
+/* This is called before the input files are opened.  We create a new
+   fake input file to hold the stub section and generate the section itself.  */
+
+static void
+avr_elf_create_output_section_statements (void)
+{
+  flagword flags;
+
+  stub_file = lang_add_input_file ("linker stubs",
+                                   lang_input_file_is_fake_enum,
+                                   NULL);
+
+  stub_file->the_bfd = bfd_create ("linker stubs", output_bfd);
+  if (stub_file->the_bfd == NULL
+      || !bfd_set_arch_mach (stub_file->the_bfd,
+                             bfd_get_arch (output_bfd),
+                             bfd_get_mach (output_bfd)))
+    {
+      einfo ("%X%P: can not create stub BFD %E\n");
+      return;
+    }
+
+  /* Now we add the stub section.  */
+
+  avr_stub_section = bfd_make_section_anyway (stub_file->the_bfd,
+                                              ".trampolines");
+  if (avr_stub_section == NULL)
+    goto err_ret;
+  
+  flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
+           | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP);
+  if (!bfd_set_section_flags (stub_file->the_bfd, avr_stub_section, flags))
+    goto err_ret;
+
+  avr_stub_section->alignment_power = 1;
+  
+  ldlang_add_file (stub_file);
+
+  return;
+
+  err_ret:
+   einfo ("%X%P: can not make stub section: %E\n");
+   return;
+}
+
+/* Re-calculates the size of the stubs so that we won't waste space.  */
+static void
+avr_elf_finish (void)
+{ 
+  if (!avr_no_stubs)
+    {
+      /* Now build the linker stubs.  */
+      if (stub_file->the_bfd->sections != NULL)
+       {
+         /* Call again the trampoline analyzer to initialize the trampoline
+            stubs with the correct symbol addresses.  Since there could have
+            been relaxation, the symbol addresses that were found during
+            first call may no longer be correct.  */
+         if (!elf32_avr_size_stubs (output_bfd,&link_info,FALSE))
+           {
+             einfo ("%X%P: can not size stub section: %E\n");
+             return;
+           }
+         if (!elf32_avr_build_stubs (&link_info))
+           einfo ("%X%P: can not build stubs: %E\n");
+       }
+    }
+
+  gld${EMULATION_NAME}_finish ();
+}
+
+
+EOF
+
+
+PARSE_AND_LIST_PROLOGUE='
+
+#define OPTION_NO_CALL_RET_REPLACEMENT 301
+#define OPTION_PMEM_WRAP_AROUND        302
+#define OPTION_NO_STUBS                303
+#define OPTION_DEBUG_STUBS             304
+#define OPTION_DEBUG_RELAX             305
+'
+
+PARSE_AND_LIST_LONGOPTS='
+  { "no-call-ret-replacement", no_argument, 
+     NULL, OPTION_NO_CALL_RET_REPLACEMENT},
+  { "pmem-wrap-around", required_argument, 
+    NULL, OPTION_PMEM_WRAP_AROUND},
+  { "no-stubs", no_argument, 
+    NULL, OPTION_NO_STUBS},
+  { "debug-stubs", no_argument, 
+    NULL, OPTION_DEBUG_STUBS},
+  { "debug-relax", no_argument, 
+    NULL, OPTION_DEBUG_RELAX},
+'
+
+PARSE_AND_LIST_OPTIONS='
+  fprintf (file, _("     --pmem-wrap-around=<val> "
+                           "Make the linker relaxation machine assume that a\n"
+                   "                              "
+                           "program counter wrap-around occures at address\n"
+                   "                              "
+                           "<val>. Supported values are 16k, 32k and 64k.\n"));
+  fprintf (file, _("     --no-call-ret-replacement "
+                           "The relaxation machine normally will\n"
+                   "                               "
+                           "substitute two immediately following call/ret\n"
+                   "                               "
+                           "instructions by a single jump instruction.\n"
+                   "                               "
+                           "This option disables this optimization.\n"));
+  fprintf (file, _("     --no-stubs "
+                           "If the linker detects to attempt to access\n"
+                   "                               "
+                           "an instruction beyond 128k by a reloc that\n"
+                   "                               "
+                           "is limited to 128k max, it inserts a jump\n"
+                   "                               "
+                           "stub. You can de-active this with this switch.\n"));
+  fprintf (file, _("     --debug-stubs Used for debugging avr-ld.\n"));
+  fprintf (file, _("     --debug-relax Used for debugging avr-ld.\n"));
+'
+
+PARSE_AND_LIST_ARGS_CASES='
+
+    case OPTION_PMEM_WRAP_AROUND:
+      { 
+        /* This variable is defined in the bfd library.  */
+        if ((!strcmp (optarg,"32k"))
+            || (!strcmp (optarg,"32K")))
+          avr_pc_wrap_around = 32768;
+        else if ((!strcmp (optarg,"16k"))
+                 || (!strcmp (optarg,"16K")))
+               avr_pc_wrap_around = 16384;
+        else if ((!strcmp (optarg,"64k"))
+                 || (!strcmp (optarg,"64K")))
+               avr_pc_wrap_around = 0x10000;
+        else
+           return FALSE;
+      }
+      break;
+
+    case OPTION_DEBUG_STUBS:
+      avr_debug_stubs = TRUE;
+      break;
+
+    case OPTION_DEBUG_RELAX:
+      avr_debug_relax = TRUE;
+      break;
+
+    case OPTION_NO_STUBS:
+      avr_no_stubs = TRUE;
+      break;
+
+    case OPTION_NO_CALL_RET_REPLACEMENT:
+      {
+        /* This variable is defined in the bfd library.  */
+        avr_replace_call_ret_sequences = FALSE;
+      }
+      break;
+'
+
+#
+# Put these extra avr-elf routines in ld_${EMULATION_NAME}_emulation
+#
+LDEMUL_BEFORE_ALLOCATION=avr_elf_${EMULATION_NAME}_before_allocation
+LDEMUL_FINISH=avr_elf_finish
+LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=avr_elf_create_output_section_statements
+

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