home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / bfd / elf32-sparc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-26  |  28.4 KB  |  845 lines

  1. /* SPARC-specific support for 32-bit ELF
  2.    Copyright 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of BFD, the Binary File Descriptor library.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "bfd.h"
  21. #include "sysdep.h"
  22. #include "bfdlink.h"
  23. #include "libbfd.h"
  24. #include "libelf.h"
  25.  
  26. static CONST struct reloc_howto_struct *bfd_elf32_bfd_reloc_type_lookup
  27.   PARAMS ((bfd *, bfd_reloc_code_real_type));
  28. static void elf_info_to_howto
  29.   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
  30. static boolean elf32_sparc_create_dynamic_sections
  31.   PARAMS ((bfd *, struct bfd_link_info *));
  32. static boolean elf32_sparc_adjust_dynamic_symbol
  33.   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
  34. static boolean elf32_sparc_allocate_dynamic_section
  35.   PARAMS ((bfd *, const char *));
  36. static boolean elf32_sparc_size_dynamic_sections
  37.   PARAMS ((bfd *, struct bfd_link_info *));
  38. static boolean elf32_sparc_relocate_section
  39.   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  40.        Elf_Internal_Rela *, Elf_Internal_Sym *, asection **, char *));
  41. static boolean elf32_sparc_finish_dynamic_symbol
  42.   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
  43.        Elf_Internal_Sym *));
  44. static boolean elf32_sparc_finish_dynamic_sections
  45.   PARAMS ((bfd *, struct bfd_link_info *));
  46.  
  47. enum reloc_type
  48.   {
  49.     R_SPARC_NONE = 0,
  50.     R_SPARC_8,        R_SPARC_16,        R_SPARC_32, 
  51.     R_SPARC_DISP8,    R_SPARC_DISP16,        R_SPARC_DISP32, 
  52.     R_SPARC_WDISP30,    R_SPARC_WDISP22,
  53.     R_SPARC_HI22,    R_SPARC_22,
  54.     R_SPARC_13,        R_SPARC_LO10,
  55.     R_SPARC_GOT10,    R_SPARC_GOT13,        R_SPARC_GOT22,
  56.     R_SPARC_PC10,    R_SPARC_PC22,
  57.     R_SPARC_WPLT30,
  58.     R_SPARC_COPY,
  59.     R_SPARC_GLOB_DAT,    R_SPARC_JMP_SLOT,
  60.     R_SPARC_RELATIVE,
  61.     R_SPARC_UA32,
  62.     R_SPARC_max
  63.   };
  64.  
  65. #if 0
  66. static CONST char *CONST reloc_type_names[] =
  67. {
  68.   "R_SPARC_NONE",
  69.   "R_SPARC_8",        "R_SPARC_16",        "R_SPARC_32",
  70.   "R_SPARC_DISP8",    "R_SPARC_DISP16",    "R_SPARC_DISP32",
  71.   "R_SPARC_WDISP30",    "R_SPARC_WDISP22",
  72.   "R_SPARC_HI22",    "R_SPARC_22",
  73.   "R_SPARC_13",        "R_SPARC_LO10",
  74.   "R_SPARC_GOT10",    "R_SPARC_GOT13",    "R_SPARC_GOT22",
  75.   "R_SPARC_PC10",    "R_SPARC_PC22",
  76.   "R_SPARC_WPLT30",
  77.   "R_SPARC_COPY",
  78.   "R_SPARC_GLOB_DAT",    "R_SPARC_JMP_SLOT",
  79.   "R_SPARC_RELATIVE",
  80.   "R_SPARC_UA32",
  81. };
  82. #endif
  83.  
  84. static reloc_howto_type elf_sparc_howto_table[] = 
  85. {
  86.   HOWTO(R_SPARC_NONE,    0,0, 0,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_NONE",    false,0,0x00000000,true),
  87.   HOWTO(R_SPARC_8,       0,0, 8,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_8",       false,0,0x000000ff,true),
  88.   HOWTO(R_SPARC_16,      0,1,16,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_16",      false,0,0x0000ffff,true),
  89.   HOWTO(R_SPARC_32,      0,2,32,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_32",      false,0,0xffffffff,true),
  90.   HOWTO(R_SPARC_DISP8,   0,0, 8,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,"R_SPARC_DISP8",   false,0,0x000000ff,true),
  91.   HOWTO(R_SPARC_DISP16,  0,1,16,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,"R_SPARC_DISP16",  false,0,0x0000ffff,true),
  92.   HOWTO(R_SPARC_DISP32,  0,2,32,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,"R_SPARC_DISP32",  false,0,0x00ffffff,true),
  93.   HOWTO(R_SPARC_WDISP30, 2,2,30,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,"R_SPARC_WDISP30", false,0,0x3fffffff,true),
  94.   HOWTO(R_SPARC_WDISP22, 2,2,22,true, 0,complain_overflow_signed,  bfd_elf_generic_reloc,"R_SPARC_WDISP22", false,0,0x003fffff,true),
  95.   HOWTO(R_SPARC_HI22,   10,2,22,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_HI22",    false,0,0x003fffff,true),
  96.   HOWTO(R_SPARC_22,      0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_22",      false,0,0x003fffff,true),
  97.   HOWTO(R_SPARC_13,      0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_13",      false,0,0x00001fff,true),
  98.   HOWTO(R_SPARC_LO10,    0,2,10,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_LO10",    false,0,0x000003ff,true),
  99.   HOWTO(R_SPARC_GOT10,   0,2,10,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_GOT10",   false,0,0x000003ff,true),
  100.   HOWTO(R_SPARC_GOT13,   0,2,13,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_GOT13",   false,0,0x00001fff,true),
  101.   HOWTO(R_SPARC_GOT22,  10,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_GOT22",   false,0,0x003fffff,true),
  102.   HOWTO(R_SPARC_PC10,    0,2,10,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_PC10",    false,0,0x000003ff,true),
  103.   HOWTO(R_SPARC_PC22,    0,2,22,false,0,complain_overflow_bitfield,bfd_elf_generic_reloc,"R_SPARC_PC22",    false,0,0x003fffff,true),
  104.   HOWTO(R_SPARC_WPLT30,  0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_WPLT30",  false,0,0x00000000,true),
  105.   HOWTO(R_SPARC_COPY,    0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_COPY",    false,0,0x00000000,true),
  106.   HOWTO(R_SPARC_GLOB_DAT,0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_GLOB_DAT",false,0,0x00000000,true),
  107.   HOWTO(R_SPARC_JMP_SLOT,0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_JMP_SLOT",false,0,0x00000000,true),
  108.   HOWTO(R_SPARC_RELATIVE,0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_RELATIVE",false,0,0x00000000,true),
  109.   HOWTO(R_SPARC_UA32,    0,0,00,false,0,complain_overflow_dont,    bfd_elf_generic_reloc,"R_SPARC_UA32",    false,0,0x00000000,true),
  110. };
  111.  
  112. struct elf_reloc_map {
  113.   unsigned char bfd_reloc_val;
  114.   unsigned char elf_reloc_val;
  115. };
  116.  
  117. static CONST struct elf_reloc_map sparc_reloc_map[] =
  118. {
  119.   { BFD_RELOC_NONE, R_SPARC_NONE, },
  120.   { BFD_RELOC_16, R_SPARC_16, },
  121.   { BFD_RELOC_8, R_SPARC_8 },
  122.   { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
  123.   { BFD_RELOC_CTOR, R_SPARC_32 }, /* @@ Assumes 32 bits.  */
  124.   { BFD_RELOC_32, R_SPARC_32 },
  125.   { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
  126.   { BFD_RELOC_HI22, R_SPARC_HI22 },
  127.   { BFD_RELOC_LO10, R_SPARC_LO10, },
  128.   { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
  129.   { BFD_RELOC_SPARC22, R_SPARC_22 },
  130.   { BFD_RELOC_SPARC13, R_SPARC_13 },
  131.   { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
  132.   { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
  133.   { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
  134.   { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
  135.   { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
  136.   { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
  137.   { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
  138.   { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
  139.   { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
  140.   { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
  141.   { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
  142.   /*  { BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */
  143. };
  144.  
  145. static CONST struct reloc_howto_struct *
  146. bfd_elf32_bfd_reloc_type_lookup (abfd, code)
  147.      bfd *abfd;
  148.      bfd_reloc_code_real_type code;
  149. {
  150.   int i;
  151.   for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct elf_reloc_map); i++)
  152.     {
  153.       if (sparc_reloc_map[i].bfd_reloc_val == code)
  154.     return &elf_sparc_howto_table[(int) sparc_reloc_map[i].elf_reloc_val];
  155.     }
  156.   return 0;
  157. }
  158.  
  159. static void
  160. elf_info_to_howto (abfd, cache_ptr, dst)
  161.      bfd *abfd;
  162.      arelent *cache_ptr;
  163.      Elf_Internal_Rela *dst;
  164. {
  165.   BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_SPARC_max);
  166.   cache_ptr->howto = &elf_sparc_howto_table[ELF32_R_TYPE(dst->r_info)];
  167. }
  168.  
  169.  
  170. /* Functions for the SPARC ELF linker.  */
  171.  
  172. /* The name of the dynamic interpreter.  This is put in the .interp
  173.    section.  */
  174.  
  175. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
  176.  
  177. /* The nop opcode we use.  */
  178.  
  179. #define SPARC_NOP 0x01000000
  180.  
  181. /* The size in bytes of an entry in the procedure linkage table.  */
  182.  
  183. #define PLT_ENTRY_SIZE 12
  184.  
  185. /* The first four entries in a procedure linkage table are reserved,
  186.    and the initial contents are unimportant (we zero them out).
  187.    Subsequent entries look like this.  See the SVR4 ABI SPARC
  188.    supplement to see how this works.  */
  189.  
  190. /* sethi %hi(.-.plt0),%g1.  We fill in the address later.  */
  191. #define PLT_ENTRY_WORD0 0x03000000
  192. /* b,a .plt0.  We fill in the offset later.  */
  193. #define PLT_ENTRY_WORD1 0x30800000
  194. /* nop.  */
  195. #define PLT_ENTRY_WORD2 SPARC_NOP
  196.  
  197. /* Create dynamic sections when linking against a dynamic object.  */
  198.  
  199. static boolean
  200. elf32_sparc_create_dynamic_sections (abfd, info)
  201.      bfd *abfd;
  202.      struct bfd_link_info *info;
  203. {
  204.   flagword flags;
  205.   register asection *s;
  206.   struct elf_link_hash_entry *h;
  207.  
  208.   /* We need to create .plt, .rela.plt, .got, .dynbss, and .rela.bss
  209.      sections.  */
  210.  
  211.   flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
  212.  
  213.   s = bfd_make_section (abfd, ".plt");
  214.   if (s == NULL
  215.       || ! bfd_set_section_flags (abfd, s, flags | SEC_CODE)
  216.       || ! bfd_set_section_alignment (abfd, s, 2))
  217.     return false;
  218.  
  219.   /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
  220.      .plt section.  */
  221.   h = NULL;
  222.   if (! (_bfd_generic_link_add_one_symbol
  223.      (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, (bfd_vma) 0,
  224.       (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
  225.       (struct bfd_link_hash_entry **) &h)))
  226.     return false;
  227.   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
  228.  
  229.   /* The first four entries in .plt are reserved.  */
  230.   s->_raw_size = 4 * PLT_ENTRY_SIZE;
  231.  
  232.   s = bfd_make_section (abfd, ".rela.plt");
  233.   if (s == NULL
  234.       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
  235.       || ! bfd_set_section_alignment (abfd, s, 2))
  236.     return false;
  237.  
  238.   s = bfd_make_section (abfd, ".got");
  239.   if (s == NULL
  240.       || ! bfd_set_section_flags (abfd, s, flags)
  241.       || ! bfd_set_section_alignment (abfd, s, 2))
  242.     return false;
  243.  
  244.   /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
  245.      section.  We don't do this in the linker script because we don't
  246.      want to define the symbol if we are not creating a global offset
  247.      table.  FIXME: The Solaris linker puts _GLOBAL_OFFSET_TABLE_ at
  248.      the start of the .got section, but when using the small PIC model
  249.      the .got is accessed using a signed 13 bit offset.  Shouldn't
  250.      _GLOBAL_OFFSET_TABLE_ be located at .got + 4096?  */
  251.   h = NULL;
  252.   if (! (_bfd_generic_link_add_one_symbol
  253.      (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, (bfd_vma) 0,
  254.       (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
  255.       (struct bfd_link_hash_entry **) &h)))
  256.     return false;
  257.   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
  258.  
  259.   /* The first global offset table entry is reserved.  */
  260.   s->_raw_size += 4;
  261.  
  262.   /* The .dynbss section is a place to put symbols which are defined
  263.      by dynamic objects, are referenced by regular objects, and are
  264.      not functions.  We must allocate space for them in the process
  265.      image and use a R_SPARC_COPY reloc to tell the dynamic linker to
  266.      initialize them at run time.  The linker script puts the .dynbss
  267.      section into the .bss section of the final image.  */
  268.   s = bfd_make_section (abfd, ".dynbss");
  269.   if (s == NULL
  270.       || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
  271.     return false;
  272.  
  273.   /* The .rela.bss section holds copy relocs.  */
  274.   s = bfd_make_section (abfd, ".rela.bss");
  275.   if (s == NULL
  276.       || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
  277.       || ! bfd_set_section_alignment (abfd, s, 2))
  278.     return false;
  279.  
  280.   return true;
  281. }
  282.  
  283. /* Adjust a symbol defined by a dynamic object and referenced by a
  284.    regular object.  The current definition is in some section of the
  285.    dynamic object, but we're not including those sections.  We have to
  286.    change the definition to something the rest of the link can
  287.    understand.  */
  288.  
  289. static boolean
  290. elf32_sparc_adjust_dynamic_symbol (info, h)
  291.      struct bfd_link_info *info;
  292.      struct elf_link_hash_entry *h;
  293. {
  294.   bfd *dynobj;
  295.   asection *s;
  296.   unsigned int power_of_two;
  297.   size_t align;
  298.  
  299.   dynobj = elf_hash_table (info)->dynobj;
  300.  
  301.   /* Make sure we know what is going on here.  */
  302.   BFD_ASSERT (dynobj != NULL
  303.           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
  304.           && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0
  305.           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
  306.           && h->root.type == bfd_link_hash_defined
  307.           && (bfd_get_flavour (h->root.u.def.section->owner)
  308.           == bfd_target_elf_flavour)
  309.           && (elf_elfheader (h->root.u.def.section->owner)->e_type
  310.           == ET_DYN)
  311.           && h->root.u.def.section->output_section == NULL);
  312.  
  313.   /* If this is a function, put it in the procedure linkage table.  We
  314.      will fill in the contents of the procedure linkage table later
  315.      (although we could actually do it here).  */
  316.   if (h->type == STT_FUNC)
  317.     {
  318.       s = bfd_get_section_by_name (dynobj, ".plt");
  319.       BFD_ASSERT (s != NULL);
  320.  
  321.       /* The procedure linkage table has a maximum size.  */
  322.       if (s->_raw_size >= 0x400000)
  323.     {
  324.       bfd_set_error (bfd_error_bad_value);
  325.       return false;
  326.     }
  327.  
  328.       /* Set the symbol to this location in the .plt.  */
  329.       h->root.u.def.section = s;
  330.       h->root.u.def.value = s->_raw_size;
  331.  
  332.       /* Make room for this entry.  */
  333.       s->_raw_size += PLT_ENTRY_SIZE;
  334.  
  335.       /* We also need to make an entry in the .rela.plt section.  */
  336.  
  337.       s = bfd_get_section_by_name (dynobj, ".rela.plt");
  338.       BFD_ASSERT (s != NULL);
  339.       s->_raw_size += sizeof (Elf32_External_Rela);
  340.  
  341.       return true;
  342.     }
  343.  
  344.   /* If this is a weak symbol, and there is a real definition, the
  345.      processor independent code will have arranged for us to see the
  346.      real definition first, and we can just use the same value.  */
  347.   if (h->weakdef != NULL)
  348.     {
  349.       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined);
  350.       h->root.u.def.section = h->weakdef->root.u.def.section;
  351.       h->root.u.def.value = h->weakdef->root.u.def.value;
  352.       h->align = (bfd_size_type) -1;
  353.       return true;
  354.     }
  355.  
  356.   /* This is a reference to a symbol defined by a dynamic object which
  357.      is not a function.  We must allocate it in our .dynbss section,
  358.      which will become part of the .bss section of the executable.
  359.      There will be an entry for this symbol in the .dynsym section.
  360.      The dynamic object will contain position independent code, so all
  361.      references from the dynamic object to this symbol will go through
  362.      the global offset table.  The dynamic linker will use the .dynsym
  363.      entry to determine the address it must put in the global offset
  364.      table, so both the dynamic object and the regular object will
  365.      refer to the same memory location for the variable.  */
  366.  
  367.   s = bfd_get_section_by_name (dynobj, ".dynbss");
  368.   BFD_ASSERT (s != NULL);
  369.  
  370.   /* If the symbol is currently defined in the .bss section of the
  371.      dynamic object, then it is OK to simply initialize it to zero.
  372.      If the symbol is in some other section, we must generate a
  373.      R_SPARC_COPY reloc to tell the dynamic linker to copy the initial
  374.      value out of the dynamic object and into the runtime process
  375.      image.  We need to remember the offset into the .rel.bss section
  376.      we are going to use, and we coopt the align field for this
  377.      purpose (the align field is only used for common symbols, and
  378.      these symbols are always defined).  It would be cleaner to use a
  379.      new field, but that would waste memory.  */
  380.   if ((h->root.u.def.section->flags & SEC_LOAD) == 0)
  381.     h->align = (bfd_size_type) -1;
  382.   else
  383.     {
  384.       asection *srel;
  385.  
  386.       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
  387.       BFD_ASSERT (srel != NULL);
  388.       h->align = srel->_raw_size;
  389.       srel->_raw_size += sizeof (Elf32_External_Rela);
  390.     }
  391.  
  392.   /* We need to figure out the alignment required for this symbol.  I
  393.      have no idea how ELF linkers handle this.  */
  394.   switch (h->size)
  395.     {
  396.     case 0:
  397.     case 1:
  398.       power_of_two = 0;
  399.       align = 1;
  400.       break;
  401.     case 2:
  402.       power_of_two = 1;
  403.       align = 2;
  404.       break;
  405.     case 3:
  406.     case 4:
  407.       power_of_two = 2;
  408.       align = 4;
  409.       break;
  410.     case 5:
  411.     case 6:
  412.     case 7:
  413.     case 8:
  414.       power_of_two = 3;
  415.       align = 8;
  416.       break;
  417.     default:
  418.       power_of_two = 4;
  419.       align = 16;
  420.       break;
  421.     }
  422.  
  423.   /* Apply the required alignment.  */
  424.   s->_raw_size = BFD_ALIGN (s->_raw_size, align);
  425.   if (power_of_two > bfd_get_section_alignment (dynobj, s))
  426.     {
  427.       if (! bfd_set_section_alignment (dynobj, s, power_of_two))
  428.     return false;
  429.     }
  430.  
  431.   /* Define the symbol as being at this point in the section.  */
  432.   h->root.u.def.section = s;
  433.   h->root.u.def.value = s->_raw_size;
  434.  
  435.   /* Increment the section size to make room for the symbol.  */
  436.   s->_raw_size += h->size;
  437.  
  438.   return true;
  439. }
  440.  
  441. /* Allocate contents for a section.  */
  442.  
  443. static INLINE boolean
  444. elf32_sparc_allocate_dynamic_section (dynobj, name)
  445.      bfd *dynobj;
  446.      const char *name;
  447. {
  448.   register asection *s;
  449.  
  450.   s = bfd_get_section_by_name (dynobj, name);
  451.   BFD_ASSERT (s != NULL);
  452.   s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
  453.   if (s->contents == NULL && s->_raw_size != 0)
  454.     {
  455.       bfd_set_error (bfd_error_no_memory);
  456.       return false;
  457.     }
  458.   return true;
  459. }
  460.  
  461. /* Set the sizes of the dynamic sections.  */
  462.  
  463. static boolean
  464. elf32_sparc_size_dynamic_sections (output_bfd, info)
  465.      bfd *output_bfd;
  466.      struct bfd_link_info *info;
  467. {
  468.   bfd *dynobj;
  469.   asection *s;
  470.  
  471.   dynobj = elf_hash_table (info)->dynobj;
  472.   BFD_ASSERT (dynobj != NULL);
  473.  
  474.   /* Set the contents of the .interp section to the interpreter.  */
  475.   if (! info->shared)
  476.     {
  477.       s = bfd_get_section_by_name (dynobj, ".interp");
  478.       BFD_ASSERT (s != NULL);
  479.       s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
  480.       s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  481.     }
  482.  
  483.   /* Make space for the trailing nop in .plt.  */
  484.   s = bfd_get_section_by_name (dynobj, ".plt");
  485.   BFD_ASSERT (s != NULL);
  486.   s->_raw_size += 4;
  487.  
  488.   /* The adjust_dynamic_symbol entry point has determined the sizes of
  489.      the various dynamic sections.  Allocate some memory for them to
  490.      hold contents.  */
  491.   if (! elf32_sparc_allocate_dynamic_section (dynobj, ".plt")
  492.       || ! elf32_sparc_allocate_dynamic_section (dynobj, ".rela.plt")
  493.       || ! elf32_sparc_allocate_dynamic_section (dynobj, ".got")
  494.       || ! elf32_sparc_allocate_dynamic_section (dynobj, ".rela.bss"))
  495.     return false;
  496.  
  497.   /* Add some entries to the .dynamic section.  We fill in the values
  498.      later, in elf32_sparc_finish_dynamic_sections, but we must add
  499.      the entries now so that we get the correct size for the .dynamic
  500.      section.  The DT_DEBUG entry is filled in by the dynamic linker
  501.      and used by the debugger.  */
  502.   if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0)
  503.       || ! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
  504.       || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
  505.       || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
  506.       || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0)
  507.       || ! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
  508.       || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
  509.       || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
  510.                     sizeof (Elf32_External_Rela)))
  511.     return false;
  512.  
  513.   return true;
  514. }
  515.  
  516. /* Relocate a SPARC ELF section.  */
  517.  
  518. static boolean
  519. elf32_sparc_relocate_section (output_bfd, info, input_bfd, input_section,
  520.                   contents, relocs, local_syms, local_sections,
  521.                   output_names)
  522.      bfd *output_bfd;
  523.      struct bfd_link_info *info;
  524.      bfd *input_bfd;
  525.      asection *input_section;
  526.      bfd_byte *contents;
  527.      Elf_Internal_Rela *relocs;
  528.      Elf_Internal_Sym *local_syms;
  529.      asection **local_sections;
  530.      char *output_names;
  531. {
  532.   Elf_Internal_Shdr *symtab_hdr;
  533.   struct elf_link_hash_entry **sym_hashes;
  534.   Elf_Internal_Rela *rel;
  535.   Elf_Internal_Rela *relend;
  536.  
  537.   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  538.   sym_hashes = elf_sym_hashes (input_bfd);
  539.  
  540.   rel = relocs;
  541.   relend = relocs + input_section->reloc_count;
  542.   for (; rel < relend; rel++)
  543.     {
  544.       int r_type;
  545.       const reloc_howto_type *howto;
  546.       long r_symndx;
  547.       struct elf_link_hash_entry *h;
  548.       Elf_Internal_Sym *sym;
  549.       asection *sec;
  550.       bfd_vma relocation;
  551.       bfd_reloc_status_type r;
  552.  
  553.       r_type = ELF32_R_TYPE (rel->r_info);
  554.       if (r_type < 0 || r_type >= (int) R_SPARC_max)
  555.     {
  556.       bfd_set_error (bfd_error_bad_value);
  557.       return false;
  558.     }
  559.       howto = elf_sparc_howto_table + r_type;
  560.  
  561.       r_symndx = ELF32_R_SYM (rel->r_info);
  562.  
  563.       if (info->relocateable)
  564.     {
  565.       /* This is a relocateable link.  We don't have to change
  566.          anything, unless the reloc is against a section symbol,
  567.          in which case we have to adjust according to where the
  568.          section symbol winds up in the output section.  */
  569.       if (r_symndx < symtab_hdr->sh_info)
  570.         {
  571.           sym = local_syms + r_symndx;
  572.           if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  573.         {
  574.           sec = local_sections[r_symndx];
  575.           rel->r_addend += sec->output_offset + sym->st_value;
  576.         }
  577.         }
  578.  
  579.       continue;
  580.     }
  581.  
  582.       /* This is a final link.  */
  583.       h = NULL;
  584.       sym = NULL;
  585.       sec = NULL;
  586.       if (r_symndx < symtab_hdr->sh_info)
  587.     {
  588.       sym = local_syms + r_symndx;
  589.       sec = local_sections[r_symndx];
  590.       relocation = (sec->output_section->vma
  591.             + sec->output_offset
  592.             + sym->st_value);
  593.     }
  594.       else
  595.     {
  596.       long indx;
  597.  
  598.       indx = r_symndx - symtab_hdr->sh_info;
  599.       h = sym_hashes[indx];
  600.       if (h->root.type == bfd_link_hash_defined)
  601.         {
  602.           sec = h->root.u.def.section;
  603.           relocation = (h->root.u.def.value
  604.                 + sec->output_section->vma
  605.                 + sec->output_offset);
  606.         }
  607.       else if (h->root.type == bfd_link_hash_weak)
  608.         relocation = 0;
  609.       else
  610.         {
  611.           if (! ((*info->callbacks->undefined_symbol)
  612.              (info, h->root.root.string, input_bfd,
  613.               input_section, rel->r_offset)))
  614.         return false;
  615.           relocation = 0;
  616.         }
  617.     }
  618.  
  619.       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  620.                     contents, rel->r_offset,
  621.                     relocation, rel->r_addend);
  622.  
  623.       if (r != bfd_reloc_ok)
  624.     {
  625.       switch (r)
  626.         {
  627.         default:
  628.         case bfd_reloc_outofrange:
  629.           abort ();
  630.         case bfd_reloc_overflow:
  631.           {
  632.         const char *name;
  633.  
  634.         if (h != NULL)
  635.           name = h->root.root.string;
  636.         else
  637.           {
  638.             name = output_names + sym->st_name;
  639.             if (name == NULL)
  640.               return false;
  641.             if (*name == '\0')
  642.               name = bfd_section_name (input_bfd, sec);
  643.           }
  644.         if (! ((*info->callbacks->reloc_overflow)
  645.                (info, name, howto->name, (bfd_vma) 0,
  646.             input_bfd, input_section, rel->r_offset)))
  647.           return false;
  648.           }
  649.           break;
  650.         }
  651.     }
  652.     }
  653.  
  654.   return true;
  655. }
  656.  
  657. /* Finish up dynamic symbol handling.  We set the contents of various
  658.    dynamic sections here.  */
  659.  
  660. static boolean
  661. elf32_sparc_finish_dynamic_symbol (output_bfd, info, h, sym)
  662.      bfd *output_bfd;
  663.      struct bfd_link_info *info;
  664.      struct elf_link_hash_entry *h;
  665.      Elf_Internal_Sym *sym;
  666. {
  667.   /* If this symbol is not defined by a dynamic object, or is not
  668.      referenced by a regular object, ignore it.  */
  669.   if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
  670.       || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
  671.       || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
  672.     {
  673.       /* Mark some specially defined symbols as absolute.  */
  674.       if (strcmp (h->root.root.string, "_DYNAMIC") == 0
  675.       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
  676.       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
  677.     sym->st_shndx = SHN_ABS;
  678.       return true;
  679.     }
  680.  
  681.   BFD_ASSERT (h->root.type == bfd_link_hash_defined);
  682.   BFD_ASSERT (h->dynindx != -1);
  683.  
  684.   if (h->type == STT_FUNC)
  685.     {
  686.       asection *splt;
  687.       asection *srela;
  688.       Elf_Internal_Rela rela;
  689.  
  690.       splt = h->root.u.def.section;
  691.       BFD_ASSERT (strcmp (bfd_get_section_name (splt->owner, splt), ".plt")
  692.           == 0);
  693.       srela = bfd_get_section_by_name (splt->owner, ".rela.plt");
  694.       BFD_ASSERT (srela != NULL);
  695.  
  696.       /* Fill in the entry in the procedure linkage table.  */
  697.       bfd_put_32 (output_bfd,
  698.           PLT_ENTRY_WORD0 + h->root.u.def.value,
  699.           splt->contents + h->root.u.def.value);
  700.       bfd_put_32 (output_bfd,
  701.           (PLT_ENTRY_WORD1
  702.            + (((- (h->root.u.def.value + 4)) >> 2) & 0x3fffff)),
  703.           splt->contents + h->root.u.def.value + 4);
  704.       bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
  705.           splt->contents + h->root.u.def.value + 8);
  706.  
  707.       /* Fill in the entry in the .rela.plt section.  */
  708.       rela.r_offset = (splt->output_section->vma
  709.                + splt->output_offset
  710.                + h->root.u.def.value);
  711.       rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_JMP_SLOT);
  712.       rela.r_addend = 0;
  713.       bfd_elf32_swap_reloca_out (output_bfd, &rela,
  714.                  ((Elf32_External_Rela *) srela->contents
  715.                   + (h->root.u.def.value / PLT_ENTRY_SIZE
  716.                      - 4)));
  717.  
  718.       /* Mark the symbol as undefined, rather than as defined in the
  719.      .plt section.  Leave the value alone.  */
  720.       sym->st_shndx = SHN_UNDEF;
  721.     }
  722.   else
  723.     {
  724.       /* This is not a function.  We have already allocated memory for
  725.      it in the .bss section (via .dynbss).  All we have to do here
  726.      is create a COPY reloc if required.  */
  727.       if (h->align != (bfd_size_type) -1)
  728.     {
  729.       asection *s;
  730.       Elf_Internal_Rela rela;
  731.  
  732.       s = bfd_get_section_by_name (h->root.u.def.section->owner,
  733.                        ".rela.bss");
  734.       BFD_ASSERT (s != NULL);
  735.  
  736.       rela.r_offset = (h->root.u.def.value
  737.                + h->root.u.def.section->output_section->vma
  738.                + h->root.u.def.section->output_offset);
  739.       rela.r_info = ELF32_R_INFO (h->dynindx, R_SPARC_COPY);
  740.       rela.r_addend = 0;
  741.       bfd_elf32_swap_reloca_out (output_bfd, &rela,
  742.                      ((Elf32_External_Rela *)
  743.                       (s->contents + h->align)));
  744.     }
  745.     }
  746.  
  747.   return true;
  748. }
  749.  
  750. /* Finish up the dynamic sections.  */
  751.  
  752. static boolean
  753. elf32_sparc_finish_dynamic_sections (output_bfd, info)
  754.      bfd *output_bfd;
  755.      struct bfd_link_info *info;
  756. {
  757.   asection *splt;
  758.   asection *sgot;
  759.   asection *sdyn;
  760.   Elf32_External_Dyn *dyncon, *dynconend;
  761.  
  762.   splt = bfd_get_section_by_name (elf_hash_table (info)->dynobj, ".plt");
  763.   sgot = bfd_get_section_by_name (elf_hash_table (info)->dynobj, ".got");
  764.   sdyn = bfd_get_section_by_name (elf_hash_table (info)->dynobj, ".dynamic");
  765.   BFD_ASSERT (splt != NULL && sgot != NULL && sdyn != NULL);
  766.  
  767.   dyncon = (Elf32_External_Dyn *) sdyn->contents;
  768.   dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
  769.   for (; dyncon < dynconend; dyncon++)
  770.     {
  771.       Elf_Internal_Dyn dyn;
  772.       const char *name;
  773.       boolean size;
  774.  
  775.       bfd_elf32_swap_dyn_in (elf_hash_table (info)->dynobj, dyncon, &dyn);
  776.  
  777.       switch (dyn.d_tag)
  778.     {
  779.     case DT_PLTGOT:   name = ".plt"; size = false; break;
  780.     case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
  781.     case DT_JMPREL:   name = ".rela.plt"; size = false; break;
  782.     default:      name = NULL; size = false; break;
  783.     }
  784.  
  785.       if (name != NULL)
  786.     {
  787.       asection *s;
  788.  
  789.       s = bfd_get_section_by_name (output_bfd, name);
  790.       BFD_ASSERT (s != NULL);
  791.       if (! size)
  792.         dyn.d_un.d_ptr = s->vma;
  793.       else
  794.         {
  795.           if (s->_cooked_size != 0)
  796.         dyn.d_un.d_val = s->_cooked_size;
  797.           else
  798.         dyn.d_un.d_val = s->_raw_size;
  799.         }
  800.       bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  801.     }
  802.     }
  803.  
  804.   /* Clear the first four entries in the procedure linkage table, and
  805.      put a nop in the last four bytes.  */
  806.   if (splt->_raw_size > 0)
  807.     {
  808.       memset (splt->contents, 0, 4 * PLT_ENTRY_SIZE);
  809.       bfd_put_32 (output_bfd, SPARC_NOP,
  810.           splt->contents + splt->_raw_size - 4);
  811.     }
  812.  
  813.   /* Set the first entry in the global offset table to the address of
  814.      the dynamic section.  */
  815.   if (sgot->_raw_size > 0)
  816.     bfd_put_32 (output_bfd,
  817.         sdyn->output_section->vma + sdyn->output_offset,
  818.         sgot->contents);
  819.  
  820.   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
  821.   elf_section_data (splt->output_section)->this_hdr.sh_entsize =
  822.     PLT_ENTRY_SIZE;
  823.  
  824.   return true;
  825. }
  826.  
  827. #define TARGET_BIG_SYM    bfd_elf32_sparc_vec
  828. #define TARGET_BIG_NAME    "elf32-sparc"
  829. #define ELF_ARCH    bfd_arch_sparc
  830. #define ELF_MACHINE_CODE EM_SPARC
  831. #define ELF_MAXPAGESIZE 0x10000
  832. #define elf_backend_create_dynamic_sections \
  833.                     elf32_sparc_create_dynamic_sections
  834. #define elf_backend_adjust_dynamic_symbol \
  835.                     elf32_sparc_adjust_dynamic_symbol
  836. #define elf_backend_size_dynamic_sections \
  837.                     elf32_sparc_size_dynamic_sections
  838. #define elf_backend_relocate_section    elf32_sparc_relocate_section
  839. #define elf_backend_finish_dynamic_symbol \
  840.                     elf32_sparc_finish_dynamic_symbol
  841. #define elf_backend_finish_dynamic_sections \
  842.                     elf32_sparc_finish_dynamic_sections
  843.  
  844. #include "elf32-target.h"
  845.