Commit 22566c16 authored by Artem Savkov's avatar Artem Savkov Committed by Ingo Molnar

objtool: Fix segfault in .cold detection with -ffunction-sections

Because find_symbol_by_name() traverses the same lists as
read_symbols(), changing sym->name in place without copying it affects
the result of find_symbol_by_name().  In the case where a ".cold"
function precedes its parent in sec->symbol_list, it can result in a
function being considered a parent of itself. This leads to function
length being set to 0 and other consequent side-effects including a
segfault in add_switch_table().  The effects of this bug are only
visible when building with -ffunction-sections in KCFLAGS.

Fix by copying the search string instead of modifying it in place.
Signed-off-by: default avatarArtem Savkov <>
Signed-off-by: default avatarJosh Poimboeuf <>
Cc: Linus Torvalds <>
Cc: Peter Zijlstra <>
Cc: Thomas Gleixner <>
Fixes: 13810435 ("objtool: Support GCC 8's cold subfunctions")
Link: Ingo Molnar's avatarIngo Molnar <>
parent 0b9301fb
......@@ -31,6 +31,8 @@
#include "elf.h"
#include "warn.h"
#define MAX_NAME_LEN 128
struct section *find_section_by_name(struct elf *elf, const char *name)
struct section *sec;
......@@ -298,6 +300,8 @@ static int read_symbols(struct elf *elf)
/* Create parent/child links for any cold subfunctions */
list_for_each_entry(sec, &elf->sections, list) {
list_for_each_entry(sym, &sec->symbol_list, list) {
char pname[MAX_NAME_LEN + 1];
size_t pnamelen;
if (sym->type != STT_FUNC)
sym->pfunc = sym->cfunc = sym;
......@@ -305,9 +309,16 @@ static int read_symbols(struct elf *elf)
if (!coldstr)
coldstr[0] = '\0';
pfunc = find_symbol_by_name(elf, sym->name);
coldstr[0] = '.';
pnamelen = coldstr - sym->name;
if (pnamelen > MAX_NAME_LEN) {
WARN("%s(): parent function name exceeds maximum length of %d characters",
sym->name, MAX_NAME_LEN);
return -1;
strncpy(pname, sym->name, pnamelen);
pname[pnamelen] = '\0';
pfunc = find_symbol_by_name(elf, pname);
if (!pfunc) {
WARN("%s(): can't find parent function",
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment