Home | History | Annotate | Download | only in libelf
      1 /* Return number of sections in the ELF file.
      2    Copyright (C) 2002 Red Hat, Inc.
      3    Written by Ulrich Drepper <drepper (at) redhat.com>, 2002.
      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 <stddef.h>
     25 
     26 #include "libelfP.h"
     27 
     28 
     29 int
     30 elf_getshnum (elf, dst)
     31      Elf *elf;
     32      size_t *dst;
     33 {
     34   int result = 0;
     35   int idx;
     36 
     37   if (elf == NULL)
     38     return -1;
     39 
     40   if (unlikely (elf->kind != ELF_K_ELF))
     41     {
     42       __libelf_seterrno (ELF_E_INVALID_HANDLE);
     43       return -1;
     44     }
     45 
     46   rwlock_rdlock (elf->lock);
     47 
     48   idx = elf->state.elf.scns_last->cnt;
     49   if (idx != 0
     50       || (elf->state.elf.scns_last
     51 	  != (elf->class == ELFCLASS32
     52 	      || (offsetof (Elf, state.elf32.scns)
     53 		  == offsetof (Elf, state.elf64.scns))
     54 	      ? &elf->state.elf32.scns : &elf->state.elf64.scns)))
     55     /* There is at least one section.  */
     56     *dst = 1 + elf->state.elf.scns_last->data[idx - 1].index;
     57   else
     58     *dst = 0;
     59 
     60   rwlock_unlock (elf->lock);
     61 
     62   return result;
     63 }
     64 INTDEF(elf_getshnum)
     65