1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
/* Test program for copying a whole ELF file using libelf.
Copyright (C) 2018 Red Hat, Inc.
This file is part of elfutils.
This file is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
elfutils is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include ELFUTILS_HEADER(elf)
#include <gelf.h>
/* shstrndx is special, might overflow into section zero header sh_link. */
static int
setshstrndx (Elf *elf, size_t ndx)
{
printf ("setshstrndx: %zd\n", ndx);
GElf_Ehdr ehdr_mem;
GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
if (ehdr == NULL)
return -1;
if (ndx < SHN_LORESERVE)
ehdr->e_shstrndx = ndx;
else
{
ehdr->e_shstrndx = SHN_XINDEX;
Elf_Scn *zscn = elf_getscn (elf, 0);
GElf_Shdr zshdr_mem;
GElf_Shdr *zshdr = gelf_getshdr (zscn, &zshdr_mem);
if (zshdr == NULL)
return -1;
zshdr->sh_link = ndx;
if (gelf_update_shdr (zscn, zshdr) == 0)
return -1;
}
if (gelf_update_ehdr (elf, ehdr) == 0)
return -1;
return 0;
}
/* Copies all elements of an ELF file either using mmap or read. */
static void
copy_elf (const char *in, const char *out, bool use_mmap, bool reverse_offs)
{
printf ("\ncopy_elf: %s -> %s (%s,%s)\n", in, out,
use_mmap ? "mmap" : "read",
reverse_offs ? "reverse" : "same");
/* Existing ELF file. */
int fda = open (in, O_RDONLY);
if (fda < 0)
{
fprintf (stderr, "Couldn't open file '%s': %s\n",
in, strerror (errno));
exit (1);
}
Elf *elfa = elf_begin (fda, use_mmap ? ELF_C_READ_MMAP : ELF_C_READ, NULL);
if (elfa == NULL)
{
fprintf (stderr, "Couldn't open ELF file '%s': %s\n",
in, elf_errmsg (-1));
exit (1);
}
/* Open new file. */
int fdb = open (out, O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fdb < 0)
{
fprintf (stderr, "Couldn't create file '%s': %s\n",
out, strerror (errno));
exit (1);
}
Elf *elfb = elf_begin (fdb, use_mmap ? ELF_C_WRITE_MMAP : ELF_C_WRITE, NULL);
if (elfb == NULL)
{
fprintf (stderr, "Couldn't create ELF file '%s': %s\n",
out, elf_errmsg (-1));
exit (1);
}
// Copy ELF header.
GElf_Ehdr ehdr_mema;
GElf_Ehdr *ehdra = gelf_getehdr (elfa, &ehdr_mema);
if (ehdra == NULL)
{
printf ("cannot get ELF header: %s\n", elf_errmsg (-1));
exit (1);
}
int class = gelf_getclass (elfa);
// Create an ELF header.
if (gelf_newehdr (elfb, class) == 0)
{
printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
exit (1);
}
/* New elf header is an exact copy. */
GElf_Ehdr ehdr_memb;
GElf_Ehdr *ehdrb = &ehdr_memb;
*ehdrb = *ehdra;
if (gelf_update_ehdr (elfb, ehdrb) == 0)
{
printf ("cannot update ELF header: %s\n", elf_errmsg (-1));
exit (1);
}
/* shstrndx is special. (Technically phdrnum and shdrnum are also
special, but they are handled by libelf.) */
size_t shstrndx;
if (elf_getshdrstrndx (elfa, &shstrndx) < 0)
{
printf ("cannot get shstrndx: %s\n", elf_errmsg (-1));
exit (1);
}
if (setshstrndx (elfb, shstrndx) < 0)
{
printf ("cannot set shstrndx: %s\n", elf_errmsg (-1));
exit (1);
}
/* If there are phdrs, copy them over. */
size_t phnum;
if (elf_getphdrnum (elfa, &phnum) != 0)
{
printf ("cannot get phdrs: %s\n", elf_errmsg (-1));
exit (1);
}
if (phnum > 0)
{
if (gelf_newphdr (elfb, phnum) == 0)
{
printf ("cannot create phdrs: %s\n", elf_errmsg (-1));
exit (1);
}
for (size_t cnt = 0; cnt < phnum; ++cnt)
{
GElf_Phdr phdr_mem;
GElf_Phdr *phdr = gelf_getphdr (elfa, cnt, &phdr_mem);
if (phdr == NULL)
{
printf ("couldn't get phdr %zd: %s\n", cnt, elf_errmsg (-1));
exit (1);
}
if (gelf_update_phdr (elfb, cnt, phdr) == 0)
{
printf ("couldn't update phdr %zd: %s\n", cnt, elf_errmsg (-1));
exit (1);
}
}
}
GElf_Off *offs = NULL;
size_t shnum;
if (reverse_offs)
{
if (elf_getshdrnum (elfa, &shnum) < 0)
{
printf ("couldn't get shdrnum: %s\n", elf_errmsg (-1));
exit (1);
}
offs = malloc (shnum * sizeof (GElf_Off));
if (offs == NULL)
{
printf ("couldn't allocate memory for offs\n");
exit (1);
}
}
/* Copy all sections, headers and data. */
Elf_Scn *scn = NULL;
size_t last_off = 0;
GElf_Shdr last_shdr = { .sh_type = SHT_NULL };
while ((scn = elf_nextscn (elfa, scn)) != NULL)
{
/* Get the header. */
GElf_Shdr shdr;
if (gelf_getshdr (scn, &shdr) == NULL)
{
printf ("couldn't get shdr: %s\n", elf_errmsg (-1));
exit (1);
}
if (reverse_offs)
{
offs[last_off] = shdr.sh_offset;
if (last_shdr.sh_type != SHT_NULL
&& last_shdr.sh_addralign == shdr.sh_addralign
&& shdr.sh_addralign == 1
&& last_shdr.sh_type != SHT_NOBITS
&& shdr.sh_type != SHT_NOBITS
&& last_shdr.sh_offset + last_shdr.sh_size == shdr.sh_offset
&& (phnum == 0
|| ((shdr.sh_flags & SHF_ALLOC) == 0
&& (last_shdr.sh_flags & SHF_ALLOC) == 0)))
{
printf ("Swapping offsets of section %zd and %zd\n",
last_off, last_off + 1);
GElf_Word off = offs[last_off - 1];
offs[last_off - 1] = off + shdr.sh_size;
offs[last_off] = off;
last_shdr.sh_type = SHT_NULL;
}
else
{
last_shdr = shdr;
offs[last_off] = shdr.sh_offset;
}
last_off++;
}
/* Create new section. */
Elf_Scn *new_scn = elf_newscn (elfb);
if (new_scn == NULL)
{
printf ("couldn't create new section: %s\n", elf_errmsg (-1));
exit (1);
}
if (gelf_update_shdr (new_scn, &shdr) == 0)
{
printf ("couldn't update shdr: %s\n", elf_errmsg (-1));
exit (1);
}
/* Copy over section data. */
Elf_Data *data = NULL;
while ((data = elf_getdata (scn, data)) != NULL)
{
Elf_Data *new_data = elf_newdata (new_scn);
if (new_data == NULL)
{
printf ("couldn't create new section data: %s\n",
elf_errmsg (-1));
exit (1);
}
*new_data = *data;
}
}
if (reverse_offs)
{
last_off = 0;
scn = NULL;
while ((scn = elf_nextscn (elfb, scn)) != NULL)
{
GElf_Shdr shdr;
if (gelf_getshdr (scn, &shdr) == NULL)
{
printf ("couldn't get shdr for updating: %s\n", elf_errmsg (-1));
exit (1);
}
shdr.sh_offset = offs[last_off++];
if (gelf_update_shdr (scn, &shdr) == 0)
{
printf ("couldn't update shdr sh_off: %s\n", elf_errmsg (-1));
exit (1);
}
}
free (offs);
}
/* Write everything to disk. If there are any phdrs, or we want to
update the offsets, then we want the exact same layout. Do we
want ELF_F_PERMISSIVE? */
if (phnum > 0 || reverse_offs)
elf_flagelf (elfb, ELF_C_SET, ELF_F_LAYOUT);
if (elf_update (elfb, ELF_C_WRITE) < 0)
{
printf ("failure in elf_update: %s\n", elf_errmsg (-1));
exit (1);
}
if (elf_end (elfa) != 0)
{
printf ("couldn't cleanup elf '%s': %s\n", in, elf_errmsg (-1));
exit (1);
}
if (close (fda) != 0)
{
printf ("couldn't close '%s': %s\n", in, strerror (errno));
exit (1);
}
if (elf_end (elfb) != 0)
{
printf ("couldn't cleanup elf '%s': %s\n", out, elf_errmsg (-1));
exit (1);
}
if (close (fdb) != 0)
{
printf ("couldn't close '%s': %s\n", out, strerror (errno));
exit (1);
}
}
int
main (int argc, const char *argv[])
{
elf_version (EV_CURRENT);
/* Takes the given file, and create a new identical one. */
if (argc < 3 || argc > 5)
{
fprintf (stderr, "elfcopy [--mmap] [--reverse-offs] in.elf out.elf\n");
exit (1);
}
int argn = 1;
bool use_mmap = false;
if (strcmp (argv[argn], "--mmap") == 0)
{
use_mmap = true;
argn++;
}
bool reverse_offs = false;
if (strcmp (argv[argn], "--reverse-offs") == 0)
{
reverse_offs = true;
argn++;
}
const char *in = argv[argn++];
const char *out = argv[argn];
copy_elf (in, out, use_mmap, reverse_offs);
return 0;
}
|