1 /*
2 opt.delscn.c - implementation of the elf_delscn(3) function.
3 Copyright (C) 1995 - 2001 Michael Riepe
4 
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9 
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Library General Public License for more details.
14 
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19 
20 #include <private.h>
21 
22 #ifndef lint
23 static const char rcsid[] = "@(#) $Id: opt.delscn.c,v 1.12 2008/05/23 08:15:35 michael Exp $";
24 #endif /* lint */
25 
26 static size_t
_newindex(size_t old,size_t index)27 _newindex(size_t old, size_t index) {
28     return old == index ? SHN_UNDEF : (old > index ? old - 1 : old);
29 }
30 
31 static void
_elf32_update_shdr(Elf * elf,size_t index)32 _elf32_update_shdr(Elf *elf, size_t index) {
33     Elf32_Shdr *shdr;
34     Elf_Scn *scn;
35 
36     ((Elf32_Ehdr*)elf->e_ehdr)->e_shnum = elf->e_scn_n->s_index + 1;
37     for (scn = elf->e_scn_1; scn; scn = scn->s_link) {
38 	shdr = &scn->s_shdr32;
39 	switch (shdr->sh_type) {
40 	    case SHT_REL:
41 	    case SHT_RELA:
42 		shdr->sh_info = _newindex(shdr->sh_info, index);
43 		/* fall through */
44 	    case SHT_DYNSYM:
45 	    case SHT_DYNAMIC:
46 	    case SHT_HASH:
47 	    case SHT_SYMTAB:
48 #if __LIBELF_SYMBOL_VERSIONS
49 #if __LIBELF_SUN_SYMBOL_VERSIONS
50 	    case SHT_SUNW_verdef:
51 	    case SHT_SUNW_verneed:
52 	    case SHT_SUNW_versym:
53 #else /* __LIBELF_SUN_SYMBOL_VERSIONS */
54 	    case SHT_GNU_verdef:
55 	    case SHT_GNU_verneed:
56 	    case SHT_GNU_versym:
57 #endif /* __LIBELF_SUN_SYMBOL_VERSIONS */
58 #endif /* __LIBELF_SYMBOL_VERSIONS */
59 		shdr->sh_link = _newindex(shdr->sh_link, index);
60 		/* fall through */
61 	    default:
62 		break;
63 	}
64     }
65 }
66 
67 #if __LIBELF64
68 
69 static void
_elf64_update_shdr(Elf * elf,size_t index)70 _elf64_update_shdr(Elf *elf, size_t index) {
71     Elf64_Shdr *shdr;
72     Elf_Scn *scn;
73 
74     ((Elf64_Ehdr*)elf->e_ehdr)->e_shnum = elf->e_scn_n->s_index + 1;
75     for (scn = elf->e_scn_1; scn; scn = scn->s_link) {
76 	shdr = &scn->s_shdr64;
77 	switch (shdr->sh_type) {
78 	    case SHT_REL:
79 	    case SHT_RELA:
80 		shdr->sh_info = _newindex(shdr->sh_info, index);
81 		/* fall through */
82 	    case SHT_DYNSYM:
83 	    case SHT_DYNAMIC:
84 	    case SHT_HASH:
85 	    case SHT_SYMTAB:
86 #if __LIBELF_SYMBOL_VERSIONS
87 #if __LIBELF_SUN_SYMBOL_VERSIONS
88 	    case SHT_SUNW_verdef:
89 	    case SHT_SUNW_verneed:
90 	    case SHT_SUNW_versym:
91 #else /* __LIBELF_SUN_SYMBOL_VERSIONS */
92 	    case SHT_GNU_verdef:
93 	    case SHT_GNU_verneed:
94 	    case SHT_GNU_versym:
95 #endif /* __LIBELF_SUN_SYMBOL_VERSIONS */
96 #endif /* __LIBELF_SYMBOL_VERSIONS */
97 		shdr->sh_link = _newindex(shdr->sh_link, index);
98 		/* fall through */
99 	    default:
100 		break;
101 	}
102     }
103 }
104 
105 #endif /* __LIBELF64 */
106 
107 size_t
elf_delscn(Elf * elf,Elf_Scn * scn)108 elf_delscn(Elf *elf, Elf_Scn *scn) {
109     Elf_Scn *pscn;
110     Scn_Data *sd;
111     Scn_Data *tmp;
112     size_t index;
113 
114     if (!elf || !scn) {
115 	return SHN_UNDEF;
116     }
117     elf_assert(elf->e_magic == ELF_MAGIC);
118     elf_assert(scn->s_magic == SCN_MAGIC);
119     elf_assert(elf->e_ehdr);
120     if (scn->s_elf != elf) {
121 	seterr(ERROR_ELFSCNMISMATCH);
122 	return SHN_UNDEF;
123     }
124     elf_assert(elf->e_scn_1);
125     if (scn == elf->e_scn_1) {
126 	seterr(ERROR_NULLSCN);
127 	return SHN_UNDEF;
128     }
129 
130     /*
131      * Find previous section.
132      */
133     for (pscn = elf->e_scn_1; pscn->s_link; pscn = pscn->s_link) {
134 	if (pscn->s_link == scn) {
135 	    break;
136 	}
137     }
138     if (pscn->s_link != scn) {
139 	seterr(ERROR_ELFSCNMISMATCH);
140 	return SHN_UNDEF;
141     }
142     /*
143      * Unlink section.
144      */
145     if (elf->e_scn_n == scn) {
146 	elf->e_scn_n = pscn;
147     }
148     pscn->s_link = scn->s_link;
149     index = scn->s_index;
150     /*
151      * Free section descriptor and data.
152      */
153     for (sd = scn->s_data_1; sd; sd = tmp) {
154 	elf_assert(sd->sd_magic == DATA_MAGIC);
155 	elf_assert(sd->sd_scn == scn);
156 	tmp = sd->sd_link;
157 	if (sd->sd_free_data && sd->sd_memdata) {
158 	    free(sd->sd_memdata);
159 	}
160 	if (sd->sd_freeme) {
161 	    free(sd);
162 	}
163     }
164     if ((sd = scn->s_rawdata)) {
165 	elf_assert(sd->sd_magic == DATA_MAGIC);
166 	elf_assert(sd->sd_scn == scn);
167 	if (sd->sd_free_data && sd->sd_memdata) {
168 	    free(sd->sd_memdata);
169 	}
170 	if (sd->sd_freeme) {
171 	    free(sd);
172 	}
173     }
174     if (scn->s_freeme) {
175 	elf_assert(scn->s_index > 0);
176 	free(scn);
177     }
178     /*
179      * Adjust section indices.
180      */
181     for (scn = pscn->s_link; scn; scn = scn->s_link) {
182 	elf_assert(scn->s_index > index);
183 	scn->s_index--;
184     }
185     /*
186      * Adjust ELF header and well-known section headers.
187      */
188     if (elf->e_class == ELFCLASS32) {
189 	_elf32_update_shdr(elf, index);
190 	return index;
191     }
192 #if __LIBELF64
193     else if (elf->e_class == ELFCLASS64) {
194 	_elf64_update_shdr(elf, index);
195 	return index;
196     }
197 #endif /* __LIBELF64 */
198     else if (valid_class(elf->e_class)) {
199 	seterr(ERROR_UNIMPLEMENTED);
200     }
201     else {
202 	seterr(ERROR_UNKNOWN_CLASS);
203     }
204     return SHN_UNDEF;
205 }
206