xref: /third_party/elfutils/tests/addsections.c (revision da0c48c4)
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.  */
39static int
40setshstrndx (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.  */
72static void
73add_sections (const char *name, size_t nr, int use_mmap, size_t sec_size)
74{
75  printf ("add_sections '%s': %zd (sec_size: %zd)\n", name, nr, sec_size);
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  void *buf;
153  size_t bufsz;
154  if (sec_size == 0)
155    {
156      buf = strdup ("extra");
157      bufsz = strlen ("extra") + 1;
158    }
159  else
160    {
161      buf = malloc (sec_size);
162      if (buf == NULL)
163	{
164	  printf ("cannot allocate buffer data of %zd bytes\n", sec_size);
165	  exit (1);
166	}
167      memset (buf, 0xAA, sec_size);
168      bufsz = sec_size;
169    }
170
171  // Add lots of .extra sections...
172  size_t cnt = 0;
173  while (cnt++ < nr)
174    {
175      Elf_Scn *scn = elf_newscn (elf);
176      if (scn == NULL)
177	{
178	  printf ("cannot create .extra section (%zd): %s\n", cnt,
179		  elf_errmsg (-1));
180	  exit (1);
181	}
182
183      Elf_Data *data = elf_newdata (scn);
184      if (data == NULL)
185	{
186	  printf ("couldn't create new section data (%zd): %s\n", cnt,
187		  elf_errmsg (-1));
188	  exit (1);
189	}
190
191      data->d_size = bufsz;
192      data->d_buf = buf;
193      data->d_type = ELF_T_BYTE;
194      data->d_align = 1;
195
196      shdr = gelf_getshdr (scn, &shdr_mem);
197      if (shdr == NULL)
198	{
199	  printf ("cannot get header for new section (%zd): %s\n", cnt,
200		  elf_errmsg (-1));
201	  exit (1);
202	}
203
204      shdr->sh_type = SHT_PROGBITS;
205      shdr->sh_flags = 0;
206      shdr->sh_addr = 0;
207      shdr->sh_link = SHN_UNDEF;
208      shdr->sh_info = SHN_UNDEF;
209      shdr->sh_addralign = 1;
210      shdr->sh_entsize = 0;
211      shdr->sh_size = data->d_size;
212      shdr->sh_name = extra_idx;
213
214      if (gelf_update_shdr (scn, shdr) == 0)
215	{
216	  printf ("cannot update new section header (%zd): %s\n", cnt,
217		  elf_errmsg (-1));
218	  exit (1);
219	}
220    }
221
222  // Create new shstrtab section.
223  Elf_Scn *new_shstrtab_scn = elf_newscn (elf);
224  if (new_shstrtab_scn == NULL)
225    {
226      printf ("cannot create new shstrtab section: %s\n", elf_errmsg (-1));
227      exit (1);
228    }
229
230  Elf_Data *new_shstrtab_data = elf_newdata (new_shstrtab_scn);
231  if (new_shstrtab_data == NULL)
232    {
233      printf ("couldn't create new shstrtab section data: %s\n",
234	      elf_errmsg (-1));
235      exit (1);
236    }
237
238  new_shstrtab_data->d_size = new_shstrtab_size;
239  new_shstrtab_data->d_buf = new_shstrtab_buf;
240  new_shstrtab_data->d_type = ELF_T_BYTE;
241  new_shstrtab_data->d_align = 1;
242
243  shdr = gelf_getshdr (new_shstrtab_scn, &shdr_mem);
244  if (shdr == NULL)
245    {
246      printf ("cannot get header for new shstrtab section: %s\n",
247	      elf_errmsg (-1));
248      exit (1);
249    }
250
251  shdr->sh_type = SHT_STRTAB;
252  shdr->sh_flags = 0;
253  shdr->sh_addr = 0;
254  shdr->sh_link = SHN_UNDEF;
255  shdr->sh_info = SHN_UNDEF;
256  shdr->sh_addralign = 1;
257  shdr->sh_entsize = 0;
258  shdr->sh_size = new_shstrtab_size;
259  shdr->sh_name = shstrtab_idx;
260
261  // Finished new shstrtab section, update the header.
262  if (gelf_update_shdr (new_shstrtab_scn, shdr) == 0)
263    {
264      printf ("cannot update new shstrtab section header: %s\n",
265	      elf_errmsg (-1));
266      exit (1);
267    }
268
269  // Set it as the new shstrtab section to get the names correct.
270  size_t new_shstrndx = elf_ndxscn (new_shstrtab_scn);
271  if (setshstrndx (elf, new_shstrndx) < 0)
272    {
273      printf ("cannot set shstrndx: %s\n", elf_errmsg (-1));
274      exit (1);
275    }
276
277  // Write everything to disk.
278  if (elf_update (elf, ELF_C_WRITE) < 0)
279    {
280      printf ("failure in elf_update: %s\n", elf_errmsg (-1));
281      exit (1);
282    }
283
284  if (elf_end (elf) != 0)
285    {
286      printf ("couldn't cleanup elf '%s': %s\n", name, elf_errmsg (-1));
287      exit (1);
288    }
289
290  if (close (fd) != 0)
291    {
292      printf ("couldn't close '%s': %s\n", name, strerror (errno));
293      exit (1);
294    }
295
296  free (buf);
297  free (new_shstrtab_buf);
298}
299
300int
301main (int argc, char *argv[])
302{
303  elf_version (EV_CURRENT);
304
305  /* Takes the given file, and adds the given number of sections.
306     Optionally using mmap and optionally using a given section size.  */
307  if (argc < 3 || argc > 5)
308    {
309      fprintf (stderr, "addsections [--mmap] nr elf.file [sec_size]\n");
310      exit (1);
311    }
312
313  int argn = 1;
314  bool use_mmap = false;
315  if (strcmp (argv[argn], "--mmap") == 0)
316    {
317      use_mmap = true;
318      argn++;
319    }
320
321  size_t nr = atoi (argv[argn++]);
322  const char *file = argv[argn++];
323
324  size_t sec_size = 0;
325  if (argn < argc)
326    sec_size = atol (argv[argn++]);
327
328  add_sections (file, nr, use_mmap, sec_size);
329
330  return 0;
331}
332