Skip to content

Commit 9290e77

Browse files
jpoimboePeter Zijlstra
authored andcommitted
objtool: Add symbol iteration helpers
Add [sec_]for_each_sym() and use them. Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lkml.kernel.org/r/59023e5886ab125aa30702e633be7732b1acaa7e.1681325924.git.jpoimboe@kernel.org
1 parent 246b2c8 commit 9290e77

3 files changed

Lines changed: 51 additions & 58 deletions

File tree

tools/objtool/check.c

Lines changed: 41 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ static int decode_instructions(struct objtool_file *file)
470470

471471
// printf("%s: last chunk used: %d\n", sec->name, (int)idx);
472472

473-
list_for_each_entry(func, &sec->symbol_list, list) {
473+
sec_for_each_sym(sec, func) {
474474
if (func->type != STT_NOTYPE && func->type != STT_FUNC)
475475
continue;
476476

@@ -924,7 +924,7 @@ static int create_ibt_endbr_seal_sections(struct objtool_file *file)
924924

925925
static int create_cfi_sections(struct objtool_file *file)
926926
{
927-
struct section *sec, *s;
927+
struct section *sec;
928928
struct symbol *sym;
929929
unsigned int *loc;
930930
int idx;
@@ -937,48 +937,38 @@ static int create_cfi_sections(struct objtool_file *file)
937937
}
938938

939939
idx = 0;
940-
for_each_sec(file, s) {
941-
if (!s->text)
940+
for_each_sym(file, sym) {
941+
if (sym->type != STT_FUNC)
942942
continue;
943943

944-
list_for_each_entry(sym, &s->symbol_list, list) {
945-
if (sym->type != STT_FUNC)
946-
continue;
947-
948-
if (strncmp(sym->name, "__cfi_", 6))
949-
continue;
944+
if (strncmp(sym->name, "__cfi_", 6))
945+
continue;
950946

951-
idx++;
952-
}
947+
idx++;
953948
}
954949

955950
sec = elf_create_section(file->elf, ".cfi_sites", 0, sizeof(unsigned int), idx);
956951
if (!sec)
957952
return -1;
958953

959954
idx = 0;
960-
for_each_sec(file, s) {
961-
if (!s->text)
955+
for_each_sym(file, sym) {
956+
if (sym->type != STT_FUNC)
962957
continue;
963958

964-
list_for_each_entry(sym, &s->symbol_list, list) {
965-
if (sym->type != STT_FUNC)
966-
continue;
967-
968-
if (strncmp(sym->name, "__cfi_", 6))
969-
continue;
959+
if (strncmp(sym->name, "__cfi_", 6))
960+
continue;
970961

971-
loc = (unsigned int *)sec->data->d_buf + idx;
972-
memset(loc, 0, sizeof(unsigned int));
962+
loc = (unsigned int *)sec->data->d_buf + idx;
963+
memset(loc, 0, sizeof(unsigned int));
973964

974-
if (elf_add_reloc_to_insn(file->elf, sec,
975-
idx * sizeof(unsigned int),
976-
R_X86_64_PC32,
977-
s, sym->offset))
978-
return -1;
965+
if (elf_add_reloc_to_insn(file->elf, sec,
966+
idx * sizeof(unsigned int),
967+
R_X86_64_PC32,
968+
sym->sec, sym->offset))
969+
return -1;
979970

980-
idx++;
981-
}
971+
idx++;
982972
}
983973

984974
return 0;
@@ -2207,23 +2197,20 @@ static int add_func_jump_tables(struct objtool_file *file,
22072197
*/
22082198
static int add_jump_table_alts(struct objtool_file *file)
22092199
{
2210-
struct section *sec;
22112200
struct symbol *func;
22122201
int ret;
22132202

22142203
if (!file->rodata)
22152204
return 0;
22162205

2217-
for_each_sec(file, sec) {
2218-
list_for_each_entry(func, &sec->symbol_list, list) {
2219-
if (func->type != STT_FUNC)
2220-
continue;
2206+
for_each_sym(file, func) {
2207+
if (func->type != STT_FUNC)
2208+
continue;
22212209

2222-
mark_func_jump_tables(file, func);
2223-
ret = add_func_jump_tables(file, func);
2224-
if (ret)
2225-
return ret;
2226-
}
2210+
mark_func_jump_tables(file, func);
2211+
ret = add_func_jump_tables(file, func);
2212+
if (ret)
2213+
return ret;
22272214
}
22282215

22292216
return 0;
@@ -2535,30 +2522,27 @@ static bool is_profiling_func(const char *name)
25352522

25362523
static int classify_symbols(struct objtool_file *file)
25372524
{
2538-
struct section *sec;
25392525
struct symbol *func;
25402526

2541-
for_each_sec(file, sec) {
2542-
list_for_each_entry(func, &sec->symbol_list, list) {
2543-
if (func->bind != STB_GLOBAL)
2544-
continue;
2527+
for_each_sym(file, func) {
2528+
if (func->bind != STB_GLOBAL)
2529+
continue;
25452530

2546-
if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
2547-
strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
2548-
func->static_call_tramp = true;
2531+
if (!strncmp(func->name, STATIC_CALL_TRAMP_PREFIX_STR,
2532+
strlen(STATIC_CALL_TRAMP_PREFIX_STR)))
2533+
func->static_call_tramp = true;
25492534

2550-
if (arch_is_retpoline(func))
2551-
func->retpoline_thunk = true;
2535+
if (arch_is_retpoline(func))
2536+
func->retpoline_thunk = true;
25522537

2553-
if (arch_is_rethunk(func))
2554-
func->return_thunk = true;
2538+
if (arch_is_rethunk(func))
2539+
func->return_thunk = true;
25552540

2556-
if (arch_ftrace_match(func->name))
2557-
func->fentry = true;
2541+
if (arch_ftrace_match(func->name))
2542+
func->fentry = true;
25582543

2559-
if (is_profiling_func(func->name))
2560-
func->profiling_func = true;
2561-
}
2544+
if (is_profiling_func(func->name))
2545+
func->profiling_func = true;
25622546
}
25632547

25642548
return 0;
@@ -4213,7 +4197,7 @@ static int validate_section(struct objtool_file *file, struct section *sec)
42134197
struct symbol *func;
42144198
int warnings = 0;
42154199

4216-
list_for_each_entry(func, &sec->symbol_list, list) {
4200+
sec_for_each_sym(sec, func) {
42174201
if (func->type != STT_FUNC)
42184202
continue;
42194203

tools/objtool/elf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ static int read_symbols(struct elf *elf)
474474

475475
/* Create parent/child links for any cold subfunctions */
476476
list_for_each_entry(sec, &elf->sections, list) {
477-
list_for_each_entry(sym, &sec->symbol_list, list) {
477+
sec_for_each_sym(sec, sym) {
478478
char pname[MAX_NAME_LEN + 1];
479479
size_t pnamelen;
480480
if (sym->type != STT_FUNC)

tools/objtool/include/objtool/elf.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,13 @@ struct symbol *find_func_containing(struct section *sec, unsigned long offset);
188188
#define for_each_sec(file, sec) \
189189
list_for_each_entry(sec, &file->elf->sections, list)
190190

191+
#define sec_for_each_sym(sec, sym) \
192+
list_for_each_entry(sym, &sec->symbol_list, list)
193+
194+
#define for_each_sym(file, sym) \
195+
for (struct section *__sec, *__fake = (struct section *)1; \
196+
__fake; __fake = NULL) \
197+
for_each_sec(file, __sec) \
198+
sec_for_each_sym(__sec, sym)
199+
191200
#endif /* _OBJTOOL_ELF_H */

0 commit comments

Comments
 (0)