-
Notifications
You must be signed in to change notification settings - Fork 4
/
scncopy.c
125 lines (110 loc) · 2.71 KB
/
scncopy.c
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
/*
* SPDX-License-Identifier: GPL-2.0-only
*
* Copyright 2009 Red Hat, Inc.
*
* Author: Peter Jones <pjones@redhat.com>
*/
#include <gelf.h>
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "elfcreator.h"
#include "dutil.h"
static int should_copy_scn(Elf *elf, GElf_Shdr *shdr, struct strlist *scns)
{
char *name;
size_t shstrndx;
if (elf_getshdrstrndx(elf, &shstrndx) < 0)
return 0;
name = elf_strptr(elf, shstrndx, shdr->sh_name);
if (name == NULL)
return 0;
if (strlist__has_entry(scns, name))
return 1;
return 0;
}
int main(int argc, char *argv[])
{
int n;
struct strlist *sections;
char *infile = NULL, *outfile = NULL;
int fd;
Elf *elf;
Elf_Scn *scn;
int copy_all_sections = 0;
ElfCreator *ctor;
sections = strlist__new(false);
for (n = 1; n < argc; n++) {
if (!strcmp(argv[n], "-a")) {
copy_all_sections = 1;
} else if (!strcmp(argv[n], "-s")) {
if (n == argc-1) {
fprintf(stderr, "Missing argument to -s\n");
return -1;
}
n++;
strlist__add(sections, argv[n]);
continue;
} else if (!strcmp(argv[n], "-o")) {
if (n == argc-1) {
fprintf(stderr, "Missing argument to -o\n");
return -1;
}
n++;
outfile = argv[n];
continue;
} else if (!strcmp(argv[n], "-?") ||
!strcmp(argv[n], "--help") ||
!strcmp(argv[n], "--usage")) {
printf("usage: scncopy [-s section0 [[-s section1] ... -s sectionN] | -a ] -o outfile infile\n");
return 0;
} else if (n == argc-1) {
infile = argv[n];
} else {
fprintf(stderr, "usage: pjoc -s section 0 [[-s section1] ... -s sectionN] -o outfile infile\n");
return 1;
}
}
if (!infile || !outfile) {
fprintf(stderr, "usage: pjoc -s section 0 [[-s section1] ... -s sectionN] -o outfile infile\n");
return 1;
}
if (!(fd = open(infile, O_RDONLY))) {
fprintf(stderr, "Could not open \"%s\" for reading: %m\n", infile);
return 1;
}
elf_version(EV_CURRENT);
if ((elf = elf_begin(fd, ELF_C_READ_MMAP_PRIVATE, NULL)) == NULL) {
fprintf(stderr, "cannot get elf descriptor for \"%s\": %s\n",
infile, elf_errmsg(-1));
close(fd);
return 1;
}
if (elf_kind(elf) != ELF_K_ELF) {
fprintf(stderr, "\"%s\" is not an ELF file\n", infile);
err:
elf_end(elf);
close(fd);
return 1;
}
if ((ctor = elfcreator_begin(outfile, elf)) == NULL) {
fprintf(stderr, "could not initialize ELF creator\n");
goto err;
}
scn = NULL;
while ((scn = elf_nextscn(elf, scn)) != NULL) {
GElf_Shdr shdr_mem, *shdr;
shdr = gelf_getshdr(scn, &shdr_mem);
if (shdr == NULL)
continue;
if (!should_copy_scn(elf, shdr, sections) && !copy_all_sections)
continue;
elfcreator_copy_scn(ctor, elf, scn);
}
elfcreator_end(ctor);
return 0;
}