summaryrefslogtreecommitdiffstats
path: root/llvm/tools/llvm-objcopy/Object.h
diff options
context:
space:
mode:
authorJake Ehrlich <jakehehrlich@google.com>2018-02-14 23:31:33 +0000
committerJake Ehrlich <jakehehrlich@google.com>2018-02-14 23:31:33 +0000
commit6452b11fd895b2af684bfc4b01aacfa77048082d (patch)
tree914975fed6e2201b6731517c628d5e7580e52152 /llvm/tools/llvm-objcopy/Object.h
parent1e368d155844403ad9a82a580c1f796e3001ad26 (diff)
downloadbcm5719-llvm-6452b11fd895b2af684bfc4b01aacfa77048082d.tar.gz
bcm5719-llvm-6452b11fd895b2af684bfc4b01aacfa77048082d.zip
[llvm-objcopy] Fix handling of zero-size segments in llvm-objcopy
Some ELF files produced by lld may have zero-size segment placeholders as shown below. Since GNU_STACK Offset is 0, the current code makes it the lowest used offset, and relocates all the segments over the ELF header. The resulting binary is total garbage. This change fixes how llvm-objcopy handles PT_PHDR properlly by treating ELF headers and the program header table as segments to allow the layout algorithm decide where those should go. Author: vit9696 Differential Revision: https://reviews.llvm.org/D42872 llvm-svn: 325189
Diffstat (limited to 'llvm/tools/llvm-objcopy/Object.h')
-rw-r--r--llvm/tools/llvm-objcopy/Object.h13
1 files changed, 13 insertions, 0 deletions
diff --git a/llvm/tools/llvm-objcopy/Object.h b/llvm/tools/llvm-objcopy/Object.h
index 05f7480b261..88357ad82c2 100644
--- a/llvm/tools/llvm-objcopy/Object.h
+++ b/llvm/tools/llvm-objcopy/Object.h
@@ -238,6 +238,7 @@ public:
Segment *ParentSegment = nullptr;
Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
+ Segment() {}
const SectionBase *firstSection() const {
if (!Sections.empty())
@@ -511,11 +512,14 @@ using object::ELFObjectFile;
template <class ELFT> class ELFBuilder {
private:
+ using Elf_Addr = typename ELFT::Addr;
using Elf_Shdr = typename ELFT::Shdr;
+ using Elf_Ehdr = typename ELFT::Ehdr;
const ELFFile<ELFT> &ElfFile;
Object &Obj;
+ void setParentSegment(Segment &Child);
void readProgramHeaders();
void initSymbolTable(SymbolTableSection *SymTab);
void readSectionHeaders();
@@ -557,6 +561,15 @@ public:
using ConstRange = iterator_range<pointee_iterator<
typename std::vector<std::unique_ptr<T>>::const_iterator>>;
+ // It is often the case that the ELF header and the program header table are
+ // not present in any segment. This could be a problem during file layout,
+ // because other segments may get assigned an offset where either of the
+ // two should reside, which will effectively corrupt the resulting binary.
+ // Other than that we use these segments to track program header offsets
+ // when they may not follow the ELF header.
+ Segment ElfHdrSegment;
+ Segment ProgramHdrSegment;
+
uint8_t Ident[16];
uint64_t Entry;
uint64_t SHOffset;
OpenPOWER on IntegriCloud