Print this page
11657 Remove old ctf tools
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/ctfmerge/ctfmerge.c
+++ new/usr/src/cmd/ctfmerge/ctfmerge.c
1 1 /*
2 2 * This file and its contents are supplied under the terms of the
3 3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 4 * You may only use this file in accordance with the terms of version
5 5 * 1.0 of the CDDL.
6 6 *
7 7 * A full copy of the text of the CDDL should have accompanied this
8 8 * source. A copy of the CDDL is also available via the Internet at
9 9 * http://www.illumos.org/license/CDDL.
10 10 */
11 11
12 12 /*
13 13 * Copyright 2019 Joyent, Inc.
14 14 */
15 15
16 16 /*
17 17 * merge CTF containers
18 18 */
19 19
20 20 #include <stdio.h>
21 21 #include <libctf.h>
22 22 #include <sys/stat.h>
23 23 #include <sys/types.h>
24 24 #include <fcntl.h>
25 25 #include <errno.h>
26 26 #include <strings.h>
27 27 #include <assert.h>
28 28 #include <unistd.h>
29 29 #include <sys/fcntl.h>
30 30 #include <stdlib.h>
31 31 #include <libelf.h>
32 32 #include <gelf.h>
33 33 #include <sys/mman.h>
34 34 #include <libgen.h>
35 35 #include <stdarg.h>
36 36 #include <limits.h>
37 37
↓ open down ↓ |
37 lines elided |
↑ open up ↑ |
38 38 static char *g_progname;
39 39 static char *g_unique;
40 40 static char *g_outfile;
41 41 static uint_t g_nctf;
42 42
43 43 #define CTFMERGE_OK 0
44 44 #define CTFMERGE_FATAL 1
45 45 #define CTFMERGE_USAGE 2
46 46
47 47 #define CTFMERGE_DEFAULT_NTHREADS 8
48 -#define CTFMERGE_ALTEXEC "CTFMERGE_ALTEXEC"
49 48
50 49 static void __attribute__((__noreturn__))
51 50 ctfmerge_fatal(const char *fmt, ...)
52 51 {
53 52 va_list ap;
54 53
55 54 (void) fprintf(stderr, "%s: ", g_progname);
56 55 va_start(ap, fmt);
57 56 (void) vfprintf(stderr, fmt, ap);
58 57 va_end(ap);
59 58
60 59 if (g_outfile != NULL)
61 60 (void) unlink(g_outfile);
62 61
63 62 exit(CTFMERGE_FATAL);
64 63 }
65 64
66 65 /*
67 66 * We failed to find CTF for this file, check if it's OK. If we're not derived
68 67 * from C, or we have the -m option, we let missing CTF pass.
69 68 */
70 69 static void
71 70 ctfmerge_check_for_c(const char *name, Elf *elf, uint_t flags)
72 71 {
73 72 char errmsg[1024];
74 73
75 74 if (flags & CTF_ALLOW_MISSING_DEBUG)
76 75 return;
77 76
78 77 switch (ctf_has_c_source(elf, errmsg, sizeof (errmsg))) {
79 78 case CHR_ERROR:
80 79 ctfmerge_fatal("failed to open %s: %s\n", name, errmsg);
81 80 break;
82 81
83 82 case CHR_NO_C_SOURCE:
84 83 return;
85 84
86 85 default:
87 86 ctfmerge_fatal("failed to open %s: %s\n", name,
88 87 ctf_errmsg(ECTF_NOCTFDATA));
89 88 break;
90 89 }
91 90 }
92 91
93 92 /*
94 93 * Go through and construct enough information for this Elf Object to try and do
95 94 * a ctf_bufopen().
96 95 */
97 96 static int
98 97 ctfmerge_elfopen(const char *name, Elf *elf, ctf_merge_t *cmh, uint_t flags)
99 98 {
100 99 GElf_Ehdr ehdr;
101 100 GElf_Shdr shdr;
102 101 Elf_Scn *scn;
103 102 Elf_Data *ctf_data, *str_data, *sym_data;
104 103 ctf_sect_t ctfsect, symsect, strsect;
105 104 ctf_file_t *fp;
106 105 int err;
107 106
108 107 if (gelf_getehdr(elf, &ehdr) == NULL)
109 108 ctfmerge_fatal("failed to get ELF header for %s: %s\n",
110 109 name, elf_errmsg(elf_errno()));
111 110
112 111 bzero(&ctfsect, sizeof (ctf_sect_t));
113 112 bzero(&symsect, sizeof (ctf_sect_t));
114 113 bzero(&strsect, sizeof (ctf_sect_t));
115 114
116 115 scn = NULL;
117 116 while ((scn = elf_nextscn(elf, scn)) != NULL) {
118 117 const char *sname;
119 118
120 119 if (gelf_getshdr(scn, &shdr) == NULL)
121 120 ctfmerge_fatal("failed to get section header for "
122 121 "file %s: %s\n", name, elf_errmsg(elf_errno()));
123 122
124 123 sname = elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name);
125 124 if (shdr.sh_type == SHT_PROGBITS &&
126 125 strcmp(sname, ".SUNW_ctf") == 0) {
127 126 ctfsect.cts_name = sname;
128 127 ctfsect.cts_type = shdr.sh_type;
129 128 ctfsect.cts_flags = shdr.sh_flags;
130 129 ctfsect.cts_size = shdr.sh_size;
131 130 ctfsect.cts_entsize = shdr.sh_entsize;
132 131 ctfsect.cts_offset = (off64_t)shdr.sh_offset;
133 132
134 133 ctf_data = elf_getdata(scn, NULL);
135 134 if (ctf_data == NULL)
136 135 ctfmerge_fatal("failed to get ELF CTF "
137 136 "data section for %s: %s\n", name,
138 137 elf_errmsg(elf_errno()));
139 138 ctfsect.cts_data = ctf_data->d_buf;
140 139 } else if (shdr.sh_type == SHT_SYMTAB) {
141 140 Elf_Scn *strscn;
142 141 GElf_Shdr strhdr;
143 142
144 143 symsect.cts_name = sname;
145 144 symsect.cts_type = shdr.sh_type;
146 145 symsect.cts_flags = shdr.sh_flags;
147 146 symsect.cts_size = shdr.sh_size;
148 147 symsect.cts_entsize = shdr.sh_entsize;
149 148 symsect.cts_offset = (off64_t)shdr.sh_offset;
150 149
151 150 if ((strscn = elf_getscn(elf, shdr.sh_link)) == NULL ||
152 151 gelf_getshdr(strscn, &strhdr) == NULL)
153 152 ctfmerge_fatal("failed to get "
154 153 "string table for file %s: %s\n", name,
155 154 elf_errmsg(elf_errno()));
156 155
157 156 strsect.cts_name = elf_strptr(elf, ehdr.e_shstrndx,
158 157 strhdr.sh_name);
159 158 strsect.cts_type = strhdr.sh_type;
160 159 strsect.cts_flags = strhdr.sh_flags;
161 160 strsect.cts_size = strhdr.sh_size;
162 161 strsect.cts_entsize = strhdr.sh_entsize;
163 162 strsect.cts_offset = (off64_t)strhdr.sh_offset;
164 163
165 164 sym_data = elf_getdata(scn, NULL);
166 165 if (sym_data == NULL)
167 166 ctfmerge_fatal("failed to get ELF CTF "
168 167 "data section for %s: %s\n", name,
169 168 elf_errmsg(elf_errno()));
170 169 symsect.cts_data = sym_data->d_buf;
171 170
172 171 str_data = elf_getdata(strscn, NULL);
173 172 if (str_data == NULL)
174 173 ctfmerge_fatal("failed to get ELF CTF "
175 174 "data section for %s: %s\n", name,
176 175 elf_errmsg(elf_errno()));
177 176 strsect.cts_data = str_data->d_buf;
178 177 }
179 178 }
180 179
181 180 if (ctfsect.cts_type == SHT_NULL) {
182 181 ctfmerge_check_for_c(name, elf, flags);
183 182 return (ENOENT);
184 183 }
185 184
186 185 if (symsect.cts_type != SHT_NULL && strsect.cts_type != SHT_NULL) {
187 186 fp = ctf_bufopen(&ctfsect, &symsect, &strsect, &err);
188 187 } else {
189 188 fp = ctf_bufopen(&ctfsect, NULL, NULL, &err);
190 189 }
191 190
192 191 if (fp == NULL) {
193 192 ctfmerge_fatal("failed to open file %s: %s\n",
194 193 name, ctf_errmsg(err));
195 194 }
196 195
197 196 if ((err = ctf_merge_add(cmh, fp)) != 0) {
198 197 ctfmerge_fatal("failed to add input %s: %s\n",
199 198 name, ctf_errmsg(err));
200 199 }
201 200
202 201 g_nctf++;
203 202 return (0);
204 203 }
205 204
206 205 static void
207 206 ctfmerge_read_archive(const char *name, int fd, Elf *elf,
208 207 ctf_merge_t *cmh, uint_t flags)
209 208 {
210 209 Elf_Cmd cmd = ELF_C_READ;
211 210 int cursec = 1;
212 211 Elf *aelf;
213 212
214 213 while ((aelf = elf_begin(fd, cmd, elf)) != NULL) {
215 214 char *nname = NULL;
216 215 Elf_Arhdr *arhdr;
217 216
218 217 if ((arhdr = elf_getarhdr(aelf)) == NULL)
219 218 ctfmerge_fatal("failed to get archive header %d for "
220 219 "%s: %s\n", cursec, name, elf_errmsg(elf_errno()));
221 220
222 221 cmd = elf_next(aelf);
223 222
224 223 if (*(arhdr->ar_name) == '/')
225 224 goto next;
226 225
227 226 if (asprintf(&nname, "%s.%s.%d", name, arhdr->ar_name,
228 227 cursec) < 0)
229 228 ctfmerge_fatal("failed to allocate memory for archive "
230 229 "%d of file %s\n", cursec, name);
231 230
232 231 switch (elf_kind(aelf)) {
233 232 case ELF_K_AR:
234 233 ctfmerge_read_archive(nname, fd, aelf, cmh, flags);
235 234 break;
236 235 case ELF_K_ELF:
237 236 /* ctfmerge_elfopen() takes ownership of aelf. */
238 237 if (ctfmerge_elfopen(nname, aelf, cmh, flags) == 0)
239 238 aelf = NULL;
240 239 break;
241 240 default:
242 241 ctfmerge_fatal("unknown elf kind (%d) in archive %d "
243 242 "for %s\n", elf_kind(aelf), cursec, name);
244 243 break;
245 244 }
246 245
247 246 next:
248 247 (void) elf_end(aelf);
249 248 free(nname);
250 249 cursec++;
251 250 }
252 251 }
253 252
254 253 static void
255 254 ctfmerge_file_add(ctf_merge_t *cmh, const char *file, uint_t flags)
256 255 {
257 256 Elf *e;
258 257 int fd;
259 258
260 259 if ((fd = open(file, O_RDONLY)) < 0) {
261 260 ctfmerge_fatal("failed to open file %s: %s\n",
262 261 file, strerror(errno));
263 262 }
264 263
265 264 if ((e = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
266 265 (void) close(fd);
267 266 ctfmerge_fatal("failed to open %s: %s\n",
268 267 file, elf_errmsg(elf_errno()));
269 268 }
270 269
271 270 switch (elf_kind(e)) {
272 271 case ELF_K_AR:
273 272 ctfmerge_read_archive(file, fd, e, cmh, flags);
274 273 break;
275 274
276 275 case ELF_K_ELF:
277 276 /* ctfmerge_elfopen() takes ownership of e. */
278 277 if (ctfmerge_elfopen(file, e, cmh, flags) == 0)
279 278 e = NULL;
280 279 break;
281 280
282 281 default:
283 282 ctfmerge_fatal("unknown elf kind (%d) for %s\n",
284 283 elf_kind(e), file);
285 284 }
286 285
287 286 (void) elf_end(e);
288 287 (void) close(fd);
289 288 }
290 289
291 290 static void
292 291 ctfmerge_usage(const char *fmt, ...)
293 292 {
294 293 if (fmt != NULL) {
295 294 va_list ap;
296 295
297 296 (void) fprintf(stderr, "%s: ", g_progname);
298 297 va_start(ap, fmt);
299 298 (void) vfprintf(stderr, fmt, ap);
300 299 va_end(ap);
301 300 }
302 301
303 302 (void) fprintf(stderr, "Usage: %s [-m] [-d uniqfile] [-l label] "
304 303 "[-L labelenv] [-j nthrs] -o outfile file ...\n"
↓ open down ↓ |
246 lines elided |
↑ open up ↑ |
305 304 "\n"
306 305 "\t-d uniquify merged output against uniqfile\n"
307 306 "\t-j use nthrs threads to perform the merge\n"
308 307 "\t-l set output container's label to specified value\n"
309 308 "\t-L set output container's label to value from environment\n"
310 309 "\t-m allow C-based input files to not have CTF\n"
311 310 "\t-o file to add CTF data to\n",
312 311 g_progname);
313 312 }
314 313
315 -static void
316 -ctfmerge_altexec(char **argv)
317 -{
318 - const char *alt;
319 - char *altexec;
320 -
321 - alt = getenv(CTFMERGE_ALTEXEC);
322 - if (alt == NULL || *alt == '\0')
323 - return;
324 -
325 - altexec = strdup(alt);
326 - if (altexec == NULL)
327 - ctfmerge_fatal("failed to allocate memory for altexec\n");
328 - if (unsetenv(CTFMERGE_ALTEXEC) != 0)
329 - ctfmerge_fatal("failed to unset %s from environment: %s\n",
330 - CTFMERGE_ALTEXEC, strerror(errno));
331 -
332 - (void) execv(altexec, argv);
333 - ctfmerge_fatal("failed to execute alternate program %s: %s",
334 - altexec, strerror(errno));
335 -}
336 -
337 314 int
338 315 main(int argc, char *argv[])
339 316 {
340 317 int err, i, c, ofd;
341 318 uint_t nthreads = CTFMERGE_DEFAULT_NTHREADS;
342 319 char *tmpfile = NULL, *label = NULL;
343 320 int wflags = CTF_ELFWRITE_F_COMPRESS;
344 321 uint_t flags = 0;
345 322 ctf_merge_t *cmh;
346 323 ctf_file_t *ofp;
347 324 long argj;
348 325 char *eptr;
349 326
350 327 g_progname = basename(argv[0]);
351 328
352 - ctfmerge_altexec(argv);
353 -
354 329 /*
355 330 * We support a subset of the old CTF merge flags, mostly for
356 331 * compatibility.
357 332 */
358 333 while ((c = getopt(argc, argv, ":d:fgj:l:L:mo:t")) != -1) {
359 334 switch (c) {
360 335 case 'd':
361 336 g_unique = optarg;
362 337 break;
363 338 case 'f':
364 339 /* Silently ignored for compatibility */
365 340 break;
366 341 case 'g':
367 342 /* Silently ignored for compatibility */
368 343 break;
369 344 case 'j':
370 345 errno = 0;
371 346 argj = strtol(optarg, &eptr, 10);
372 347 if (errno != 0 || argj == LONG_MAX ||
373 348 argj > 1024 || *eptr != '\0') {
374 349 ctfmerge_fatal("invalid argument for -j: %s\n",
375 350 optarg);
376 351 }
377 352 nthreads = (uint_t)argj;
378 353 break;
379 354 case 'l':
380 355 label = optarg;
381 356 break;
382 357 case 'L':
383 358 label = getenv(optarg);
384 359 break;
385 360 case 'm':
386 361 flags |= CTF_ALLOW_MISSING_DEBUG;
387 362 break;
388 363 case 'o':
389 364 g_outfile = optarg;
390 365 break;
391 366 case 't':
392 367 /* Silently ignored for compatibility */
393 368 break;
394 369 case ':':
395 370 ctfmerge_usage("Option -%c requires an operand\n",
396 371 optopt);
397 372 return (CTFMERGE_USAGE);
398 373 case '?':
399 374 ctfmerge_usage("Unknown option: -%c\n", optopt);
400 375 return (CTFMERGE_USAGE);
401 376 }
402 377 }
403 378
404 379 if (g_outfile == NULL) {
405 380 ctfmerge_usage("missing required -o output file\n");
406 381 return (CTFMERGE_USAGE);
407 382 }
408 383
409 384 (void) elf_version(EV_CURRENT);
410 385
411 386 /*
412 387 * Obviously this isn't atomic, but at least gives us a good starting
413 388 * point.
414 389 */
415 390 if ((ofd = open(g_outfile, O_RDWR)) < 0)
416 391 ctfmerge_fatal("cannot open output file %s: %s\n", g_outfile,
417 392 strerror(errno));
418 393
419 394 argc -= optind;
420 395 argv += optind;
421 396
422 397 if (argc < 1) {
423 398 ctfmerge_usage("no input files specified");
424 399 return (CTFMERGE_USAGE);
425 400 }
426 401
427 402 cmh = ctf_merge_init(ofd, &err);
428 403 if (cmh == NULL)
429 404 ctfmerge_fatal("failed to create merge handle: %s\n",
430 405 ctf_errmsg(err));
431 406
432 407 if ((err = ctf_merge_set_nthreads(cmh, nthreads)) != 0)
433 408 ctfmerge_fatal("failed to set parallelism to %u: %s\n",
434 409 nthreads, ctf_errmsg(err));
435 410
436 411 for (i = 0; i < argc; i++) {
437 412 ctfmerge_file_add(cmh, argv[i], flags);
438 413 }
439 414
440 415 if (g_nctf == 0) {
441 416 ctf_merge_fini(cmh);
442 417 return (0);
443 418 }
444 419
445 420 if (g_unique != NULL) {
446 421 ctf_file_t *ufp;
447 422 char *base;
448 423
449 424 ufp = ctf_open(g_unique, &err);
450 425 if (ufp == NULL) {
451 426 ctfmerge_fatal("failed to open uniquify file %s: %s\n",
452 427 g_unique, ctf_errmsg(err));
453 428 }
454 429
455 430 base = basename(g_unique);
456 431 (void) ctf_merge_uniquify(cmh, ufp, base);
457 432 }
458 433
459 434 if (label != NULL) {
460 435 if ((err = ctf_merge_label(cmh, label)) != 0)
461 436 ctfmerge_fatal("failed to add label %s: %s\n", label,
462 437 ctf_errmsg(err));
463 438 }
464 439
465 440 err = ctf_merge_merge(cmh, &ofp);
466 441 if (err != 0)
467 442 ctfmerge_fatal("failed to merge types: %s\n", ctf_errmsg(err));
468 443 ctf_merge_fini(cmh);
469 444
470 445 if (asprintf(&tmpfile, "%s.ctf", g_outfile) == -1)
471 446 ctfmerge_fatal("ran out of memory for temporary file name\n");
472 447 err = ctf_elfwrite(ofp, g_outfile, tmpfile, wflags);
473 448 if (err == CTF_ERR) {
474 449 (void) unlink(tmpfile);
475 450 free(tmpfile);
476 451 ctfmerge_fatal("encountered a libctf error: %s!\n",
477 452 ctf_errmsg(ctf_errno(ofp)));
478 453 }
479 454
480 455 if (rename(tmpfile, g_outfile) != 0) {
481 456 (void) unlink(tmpfile);
482 457 free(tmpfile);
483 458 ctfmerge_fatal("failed to rename temporary file: %s\n",
484 459 strerror(errno));
485 460 }
486 461 free(tmpfile);
487 462
488 463 return (CTFMERGE_OK);
489 464 }
↓ open down ↓ |
126 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX