|
| 1 | +// SPDX-License-Identifier: BSD-2-Clause |
| 2 | +#include <stdio.h> |
| 3 | +#include "common.h" |
| 4 | +#include "../usdt.h" |
| 5 | + |
| 6 | +/* |
| 7 | + * Validate USDT note integrity when the linker garbage-collects sections. |
| 8 | + * This test is compiled with -ffunction-sections -fdata-sections and linked |
| 9 | + * with -Wl,--gc-sections (see Makefile). |
| 10 | + * |
| 11 | + * gc_collected_func() is never called, so its section is eligible for |
| 12 | + * collection. .note.stapsdt is a non-SHF_ALLOC section and linkers differ in |
| 13 | + * whether references from it keep sections alive: |
| 14 | + * - BFD ld treats them as GC roots, so the unused function (and its |
| 15 | + * semaphore) survive and the note stays valid; |
| 16 | + * - LLD does not follow them: the function is collected and the note's |
| 17 | + * relocation against the collected section silently resolves to |
| 18 | + * 0 + addend, leaving a dangling note whose "location" is the nop's |
| 19 | + * offset inside the discarded section (tiny bogus value). The implicit |
| 20 | + * semaphore in .probes and the .stapsdt.base section are collected the |
| 21 | + * same way, zeroing the note's semaphore/base fields. |
| 22 | + * Tools iterating notes then trip on the dangling entry; e.g. libbpf fails |
| 23 | + * the entire probe, including its valid same-name copies. |
| 24 | + * |
| 25 | + * run_test.sh fails any test whose binary contains a note whose location |
| 26 | + * falls in no executable segment, which is what this test is really |
| 27 | + * asserting. |
| 28 | + * Which of gc_collected_func()'s notes survive (and with what semaphore |
| 29 | + * values) is linker-dependent, so USDT_SPECS only pins down the live |
| 30 | + * probes and accepts anything extra. The BASE* and SEMA* stubs (as opposed |
| 31 | + * to plain *) assert that the kept probes' .stapsdt.base and semaphore |
| 32 | + * references stayed intact (LLD used to garbage-collect those sections and |
| 33 | + * zero out the fields even in surviving notes). |
| 34 | + */ |
| 35 | + |
| 36 | +void gc_collected_func(int x) |
| 37 | +{ |
| 38 | + USDT(test, collected, x); |
| 39 | + USDT_WITH_SEMA(test, collected_sema, x); |
| 40 | +} |
| 41 | + |
| 42 | +int main(int argc, char **argv) |
| 43 | +{ |
| 44 | + if (handle_args(argc, argv)) |
| 45 | + return 0; |
| 46 | + |
| 47 | + USDT(test, kept, 1); |
| 48 | + USDT_WITH_SEMA(test, kept_sema, 2); |
| 49 | + |
| 50 | + return 0; |
| 51 | +} |
| 52 | + |
| 53 | +const char *USDT_SPECS = |
| 54 | +"...\n" |
| 55 | +"test:kept base=BASE* sema=0 argn=1 args=*.\n" |
| 56 | +"test:kept_sema base=BASE* sema=SEMA* argn=1 args=*.\n" |
| 57 | +"...\n" |
| 58 | +; |
0 commit comments