Skip to content

Commit 2b10be2

Browse files
author
Peter Zijlstra
committed
objtool: Clean up elf_write() condition
With there being multiple ways to change the ELF data, let's more concisely track modification. Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
1 parent b3a9e3b commit 2b10be2

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

tools/objtool/check.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2740,7 +2740,7 @@ int check(const char *_objname, bool orc)
27402740

27412741
objname = _objname;
27422742

2743-
file.elf = elf_open_read(objname, orc ? O_RDWR : O_RDONLY);
2743+
file.elf = elf_open_read(objname, O_RDWR);
27442744
if (!file.elf)
27452745
return 1;
27462746

@@ -2801,7 +2801,9 @@ int check(const char *_objname, bool orc)
28012801
ret = create_orc_sections(&file);
28022802
if (ret < 0)
28032803
goto out;
2804+
}
28042805

2806+
if (file.elf->changed) {
28052807
ret = elf_write(file.elf);
28062808
if (ret < 0)
28072809
goto out;

tools/objtool/elf.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,8 @@ struct section *elf_create_section(struct elf *elf, const char *name,
713713
elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
714714
elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
715715

716+
elf->changed = true;
717+
716718
return sec;
717719
}
718720

@@ -746,7 +748,7 @@ struct section *elf_create_rela_section(struct elf *elf, struct section *base)
746748
return sec;
747749
}
748750

749-
int elf_rebuild_rela_section(struct section *sec)
751+
int elf_rebuild_rela_section(struct elf *elf, struct section *sec)
750752
{
751753
struct rela *rela;
752754
int nr, idx = 0, size;
@@ -763,6 +765,9 @@ int elf_rebuild_rela_section(struct section *sec)
763765
return -1;
764766
}
765767

768+
sec->changed = true;
769+
elf->changed = true;
770+
766771
sec->data->d_buf = relas;
767772
sec->data->d_size = size;
768773

@@ -779,7 +784,7 @@ int elf_rebuild_rela_section(struct section *sec)
779784
return 0;
780785
}
781786

782-
int elf_write(const struct elf *elf)
787+
int elf_write(struct elf *elf)
783788
{
784789
struct section *sec;
785790
Elf_Scn *s;
@@ -796,6 +801,8 @@ int elf_write(const struct elf *elf)
796801
WARN_ELF("gelf_update_shdr");
797802
return -1;
798803
}
804+
805+
sec->changed = false;
799806
}
800807
}
801808

@@ -808,6 +815,8 @@ int elf_write(const struct elf *elf)
808815
return -1;
809816
}
810817

818+
elf->changed = false;
819+
811820
return 0;
812821
}
813822

tools/objtool/elf.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ struct elf {
7676
Elf *elf;
7777
GElf_Ehdr ehdr;
7878
int fd;
79+
bool changed;
7980
char *name;
8081
struct list_head sections;
8182
DECLARE_HASHTABLE(symbol_hash, ELF_HASH_BITS);
@@ -118,7 +119,7 @@ struct elf *elf_open_read(const char *name, int flags);
118119
struct section *elf_create_section(struct elf *elf, const char *name, size_t entsize, int nr);
119120
struct section *elf_create_rela_section(struct elf *elf, struct section *base);
120121
void elf_add_rela(struct elf *elf, struct rela *rela);
121-
int elf_write(const struct elf *elf);
122+
int elf_write(struct elf *elf);
122123
void elf_close(struct elf *elf);
123124

124125
struct section *find_section_by_name(const struct elf *elf, const char *name);
@@ -130,7 +131,7 @@ struct rela *find_rela_by_dest(const struct elf *elf, struct section *sec, unsig
130131
struct rela *find_rela_by_dest_range(const struct elf *elf, struct section *sec,
131132
unsigned long offset, unsigned int len);
132133
struct symbol *find_func_containing(struct section *sec, unsigned long offset);
133-
int elf_rebuild_rela_section(struct section *sec);
134+
int elf_rebuild_rela_section(struct elf *elf, struct section *sec);
134135

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

tools/objtool/orc_gen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ int create_orc_sections(struct objtool_file *file)
222222
}
223223
}
224224

225-
if (elf_rebuild_rela_section(ip_relasec))
225+
if (elf_rebuild_rela_section(file->elf, ip_relasec))
226226
return -1;
227227

228228
return 0;

0 commit comments

Comments
 (0)