Home | History | Annotate | Download | only in libelf
      1 /* Get library from table at the given index.
      2    Copyright (C) 2004 Red Hat, Inc.
      3    Written by Ulrich Drepper <drepper (at) redhat.com>, 2004.
      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 <assert.h>
     23 #include <gelf.h>
     24 #include <string.h>
     25 
     26 #include "libelfP.h"
     27 
     28 
     29 GElf_Lib *
     30 gelf_getlib (data, ndx, dst)
     31      Elf_Data *data;
     32      int ndx;
     33      GElf_Lib *dst;
     34 {
     35   if (data == NULL)
     36     return NULL;
     37 
     38   if (unlikely (data->d_type != ELF_T_LIB))
     39     {
     40       __libelf_seterrno (ELF_E_INVALID_HANDLE);
     41       return NULL;
     42     }
     43 
     44   Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
     45 
     46   rwlock_rdlock (data_scn->s->elf->lock);
     47 
     48   /* The on disk format of Elf32_Lib and Elf64_Lib is identical.  So
     49      we can simplify things significantly.  */
     50   assert (sizeof (GElf_Lib) == sizeof (Elf32_Lib));
     51   assert (sizeof (GElf_Lib) == sizeof (Elf64_Lib));
     52 
     53   /* The data is already in the correct form.  Just make sure the
     54      index is OK.  */
     55   GElf_Lib *result = NULL;
     56   if (unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
     57     __libelf_seterrno (ELF_E_INVALID_INDEX);
     58   else
     59     {
     60       *dst = ((GElf_Lib *) data->d_buf)[ndx];
     61 
     62       result = dst;
     63     }
     64 
     65   rwlock_unlock (data_scn->s->elf->lock);
     66 
     67   return result;
     68 }
     69