00001 /* 00002 * Copyright 2005 PathScale, Inc. All Rights Reserved. 00003 */ 00004 00005 /* 00006 x.remscn.c - implementation of the elfx_remscn(3) function. 00007 Copyright (C) 1995 - 2001, 2003 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: x.remscn.c,v 1.1.1.1 2005/10/21 19:00:00 marcel Exp $"; 00028 #endif /* lint */ 00029 00030 size_t 00031 elfx_remscn(Elf *elf, Elf_Scn *scn) { 00032 Elf_Scn *pscn; 00033 Scn_Data *sd; 00034 Scn_Data *tmp; 00035 size_t index; 00036 00037 if (!elf || !scn) { 00038 return SHN_UNDEF; 00039 } 00040 elf_assert(elf->e_magic == ELF_MAGIC); 00041 if (elf->e_kind != ELF_K_ELF) { 00042 seterr(ERROR_NOTELF); 00043 return SHN_UNDEF; 00044 } 00045 elf_assert(scn->s_magic == SCN_MAGIC); 00046 elf_assert(elf->e_ehdr); 00047 if (scn->s_elf != elf) { 00048 seterr(ERROR_ELFSCNMISMATCH); 00049 return SHN_UNDEF; 00050 } 00051 elf_assert(elf->e_scn_1); 00052 if (scn == elf->e_scn_1) { 00053 seterr(ERROR_NULLSCN); 00054 return SHN_UNDEF; 00055 } 00056 00057 /* 00058 * Find previous section. 00059 */ 00060 for (pscn = elf->e_scn_1; pscn->s_link; pscn = pscn->s_link) { 00061 if (pscn->s_link == scn) { 00062 break; 00063 } 00064 } 00065 if (pscn->s_link != scn) { 00066 seterr(ERROR_ELFSCNMISMATCH); 00067 return SHN_UNDEF; 00068 } 00069 00070 /* 00071 * Unlink section. 00072 */ 00073 if (elf->e_scn_n == scn) { 00074 elf->e_scn_n = pscn; 00075 } 00076 pscn->s_link = scn->s_link; 00077 index = scn->s_index; 00078 00079 /* 00080 * Free section descriptor and data. 00081 */ 00082 for (sd = scn->s_data_1; sd; sd = tmp) { 00083 elf_assert(sd->sd_magic == DATA_MAGIC); 00084 elf_assert(sd->sd_scn == scn); 00085 tmp = sd->sd_link; 00086 if (sd->sd_free_data && sd->sd_memdata) { 00087 free(sd->sd_memdata); 00088 } 00089 if (sd->sd_freeme) { 00090 free(sd); 00091 } 00092 } 00093 if ((sd = scn->s_rawdata)) { 00094 elf_assert(sd->sd_magic == DATA_MAGIC); 00095 elf_assert(sd->sd_scn == scn); 00096 if (sd->sd_free_data && sd->sd_memdata) { 00097 free(sd->sd_memdata); 00098 } 00099 if (sd->sd_freeme) { 00100 free(sd); 00101 } 00102 } 00103 if (scn->s_freeme) { 00104 elf_assert(scn->s_index > 0); 00105 free(scn); 00106 } 00107 00108 /* 00109 * Adjust section indices. 00110 */ 00111 for (scn = pscn->s_link; scn; scn = scn->s_link) { 00112 elf_assert(scn->s_index > index); 00113 scn->s_index--; 00114 } 00115 00116 /* 00117 * Adjust section count in ELF header 00118 */ 00119 if (_elf_update_shnum(elf, elf->e_scn_n->s_index + 1)) { 00120 return SHN_UNDEF; 00121 } 00122 return index; 00123 }
1.5.6