Home | History | Annotate | Download | only in libelf
      1 /* Create clone of a given descriptor.
      2    Copyright (C) 2003, 2004 Red Hat, Inc.
      3    Written by Ulrich Drepper <drepper (at) redhat.com>, 2003.
      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 <stddef.h>
     24 #include "libelfP.h"
     25 #include "common.h"
     26 
     27 
     28 Elf *
     29 elf_clone (Elf *elf, Elf_Cmd cmd)
     30 {
     31   Elf *retval = NULL;
     32 
     33   if (elf == NULL)
     34     /* Some earlier mistake.  */
     35     return NULL;
     36 
     37   /* Make sure the descriptor is not suddenly going away.  */
     38   rwlock_rdlock (elf->lock);
     39 
     40   if (cmd != ELF_C_EMPTY)
     41     // XXX TODO handle ELF_C_READ/WRITE etc
     42     goto out;
     43 
     44   retval = allocate_elf (elf->fildes, elf->map_address, elf->start_offset,
     45 			 elf->maximum_size, elf->cmd, elf->parent, elf->kind,
     46 			 elf->state.elf32.scns.max * sizeof (Elf_Scn));
     47   if (retval != NULL)
     48     {
     49       /* We have to write to the file in any case.  */
     50       retval->flags = ELF_F_DIRTY;
     51 
     52       /* Some more or less arbitrary value.  */
     53       retval->state.elf.scnincr = 10;
     54 
     55       /* We have allocated room for some sections.  */
     56       assert (offsetof (struct Elf, state.elf32.scns)
     57 	      == offsetof (struct Elf, state.elf64.scns));
     58       retval->state.elf.scns_last = &retval->state.elf32.scns;
     59       retval->state.elf32.scns.max = elf->state.elf32.scns.max;
     60 
     61       retval->class = elf->class;
     62     }
     63 
     64   /* Release the lock.  */
     65  out:
     66   rwlock_unlock (elf->lock);
     67 
     68   return retval;
     69 }
     70