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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* acom: Append Comment
*
* This program demonstrates the use of the libelf interface to
* modify a ELF file. This program will open an ELF file and
* either modify an existing .comment section and/or append
* a new .comment section to an existing ELF file.
*/
#include <stdio.h>
#include <libelf.h>
#include <gelf.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
static const char *CommentStr = ".comment";
static void
update_comment(Elf *elf, const char *file, const char *comment)
{
Elf_Scn *scn = NULL;
GElf_Shdr shdr;
Elf_Data *data;
size_t shstrndx;
if (elf_getshdrstrndx(elf, &shstrndx) == -1) {
(void) fprintf(stderr, "%s: gelf_getshdrstrdx() failed: %s\n",
file, elf_errmsg(0));
return;
}
while ((scn = elf_nextscn(elf, scn)) != NULL) {
/*
* Do a string compare to examine each section header
* to see if it is a ".comment" section. If it is then
* this is the section we want to process.
*/
if (gelf_getshdr(scn, &shdr) == NULL) {
(void) fprintf(stderr, "%s: elf_getshdr() failed: %s\n",
file, elf_errmsg(0));
return;
}
if (strcmp(CommentStr, elf_strptr(elf, shstrndx,
shdr.sh_name)) == 0)
break;
}
if (scn == NULL) {
int ndx;
(void) printf("%s has no .comment section. "
"Creating one...\n", file);
/*
* First add the ".comment" string to the string table
*/
if ((scn = elf_getscn(elf, shstrndx)) == NULL) {
(void) fprintf(stderr, "%s: elf_getscn() failed: %s\n",
file, elf_errmsg(0));
return;
}
if ((data = elf_getdata(scn, NULL)) == NULL) {
(void) fprintf(stderr, "%s: elf_getdata() failed: %s\n",
file, elf_errmsg(0));
return;
}
ndx = data->d_off + data->d_size;
if ((data = elf_newdata(scn)) == NULL) {
(void) fprintf(stderr, "%s: elf_newdata() failed: %s\n",
file, elf_errmsg(0));
return;
}
data->d_buf = (void *)CommentStr;
data->d_size = strlen(CommentStr) + 1;
data->d_align = 1;
/*
* Add the ".comment" section to the end of the file.
* Initialize the fields in the Section Header that
* libelf will not fill in.
*/
if ((scn = elf_newscn(elf)) == NULL) {
(void) fprintf(stderr, "%s: elf_newscn() failed: %s\n",
file, elf_errmsg(0));
return;
}
if (gelf_getshdr(scn, &shdr) == NULL) {
(void) fprintf(stderr, "%s: elf_getshdr() failed: %s\n",
file, elf_errmsg(0));
return;
}
shdr.sh_name = ndx;
shdr.sh_type = SHT_PROGBITS;
shdr.sh_flags = 0;
shdr.sh_addr = 0;
shdr.sh_link = 0;
shdr.sh_info = 0;
/*
* Flush the changes to the underlying elf32 or elf64
* section header.
*/
(void) gelf_update_shdr(scn, &shdr);
}
if (shdr.sh_addr != 0) {
(void) printf("%s: .comment section is part of a "
"loadable segment, it cannot be changed.\n", file);
return;
}
if ((data = elf_newdata(scn)) == NULL) {
(void) fprintf(stderr, "%s: elf_getdata() failed: %s\n",
file, elf_errmsg(0));
return;
}
data->d_buf = (void *)comment;
data->d_size = strlen(comment) + 1;
data->d_align = 1;
if (elf_update(elf, ELF_C_WRITE) == -1)
(void) fprintf(stderr, "%s: elf_update() failed: %s\n", file,
elf_errmsg(0));
}
int
main(int argc, char **argv)
{
int i;
char *new_comment;
if (argc < 3) {
(void) printf("usage: %s <new comment> elf_file ...\n",
argv[0]);
return (1);
}
/*
* Initialize the elf library, must be called before elf_begin()
* can be called.
*/
if (elf_version(EV_CURRENT) == EV_NONE) {
(void) fprintf(stderr, "elf_version() failed: %s\n",
elf_errmsg(0));
return (1);
}
/*
* The new comment is passed in through the command line.
* This string will be used to update the .comment section of
* the specified ELF files.
*/
new_comment = argv[1];
for (i = 2; i < argc; i++) {
int fd;
Elf *elf;
char *elf_fname;
elf_fname = argv[i];
if ((fd = open(elf_fname, O_RDWR)) == -1) {
perror("open");
continue;
}
/*
* Attempt to open an Elf descriptor Read/Write
* for each file.
*/
if ((elf = elf_begin(fd, ELF_C_RDWR, 0)) == NULL) {
(void) fprintf(stderr, "elf_begin() failed: %s\n",
elf_errmsg(0));
(void) close(fd);
continue;
}
/*
* Determine what kind of elf file this is:
*/
if (elf_kind(elf) == ELF_K_ELF)
update_comment(elf, elf_fname, new_comment);
else
(void) printf("%s not of type ELF_K_ELF. "
"elf_kind == %d\n", elf_fname, elf_kind(elf));
(void) elf_end(elf);
(void) close(fd);
}
return (0);
}
|