Loading tools/objtool/builtin-check.c +1 −1 Original line number Diff line number Diff line Loading @@ -329,5 +329,5 @@ int objtool_run(int argc, const char **argv) if (!opts.dryrun && file->elf->changed && elf_write(file->elf)) return 1; return 0; return elf_close(file->elf); } tools/objtool/elf.c +143 −1 Original line number Diff line number Diff line Loading @@ -16,6 +16,7 @@ #include <string.h> #include <unistd.h> #include <errno.h> #include <libgen.h> #include <linux/interval_tree_generic.h> #include <objtool/builtin.h> #include <objtool/elf.h> Loading Loading @@ -1067,6 +1068,12 @@ struct elf *elf_open_read(const char *name, int flags) goto err; } elf->name = strdup(name); if (!elf->name) { ERROR_GLIBC("strdup"); return NULL; } if ((flags & O_ACCMODE) == O_RDONLY) cmd = ELF_C_READ_MMAP; else if ((flags & O_ACCMODE) == O_RDWR) Loading Loading @@ -1104,6 +1111,137 @@ struct elf *elf_open_read(const char *name, int flags) return NULL; } struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name) { struct section *null, *symtab, *strtab, *shstrtab; char *dir, *base, *tmp_name; struct symbol *sym; struct elf *elf; elf_version(EV_CURRENT); elf = calloc(1, sizeof(*elf)); if (!elf) { ERROR_GLIBC("calloc"); return NULL; } INIT_LIST_HEAD(&elf->sections); dir = strdup(name); if (!dir) { ERROR_GLIBC("strdup"); return NULL; } dir = dirname(dir); base = strdup(name); if (!base) { ERROR_GLIBC("strdup"); return NULL; } base = basename(base); tmp_name = malloc(256); if (!tmp_name) { ERROR_GLIBC("malloc"); return NULL; } snprintf(tmp_name, 256, "%s/%s.XXXXXX", dir, base); elf->fd = mkstemp(tmp_name); if (elf->fd == -1) { ERROR_GLIBC("can't create tmp file"); exit(1); } elf->tmp_name = tmp_name; elf->name = strdup(name); if (!elf->name) { ERROR_GLIBC("strdup"); return NULL; } elf->elf = elf_begin(elf->fd, ELF_C_WRITE, NULL); if (!elf->elf) { ERROR_ELF("elf_begin"); return NULL; } if (!gelf_newehdr(elf->elf, ELFCLASS64)) { ERROR_ELF("gelf_newehdr"); return NULL; } memcpy(&elf->ehdr, ehdr, sizeof(elf->ehdr)); if (!gelf_update_ehdr(elf->elf, &elf->ehdr)) { ERROR_ELF("gelf_update_ehdr"); return NULL; } if (!elf_alloc_hash(section, 1000) || !elf_alloc_hash(section_name, 1000) || !elf_alloc_hash(symbol, 10000) || !elf_alloc_hash(symbol_name, 10000) || !elf_alloc_hash(reloc, 100000)) return NULL; null = elf_create_section(elf, NULL, 0, 0, SHT_NULL, 0, 0); shstrtab = elf_create_section(elf, NULL, 0, 0, SHT_STRTAB, 1, 0); strtab = elf_create_section(elf, NULL, 0, 0, SHT_STRTAB, 1, 0); if (!null || !shstrtab || !strtab) return NULL; null->name = ""; shstrtab->name = ".shstrtab"; strtab->name = ".strtab"; null->sh.sh_name = elf_add_string(elf, shstrtab, null->name); shstrtab->sh.sh_name = elf_add_string(elf, shstrtab, shstrtab->name); strtab->sh.sh_name = elf_add_string(elf, shstrtab, strtab->name); if (null->sh.sh_name == -1 || shstrtab->sh.sh_name == -1 || strtab->sh.sh_name == -1) return NULL; elf_hash_add(section_name, &null->name_hash, str_hash(null->name)); elf_hash_add(section_name, &strtab->name_hash, str_hash(strtab->name)); elf_hash_add(section_name, &shstrtab->name_hash, str_hash(shstrtab->name)); if (elf_add_string(elf, strtab, "") == -1) return NULL; symtab = elf_create_section(elf, ".symtab", 0x18, 0x18, SHT_SYMTAB, 0x8, 0); if (!symtab) return NULL; symtab->sh.sh_link = strtab->idx; symtab->sh.sh_info = 1; elf->ehdr.e_shstrndx = shstrtab->idx; if (!gelf_update_ehdr(elf->elf, &elf->ehdr)) { ERROR_ELF("gelf_update_ehdr"); return NULL; } sym = calloc(1, sizeof(*sym)); if (!sym) { ERROR_GLIBC("calloc"); return NULL; } sym->name = ""; sym->sec = null; elf_add_symbol(elf, sym); return elf; } unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char *str) { unsigned int offset; Loading Loading @@ -1568,7 +1706,7 @@ int elf_write(struct elf *elf) return 0; } void elf_close(struct elf *elf) int elf_close(struct elf *elf) { if (elf->elf) elf_end(elf->elf); Loading @@ -1576,8 +1714,12 @@ void elf_close(struct elf *elf) if (elf->fd > 0) close(elf->fd); if (elf->tmp_name && rename(elf->tmp_name, elf->name)) return -1; /* * NOTE: All remaining allocations are leaked on purpose. Objtool is * about to exit anyway. */ return 0; } tools/objtool/include/objtool/elf.h +3 −2 Original line number Diff line number Diff line Loading @@ -94,7 +94,7 @@ struct elf { GElf_Ehdr ehdr; int fd; bool changed; const char *name; const char *name, *tmp_name; unsigned int num_files; struct list_head sections; unsigned long num_relocs; Loading @@ -116,6 +116,7 @@ struct elf { }; struct elf *elf_open_read(const char *name, int flags); struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name); struct section *elf_create_section(struct elf *elf, const char *name, size_t size, size_t entsize, Loading Loading @@ -165,7 +166,7 @@ int elf_write_insn(struct elf *elf, struct section *sec, unsigned long offset, unsigned int len, const char *insn); int elf_write(struct elf *elf); void elf_close(struct elf *elf); int elf_close(struct elf *elf); struct section *find_section_by_name(const struct elf *elf, const char *name); struct symbol *find_func_by_offset(struct section *sec, unsigned long offset); Loading Loading
tools/objtool/builtin-check.c +1 −1 Original line number Diff line number Diff line Loading @@ -329,5 +329,5 @@ int objtool_run(int argc, const char **argv) if (!opts.dryrun && file->elf->changed && elf_write(file->elf)) return 1; return 0; return elf_close(file->elf); }
tools/objtool/elf.c +143 −1 Original line number Diff line number Diff line Loading @@ -16,6 +16,7 @@ #include <string.h> #include <unistd.h> #include <errno.h> #include <libgen.h> #include <linux/interval_tree_generic.h> #include <objtool/builtin.h> #include <objtool/elf.h> Loading Loading @@ -1067,6 +1068,12 @@ struct elf *elf_open_read(const char *name, int flags) goto err; } elf->name = strdup(name); if (!elf->name) { ERROR_GLIBC("strdup"); return NULL; } if ((flags & O_ACCMODE) == O_RDONLY) cmd = ELF_C_READ_MMAP; else if ((flags & O_ACCMODE) == O_RDWR) Loading Loading @@ -1104,6 +1111,137 @@ struct elf *elf_open_read(const char *name, int flags) return NULL; } struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name) { struct section *null, *symtab, *strtab, *shstrtab; char *dir, *base, *tmp_name; struct symbol *sym; struct elf *elf; elf_version(EV_CURRENT); elf = calloc(1, sizeof(*elf)); if (!elf) { ERROR_GLIBC("calloc"); return NULL; } INIT_LIST_HEAD(&elf->sections); dir = strdup(name); if (!dir) { ERROR_GLIBC("strdup"); return NULL; } dir = dirname(dir); base = strdup(name); if (!base) { ERROR_GLIBC("strdup"); return NULL; } base = basename(base); tmp_name = malloc(256); if (!tmp_name) { ERROR_GLIBC("malloc"); return NULL; } snprintf(tmp_name, 256, "%s/%s.XXXXXX", dir, base); elf->fd = mkstemp(tmp_name); if (elf->fd == -1) { ERROR_GLIBC("can't create tmp file"); exit(1); } elf->tmp_name = tmp_name; elf->name = strdup(name); if (!elf->name) { ERROR_GLIBC("strdup"); return NULL; } elf->elf = elf_begin(elf->fd, ELF_C_WRITE, NULL); if (!elf->elf) { ERROR_ELF("elf_begin"); return NULL; } if (!gelf_newehdr(elf->elf, ELFCLASS64)) { ERROR_ELF("gelf_newehdr"); return NULL; } memcpy(&elf->ehdr, ehdr, sizeof(elf->ehdr)); if (!gelf_update_ehdr(elf->elf, &elf->ehdr)) { ERROR_ELF("gelf_update_ehdr"); return NULL; } if (!elf_alloc_hash(section, 1000) || !elf_alloc_hash(section_name, 1000) || !elf_alloc_hash(symbol, 10000) || !elf_alloc_hash(symbol_name, 10000) || !elf_alloc_hash(reloc, 100000)) return NULL; null = elf_create_section(elf, NULL, 0, 0, SHT_NULL, 0, 0); shstrtab = elf_create_section(elf, NULL, 0, 0, SHT_STRTAB, 1, 0); strtab = elf_create_section(elf, NULL, 0, 0, SHT_STRTAB, 1, 0); if (!null || !shstrtab || !strtab) return NULL; null->name = ""; shstrtab->name = ".shstrtab"; strtab->name = ".strtab"; null->sh.sh_name = elf_add_string(elf, shstrtab, null->name); shstrtab->sh.sh_name = elf_add_string(elf, shstrtab, shstrtab->name); strtab->sh.sh_name = elf_add_string(elf, shstrtab, strtab->name); if (null->sh.sh_name == -1 || shstrtab->sh.sh_name == -1 || strtab->sh.sh_name == -1) return NULL; elf_hash_add(section_name, &null->name_hash, str_hash(null->name)); elf_hash_add(section_name, &strtab->name_hash, str_hash(strtab->name)); elf_hash_add(section_name, &shstrtab->name_hash, str_hash(shstrtab->name)); if (elf_add_string(elf, strtab, "") == -1) return NULL; symtab = elf_create_section(elf, ".symtab", 0x18, 0x18, SHT_SYMTAB, 0x8, 0); if (!symtab) return NULL; symtab->sh.sh_link = strtab->idx; symtab->sh.sh_info = 1; elf->ehdr.e_shstrndx = shstrtab->idx; if (!gelf_update_ehdr(elf->elf, &elf->ehdr)) { ERROR_ELF("gelf_update_ehdr"); return NULL; } sym = calloc(1, sizeof(*sym)); if (!sym) { ERROR_GLIBC("calloc"); return NULL; } sym->name = ""; sym->sec = null; elf_add_symbol(elf, sym); return elf; } unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char *str) { unsigned int offset; Loading Loading @@ -1568,7 +1706,7 @@ int elf_write(struct elf *elf) return 0; } void elf_close(struct elf *elf) int elf_close(struct elf *elf) { if (elf->elf) elf_end(elf->elf); Loading @@ -1576,8 +1714,12 @@ void elf_close(struct elf *elf) if (elf->fd > 0) close(elf->fd); if (elf->tmp_name && rename(elf->tmp_name, elf->name)) return -1; /* * NOTE: All remaining allocations are leaked on purpose. Objtool is * about to exit anyway. */ return 0; }
tools/objtool/include/objtool/elf.h +3 −2 Original line number Diff line number Diff line Loading @@ -94,7 +94,7 @@ struct elf { GElf_Ehdr ehdr; int fd; bool changed; const char *name; const char *name, *tmp_name; unsigned int num_files; struct list_head sections; unsigned long num_relocs; Loading @@ -116,6 +116,7 @@ struct elf { }; struct elf *elf_open_read(const char *name, int flags); struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name); struct section *elf_create_section(struct elf *elf, const char *name, size_t size, size_t entsize, Loading Loading @@ -165,7 +166,7 @@ int elf_write_insn(struct elf *elf, struct section *sec, unsigned long offset, unsigned int len, const char *insn); int elf_write(struct elf *elf); void elf_close(struct elf *elf); int elf_close(struct elf *elf); struct section *find_section_by_name(const struct elf *elf, const char *name); struct symbol *find_func_by_offset(struct section *sec, unsigned long offset); Loading