Ignore:
Timestamp:
Aug 16, 2003, 6:59:22 PM (22 years ago)
Author:
bird
Message:

binutils v2.14 - offical sources.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GNU/src/binutils/bfd/elfcode.h

    • Property cvs2svn:cvs-rev changed from 1.1 to 1.1.1.2
    r608 r609  
    11/* ELF executable support for BFD.
    22   Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
    3    2001 Free Software Foundation, Inc.
     3   2001, 2002 Free Software Foundation, Inc.
    44
    55   Written by Fred Fish @ Cygnus Support, from information published
     
    6767#include "bfd.h"
    6868#include "sysdep.h"
     69#include "libiberty.h"
    6970#include "bfdlink.h"
    7071#include "libbfd.h"
     
    123124#define elf_link_create_dynamic_sections \
    124125  NAME(bfd_elf,link_create_dynamic_sections)
     126#define elf_bfd_discard_info            NAME(bfd_elf,discard_info)
     127#define elf_reloc_symbol_deleted_p      NAME(_bfd_elf,reloc_symbol_deleted_p)
    125128#define elf_link_record_dynamic_symbol  _bfd_elf_link_record_dynamic_symbol
    126129#define elf_bfd_final_link              NAME(bfd_elf,bfd_final_link)
     
    168171#define section_from_elf_index bfd_section_from_elf_index
    169172
    170 static boolean elf_slurp_reloc_table_from_section
     173static bfd_boolean elf_slurp_reloc_table_from_section
    171174  PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, bfd_size_type,
    172            arelent *, asymbol **, boolean));
    173 
    174 static boolean elf_file_p PARAMS ((Elf_External_Ehdr *));
     175           arelent *, asymbol **, bfd_boolean));
     176
     177static bfd_boolean elf_file_p PARAMS ((Elf_External_Ehdr *));
    175178
    176179#ifdef DEBUG
     
    186189   can be handled by explicitly specifying 32 bits or "the long type".  */
    187190#if ARCH_SIZE == 64
    188 #define put_word        bfd_h_put_64
    189 #define put_signed_word bfd_h_put_signed_64
    190 #define get_word        bfd_h_get_64
    191 #define get_signed_word bfd_h_get_signed_64
     191#define H_PUT_WORD              H_PUT_64
     192#define H_PUT_SIGNED_WORD       H_PUT_S64
     193#define H_GET_WORD              H_GET_64
     194#define H_GET_SIGNED_WORD       H_GET_S64
    192195#endif
    193196#if ARCH_SIZE == 32
    194 #define put_word        bfd_h_put_32
    195 #define put_signed_word bfd_h_put_signed_32
    196 #define get_word        bfd_h_get_32
    197 #define get_signed_word bfd_h_get_signed_32
     197#define H_PUT_WORD              H_PUT_32
     198#define H_PUT_SIGNED_WORD       H_PUT_S32
     199#define H_GET_WORD              H_GET_32
     200#define H_GET_SIGNED_WORD       H_GET_S32
    198201#endif
    199202
     
    202205
    203206void
    204 elf_swap_symbol_in (abfd, src, dst)
    205      bfd *abfd;
    206      const Elf_External_Sym *src;
     207elf_swap_symbol_in (abfd, psrc, pshn, dst)
     208     bfd *abfd;
     209     const PTR psrc;
     210     const PTR pshn;
    207211     Elf_Internal_Sym *dst;
    208212{
     213  const Elf_External_Sym *src = (const Elf_External_Sym *) psrc;
     214  const Elf_External_Sym_Shndx *shndx = (const Elf_External_Sym_Shndx *) pshn;
    209215  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
    210216
    211   dst->st_name = bfd_h_get_32 (abfd, (bfd_byte *) src->st_name);
     217  dst->st_name = H_GET_32 (abfd, src->st_name);
    212218  if (signed_vma)
    213     dst->st_value = get_signed_word (abfd, (bfd_byte *) src->st_value);
     219    dst->st_value = H_GET_SIGNED_WORD (abfd, src->st_value);
    214220  else
    215     dst->st_value = get_word (abfd, (bfd_byte *) src->st_value);
    216   dst->st_size = get_word (abfd, (bfd_byte *) src->st_size);
    217   dst->st_info = bfd_h_get_8 (abfd, (bfd_byte *) src->st_info);
    218   dst->st_other = bfd_h_get_8 (abfd, (bfd_byte *) src->st_other);
    219   dst->st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src->st_shndx);
     221    dst->st_value = H_GET_WORD (abfd, src->st_value);
     222  dst->st_size = H_GET_WORD (abfd, src->st_size);
     223  dst->st_info = H_GET_8 (abfd, src->st_info);
     224  dst->st_other = H_GET_8 (abfd, src->st_other);
     225  dst->st_shndx = H_GET_16 (abfd, src->st_shndx);
     226  if (dst->st_shndx == SHN_XINDEX)
     227    {
     228      if (shndx == NULL)
     229        abort ();
     230      dst->st_shndx = H_GET_32 (abfd, shndx->est_shndx);
     231    }
    220232}
    221233
     
    224236
    225237void
    226 elf_swap_symbol_out (abfd, src, cdst)
     238elf_swap_symbol_out (abfd, src, cdst, shndx)
    227239     bfd *abfd;
    228240     const Elf_Internal_Sym *src;
    229241     PTR cdst;
    230 {
     242     PTR shndx;
     243{
     244  unsigned int tmp;
    231245  Elf_External_Sym *dst = (Elf_External_Sym *) cdst;
    232   bfd_h_put_32 (abfd, src->st_name, dst->st_name);
    233   put_word (abfd, src->st_value, dst->st_value);
    234   put_word (abfd, src->st_size, dst->st_size);
    235   bfd_h_put_8 (abfd, src->st_info, dst->st_info);
    236   bfd_h_put_8 (abfd, src->st_other, dst->st_other);
    237   bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx);
     246  H_PUT_32 (abfd, src->st_name, dst->st_name);
     247  H_PUT_WORD (abfd, src->st_value, dst->st_value);
     248  H_PUT_WORD (abfd, src->st_size, dst->st_size);
     249  H_PUT_8 (abfd, src->st_info, dst->st_info);
     250  H_PUT_8 (abfd, src->st_other, dst->st_other);
     251  tmp = src->st_shndx;
     252  if (tmp > SHN_HIRESERVE)
     253    {
     254      if (shndx == NULL)
     255        abort ();
     256      H_PUT_32 (abfd, tmp, shndx);
     257      tmp = SHN_XINDEX;
     258    }
     259  H_PUT_16 (abfd, tmp, dst->st_shndx);
    238260}
    239261
     
    249271  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
    250272  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
    251   dst->e_type = bfd_h_get_16 (abfd, (bfd_byte *) src->e_type);
    252   dst->e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src->e_machine);
    253   dst->e_version = bfd_h_get_32 (abfd, (bfd_byte *) src->e_version);
     273  dst->e_type = H_GET_16 (abfd, src->e_type);
     274  dst->e_machine = H_GET_16 (abfd, src->e_machine);
     275  dst->e_version = H_GET_32 (abfd, src->e_version);
    254276  if (signed_vma)
    255     dst->e_entry = get_signed_word (abfd, (bfd_byte *) src->e_entry);
     277    dst->e_entry = H_GET_SIGNED_WORD (abfd, src->e_entry);
    256278  else
    257     dst->e_entry = get_word (abfd, (bfd_byte *) src->e_entry);
    258   dst->e_phoff = get_word (abfd, (bfd_byte *) src->e_phoff);
    259   dst->e_shoff = get_word (abfd, (bfd_byte *) src->e_shoff);
    260   dst->e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->e_flags);
    261   dst->e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_ehsize);
    262   dst->e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phentsize);
    263   dst->e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phnum);
    264   dst->e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shentsize);
    265   dst->e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shnum);
    266   dst->e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shstrndx);
     279    dst->e_entry = H_GET_WORD (abfd, src->e_entry);
     280  dst->e_phoff = H_GET_WORD (abfd, src->e_phoff);
     281  dst->e_shoff = H_GET_WORD (abfd, src->e_shoff);
     282  dst->e_flags = H_GET_32 (abfd, src->e_flags);
     283  dst->e_ehsize = H_GET_16 (abfd, src->e_ehsize);
     284  dst->e_phentsize = H_GET_16 (abfd, src->e_phentsize);
     285  dst->e_phnum = H_GET_16 (abfd, src->e_phnum);
     286  dst->e_shentsize = H_GET_16 (abfd, src->e_shentsize);
     287  dst->e_shnum = H_GET_16 (abfd, src->e_shnum);
     288  dst->e_shstrndx = H_GET_16 (abfd, src->e_shstrndx);
    267289}
    268290
     
    276298     Elf_External_Ehdr *dst;
    277299{
     300  unsigned int tmp;
    278301  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
    279302  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
    280303  /* note that all elements of dst are *arrays of unsigned char* already...  */
    281   bfd_h_put_16 (abfd, src->e_type, dst->e_type);
    282   bfd_h_put_16 (abfd, src->e_machine, dst->e_machine);
    283   bfd_h_put_32 (abfd, src->e_version, dst->e_version);
     304  H_PUT_16 (abfd, src->e_type, dst->e_type);
     305  H_PUT_16 (abfd, src->e_machine, dst->e_machine);
     306  H_PUT_32 (abfd, src->e_version, dst->e_version);
    284307  if (signed_vma)
    285     put_signed_word (abfd, src->e_entry, dst->e_entry);
     308    H_PUT_SIGNED_WORD (abfd, src->e_entry, dst->e_entry);
    286309  else
    287     put_word (abfd, src->e_entry, dst->e_entry);
    288   put_word (abfd, src->e_phoff, dst->e_phoff);
    289   put_word (abfd, src->e_shoff, dst->e_shoff);
    290   bfd_h_put_32 (abfd, src->e_flags, dst->e_flags);
    291   bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize);
    292   bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize);
    293   bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum);
    294   bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize);
    295   bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum);
    296   bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx);
     310    H_PUT_WORD (abfd, src->e_entry, dst->e_entry);
     311  H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff);
     312  H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff);
     313  H_PUT_32 (abfd, src->e_flags, dst->e_flags);
     314  H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize);
     315  H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize);
     316  H_PUT_16 (abfd, src->e_phnum, dst->e_phnum);
     317  H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize);
     318  tmp = src->e_shnum;
     319  if (tmp >= SHN_LORESERVE)
     320    tmp = SHN_UNDEF;
     321  H_PUT_16 (abfd, tmp, dst->e_shnum);
     322  tmp = src->e_shstrndx;
     323  if (tmp >= SHN_LORESERVE)
     324    tmp = SHN_XINDEX;
     325  H_PUT_16 (abfd, tmp, dst->e_shstrndx);
    297326}
    298327
     
    308337  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
    309338
    310   dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name);
    311   dst->sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_type);
    312   dst->sh_flags = get_word (abfd, (bfd_byte *) src->sh_flags);
     339  dst->sh_name = H_GET_32 (abfd, src->sh_name);
     340  dst->sh_type = H_GET_32 (abfd, src->sh_type);
     341  dst->sh_flags = H_GET_WORD (abfd, src->sh_flags);
    313342  if (signed_vma)
    314     dst->sh_addr = get_signed_word (abfd, (bfd_byte *) src->sh_addr);
     343    dst->sh_addr = H_GET_SIGNED_WORD (abfd, src->sh_addr);
    315344  else
    316     dst->sh_addr = get_word (abfd, (bfd_byte *) src->sh_addr);
    317   dst->sh_offset = get_word (abfd, (bfd_byte *) src->sh_offset);
    318   dst->sh_size = get_word (abfd, (bfd_byte *) src->sh_size);
    319   dst->sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_link);
    320   dst->sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_info);
    321   dst->sh_addralign = get_word (abfd, (bfd_byte *) src->sh_addralign);
    322   dst->sh_entsize = get_word (abfd, (bfd_byte *) src->sh_entsize);
     345    dst->sh_addr = H_GET_WORD (abfd, src->sh_addr);
     346  dst->sh_offset = H_GET_WORD (abfd, src->sh_offset);
     347  dst->sh_size = H_GET_WORD (abfd, src->sh_size);
     348  dst->sh_link = H_GET_32 (abfd, src->sh_link);
     349  dst->sh_info = H_GET_32 (abfd, src->sh_info);
     350  dst->sh_addralign = H_GET_WORD (abfd, src->sh_addralign);
     351  dst->sh_entsize = H_GET_WORD (abfd, src->sh_entsize);
    323352  dst->bfd_section = NULL;
    324353  dst->contents = NULL;
     
    335364{
    336365  /* note that all elements of dst are *arrays of unsigned char* already...  */
    337   bfd_h_put_32 (abfd, src->sh_name, dst->sh_name);
    338   bfd_h_put_32 (abfd, src->sh_type, dst->sh_type);
    339   put_word (abfd, src->sh_flags, dst->sh_flags);
    340   put_word (abfd, src->sh_addr, dst->sh_addr);
    341   put_word (abfd, src->sh_offset, dst->sh_offset);
    342   put_word (abfd, src->sh_size, dst->sh_size);
    343   bfd_h_put_32 (abfd, src->sh_link, dst->sh_link);
    344   bfd_h_put_32 (abfd, src->sh_info, dst->sh_info);
    345   put_word (abfd, src->sh_addralign, dst->sh_addralign);
    346   put_word (abfd, src->sh_entsize, dst->sh_entsize);
     366  H_PUT_32 (abfd, src->sh_name, dst->sh_name);
     367  H_PUT_32 (abfd, src->sh_type, dst->sh_type);
     368  H_PUT_WORD (abfd, src->sh_flags, dst->sh_flags);
     369  H_PUT_WORD (abfd, src->sh_addr, dst->sh_addr);
     370  H_PUT_WORD (abfd, src->sh_offset, dst->sh_offset);
     371  H_PUT_WORD (abfd, src->sh_size, dst->sh_size);
     372  H_PUT_32 (abfd, src->sh_link, dst->sh_link);
     373  H_PUT_32 (abfd, src->sh_info, dst->sh_info);
     374  H_PUT_WORD (abfd, src->sh_addralign, dst->sh_addralign);
     375  H_PUT_WORD (abfd, src->sh_entsize, dst->sh_entsize);
    347376}
    348377
     
    358387  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
    359388
    360   dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type);
    361   dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags);
    362   dst->p_offset = get_word (abfd, (bfd_byte *) src->p_offset);
     389  dst->p_type = H_GET_32 (abfd, src->p_type);
     390  dst->p_flags = H_GET_32 (abfd, src->p_flags);
     391  dst->p_offset = H_GET_WORD (abfd, src->p_offset);
    363392  if (signed_vma)
    364393    {
    365       dst->p_vaddr = get_signed_word (abfd, (bfd_byte *) src->p_vaddr);
    366       dst->p_paddr = get_signed_word (abfd, (bfd_byte *) src->p_paddr);
     394      dst->p_vaddr = H_GET_SIGNED_WORD (abfd, src->p_vaddr);
     395      dst->p_paddr = H_GET_SIGNED_WORD (abfd, src->p_paddr);
    367396    }
    368397  else
    369398    {
    370       dst->p_vaddr = get_word (abfd, (bfd_byte *) src->p_vaddr);
    371       dst->p_paddr = get_word (abfd, (bfd_byte *) src->p_paddr);
    372     }
    373   dst->p_filesz = get_word (abfd, (bfd_byte *) src->p_filesz);
    374   dst->p_memsz = get_word (abfd, (bfd_byte *) src->p_memsz);
    375   dst->p_align = get_word (abfd, (bfd_byte *) src->p_align);
     399      dst->p_vaddr = H_GET_WORD (abfd, src->p_vaddr);
     400      dst->p_paddr = H_GET_WORD (abfd, src->p_paddr);
     401    }
     402  dst->p_filesz = H_GET_WORD (abfd, src->p_filesz);
     403  dst->p_memsz = H_GET_WORD (abfd, src->p_memsz);
     404  dst->p_align = H_GET_WORD (abfd, src->p_align);
    376405}
    377406
     
    383412{
    384413  /* note that all elements of dst are *arrays of unsigned char* already...  */
    385   bfd_h_put_32 (abfd, src->p_type, dst->p_type);
    386   put_word (abfd, src->p_offset, dst->p_offset);
    387   put_word (abfd, src->p_vaddr, dst->p_vaddr);
    388   put_word (abfd, src->p_paddr, dst->p_paddr);
    389   put_word (abfd, src->p_filesz, dst->p_filesz);
    390   put_word (abfd, src->p_memsz, dst->p_memsz);
    391   bfd_h_put_32 (abfd, src->p_flags, dst->p_flags);
    392   put_word (abfd, src->p_align, dst->p_align);
     414  H_PUT_32 (abfd, src->p_type, dst->p_type);
     415  H_PUT_WORD (abfd, src->p_offset, dst->p_offset);
     416  H_PUT_WORD (abfd, src->p_vaddr, dst->p_vaddr);
     417  H_PUT_WORD (abfd, src->p_paddr, dst->p_paddr);
     418  H_PUT_WORD (abfd, src->p_filesz, dst->p_filesz);
     419  H_PUT_WORD (abfd, src->p_memsz, dst->p_memsz);
     420  H_PUT_32 (abfd, src->p_flags, dst->p_flags);
     421  H_PUT_WORD (abfd, src->p_align, dst->p_align);
    393422}
    394423
    395424/* Translate an ELF reloc from external format to internal format.  */
    396 INLINE void
    397 elf_swap_reloc_in (abfd, src, dst)
    398      bfd *abfd;
    399      const Elf_External_Rel *src;
    400      Elf_Internal_Rel *dst;
    401 {
    402   dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
    403   dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
    404 }
    405 
    406 INLINE void
    407 elf_swap_reloca_in (abfd, src, dst)
    408      bfd *abfd;
    409      const Elf_External_Rela *src;
     425void
     426elf_swap_reloc_in (abfd, s, dst)
     427     bfd *abfd;
     428     const bfd_byte *s;
    410429     Elf_Internal_Rela *dst;
    411430{
    412   dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
    413   dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
    414   dst->r_addend = get_signed_word (abfd, (bfd_byte *) src->r_addend);
     431  const Elf_External_Rel *src = (const Elf_External_Rel *) s;
     432  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
     433  dst->r_info = H_GET_WORD (abfd, src->r_info);
     434  dst->r_addend = 0;
     435}
     436
     437void
     438elf_swap_reloca_in (abfd, s, dst)
     439     bfd *abfd;
     440     const bfd_byte *s;
     441     Elf_Internal_Rela *dst;
     442{
     443  const Elf_External_Rela *src = (const Elf_External_Rela *) s;
     444  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
     445  dst->r_info = H_GET_WORD (abfd, src->r_info);
     446  dst->r_addend = H_GET_SIGNED_WORD (abfd, src->r_addend);
    415447}
    416448
    417449/* Translate an ELF reloc from internal format to external format.  */
    418 INLINE void
    419 elf_swap_reloc_out (abfd, src, dst)
    420      bfd *abfd;
    421      const Elf_Internal_Rel *src;
    422      Elf_External_Rel *dst;
    423 {
    424   put_word (abfd, src->r_offset, dst->r_offset);
    425   put_word (abfd, src->r_info, dst->r_info);
    426 }
    427 
    428 INLINE void
    429 elf_swap_reloca_out (abfd, src, dst)
     450void
     451elf_swap_reloc_out (abfd, src, d)
    430452     bfd *abfd;
    431453     const Elf_Internal_Rela *src;
    432      Elf_External_Rela *dst;
    433 {
    434   put_word (abfd, src->r_offset, dst->r_offset);
    435   put_word (abfd, src->r_info, dst->r_info);
    436   put_signed_word (abfd, src->r_addend, dst->r_addend);
     454     bfd_byte *d;
     455{
     456  Elf_External_Rel *dst = (Elf_External_Rel *) d;
     457  H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
     458  H_PUT_WORD (abfd, src->r_info, dst->r_info);
     459}
     460
     461void
     462elf_swap_reloca_out (abfd, src, d)
     463     bfd *abfd;
     464     const Elf_Internal_Rela *src;
     465     bfd_byte *d;
     466{
     467  Elf_External_Rela *dst = (Elf_External_Rela *) d;
     468  H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
     469  H_PUT_WORD (abfd, src->r_info, dst->r_info);
     470  H_PUT_SIGNED_WORD (abfd, src->r_addend, dst->r_addend);
    437471}
    438472
     
    445479  const Elf_External_Dyn *src = (const Elf_External_Dyn *) p;
    446480
    447   dst->d_tag = get_word (abfd, src->d_tag);
    448   dst->d_un.d_val = get_word (abfd, src->d_un.d_val);
     481  dst->d_tag = H_GET_WORD (abfd, src->d_tag);
     482  dst->d_un.d_val = H_GET_WORD (abfd, src->d_un.d_val);
    449483}
    450484
     
    457491  Elf_External_Dyn *dst = (Elf_External_Dyn *) p;
    458492
    459   put_word (abfd, src->d_tag, dst->d_tag);
    460   put_word (abfd, src->d_un.d_val, dst->d_un.d_val);
     493  H_PUT_WORD (abfd, src->d_tag, dst->d_tag);
     494  H_PUT_WORD (abfd, src->d_un.d_val, dst->d_un.d_val);
    461495}
    462496
     
    469503   the magic number.  */
    470504
    471 static INLINE boolean
     505static INLINE bfd_boolean
    472506elf_file_p (x_ehdrp)
    473507     Elf_External_Ehdr *x_ehdrp;
     
    494528  Elf_Internal_Ehdr *i_ehdrp;   /* Elf file header, internal form */
    495529  Elf_External_Shdr x_shdr;     /* Section header table entry, external form */
    496   Elf_Internal_Shdr *i_shdrp = NULL; /* Section header table, internal form */
     530  Elf_Internal_Shdr i_shdr;
     531  Elf_Internal_Shdr *i_shdrp;   /* Section header table, internal form */
    497532  unsigned int shindex;
    498533  char *shstrtab;               /* Internal copy of section header stringtab */
    499534  struct elf_backend_data *ebd;
    500   struct elf_obj_tdata *preserved_tdata = elf_tdata (abfd);
    501   struct sec *preserved_sections = abfd->sections;
    502   unsigned int preserved_section_count = abfd->section_count;
    503   enum bfd_architecture previous_arch = bfd_get_arch (abfd);
    504   unsigned long previous_mach = bfd_get_mach (abfd);
    505   struct elf_obj_tdata *new_tdata = NULL;
     535  struct bfd_preserve preserve;
    506536  asection *s;
    507 
    508   /* Clear section information, since there might be a recognized bfd that
    509      we now check if we can replace, and we don't want to append to it.  */
    510   abfd->sections = NULL;
    511   abfd->section_count = 0;
     537  bfd_size_type amt;
     538
     539  preserve.marker = NULL;
    512540
    513541  /* Read in the ELF header in external format.  */
    514542
    515   if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
     543  if (bfd_bread ((PTR) & x_ehdr, (bfd_size_type) sizeof (x_ehdr), abfd)
     544      != sizeof (x_ehdr))
    516545    {
    517546      if (bfd_get_error () != bfd_error_system_call)
     
    527556     file).  */
    528557
    529   if ((elf_file_p (&x_ehdr) == false) ||
    530       (x_ehdr.e_ident[EI_VERSION] != EV_CURRENT) ||
    531       (x_ehdr.e_ident[EI_CLASS] != ELFCLASS))
     558  if (! elf_file_p (&x_ehdr)
     559      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
     560      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
    532561    goto got_wrong_format_error;
    533562
     
    551580     the tdata pointer in the bfd.  */
    552581
    553   new_tdata = ((struct elf_obj_tdata *)
    554                bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)));
    555   if (new_tdata == NULL)
     582  amt = sizeof (struct elf_obj_tdata);
     583  preserve.marker = bfd_zalloc (abfd, amt);
     584  if (preserve.marker == NULL)
    556585    goto got_no_match;
    557   elf_tdata (abfd) = new_tdata;
     586  if (!bfd_preserve_save (abfd, &preserve))
     587    goto got_no_match;
     588
     589  elf_tdata (abfd) = preserve.marker;
    558590
    559591  /* Now that we know the byte order, swap in the rest of the header */
     
    568600    goto got_wrong_format_error;
    569601
    570   /* If there is no section header table, we're hosed.  */
    571   if (i_ehdrp->e_shoff == 0)
     602  /* If this is a relocatable file and there is no section header
     603     table, then we're hosed.  */
     604  if (i_ehdrp->e_shoff == 0 && i_ehdrp->e_type == ET_REL)
    572605    goto got_wrong_format_error;
    573606
    574607  /* As a simple sanity check, verify that the what BFD thinks is the
    575608     size of each section header table entry actually matches the size
    576      recorded in the file.  */
    577   if (i_ehdrp->e_shentsize != sizeof (x_shdr))
     609     recorded in the file, but only if there are any sections.  */
     610  if (i_ehdrp->e_shentsize != sizeof (x_shdr) && i_ehdrp->e_shnum != 0)
     611    goto got_wrong_format_error;
     612
     613  /* Further sanity check.  */
     614  if (i_ehdrp->e_shoff == 0 && i_ehdrp->e_shnum != 0)
    578615    goto got_wrong_format_error;
    579616
     
    583620     BFD format expects.  */
    584621  if (ebd->elf_machine_code != i_ehdrp->e_machine
    585       && (ebd->elf_machine_alt1 == 0 || i_ehdrp->e_machine != ebd->elf_machine_alt1)
    586       && (ebd->elf_machine_alt2 == 0 || i_ehdrp->e_machine != ebd->elf_machine_alt2))
     622      && (ebd->elf_machine_alt1 == 0
     623          || i_ehdrp->e_machine != ebd->elf_machine_alt1)
     624      && (ebd->elf_machine_alt2 == 0
     625          || i_ehdrp->e_machine != ebd->elf_machine_alt2))
    587626    {
    588627      const bfd_target * const *target_ptr;
     
    631670  bfd_set_start_address (abfd, i_ehdrp->e_entry);
    632671
     672  if (i_ehdrp->e_shoff != 0)
     673    {
     674      /* Seek to the section header table in the file.  */
     675      if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0)
     676        goto got_no_match;
     677
     678      /* Read the first section header at index 0, and convert to internal
     679         form.  */
     680      if (bfd_bread ((PTR) & x_shdr, (bfd_size_type) sizeof x_shdr, abfd)
     681          != sizeof (x_shdr))
     682        goto got_no_match;
     683      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
     684
     685      /* If the section count is zero, the actual count is in the first
     686         section header.  */
     687      if (i_ehdrp->e_shnum == SHN_UNDEF)
     688        i_ehdrp->e_shnum = i_shdr.sh_size;
     689
     690      /* And similarly for the string table index.  */
     691      if (i_ehdrp->e_shstrndx == SHN_XINDEX)
     692        i_ehdrp->e_shstrndx = i_shdr.sh_link;
     693    }
     694
    633695  /* Allocate space for a copy of the section header table in
    634      internal form, seek to the section header table in the file,
    635      read it in, and convert it to internal form.  */
    636   i_shdrp = ((Elf_Internal_Shdr *)
    637              bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum));
    638   elf_elfsections (abfd) = ((Elf_Internal_Shdr **)
    639                             bfd_alloc (abfd,
    640                                        sizeof (i_shdrp) * i_ehdrp->e_shnum));
    641   if (!i_shdrp || !elf_elfsections (abfd))
    642     goto got_no_match;
    643   if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0)
    644     goto got_no_match;
    645   for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++)
    646     {
    647       if (bfd_read ((PTR) & x_shdr, sizeof x_shdr, 1, abfd) != sizeof (x_shdr))
     696     internal form.  */
     697  if (i_ehdrp->e_shnum != 0)
     698    {
     699      Elf_Internal_Shdr *shdrp;
     700      unsigned int num_sec;
     701
     702      amt = sizeof (*i_shdrp) * i_ehdrp->e_shnum;
     703      i_shdrp = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
     704      if (!i_shdrp)
    648705        goto got_no_match;
    649       elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
    650       elf_elfsections (abfd)[shindex] = i_shdrp + shindex;
    651 
    652       /* If the section is loaded, but not page aligned, clear
    653          D_PAGED.  */
    654       if ((i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
    655           && i_shdrp[shindex].sh_type != SHT_NOBITS
    656           && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
    657                % ebd->maxpagesize)
    658               != 0))
    659         abfd->flags &= ~D_PAGED;
    660     }
    661   if (i_ehdrp->e_shstrndx)
     706      num_sec = i_ehdrp->e_shnum;
     707      if (num_sec > SHN_LORESERVE)
     708        num_sec += SHN_HIRESERVE + 1 - SHN_LORESERVE;
     709      elf_numsections (abfd) = num_sec;
     710      amt = sizeof (i_shdrp) * num_sec;
     711      elf_elfsections (abfd) = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
     712      if (!elf_elfsections (abfd))
     713        goto got_no_match;
     714
     715      memcpy (i_shdrp, &i_shdr, sizeof (*i_shdrp));
     716      shdrp = i_shdrp;
     717      shindex = 0;
     718      if (num_sec > SHN_LORESERVE)
     719        {
     720          for ( ; shindex < SHN_LORESERVE; shindex++)
     721            elf_elfsections (abfd)[shindex] = shdrp++;
     722          for ( ; shindex < SHN_HIRESERVE + 1; shindex++)
     723            elf_elfsections (abfd)[shindex] = i_shdrp;
     724        }
     725      for ( ; shindex < num_sec; shindex++)
     726        elf_elfsections (abfd)[shindex] = shdrp++;
     727
     728      /* Read in the rest of the section header table and convert it
     729         to internal form.  */
     730      for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
     731        {
     732          if (bfd_bread ((PTR) & x_shdr, (bfd_size_type) sizeof x_shdr, abfd)
     733              != sizeof (x_shdr))
     734            goto got_no_match;
     735          elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
     736
     737          /* If the section is loaded, but not page aligned, clear
     738             D_PAGED.  */
     739          if (i_shdrp[shindex].sh_size != 0
     740              && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
     741              && i_shdrp[shindex].sh_type != SHT_NOBITS
     742              && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
     743                   % ebd->maxpagesize)
     744                  != 0))
     745            abfd->flags &= ~D_PAGED;
     746        }
     747    }
     748
     749  if (i_ehdrp->e_shstrndx && i_ehdrp->e_shoff)
    662750    {
    663751      if (! bfd_section_from_shdr (abfd, i_ehdrp->e_shstrndx))
     
    673761      unsigned int i;
    674762
    675       elf_tdata (abfd)->phdr = ((Elf_Internal_Phdr *)
    676                                 bfd_alloc (abfd,
    677                                            (i_ehdrp->e_phnum
    678                                             * sizeof (Elf_Internal_Phdr))));
     763      amt = i_ehdrp->e_phnum * sizeof (Elf_Internal_Phdr);
     764      elf_tdata (abfd)->phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
    679765      if (elf_tdata (abfd)->phdr == NULL)
    680766        goto got_no_match;
    681       if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
     767      if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_phoff, SEEK_SET) != 0)
    682768        goto got_no_match;
    683769      i_phdr = elf_tdata (abfd)->phdr;
     
    686772          Elf_External_Phdr x_phdr;
    687773
    688           if (bfd_read ((PTR) &x_phdr, sizeof x_phdr, 1, abfd)
     774          if (bfd_bread ((PTR) &x_phdr, (bfd_size_type) sizeof x_phdr, abfd)
    689775              != sizeof x_phdr)
    690776            goto got_no_match;
     
    699785     used to find all of the ELF section names.) */
    700786
    701   shstrtab = bfd_elf_get_str_section (abfd, i_ehdrp->e_shstrndx);
    702   if (!shstrtab)
    703     goto got_no_match;
    704 
    705   /* Once all of the section headers have been read and converted, we
    706      can start processing them.  Note that the first section header is
    707      a dummy placeholder entry, so we ignore it.  */
    708 
    709   for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
    710     {
    711       if (! bfd_section_from_shdr (abfd, shindex))
     787  if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff)
     788    {
     789      unsigned int num_sec;
     790
     791      shstrtab = bfd_elf_get_str_section (abfd, i_ehdrp->e_shstrndx);
     792      if (!shstrtab)
    712793        goto got_no_match;
     794
     795      /* Once all of the section headers have been read and converted, we
     796         can start processing them.  Note that the first section header is
     797         a dummy placeholder entry, so we ignore it.  */
     798      num_sec = elf_numsections (abfd);
     799      for (shindex = 1; shindex < num_sec; shindex++)
     800        {
     801          if (! bfd_section_from_shdr (abfd, shindex))
     802            goto got_no_match;
     803          if (shindex == SHN_LORESERVE - 1)
     804            shindex += SHN_HIRESERVE + 1 - SHN_LORESERVE;
     805        }
    713806    }
    714807
     
    717810  if (ebd->elf_backend_object_p)
    718811    {
    719       if ((*ebd->elf_backend_object_p) (abfd) == false)
     812      if (! (*ebd->elf_backend_object_p) (abfd))
    720813        goto got_wrong_format_error;
    721814    }
     
    740833    }
    741834
    742   return (abfd->xvec);
     835  bfd_preserve_finish (abfd, &preserve);
     836  return abfd->xvec;
    743837
    744838 got_wrong_format_error:
     
    752846     whole bfd here and restoring it would be even worse; the first thing
    753847     you notice is that the cached bfd file position gets out of sync.  */
    754   bfd_default_set_arch_mach (abfd, previous_arch, previous_mach);
    755848  bfd_set_error (bfd_error_wrong_format);
     849
    756850 got_no_match:
    757   if (new_tdata != NULL
    758       && new_tdata->elf_sect_ptr != NULL)
    759     bfd_release (abfd, new_tdata->elf_sect_ptr);
    760   if (i_shdrp != NULL)
    761     bfd_release (abfd, i_shdrp);
    762   if (new_tdata != NULL)
    763     bfd_release (abfd, new_tdata);
    764   elf_tdata (abfd) = preserved_tdata;
    765   abfd->sections = preserved_sections;
    766   abfd->section_count = preserved_section_count;
    767   return (NULL);
     851  if (preserve.marker != NULL)
     852    bfd_preserve_restore (abfd, &preserve);
     853  return NULL;
    768854}
    769855
     
    779865     PTR data;
    780866{
    781   boolean *failedp = (boolean *) data;
     867  bfd_boolean *failedp = (bfd_boolean *) data;
    782868  Elf_Internal_Shdr *rela_hdr;
    783   Elf_External_Rela *outbound_relocas;
    784   Elf_External_Rel *outbound_relocs;
     869  bfd_vma addr_offset;
     870  void (*swap_out) PARAMS ((bfd *, const Elf_Internal_Rela *, bfd_byte *));
     871  size_t extsize;
     872  bfd_byte *dst_rela;
    785873  unsigned int idx;
    786   int use_rela_p;
    787   asymbol *last_sym = 0;
    788   int last_sym_idx = 0;
     874  asymbol *last_sym;
     875  int last_sym_idx;
    789876
    790877  /* If we have already failed, don't do anything.  */
     
    808895  if (rela_hdr->contents == NULL)
    809896    {
    810       *failedp = true;
     897      *failedp = TRUE;
    811898      return;
    812899    }
     
    814901  /* Figure out whether the relocations are RELA or REL relocations.  */
    815902  if (rela_hdr->sh_type == SHT_RELA)
    816     use_rela_p = true;
     903    {
     904      swap_out = elf_swap_reloca_out;
     905      extsize = sizeof (Elf_External_Rela);
     906    }
    817907  else if (rela_hdr->sh_type == SHT_REL)
    818     use_rela_p = false;
     908    {
     909      swap_out = elf_swap_reloc_out;
     910      extsize = sizeof (Elf_External_Rel);
     911    }
    819912  else
    820913    /* Every relocation section should be either an SHT_RELA or an
     
    822915    abort ();
    823916
     917  /* The address of an ELF reloc is section relative for an object
     918     file, and absolute for an executable file or shared library.
     919     The address of a BFD reloc is always section relative.  */
     920  addr_offset = 0;
     921  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
     922    addr_offset = sec->vma;
     923
    824924  /* orelocation has the data, reloc_count has the count...  */
    825   if (use_rela_p)
    826     {
    827       outbound_relocas = (Elf_External_Rela *) rela_hdr->contents;
    828 
    829       for (idx = 0; idx < sec->reloc_count; idx++)
     925  last_sym = 0;
     926  last_sym_idx = 0;
     927  dst_rela = rela_hdr->contents;
     928
     929  for (idx = 0; idx < sec->reloc_count; idx++, dst_rela += extsize)
     930    {
     931      Elf_Internal_Rela src_rela;
     932      arelent *ptr;
     933      asymbol *sym;
     934      int n;
     935
     936      ptr = sec->orelocation[idx];
     937      sym = *ptr->sym_ptr_ptr;
     938      if (sym == last_sym)
     939        n = last_sym_idx;
     940      else if (bfd_is_abs_section (sym->section) && sym->value == 0)
     941        n = STN_UNDEF;
     942      else
    830943        {
    831           Elf_Internal_Rela dst_rela;
    832           Elf_External_Rela *src_rela;
    833           arelent *ptr;
    834           asymbol *sym;
    835           int n;
    836 
    837           ptr = sec->orelocation[idx];
    838           src_rela = outbound_relocas + idx;
    839 
    840           /* The address of an ELF reloc is section relative for an object
    841              file, and absolute for an executable file or shared library.
    842              The address of a BFD reloc is always section relative.  */
    843           if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
    844             dst_rela.r_offset = ptr->address;
    845           else
    846             dst_rela.r_offset = ptr->address + sec->vma;
    847 
    848           sym = *ptr->sym_ptr_ptr;
    849           if (sym == last_sym)
    850             n = last_sym_idx;
    851           else if (bfd_is_abs_section (sym->section) && sym->value == 0)
    852             n = STN_UNDEF;
    853           else
     944          last_sym = sym;
     945          n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
     946          if (n < 0)
    854947            {
    855               last_sym = sym;
    856               n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
    857               if (n < 0)
    858                 {
    859                   *failedp = true;
    860                   return;
    861                 }
    862               last_sym_idx = n;
    863             }
    864 
    865           if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
    866               && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
    867               && ! _bfd_elf_validate_reloc (abfd, ptr))
    868             {
    869               *failedp = true;
     948              *failedp = TRUE;
    870949              return;
    871950            }
    872 
    873           dst_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
    874 
    875           dst_rela.r_addend = ptr->addend;
    876           elf_swap_reloca_out (abfd, &dst_rela, src_rela);
     951          last_sym_idx = n;
    877952        }
    878     }
    879   else
    880     /* REL relocations */
    881     {
    882       outbound_relocs = (Elf_External_Rel *) rela_hdr->contents;
    883 
    884       for (idx = 0; idx < sec->reloc_count; idx++)
     953
     954      if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
     955          && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
     956          && ! _bfd_elf_validate_reloc (abfd, ptr))
    885957        {
    886           Elf_Internal_Rel dst_rel;
    887           Elf_External_Rel *src_rel;
    888           arelent *ptr;
    889           int n;
    890           asymbol *sym;
    891 
    892           ptr = sec->orelocation[idx];
    893           sym = *ptr->sym_ptr_ptr;
    894           src_rel = outbound_relocs + idx;
    895 
    896           /* The address of an ELF reloc is section relative for an object
    897              file, and absolute for an executable file or shared library.
    898              The address of a BFD reloc is always section relative.  */
    899           if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
    900             dst_rel.r_offset = ptr->address;
    901           else
    902             dst_rel.r_offset = ptr->address + sec->vma;
    903 
    904           if (sym == last_sym)
    905             n = last_sym_idx;
    906           else if (bfd_is_abs_section (sym->section) && sym->value == 0)
    907             n = STN_UNDEF;
    908           else
    909             {
    910               last_sym = sym;
    911               n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
    912               if (n < 0)
    913                 {
    914                   *failedp = true;
    915                   return;
    916                 }
    917               last_sym_idx = n;
    918             }
    919 
    920           if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
    921               && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
    922               && ! _bfd_elf_validate_reloc (abfd, ptr))
    923             {
    924               *failedp = true;
    925               return;
    926             }
    927 
    928           dst_rel.r_info = ELF_R_INFO (n, ptr->howto->type);
    929 
    930           elf_swap_reloc_out (abfd, &dst_rel, src_rel);
     958          *failedp = TRUE;
     959          return;
    931960        }
     961
     962      src_rela.r_offset = ptr->address + addr_offset;
     963      src_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
     964      src_rela.r_addend = ptr->addend;
     965      (*swap_out) (abfd, &src_rela, dst_rela);
    932966    }
    933967}
     
    939973     bfd *abfd;
    940974     const Elf_Internal_Phdr *phdr;
    941      int count;
     975     unsigned int count;
    942976{
    943977  while (count--)
     
    945979      Elf_External_Phdr extphdr;
    946980      elf_swap_phdr_out (abfd, phdr, &extphdr);
    947       if (bfd_write (&extphdr, sizeof (Elf_External_Phdr), 1, abfd)
    948           != sizeof (Elf_External_Phdr))
     981      if (bfd_bwrite (&extphdr, (bfd_size_type) sizeof (Elf_External_Phdr),
     982                     abfd) != sizeof (Elf_External_Phdr))
    949983        return -1;
    950984      phdr++;
     
    955989/* Write out the section headers and the ELF file header.  */
    956990
    957 boolean
     991bfd_boolean
    958992elf_write_shdrs_and_ehdr (abfd)
    959993     bfd *abfd;
     
    964998  Elf_Internal_Shdr **i_shdrp;  /* Section header table, internal form */
    965999  unsigned int count;
     1000  bfd_size_type amt;
    9661001
    9671002  i_ehdrp = elf_elfheader (abfd);
     
    9741009#endif
    9751010  elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
     1011  amt = sizeof (x_ehdr);
    9761012  if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
    977       || (bfd_write ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd)
    978           != sizeof (x_ehdr)))
    979     return false;
     1013      || bfd_bwrite ((PTR) & x_ehdr, amt, abfd) != amt)
     1014    return FALSE;
     1015
     1016  /* Some fields in the first section header handle overflow of ehdr
     1017     fields.  */
     1018  if (i_ehdrp->e_shnum >= SHN_LORESERVE)
     1019    i_shdrp[0]->sh_size = i_ehdrp->e_shnum;
     1020  if (i_ehdrp->e_shstrndx >= SHN_LORESERVE)
     1021    i_shdrp[0]->sh_link = i_ehdrp->e_shstrndx;
    9801022
    9811023  /* at this point we've concocted all the ELF sections...  */
    982   x_shdrp = (Elf_External_Shdr *)
    983     bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum));
     1024  amt = i_ehdrp->e_shnum;
     1025  amt *= sizeof (*x_shdrp);
     1026  x_shdrp = (Elf_External_Shdr *) bfd_alloc (abfd, amt);
    9841027  if (!x_shdrp)
    985     return false;
    986 
    987   for (count = 0; count < i_ehdrp->e_shnum; count++)
     1028    return FALSE;
     1029
     1030  for (count = 0; count < i_ehdrp->e_shnum; i_shdrp++, count++)
    9881031    {
    9891032#if DEBUG & 2
    990       elf_debug_section (count, i_shdrp[count]);
     1033      elf_debug_section (count, *i_shdrp);
    9911034#endif
    992       elf_swap_shdr_out (abfd, i_shdrp[count], x_shdrp + count);
     1035      elf_swap_shdr_out (abfd, *i_shdrp, x_shdrp + count);
     1036
     1037      if (count == SHN_LORESERVE - 1)
     1038        i_shdrp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
    9931039    }
    9941040  if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0
    995       || (bfd_write ((PTR) x_shdrp, sizeof (*x_shdrp), i_ehdrp->e_shnum, abfd)
    996           != sizeof (*x_shdrp) * i_ehdrp->e_shnum))
    997     return false;
     1041      || bfd_bwrite ((PTR) x_shdrp, amt, abfd) != amt)
     1042    return FALSE;
    9981043
    9991044  /* need to dump the string table too...  */
    10001045
    1001   return true;
     1046  return TRUE;
    10021047}
    10031048
     
    10061051     bfd *abfd;
    10071052     asymbol **symptrs;         /* Buffer for generated bfd symbols */
    1008      boolean dynamic;
     1053     bfd_boolean dynamic;
    10091054{
    10101055  Elf_Internal_Shdr *hdr;
     
    10131058  elf_symbol_type *sym;         /* Pointer to current bfd symbol */
    10141059  elf_symbol_type *symbase;     /* Buffer for generated bfd symbols */
    1015   Elf_Internal_Sym i_sym;
    1016   Elf_External_Sym *x_symp = NULL;
    1017   Elf_External_Versym *x_versymp = NULL;
     1060  Elf_Internal_Sym *isym;
     1061  Elf_Internal_Sym *isymend;
     1062  Elf_Internal_Sym *isymbuf = NULL;
     1063  Elf_External_Versym *xver;
     1064  Elf_External_Versym *xverbuf = NULL;
     1065  struct elf_backend_data *ebd;
     1066  bfd_size_type amt;
    10181067
    10191068  /* Read each raw ELF symbol, converting from external ELF form to
     
    10491098    }
    10501099
    1051   if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
    1052     return -1;
    1053 
     1100  ebd = get_elf_backend_data (abfd);
    10541101  symcount = hdr->sh_size / sizeof (Elf_External_Sym);
    1055 
    10561102  if (symcount == 0)
    10571103    sym = symbase = NULL;
    10581104  else
    10591105    {
    1060       unsigned long i;
    1061 
    1062       if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
     1106      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, symcount, 0,
     1107                                      NULL, NULL, NULL);
     1108      if (isymbuf == NULL)
    10631109        return -1;
    10641110
    1065       symbase = ((elf_symbol_type *)
    1066                  bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type)));
     1111      amt = symcount;
     1112      amt *= sizeof (elf_symbol_type);
     1113      symbase = (elf_symbol_type *) bfd_zalloc (abfd, amt);
    10671114      if (symbase == (elf_symbol_type *) NULL)
    1068         return -1;
    1069       sym = symbase;
    1070 
    1071       /* Temporarily allocate room for the raw ELF symbols.  */
    1072       x_symp = ((Elf_External_Sym *)
    1073                 bfd_malloc (symcount * sizeof (Elf_External_Sym)));
    1074       if (x_symp == NULL && symcount != 0)
    10751115        goto error_return;
    10761116
    1077       if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd)
    1078           != symcount * sizeof (Elf_External_Sym))
    1079         goto error_return;
    1080 
    10811117      /* Read the raw ELF version symbol information.  */
    1082 
    10831118      if (verhdr != NULL
    10841119          && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount)
     
    11001135            goto error_return;
    11011136
    1102           x_versymp = (Elf_External_Versym *) bfd_malloc (verhdr->sh_size);
    1103           if (x_versymp == NULL && verhdr->sh_size != 0)
     1137          xverbuf = (Elf_External_Versym *) bfd_malloc (verhdr->sh_size);
     1138          if (xverbuf == NULL && verhdr->sh_size != 0)
    11041139            goto error_return;
    11051140
    1106           if (bfd_read ((PTR) x_versymp, 1, verhdr->sh_size, abfd)
     1141          if (bfd_bread ((PTR) xverbuf, verhdr->sh_size, abfd)
    11071142              != verhdr->sh_size)
    11081143            goto error_return;
     
    11101145
    11111146      /* Skip first symbol, which is a null dummy.  */
    1112       for (i = 1; i < symcount; i++)
     1147      xver = xverbuf;
     1148      if (xver != NULL)
     1149        ++xver;
     1150      isymend = isymbuf + symcount;
     1151      for (isym = isymbuf + 1, sym = symbase; isym < isymend; isym++, sym++)
    11131152        {
    1114           elf_swap_symbol_in (abfd, x_symp + i, &i_sym);
    1115           memcpy (&sym->internal_elf_sym, &i_sym, sizeof (Elf_Internal_Sym));
    1116 #ifdef ELF_KEEP_EXTSYM
    1117           memcpy (&sym->native_elf_sym, x_symp + i, sizeof (Elf_External_Sym));
    1118 #endif
     1153          memcpy (&sym->internal_elf_sym, isym, sizeof (Elf_Internal_Sym));
    11191154          sym->symbol.the_bfd = abfd;
    11201155
    11211156          sym->symbol.name = bfd_elf_string_from_elf_section (abfd,
    11221157                                                              hdr->sh_link,
    1123                                                               i_sym.st_name);
    1124 
    1125           sym->symbol.value = i_sym.st_value;
    1126 
    1127           if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERVE)
     1158                                                              isym->st_name);
     1159
     1160          sym->symbol.value = isym->st_value;
     1161
     1162          if (isym->st_shndx == SHN_UNDEF)
     1163            {
     1164              sym->symbol.section = bfd_und_section_ptr;
     1165            }
     1166          else if (isym->st_shndx < SHN_LORESERVE
     1167                   || isym->st_shndx > SHN_HIRESERVE)
    11281168            {
    11291169              sym->symbol.section = section_from_elf_index (abfd,
    1130                                                             i_sym.st_shndx);
     1170                                                            isym->st_shndx);
    11311171              if (sym->symbol.section == NULL)
    11321172                {
     
    11371177                }
    11381178            }
    1139           else if (i_sym.st_shndx == SHN_ABS)
     1179          else if (isym->st_shndx == SHN_ABS)
    11401180            {
    11411181              sym->symbol.section = bfd_abs_section_ptr;
    11421182            }
    1143           else if (i_sym.st_shndx == SHN_COMMON)
     1183          else if (isym->st_shndx == SHN_COMMON)
    11441184            {
    11451185              sym->symbol.section = bfd_com_section_ptr;
     
    11481188                 size in the value field, and doesn't care (at the
    11491189                 moment) about the alignment.  */
    1150               sym->symbol.value = i_sym.st_size;
    1151             }
    1152           else if (i_sym.st_shndx == SHN_UNDEF)
    1153             {
    1154               sym->symbol.section = bfd_und_section_ptr;
     1190              sym->symbol.value = isym->st_size;
    11551191            }
    11561192          else
     
    11621198            sym->symbol.value -= sym->symbol.section->vma;
    11631199
    1164           switch (ELF_ST_BIND (i_sym.st_info))
     1200          switch (ELF_ST_BIND (isym->st_info))
    11651201            {
    11661202            case STB_LOCAL:
     
    11681204              break;
    11691205            case STB_GLOBAL:
    1170               if (i_sym.st_shndx != SHN_UNDEF
    1171                   && i_sym.st_shndx != SHN_COMMON)
     1206              if (isym->st_shndx != SHN_UNDEF && isym->st_shndx != SHN_COMMON)
    11721207                sym->symbol.flags |= BSF_GLOBAL;
    11731208              break;
     
    11771212            }
    11781213
    1179           switch (ELF_ST_TYPE (i_sym.st_info))
     1214          switch (ELF_ST_TYPE (isym->st_info))
    11801215            {
    11811216            case STT_SECTION:
     
    11961231            sym->symbol.flags |= BSF_DYNAMIC;
    11971232
    1198           if (x_versymp != NULL)
     1233          if (xver != NULL)
    11991234            {
    12001235              Elf_Internal_Versym iversym;
    12011236
    1202               _bfd_elf_swap_versym_in (abfd, x_versymp + i, &iversym);
     1237              _bfd_elf_swap_versym_in (abfd, xver, &iversym);
    12031238              sym->version = iversym.vs_vers;
     1239              xver++;
    12041240            }
    12051241
    12061242          /* Do some backend-specific processing on this symbol.  */
    1207           {
    1208             struct elf_backend_data *ebd = get_elf_backend_data (abfd);
    1209             if (ebd->elf_backend_symbol_processing)
    1210               (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
    1211           }
    1212 
    1213           sym++;
     1243          if (ebd->elf_backend_symbol_processing)
     1244            (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
    12141245        }
    12151246    }
    12161247
    12171248  /* Do some backend-specific processing on this symbol table.  */
    1218   {
    1219     struct elf_backend_data *ebd = get_elf_backend_data (abfd);
    1220     if (ebd->elf_backend_symbol_table_processing)
    1221       (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
    1222   }
     1249  if (ebd->elf_backend_symbol_table_processing)
     1250    (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
    12231251
    12241252  /* We rely on the zalloc to clear out the final symbol entry.  */
     
    12401268    }
    12411269
    1242   if (x_versymp != NULL)
    1243     free (x_versymp);
    1244   if (x_symp != NULL)
    1245     free (x_symp);
     1270  if (xverbuf != NULL)
     1271    free (xverbuf);
     1272  if (isymbuf != NULL && hdr->contents != (unsigned char *) isymbuf)
     1273    free (isymbuf);
    12461274  return symcount;
     1275
    12471276error_return:
    1248   if (x_versymp != NULL)
    1249     free (x_versymp);
    1250   if (x_symp != NULL)
    1251     free (x_symp);
     1277  if (xverbuf != NULL)
     1278    free (xverbuf);
     1279  if (isymbuf != NULL && hdr->contents != (unsigned char *) isymbuf)
     1280    free (isymbuf);
    12521281  return -1;
    12531282}
     
    12561285   them.  */
    12571286
    1258 static boolean
     1287static bfd_boolean
    12591288elf_slurp_reloc_table_from_section (abfd, asect, rel_hdr, reloc_count,
    12601289                                    relents, symbols, dynamic)
     
    12651294     arelent *relents;
    12661295     asymbol **symbols;
    1267      boolean dynamic;
     1296     bfd_boolean dynamic;
    12681297{
    12691298  struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
     
    12731302  unsigned int i;
    12741303  int entsize;
    1275 
    1276   allocated = (PTR) bfd_malloc ((size_t) rel_hdr->sh_size);
     1304  unsigned int symcount;
     1305
     1306  allocated = (PTR) bfd_malloc (rel_hdr->sh_size);
    12771307  if (allocated == NULL)
    12781308    goto error_return;
    12791309
    12801310  if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0
    1281       || (bfd_read (allocated, 1, rel_hdr->sh_size, abfd)
     1311      || (bfd_bread (allocated, rel_hdr->sh_size, abfd)
    12821312          != rel_hdr->sh_size))
    12831313    goto error_return;
     
    12891319              || entsize == sizeof (Elf_External_Rela));
    12901320
     1321  if (dynamic)
     1322    symcount = bfd_get_dynamic_symcount (abfd);
     1323  else
     1324    symcount = bfd_get_symcount (abfd);
     1325
    12911326  for (i = 0, relent = relents;
    12921327       i < reloc_count;
     
    12941329    {
    12951330      Elf_Internal_Rela rela;
    1296       Elf_Internal_Rel rel;
    12971331
    12981332      if (entsize == sizeof (Elf_External_Rela))
    1299         elf_swap_reloca_in (abfd, (Elf_External_Rela *) native_relocs, &rela);
     1333        elf_swap_reloca_in (abfd, native_relocs, &rela);
    13001334      else
    1301         {
    1302           elf_swap_reloc_in (abfd, (Elf_External_Rel *) native_relocs, &rel);
    1303           rela.r_offset = rel.r_offset;
    1304           rela.r_info = rel.r_info;
    1305           rela.r_addend = 0;
    1306         }
     1335        elf_swap_reloc_in (abfd, native_relocs, &rela);
    13071336
    13081337      /* The address of an ELF reloc is section relative for an object
     
    13171346      if (ELF_R_SYM (rela.r_info) == 0)
    13181347        relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
     1348      else if (ELF_R_SYM (rela.r_info) > symcount)
     1349        {
     1350          (*_bfd_error_handler)
     1351            (_("%s(%s): relocation %d has invalid symbol index %ld"),
     1352             abfd->filename, asect->name, i, ELF_R_SYM (rela.r_info));
     1353          relent->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
     1354        }
    13191355      else
    13201356        {
     
    13331369      relent->addend = rela.r_addend;
    13341370
    1335       if (entsize == sizeof (Elf_External_Rela))
     1371      if ((entsize == sizeof (Elf_External_Rela)
     1372           && ebd->elf_info_to_howto != NULL)
     1373          || ebd->elf_info_to_howto_rel == NULL)
    13361374        (*ebd->elf_info_to_howto) (abfd, relent, &rela);
    13371375      else
    1338         (*ebd->elf_info_to_howto_rel) (abfd, relent, &rel);
     1376        (*ebd->elf_info_to_howto_rel) (abfd, relent, &rela);
    13391377    }
    13401378
     
    13421380    free (allocated);
    13431381
    1344   return true;
     1382  return TRUE;
    13451383
    13461384 error_return:
    13471385  if (allocated != NULL)
    13481386    free (allocated);
    1349   return false;
     1387  return FALSE;
    13501388}
    13511389
    13521390/* Read in and swap the external relocs.  */
    13531391
    1354 boolean
     1392bfd_boolean
    13551393elf_slurp_reloc_table (abfd, asect, symbols, dynamic)
    13561394     bfd *abfd;
    13571395     asection *asect;
    13581396     asymbol **symbols;
    1359      boolean dynamic;
     1397     bfd_boolean dynamic;
    13601398{
    13611399  struct bfd_elf_section_data * const d = elf_section_data (asect);
     
    13651403  bfd_size_type reloc_count2;
    13661404  arelent *relents;
     1405  bfd_size_type amt;
    13671406
    13681407  if (asect->relocation != NULL)
    1369     return true;
     1408    return TRUE;
    13701409
    13711410  if (! dynamic)
     
    13731412      if ((asect->flags & SEC_RELOC) == 0
    13741413          || asect->reloc_count == 0)
    1375         return true;
     1414        return TRUE;
    13761415
    13771416      rel_hdr = &d->rel_hdr;
     
    13921431         in elf.c does not update the RELOC_COUNT.  */
    13931432      if (asect->_raw_size == 0)
    1394         return true;
     1433        return TRUE;
    13951434
    13961435      rel_hdr = &d->this_hdr;
     
    14001439    }
    14011440
    1402   relents = ((arelent *)
    1403              bfd_alloc (abfd,
    1404                         (reloc_count + reloc_count2) * sizeof (arelent)));
     1441  amt = (reloc_count + reloc_count2) * sizeof (arelent);
     1442  relents = (arelent *) bfd_alloc (abfd, amt);
    14051443  if (relents == NULL)
    1406     return false;
     1444    return FALSE;
    14071445
    14081446  if (!elf_slurp_reloc_table_from_section (abfd, asect,
     
    14101448                                           relents,
    14111449                                           symbols, dynamic))
    1412     return false;
     1450    return FALSE;
    14131451
    14141452  if (rel_hdr2
     
    14171455                                              relents + reloc_count,
    14181456                                              symbols, dynamic))
    1419     return false;
     1457    return FALSE;
    14201458
    14211459  asect->relocation = relents;
    1422   return true;
     1460  return TRUE;
    14231461}
    14241462
     
    15601598  elf_write_shdrs_and_ehdr,
    15611599  elf_write_relocs,
     1600  elf_swap_symbol_in,
    15621601  elf_swap_symbol_out,
    15631602  elf_slurp_reloc_table,
     
    15651604  elf_swap_dyn_in,
    15661605  elf_swap_dyn_out,
    1567   NULL,
    1568   NULL,
    1569   NULL,
    1570   NULL
     1606  elf_swap_reloc_in,
     1607  elf_swap_reloc_out,
     1608  elf_swap_reloca_in,
     1609  elf_swap_reloca_out
    15711610};
Note: See TracChangeset for help on using the changeset viewer.