1 /* Test program for adding (more than SHN_LORESERVE) sections.
2 Copyright (C) 2018 Red Hat, Inc.
3 This file is part of elfutils.
4
5 This file 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; either version 3 of the License, or
8 (at your option) any later version.
9
10 elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <inttypes.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33
34 #include ELFUTILS_HEADER(elf)
35 #include <gelf.h>
36
37
38 /* shstrndx is special, might overflow into section zero header sh_link. */
39 static int
setshstrndx(Elf * elf,size_t ndx)40 setshstrndx (Elf *elf, size_t ndx)
41 {
42 printf ("setshstrndx: %zd\n", ndx);
43
44 GElf_Ehdr ehdr_mem;
45 GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
46 if (ehdr == NULL)
47 return -1;
48
49 if (ndx < SHN_LORESERVE)
50 ehdr->e_shstrndx = ndx;
51 else
52 {
53 ehdr->e_shstrndx = SHN_XINDEX;
54 Elf_Scn *zscn = elf_getscn (elf, 0);
55 GElf_Shdr zshdr_mem;
56 GElf_Shdr *zshdr = gelf_getshdr (zscn, &zshdr_mem);
57 if (zshdr == NULL)
58 return -1;
59 zshdr->sh_link = ndx;
60 if (gelf_update_shdr (zscn, zshdr) == 0)
61 return -1;
62 }
63
64 if (gelf_update_ehdr (elf, ehdr) == 0)
65 return -1;
66
67 return 0;
68 }
69
70 /* Will add nr new '.extra' sections and a new '.new_shstrtab' section
71 at the end. */
72 static void
add_sections(const char * name,size_t nr,int use_mmap)73 add_sections (const char *name, size_t nr, int use_mmap)
74 {
75 printf ("add_sections '%s': %zd\n", name, nr);
76
77 int fd = open (name, O_RDWR);
78 if (fd < 0)
79 {
80 fprintf (stderr, "Couldn't open file '%s': %s\n",
81 name, strerror (errno));
82 exit (1);
83 }
84
85 Elf *elf = elf_begin (fd, use_mmap ? ELF_C_RDWR_MMAP : ELF_C_RDWR, NULL);
86 if (elf == NULL)
87 {
88 fprintf (stderr, "Couldn't open ELF file '%s': %s\n",
89 name, elf_errmsg (-1));
90 exit (1);
91 }
92
93 /* We will add a new shstrtab section with two new names at the end.
94 Just get the current shstrtab table and add two entries '.extra'
95 and '.old_shstrtab' at the end of the table, so all existing indexes
96 are still valid. */
97 size_t shstrndx;
98 if (elf_getshdrstrndx (elf, &shstrndx) < 0)
99 {
100 printf ("cannot get shstrndx: %s\n", elf_errmsg (-1));
101 exit (1);
102 }
103
104 Elf_Scn *shstrtab_scn = elf_getscn (elf, shstrndx);
105 if (shstrtab_scn == NULL)
106 {
107 printf ("couldn't get shstrtab scn: %s\n", elf_errmsg (-1));
108 exit (1);
109 }
110 Elf_Data *shstrtab_data = elf_getdata (shstrtab_scn, NULL);
111 if (shstrtab_data == NULL)
112 {
113 printf ("couldn't get shstrtab data: %s\n", elf_errmsg (-1));
114 exit (1);
115 }
116 size_t new_shstrtab_size = (shstrtab_data->d_size
117 + strlen (".extra") + 1
118 + strlen (".old_shstrtab") + 1);
119 void *new_shstrtab_buf = malloc (new_shstrtab_size);
120 if (new_shstrtab_buf == NULL)
121 {
122 printf ("couldn't allocate new shstrtab data d_buf\n");
123 exit (1);
124 }
125 memcpy (new_shstrtab_buf, shstrtab_data->d_buf, shstrtab_data->d_size);
126 size_t extra_idx = shstrtab_data->d_size;
127 size_t old_shstrtab_idx = extra_idx + strlen (".extra") + 1;
128 strcpy (new_shstrtab_buf + extra_idx, ".extra");
129 strcpy (new_shstrtab_buf + old_shstrtab_idx, ".old_shstrtab");
130
131 /* Change the name of the old shstrtab section, because elflint
132 has a strict check on the name/type for .shstrtab. */
133 GElf_Shdr shdr_mem;
134 GElf_Shdr *shdr = gelf_getshdr (shstrtab_scn, &shdr_mem);
135 if (shdr == NULL)
136 {
137 printf ("cannot get header for old shstrtab section: %s\n",
138 elf_errmsg (-1));
139 exit (1);
140 }
141
142 size_t shstrtab_idx = shdr->sh_name;
143 shdr->sh_name = old_shstrtab_idx;
144
145 if (gelf_update_shdr (shstrtab_scn, shdr) == 0)
146 {
147 printf ("cannot update old shstrtab section header: %s\n",
148 elf_errmsg (-1));
149 exit (1);
150 }
151
152 // Add lots of .extra sections...
153 size_t cnt = 0;
154 while (cnt++ < nr)
155 {
156 Elf_Scn *scn = elf_newscn (elf);
157 if (scn == NULL)
158 {
159 printf ("cannot create .extra section (%zd): %s\n", cnt,
160 elf_errmsg (-1));
161 exit (1);
162 }
163
164 Elf_Data *data = elf_newdata (scn);
165 if (data == NULL)
166 {
167 printf ("couldn't create new section data (%zd): %s\n", cnt,
168 elf_errmsg (-1));
169 exit (1);
170 }
171
172 data->d_size = strlen ("extra") + 1;
173 data->d_buf = "extra";
174 data->d_type = ELF_T_BYTE;
175 data->d_align = 1;
176
177 shdr = gelf_getshdr (scn, &shdr_mem);
178 if (shdr == NULL)
179 {
180 printf ("cannot get header for new section (%zd): %s\n", cnt,
181 elf_errmsg (-1));
182 exit (1);
183 }
184
185 shdr->sh_type = SHT_PROGBITS;
186 shdr->sh_flags = 0;
187 shdr->sh_addr = 0;
188 shdr->sh_link = SHN_UNDEF;
189 shdr->sh_info = SHN_UNDEF;
190 shdr->sh_addralign = 1;
191 shdr->sh_entsize = 0;
192 shdr->sh_size = data->d_size;
193 shdr->sh_name = extra_idx;
194
195 if (gelf_update_shdr (scn, shdr) == 0)
196 {
197 printf ("cannot update new section header (%zd): %s\n", cnt,
198 elf_errmsg (-1));
199 exit (1);
200 }
201 }
202
203 // Create new shstrtab section.
204 Elf_Scn *new_shstrtab_scn = elf_newscn (elf);
205 if (new_shstrtab_scn == NULL)
206 {
207 printf ("cannot create new shstrtab section: %s\n", elf_errmsg (-1));
208 exit (1);
209 }
210
211 Elf_Data *new_shstrtab_data = elf_newdata (new_shstrtab_scn);
212 if (new_shstrtab_data == NULL)
213 {
214 printf ("couldn't create new shstrtab section data: %s\n",
215 elf_errmsg (-1));
216 exit (1);
217 }
218
219 new_shstrtab_data->d_size = new_shstrtab_size;
220 new_shstrtab_data->d_buf = new_shstrtab_buf;
221 new_shstrtab_data->d_type = ELF_T_BYTE;
222 new_shstrtab_data->d_align = 1;
223
224 shdr = gelf_getshdr (new_shstrtab_scn, &shdr_mem);
225 if (shdr == NULL)
226 {
227 printf ("cannot get header for new shstrtab section: %s\n",
228 elf_errmsg (-1));
229 exit (1);
230 }
231
232 shdr->sh_type = SHT_STRTAB;
233 shdr->sh_flags = 0;
234 shdr->sh_addr = 0;
235 shdr->sh_link = SHN_UNDEF;
236 shdr->sh_info = SHN_UNDEF;
237 shdr->sh_addralign = 1;
238 shdr->sh_entsize = 0;
239 shdr->sh_size = new_shstrtab_size;
240 shdr->sh_name = shstrtab_idx;
241
242 // Finished new shstrtab section, update the header.
243 if (gelf_update_shdr (new_shstrtab_scn, shdr) == 0)
244 {
245 printf ("cannot update new shstrtab section header: %s\n",
246 elf_errmsg (-1));
247 exit (1);
248 }
249
250 // Set it as the new shstrtab section to get the names correct.
251 size_t new_shstrndx = elf_ndxscn (new_shstrtab_scn);
252 if (setshstrndx (elf, new_shstrndx) < 0)
253 {
254 printf ("cannot set shstrndx: %s\n", elf_errmsg (-1));
255 exit (1);
256 }
257
258 // Write everything to disk.
259 if (elf_update (elf, ELF_C_WRITE) < 0)
260 {
261 printf ("failure in elf_update: %s\n", elf_errmsg (-1));
262 exit (1);
263 }
264
265 if (elf_end (elf) != 0)
266 {
267 printf ("couldn't cleanup elf '%s': %s\n", name, elf_errmsg (-1));
268 exit (1);
269 }
270
271 if (close (fd) != 0)
272 {
273 printf ("couldn't close '%s': %s\n", name, strerror (errno));
274 exit (1);
275 }
276
277 free (new_shstrtab_buf);
278 }
279
280 int
main(int argc,char * argv[])281 main (int argc, char *argv[])
282 {
283 elf_version (EV_CURRENT);
284
285 /* Takes the given file, and adds the given number of sections. */
286 if (argc < 3 || argc > 4)
287 {
288 fprintf (stderr, "addsections [--mmap] nr elf.file\n");
289 exit (1);
290 }
291
292 int argn = 1;
293 bool use_mmap = false;
294 if (strcmp (argv[argn], "--mmap") == 0)
295 {
296 use_mmap = true;
297 argn++;
298 }
299
300 size_t nr = atoi (argv[argn++]);
301 const char *file = argv[argn];
302 add_sections (file, nr, use_mmap);
303
304 return 0;
305 }
306