2017-11-01 21:16:06 +00:00
|
|
|
//===- Object.cpp ---------------------------------------------------------===//
|
2017-08-01 00:33:58 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-08-01 00:33:58 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-11-01 21:16:06 +00:00
|
|
|
|
2017-08-01 00:33:58 +00:00
|
|
|
#include "Object.h"
|
|
|
|
#include "llvm-objcopy.h"
|
2017-11-01 21:16:06 +00:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/ADT/iterator_range.h"
|
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2018-09-07 08:10:22 +00:00
|
|
|
#include "llvm/MC/MCTargetOptions.h"
|
2017-11-01 21:16:06 +00:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2018-09-07 08:10:22 +00:00
|
|
|
#include "llvm/Support/Compression.h"
|
2019-02-01 15:20:36 +00:00
|
|
|
#include "llvm/Support/Errc.h"
|
2017-11-01 21:16:06 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
2018-01-25 22:15:14 +00:00
|
|
|
#include "llvm/Support/Path.h"
|
2017-11-01 21:16:06 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iterator>
|
[llvm-objcopy] Make removeSectionReferences batched
Summary:
Removing a large number of sections from a file with a lot of symbols can have abysmal (i.e. O(n^2)) performance, e.g. when running `--only-section` to extract one section out of a large file.
This comes from iterating over all symbols in the symbol table each time we remove a section, to remove references to the section we just removed.
Instead, do just one pass of symbol removal by passing a hash set of all the sections we'd like to remove references to.
This fixes a regression when running llvm-objcopy -j <one section> on an object file with many sections and symbols -- on my machine, running `objcopy -j .keep_me huge-input.o /tmp/foo.o` takes .3s with GNU objcopy, 1.3s with an updated llvm-objcopy, and 7+ minutes with llvm-objcopy prior to this patch.
Reviewers: MaskRay, jhenderson, jakehehrlich, alexshap, espindola
Reviewed By: MaskRay, jhenderson
Subscribers: echristo, emaste, arichardson, mgrang, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D58296
llvm-svn: 354597
2019-02-21 16:45:42 +00:00
|
|
|
#include <unordered_set>
|
2017-11-01 21:16:06 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2017-08-01 00:33:58 +00:00
|
|
|
|
2018-10-24 22:49:06 +00:00
|
|
|
namespace llvm {
|
|
|
|
namespace objcopy {
|
|
|
|
namespace elf {
|
|
|
|
|
2017-08-01 00:33:58 +00:00
|
|
|
using namespace object;
|
|
|
|
using namespace ELF;
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
template <class ELFT> void ELFWriter<ELFT>::writePhdr(const Segment &Seg) {
|
2018-07-06 17:51:03 +00:00
|
|
|
uint8_t *B = Buf.getBufferStart();
|
|
|
|
B += Obj.ProgramHdrSegment.Offset + Seg.Index * sizeof(Elf_Phdr);
|
|
|
|
Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(B);
|
2018-01-25 22:46:17 +00:00
|
|
|
Phdr.p_type = Seg.Type;
|
|
|
|
Phdr.p_flags = Seg.Flags;
|
|
|
|
Phdr.p_offset = Seg.Offset;
|
|
|
|
Phdr.p_vaddr = Seg.VAddr;
|
|
|
|
Phdr.p_paddr = Seg.PAddr;
|
|
|
|
Phdr.p_filesz = Seg.FileSize;
|
|
|
|
Phdr.p_memsz = Seg.MemSize;
|
|
|
|
Phdr.p_align = Seg.Align;
|
2017-08-04 21:09:26 +00:00
|
|
|
}
|
|
|
|
|
[llvm-objcopy] Make removeSectionReferences batched
Summary:
Removing a large number of sections from a file with a lot of symbols can have abysmal (i.e. O(n^2)) performance, e.g. when running `--only-section` to extract one section out of a large file.
This comes from iterating over all symbols in the symbol table each time we remove a section, to remove references to the section we just removed.
Instead, do just one pass of symbol removal by passing a hash set of all the sections we'd like to remove references to.
This fixes a regression when running llvm-objcopy -j <one section> on an object file with many sections and symbols -- on my machine, running `objcopy -j .keep_me huge-input.o /tmp/foo.o` takes .3s with GNU objcopy, 1.3s with an updated llvm-objcopy, and 7+ minutes with llvm-objcopy prior to this patch.
Reviewers: MaskRay, jhenderson, jakehehrlich, alexshap, espindola
Reviewed By: MaskRay, jhenderson
Subscribers: echristo, emaste, arichardson, mgrang, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D58296
llvm-svn: 354597
2019-02-21 16:45:42 +00:00
|
|
|
Error SectionBase::removeSectionReferences(
|
|
|
|
function_ref<bool(const SectionBase *)> ToRemove) {
|
2019-02-01 15:20:36 +00:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
Error SectionBase::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2017-09-25 20:37:28 +00:00
|
|
|
void SectionBase::initialize(SectionTableRef SecTable) {}
|
2017-08-01 00:33:58 +00:00
|
|
|
void SectionBase::finalize() {}
|
2018-05-25 11:01:25 +00:00
|
|
|
void SectionBase::markSymbols() {}
|
2019-03-11 11:01:24 +00:00
|
|
|
void SectionBase::replaceSectionReferences(
|
|
|
|
const DenseMap<SectionBase *, SectionBase *> &) {}
|
2017-08-01 00:33:58 +00:00
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
template <class ELFT> void ELFWriter<ELFT>::writeShdr(const SectionBase &Sec) {
|
2018-07-06 17:51:03 +00:00
|
|
|
uint8_t *B = Buf.getBufferStart();
|
|
|
|
B += Sec.HeaderOffset;
|
2018-08-10 16:25:58 +00:00
|
|
|
Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B);
|
2018-01-25 22:46:17 +00:00
|
|
|
Shdr.sh_name = Sec.NameIndex;
|
|
|
|
Shdr.sh_type = Sec.Type;
|
|
|
|
Shdr.sh_flags = Sec.Flags;
|
|
|
|
Shdr.sh_addr = Sec.Addr;
|
|
|
|
Shdr.sh_offset = Sec.Offset;
|
|
|
|
Shdr.sh_size = Sec.Size;
|
|
|
|
Shdr.sh_link = Sec.Link;
|
|
|
|
Shdr.sh_info = Sec.Info;
|
|
|
|
Shdr.sh_addralign = Sec.Align;
|
|
|
|
Shdr.sh_entsize = Sec.EntrySize;
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
template <class ELFT> void ELFSectionSizer<ELFT>::visit(Section &Sec) {}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void ELFSectionSizer<ELFT>::visit(OwnedDataSection &Sec) {}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void ELFSectionSizer<ELFT>::visit(StringTableSection &Sec) {}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void ELFSectionSizer<ELFT>::visit(DynamicRelocationSection &Sec) {}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void ELFSectionSizer<ELFT>::visit(SymbolTableSection &Sec) {
|
|
|
|
Sec.EntrySize = sizeof(Elf_Sym);
|
|
|
|
Sec.Size = Sec.Symbols.size() * Sec.EntrySize;
|
2019-01-03 17:51:32 +00:00
|
|
|
// Align to the largest field in Elf_Sym.
|
2019-01-03 19:09:00 +00:00
|
|
|
Sec.Align = ELFT::Is64Bits ? sizeof(Elf_Xword) : sizeof(Elf_Word);
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void ELFSectionSizer<ELFT>::visit(RelocationSection &Sec) {
|
|
|
|
Sec.EntrySize = Sec.Type == SHT_REL ? sizeof(Elf_Rel) : sizeof(Elf_Rela);
|
|
|
|
Sec.Size = Sec.Relocations.size() * Sec.EntrySize;
|
2019-01-03 17:51:32 +00:00
|
|
|
// Align to the largest field in Elf_Rel(a).
|
2019-01-03 19:09:00 +00:00
|
|
|
Sec.Align = ELFT::Is64Bits ? sizeof(Elf_Xword) : sizeof(Elf_Word);
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void ELFSectionSizer<ELFT>::visit(GnuDebugLinkSection &Sec) {}
|
|
|
|
|
|
|
|
template <class ELFT> void ELFSectionSizer<ELFT>::visit(GroupSection &Sec) {}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void ELFSectionSizer<ELFT>::visit(SectionIndexSection &Sec) {}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void ELFSectionSizer<ELFT>::visit(CompressedSection &Sec) {}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void ELFSectionSizer<ELFT>::visit(DecompressedSection &Sec) {}
|
2018-01-25 22:46:17 +00:00
|
|
|
|
2018-07-16 19:48:52 +00:00
|
|
|
void BinarySectionWriter::visit(const SectionIndexSection &Sec) {
|
|
|
|
error("Cannot write symbol section index table '" + Sec.Name + "' ");
|
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
void BinarySectionWriter::visit(const SymbolTableSection &Sec) {
|
|
|
|
error("Cannot write symbol table '" + Sec.Name + "' out to binary");
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinarySectionWriter::visit(const RelocationSection &Sec) {
|
|
|
|
error("Cannot write relocation section '" + Sec.Name + "' out to binary");
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) {
|
2018-03-21 19:53:44 +00:00
|
|
|
error("Cannot write '" + Sec.Name + "' out to binary");
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinarySectionWriter::visit(const GroupSection &Sec) {
|
|
|
|
error("Cannot write '" + Sec.Name + "' out to binary");
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SectionWriter::visit(const Section &Sec) {
|
|
|
|
if (Sec.Type == SHT_NOBITS)
|
2017-08-01 00:33:58 +00:00
|
|
|
return;
|
2018-01-25 22:46:17 +00:00
|
|
|
uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
|
2018-11-17 01:44:25 +00:00
|
|
|
llvm::copy(Sec.Contents, Buf);
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
void Section::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); }
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
void Section::accept(MutableSectionVisitor &Visitor) { Visitor.visit(*this); }
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
void SectionWriter::visit(const OwnedDataSection &Sec) {
|
|
|
|
uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
|
2018-11-17 01:44:25 +00:00
|
|
|
llvm::copy(Sec.Data, Buf);
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 10:29:41 +00:00
|
|
|
static const std::vector<uint8_t> ZlibGnuMagic = {'Z', 'L', 'I', 'B'};
|
|
|
|
|
|
|
|
static bool isDataGnuCompressed(ArrayRef<uint8_t> Data) {
|
|
|
|
return Data.size() > ZlibGnuMagic.size() &&
|
|
|
|
std::equal(ZlibGnuMagic.begin(), ZlibGnuMagic.end(), Data.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
static std::tuple<uint64_t, uint64_t>
|
|
|
|
getDecompressedSizeAndAlignment(ArrayRef<uint8_t> Data) {
|
|
|
|
const bool IsGnuDebug = isDataGnuCompressed(Data);
|
|
|
|
const uint64_t DecompressedSize =
|
|
|
|
IsGnuDebug
|
|
|
|
? support::endian::read64be(reinterpret_cast<const uint64_t *>(
|
|
|
|
Data.data() + ZlibGnuMagic.size()))
|
|
|
|
: reinterpret_cast<const Elf_Chdr_Impl<ELFT> *>(Data.data())->ch_size;
|
|
|
|
const uint64_t DecompressedAlign =
|
|
|
|
IsGnuDebug ? 1
|
|
|
|
: reinterpret_cast<const Elf_Chdr_Impl<ELFT> *>(Data.data())
|
|
|
|
->ch_addralign;
|
|
|
|
|
|
|
|
return std::make_tuple(DecompressedSize, DecompressedAlign);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void ELFSectionWriter<ELFT>::visit(const DecompressedSection &Sec) {
|
|
|
|
const size_t DataOffset = isDataGnuCompressed(Sec.OriginalData)
|
|
|
|
? (ZlibGnuMagic.size() + sizeof(Sec.Size))
|
|
|
|
: sizeof(Elf_Chdr_Impl<ELFT>);
|
|
|
|
|
|
|
|
StringRef CompressedContent(
|
|
|
|
reinterpret_cast<const char *>(Sec.OriginalData.data()) + DataOffset,
|
|
|
|
Sec.OriginalData.size() - DataOffset);
|
|
|
|
|
|
|
|
SmallVector<char, 128> DecompressedContent;
|
|
|
|
if (Error E = zlib::uncompress(CompressedContent, DecompressedContent,
|
|
|
|
static_cast<size_t>(Sec.Size)))
|
|
|
|
reportError(Sec.Name, std::move(E));
|
|
|
|
|
2019-03-06 14:12:18 +00:00
|
|
|
uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
|
2018-10-01 10:29:41 +00:00
|
|
|
std::copy(DecompressedContent.begin(), DecompressedContent.end(), Buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinarySectionWriter::visit(const DecompressedSection &Sec) {
|
|
|
|
error("Cannot write compressed section '" + Sec.Name + "' ");
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecompressedSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
Visitor.visit(*this);
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
void DecompressedSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
Visitor.visit(*this);
|
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
void OwnedDataSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
Visitor.visit(*this);
|
2017-12-19 00:47:30 +00:00
|
|
|
}
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
void OwnedDataSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
Visitor.visit(*this);
|
|
|
|
}
|
|
|
|
|
2018-09-07 08:10:22 +00:00
|
|
|
void BinarySectionWriter::visit(const CompressedSection &Sec) {
|
|
|
|
error("Cannot write compressed section '" + Sec.Name + "' ");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
void ELFSectionWriter<ELFT>::visit(const CompressedSection &Sec) {
|
|
|
|
uint8_t *Buf = Out.getBufferStart();
|
|
|
|
Buf += Sec.Offset;
|
|
|
|
|
|
|
|
if (Sec.CompressionType == DebugCompressionType::None) {
|
|
|
|
std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Sec.CompressionType == DebugCompressionType::GNU) {
|
|
|
|
const char *Magic = "ZLIB";
|
|
|
|
memcpy(Buf, Magic, strlen(Magic));
|
|
|
|
Buf += strlen(Magic);
|
|
|
|
const uint64_t DecompressedSize =
|
|
|
|
support::endian::read64be(&Sec.DecompressedSize);
|
|
|
|
memcpy(Buf, &DecompressedSize, sizeof(DecompressedSize));
|
|
|
|
Buf += sizeof(DecompressedSize);
|
|
|
|
} else {
|
|
|
|
Elf_Chdr_Impl<ELFT> Chdr;
|
|
|
|
Chdr.ch_type = ELF::ELFCOMPRESS_ZLIB;
|
|
|
|
Chdr.ch_size = Sec.DecompressedSize;
|
|
|
|
Chdr.ch_addralign = Sec.DecompressedAlign;
|
|
|
|
memcpy(Buf, &Chdr, sizeof(Chdr));
|
|
|
|
Buf += sizeof(Chdr);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::copy(Sec.CompressedData.begin(), Sec.CompressedData.end(), Buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
CompressedSection::CompressedSection(const SectionBase &Sec,
|
|
|
|
DebugCompressionType CompressionType)
|
|
|
|
: SectionBase(Sec), CompressionType(CompressionType),
|
|
|
|
DecompressedSize(Sec.OriginalData.size()), DecompressedAlign(Sec.Align) {
|
|
|
|
if (Error E = zlib::compress(
|
|
|
|
StringRef(reinterpret_cast<const char *>(OriginalData.data()),
|
|
|
|
OriginalData.size()),
|
|
|
|
CompressedData))
|
|
|
|
reportError(Name, std::move(E));
|
|
|
|
|
|
|
|
size_t ChdrSize;
|
|
|
|
if (CompressionType == DebugCompressionType::GNU) {
|
|
|
|
Name = ".z" + Sec.Name.substr(1);
|
|
|
|
ChdrSize = sizeof("ZLIB") - 1 + sizeof(uint64_t);
|
|
|
|
} else {
|
|
|
|
Flags |= ELF::SHF_COMPRESSED;
|
|
|
|
ChdrSize =
|
|
|
|
std::max(std::max(sizeof(object::Elf_Chdr_Impl<object::ELF64LE>),
|
|
|
|
sizeof(object::Elf_Chdr_Impl<object::ELF64BE>)),
|
|
|
|
std::max(sizeof(object::Elf_Chdr_Impl<object::ELF32LE>),
|
|
|
|
sizeof(object::Elf_Chdr_Impl<object::ELF32BE>)));
|
|
|
|
}
|
|
|
|
Size = ChdrSize + CompressedData.size();
|
|
|
|
Align = 8;
|
|
|
|
}
|
|
|
|
|
2018-10-01 10:29:41 +00:00
|
|
|
CompressedSection::CompressedSection(ArrayRef<uint8_t> CompressedData,
|
|
|
|
uint64_t DecompressedSize,
|
|
|
|
uint64_t DecompressedAlign)
|
|
|
|
: CompressionType(DebugCompressionType::None),
|
|
|
|
DecompressedSize(DecompressedSize), DecompressedAlign(DecompressedAlign) {
|
|
|
|
OriginalData = CompressedData;
|
|
|
|
}
|
|
|
|
|
2018-09-07 08:10:22 +00:00
|
|
|
void CompressedSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
Visitor.visit(*this);
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
void CompressedSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
Visitor.visit(*this);
|
|
|
|
}
|
|
|
|
|
2019-03-18 14:27:41 +00:00
|
|
|
void StringTableSection::addString(StringRef Name) { StrTabBuilder.add(Name); }
|
2017-08-01 00:33:58 +00:00
|
|
|
|
|
|
|
uint32_t StringTableSection::findIndex(StringRef Name) const {
|
|
|
|
return StrTabBuilder.getOffset(Name);
|
|
|
|
}
|
|
|
|
|
2019-03-18 14:27:41 +00:00
|
|
|
void StringTableSection::prepareForLayout() {
|
|
|
|
StrTabBuilder.finalize();
|
|
|
|
Size = StrTabBuilder.getSize();
|
|
|
|
}
|
2017-08-01 00:33:58 +00:00
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
void SectionWriter::visit(const StringTableSection &Sec) {
|
|
|
|
Sec.StrTabBuilder.write(Out.getBufferStart() + Sec.Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTableSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
Visitor.visit(*this);
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
void StringTableSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
Visitor.visit(*this);
|
|
|
|
}
|
|
|
|
|
2018-07-16 19:48:52 +00:00
|
|
|
template <class ELFT>
|
|
|
|
void ELFSectionWriter<ELFT>::visit(const SectionIndexSection &Sec) {
|
|
|
|
uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
|
2018-08-10 16:25:58 +00:00
|
|
|
auto *IndexesBuffer = reinterpret_cast<Elf_Word *>(Buf);
|
2018-11-17 01:44:25 +00:00
|
|
|
llvm::copy(Sec.Indexes, IndexesBuffer);
|
2018-07-16 19:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SectionIndexSection::initialize(SectionTableRef SecTable) {
|
|
|
|
Size = 0;
|
|
|
|
setSymTab(SecTable.getSectionOfType<SymbolTableSection>(
|
|
|
|
Link,
|
|
|
|
"Link field value " + Twine(Link) + " in section " + Name + " is invalid",
|
|
|
|
"Link field value " + Twine(Link) + " in section " + Name +
|
|
|
|
" is not a symbol table"));
|
|
|
|
Symbols->setShndxTable(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SectionIndexSection::finalize() { Link = Symbols->Index; }
|
|
|
|
|
|
|
|
void SectionIndexSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
Visitor.visit(*this);
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
void SectionIndexSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
Visitor.visit(*this);
|
|
|
|
}
|
|
|
|
|
2017-09-13 03:04:50 +00:00
|
|
|
static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
|
2017-09-07 23:02:50 +00:00
|
|
|
switch (Index) {
|
|
|
|
case SHN_ABS:
|
|
|
|
case SHN_COMMON:
|
|
|
|
return true;
|
|
|
|
}
|
2017-09-13 03:04:50 +00:00
|
|
|
if (Machine == EM_HEXAGON) {
|
|
|
|
switch (Index) {
|
|
|
|
case SHN_HEXAGON_SCOMMON:
|
|
|
|
case SHN_HEXAGON_SCOMMON_2:
|
|
|
|
case SHN_HEXAGON_SCOMMON_4:
|
|
|
|
case SHN_HEXAGON_SCOMMON_8:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2017-09-07 23:02:50 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 19:48:52 +00:00
|
|
|
// Large indexes force us to clarify exactly what this function should do. This
|
|
|
|
// function should return the value that will appear in st_shndx when written
|
|
|
|
// out.
|
2017-09-07 23:02:50 +00:00
|
|
|
uint16_t Symbol::getShndx() const {
|
|
|
|
if (DefinedIn != nullptr) {
|
2018-07-16 19:48:52 +00:00
|
|
|
if (DefinedIn->Index >= SHN_LORESERVE)
|
|
|
|
return SHN_XINDEX;
|
2017-09-07 23:02:50 +00:00
|
|
|
return DefinedIn->Index;
|
|
|
|
}
|
|
|
|
switch (ShndxType) {
|
|
|
|
// This means that we don't have a defined section but we do need to
|
|
|
|
// output a legitimate section index.
|
|
|
|
case SYMBOL_SIMPLE_INDEX:
|
|
|
|
return SHN_UNDEF;
|
|
|
|
case SYMBOL_ABS:
|
|
|
|
case SYMBOL_COMMON:
|
|
|
|
case SYMBOL_HEXAGON_SCOMMON:
|
|
|
|
case SYMBOL_HEXAGON_SCOMMON_2:
|
|
|
|
case SYMBOL_HEXAGON_SCOMMON_4:
|
|
|
|
case SYMBOL_HEXAGON_SCOMMON_8:
|
2018-07-16 19:48:52 +00:00
|
|
|
case SYMBOL_XINDEX:
|
2017-09-07 23:02:50 +00:00
|
|
|
return static_cast<uint16_t>(ShndxType);
|
|
|
|
}
|
|
|
|
llvm_unreachable("Symbol with invalid ShndxType encountered");
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy] Don't apply --localize flags to common symbols
Summary:
--localize-symbol and --localize-hidden will currently localize common symbols. GNU objcopy will not localize these symbols even when explicitly requested, which seems reasonable; common symbols should always be global so they can be merged during linking.
See PR39461
Reviewers: jakehehrlich, jhenderson, alexshap, MaskRay, espindola
Reviewed By: jakehehrlich, jhenderson, alexshap, MaskRay
Subscribers: emaste, arichardson, alexshap, MaskRay, llvm-commits
Differential Revision: https://reviews.llvm.org/D53782
llvm-svn: 345856
2018-11-01 17:26:36 +00:00
|
|
|
bool Symbol::isCommon() const { return getShndx() == SHN_COMMON; }
|
|
|
|
|
2018-03-21 19:53:44 +00:00
|
|
|
void SymbolTableSection::assignIndices() {
|
|
|
|
uint32_t Index = 0;
|
|
|
|
for (auto &Sym : Symbols)
|
|
|
|
Sym->Index = Index++;
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
void SymbolTableSection::addSymbol(Twine Name, uint8_t Bind, uint8_t Type,
|
2017-08-29 02:12:03 +00:00
|
|
|
SectionBase *DefinedIn, uint64_t Value,
|
2018-01-02 23:01:24 +00:00
|
|
|
uint8_t Visibility, uint16_t Shndx,
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
uint64_t Size) {
|
2017-08-29 02:12:03 +00:00
|
|
|
Symbol Sym;
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
Sym.Name = Name.str();
|
2017-08-29 02:12:03 +00:00
|
|
|
Sym.Binding = Bind;
|
|
|
|
Sym.Type = Type;
|
|
|
|
Sym.DefinedIn = DefinedIn;
|
2018-07-16 19:48:52 +00:00
|
|
|
if (DefinedIn != nullptr)
|
|
|
|
DefinedIn->HasSymbol = true;
|
2018-03-07 20:33:02 +00:00
|
|
|
if (DefinedIn == nullptr) {
|
|
|
|
if (Shndx >= SHN_LORESERVE)
|
|
|
|
Sym.ShndxType = static_cast<SymbolShndxType>(Shndx);
|
|
|
|
else
|
|
|
|
Sym.ShndxType = SYMBOL_SIMPLE_INDEX;
|
|
|
|
}
|
2017-08-29 02:12:03 +00:00
|
|
|
Sym.Value = Value;
|
2018-01-02 23:01:24 +00:00
|
|
|
Sym.Visibility = Visibility;
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
Sym.Size = Size;
|
2017-08-29 02:12:03 +00:00
|
|
|
Sym.Index = Symbols.size();
|
|
|
|
Symbols.emplace_back(llvm::make_unique<Symbol>(Sym));
|
|
|
|
Size += this->EntrySize;
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy] Make removeSectionReferences batched
Summary:
Removing a large number of sections from a file with a lot of symbols can have abysmal (i.e. O(n^2)) performance, e.g. when running `--only-section` to extract one section out of a large file.
This comes from iterating over all symbols in the symbol table each time we remove a section, to remove references to the section we just removed.
Instead, do just one pass of symbol removal by passing a hash set of all the sections we'd like to remove references to.
This fixes a regression when running llvm-objcopy -j <one section> on an object file with many sections and symbols -- on my machine, running `objcopy -j .keep_me huge-input.o /tmp/foo.o` takes .3s with GNU objcopy, 1.3s with an updated llvm-objcopy, and 7+ minutes with llvm-objcopy prior to this patch.
Reviewers: MaskRay, jhenderson, jakehehrlich, alexshap, espindola
Reviewed By: MaskRay, jhenderson
Subscribers: echristo, emaste, arichardson, mgrang, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D58296
llvm-svn: 354597
2019-02-21 16:45:42 +00:00
|
|
|
Error SymbolTableSection::removeSectionReferences(
|
|
|
|
function_ref<bool(const SectionBase *)> ToRemove) {
|
|
|
|
if (ToRemove(SectionIndexTable))
|
2018-07-16 19:48:52 +00:00
|
|
|
SectionIndexTable = nullptr;
|
[llvm-objcopy] Make removeSectionReferences batched
Summary:
Removing a large number of sections from a file with a lot of symbols can have abysmal (i.e. O(n^2)) performance, e.g. when running `--only-section` to extract one section out of a large file.
This comes from iterating over all symbols in the symbol table each time we remove a section, to remove references to the section we just removed.
Instead, do just one pass of symbol removal by passing a hash set of all the sections we'd like to remove references to.
This fixes a regression when running llvm-objcopy -j <one section> on an object file with many sections and symbols -- on my machine, running `objcopy -j .keep_me huge-input.o /tmp/foo.o` takes .3s with GNU objcopy, 1.3s with an updated llvm-objcopy, and 7+ minutes with llvm-objcopy prior to this patch.
Reviewers: MaskRay, jhenderson, jakehehrlich, alexshap, espindola
Reviewed By: MaskRay, jhenderson
Subscribers: echristo, emaste, arichardson, mgrang, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D58296
llvm-svn: 354597
2019-02-21 16:45:42 +00:00
|
|
|
if (ToRemove(SymbolNames))
|
2019-02-01 15:20:36 +00:00
|
|
|
return createStringError(llvm::errc::invalid_argument,
|
|
|
|
"String table %s cannot be removed because it is "
|
|
|
|
"referenced by the symbol table %s",
|
|
|
|
SymbolNames->Name.data(), this->Name.data());
|
|
|
|
return removeSymbols(
|
[llvm-objcopy] Make removeSectionReferences batched
Summary:
Removing a large number of sections from a file with a lot of symbols can have abysmal (i.e. O(n^2)) performance, e.g. when running `--only-section` to extract one section out of a large file.
This comes from iterating over all symbols in the symbol table each time we remove a section, to remove references to the section we just removed.
Instead, do just one pass of symbol removal by passing a hash set of all the sections we'd like to remove references to.
This fixes a regression when running llvm-objcopy -j <one section> on an object file with many sections and symbols -- on my machine, running `objcopy -j .keep_me huge-input.o /tmp/foo.o` takes .3s with GNU objcopy, 1.3s with an updated llvm-objcopy, and 7+ minutes with llvm-objcopy prior to this patch.
Reviewers: MaskRay, jhenderson, jakehehrlich, alexshap, espindola
Reviewed By: MaskRay, jhenderson
Subscribers: echristo, emaste, arichardson, mgrang, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D58296
llvm-svn: 354597
2019-02-21 16:45:42 +00:00
|
|
|
[ToRemove](const Symbol &Sym) { return ToRemove(Sym.DefinedIn); });
|
2017-10-10 18:47:09 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 18:28:17 +00:00
|
|
|
void SymbolTableSection::updateSymbols(function_ref<void(Symbol &)> Callable) {
|
2018-06-01 16:19:46 +00:00
|
|
|
std::for_each(std::begin(Symbols) + 1, std::end(Symbols),
|
|
|
|
[Callable](SymPtr &Sym) { Callable(*Sym); });
|
2018-01-05 19:19:09 +00:00
|
|
|
std::stable_partition(
|
|
|
|
std::begin(Symbols), std::end(Symbols),
|
|
|
|
[](const SymPtr &Sym) { return Sym->Binding == STB_LOCAL; });
|
2018-03-21 19:53:44 +00:00
|
|
|
assignIndices();
|
2018-01-05 19:19:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 15:20:36 +00:00
|
|
|
Error SymbolTableSection::removeSymbols(
|
2018-05-09 21:36:54 +00:00
|
|
|
function_ref<bool(const Symbol &)> ToRemove) {
|
2018-05-02 20:19:22 +00:00
|
|
|
Symbols.erase(
|
2018-06-01 16:19:46 +00:00
|
|
|
std::remove_if(std::begin(Symbols) + 1, std::end(Symbols),
|
2018-05-02 20:19:22 +00:00
|
|
|
[ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }),
|
|
|
|
std::end(Symbols));
|
|
|
|
Size = Symbols.size() * EntrySize;
|
|
|
|
assignIndices();
|
2019-02-01 15:20:36 +00:00
|
|
|
return Error::success();
|
2018-05-02 20:19:22 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 13:57:47 +00:00
|
|
|
void SymbolTableSection::replaceSectionReferences(
|
|
|
|
const DenseMap<SectionBase *, SectionBase *> &FromTo) {
|
|
|
|
for (std::unique_ptr<Symbol> &Sym : Symbols)
|
|
|
|
if (SectionBase *To = FromTo.lookup(Sym->DefinedIn))
|
|
|
|
Sym->DefinedIn = To;
|
|
|
|
}
|
|
|
|
|
2017-09-25 20:37:28 +00:00
|
|
|
void SymbolTableSection::initialize(SectionTableRef SecTable) {
|
|
|
|
Size = 0;
|
|
|
|
setStrTab(SecTable.getSectionOfType<StringTableSection>(
|
|
|
|
Link,
|
|
|
|
"Symbol table has link index of " + Twine(Link) +
|
|
|
|
" which is not a valid index",
|
|
|
|
"Symbol table has link index of " + Twine(Link) +
|
|
|
|
" which is not a string table"));
|
|
|
|
}
|
|
|
|
|
2017-08-29 02:12:03 +00:00
|
|
|
void SymbolTableSection::finalize() {
|
|
|
|
uint32_t MaxLocalIndex = 0;
|
|
|
|
for (auto &Sym : Symbols) {
|
|
|
|
Sym->NameIndex = SymbolNames->findIndex(Sym->Name);
|
|
|
|
if (Sym->Binding == STB_LOCAL)
|
|
|
|
MaxLocalIndex = std::max(MaxLocalIndex, Sym->Index);
|
|
|
|
}
|
|
|
|
// Now we need to set the Link and Info fields.
|
|
|
|
Link = SymbolNames->Index;
|
|
|
|
Info = MaxLocalIndex + 1;
|
|
|
|
}
|
|
|
|
|
2018-07-16 19:48:52 +00:00
|
|
|
void SymbolTableSection::prepareForLayout() {
|
|
|
|
// Add all potential section indexes before file layout so that the section
|
|
|
|
// index section has the approprite size.
|
|
|
|
if (SectionIndexTable != nullptr) {
|
|
|
|
for (const auto &Sym : Symbols) {
|
|
|
|
if (Sym->DefinedIn != nullptr && Sym->DefinedIn->Index >= SHN_LORESERVE)
|
|
|
|
SectionIndexTable->addIndex(Sym->DefinedIn->Index);
|
|
|
|
else
|
|
|
|
SectionIndexTable->addIndex(SHN_UNDEF);
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 02:12:03 +00:00
|
|
|
// Add all of our strings to SymbolNames so that SymbolNames has the right
|
|
|
|
// size before layout is decided.
|
|
|
|
for (auto &Sym : Symbols)
|
|
|
|
SymbolNames->addString(Sym->Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const {
|
|
|
|
if (Symbols.size() <= Index)
|
|
|
|
error("Invalid symbol index: " + Twine(Index));
|
|
|
|
return Symbols[Index].get();
|
|
|
|
}
|
|
|
|
|
2018-05-25 11:01:25 +00:00
|
|
|
Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) {
|
|
|
|
return const_cast<Symbol *>(
|
|
|
|
static_cast<const SymbolTableSection *>(this)->getSymbolByIndex(Index));
|
|
|
|
}
|
|
|
|
|
2017-08-29 02:12:03 +00:00
|
|
|
template <class ELFT>
|
2018-01-25 22:46:17 +00:00
|
|
|
void ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) {
|
2017-08-29 02:12:03 +00:00
|
|
|
uint8_t *Buf = Out.getBufferStart();
|
2018-01-25 22:46:17 +00:00
|
|
|
Buf += Sec.Offset;
|
2018-08-10 16:25:58 +00:00
|
|
|
Elf_Sym *Sym = reinterpret_cast<Elf_Sym *>(Buf);
|
2017-08-29 02:12:03 +00:00
|
|
|
// Loop though symbols setting each entry of the symbol table.
|
2018-01-25 22:46:17 +00:00
|
|
|
for (auto &Symbol : Sec.Symbols) {
|
2017-08-29 02:12:03 +00:00
|
|
|
Sym->st_name = Symbol->NameIndex;
|
|
|
|
Sym->st_value = Symbol->Value;
|
|
|
|
Sym->st_size = Symbol->Size;
|
2018-01-02 23:01:24 +00:00
|
|
|
Sym->st_other = Symbol->Visibility;
|
2017-08-29 02:12:03 +00:00
|
|
|
Sym->setBinding(Symbol->Binding);
|
|
|
|
Sym->setType(Symbol->Type);
|
2017-09-07 23:02:50 +00:00
|
|
|
Sym->st_shndx = Symbol->getShndx();
|
2017-08-29 02:12:03 +00:00
|
|
|
++Sym;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
void SymbolTableSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
Visitor.visit(*this);
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
void SymbolTableSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
Visitor.visit(*this);
|
|
|
|
}
|
|
|
|
|
2019-02-27 11:18:27 +00:00
|
|
|
Error RelocationSection::removeSectionReferences(
|
[llvm-objcopy] Make removeSectionReferences batched
Summary:
Removing a large number of sections from a file with a lot of symbols can have abysmal (i.e. O(n^2)) performance, e.g. when running `--only-section` to extract one section out of a large file.
This comes from iterating over all symbols in the symbol table each time we remove a section, to remove references to the section we just removed.
Instead, do just one pass of symbol removal by passing a hash set of all the sections we'd like to remove references to.
This fixes a regression when running llvm-objcopy -j <one section> on an object file with many sections and symbols -- on my machine, running `objcopy -j .keep_me huge-input.o /tmp/foo.o` takes .3s with GNU objcopy, 1.3s with an updated llvm-objcopy, and 7+ minutes with llvm-objcopy prior to this patch.
Reviewers: MaskRay, jhenderson, jakehehrlich, alexshap, espindola
Reviewed By: MaskRay, jhenderson
Subscribers: echristo, emaste, arichardson, mgrang, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D58296
llvm-svn: 354597
2019-02-21 16:45:42 +00:00
|
|
|
function_ref<bool(const SectionBase *)> ToRemove) {
|
|
|
|
if (ToRemove(Symbols))
|
2019-02-01 15:20:36 +00:00
|
|
|
return createStringError(llvm::errc::invalid_argument,
|
|
|
|
"Symbol table %s cannot be removed because it is "
|
|
|
|
"referenced by the relocation section %s.",
|
|
|
|
Symbols->Name.data(), this->Name.data());
|
2019-02-27 11:18:27 +00:00
|
|
|
|
|
|
|
for (const Relocation &R : Relocations) {
|
|
|
|
if (!R.RelocSymbol->DefinedIn || !ToRemove(R.RelocSymbol->DefinedIn))
|
|
|
|
continue;
|
2019-02-28 08:21:50 +00:00
|
|
|
return createStringError(llvm::errc::invalid_argument,
|
|
|
|
"Section %s can't be removed: (%s+0x%" PRIx64
|
|
|
|
") has relocation against symbol '%s'",
|
|
|
|
R.RelocSymbol->DefinedIn->Name.data(),
|
|
|
|
SecToApplyRel->Name.data(), R.Offset,
|
|
|
|
R.RelocSymbol->Name.c_str());
|
2019-02-27 11:18:27 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 15:20:36 +00:00
|
|
|
return Error::success();
|
2017-10-10 18:47:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class SymTabType>
|
|
|
|
void RelocSectionWithSymtabBase<SymTabType>::initialize(
|
|
|
|
SectionTableRef SecTable) {
|
2018-09-04 22:28:49 +00:00
|
|
|
if (Link != SHN_UNDEF)
|
|
|
|
setSymTab(SecTable.getSectionOfType<SymTabType>(
|
|
|
|
Link,
|
|
|
|
"Link field value " + Twine(Link) + " in section " + Name +
|
|
|
|
" is invalid",
|
|
|
|
"Link field value " + Twine(Link) + " in section " + Name +
|
|
|
|
" is not a symbol table"));
|
2017-09-25 20:37:28 +00:00
|
|
|
|
2017-09-26 18:02:25 +00:00
|
|
|
if (Info != SHN_UNDEF)
|
2018-03-21 19:53:44 +00:00
|
|
|
setSection(SecTable.getSection(Info, "Info field value " + Twine(Info) +
|
|
|
|
" in section " + Name +
|
|
|
|
" is invalid"));
|
2017-09-26 22:44:01 +00:00
|
|
|
else
|
|
|
|
setSection(nullptr);
|
2017-09-25 20:37:28 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 18:47:09 +00:00
|
|
|
template <class SymTabType>
|
|
|
|
void RelocSectionWithSymtabBase<SymTabType>::finalize() {
|
2018-09-04 22:28:49 +00:00
|
|
|
this->Link = Symbols ? Symbols->Index : 0;
|
|
|
|
|
2017-09-26 18:02:25 +00:00
|
|
|
if (SecToApplyRel != nullptr)
|
|
|
|
this->Info = SecToApplyRel->Index;
|
2017-09-06 23:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2018-07-16 22:17:05 +00:00
|
|
|
static void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {}
|
2017-09-06 23:41:02 +00:00
|
|
|
|
|
|
|
template <class ELFT>
|
2018-07-16 22:17:05 +00:00
|
|
|
static void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) {
|
2017-09-06 23:41:02 +00:00
|
|
|
Rela.r_addend = Addend;
|
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
template <class RelRange, class T>
|
2018-07-16 22:17:05 +00:00
|
|
|
static void writeRel(const RelRange &Relocations, T *Buf) {
|
2017-09-06 23:41:02 +00:00
|
|
|
for (const auto &Reloc : Relocations) {
|
|
|
|
Buf->r_offset = Reloc.Offset;
|
|
|
|
setAddend(*Buf, Reloc.Addend);
|
|
|
|
Buf->setSymbolAndType(Reloc.RelocSymbol->Index, Reloc.Type, false);
|
|
|
|
++Buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2018-01-25 22:46:17 +00:00
|
|
|
void ELFSectionWriter<ELFT>::visit(const RelocationSection &Sec) {
|
|
|
|
uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
|
|
|
|
if (Sec.Type == SHT_REL)
|
|
|
|
writeRel(Sec.Relocations, reinterpret_cast<Elf_Rel *>(Buf));
|
2017-09-06 23:41:02 +00:00
|
|
|
else
|
2018-01-25 22:46:17 +00:00
|
|
|
writeRel(Sec.Relocations, reinterpret_cast<Elf_Rela *>(Buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
void RelocationSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
Visitor.visit(*this);
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
void RelocationSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
Visitor.visit(*this);
|
|
|
|
}
|
|
|
|
|
2019-02-01 15:20:36 +00:00
|
|
|
Error RelocationSection::removeSymbols(
|
2018-05-09 21:36:54 +00:00
|
|
|
function_ref<bool(const Symbol &)> ToRemove) {
|
|
|
|
for (const Relocation &Reloc : Relocations)
|
|
|
|
if (ToRemove(*Reloc.RelocSymbol))
|
2019-02-01 15:20:36 +00:00
|
|
|
return createStringError(
|
|
|
|
llvm::errc::invalid_argument,
|
|
|
|
"not stripping symbol '%s' because it is named in a relocation.",
|
|
|
|
Reloc.RelocSymbol->Name.data());
|
|
|
|
return Error::success();
|
2018-05-09 21:36:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 11:01:25 +00:00
|
|
|
void RelocationSection::markSymbols() {
|
|
|
|
for (const Relocation &Reloc : Relocations)
|
|
|
|
Reloc.RelocSymbol->Referenced = true;
|
|
|
|
}
|
|
|
|
|
2019-03-11 11:01:24 +00:00
|
|
|
void RelocationSection::replaceSectionReferences(
|
|
|
|
const DenseMap<SectionBase *, SectionBase *> &FromTo) {
|
|
|
|
// Update the target section if it was replaced.
|
|
|
|
if (SectionBase *To = FromTo.lookup(SecToApplyRel))
|
|
|
|
SecToApplyRel = To;
|
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
void SectionWriter::visit(const DynamicRelocationSection &Sec) {
|
2018-11-17 01:44:25 +00:00
|
|
|
llvm::copy(Sec.Contents,
|
2018-01-25 22:46:17 +00:00
|
|
|
Out.getBufferStart() + Sec.Offset);
|
2017-09-06 23:41:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
void DynamicRelocationSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
Visitor.visit(*this);
|
2017-09-26 18:02:25 +00:00
|
|
|
}
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
void DynamicRelocationSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
Visitor.visit(*this);
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy] Make removeSectionReferences batched
Summary:
Removing a large number of sections from a file with a lot of symbols can have abysmal (i.e. O(n^2)) performance, e.g. when running `--only-section` to extract one section out of a large file.
This comes from iterating over all symbols in the symbol table each time we remove a section, to remove references to the section we just removed.
Instead, do just one pass of symbol removal by passing a hash set of all the sections we'd like to remove references to.
This fixes a regression when running llvm-objcopy -j <one section> on an object file with many sections and symbols -- on my machine, running `objcopy -j .keep_me huge-input.o /tmp/foo.o` takes .3s with GNU objcopy, 1.3s with an updated llvm-objcopy, and 7+ minutes with llvm-objcopy prior to this patch.
Reviewers: MaskRay, jhenderson, jakehehrlich, alexshap, espindola
Reviewed By: MaskRay, jhenderson
Subscribers: echristo, emaste, arichardson, mgrang, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D58296
llvm-svn: 354597
2019-02-21 16:45:42 +00:00
|
|
|
Error Section::removeSectionReferences(
|
|
|
|
function_ref<bool(const SectionBase *)> ToRemove) {
|
|
|
|
if (ToRemove(LinkSection))
|
2019-02-01 15:20:36 +00:00
|
|
|
return createStringError(llvm::errc::invalid_argument,
|
|
|
|
"Section %s cannot be removed because it is "
|
|
|
|
"referenced by the section %s",
|
|
|
|
LinkSection->Name.data(), this->Name.data());
|
|
|
|
return Error::success();
|
2017-10-10 18:47:09 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 19:53:44 +00:00
|
|
|
void GroupSection::finalize() {
|
|
|
|
this->Info = Sym->Index;
|
|
|
|
this->Link = SymTab->Index;
|
|
|
|
}
|
|
|
|
|
2019-02-01 15:20:36 +00:00
|
|
|
Error GroupSection::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
|
|
|
|
if (ToRemove(*Sym))
|
|
|
|
return createStringError(llvm::errc::invalid_argument,
|
|
|
|
"Symbol %s cannot be removed because it is "
|
|
|
|
"referenced by the section %s[%d].",
|
|
|
|
Sym->Name.data(), this->Name.data(), this->Index);
|
|
|
|
return Error::success();
|
2018-05-09 21:36:54 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 11:01:25 +00:00
|
|
|
void GroupSection::markSymbols() {
|
|
|
|
if (Sym)
|
|
|
|
Sym->Referenced = true;
|
|
|
|
}
|
|
|
|
|
2018-04-20 20:46:04 +00:00
|
|
|
void Section::initialize(SectionTableRef SecTable) {
|
2018-05-30 19:30:39 +00:00
|
|
|
if (Link != ELF::SHN_UNDEF) {
|
2018-04-20 20:46:04 +00:00
|
|
|
LinkSection =
|
|
|
|
SecTable.getSection(Link, "Link field value " + Twine(Link) +
|
|
|
|
" in section " + Name + " is invalid");
|
2018-05-30 19:30:39 +00:00
|
|
|
if (LinkSection->Type == ELF::SHT_SYMTAB)
|
|
|
|
LinkSection = nullptr;
|
|
|
|
}
|
2017-09-20 17:11:58 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 17:51:03 +00:00
|
|
|
void Section::finalize() { this->Link = LinkSection ? LinkSection->Index : 0; }
|
2017-09-25 20:37:28 +00:00
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
void GnuDebugLinkSection::init(StringRef File, StringRef Data) {
|
2018-02-19 19:53:44 +00:00
|
|
|
FileName = sys::path::filename(File);
|
|
|
|
// The format for the .gnu_debuglink starts with the file name and is
|
2018-01-25 22:15:14 +00:00
|
|
|
// followed by a null terminator and then the CRC32 of the file. The CRC32
|
|
|
|
// should be 4 byte aligned. So we add the FileName size, a 1 for the null
|
|
|
|
// byte, and then finally push the size to alignment and add 4.
|
|
|
|
Size = alignTo(FileName.size() + 1, 4) + 4;
|
|
|
|
// The CRC32 will only be aligned if we align the whole section.
|
|
|
|
Align = 4;
|
|
|
|
Type = ELF::SHT_PROGBITS;
|
|
|
|
Name = ".gnu_debuglink";
|
|
|
|
// For sections not found in segments, OriginalOffset is only used to
|
|
|
|
// establish the order that sections should go in. By using the maximum
|
|
|
|
// possible offset we cause this section to wind up at the end.
|
|
|
|
OriginalOffset = std::numeric_limits<uint64_t>::max();
|
2018-11-01 16:02:12 +00:00
|
|
|
JamCRC CRC;
|
|
|
|
CRC.update(ArrayRef<char>(Data.data(), Data.size()));
|
2018-01-25 22:15:14 +00:00
|
|
|
// The CRC32 value needs to be complemented because the JamCRC dosn't
|
|
|
|
// finalize the CRC32 value. It also dosn't negate the initial CRC32 value
|
|
|
|
// but it starts by default at 0xFFFFFFFF which is the complement of zero.
|
2018-11-01 16:02:12 +00:00
|
|
|
CRC32 = ~CRC.getCRC();
|
2018-01-25 22:15:14 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
GnuDebugLinkSection::GnuDebugLinkSection(StringRef File) : FileName(File) {
|
2018-01-25 22:15:14 +00:00
|
|
|
// Read in the file to compute the CRC of it.
|
|
|
|
auto DebugOrErr = MemoryBuffer::getFile(File);
|
|
|
|
if (!DebugOrErr)
|
|
|
|
error("'" + File + "': " + DebugOrErr.getError().message());
|
|
|
|
auto Debug = std::move(*DebugOrErr);
|
|
|
|
init(File, Debug->getBuffer());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2018-01-25 22:46:17 +00:00
|
|
|
void ELFSectionWriter<ELFT>::visit(const GnuDebugLinkSection &Sec) {
|
|
|
|
auto Buf = Out.getBufferStart() + Sec.Offset;
|
2018-01-25 22:15:14 +00:00
|
|
|
char *File = reinterpret_cast<char *>(Buf);
|
2018-01-25 22:46:17 +00:00
|
|
|
Elf_Word *CRC =
|
|
|
|
reinterpret_cast<Elf_Word *>(Buf + Sec.Size - sizeof(Elf_Word));
|
|
|
|
*CRC = Sec.CRC32;
|
2018-11-17 01:44:25 +00:00
|
|
|
llvm::copy(Sec.FileName, File);
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GnuDebugLinkSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
Visitor.visit(*this);
|
2018-01-25 22:15:14 +00:00
|
|
|
}
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
void GnuDebugLinkSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
Visitor.visit(*this);
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:53:44 +00:00
|
|
|
template <class ELFT>
|
|
|
|
void ELFSectionWriter<ELFT>::visit(const GroupSection &Sec) {
|
|
|
|
ELF::Elf32_Word *Buf =
|
|
|
|
reinterpret_cast<ELF::Elf32_Word *>(Out.getBufferStart() + Sec.Offset);
|
|
|
|
*Buf++ = Sec.FlagWord;
|
|
|
|
for (const auto *S : Sec.GroupMembers)
|
|
|
|
support::endian::write32<ELFT::TargetEndianness>(Buf++, S->Index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
Visitor.visit(*this);
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
void GroupSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
Visitor.visit(*this);
|
|
|
|
}
|
|
|
|
|
2017-08-01 00:33:58 +00:00
|
|
|
// Returns true IFF a section is wholly inside the range of a segment
|
|
|
|
static bool sectionWithinSegment(const SectionBase &Section,
|
|
|
|
const Segment &Segment) {
|
|
|
|
// If a section is empty it should be treated like it has a size of 1. This is
|
|
|
|
// to clarify the case when an empty section lies on a boundary between two
|
|
|
|
// segments and ensures that the section "belongs" to the second segment and
|
|
|
|
// not the first.
|
|
|
|
uint64_t SecSize = Section.Size ? Section.Size : 1;
|
|
|
|
return Segment.Offset <= Section.OriginalOffset &&
|
|
|
|
Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize;
|
|
|
|
}
|
|
|
|
|
2017-09-19 21:37:35 +00:00
|
|
|
// Returns true IFF a segment's original offset is inside of another segment's
|
|
|
|
// range.
|
|
|
|
static bool segmentOverlapsSegment(const Segment &Child,
|
|
|
|
const Segment &Parent) {
|
|
|
|
|
|
|
|
return Parent.OriginalOffset <= Child.OriginalOffset &&
|
|
|
|
Parent.OriginalOffset + Parent.FileSize > Child.OriginalOffset;
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:27:30 +00:00
|
|
|
static bool compareSegmentsByOffset(const Segment *A, const Segment *B) {
|
2017-11-15 19:13:31 +00:00
|
|
|
// Any segment without a parent segment should come before a segment
|
|
|
|
// that has a parent segment.
|
|
|
|
if (A->OriginalOffset < B->OriginalOffset)
|
|
|
|
return true;
|
|
|
|
if (A->OriginalOffset > B->OriginalOffset)
|
|
|
|
return false;
|
|
|
|
return A->Index < B->Index;
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:27:30 +00:00
|
|
|
static bool compareSegmentsByPAddr(const Segment *A, const Segment *B) {
|
|
|
|
if (A->PAddr < B->PAddr)
|
|
|
|
return true;
|
|
|
|
if (A->PAddr > B->PAddr)
|
|
|
|
return false;
|
|
|
|
return A->Index < B->Index;
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
void BinaryELFBuilder::initFileHeader() {
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
Obj->Flags = 0x0;
|
|
|
|
Obj->Type = ET_REL;
|
2018-12-20 10:59:52 +00:00
|
|
|
Obj->OSABI = ELFOSABI_NONE;
|
2018-12-20 10:51:42 +00:00
|
|
|
Obj->ABIVersion = 0;
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
Obj->Entry = 0x0;
|
|
|
|
Obj->Machine = EMachine;
|
|
|
|
Obj->Version = 1;
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
void BinaryELFBuilder::initHeaderSegment() { Obj->ElfHdrSegment.Index = 0; }
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
StringTableSection *BinaryELFBuilder::addStrTab() {
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
auto &StrTab = Obj->addSection<StringTableSection>();
|
|
|
|
StrTab.Name = ".strtab";
|
|
|
|
|
|
|
|
Obj->SectionNames = &StrTab;
|
|
|
|
return &StrTab;
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
SymbolTableSection *BinaryELFBuilder::addSymTab(StringTableSection *StrTab) {
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
auto &SymTab = Obj->addSection<SymbolTableSection>();
|
|
|
|
|
|
|
|
SymTab.Name = ".symtab";
|
|
|
|
SymTab.Link = StrTab->Index;
|
|
|
|
|
|
|
|
// The symbol table always needs a null symbol
|
|
|
|
SymTab.addSymbol("", 0, 0, nullptr, 0, 0, 0, 0);
|
|
|
|
|
|
|
|
Obj->SymbolTable = &SymTab;
|
|
|
|
return &SymTab;
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
void BinaryELFBuilder::addData(SymbolTableSection *SymTab) {
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
auto Data = ArrayRef<uint8_t>(
|
|
|
|
reinterpret_cast<const uint8_t *>(MemBuf->getBufferStart()),
|
|
|
|
MemBuf->getBufferSize());
|
|
|
|
auto &DataSection = Obj->addSection<Section>(Data);
|
|
|
|
DataSection.Name = ".data";
|
|
|
|
DataSection.Type = ELF::SHT_PROGBITS;
|
|
|
|
DataSection.Size = Data.size();
|
|
|
|
DataSection.Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
|
|
|
|
|
|
|
|
std::string SanitizedFilename = MemBuf->getBufferIdentifier().str();
|
|
|
|
std::replace_if(std::begin(SanitizedFilename), std::end(SanitizedFilename),
|
2018-11-01 16:02:12 +00:00
|
|
|
[](char C) { return !isalnum(C); }, '_');
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
Twine Prefix = Twine("_binary_") + SanitizedFilename;
|
|
|
|
|
|
|
|
SymTab->addSymbol(Prefix + "_start", STB_GLOBAL, STT_NOTYPE, &DataSection,
|
|
|
|
/*Value=*/0, STV_DEFAULT, 0, 0);
|
|
|
|
SymTab->addSymbol(Prefix + "_end", STB_GLOBAL, STT_NOTYPE, &DataSection,
|
|
|
|
/*Value=*/DataSection.Size, STV_DEFAULT, 0, 0);
|
|
|
|
SymTab->addSymbol(Prefix + "_size", STB_GLOBAL, STT_NOTYPE, nullptr,
|
|
|
|
/*Value=*/DataSection.Size, STV_DEFAULT, SHN_ABS, 0);
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
void BinaryELFBuilder::initSections() {
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
for (auto &Section : Obj->sections()) {
|
|
|
|
Section.initialize(Obj->sections());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
std::unique_ptr<Object> BinaryELFBuilder::build() {
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
initFileHeader();
|
|
|
|
initHeaderSegment();
|
|
|
|
StringTableSection *StrTab = addStrTab();
|
|
|
|
SymbolTableSection *SymTab = addSymTab(StrTab);
|
|
|
|
initSections();
|
|
|
|
addData(SymTab);
|
|
|
|
|
|
|
|
return std::move(Obj);
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:53:44 +00:00
|
|
|
template <class ELFT> void ELFBuilder<ELFT>::setParentSegment(Segment &Child) {
|
2018-02-14 23:31:33 +00:00
|
|
|
for (auto &Parent : Obj.segments()) {
|
|
|
|
// Every segment will overlap with itself but we don't want a segment to
|
|
|
|
// be it's own parent so we avoid that situation.
|
|
|
|
if (&Child != &Parent && segmentOverlapsSegment(Child, Parent)) {
|
|
|
|
// We want a canonical "most parental" segment but this requires
|
|
|
|
// inspecting the ParentSegment.
|
|
|
|
if (compareSegmentsByOffset(&Parent, &Child))
|
|
|
|
if (Child.ParentSegment == nullptr ||
|
|
|
|
compareSegmentsByOffset(&Parent, Child.ParentSegment)) {
|
|
|
|
Child.ParentSegment = &Parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
template <class ELFT> void ELFBuilder<ELFT>::readProgramHeaders() {
|
2017-08-01 00:33:58 +00:00
|
|
|
uint32_t Index = 0;
|
|
|
|
for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) {
|
2019-03-12 02:17:01 +00:00
|
|
|
Segment &Seg = Obj.addSegment();
|
2017-08-01 00:33:58 +00:00
|
|
|
Seg.Type = Phdr.p_type;
|
|
|
|
Seg.Flags = Phdr.p_flags;
|
2017-08-26 01:32:20 +00:00
|
|
|
Seg.OriginalOffset = Phdr.p_offset;
|
2017-08-01 00:33:58 +00:00
|
|
|
Seg.Offset = Phdr.p_offset;
|
|
|
|
Seg.VAddr = Phdr.p_vaddr;
|
|
|
|
Seg.PAddr = Phdr.p_paddr;
|
|
|
|
Seg.FileSize = Phdr.p_filesz;
|
|
|
|
Seg.MemSize = Phdr.p_memsz;
|
|
|
|
Seg.Align = Phdr.p_align;
|
|
|
|
Seg.Index = Index++;
|
2018-01-25 22:46:17 +00:00
|
|
|
for (auto &Section : Obj.sections()) {
|
|
|
|
if (sectionWithinSegment(Section, Seg)) {
|
|
|
|
Seg.addSection(&Section);
|
|
|
|
if (!Section.ParentSegment ||
|
|
|
|
Section.ParentSegment->Offset > Seg.Offset) {
|
|
|
|
Section.ParentSegment = &Seg;
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-14 23:31:33 +00:00
|
|
|
|
|
|
|
auto &ElfHdr = Obj.ElfHdrSegment;
|
|
|
|
ElfHdr.Index = Index++;
|
|
|
|
|
|
|
|
const auto &Ehdr = *ElfFile.getHeader();
|
|
|
|
auto &PrHdr = Obj.ProgramHdrSegment;
|
|
|
|
PrHdr.Type = PT_PHDR;
|
|
|
|
PrHdr.Flags = 0;
|
|
|
|
// The spec requires us to have p_vaddr % p_align == p_offset % p_align.
|
|
|
|
// Whereas this works automatically for ElfHdr, here OriginalOffset is
|
|
|
|
// always non-zero and to ensure the equation we assign the same value to
|
|
|
|
// VAddr as well.
|
|
|
|
PrHdr.OriginalOffset = PrHdr.Offset = PrHdr.VAddr = Ehdr.e_phoff;
|
|
|
|
PrHdr.PAddr = 0;
|
|
|
|
PrHdr.FileSize = PrHdr.MemSize = Ehdr.e_phentsize * Ehdr.e_phnum;
|
2018-03-21 19:53:44 +00:00
|
|
|
// The spec requires us to naturally align all the fields.
|
2018-02-14 23:31:33 +00:00
|
|
|
PrHdr.Align = sizeof(Elf_Addr);
|
|
|
|
PrHdr.Index = Index++;
|
|
|
|
|
2017-09-19 21:37:35 +00:00
|
|
|
// Now we do an O(n^2) loop through the segments in order to match up
|
|
|
|
// segments.
|
2018-02-14 23:31:33 +00:00
|
|
|
for (auto &Child : Obj.segments())
|
|
|
|
setParentSegment(Child);
|
|
|
|
setParentSegment(ElfHdr);
|
|
|
|
setParentSegment(PrHdr);
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 19:53:44 +00:00
|
|
|
template <class ELFT>
|
|
|
|
void ELFBuilder<ELFT>::initGroupSection(GroupSection *GroupSec) {
|
|
|
|
auto SecTable = Obj.sections();
|
|
|
|
auto SymTab = SecTable.template getSectionOfType<SymbolTableSection>(
|
|
|
|
GroupSec->Link,
|
|
|
|
"Link field value " + Twine(GroupSec->Link) + " in section " +
|
|
|
|
GroupSec->Name + " is invalid",
|
|
|
|
"Link field value " + Twine(GroupSec->Link) + " in section " +
|
|
|
|
GroupSec->Name + " is not a symbol table");
|
|
|
|
auto Sym = SymTab->getSymbolByIndex(GroupSec->Info);
|
|
|
|
if (!Sym)
|
|
|
|
error("Info field value " + Twine(GroupSec->Info) + " in section " +
|
|
|
|
GroupSec->Name + " is not a valid symbol index");
|
|
|
|
GroupSec->setSymTab(SymTab);
|
|
|
|
GroupSec->setSymbol(Sym);
|
|
|
|
if (GroupSec->Contents.size() % sizeof(ELF::Elf32_Word) ||
|
|
|
|
GroupSec->Contents.empty())
|
|
|
|
error("The content of the section " + GroupSec->Name + " is malformed");
|
|
|
|
const ELF::Elf32_Word *Word =
|
|
|
|
reinterpret_cast<const ELF::Elf32_Word *>(GroupSec->Contents.data());
|
|
|
|
const ELF::Elf32_Word *End =
|
|
|
|
Word + GroupSec->Contents.size() / sizeof(ELF::Elf32_Word);
|
|
|
|
GroupSec->setFlagWord(*Word++);
|
|
|
|
for (; Word != End; ++Word) {
|
|
|
|
uint32_t Index = support::endian::read32<ELFT::TargetEndianness>(Word);
|
|
|
|
GroupSec->addMember(SecTable.getSection(
|
|
|
|
Index, "Group member index " + Twine(Index) + " in section " +
|
|
|
|
GroupSec->Name + " is invalid"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 02:12:03 +00:00
|
|
|
template <class ELFT>
|
2018-01-25 22:46:17 +00:00
|
|
|
void ELFBuilder<ELFT>::initSymbolTable(SymbolTableSection *SymTab) {
|
2017-08-29 02:12:03 +00:00
|
|
|
const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
|
|
|
|
StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
|
2018-07-16 19:48:52 +00:00
|
|
|
ArrayRef<Elf_Word> ShndxData;
|
2017-08-29 02:12:03 +00:00
|
|
|
|
2018-07-16 19:48:52 +00:00
|
|
|
auto Symbols = unwrapOrError(ElfFile.symbols(&Shdr));
|
|
|
|
for (const auto &Sym : Symbols) {
|
2017-08-29 02:12:03 +00:00
|
|
|
SectionBase *DefSection = nullptr;
|
|
|
|
StringRef Name = unwrapOrError(Sym.getName(StrTabData));
|
2017-09-20 17:11:58 +00:00
|
|
|
|
2018-07-16 19:48:52 +00:00
|
|
|
if (Sym.st_shndx == SHN_XINDEX) {
|
|
|
|
if (SymTab->getShndxTable() == nullptr)
|
|
|
|
error("Symbol '" + Name +
|
|
|
|
"' has index SHN_XINDEX but no SHT_SYMTAB_SHNDX section exists.");
|
|
|
|
if (ShndxData.data() == nullptr) {
|
|
|
|
const Elf_Shdr &ShndxSec =
|
|
|
|
*unwrapOrError(ElfFile.getSection(SymTab->getShndxTable()->Index));
|
|
|
|
ShndxData = unwrapOrError(
|
|
|
|
ElfFile.template getSectionContentsAsArray<Elf_Word>(&ShndxSec));
|
|
|
|
if (ShndxData.size() != Symbols.size())
|
|
|
|
error("Symbol section index table does not have the same number of "
|
|
|
|
"entries as the symbol table.");
|
|
|
|
}
|
|
|
|
Elf_Word Index = ShndxData[&Sym - Symbols.begin()];
|
|
|
|
DefSection = Obj.sections().getSection(
|
|
|
|
Index,
|
2018-08-02 18:16:52 +00:00
|
|
|
"Symbol '" + Name + "' has invalid section index " + Twine(Index));
|
2018-07-16 19:48:52 +00:00
|
|
|
} else if (Sym.st_shndx >= SHN_LORESERVE) {
|
2018-01-25 22:46:17 +00:00
|
|
|
if (!isValidReservedSectionIndex(Sym.st_shndx, Obj.Machine)) {
|
2017-09-07 23:02:50 +00:00
|
|
|
error(
|
|
|
|
"Symbol '" + Name +
|
|
|
|
"' has unsupported value greater than or equal to SHN_LORESERVE: " +
|
|
|
|
Twine(Sym.st_shndx));
|
|
|
|
}
|
|
|
|
} else if (Sym.st_shndx != SHN_UNDEF) {
|
2018-01-25 22:46:17 +00:00
|
|
|
DefSection = Obj.sections().getSection(
|
2018-03-21 19:53:44 +00:00
|
|
|
Sym.st_shndx, "Symbol '" + Name +
|
2018-07-16 19:48:52 +00:00
|
|
|
"' is defined has invalid section index " +
|
2018-03-21 19:53:44 +00:00
|
|
|
Twine(Sym.st_shndx));
|
2017-08-29 02:12:03 +00:00
|
|
|
}
|
2017-09-20 17:11:58 +00:00
|
|
|
|
2017-08-29 02:12:03 +00:00
|
|
|
SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection,
|
2018-01-02 23:01:24 +00:00
|
|
|
Sym.getValue(), Sym.st_other, Sym.st_shndx, Sym.st_size);
|
2017-08-29 02:12:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-06 23:41:02 +00:00
|
|
|
template <class ELFT>
|
|
|
|
static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, false> &Rel) {}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) {
|
|
|
|
ToSet = Rela.r_addend;
|
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
template <class T>
|
2018-07-16 22:17:05 +00:00
|
|
|
static void initRelocations(RelocationSection *Relocs,
|
|
|
|
SymbolTableSection *SymbolTable, T RelRange) {
|
2017-09-06 23:41:02 +00:00
|
|
|
for (const auto &Rel : RelRange) {
|
|
|
|
Relocation ToAdd;
|
|
|
|
ToAdd.Offset = Rel.r_offset;
|
|
|
|
getAddend(ToAdd.Addend, Rel);
|
|
|
|
ToAdd.Type = Rel.getType(false);
|
2018-05-22 01:04:36 +00:00
|
|
|
ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Rel.getSymbol(false));
|
2017-09-06 23:41:02 +00:00
|
|
|
Relocs->addRelocation(ToAdd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 19:48:52 +00:00
|
|
|
SectionBase *SectionTableRef::getSection(uint32_t Index, Twine ErrMsg) {
|
2017-09-25 20:37:28 +00:00
|
|
|
if (Index == SHN_UNDEF || Index > Sections.size())
|
|
|
|
error(ErrMsg);
|
|
|
|
return Sections[Index - 1].get();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2018-07-16 19:48:52 +00:00
|
|
|
T *SectionTableRef::getSectionOfType(uint32_t Index, Twine IndexErrMsg,
|
2017-10-11 23:54:34 +00:00
|
|
|
Twine TypeErrMsg) {
|
2017-11-01 21:16:06 +00:00
|
|
|
if (T *Sec = dyn_cast<T>(getSection(Index, IndexErrMsg)))
|
2017-09-25 20:37:28 +00:00
|
|
|
return Sec;
|
|
|
|
error(TypeErrMsg);
|
|
|
|
}
|
|
|
|
|
2017-08-01 00:33:58 +00:00
|
|
|
template <class ELFT>
|
2018-01-25 22:46:17 +00:00
|
|
|
SectionBase &ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
|
2017-08-01 00:33:58 +00:00
|
|
|
ArrayRef<uint8_t> Data;
|
|
|
|
switch (Shdr.sh_type) {
|
2017-09-06 23:41:02 +00:00
|
|
|
case SHT_REL:
|
|
|
|
case SHT_RELA:
|
2017-09-26 18:02:25 +00:00
|
|
|
if (Shdr.sh_flags & SHF_ALLOC) {
|
|
|
|
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
|
2018-01-25 22:46:17 +00:00
|
|
|
return Obj.addSection<DynamicRelocationSection>(Data);
|
2017-09-26 18:02:25 +00:00
|
|
|
}
|
2018-01-25 22:46:17 +00:00
|
|
|
return Obj.addSection<RelocationSection>();
|
2017-08-01 00:33:58 +00:00
|
|
|
case SHT_STRTAB:
|
2017-09-20 17:11:58 +00:00
|
|
|
// If a string table is allocated we don't want to mess with it. That would
|
|
|
|
// mean altering the memory image. There are no special link types or
|
|
|
|
// anything so we can just use a Section.
|
|
|
|
if (Shdr.sh_flags & SHF_ALLOC) {
|
|
|
|
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
|
2018-01-25 22:46:17 +00:00
|
|
|
return Obj.addSection<Section>(Data);
|
2017-09-20 17:11:58 +00:00
|
|
|
}
|
2018-01-25 22:46:17 +00:00
|
|
|
return Obj.addSection<StringTableSection>();
|
2017-09-20 17:11:58 +00:00
|
|
|
case SHT_HASH:
|
|
|
|
case SHT_GNU_HASH:
|
|
|
|
// Hash tables should refer to SHT_DYNSYM which we're not going to change.
|
|
|
|
// Because of this we don't need to mess with the hash tables either.
|
|
|
|
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
|
2018-01-25 22:46:17 +00:00
|
|
|
return Obj.addSection<Section>(Data);
|
2018-03-21 19:53:44 +00:00
|
|
|
case SHT_GROUP:
|
|
|
|
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
|
|
|
|
return Obj.addSection<GroupSection>(Data);
|
2017-09-20 17:11:58 +00:00
|
|
|
case SHT_DYNSYM:
|
|
|
|
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
|
2018-01-25 22:46:17 +00:00
|
|
|
return Obj.addSection<DynamicSymbolTableSection>(Data);
|
2017-09-20 17:11:58 +00:00
|
|
|
case SHT_DYNAMIC:
|
|
|
|
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
|
2018-01-25 22:46:17 +00:00
|
|
|
return Obj.addSection<DynamicSection>(Data);
|
2017-08-29 02:12:03 +00:00
|
|
|
case SHT_SYMTAB: {
|
2018-01-25 22:46:17 +00:00
|
|
|
auto &SymTab = Obj.addSection<SymbolTableSection>();
|
|
|
|
Obj.SymbolTable = &SymTab;
|
|
|
|
return SymTab;
|
2017-08-29 02:12:03 +00:00
|
|
|
}
|
2018-07-16 19:48:52 +00:00
|
|
|
case SHT_SYMTAB_SHNDX: {
|
|
|
|
auto &ShndxSection = Obj.addSection<SectionIndexSection>();
|
|
|
|
Obj.SectionIndexTable = &ShndxSection;
|
|
|
|
return ShndxSection;
|
|
|
|
}
|
2017-08-01 00:33:58 +00:00
|
|
|
case SHT_NOBITS:
|
2018-01-25 22:46:17 +00:00
|
|
|
return Obj.addSection<Section>(Data);
|
2018-10-01 10:29:41 +00:00
|
|
|
default: {
|
2017-08-01 00:33:58 +00:00
|
|
|
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
|
2018-10-01 10:29:41 +00:00
|
|
|
|
2019-03-06 14:01:54 +00:00
|
|
|
StringRef Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
|
|
|
|
if (Name.startswith(".zdebug") || (Shdr.sh_flags & ELF::SHF_COMPRESSED)) {
|
2018-10-01 10:29:41 +00:00
|
|
|
uint64_t DecompressedSize, DecompressedAlign;
|
|
|
|
std::tie(DecompressedSize, DecompressedAlign) =
|
|
|
|
getDecompressedSizeAndAlignment<ELFT>(Data);
|
|
|
|
return Obj.addSection<CompressedSection>(Data, DecompressedSize,
|
|
|
|
DecompressedAlign);
|
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
return Obj.addSection<Section>(Data);
|
2017-08-04 21:09:26 +00:00
|
|
|
}
|
2018-10-01 10:29:41 +00:00
|
|
|
}
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
template <class ELFT> void ELFBuilder<ELFT>::readSectionHeaders() {
|
2017-08-01 00:33:58 +00:00
|
|
|
uint32_t Index = 0;
|
|
|
|
for (const auto &Shdr : unwrapOrError(ElfFile.sections())) {
|
|
|
|
if (Index == 0) {
|
|
|
|
++Index;
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-25 22:46:17 +00:00
|
|
|
auto &Sec = makeSection(Shdr);
|
|
|
|
Sec.Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
|
|
|
|
Sec.Type = Shdr.sh_type;
|
|
|
|
Sec.Flags = Shdr.sh_flags;
|
|
|
|
Sec.Addr = Shdr.sh_addr;
|
|
|
|
Sec.Offset = Shdr.sh_offset;
|
|
|
|
Sec.OriginalOffset = Shdr.sh_offset;
|
|
|
|
Sec.Size = Shdr.sh_size;
|
|
|
|
Sec.Link = Shdr.sh_link;
|
|
|
|
Sec.Info = Shdr.sh_info;
|
|
|
|
Sec.Align = Shdr.sh_addralign;
|
|
|
|
Sec.EntrySize = Shdr.sh_entsize;
|
|
|
|
Sec.Index = Index++;
|
2018-08-09 17:05:21 +00:00
|
|
|
Sec.OriginalData =
|
|
|
|
ArrayRef<uint8_t>(ElfFile.base() + Shdr.sh_offset,
|
|
|
|
(Shdr.sh_type == SHT_NOBITS) ? 0 : Shdr.sh_size);
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
2017-08-29 02:12:03 +00:00
|
|
|
|
2018-07-16 19:48:52 +00:00
|
|
|
// If a section index table exists we'll need to initialize it before we
|
|
|
|
// initialize the symbol table because the symbol table might need to
|
|
|
|
// reference it.
|
|
|
|
if (Obj.SectionIndexTable)
|
|
|
|
Obj.SectionIndexTable->initialize(Obj.sections());
|
|
|
|
|
2017-08-29 02:12:03 +00:00
|
|
|
// Now that all of the sections have been added we can fill out some extra
|
2017-09-25 20:37:28 +00:00
|
|
|
// details about symbol tables. We need the symbol table filled out before
|
|
|
|
// any relocations.
|
2018-01-25 22:46:17 +00:00
|
|
|
if (Obj.SymbolTable) {
|
|
|
|
Obj.SymbolTable->initialize(Obj.sections());
|
|
|
|
initSymbolTable(Obj.SymbolTable);
|
2017-09-25 20:37:28 +00:00
|
|
|
}
|
2017-09-06 23:41:02 +00:00
|
|
|
|
|
|
|
// Now that all sections and symbols have been added we can add
|
|
|
|
// relocations that reference symbols and set the link and info fields for
|
|
|
|
// relocation sections.
|
2018-01-25 22:46:17 +00:00
|
|
|
for (auto &Section : Obj.sections()) {
|
|
|
|
if (&Section == Obj.SymbolTable)
|
2017-09-25 20:37:28 +00:00
|
|
|
continue;
|
2018-01-25 22:46:17 +00:00
|
|
|
Section.initialize(Obj.sections());
|
|
|
|
if (auto RelSec = dyn_cast<RelocationSection>(&Section)) {
|
2017-09-06 23:41:02 +00:00
|
|
|
auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
|
|
|
|
if (RelSec->Type == SHT_REL)
|
2018-01-25 22:46:17 +00:00
|
|
|
initRelocations(RelSec, Obj.SymbolTable,
|
|
|
|
unwrapOrError(ElfFile.rels(Shdr)));
|
2017-09-06 23:41:02 +00:00
|
|
|
else
|
2018-01-25 22:46:17 +00:00
|
|
|
initRelocations(RelSec, Obj.SymbolTable,
|
2017-09-25 20:37:28 +00:00
|
|
|
unwrapOrError(ElfFile.relas(Shdr)));
|
2018-03-21 19:53:44 +00:00
|
|
|
} else if (auto GroupSec = dyn_cast<GroupSection>(&Section)) {
|
|
|
|
initGroupSection(GroupSec);
|
2017-09-06 23:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
template <class ELFT> void ELFBuilder<ELFT>::build() {
|
2018-01-25 21:03:38 +00:00
|
|
|
const auto &Ehdr = *ElfFile.getHeader();
|
2018-01-25 20:24:17 +00:00
|
|
|
|
2018-12-20 10:51:42 +00:00
|
|
|
Obj.OSABI = Ehdr.e_ident[EI_OSABI];
|
|
|
|
Obj.ABIVersion = Ehdr.e_ident[EI_ABIVERSION];
|
2018-01-25 22:46:17 +00:00
|
|
|
Obj.Type = Ehdr.e_type;
|
|
|
|
Obj.Machine = Ehdr.e_machine;
|
|
|
|
Obj.Version = Ehdr.e_version;
|
|
|
|
Obj.Entry = Ehdr.e_entry;
|
|
|
|
Obj.Flags = Ehdr.e_flags;
|
|
|
|
|
|
|
|
readSectionHeaders();
|
|
|
|
readProgramHeaders();
|
|
|
|
|
2018-07-16 19:48:52 +00:00
|
|
|
uint32_t ShstrIndex = Ehdr.e_shstrndx;
|
|
|
|
if (ShstrIndex == SHN_XINDEX)
|
|
|
|
ShstrIndex = unwrapOrError(ElfFile.getSection(0))->sh_link;
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
Obj.SectionNames =
|
|
|
|
Obj.sections().template getSectionOfType<StringTableSection>(
|
2018-07-16 19:48:52 +00:00
|
|
|
ShstrIndex,
|
2018-03-07 20:33:02 +00:00
|
|
|
"e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
|
2018-01-25 22:46:17 +00:00
|
|
|
" in elf header " + " is invalid",
|
2018-03-07 20:33:02 +00:00
|
|
|
"e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
|
2018-01-25 22:46:17 +00:00
|
|
|
" in elf header " + " is not a string table");
|
2018-01-25 20:24:17 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
// A generic size function which computes sizes of any random access range.
|
|
|
|
template <class R> size_t size(R &&Range) {
|
|
|
|
return static_cast<size_t>(std::end(Range) - std::begin(Range));
|
|
|
|
}
|
|
|
|
|
|
|
|
Writer::~Writer() {}
|
|
|
|
|
|
|
|
Reader::~Reader() {}
|
|
|
|
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
std::unique_ptr<Object> BinaryReader::create() const {
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
return BinaryELFBuilder(MInfo.EMachine, MemBuf).build();
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Object> ELFReader::create() const {
|
2018-06-07 19:41:42 +00:00
|
|
|
auto Obj = llvm::make_unique<Object>();
|
2018-11-01 16:02:12 +00:00
|
|
|
if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(Bin)) {
|
|
|
|
ELFBuilder<ELF32LE> Builder(*O, *Obj);
|
2018-01-25 22:46:17 +00:00
|
|
|
Builder.build();
|
|
|
|
return Obj;
|
2018-11-01 16:02:12 +00:00
|
|
|
} else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(Bin)) {
|
|
|
|
ELFBuilder<ELF64LE> Builder(*O, *Obj);
|
2018-01-25 22:46:17 +00:00
|
|
|
Builder.build();
|
|
|
|
return Obj;
|
2018-11-01 16:02:12 +00:00
|
|
|
} else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(Bin)) {
|
|
|
|
ELFBuilder<ELF32BE> Builder(*O, *Obj);
|
2018-01-25 22:46:17 +00:00
|
|
|
Builder.build();
|
|
|
|
return Obj;
|
2018-11-01 16:02:12 +00:00
|
|
|
} else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(Bin)) {
|
|
|
|
ELFBuilder<ELF64BE> Builder(*O, *Obj);
|
2018-01-25 22:46:17 +00:00
|
|
|
Builder.build();
|
|
|
|
return Obj;
|
|
|
|
}
|
|
|
|
error("Invalid file type");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void ELFWriter<ELFT>::writeEhdr() {
|
2018-07-06 17:51:03 +00:00
|
|
|
uint8_t *B = Buf.getBufferStart();
|
|
|
|
Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(B);
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
std::fill(Ehdr.e_ident, Ehdr.e_ident + 16, 0);
|
|
|
|
Ehdr.e_ident[EI_MAG0] = 0x7f;
|
|
|
|
Ehdr.e_ident[EI_MAG1] = 'E';
|
|
|
|
Ehdr.e_ident[EI_MAG2] = 'L';
|
|
|
|
Ehdr.e_ident[EI_MAG3] = 'F';
|
|
|
|
Ehdr.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
|
|
|
|
Ehdr.e_ident[EI_DATA] =
|
|
|
|
ELFT::TargetEndianness == support::big ? ELFDATA2MSB : ELFDATA2LSB;
|
|
|
|
Ehdr.e_ident[EI_VERSION] = EV_CURRENT;
|
2018-12-20 10:51:42 +00:00
|
|
|
Ehdr.e_ident[EI_OSABI] = Obj.OSABI;
|
|
|
|
Ehdr.e_ident[EI_ABIVERSION] = Obj.ABIVersion;
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
Ehdr.e_type = Obj.Type;
|
|
|
|
Ehdr.e_machine = Obj.Machine;
|
|
|
|
Ehdr.e_version = Obj.Version;
|
|
|
|
Ehdr.e_entry = Obj.Entry;
|
2018-10-24 22:49:06 +00:00
|
|
|
// We have to use the fully-qualified name llvm::size
|
|
|
|
// since some compilers complain on ambiguous resolution.
|
|
|
|
Ehdr.e_phnum = llvm::size(Obj.segments());
|
2018-09-12 17:56:31 +00:00
|
|
|
Ehdr.e_phoff = (Ehdr.e_phnum != 0) ? Obj.ProgramHdrSegment.Offset : 0;
|
|
|
|
Ehdr.e_phentsize = (Ehdr.e_phnum != 0) ? sizeof(Elf_Phdr) : 0;
|
2018-01-25 22:46:17 +00:00
|
|
|
Ehdr.e_flags = Obj.Flags;
|
2017-08-04 21:09:26 +00:00
|
|
|
Ehdr.e_ehsize = sizeof(Elf_Ehdr);
|
2018-09-12 17:56:31 +00:00
|
|
|
if (WriteSectionHeaders && size(Obj.sections()) != 0) {
|
|
|
|
Ehdr.e_shentsize = sizeof(Elf_Shdr);
|
2018-01-25 22:46:17 +00:00
|
|
|
Ehdr.e_shoff = Obj.SHOffset;
|
2018-07-16 19:48:52 +00:00
|
|
|
// """
|
|
|
|
// If the number of sections is greater than or equal to
|
|
|
|
// SHN_LORESERVE (0xff00), this member has the value zero and the actual
|
|
|
|
// number of section header table entries is contained in the sh_size field
|
|
|
|
// of the section header at index 0.
|
|
|
|
// """
|
|
|
|
auto Shnum = size(Obj.sections()) + 1;
|
|
|
|
if (Shnum >= SHN_LORESERVE)
|
|
|
|
Ehdr.e_shnum = 0;
|
|
|
|
else
|
|
|
|
Ehdr.e_shnum = Shnum;
|
|
|
|
// """
|
|
|
|
// If the section name string table section index is greater than or equal
|
|
|
|
// to SHN_LORESERVE (0xff00), this member has the value SHN_XINDEX (0xffff)
|
|
|
|
// and the actual index of the section name string table section is
|
|
|
|
// contained in the sh_link field of the section header at index 0.
|
|
|
|
// """
|
|
|
|
if (Obj.SectionNames->Index >= SHN_LORESERVE)
|
|
|
|
Ehdr.e_shstrndx = SHN_XINDEX;
|
|
|
|
else
|
|
|
|
Ehdr.e_shstrndx = Obj.SectionNames->Index;
|
2017-10-11 18:09:18 +00:00
|
|
|
} else {
|
2018-09-12 17:56:31 +00:00
|
|
|
Ehdr.e_shentsize = 0;
|
2017-10-11 18:09:18 +00:00
|
|
|
Ehdr.e_shoff = 0;
|
|
|
|
Ehdr.e_shnum = 0;
|
|
|
|
Ehdr.e_shstrndx = 0;
|
|
|
|
}
|
2017-08-04 21:09:26 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
template <class ELFT> void ELFWriter<ELFT>::writePhdrs() {
|
|
|
|
for (auto &Seg : Obj.segments())
|
|
|
|
writePhdr(Seg);
|
2017-08-04 21:09:26 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
template <class ELFT> void ELFWriter<ELFT>::writeShdrs() {
|
2018-07-06 17:51:03 +00:00
|
|
|
uint8_t *B = Buf.getBufferStart() + Obj.SHOffset;
|
2017-08-04 21:09:26 +00:00
|
|
|
// This reference serves to write the dummy section header at the begining
|
2017-09-15 22:04:09 +00:00
|
|
|
// of the file. It is not used for anything else
|
2018-07-06 17:51:03 +00:00
|
|
|
Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B);
|
2017-08-04 21:09:26 +00:00
|
|
|
Shdr.sh_name = 0;
|
|
|
|
Shdr.sh_type = SHT_NULL;
|
|
|
|
Shdr.sh_flags = 0;
|
|
|
|
Shdr.sh_addr = 0;
|
|
|
|
Shdr.sh_offset = 0;
|
2018-07-16 19:48:52 +00:00
|
|
|
// See writeEhdr for why we do this.
|
|
|
|
uint64_t Shnum = size(Obj.sections()) + 1;
|
|
|
|
if (Shnum >= SHN_LORESERVE)
|
|
|
|
Shdr.sh_size = Shnum;
|
|
|
|
else
|
|
|
|
Shdr.sh_size = 0;
|
|
|
|
// See writeEhdr for why we do this.
|
|
|
|
if (Obj.SectionNames != nullptr && Obj.SectionNames->Index >= SHN_LORESERVE)
|
|
|
|
Shdr.sh_link = Obj.SectionNames->Index;
|
|
|
|
else
|
|
|
|
Shdr.sh_link = 0;
|
2017-08-04 21:09:26 +00:00
|
|
|
Shdr.sh_info = 0;
|
|
|
|
Shdr.sh_addralign = 0;
|
|
|
|
Shdr.sh_entsize = 0;
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
for (auto &Sec : Obj.sections())
|
|
|
|
writeShdr(Sec);
|
2017-08-04 21:09:26 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
template <class ELFT> void ELFWriter<ELFT>::writeSectionData() {
|
|
|
|
for (auto &Sec : Obj.sections())
|
|
|
|
Sec.accept(*SecWriter);
|
2017-08-04 21:09:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 15:20:36 +00:00
|
|
|
Error Object::removeSections(
|
|
|
|
std::function<bool(const SectionBase &)> ToRemove) {
|
2017-10-10 18:47:09 +00:00
|
|
|
|
|
|
|
auto Iter = std::stable_partition(
|
|
|
|
std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
|
|
|
|
if (ToRemove(*Sec))
|
|
|
|
return false;
|
2017-10-11 18:09:18 +00:00
|
|
|
if (auto RelSec = dyn_cast<RelocationSectionBase>(Sec.get())) {
|
|
|
|
if (auto ToRelSec = RelSec->getSection())
|
|
|
|
return !ToRemove(*ToRelSec);
|
|
|
|
}
|
2017-10-10 18:47:09 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
if (SymbolTable != nullptr && ToRemove(*SymbolTable))
|
|
|
|
SymbolTable = nullptr;
|
2018-07-16 19:48:52 +00:00
|
|
|
if (SectionNames != nullptr && ToRemove(*SectionNames))
|
2017-10-11 18:09:18 +00:00
|
|
|
SectionNames = nullptr;
|
2018-07-16 19:48:52 +00:00
|
|
|
if (SectionIndexTable != nullptr && ToRemove(*SectionIndexTable))
|
|
|
|
SectionIndexTable = nullptr;
|
2017-10-10 18:47:09 +00:00
|
|
|
// Now make sure there are no remaining references to the sections that will
|
|
|
|
// be removed. Sometimes it is impossible to remove a reference so we emit
|
|
|
|
// an error here instead.
|
[llvm-objcopy] Make removeSectionReferences batched
Summary:
Removing a large number of sections from a file with a lot of symbols can have abysmal (i.e. O(n^2)) performance, e.g. when running `--only-section` to extract one section out of a large file.
This comes from iterating over all symbols in the symbol table each time we remove a section, to remove references to the section we just removed.
Instead, do just one pass of symbol removal by passing a hash set of all the sections we'd like to remove references to.
This fixes a regression when running llvm-objcopy -j <one section> on an object file with many sections and symbols -- on my machine, running `objcopy -j .keep_me huge-input.o /tmp/foo.o` takes .3s with GNU objcopy, 1.3s with an updated llvm-objcopy, and 7+ minutes with llvm-objcopy prior to this patch.
Reviewers: MaskRay, jhenderson, jakehehrlich, alexshap, espindola
Reviewed By: MaskRay, jhenderson
Subscribers: echristo, emaste, arichardson, mgrang, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D58296
llvm-svn: 354597
2019-02-21 16:45:42 +00:00
|
|
|
std::unordered_set<const SectionBase *> RemoveSections;
|
|
|
|
RemoveSections.reserve(std::distance(Iter, std::end(Sections)));
|
2017-10-10 18:47:09 +00:00
|
|
|
for (auto &RemoveSec : make_range(Iter, std::end(Sections))) {
|
|
|
|
for (auto &Segment : Segments)
|
|
|
|
Segment->removeSection(RemoveSec.get());
|
[llvm-objcopy] Make removeSectionReferences batched
Summary:
Removing a large number of sections from a file with a lot of symbols can have abysmal (i.e. O(n^2)) performance, e.g. when running `--only-section` to extract one section out of a large file.
This comes from iterating over all symbols in the symbol table each time we remove a section, to remove references to the section we just removed.
Instead, do just one pass of symbol removal by passing a hash set of all the sections we'd like to remove references to.
This fixes a regression when running llvm-objcopy -j <one section> on an object file with many sections and symbols -- on my machine, running `objcopy -j .keep_me huge-input.o /tmp/foo.o` takes .3s with GNU objcopy, 1.3s with an updated llvm-objcopy, and 7+ minutes with llvm-objcopy prior to this patch.
Reviewers: MaskRay, jhenderson, jakehehrlich, alexshap, espindola
Reviewed By: MaskRay, jhenderson
Subscribers: echristo, emaste, arichardson, mgrang, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D58296
llvm-svn: 354597
2019-02-21 16:45:42 +00:00
|
|
|
RemoveSections.insert(RemoveSec.get());
|
2017-10-10 18:47:09 +00:00
|
|
|
}
|
2019-02-27 11:18:27 +00:00
|
|
|
|
|
|
|
// For each section that remains alive, we want to remove the dead references.
|
|
|
|
// This either might update the content of the section (e.g. remove symbols
|
|
|
|
// from symbol table that belongs to removed section) or trigger an error if
|
|
|
|
// a live section critically depends on a section being removed somehow
|
|
|
|
// (e.g. the removed section is referenced by a relocation).
|
|
|
|
for (auto &KeepSec : make_range(std::begin(Sections), Iter)) {
|
[llvm-objcopy] Make removeSectionReferences batched
Summary:
Removing a large number of sections from a file with a lot of symbols can have abysmal (i.e. O(n^2)) performance, e.g. when running `--only-section` to extract one section out of a large file.
This comes from iterating over all symbols in the symbol table each time we remove a section, to remove references to the section we just removed.
Instead, do just one pass of symbol removal by passing a hash set of all the sections we'd like to remove references to.
This fixes a regression when running llvm-objcopy -j <one section> on an object file with many sections and symbols -- on my machine, running `objcopy -j .keep_me huge-input.o /tmp/foo.o` takes .3s with GNU objcopy, 1.3s with an updated llvm-objcopy, and 7+ minutes with llvm-objcopy prior to this patch.
Reviewers: MaskRay, jhenderson, jakehehrlich, alexshap, espindola
Reviewed By: MaskRay, jhenderson
Subscribers: echristo, emaste, arichardson, mgrang, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D58296
llvm-svn: 354597
2019-02-21 16:45:42 +00:00
|
|
|
if (Error E = KeepSec->removeSectionReferences(
|
|
|
|
[&RemoveSections](const SectionBase *Sec) {
|
|
|
|
return RemoveSections.find(Sec) != RemoveSections.end();
|
|
|
|
}))
|
|
|
|
return E;
|
2019-02-27 11:18:27 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 18:47:09 +00:00
|
|
|
// Now finally get rid of them all togethor.
|
|
|
|
Sections.erase(Iter, std::end(Sections));
|
2019-02-01 15:20:36 +00:00
|
|
|
return Error::success();
|
2017-10-10 18:47:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 15:20:36 +00:00
|
|
|
Error Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
|
|
|
|
if (SymbolTable)
|
|
|
|
for (const SecPtr &Sec : Sections)
|
|
|
|
if (Error E = Sec->removeSymbols(ToRemove))
|
|
|
|
return E;
|
|
|
|
return Error::success();
|
2018-05-09 21:36:54 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
void Object::sortSections() {
|
2017-08-01 00:33:58 +00:00
|
|
|
// Put all sections in offset order. Maintain the ordering as closely as
|
|
|
|
// possible while meeting that demand however.
|
|
|
|
auto CompareSections = [](const SecPtr &A, const SecPtr &B) {
|
|
|
|
return A->OriginalOffset < B->OriginalOffset;
|
|
|
|
};
|
2017-08-04 21:09:26 +00:00
|
|
|
std::stable_sort(std::begin(this->Sections), std::end(this->Sections),
|
|
|
|
CompareSections);
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 23:24:04 +00:00
|
|
|
static uint64_t alignToAddr(uint64_t Offset, uint64_t Addr, uint64_t Align) {
|
|
|
|
// Calculate Diff such that (Offset + Diff) & -Align == Addr & -Align.
|
|
|
|
if (Align == 0)
|
|
|
|
Align = 1;
|
|
|
|
auto Diff =
|
|
|
|
static_cast<int64_t>(Addr % Align) - static_cast<int64_t>(Offset % Align);
|
|
|
|
// We only want to add to Offset, however, so if Diff < 0 we can add Align and
|
|
|
|
// (Offset + Diff) & -Align == Addr & -Align will still hold.
|
|
|
|
if (Diff < 0)
|
|
|
|
Diff += Align;
|
|
|
|
return Offset + Diff;
|
|
|
|
}
|
|
|
|
|
2017-11-15 19:13:31 +00:00
|
|
|
// Orders segments such that if x = y->ParentSegment then y comes before x.
|
2018-11-01 16:02:12 +00:00
|
|
|
static void orderSegments(std::vector<Segment *> &Segments) {
|
2018-01-22 19:27:30 +00:00
|
|
|
std::stable_sort(std::begin(Segments), std::end(Segments),
|
|
|
|
compareSegmentsByOffset);
|
2017-11-15 19:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function finds a consistent layout for a list of segments starting from
|
|
|
|
// an Offset. It assumes that Segments have been sorted by OrderSegments and
|
|
|
|
// returns an Offset one past the end of the last segment.
|
|
|
|
static uint64_t LayoutSegments(std::vector<Segment *> &Segments,
|
|
|
|
uint64_t Offset) {
|
|
|
|
assert(std::is_sorted(std::begin(Segments), std::end(Segments),
|
2018-01-22 19:27:30 +00:00
|
|
|
compareSegmentsByOffset));
|
2017-08-26 01:32:20 +00:00
|
|
|
// The only way a segment should move is if a section was between two
|
|
|
|
// segments and that section was removed. If that section isn't in a segment
|
|
|
|
// then it's acceptable, but not ideal, to simply move it to after the
|
|
|
|
// segments. So we can simply layout segments one after the other accounting
|
|
|
|
// for alignment.
|
2017-11-15 19:13:31 +00:00
|
|
|
for (auto &Segment : Segments) {
|
2017-09-19 21:37:35 +00:00
|
|
|
// We assume that segments have been ordered by OriginalOffset and Index
|
|
|
|
// such that a parent segment will always come before a child segment in
|
|
|
|
// OrderedSegments. This means that the Offset of the ParentSegment should
|
|
|
|
// already be set and we can set our offset relative to it.
|
|
|
|
if (Segment->ParentSegment != nullptr) {
|
|
|
|
auto Parent = Segment->ParentSegment;
|
|
|
|
Segment->Offset =
|
|
|
|
Parent->Offset + Segment->OriginalOffset - Parent->OriginalOffset;
|
|
|
|
} else {
|
2017-11-02 23:24:04 +00:00
|
|
|
Offset = alignToAddr(Offset, Segment->VAddr, Segment->Align);
|
2017-09-19 21:37:35 +00:00
|
|
|
Segment->Offset = Offset;
|
|
|
|
}
|
2017-10-04 17:44:42 +00:00
|
|
|
Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
|
2017-08-26 01:32:20 +00:00
|
|
|
}
|
2017-11-15 19:13:31 +00:00
|
|
|
return Offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function finds a consistent layout for a list of sections. It assumes
|
|
|
|
// that the ->ParentSegment of each section has already been laid out. The
|
|
|
|
// supplied starting Offset is used for the starting offset of any section that
|
|
|
|
// does not have a ParentSegment. It returns either the offset given if all
|
|
|
|
// sections had a ParentSegment or an offset one past the last section if there
|
|
|
|
// was a section that didn't have a ParentSegment.
|
2018-01-25 22:46:17 +00:00
|
|
|
template <class Range>
|
2018-11-01 16:02:12 +00:00
|
|
|
static uint64_t layoutSections(Range Sections, uint64_t Offset) {
|
2017-08-26 01:32:20 +00:00
|
|
|
// Now the offset of every segment has been set we can assign the offsets
|
|
|
|
// of each section. For sections that are covered by a segment we should use
|
|
|
|
// the segment's original offset and the section's original offset to compute
|
|
|
|
// the offset from the start of the segment. Using the offset from the start
|
|
|
|
// of the segment we can assign a new offset to the section. For sections not
|
|
|
|
// covered by segments we can just bump Offset to the next valid location.
|
|
|
|
uint32_t Index = 1;
|
2017-11-15 19:13:31 +00:00
|
|
|
for (auto &Section : Sections) {
|
2018-01-25 22:46:17 +00:00
|
|
|
Section.Index = Index++;
|
|
|
|
if (Section.ParentSegment != nullptr) {
|
|
|
|
auto Segment = *Section.ParentSegment;
|
|
|
|
Section.Offset =
|
|
|
|
Segment.Offset + (Section.OriginalOffset - Segment.OriginalOffset);
|
2017-08-26 01:32:20 +00:00
|
|
|
} else {
|
2018-01-25 22:46:17 +00:00
|
|
|
Offset = alignTo(Offset, Section.Align == 0 ? 1 : Section.Align);
|
|
|
|
Section.Offset = Offset;
|
|
|
|
if (Section.Type != SHT_NOBITS)
|
|
|
|
Offset += Section.Size;
|
2017-08-26 01:32:20 +00:00
|
|
|
}
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
2017-11-15 19:13:31 +00:00
|
|
|
return Offset;
|
|
|
|
}
|
2017-08-26 01:32:20 +00:00
|
|
|
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
template <class ELFT> void ELFWriter<ELFT>::initEhdrSegment() {
|
|
|
|
auto &ElfHdr = Obj.ElfHdrSegment;
|
|
|
|
ElfHdr.Type = PT_PHDR;
|
|
|
|
ElfHdr.Flags = 0;
|
|
|
|
ElfHdr.OriginalOffset = ElfHdr.Offset = 0;
|
|
|
|
ElfHdr.VAddr = 0;
|
|
|
|
ElfHdr.PAddr = 0;
|
|
|
|
ElfHdr.FileSize = ElfHdr.MemSize = sizeof(Elf_Ehdr);
|
|
|
|
ElfHdr.Align = 0;
|
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
template <class ELFT> void ELFWriter<ELFT>::assignOffsets() {
|
2017-11-15 19:13:31 +00:00
|
|
|
// We need a temporary list of segments that has a special order to it
|
|
|
|
// so that we know that anytime ->ParentSegment is set that segment has
|
|
|
|
// already had its offset properly set.
|
|
|
|
std::vector<Segment *> OrderedSegments;
|
2018-01-25 22:46:17 +00:00
|
|
|
for (auto &Segment : Obj.segments())
|
|
|
|
OrderedSegments.push_back(&Segment);
|
2018-02-14 23:31:33 +00:00
|
|
|
OrderedSegments.push_back(&Obj.ElfHdrSegment);
|
|
|
|
OrderedSegments.push_back(&Obj.ProgramHdrSegment);
|
2018-11-01 16:02:12 +00:00
|
|
|
orderSegments(OrderedSegments);
|
2018-02-14 23:31:33 +00:00
|
|
|
// Offset is used as the start offset of the first segment to be laid out.
|
|
|
|
// Since the ELF Header (ElfHdrSegment) must be at the start of the file,
|
|
|
|
// we start at offset 0.
|
|
|
|
uint64_t Offset = 0;
|
2017-11-15 19:13:31 +00:00
|
|
|
Offset = LayoutSegments(OrderedSegments, Offset);
|
2018-11-01 16:02:12 +00:00
|
|
|
Offset = layoutSections(Obj.sections(), Offset);
|
2017-11-15 19:13:31 +00:00
|
|
|
// If we need to write the section header table out then we need to align the
|
|
|
|
// Offset so that SHOffset is valid.
|
2018-01-25 22:46:17 +00:00
|
|
|
if (WriteSectionHeaders)
|
2018-08-10 16:25:58 +00:00
|
|
|
Offset = alignTo(Offset, sizeof(Elf_Addr));
|
2018-01-25 22:46:17 +00:00
|
|
|
Obj.SHOffset = Offset;
|
2017-08-01 05:18:30 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
template <class ELFT> size_t ELFWriter<ELFT>::totalSize() const {
|
2017-08-04 21:09:26 +00:00
|
|
|
// We already have the section header offset so we can calculate the total
|
|
|
|
// size by just adding up the size of each section header.
|
2018-01-25 22:46:17 +00:00
|
|
|
auto NullSectionSize = WriteSectionHeaders ? sizeof(Elf_Shdr) : 0;
|
|
|
|
return Obj.SHOffset + size(Obj.sections()) * sizeof(Elf_Shdr) +
|
2017-10-11 18:09:18 +00:00
|
|
|
NullSectionSize;
|
2017-08-04 21:09:26 +00:00
|
|
|
}
|
|
|
|
|
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
2019-01-22 23:49:16 +00:00
|
|
|
template <class ELFT> Error ELFWriter<ELFT>::write() {
|
2018-01-25 22:46:17 +00:00
|
|
|
writeEhdr();
|
|
|
|
writePhdrs();
|
|
|
|
writeSectionData();
|
|
|
|
if (WriteSectionHeaders)
|
|
|
|
writeShdrs();
|
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
2019-01-22 23:49:16 +00:00
|
|
|
return Buf.commit();
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
|
|
|
|
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
2019-01-22 23:49:16 +00:00
|
|
|
template <class ELFT> Error ELFWriter<ELFT>::finalize() {
|
2018-01-25 22:46:17 +00:00
|
|
|
// It could happen that SectionNames has been removed and yet the user wants
|
|
|
|
// a section header table output. We need to throw an error if a user tries
|
|
|
|
// to do that.
|
|
|
|
if (Obj.SectionNames == nullptr && WriteSectionHeaders)
|
2019-02-01 15:20:36 +00:00
|
|
|
return createStringError(llvm::errc::invalid_argument,
|
|
|
|
"Cannot write section header table because "
|
|
|
|
"section header string table was removed.");
|
2018-01-25 22:46:17 +00:00
|
|
|
|
2018-07-16 19:48:52 +00:00
|
|
|
Obj.sortSections();
|
|
|
|
|
|
|
|
// We need to assign indexes before we perform layout because we need to know
|
|
|
|
// if we need large indexes or not. We can assign indexes first and check as
|
|
|
|
// we go to see if we will actully need large indexes.
|
|
|
|
bool NeedsLargeIndexes = false;
|
|
|
|
if (size(Obj.sections()) >= SHN_LORESERVE) {
|
|
|
|
auto Sections = Obj.sections();
|
|
|
|
NeedsLargeIndexes =
|
|
|
|
std::any_of(Sections.begin() + SHN_LORESERVE, Sections.end(),
|
|
|
|
[](const SectionBase &Sec) { return Sec.HasSymbol; });
|
|
|
|
// TODO: handle case where only one section needs the large index table but
|
|
|
|
// only needs it because the large index table hasn't been removed yet.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NeedsLargeIndexes) {
|
|
|
|
// This means we definitely need to have a section index table but if we
|
|
|
|
// already have one then we should use it instead of making a new one.
|
|
|
|
if (Obj.SymbolTable != nullptr && Obj.SectionIndexTable == nullptr) {
|
|
|
|
// Addition of a section to the end does not invalidate the indexes of
|
|
|
|
// other sections and assigns the correct index to the new section.
|
|
|
|
auto &Shndx = Obj.addSection<SectionIndexSection>();
|
|
|
|
Obj.SymbolTable->setShndxTable(&Shndx);
|
|
|
|
Shndx.setSymTab(Obj.SymbolTable);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Since we don't need SectionIndexTable we should remove it and all
|
|
|
|
// references to it.
|
|
|
|
if (Obj.SectionIndexTable != nullptr) {
|
2019-02-01 15:20:36 +00:00
|
|
|
if (Error E = Obj.removeSections([this](const SectionBase &Sec) {
|
|
|
|
return &Sec == Obj.SectionIndexTable;
|
|
|
|
}))
|
|
|
|
return E;
|
2018-07-16 19:48:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we add the names of all the sections. Importantly this must be
|
|
|
|
// done after we decide to add or remove SectionIndexes.
|
2018-01-25 22:46:17 +00:00
|
|
|
if (Obj.SectionNames != nullptr)
|
|
|
|
for (const auto &Section : Obj.sections()) {
|
|
|
|
Obj.SectionNames->addString(Section.Name);
|
2017-10-11 18:09:18 +00:00
|
|
|
}
|
2018-07-16 19:48:52 +00:00
|
|
|
|
[llvm-objcopy] Add support for -I binary -B <arch>.
Summary:
The -I (--input-target) and -B (--binary-architecture) flags exist but are currently silently ignored. This adds support for -I binary for architectures i386, x86-64 (and alias i386:x86-64), arm, aarch64, sparc, and ppc (powerpc:common64). This is largely based on D41687.
This is done by implementing an additional subclass of Reader, BinaryReader, which works by interpreting the input file as contents for .data field, sets up a synthetic header, and adds additional sections/symbols (e.g. _binary__tmp_data_txt_start).
Reviewers: jakehehrlich, alexshap, jhenderson, javed.absar
Reviewed By: jhenderson
Subscribers: jyknight, nemanjai, kbarton, fedor.sergeev, jrtc27, kristof.beyls, paulsemel, llvm-commits
Differential Revision: https://reviews.llvm.org/D50343
llvm-svn: 340070
2018-08-17 18:51:11 +00:00
|
|
|
initEhdrSegment();
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
|
2018-07-16 19:48:52 +00:00
|
|
|
// Before we can prepare for layout the indexes need to be finalized.
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
// Also, the output arch may not be the same as the input arch, so fix up
|
|
|
|
// size-related fields before doing layout calculations.
|
2018-07-16 19:48:52 +00:00
|
|
|
uint64_t Index = 0;
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
auto SecSizer = llvm::make_unique<ELFSectionSizer<ELFT>>();
|
|
|
|
for (auto &Sec : Obj.sections()) {
|
2018-07-16 19:48:52 +00:00
|
|
|
Sec.Index = Index++;
|
[llvm-objcopy][ELF] Implement a mutable section visitor that updates size-related fields (Size, EntrySize, Align) before layout.
Summary:
Fix EntrySize, Size, and Align before doing layout calculation.
As a side cleanup, this removes a dependence on sizeof(Elf_Sym) within BinaryReader, so we can untemplatize that.
This unblocks a cleaner implementation of handling the -O<format> flag. See D53667 for a previous attempt. Actual implementation of the -O<format> flag will come in an upcoming commit, this is largely a NFC (although not _totally_ one, because alignment on binary input was actually wrong before).
Reviewers: jakehehrlich, jhenderson, alexshap, espindola
Reviewed By: jhenderson
Subscribers: emaste, arichardson, llvm-commits
Differential Revision: https://reviews.llvm.org/D56211
llvm-svn: 350336
2019-01-03 17:45:30 +00:00
|
|
|
Sec.accept(*SecSizer);
|
|
|
|
}
|
2018-07-16 19:48:52 +00:00
|
|
|
|
|
|
|
// The symbol table does not update all other sections on update. For
|
|
|
|
// instance, symbol names are not added as new symbols are added. This means
|
|
|
|
// that some sections, like .strtab, don't yet have their final size.
|
2018-01-25 22:46:17 +00:00
|
|
|
if (Obj.SymbolTable != nullptr)
|
2018-07-16 19:48:52 +00:00
|
|
|
Obj.SymbolTable->prepareForLayout();
|
2017-08-01 00:33:58 +00:00
|
|
|
|
2019-03-18 14:27:41 +00:00
|
|
|
// Now that all strings are added we want to finalize string table builders,
|
|
|
|
// because that affects section sizes which in turn affects section offsets.
|
|
|
|
for (auto &Sec : Obj.sections())
|
|
|
|
if (auto StrTab = dyn_cast<StringTableSection>(&Sec))
|
|
|
|
StrTab->prepareForLayout();
|
|
|
|
|
2017-08-01 00:33:58 +00:00
|
|
|
assignOffsets();
|
|
|
|
|
|
|
|
// Finally now that all offsets and indexes have been set we can finalize any
|
|
|
|
// remaining issues.
|
2018-01-25 22:46:17 +00:00
|
|
|
uint64_t Offset = Obj.SHOffset + sizeof(Elf_Shdr);
|
|
|
|
for (auto &Section : Obj.sections()) {
|
|
|
|
Section.HeaderOffset = Offset;
|
2017-08-01 00:33:58 +00:00
|
|
|
Offset += sizeof(Elf_Shdr);
|
2018-01-25 22:46:17 +00:00
|
|
|
if (WriteSectionHeaders)
|
|
|
|
Section.NameIndex = Obj.SectionNames->findIndex(Section.Name);
|
|
|
|
Section.finalize();
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
|
|
|
|
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
2019-01-22 23:49:16 +00:00
|
|
|
if (Error E = Buf.allocate(totalSize()))
|
|
|
|
return E;
|
2018-07-06 17:51:03 +00:00
|
|
|
SecWriter = llvm::make_unique<ELFSectionWriter<ELFT>>(Buf);
|
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
2019-01-22 23:49:16 +00:00
|
|
|
return Error::success();
|
2017-08-02 00:03:33 +00:00
|
|
|
}
|
|
|
|
|
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
2019-01-22 23:49:16 +00:00
|
|
|
Error BinaryWriter::write() {
|
2018-01-25 22:46:17 +00:00
|
|
|
for (auto &Section : Obj.sections()) {
|
|
|
|
if ((Section.Flags & SHF_ALLOC) == 0)
|
2017-11-15 19:13:31 +00:00
|
|
|
continue;
|
2018-01-25 22:46:17 +00:00
|
|
|
Section.accept(*SecWriter);
|
2017-08-04 21:09:26 +00:00
|
|
|
}
|
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
2019-01-22 23:49:16 +00:00
|
|
|
return Buf.commit();
|
2017-08-04 05:33:44 +00:00
|
|
|
}
|
|
|
|
|
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
2019-01-22 23:49:16 +00:00
|
|
|
Error BinaryWriter::finalize() {
|
2017-11-15 19:13:31 +00:00
|
|
|
// TODO: Create a filter range to construct OrderedSegments from so that this
|
|
|
|
// code can be deduped with assignOffsets above. This should also solve the
|
|
|
|
// todo below for LayoutSections.
|
|
|
|
// We need a temporary list of segments that has a special order to it
|
|
|
|
// so that we know that anytime ->ParentSegment is set that segment has
|
|
|
|
// already had it's offset properly set. We only want to consider the segments
|
|
|
|
// that will affect layout of allocated sections so we only add those.
|
|
|
|
std::vector<Segment *> OrderedSegments;
|
2018-01-25 22:46:17 +00:00
|
|
|
for (auto &Section : Obj.sections()) {
|
|
|
|
if ((Section.Flags & SHF_ALLOC) != 0 && Section.ParentSegment != nullptr) {
|
|
|
|
OrderedSegments.push_back(Section.ParentSegment);
|
2017-08-04 21:09:26 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-22 19:27:30 +00:00
|
|
|
|
|
|
|
// For binary output, we're going to use physical addresses instead of
|
|
|
|
// virtual addresses, since a binary output is used for cases like ROM
|
|
|
|
// loading and physical addresses are intended for ROM loading.
|
|
|
|
// However, if no segment has a physical address, we'll fallback to using
|
|
|
|
// virtual addresses for all.
|
2018-11-17 01:15:55 +00:00
|
|
|
if (all_of(OrderedSegments,
|
|
|
|
[](const Segment *Seg) { return Seg->PAddr == 0; }))
|
|
|
|
for (Segment *Seg : OrderedSegments)
|
|
|
|
Seg->PAddr = Seg->VAddr;
|
2018-01-22 19:27:30 +00:00
|
|
|
|
|
|
|
std::stable_sort(std::begin(OrderedSegments), std::end(OrderedSegments),
|
|
|
|
compareSegmentsByPAddr);
|
|
|
|
|
2017-11-15 19:13:31 +00:00
|
|
|
// Because we add a ParentSegment for each section we might have duplicate
|
|
|
|
// segments in OrderedSegments. If there were duplicates then LayoutSegments
|
|
|
|
// would do very strange things.
|
|
|
|
auto End =
|
|
|
|
std::unique(std::begin(OrderedSegments), std::end(OrderedSegments));
|
|
|
|
OrderedSegments.erase(End, std::end(OrderedSegments));
|
|
|
|
|
2018-01-22 19:27:30 +00:00
|
|
|
uint64_t Offset = 0;
|
|
|
|
|
2017-11-15 19:13:31 +00:00
|
|
|
// Modify the first segment so that there is no gap at the start. This allows
|
2018-11-17 01:15:55 +00:00
|
|
|
// our layout algorithm to proceed as expected while not writing out the gap
|
|
|
|
// at the start.
|
2017-11-15 19:13:31 +00:00
|
|
|
if (!OrderedSegments.empty()) {
|
|
|
|
auto Seg = OrderedSegments[0];
|
|
|
|
auto Sec = Seg->firstSection();
|
|
|
|
auto Diff = Sec->OriginalOffset - Seg->OriginalOffset;
|
|
|
|
Seg->OriginalOffset += Diff;
|
2018-01-22 19:27:30 +00:00
|
|
|
// The size needs to be shrunk as well.
|
2017-11-15 19:13:31 +00:00
|
|
|
Seg->FileSize -= Diff;
|
2018-01-22 19:27:30 +00:00
|
|
|
// The PAddr needs to be increased to remove the gap before the first
|
|
|
|
// section.
|
|
|
|
Seg->PAddr += Diff;
|
|
|
|
uint64_t LowestPAddr = Seg->PAddr;
|
|
|
|
for (auto &Segment : OrderedSegments) {
|
|
|
|
Segment->Offset = Segment->PAddr - LowestPAddr;
|
|
|
|
Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
|
|
|
|
}
|
2017-11-15 19:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: generalize LayoutSections to take a range. Pass a special range
|
|
|
|
// constructed from an iterator that skips values for which a predicate does
|
|
|
|
// not hold. Then pass such a range to LayoutSections instead of constructing
|
|
|
|
// AllocatedSections here.
|
|
|
|
std::vector<SectionBase *> AllocatedSections;
|
2018-01-25 22:46:17 +00:00
|
|
|
for (auto &Section : Obj.sections()) {
|
|
|
|
if ((Section.Flags & SHF_ALLOC) == 0)
|
2017-11-15 19:13:31 +00:00
|
|
|
continue;
|
2018-01-25 22:46:17 +00:00
|
|
|
AllocatedSections.push_back(&Section);
|
2017-11-15 19:13:31 +00:00
|
|
|
}
|
2018-11-01 16:02:12 +00:00
|
|
|
layoutSections(make_pointee_range(AllocatedSections), Offset);
|
2017-11-15 19:13:31 +00:00
|
|
|
|
|
|
|
// Now that every section has been laid out we just need to compute the total
|
|
|
|
// file size. This might not be the same as the offset returned by
|
|
|
|
// LayoutSections, because we want to truncate the last segment to the end of
|
|
|
|
// its last section, to match GNU objcopy's behaviour.
|
|
|
|
TotalSize = 0;
|
|
|
|
for (const auto &Section : AllocatedSections) {
|
|
|
|
if (Section->Type != SHT_NOBITS)
|
|
|
|
TotalSize = std::max(TotalSize, Section->Offset + Section->Size);
|
|
|
|
}
|
2018-01-25 22:46:17 +00:00
|
|
|
|
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
2019-01-22 23:49:16 +00:00
|
|
|
if (Error E = Buf.allocate(TotalSize))
|
|
|
|
return E;
|
2018-07-06 17:51:03 +00:00
|
|
|
SecWriter = llvm::make_unique<BinarySectionWriter>(Buf);
|
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
2019-01-22 23:49:16 +00:00
|
|
|
return Error::success();
|
2017-08-01 05:31:50 +00:00
|
|
|
}
|
2017-08-01 00:33:58 +00:00
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
template class ELFBuilder<ELF64LE>;
|
|
|
|
template class ELFBuilder<ELF64BE>;
|
|
|
|
template class ELFBuilder<ELF32LE>;
|
|
|
|
template class ELFBuilder<ELF32BE>;
|
2017-08-04 21:09:26 +00:00
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
template class ELFWriter<ELF64LE>;
|
|
|
|
template class ELFWriter<ELF64BE>;
|
|
|
|
template class ELFWriter<ELF32LE>;
|
|
|
|
template class ELFWriter<ELF32BE>;
|
2018-10-24 22:49:06 +00:00
|
|
|
|
|
|
|
} // end namespace elf
|
2018-07-18 00:10:51 +00:00
|
|
|
} // end namespace objcopy
|
2017-11-01 21:16:06 +00:00
|
|
|
} // end namespace llvm
|