Skip to content

Commit 3a64760

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
objtool: Rework the elf_rebuild_reloc_section() logic
Instead of manually calling elf_rebuild_reloc_section() on sections we've called elf_add_reloc() on, have elf_write() DTRT. This makes it easier to add random relocations in places without carefully tracking when we're done and need to flush what section. Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Reviewed-by: Miroslav Benes <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent a958c4f commit 3a64760

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

tools/objtool/check.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -542,9 +542,6 @@ static int create_static_call_sections(struct objtool_file *file)
542542
idx++;
543543
}
544544

545-
if (elf_rebuild_reloc_section(file->elf, reloc_sec))
546-
return -1;
547-
548545
return 0;
549546
}
550547

@@ -614,9 +611,6 @@ static int create_mcount_loc_sections(struct objtool_file *file)
614611
idx++;
615612
}
616613

617-
if (elf_rebuild_reloc_section(file->elf, reloc_sec))
618-
return -1;
619-
620614
return 0;
621615
}
622616

tools/objtool/elf.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,8 @@ void elf_add_reloc(struct elf *elf, struct reloc *reloc)
479479

480480
list_add_tail(&reloc->list, &sec->reloc_list);
481481
elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc));
482+
483+
sec->changed = true;
482484
}
483485

484486
static int read_rel_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
@@ -558,7 +560,9 @@ static int read_relocs(struct elf *elf)
558560
return -1;
559561
}
560562

561-
elf_add_reloc(elf, reloc);
563+
list_add_tail(&reloc->list, &sec->reloc_list);
564+
elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc));
565+
562566
nr_reloc++;
563567
}
564568
max_reloc = max(max_reloc, nr_reloc);
@@ -873,14 +877,11 @@ static int elf_rebuild_rela_reloc_section(struct section *sec, int nr)
873877
return 0;
874878
}
875879

876-
int elf_rebuild_reloc_section(struct elf *elf, struct section *sec)
880+
static int elf_rebuild_reloc_section(struct elf *elf, struct section *sec)
877881
{
878882
struct reloc *reloc;
879883
int nr;
880884

881-
sec->changed = true;
882-
elf->changed = true;
883-
884885
nr = 0;
885886
list_for_each_entry(reloc, &sec->reloc_list, list)
886887
nr++;
@@ -944,9 +945,15 @@ int elf_write(struct elf *elf)
944945
struct section *sec;
945946
Elf_Scn *s;
946947

947-
/* Update section headers for changed sections: */
948+
/* Update changed relocation sections and section headers: */
948949
list_for_each_entry(sec, &elf->sections, list) {
949950
if (sec->changed) {
951+
if (sec->base &&
952+
elf_rebuild_reloc_section(elf, sec)) {
953+
WARN("elf_rebuild_reloc_section");
954+
return -1;
955+
}
956+
950957
s = elf_getscn(elf->elf, sec->idx);
951958
if (!s) {
952959
WARN_ELF("elf_getscn");
@@ -958,6 +965,7 @@ int elf_write(struct elf *elf)
958965
}
959966

960967
sec->changed = false;
968+
elf->changed = true;
961969
}
962970
}
963971

tools/objtool/include/objtool/elf.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *se
142142
struct symbol *find_func_containing(struct section *sec, unsigned long offset);
143143
void insn_to_reloc_sym_addend(struct section *sec, unsigned long offset,
144144
struct reloc *reloc);
145-
int elf_rebuild_reloc_section(struct elf *elf, struct section *sec);
146145

147146
#define for_each_sec(file, sec) \
148147
list_for_each_entry(sec, &file->elf->sections, list)

tools/objtool/orc_gen.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,5 @@ int orc_create(struct objtool_file *file)
254254
return -1;
255255
}
256256

257-
if (elf_rebuild_reloc_section(file->elf, ip_rsec))
258-
return -1;
259-
260257
return 0;
261258
}

0 commit comments

Comments
 (0)