00001 /* 00002 * Copyright 2005 PathScale, Inc. All Rights Reserved. 00003 */ 00004 00005 /* 00006 end.c - implementation of the elf_end(3) function. 00007 Copyright (C) 1995 - 2000 Michael Riepe <michael@stud.uni-hannover.de> 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Library General Public 00011 License as published by the Free Software Foundation; either 00012 version 2 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Library General Public License for more details. 00018 00019 You should have received a copy of the GNU Library General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00022 */ 00023 00024 #include <private.h> 00025 00026 #ifndef lint 00027 static const char rcsid[] = "@(#) $Id: end.c,v 1.1.1.1 2005/10/21 19:00:00 marcel Exp $"; 00028 #endif /* lint */ 00029 00030 #if HAVE_MMAP 00031 #include <sys/mman.h> 00032 #endif /* HAVE_MMAP */ 00033 00034 static void 00035 _elf_free(void *ptr) { 00036 if (ptr) { 00037 free(ptr); 00038 } 00039 } 00040 00041 static void 00042 _elf_free_scns(Elf *elf, Elf_Scn *scn) { 00043 Scn_Data *sd, *tmp; 00044 Elf_Scn *freescn; 00045 00046 for (freescn = NULL; scn; scn = scn->s_link) { 00047 elf_assert(scn->s_magic == SCN_MAGIC); 00048 elf_assert(scn->s_elf == elf); 00049 for (sd = scn->s_data_1; sd; sd = tmp) { 00050 elf_assert(sd->sd_magic == DATA_MAGIC); 00051 elf_assert(sd->sd_scn == scn); 00052 tmp = sd->sd_link; 00053 if (sd->sd_free_data) { 00054 _elf_free(sd->sd_memdata); 00055 } 00056 if (sd->sd_freeme) { 00057 free(sd); 00058 } 00059 } 00060 if ((sd = scn->s_rawdata)) { 00061 elf_assert(sd->sd_magic == DATA_MAGIC); 00062 elf_assert(sd->sd_scn == scn); 00063 if (sd->sd_free_data) { 00064 _elf_free(sd->sd_memdata); 00065 } 00066 if (sd->sd_freeme) { 00067 free(sd); 00068 } 00069 } 00070 if (scn->s_freeme) { 00071 _elf_free(freescn); 00072 freescn = scn; 00073 } 00074 } 00075 _elf_free(freescn); 00076 } 00077 00078 int 00079 elf_end(Elf *elf) { 00080 Elf **siblings; 00081 00082 if (!elf) { 00083 return 0; 00084 } 00085 elf_assert(elf->e_magic == ELF_MAGIC); 00086 if (--elf->e_count) { 00087 return elf->e_count; 00088 } 00089 if (elf->e_parent) { 00090 elf_assert(elf->e_parent->e_magic == ELF_MAGIC); 00091 elf_assert(elf->e_parent->e_kind == ELF_K_AR); 00092 siblings = &elf->e_parent->e_members; 00093 while (*siblings) { 00094 if (*siblings == elf) { 00095 *siblings = elf->e_link; 00096 break; 00097 } 00098 siblings = &(*siblings)->e_link; 00099 } 00100 elf_end(elf->e_parent); 00101 _elf_free(elf->e_arhdr); 00102 } 00103 #if HAVE_MMAP 00104 else if (elf->e_unmap_data) { 00105 munmap(elf->e_data, elf->e_size); 00106 } 00107 #endif /* HAVE_MMAP */ 00108 else if (!elf->e_memory) { 00109 _elf_free(elf->e_data); 00110 } 00111 _elf_free_scns(elf, elf->e_scn_1); 00112 if (elf->e_rawdata != elf->e_data) { 00113 _elf_free(elf->e_rawdata); 00114 } 00115 if (elf->e_free_syms) { 00116 _elf_free(elf->e_symtab); 00117 } 00118 if (elf->e_free_ehdr) { 00119 _elf_free(elf->e_ehdr); 00120 } 00121 if (elf->e_free_phdr) { 00122 _elf_free(elf->e_phdr); 00123 } 00124 free(elf); 00125 return 0; 00126 }
1.5.6