[PATCH] D66063: [Object] Create MutableELFObject Class for Doing Mutations on ELFObjectFiles [Part 5]
Alex Brachet via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 10 23:21:36 PDT 2019
abrachet created this revision.
abrachet added reviewers: jhenderson, rupprecht.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Micro patch to add ability to modify the files header
https://reviews.llvm.org/D66063
Files:
llvm/include/llvm/Object/MutableELFObject.h
llvm/unittests/Object/MutableELFObjectTest.cpp
Index: llvm/unittests/Object/MutableELFObjectTest.cpp
===================================================================
--- llvm/unittests/Object/MutableELFObjectTest.cpp
+++ llvm/unittests/Object/MutableELFObjectTest.cpp
@@ -563,3 +563,24 @@
ASSERT_THAT_EXPECTED(NameOrErr, Succeeded());
EXPECT_EQ(*NameOrErr, "newsym");
}
+
+TEST(MutableELFObject, UpdateHeader) {
+ SmallString<0> Storage;
+ Expected<std::unique_ptr<ObjectFile>> ErrOrObj = yaml2ObjectFile(Storage, R"(
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64)");
+
+ ASSERT_THAT_EXPECTED(ErrOrObj, Succeeded());
+ auto *ELFObjFile = dyn_cast<ELFObjectFile<ELF64LE>>(ErrOrObj->get());
+ ASSERT_TRUE(ELFObjFile);
+ MutableELFObject<ELF64LE> MutableObject(std::move(*ELFObjFile));
+
+ Elf_Ehdr_Impl<ELF64LE> &Header = MutableObject.getHeader();
+ EXPECT_EQ(Header.e_type, ELF::ET_REL);
+ Header.e_type = ELF::ET_EXEC;
+ EXPECT_EQ(MutableObject.getHeader().e_type, ELF::ET_EXEC);
+}
Index: llvm/include/llvm/Object/MutableELFObject.h
===================================================================
--- llvm/include/llvm/Object/MutableELFObject.h
+++ llvm/include/llvm/Object/MutableELFObject.h
@@ -216,13 +216,12 @@
using MutableSymbolTable = MutableTable<Elf_Sym, MutableELFSymbol<ELFT>>;
+ Elf_Ehdr FileHeader;
MutableTable<Elf_Shdr, MutableELFSection<ELFT>> Sections;
MutableSymbolTable Symbols;
MutableSymbolTable DynSymbols;
- const Elf_Ehdr &getHeader() const {
- return *reinterpret_cast<const Elf_Ehdr *>(this->base());
- }
+ const Elf_Ehdr &getHeader() const { return FileHeader; }
/// Many getSection* methods in ELFObjectFile use getSection to get the
/// the header associated with that section. This override returns a valid
@@ -259,6 +258,7 @@
public:
explicit MutableELFObject(ELFObjectFile<ELFT> &&B)
: ELFObjectFile<ELFT>(std::move(B)),
+ FileHeader(*reinterpret_cast<const Elf_Ehdr *>(this->base())),
Sections(ArrayRef<Elf_Shdr>(reinterpret_cast<const Elf_Shdr *>(
this->base() + getHeader().e_shoff),
getHeader().e_shnum)),
@@ -376,6 +376,8 @@
/// Removes a section.
void removeSection(section_iterator Sec) { removeSection(*Sec); }
+
+ Elf_Ehdr &getHeader() { return FileHeader; }
};
template <typename ELFT>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66063.214552.patch
Type: text/x-patch
Size: 2431 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190811/6038eb13/attachment.bin>
More information about the llvm-commits
mailing list