Skip to content

Commit d832c00

Browse files
author
Peter Zijlstra
committed
Merge branch 'objtool/urgent' into objtool/core
Conflicts: tools/objtool/elf.c tools/objtool/elf.h tools/objtool/orc_gen.c tools/objtool/check.c Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
2 parents 14bda4e + 0f1441b commit d832c00

File tree

8 files changed

+120
-9
lines changed

8 files changed

+120
-9
lines changed

arch/x86/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ config X86
6767
select ARCH_HAS_FILTER_PGPROT
6868
select ARCH_HAS_FORTIFY_SOURCE
6969
select ARCH_HAS_GCOV_PROFILE_ALL
70-
select ARCH_HAS_KCOV if X86_64
70+
select ARCH_HAS_KCOV if X86_64 && STACK_VALIDATION
7171
select ARCH_HAS_MEM_ENCRYPT
7272
select ARCH_HAS_MEMBARRIER_SYNC_CORE
7373
select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE

tools/objtool/arch.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,6 @@ unsigned long arch_jump_destination(struct instruction *insn);
8484

8585
unsigned long arch_dest_reloc_offset(int addend);
8686

87+
const char *arch_nop_insn(int len);
88+
8789
#endif /* _ARCH_H */

tools/objtool/arch/x86/decode.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,21 @@ void arch_initial_func_cfi_state(struct cfi_init_state *state)
565565
state->regs[16].base = CFI_CFA;
566566
state->regs[16].offset = -8;
567567
}
568+
569+
const char *arch_nop_insn(int len)
570+
{
571+
static const char nops[5][5] = {
572+
/* 1 */ { 0x90 },
573+
/* 2 */ { 0x66, 0x90 },
574+
/* 3 */ { 0x0f, 0x1f, 0x00 },
575+
/* 4 */ { 0x0f, 0x1f, 0x40, 0x00 },
576+
/* 5 */ { 0x0f, 0x1f, 0x44, 0x00, 0x00 },
577+
};
578+
579+
if (len < 1 || len > 5) {
580+
WARN("invalid NOP size: %d\n", len);
581+
return NULL;
582+
}
583+
584+
return nops[len-1];
585+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef _OBJTOOL_ARCH_ELF
2+
#define _OBJTOOL_ARCH_ELF
3+
4+
#define R_NONE R_X86_64_NONE
5+
6+
#endif /* _OBJTOOL_ARCH_ELF */

tools/objtool/check.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "check.h"
1313
#include "special.h"
1414
#include "warn.h"
15+
#include "arch_elf.h"
1516

1617
#include <linux/hashtable.h>
1718
#include <linux/kernel.h>
@@ -765,6 +766,24 @@ static int add_call_destinations(struct objtool_file *file)
765766
} else
766767
insn->call_dest = reloc->sym;
767768

769+
/*
770+
* Many compilers cannot disable KCOV with a function attribute
771+
* so they need a little help, NOP out any KCOV calls from noinstr
772+
* text.
773+
*/
774+
if (insn->sec->noinstr &&
775+
!strncmp(insn->call_dest->name, "__sanitizer_cov_", 16)) {
776+
if (reloc) {
777+
reloc->type = R_NONE;
778+
elf_write_reloc(file->elf, reloc);
779+
}
780+
781+
elf_write_insn(file->elf, insn->sec,
782+
insn->offset, insn->len,
783+
arch_nop_insn(insn->len));
784+
insn->type = INSN_NOP;
785+
}
786+
768787
/*
769788
* Whatever stack impact regular CALLs have, should be undone
770789
* by the RETURN of the called function.
@@ -2740,7 +2759,7 @@ int check(const char *_objname, bool orc)
27402759

27412760
objname = _objname;
27422761

2743-
file.elf = elf_open_read(objname, orc ? O_RDWR : O_RDONLY);
2762+
file.elf = elf_open_read(objname, O_RDWR);
27442763
if (!file.elf)
27452764
return 1;
27462765

@@ -2801,7 +2820,9 @@ int check(const char *_objname, bool orc)
28012820
ret = create_orc_sections(&file);
28022821
if (ret < 0)
28032822
goto out;
2823+
}
28042824

2825+
if (file.elf->changed) {
28052826
ret = elf_write(file.elf);
28062827
if (ret < 0)
28072828
goto out;

tools/objtool/elf.c

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,10 @@ static int read_relocs(struct elf *elf)
563563
break;
564564
default: return -1;
565565
}
566-
reloc->sym = find_symbol_by_index(elf, symndx);
566+
567567
reloc->sec = sec;
568+
reloc->idx = i;
569+
reloc->sym = find_symbol_by_index(elf, symndx);
568570
if (!reloc->sym) {
569571
WARN("can't find reloc entry symbol %d for %s",
570572
symndx, sec->name);
@@ -747,6 +749,8 @@ struct section *elf_create_section(struct elf *elf, const char *name,
747749
elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
748750
elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
749751

752+
elf->changed = true;
753+
750754
return sec;
751755
}
752756

@@ -880,11 +884,14 @@ static int elf_rebuild_rela_reloc_section(struct section *sec, int nr)
880884
return 0;
881885
}
882886

883-
int elf_rebuild_reloc_section(struct section *sec)
887+
int elf_rebuild_reloc_section(struct elf *elf, struct section *sec)
884888
{
885889
struct reloc *reloc;
886890
int nr;
887891

892+
sec->changed = true;
893+
elf->changed = true;
894+
888895
nr = 0;
889896
list_for_each_entry(reloc, &sec->reloc_list, list)
890897
nr++;
@@ -896,7 +903,54 @@ int elf_rebuild_reloc_section(struct section *sec)
896903
}
897904
}
898905

899-
int elf_write(const struct elf *elf)
906+
int elf_write_insn(struct elf *elf, struct section *sec,
907+
unsigned long offset, unsigned int len,
908+
const char *insn)
909+
{
910+
Elf_Data *data = sec->data;
911+
912+
if (data->d_type != ELF_T_BYTE || data->d_off) {
913+
WARN("write to unexpected data for section: %s", sec->name);
914+
return -1;
915+
}
916+
917+
memcpy(data->d_buf + offset, insn, len);
918+
elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY);
919+
920+
elf->changed = true;
921+
922+
return 0;
923+
}
924+
925+
int elf_write_reloc(struct elf *elf, struct reloc *reloc)
926+
{
927+
struct section *sec = reloc->sec;
928+
929+
if (sec->sh.sh_type == SHT_REL) {
930+
reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
931+
reloc->rel.r_offset = reloc->offset;
932+
933+
if (!gelf_update_rel(sec->data, reloc->idx, &reloc->rel)) {
934+
WARN_ELF("gelf_update_rel");
935+
return -1;
936+
}
937+
} else {
938+
reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
939+
reloc->rela.r_addend = reloc->addend;
940+
reloc->rela.r_offset = reloc->offset;
941+
942+
if (!gelf_update_rela(sec->data, reloc->idx, &reloc->rela)) {
943+
WARN_ELF("gelf_update_rela");
944+
return -1;
945+
}
946+
}
947+
948+
elf->changed = true;
949+
950+
return 0;
951+
}
952+
953+
int elf_write(struct elf *elf)
900954
{
901955
struct section *sec;
902956
Elf_Scn *s;
@@ -913,6 +967,8 @@ int elf_write(const struct elf *elf)
913967
WARN_ELF("gelf_update_shdr");
914968
return -1;
915969
}
970+
971+
sec->changed = false;
916972
}
917973
}
918974

@@ -925,6 +981,8 @@ int elf_write(const struct elf *elf)
925981
return -1;
926982
}
927983

984+
elf->changed = false;
985+
928986
return 0;
929987
}
930988

tools/objtool/elf.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ struct reloc {
6767
};
6868
struct section *sec;
6969
struct symbol *sym;
70-
unsigned int type;
7170
unsigned long offset;
71+
unsigned int type;
7272
int addend;
73+
int idx;
7374
bool jump_table_start;
7475
};
7576

@@ -79,6 +80,7 @@ struct elf {
7980
Elf *elf;
8081
GElf_Ehdr ehdr;
8182
int fd;
83+
bool changed;
8284
char *name;
8385
struct list_head sections;
8486
DECLARE_HASHTABLE(symbol_hash, ELF_HASH_BITS);
@@ -121,7 +123,11 @@ struct elf *elf_open_read(const char *name, int flags);
121123
struct section *elf_create_section(struct elf *elf, const char *name, size_t entsize, int nr);
122124
struct section *elf_create_reloc_section(struct elf *elf, struct section *base, int reltype);
123125
void elf_add_reloc(struct elf *elf, struct reloc *reloc);
124-
int elf_write(const struct elf *elf);
126+
int elf_write_insn(struct elf *elf, struct section *sec,
127+
unsigned long offset, unsigned int len,
128+
const char *insn);
129+
int elf_write_reloc(struct elf *elf, struct reloc *reloc);
130+
int elf_write(struct elf *elf);
125131
void elf_close(struct elf *elf);
126132

127133
struct section *find_section_by_name(const struct elf *elf, const char *name);
@@ -133,7 +139,7 @@ struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, uns
133139
struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
134140
unsigned long offset, unsigned int len);
135141
struct symbol *find_func_containing(struct section *sec, unsigned long offset);
136-
int elf_rebuild_reloc_section(struct section *sec);
142+
int elf_rebuild_reloc_section(struct elf *elf, struct section *sec);
137143

138144
#define for_each_sec(file, sec) \
139145
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_reloc_section(ip_relocsec))
225+
if (elf_rebuild_reloc_section(file->elf, ip_relocsec))
226226
return -1;
227227

228228
return 0;

0 commit comments

Comments
 (0)