.. | .. |
---|
8 | 8 | #include <limits.h> |
---|
9 | 9 | #include <stdio.h> |
---|
10 | 10 | #include <stdlib.h> |
---|
| 11 | +#include <unistd.h> |
---|
11 | 12 | #include <linux/err.h> |
---|
| 13 | +#include <linux/string.h> |
---|
| 14 | +#include <linux/zalloc.h> |
---|
12 | 15 | #include "debug.h" |
---|
13 | 16 | #include "llvm-utils.h" |
---|
14 | 17 | #include "config.h" |
---|
.. | .. |
---|
19 | 22 | #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \ |
---|
20 | 23 | "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\ |
---|
21 | 24 | "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \ |
---|
22 | | - "$CLANG_OPTIONS $KERNEL_INC_OPTIONS $PERF_BPF_INC_OPTIONS " \ |
---|
| 25 | + "$CLANG_OPTIONS $PERF_BPF_INC_OPTIONS $KERNEL_INC_OPTIONS " \ |
---|
23 | 26 | "-Wno-unused-value -Wno-pointer-sign " \ |
---|
24 | 27 | "-working-directory $WORKING_DIR " \ |
---|
25 | 28 | "-c \"$CLANG_SOURCE\" -target bpf $CLANG_EMIT_LLVM -O2 -o - $LLVM_OPTIONS_PIPE" |
---|
.. | .. |
---|
262 | 265 | return -ENOMEM; |
---|
263 | 266 | return 0; |
---|
264 | 267 | } |
---|
| 268 | + pr_debug("%s: Couldn't find \"%s\", missing kernel-devel package?.\n", |
---|
| 269 | + __func__, autoconf_path); |
---|
265 | 270 | free(autoconf_path); |
---|
266 | 271 | return -ENOENT; |
---|
267 | 272 | } |
---|
.. | .. |
---|
285 | 290 | "obj-y := dummy.o\n" |
---|
286 | 291 | "\\$(obj)/%.o: \\$(src)/%.c\n" |
---|
287 | 292 | "\t@echo -n \"\\$(NOSTDINC_FLAGS) \\$(LINUXINCLUDE) \\$(EXTRA_CFLAGS)\"\n" |
---|
| 293 | +"\t\\$(CC) -c -o \\$@ \\$<\n" |
---|
288 | 294 | "EOF\n" |
---|
289 | 295 | "touch $TMPDIR/dummy.c\n" |
---|
290 | 296 | "make -s -C $KBUILD_DIR M=$TMPDIR $KBUILD_OPTS dummy.o 2>/dev/null\n" |
---|
.. | .. |
---|
352 | 358 | " \toption in [llvm] to \"\" to suppress this detection.\n\n", |
---|
353 | 359 | *kbuild_dir); |
---|
354 | 360 | |
---|
355 | | - free(*kbuild_dir); |
---|
356 | | - *kbuild_dir = NULL; |
---|
| 361 | + zfree(kbuild_dir); |
---|
357 | 362 | goto errout; |
---|
358 | 363 | } |
---|
359 | 364 | |
---|
.. | .. |
---|
416 | 421 | goto out; |
---|
417 | 422 | } |
---|
418 | 423 | |
---|
419 | | - pr_info("LLVM: dumping %s\n", obj_path); |
---|
| 424 | + pr_debug("LLVM: dumping %s\n", obj_path); |
---|
420 | 425 | if (fwrite(obj_buf, size, 1, fp) != 1) |
---|
421 | | - pr_warning("WARNING: failed to write to file '%s': %s, skip object dumping\n", |
---|
422 | | - obj_path, strerror(errno)); |
---|
| 426 | + pr_debug("WARNING: failed to write to file '%s': %s, skip object dumping\n", obj_path, strerror(errno)); |
---|
423 | 427 | fclose(fp); |
---|
424 | 428 | out: |
---|
425 | 429 | free(obj_path); |
---|
.. | .. |
---|
521 | 525 | |
---|
522 | 526 | pr_debug("llvm compiling command template: %s\n", template); |
---|
523 | 527 | |
---|
| 528 | + /* |
---|
| 529 | + * Below, substitute control characters for values that can cause the |
---|
| 530 | + * echo to misbehave, then substitute the values back. |
---|
| 531 | + */ |
---|
524 | 532 | err = -ENOMEM; |
---|
525 | | - if (asprintf(&command_echo, "echo -n \"%s\"", template) < 0) |
---|
| 533 | + if (asprintf(&command_echo, "echo -n \a%s\a", template) < 0) |
---|
526 | 534 | goto errout; |
---|
527 | 535 | |
---|
| 536 | +#define SWAP_CHAR(a, b) do { if (*p == a) *p = b; } while (0) |
---|
| 537 | + for (char *p = command_echo; *p; p++) { |
---|
| 538 | + SWAP_CHAR('<', '\001'); |
---|
| 539 | + SWAP_CHAR('>', '\002'); |
---|
| 540 | + SWAP_CHAR('"', '\003'); |
---|
| 541 | + SWAP_CHAR('\'', '\004'); |
---|
| 542 | + SWAP_CHAR('|', '\005'); |
---|
| 543 | + SWAP_CHAR('&', '\006'); |
---|
| 544 | + SWAP_CHAR('\a', '"'); |
---|
| 545 | + } |
---|
528 | 546 | err = read_from_pipe(command_echo, (void **) &command_out, NULL); |
---|
529 | 547 | if (err) |
---|
530 | 548 | goto errout; |
---|
531 | 549 | |
---|
| 550 | + for (char *p = command_out; *p; p++) { |
---|
| 551 | + SWAP_CHAR('\001', '<'); |
---|
| 552 | + SWAP_CHAR('\002', '>'); |
---|
| 553 | + SWAP_CHAR('\003', '"'); |
---|
| 554 | + SWAP_CHAR('\004', '\''); |
---|
| 555 | + SWAP_CHAR('\005', '|'); |
---|
| 556 | + SWAP_CHAR('\006', '&'); |
---|
| 557 | + } |
---|
| 558 | +#undef SWAP_CHAR |
---|
532 | 559 | pr_debug("llvm compiling command : %s\n", command_out); |
---|
533 | 560 | |
---|
534 | 561 | err = read_from_pipe(template, &obj_buf, &obj_buf_sz); |
---|