Home | History | Annotate | Download | only in libelf
      1 /* Update REL relocation information at given index.
      2    Copyright (C) 2000, 2001, 2002 Red Hat, Inc.
      3    Written by Ulrich Drepper <drepper (at) redhat.com>, 2000.
      4 
      5    This program is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by
      7    the Free Software Foundation, version 2.
      8 
      9    This program is distributed in the hope that it will be useful,
     10    but WITHOUT ANY WARRANTY; without even the implied warranty of
     11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12    GNU General Public License for more details.
     13 
     14    You should have received a copy of the GNU General Public License
     15    along with this program; if not, write to the Free Software Foundation,
     16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
     17 
     18 #ifdef HAVE_CONFIG_H
     19 # include <config.h>
     20 #endif
     21 
     22 #include <gelf.h>
     23 #include <stdlib.h>
     24 
     25 #include "libelfP.h"
     26 
     27 
     28 int
     29 gelf_update_rel (Elf_Data *dst, int ndx, GElf_Rel *src)
     30 {
     31   Elf_Data_Scn *data_scn = (Elf_Data_Scn *) dst;
     32   Elf_Scn *scn;
     33   int result = 0;
     34 
     35   if (dst == NULL)
     36     return 0;
     37 
     38   if (unlikely (ndx < 0))
     39     {
     40       __libelf_seterrno (ELF_E_INVALID_INDEX);
     41       return 0;
     42     }
     43 
     44   if (unlikely (data_scn->d.d_type != ELF_T_REL))
     45     {
     46       /* The type of the data better should match.  */
     47       __libelf_seterrno (ELF_E_DATA_MISMATCH);
     48       return 0;
     49     }
     50 
     51   scn = data_scn->s;
     52   rwlock_wrlock (scn->elf->lock);
     53 
     54   if (scn->elf->class == ELFCLASS32)
     55     {
     56       Elf32_Rel *rel;
     57 
     58       /* There is the possibility that the values in the input are
     59 	 too large.  */
     60       if (unlikely (src->r_offset > 0xffffffffull)
     61 	  || unlikely (GELF_R_SYM (src->r_info) > 0xffffff)
     62 	  || unlikely (GELF_R_TYPE (src->r_info) > 0xff))
     63 	{
     64 	  __libelf_seterrno (ELF_E_INVALID_DATA);
     65 	  goto out;
     66 	}
     67 
     68       /* Check whether we have to resize the data buffer.  */
     69       if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
     70 	{
     71 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
     72 	  goto out;
     73 	}
     74 
     75       rel = &((Elf32_Rel *) data_scn->d.d_buf)[ndx];
     76 
     77       rel->r_offset = src->r_offset;
     78       rel->r_info = ELF32_R_INFO (GELF_R_SYM (src->r_info),
     79 				  GELF_R_TYPE (src->r_info));
     80     }
     81   else
     82     {
     83       /* Check whether we have to resize the data buffer.  */
     84       if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
     85 	{
     86 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
     87 	  goto out;
     88 	}
     89 
     90       ((Elf64_Rel *) data_scn->d.d_buf)[ndx] = *src;
     91     }
     92 
     93   result = 1;
     94 
     95   /* Mark the section as modified.  */
     96   scn->flags |= ELF_F_DIRTY;
     97 
     98  out:
     99   rwlock_unlock (scn->elf->lock);
    100 
    101   return result;
    102 }
    103