Path: blob/master/tools/objtool/Documentation/objtool.txt
26288 views
Objtool1=======23The kernel CONFIG_OBJTOOL option enables a host tool named 'objtool'4which runs at compile time. It can do various validations and5transformations on .o files.67Objtool has become an integral part of the x86-64 kernel toolchain. The8kernel depends on it for a variety of security and performance features9(and other types of features as well).101112Features13--------1415Objtool has the following features:1617- Stack unwinding metadata validation -- useful for helping to ensure18stack traces are reliable for live patching1920- ORC unwinder metadata generation -- a faster and more precise21alternative to frame pointer based unwinding2223- Retpoline validation -- ensures that all indirect calls go through24retpoline thunks, for Spectre v2 mitigations2526- Retpoline call site annotation -- annotates all retpoline thunk call27sites, enabling the kernel to patch them inline, to prevent "thunk28funneling" for both security and performance reasons2930- Return thunk validation -- validates return thunks are used for31certain CPU mitigations including Retbleed and SRSO3233- Return thunk annotation -- annotates all return thunk sites so kernel34can patch them inline, depending on enabled mitigations3536- Return thunk untraining validation -- validate that all entry paths37untrain a "safe return" before the first return (or call)3839- Non-instrumentation validation -- validates non-instrumentable40("noinstr") code rules, preventing instrumentation in low-level C41entry code4243- Static call annotation -- annotates static call sites, enabling the44kernel to implement inline static calls, a faster alternative to some45indirect branches4647- Uaccess validation -- validates uaccess rules for a proper48implementation of Supervisor Mode Access Protection (SMAP)4950- Straight Line Speculation validation -- validates certain SLS51mitigations5253- Indirect Branch Tracking validation -- validates Intel CET IBT rules54to ensure that all functions referenced by function pointers have55corresponding ENDBR instructions5657- Indirect Branch Tracking annotation -- annotates unused ENDBR58instruction sites, enabling the kernel to "seal" them (replace them59with NOPs) to further harden IBT6061- Function entry annotation -- annotates function entries, enabling62kernel function tracing6364- Function preamble (prefix) annotation and/or symbol generation -- used65for FineIBT and call depth tracking6667- Other toolchain hacks which will go unmentioned at this time...6869Each feature can be enabled individually or in combination using the70objtool cmdline.717273Objects74-------7576Typically, objtool runs on every translation unit (TU, aka ".o file") in77the kernel. If a TU is part of a kernel module, the '--module' option78is added.7980However:8182- If noinstr validation is enabled, it also runs on vmlinux.o, with all83options removed and '--noinstr' added.8485- If IBT or LTO is enabled, it doesn't run on TUs at all. Instead it86runs on vmlinux.o and linked modules, with all options.8788In summary:8990A) Legacy mode:91TU: objtool [--module] <options>92vmlinux: N/A93module: N/A9495B) CONFIG_NOINSTR_VALIDATION=y && !(CONFIG_X86_KERNEL_IBT=y || CONFIG_LTO=y):96TU: objtool [--module] <options> // no --noinstr97vmlinux: objtool --noinstr // other options removed98module: N/A99100C) CONFIG_X86_KERNEL_IBT=y || CONFIG_LTO=y:101TU: N/A102vmlinux: objtool --noinstr <options>103module: objtool --module --noinstr <options>104105106Stack validation107----------------108109Objtool's stack validation feature analyzes every .o file and ensures110the validity of its stack metadata. It enforces a set of rules on asm111code and C inline assembly code so that stack traces can be reliable.112113For each function, it recursively follows all possible code paths and114validates the correct frame pointer state at each instruction.115116It also follows code paths involving special sections, like117.altinstructions, __jump_table, and __ex_table, which can add118alternative execution paths to a given instruction (or set of119instructions). Similarly, it knows how to follow switch statements, for120which gcc sometimes uses jump tables.121122Here are some of the benefits of validating stack metadata:123124a) More reliable stack traces for frame pointer enabled kernels125126Frame pointers are used for debugging purposes. They allow runtime127code and debug tools to be able to walk the stack to determine the128chain of function call sites that led to the currently executing129code.130131For some architectures, frame pointers are enabled by132CONFIG_FRAME_POINTER. For some other architectures they may be133required by the ABI (sometimes referred to as "backchain pointers").134135For C code, gcc automatically generates instructions for setting up136frame pointers when the -fno-omit-frame-pointer option is used.137138But for asm code, the frame setup instructions have to be written by139hand, which most people don't do. So the end result is that140CONFIG_FRAME_POINTER is honored for C code but not for most asm code.141142For stack traces based on frame pointers to be reliable, all143functions which call other functions must first create a stack frame144and update the frame pointer. If a first function doesn't properly145create a stack frame before calling a second function, the *caller*146of the first function will be skipped on the stack trace.147148For example, consider the following example backtrace with frame149pointers enabled:150151[<ffffffff81812584>] dump_stack+0x4b/0x63152[<ffffffff812d6dc2>] cmdline_proc_show+0x12/0x30153[<ffffffff8127f568>] seq_read+0x108/0x3e0154[<ffffffff812cce62>] proc_reg_read+0x42/0x70155[<ffffffff81256197>] __vfs_read+0x37/0x100156[<ffffffff81256b16>] vfs_read+0x86/0x130157[<ffffffff81257898>] SyS_read+0x58/0xd0158[<ffffffff8181c1f2>] entry_SYSCALL_64_fastpath+0x12/0x76159160It correctly shows that the caller of cmdline_proc_show() is161seq_read().162163If we remove the frame pointer logic from cmdline_proc_show() by164replacing the frame pointer related instructions with nops, here's165what it looks like instead:166167[<ffffffff81812584>] dump_stack+0x4b/0x63168[<ffffffff812d6dc2>] cmdline_proc_show+0x12/0x30169[<ffffffff812cce62>] proc_reg_read+0x42/0x70170[<ffffffff81256197>] __vfs_read+0x37/0x100171[<ffffffff81256b16>] vfs_read+0x86/0x130172[<ffffffff81257898>] SyS_read+0x58/0xd0173[<ffffffff8181c1f2>] entry_SYSCALL_64_fastpath+0x12/0x76174175Notice that cmdline_proc_show()'s caller, seq_read(), has been176skipped. Instead the stack trace seems to show that177cmdline_proc_show() was called by proc_reg_read().178179The benefit of objtool here is that because it ensures that *all*180functions honor CONFIG_FRAME_POINTER, no functions will ever[*] be181skipped on a stack trace.182183[*] unless an interrupt or exception has occurred at the very184beginning of a function before the stack frame has been created,185or at the very end of the function after the stack frame has been186destroyed. This is an inherent limitation of frame pointers.187188b) ORC (Oops Rewind Capability) unwind table generation189190An alternative to frame pointers and DWARF, ORC unwind data can be191used to walk the stack. Unlike frame pointers, ORC data is out of192band. So it doesn't affect runtime performance and it can be193reliable even when interrupts or exceptions are involved.194195For more details, see Documentation/arch/x86/orc-unwinder.rst.196197c) Higher live patching compatibility rate198199Livepatch has an optional "consistency model", which is needed for200more complex patches. In order for the consistency model to work,201stack traces need to be reliable (or an unreliable condition needs to202be detectable). Objtool makes that possible.203204For more details, see the livepatch documentation in the Linux kernel205source tree at Documentation/livepatch/livepatch.rst.206207To achieve the validation, objtool enforces the following rules:2082091. Each callable function must be annotated as such with the ELF210function type. In asm code, this is typically done using the211SYM_FUNC_{START,END} macros. If objtool finds a return instruction212outside of a function, it flags an error since that usually indicates213callable code which should be annotated accordingly.214215This rule is needed so that objtool can properly identify each216callable function in order to analyze its stack metadata.2172182. Conversely, each section of code which is *not* callable, or is219otherwise doing funny things with the stack or registers, should220*not* be annotated as an ELF function. Rather, SYM_CODE_{START,END}221should be used along with unwind hints.2222233. Each callable function which calls another function must have the224correct frame pointer logic, if required by CONFIG_FRAME_POINTER or225the architecture's back chain rules. This can by done in asm code226with the FRAME_BEGIN/FRAME_END macros.227228This rule ensures that frame pointer based stack traces will work as229designed. If function A doesn't create a stack frame before calling230function B, the _caller_ of function A will be skipped on the stack231trace.2322334. Indirect jumps and jumps to undefined symbols are only allowed if:234235a) the jump is part of a switch statement; or236237b) the jump matches sibling call semantics and the frame pointer has238the same value it had on function entry.239240This rule is needed so that objtool can reliably analyze all of a241function's code paths. If a function jumps to code in another file,242and it's not a sibling call, objtool has no way to follow the jump243because it only analyzes a single file at a time.2442455. A callable function may not execute kernel entry/exit instructions.246The only code which needs such instructions is kernel entry code,247which shouldn't be be in callable functions anyway.248249This rule is just a sanity check to ensure that callable functions250return normally.251252253Objtool warnings254----------------255256NOTE: When requesting help with an objtool warning, please recreate with257OBJTOOL_VERBOSE=1 (e.g., "make OBJTOOL_VERBOSE=1") and send the full258output, including any disassembly or backtrace below the warning, to the259objtool maintainers.260261For asm files, if you're getting an error which doesn't make sense,262first make sure that the affected code follows the above rules.263264For C files, the common culprits are inline asm statements and calls to265"noreturn" functions. See below for more details.266267Another possible cause for errors in C code is if the Makefile removes268-fno-omit-frame-pointer or adds -fomit-frame-pointer to the gcc options.269270Here are some examples of common warnings reported by objtool, what271they mean, and suggestions for how to fix them. When in doubt, ping272the objtool maintainers.2732742751. file.o: warning: objtool: func()+0x128: call without frame pointer save/setup276277The func() function made a function call without first saving and/or278updating the frame pointer, and CONFIG_FRAME_POINTER is enabled.279280If the error is for an asm file, and func() is indeed a callable281function, add proper frame pointer logic using the FRAME_BEGIN and282FRAME_END macros. Otherwise, if it's not a callable function, remove283its ELF function annotation by using SYM_CODE_{START,END} and use the284manual unwind hint macros in asm/unwind_hints.h.285286If it's a GCC-compiled .c file, the error may be because the function287uses an inline asm() statement which has a "call" instruction. An288asm() statement with a call instruction must declare the use of the289stack pointer in its output operand. On x86_64, this means adding290the ASM_CALL_CONSTRAINT as an output constraint:291292asm volatile("call func" : ASM_CALL_CONSTRAINT);293294Otherwise the stack frame may not get created before the call.295296objtool can help with pinpointing the exact function where it happens:297298$ OBJTOOL_ARGS="--verbose" make arch/x86/kvm/299300arch/x86/kvm/kvm.o: warning: objtool: .altinstr_replacement+0xc5: call without frame pointer save/setup301arch/x86/kvm/kvm.o: warning: objtool: em_loop.part.0+0x29: (alt)302arch/x86/kvm/kvm.o: warning: objtool: em_loop.part.0+0x0: <=== (sym)303LD [M] arch/x86/kvm/kvm-intel.o3040000 0000000000028220 <em_loop.part.0>:3050000 28220: 0f b6 47 61 movzbl 0x61(%rdi),%eax3060004 28224: 3c e2 cmp $0xe2,%al3070006 28226: 74 2c je 28254 <em_loop.part.0+0x34>3080008 28228: 48 8b 57 10 mov 0x10(%rdi),%rdx309000c 2822c: 83 f0 05 xor $0x5,%eax310000f 2822f: 48 c1 e0 04 shl $0x4,%rax3110013 28233: 25 f0 00 00 00 and $0xf0,%eax3120018 28238: 81 e2 d5 08 00 00 and $0x8d5,%edx313001e 2823e: 80 ce 02 or $0x2,%dh314...3153163172. file.o: warning: objtool: .text+0x53: unreachable instruction318319Objtool couldn't find a code path to reach the instruction.320321If the error is for an asm file, and the instruction is inside (or322reachable from) a callable function, the function should be annotated323with the SYM_FUNC_START and SYM_FUNC_END macros.324325Otherwise, SYM_CODE_START can be used. In that case the code needs326to be annotated with unwind hint macros.327328If you're sure the code won't affect the reliability of runtime stack329traces and want objtool to ignore it, see "Adding exceptions" below.3303313323. file.o: warning: objtool: foo+0x48c: bar() missing __noreturn in .c/.h or NORETURN() in noreturns.h333334The call from foo() to bar() doesn't return, but bar() is incorrectly335annotated. A noreturn function must be marked __noreturn in both its336declaration and its definition, and must have a NORETURN() annotation337in tools/objtool/noreturns.h.3383393404. file.o: warning: objtool: func(): can't find starting instruction341or342file.o: warning: objtool: func()+0x11dd: can't decode instruction343344Does the file have data in a text section? If so, that can confuse345objtool's instruction decoder. Move the data to a more appropriate346section like .data or .rodata.3473483495. file.o: warning: objtool: func()+0x6: unsupported instruction in callable function350351This is a kernel entry/exit instruction like sysenter or iret. Such352instructions aren't allowed in a callable function, and are most353likely part of the kernel entry code. Such code should probably be354placed in a SYM_CODE_{START,END} block with unwind hints.3553563576. file.o: warning: objtool: func()+0x26: sibling call from callable instruction with modified stack frame358359This is a branch to an UNDEF symbol. Objtool assumed it's a360sibling call and detected that the stack wasn't first restored to its361original state.362363If it's not really a sibling call, you may need to use unwind hints364and/or move the destination code to the local file.365366If the instruction is not actually in a callable function (e.g.367kernel entry code), use SYM_CODE_{START,END} and unwind hints.3683693707. file: warning: objtool: func()+0x5c: stack state mismatch371372The instruction's frame pointer state is inconsistent, depending on373which execution path was taken to reach the instruction.374375Make sure that, when CONFIG_FRAME_POINTER is enabled, the function376pushes and sets up the frame pointer (for x86_64, this means rbp) at377the beginning of the function and pops it at the end of the function.378Also make sure that no other code in the function touches the frame379pointer.380381Another possibility is that the code has some asm or inline asm which382does some unusual things to the stack or the frame pointer. In such383cases it's probably appropriate to use SYM_CODE_{START,END} with unwind384hints.3853863878. file.o: warning: objtool: funcA() falls through to next function funcB()388389This means that funcA() doesn't end with a return instruction or an390unconditional jump, and that objtool has determined that the function391can fall through into the next function. There could be different392reasons for this:393394a) funcA()'s last instruction is a call to a "noreturn" function like395panic(). In this case the noreturn function needs to be added to396objtool's hard-coded global_noreturns array. Feel free to bug the397objtool maintainer, or you can submit a patch.398399b) funcA() uses the unreachable() annotation in a section of code400that is actually reachable.401402c) Some undefined behavior like divide by zero.4034044059. file.o: warning: objtool: funcA() call to funcB() with UACCESS enabled406407This means that an unexpected call to a non-whitelisted function exists408outside of arch-specific guards.409X86: SMAP (stac/clac): __uaccess_begin()/__uaccess_end()410ARM: PAN: uaccess_enable()/uaccess_disable()411412These functions should be called to denote a minimal critical section around413access to __user variables. See also: https://lwn.net/Articles/517475/414415The intention of the warning is to prevent calls to funcB() from eventually416calling schedule(), potentially leaking the AC flags state, and not417restoring them correctly.418419It also helps verify that there are no unexpected calls to funcB() which may420access user space pages with protections against doing so disabled.421422To fix, either:4231) remove explicit calls to funcB() from funcA().4242) add the correct guards before and after calls to low level functions like425__get_user_size()/__put_user_size().4263) add funcB to uaccess_safe_builtin whitelist in tools/objtool/check.c, if427funcB obviously does not call schedule(), and is marked notrace (since428function tracing inserts additional calls, which is not obvious from the429sources).43043110. file.o: warning: func()+0x5c: stack layout conflict in alternatives432433This means that in the use of the alternative() or ALTERNATIVE()434macro, the code paths have conflicting modifications to the stack.435The problem is that there is only one ORC unwind table, which means436that the ORC unwind entries must be consistent for all possible437instruction boundaries regardless of which code has been patched.438This limitation can be overcome by massaging the alternatives with439NOPs to shift the stack changes around so they no longer conflict.44044144211. file.o: warning: unannotated intra-function call443444This warning means that a direct call is done to a destination which445is not at the beginning of a function. If this is a legit call, you446can remove this warning by putting the ANNOTATE_INTRA_FUNCTION_CALL447directive right before the call.44844945012. file.o: warning: func(): not an indirect call target451452This means that objtool is running with --ibt and a function453expected to be an indirect call target is not. In particular, this454happens for init_module() or cleanup_module() if a module relies on455these special names and does not use module_init() / module_exit()456macros to create them.457458459If the error doesn't seem to make sense, it could be a bug in objtool.460Feel free to ask objtool maintainers for help.461462463Adding exceptions464-----------------465466If you _really_ need objtool to ignore something, and are 100% sure467that it won't affect kernel stack traces, you can tell objtool to468ignore it:469470- To skip validation of a function, use the STACK_FRAME_NON_STANDARD471macro.472473- To skip validation of a file, add474475OBJECT_FILES_NON_STANDARD_filename.o := y476477to the Makefile.478479- To skip validation of a directory, add480481OBJECT_FILES_NON_STANDARD := y482483to the Makefile.484485NOTE: OBJECT_FILES_NON_STANDARD doesn't work for link time validation of486vmlinux.o or a linked module. So it should only be used for files which487aren't linked into vmlinux or a module.488489490