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"
|
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"
|
2020-09-19 18:53:44 +03:00
|
|
|
#include "llvm/Object/ELF.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-05-29 11:37:16 +00:00
|
|
|
#include "llvm/Support/Endian.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) {
|
2020-10-24 17:35:55 +03:00
|
|
|
uint8_t *B = reinterpret_cast<uint8_t *>(Buf->getBufferStart()) +
|
|
|
|
Obj.ProgramHdrSegment.Offset + Seg.Index * sizeof(Elf_Phdr);
|
2018-07-06 17:51:03 +00:00
|
|
|
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(
|
2020-10-06 14:55:45 +03:00
|
|
|
bool, function_ref<bool(const SectionBase *)>) {
|
2019-02-01 15:20:36 +00:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2020-10-06 14:55:45 +03:00
|
|
|
Error SectionBase::removeSymbols(function_ref<bool(const Symbol &)>) {
|
2019-02-01 15:20:36 +00:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error SectionBase::initialize(SectionTableRef) { return Error::success(); }
|
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 *> &) {}
|
2020-05-29 19:27:28 +07:00
|
|
|
void SectionBase::onRemove() {}
|
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) {
|
2020-10-24 17:35:55 +03:00
|
|
|
uint8_t *B =
|
|
|
|
reinterpret_cast<uint8_t *>(Buf->getBufferStart()) + 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;
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
template <class ELFT> Error ELFSectionSizer<ELFT>::visit(Section &) {
|
|
|
|
return Error::success();
|
|
|
|
}
|
[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
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
template <class ELFT> Error ELFSectionSizer<ELFT>::visit(OwnedDataSection &) {
|
|
|
|
return Error::success();
|
|
|
|
}
|
[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
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
template <class ELFT> Error ELFSectionSizer<ELFT>::visit(StringTableSection &) {
|
|
|
|
return Error::success();
|
|
|
|
}
|
[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>
|
2020-09-19 18:53:44 +03:00
|
|
|
Error ELFSectionSizer<ELFT>::visit(DynamicRelocationSection &) {
|
|
|
|
return Error::success();
|
|
|
|
}
|
[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>
|
2020-09-19 18:53:44 +03:00
|
|
|
Error ELFSectionSizer<ELFT>::visit(SymbolTableSection &Sec) {
|
[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.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);
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
[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>
|
2020-09-19 18:53:44 +03:00
|
|
|
Error ELFSectionSizer<ELFT>::visit(RelocationSection &Sec) {
|
[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.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);
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
[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>
|
2020-09-19 18:53:44 +03:00
|
|
|
Error ELFSectionSizer<ELFT>::visit(GnuDebugLinkSection &) {
|
|
|
|
return Error::success();
|
|
|
|
}
|
[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
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
template <class ELFT> Error ELFSectionSizer<ELFT>::visit(GroupSection &Sec) {
|
2020-05-29 19:28:37 +07:00
|
|
|
Sec.Size = sizeof(Elf_Word) + Sec.GroupMembers.size() * sizeof(Elf_Word);
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2020-05-29 19:28:37 +07: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
|
|
|
|
|
|
|
template <class ELFT>
|
2020-09-19 18:53:44 +03:00
|
|
|
Error ELFSectionSizer<ELFT>::visit(SectionIndexSection &) {
|
|
|
|
return Error::success();
|
|
|
|
}
|
[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
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
template <class ELFT> Error ELFSectionSizer<ELFT>::visit(CompressedSection &) {
|
|
|
|
return Error::success();
|
|
|
|
}
|
[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>
|
2020-09-19 18:53:44 +03:00
|
|
|
Error ELFSectionSizer<ELFT>::visit(DecompressedSection &) {
|
|
|
|
return Error::success();
|
|
|
|
}
|
2018-01-25 22:46:17 +00:00
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error BinarySectionWriter::visit(const SectionIndexSection &Sec) {
|
|
|
|
return createStringError(errc::operation_not_permitted,
|
|
|
|
"cannot write symbol section index table '" +
|
|
|
|
Sec.Name + "' ");
|
2018-07-16 19:48:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error BinarySectionWriter::visit(const SymbolTableSection &Sec) {
|
|
|
|
return createStringError(errc::operation_not_permitted,
|
|
|
|
"cannot write symbol table '" + Sec.Name +
|
|
|
|
"' out to binary");
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error BinarySectionWriter::visit(const RelocationSection &Sec) {
|
|
|
|
return createStringError(errc::operation_not_permitted,
|
|
|
|
"cannot write relocation section '" + Sec.Name +
|
|
|
|
"' out to binary");
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) {
|
|
|
|
return createStringError(errc::operation_not_permitted,
|
|
|
|
"cannot write '" + Sec.Name + "' out to binary");
|
2018-03-21 19:53:44 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error BinarySectionWriter::visit(const GroupSection &Sec) {
|
|
|
|
return createStringError(errc::operation_not_permitted,
|
|
|
|
"cannot write '" + Sec.Name + "' out to binary");
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error SectionWriter::visit(const Section &Sec) {
|
2019-05-23 09:18:57 +00:00
|
|
|
if (Sec.Type != SHT_NOBITS)
|
|
|
|
llvm::copy(Sec.Contents, Out.getBufferStart() + Sec.Offset);
|
2020-09-19 18:53:44 +03:00
|
|
|
|
|
|
|
return Error::success();
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 11:37:16 +00:00
|
|
|
static bool addressOverflows32bit(uint64_t Addr) {
|
|
|
|
// Sign extended 32 bit addresses (e.g 0xFFFFFFFF80000000) are ok
|
|
|
|
return Addr > UINT32_MAX && Addr + 0x80000000 > UINT32_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T> static T checkedGetHex(StringRef S) {
|
|
|
|
T Value;
|
|
|
|
bool Fail = S.getAsInteger(16, Value);
|
|
|
|
assert(!Fail);
|
|
|
|
(void)Fail;
|
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fills exactly Len bytes of buffer with hexadecimal characters
|
|
|
|
// representing value 'X'
|
|
|
|
template <class T, class Iterator>
|
|
|
|
static Iterator utohexstr(T X, Iterator It, size_t Len) {
|
|
|
|
// Fill range with '0'
|
|
|
|
std::fill(It, It + Len, '0');
|
|
|
|
|
|
|
|
for (long I = Len - 1; I >= 0; --I) {
|
|
|
|
unsigned char Mod = static_cast<unsigned char>(X) & 15;
|
|
|
|
*(It + I) = hexdigit(Mod, false);
|
|
|
|
X >>= 4;
|
|
|
|
}
|
|
|
|
assert(X == 0);
|
|
|
|
return It + Len;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t IHexRecord::getChecksum(StringRef S) {
|
|
|
|
assert((S.size() & 1) == 0);
|
|
|
|
uint8_t Checksum = 0;
|
|
|
|
while (!S.empty()) {
|
|
|
|
Checksum += checkedGetHex<uint8_t>(S.take_front(2));
|
|
|
|
S = S.drop_front(2);
|
|
|
|
}
|
|
|
|
return -Checksum;
|
|
|
|
}
|
|
|
|
|
|
|
|
IHexLineData IHexRecord::getLine(uint8_t Type, uint16_t Addr,
|
|
|
|
ArrayRef<uint8_t> Data) {
|
|
|
|
IHexLineData Line(getLineLength(Data.size()));
|
|
|
|
assert(Line.size());
|
|
|
|
auto Iter = Line.begin();
|
|
|
|
*Iter++ = ':';
|
|
|
|
Iter = utohexstr(Data.size(), Iter, 2);
|
|
|
|
Iter = utohexstr(Addr, Iter, 4);
|
|
|
|
Iter = utohexstr(Type, Iter, 2);
|
|
|
|
for (uint8_t X : Data)
|
|
|
|
Iter = utohexstr(X, Iter, 2);
|
|
|
|
StringRef S(Line.data() + 1, std::distance(Line.begin() + 1, Iter));
|
|
|
|
Iter = utohexstr(getChecksum(S), Iter, 2);
|
|
|
|
*Iter++ = '\r';
|
|
|
|
*Iter++ = '\n';
|
|
|
|
assert(Iter == Line.end());
|
|
|
|
return Line;
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:56:14 +00:00
|
|
|
static Error checkRecord(const IHexRecord &R) {
|
|
|
|
switch (R.Type) {
|
|
|
|
case IHexRecord::Data:
|
|
|
|
if (R.HexData.size() == 0)
|
|
|
|
return createStringError(
|
|
|
|
errc::invalid_argument,
|
|
|
|
"zero data length is not allowed for data records");
|
|
|
|
break;
|
|
|
|
case IHexRecord::EndOfFile:
|
|
|
|
break;
|
|
|
|
case IHexRecord::SegmentAddr:
|
|
|
|
// 20-bit segment address. Data length must be 2 bytes
|
|
|
|
// (4 bytes in hex)
|
|
|
|
if (R.HexData.size() != 4)
|
|
|
|
return createStringError(
|
|
|
|
errc::invalid_argument,
|
|
|
|
"segment address data should be 2 bytes in size");
|
|
|
|
break;
|
|
|
|
case IHexRecord::StartAddr80x86:
|
|
|
|
case IHexRecord::StartAddr:
|
|
|
|
if (R.HexData.size() != 8)
|
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
"start address data should be 4 bytes in size");
|
|
|
|
// According to Intel HEX specification '03' record
|
|
|
|
// only specifies the code address within the 20-bit
|
|
|
|
// segmented address space of the 8086/80186. This
|
|
|
|
// means 12 high order bits should be zeroes.
|
|
|
|
if (R.Type == IHexRecord::StartAddr80x86 &&
|
|
|
|
R.HexData.take_front(3) != "000")
|
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
"start address exceeds 20 bit for 80x86");
|
|
|
|
break;
|
|
|
|
case IHexRecord::ExtendedAddr:
|
|
|
|
// 16-31 bits of linear base address
|
|
|
|
if (R.HexData.size() != 4)
|
|
|
|
return createStringError(
|
|
|
|
errc::invalid_argument,
|
|
|
|
"extended address data should be 2 bytes in size");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Unknown record type
|
|
|
|
return createStringError(errc::invalid_argument, "unknown record type: %u",
|
|
|
|
static_cast<unsigned>(R.Type));
|
|
|
|
}
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks that IHEX line contains valid characters.
|
|
|
|
// This allows converting hexadecimal data to integers
|
|
|
|
// without extra verification.
|
|
|
|
static Error checkChars(StringRef Line) {
|
|
|
|
assert(!Line.empty());
|
|
|
|
if (Line[0] != ':')
|
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
"missing ':' in the beginning of line.");
|
|
|
|
|
|
|
|
for (size_t Pos = 1; Pos < Line.size(); ++Pos)
|
|
|
|
if (hexDigitValue(Line[Pos]) == -1U)
|
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
"invalid character at position %zu.", Pos + 1);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
Expected<IHexRecord> IHexRecord::parse(StringRef Line) {
|
|
|
|
assert(!Line.empty());
|
|
|
|
|
|
|
|
// ':' + Length + Address + Type + Checksum with empty data ':LLAAAATTCC'
|
|
|
|
if (Line.size() < 11)
|
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
"line is too short: %zu chars.", Line.size());
|
|
|
|
|
|
|
|
if (Error E = checkChars(Line))
|
2020-02-10 07:06:45 -08:00
|
|
|
return std::move(E);
|
2019-06-13 09:56:14 +00:00
|
|
|
|
|
|
|
IHexRecord Rec;
|
|
|
|
size_t DataLen = checkedGetHex<uint8_t>(Line.substr(1, 2));
|
|
|
|
if (Line.size() != getLength(DataLen))
|
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
"invalid line length %zu (should be %zu)",
|
|
|
|
Line.size(), getLength(DataLen));
|
|
|
|
|
|
|
|
Rec.Addr = checkedGetHex<uint16_t>(Line.substr(3, 4));
|
|
|
|
Rec.Type = checkedGetHex<uint8_t>(Line.substr(7, 2));
|
|
|
|
Rec.HexData = Line.substr(9, DataLen * 2);
|
|
|
|
|
|
|
|
if (getChecksum(Line.drop_front(1)) != 0)
|
|
|
|
return createStringError(errc::invalid_argument, "incorrect checksum.");
|
|
|
|
if (Error E = checkRecord(Rec))
|
2020-02-10 07:06:45 -08:00
|
|
|
return std::move(E);
|
2019-06-13 09:56:14 +00:00
|
|
|
return Rec;
|
|
|
|
}
|
|
|
|
|
2019-05-29 11:37:16 +00:00
|
|
|
static uint64_t sectionPhysicalAddr(const SectionBase *Sec) {
|
|
|
|
Segment *Seg = Sec->ParentSegment;
|
|
|
|
if (Seg && Seg->Type != ELF::PT_LOAD)
|
|
|
|
Seg = nullptr;
|
|
|
|
return Seg ? Seg->PAddr + Sec->OriginalOffset - Seg->OriginalOffset
|
|
|
|
: Sec->Addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IHexSectionWriterBase::writeSection(const SectionBase *Sec,
|
|
|
|
ArrayRef<uint8_t> Data) {
|
|
|
|
assert(Data.size() == Sec->Size);
|
|
|
|
const uint32_t ChunkSize = 16;
|
|
|
|
uint32_t Addr = sectionPhysicalAddr(Sec) & 0xFFFFFFFFU;
|
|
|
|
while (!Data.empty()) {
|
|
|
|
uint64_t DataSize = std::min<uint64_t>(Data.size(), ChunkSize);
|
|
|
|
if (Addr > SegmentAddr + BaseAddr + 0xFFFFU) {
|
|
|
|
if (Addr > 0xFFFFFU) {
|
|
|
|
// Write extended address record, zeroing segment address
|
|
|
|
// if needed.
|
|
|
|
if (SegmentAddr != 0)
|
|
|
|
SegmentAddr = writeSegmentAddr(0U);
|
|
|
|
BaseAddr = writeBaseAddr(Addr);
|
|
|
|
} else {
|
|
|
|
// We can still remain 16-bit
|
|
|
|
SegmentAddr = writeSegmentAddr(Addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uint64_t SegOffset = Addr - BaseAddr - SegmentAddr;
|
|
|
|
assert(SegOffset <= 0xFFFFU);
|
|
|
|
DataSize = std::min(DataSize, 0x10000U - SegOffset);
|
|
|
|
writeData(0, SegOffset, Data.take_front(DataSize));
|
|
|
|
Addr += DataSize;
|
|
|
|
Data = Data.drop_front(DataSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t IHexSectionWriterBase::writeSegmentAddr(uint64_t Addr) {
|
|
|
|
assert(Addr <= 0xFFFFFU);
|
|
|
|
uint8_t Data[] = {static_cast<uint8_t>((Addr & 0xF0000U) >> 12), 0};
|
|
|
|
writeData(2, 0, Data);
|
|
|
|
return Addr & 0xF0000U;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t IHexSectionWriterBase::writeBaseAddr(uint64_t Addr) {
|
|
|
|
assert(Addr <= 0xFFFFFFFFU);
|
|
|
|
uint64_t Base = Addr & 0xFFFF0000U;
|
|
|
|
uint8_t Data[] = {static_cast<uint8_t>(Base >> 24),
|
|
|
|
static_cast<uint8_t>((Base >> 16) & 0xFF)};
|
|
|
|
writeData(4, 0, Data);
|
|
|
|
return Base;
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
void IHexSectionWriterBase::writeData(uint8_t, uint16_t,
|
2019-05-29 11:37:16 +00:00
|
|
|
ArrayRef<uint8_t> Data) {
|
|
|
|
Offset += IHexRecord::getLineLength(Data.size());
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error IHexSectionWriterBase::visit(const Section &Sec) {
|
2019-05-29 11:37:16 +00:00
|
|
|
writeSection(&Sec, Sec.Contents);
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2019-05-29 11:37:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error IHexSectionWriterBase::visit(const OwnedDataSection &Sec) {
|
2019-05-29 11:37:16 +00:00
|
|
|
writeSection(&Sec, Sec.Data);
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2019-05-29 11:37:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error IHexSectionWriterBase::visit(const StringTableSection &Sec) {
|
2019-05-29 11:37:16 +00:00
|
|
|
// Check that sizer has already done its work
|
|
|
|
assert(Sec.Size == Sec.StrTabBuilder.getSize());
|
|
|
|
// We are free to pass an invalid pointer to writeSection as long
|
|
|
|
// as we don't actually write any data. The real writer class has
|
|
|
|
// to override this method .
|
2019-05-29 12:26:23 +00:00
|
|
|
writeSection(&Sec, {nullptr, static_cast<size_t>(Sec.Size)});
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2019-05-29 11:37:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error IHexSectionWriterBase::visit(const DynamicRelocationSection &Sec) {
|
2019-05-29 11:37:16 +00:00
|
|
|
writeSection(&Sec, Sec.Contents);
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2019-05-29 11:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IHexSectionWriter::writeData(uint8_t Type, uint16_t Addr,
|
|
|
|
ArrayRef<uint8_t> Data) {
|
|
|
|
IHexLineData HexData = IHexRecord::getLine(Type, Addr, Data);
|
|
|
|
memcpy(Out.getBufferStart() + Offset, HexData.data(), HexData.size());
|
|
|
|
Offset += HexData.size();
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error IHexSectionWriter::visit(const StringTableSection &Sec) {
|
2019-05-29 11:37:16 +00:00
|
|
|
assert(Sec.Size == Sec.StrTabBuilder.getSize());
|
|
|
|
std::vector<uint8_t> Data(Sec.Size);
|
|
|
|
Sec.StrTabBuilder.write(Data.data());
|
|
|
|
writeSection(&Sec, Data);
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2019-05-29 11:37:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error Section::accept(SectionVisitor &Visitor) const {
|
|
|
|
return Visitor.visit(*this);
|
|
|
|
}
|
2018-01-25 22:46:17 +00:00
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error Section::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
return 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
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error SectionWriter::visit(const OwnedDataSection &Sec) {
|
2019-05-23 09:18:57 +00:00
|
|
|
llvm::copy(Sec.Data, Out.getBufferStart() + Sec.Offset);
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
|
|
|
|
[NFC] Add { } to silence compiler warning [-Wmissing-braces].
/local/mnt/workspace/huihuiz/llvm-comm-git-2/llvm-project/llvm/lib/Object/MachOObjectFile.cpp:2731:7: warning: suggest braces around initialization of subobject [-Wmissing-braces]
"i386", "x86_64", "x86_64h", "armv4t", "arm", "armv5e",
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
1 warning generated.
/local/mnt/workspace/huihuiz/llvm-comm-git-2/llvm-project/llvm/lib/Target/AMDGPU/AMDGPURegisterBankInfo.cpp:355:46: warning: suggest braces around initialization of subobject [-Wmissing-braces]
return addMappingFromTable<1>(MI, MRI, { 0 }, Table);
^
{}
1 warning generated.
/local/mnt/workspace/huihuiz/llvm-comm-git-2/llvm-project/llvm/tools/llvm-objcopy/ELF/Object.cpp:400:57: warning: suggest braces around initialization of subobject [-Wmissing-braces]
static constexpr std::array<uint8_t, 4> ZlibGnuMagic = {'Z', 'L', 'I', 'B'};
^~~~~~~~~~~~~~~~~~
{ }
1 warning generated.
llvm-svn: 372811
2019-09-25 04:40:07 +00:00
|
|
|
static constexpr std::array<uint8_t, 4> ZlibGnuMagic = {{'Z', 'L', 'I', 'B'}};
|
2018-10-01 10:29:41 +00:00
|
|
|
|
|
|
|
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
|
2019-03-29 08:08:20 +00:00
|
|
|
? support::endian::read64be(Data.data() + ZlibGnuMagic.size())
|
2018-10-01 10:29:41 +00:00
|
|
|
: 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>
|
2020-09-19 18:53:44 +03:00
|
|
|
Error ELFSectionWriter<ELFT>::visit(const DecompressedSection &Sec) {
|
2018-10-01 10:29:41 +00:00
|
|
|
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;
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = zlib::uncompress(CompressedContent, DecompressedContent,
|
|
|
|
static_cast<size_t>(Sec.Size)))
|
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
"'" + Sec.Name + "': " + toString(std::move(Err)));
|
2018-10-01 10:29:41 +00:00
|
|
|
|
2020-10-24 17:35:55 +03:00
|
|
|
uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
|
2018-10-01 10:29:41 +00:00
|
|
|
std::copy(DecompressedContent.begin(), DecompressedContent.end(), Buf);
|
2020-09-19 18:53:44 +03:00
|
|
|
|
|
|
|
return Error::success();
|
2018-10-01 10:29:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error BinarySectionWriter::visit(const DecompressedSection &Sec) {
|
|
|
|
return createStringError(errc::operation_not_permitted,
|
|
|
|
"cannot write compressed section '" + Sec.Name +
|
|
|
|
"' ");
|
2018-10-01 10:29:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error DecompressedSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
return Visitor.visit(*this);
|
2018-10-01 10:29:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error DecompressedSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
return 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
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error OwnedDataSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
return Visitor.visit(*this);
|
2017-12-19 00:47:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error OwnedDataSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
return 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
|
|
|
}
|
|
|
|
|
2019-05-29 11:37:16 +00:00
|
|
|
void OwnedDataSection::appendHexData(StringRef HexData) {
|
|
|
|
assert((HexData.size() & 1) == 0);
|
|
|
|
while (!HexData.empty()) {
|
|
|
|
Data.push_back(checkedGetHex<uint8_t>(HexData.take_front(2)));
|
|
|
|
HexData = HexData.drop_front(2);
|
|
|
|
}
|
|
|
|
Size = Data.size();
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error BinarySectionWriter::visit(const CompressedSection &Sec) {
|
|
|
|
return createStringError(errc::operation_not_permitted,
|
|
|
|
"cannot write compressed section '" + Sec.Name +
|
|
|
|
"' ");
|
2018-09-07 08:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2020-09-19 18:53:44 +03:00
|
|
|
Error ELFSectionWriter<ELFT>::visit(const CompressedSection &Sec) {
|
2020-10-24 17:35:55 +03:00
|
|
|
uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
|
2018-09-07 08:10:22 +00:00
|
|
|
if (Sec.CompressionType == DebugCompressionType::None) {
|
|
|
|
std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf);
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2018-09-07 08:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
Expected<CompressedSection>
|
|
|
|
CompressedSection::create(const SectionBase &Sec,
|
|
|
|
DebugCompressionType CompressionType) {
|
|
|
|
Error Err = Error::success();
|
|
|
|
CompressedSection Section(Sec, CompressionType, Err);
|
|
|
|
|
|
|
|
if (Err)
|
|
|
|
return std::move(Err);
|
|
|
|
|
|
|
|
return Section;
|
|
|
|
}
|
|
|
|
Expected<CompressedSection>
|
|
|
|
CompressedSection::create(ArrayRef<uint8_t> CompressedData,
|
|
|
|
uint64_t DecompressedSize,
|
|
|
|
uint64_t DecompressedAlign) {
|
|
|
|
return CompressedSection(CompressedData, DecompressedSize, DecompressedAlign);
|
2018-09-07 08:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CompressedSection::CompressedSection(const SectionBase &Sec,
|
2020-09-19 18:53:44 +03:00
|
|
|
DebugCompressionType CompressionType,
|
|
|
|
Error &OutErr)
|
2018-09-07 08:10:22 +00:00
|
|
|
: SectionBase(Sec), CompressionType(CompressionType),
|
|
|
|
DecompressedSize(Sec.OriginalData.size()), DecompressedAlign(Sec.Align) {
|
2020-09-19 18:53:44 +03:00
|
|
|
ErrorAsOutParameter EAO(&OutErr);
|
|
|
|
|
|
|
|
if (Error Err = zlib::compress(
|
2018-09-07 08:10:22 +00:00
|
|
|
StringRef(reinterpret_cast<const char *>(OriginalData.data()),
|
|
|
|
OriginalData.size()),
|
2020-09-19 18:53:44 +03:00
|
|
|
CompressedData)) {
|
|
|
|
OutErr = createStringError(llvm::errc::invalid_argument,
|
|
|
|
"'" + Name + "': " + toString(std::move(Err)));
|
|
|
|
return;
|
|
|
|
}
|
2018-09-07 08:10:22 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error CompressedSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
return Visitor.visit(*this);
|
2018-09-07 08:10:22 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error CompressedSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
return 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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error SectionWriter::visit(const StringTableSection &Sec) {
|
2020-10-24 17:35:55 +03:00
|
|
|
Sec.StrTabBuilder.write(reinterpret_cast<uint8_t *>(Out.getBufferStart()) +
|
|
|
|
Sec.Offset);
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error StringTableSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
return Visitor.visit(*this);
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error StringTableSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
return 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
|
|
|
}
|
|
|
|
|
2018-07-16 19:48:52 +00:00
|
|
|
template <class ELFT>
|
2020-09-19 18:53:44 +03:00
|
|
|
Error ELFSectionWriter<ELFT>::visit(const SectionIndexSection &Sec) {
|
2020-10-24 17:35:55 +03:00
|
|
|
uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
|
2019-05-23 09:18:57 +00:00
|
|
|
llvm::copy(Sec.Indexes, reinterpret_cast<Elf_Word *>(Buf));
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2018-07-16 19:48:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error SectionIndexSection::initialize(SectionTableRef SecTable) {
|
2018-07-16 19:48:52 +00:00
|
|
|
Size = 0;
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<SymbolTableSection *> Sec =
|
|
|
|
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");
|
|
|
|
if (!Sec)
|
|
|
|
return Sec.takeError();
|
|
|
|
|
|
|
|
setSymTab(*Sec);
|
2018-07-16 19:48:52 +00:00
|
|
|
Symbols->setShndxTable(this);
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2018-07-16 19:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SectionIndexSection::finalize() { Link = Symbols->Index; }
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error SectionIndexSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
return Visitor.visit(*this);
|
2018-07-16 19:48:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error SectionIndexSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
return 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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
AMDGPU/MC: Add .amdgpu_lds directive
Summary:
The directive defines a symbol as an group/local memory (LDS) symbol.
LDS symbols behave similar to common symbols for the purposes of ELF,
using the processor-specific SHN_AMDGPU_LDS as section index.
It is the linker and/or runtime loader's job to "instantiate" LDS symbols
and resolve relocations that reference them.
It is not possible to initialize LDS memory (not even zero-initialize
as for .bss).
We want to be able to link together objects -- starting with relocatable
objects, but possible expanding to shared objects in the future -- that
access LDS memory in a flexible way.
LDS memory is in an address space that is entirely separate from the
address space that contains the program image (code and normal data),
so having program segments for it doesn't really make sense.
Furthermore, we want to be able to compile multiple kernels in a
compilation unit which have disjoint use of LDS memory. In that case,
we may want to place LDS symbols differently for different kernels
to save memory (LDS memory is very limited and physically private to
each kernel invocation), so we can't simply place LDS symbols in a
.lds section.
Hence this solution where LDS symbols always stay undefined.
Change-Id: I08cbc37a7c0c32f53f7b6123aa0afc91dbc1748f
Reviewers: arsenm, rampitec, t-tye, b-sumner, jsjodin
Subscribers: kzhuravl, jvesely, wdng, yaxunl, dstuttard, tpr, rupprecht, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D61493
llvm-svn: 364296
2019-06-25 11:51:35 +00:00
|
|
|
|
|
|
|
if (Machine == EM_AMDGPU) {
|
|
|
|
return Index == SHN_AMDGPU_LDS;
|
|
|
|
}
|
|
|
|
|
2017-09-13 03:04:50 +00:00
|
|
|
if (Machine == EM_HEXAGON) {
|
|
|
|
switch (Index) {
|
|
|
|
case SHN_HEXAGON_SCOMMON:
|
2020-06-24 09:36:03 -05:00
|
|
|
case SHN_HEXAGON_SCOMMON_1:
|
2017-09-13 03:04:50 +00:00
|
|
|
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;
|
|
|
|
}
|
AMDGPU/MC: Add .amdgpu_lds directive
Summary:
The directive defines a symbol as an group/local memory (LDS) symbol.
LDS symbols behave similar to common symbols for the purposes of ELF,
using the processor-specific SHN_AMDGPU_LDS as section index.
It is the linker and/or runtime loader's job to "instantiate" LDS symbols
and resolve relocations that reference them.
It is not possible to initialize LDS memory (not even zero-initialize
as for .bss).
We want to be able to link together objects -- starting with relocatable
objects, but possible expanding to shared objects in the future -- that
access LDS memory in a flexible way.
LDS memory is in an address space that is entirely separate from the
address space that contains the program image (code and normal data),
so having program segments for it doesn't really make sense.
Furthermore, we want to be able to compile multiple kernels in a
compilation unit which have disjoint use of LDS memory. In that case,
we may want to place LDS symbols differently for different kernels
to save memory (LDS memory is very limited and physically private to
each kernel invocation), so we can't simply place LDS symbols in a
.lds section.
Hence this solution where LDS symbols always stay undefined.
Change-Id: I08cbc37a7c0c32f53f7b6123aa0afc91dbc1748f
Reviewers: arsenm, rampitec, t-tye, b-sumner, jsjodin
Subscribers: kzhuravl, jvesely, wdng, yaxunl, dstuttard, tpr, rupprecht, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D61493
llvm-svn: 364296
2019-06-25 11:51:35 +00:00
|
|
|
|
|
|
|
if (ShndxType == SYMBOL_SIMPLE_INDEX) {
|
|
|
|
// This means that we don't have a defined section but we do need to
|
|
|
|
// output a legitimate section index.
|
2017-09-07 23:02:50 +00:00
|
|
|
return SHN_UNDEF;
|
|
|
|
}
|
AMDGPU/MC: Add .amdgpu_lds directive
Summary:
The directive defines a symbol as an group/local memory (LDS) symbol.
LDS symbols behave similar to common symbols for the purposes of ELF,
using the processor-specific SHN_AMDGPU_LDS as section index.
It is the linker and/or runtime loader's job to "instantiate" LDS symbols
and resolve relocations that reference them.
It is not possible to initialize LDS memory (not even zero-initialize
as for .bss).
We want to be able to link together objects -- starting with relocatable
objects, but possible expanding to shared objects in the future -- that
access LDS memory in a flexible way.
LDS memory is in an address space that is entirely separate from the
address space that contains the program image (code and normal data),
so having program segments for it doesn't really make sense.
Furthermore, we want to be able to compile multiple kernels in a
compilation unit which have disjoint use of LDS memory. In that case,
we may want to place LDS symbols differently for different kernels
to save memory (LDS memory is very limited and physically private to
each kernel invocation), so we can't simply place LDS symbols in a
.lds section.
Hence this solution where LDS symbols always stay undefined.
Change-Id: I08cbc37a7c0c32f53f7b6123aa0afc91dbc1748f
Reviewers: arsenm, rampitec, t-tye, b-sumner, jsjodin
Subscribers: kzhuravl, jvesely, wdng, yaxunl, dstuttard, tpr, rupprecht, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D61493
llvm-svn: 364296
2019-06-25 11:51:35 +00:00
|
|
|
|
|
|
|
assert(ShndxType == SYMBOL_ABS || ShndxType == SYMBOL_COMMON ||
|
2019-06-26 19:16:35 +00:00
|
|
|
(ShndxType >= SYMBOL_LOPROC && ShndxType <= SYMBOL_HIPROC) ||
|
|
|
|
(ShndxType >= SYMBOL_LOOS && ShndxType <= SYMBOL_HIOS));
|
AMDGPU/MC: Add .amdgpu_lds directive
Summary:
The directive defines a symbol as an group/local memory (LDS) symbol.
LDS symbols behave similar to common symbols for the purposes of ELF,
using the processor-specific SHN_AMDGPU_LDS as section index.
It is the linker and/or runtime loader's job to "instantiate" LDS symbols
and resolve relocations that reference them.
It is not possible to initialize LDS memory (not even zero-initialize
as for .bss).
We want to be able to link together objects -- starting with relocatable
objects, but possible expanding to shared objects in the future -- that
access LDS memory in a flexible way.
LDS memory is in an address space that is entirely separate from the
address space that contains the program image (code and normal data),
so having program segments for it doesn't really make sense.
Furthermore, we want to be able to compile multiple kernels in a
compilation unit which have disjoint use of LDS memory. In that case,
we may want to place LDS symbols differently for different kernels
to save memory (LDS memory is very limited and physically private to
each kernel invocation), so we can't simply place LDS symbols in a
.lds section.
Hence this solution where LDS symbols always stay undefined.
Change-Id: I08cbc37a7c0c32f53f7b6123aa0afc91dbc1748f
Reviewers: arsenm, rampitec, t-tye, b-sumner, jsjodin
Subscribers: kzhuravl, jvesely, wdng, yaxunl, dstuttard, tpr, rupprecht, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D61493
llvm-svn: 364296
2019-06-25 11:51:35 +00:00
|
|
|
return static_cast<uint16_t>(ShndxType);
|
2017-09-07 23:02:50 +00:00
|
|
|
}
|
|
|
|
|
[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,
|
2019-05-08 07:31:05 +00:00
|
|
|
uint64_t SymbolSize) {
|
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;
|
2019-05-08 07:31:05 +00:00
|
|
|
Sym.Size = SymbolSize;
|
2017-08-29 02:12:03 +00:00
|
|
|
Sym.Index = Symbols.size();
|
2019-08-15 15:54:37 +00:00
|
|
|
Symbols.emplace_back(std::make_unique<Symbol>(Sym));
|
2017-08-29 02:12:03 +00:00
|
|
|
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(
|
2020-10-06 14:55:45 +03:00
|
|
|
bool AllowBrokenLinks, function_ref<bool(const SectionBase *)> ToRemove) {
|
[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(SectionIndexTable))
|
2018-07-16 19:48:52 +00:00
|
|
|
SectionIndexTable = nullptr;
|
2019-04-18 09:13:30 +00:00
|
|
|
if (ToRemove(SymbolNames)) {
|
|
|
|
if (!AllowBrokenLinks)
|
|
|
|
return createStringError(
|
|
|
|
llvm::errc::invalid_argument,
|
2019-05-22 13:23:26 +00:00
|
|
|
"string table '%s' cannot be removed because it is "
|
|
|
|
"referenced by the symbol table '%s'",
|
2019-04-18 09:13:30 +00:00
|
|
|
SymbolNames->Name.data(), this->Name.data());
|
|
|
|
SymbolNames = nullptr;
|
|
|
|
}
|
2019-02-01 15:20:36 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error SymbolTableSection::initialize(SectionTableRef SecTable) {
|
2017-09-25 20:37:28 +00:00
|
|
|
Size = 0;
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<StringTableSection *> Sec =
|
|
|
|
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");
|
|
|
|
if (!Sec)
|
|
|
|
return Sec.takeError();
|
|
|
|
|
|
|
|
setStrTab(*Sec);
|
|
|
|
return Error::success();
|
2017-09-25 20:37:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 02:12:03 +00:00
|
|
|
void SymbolTableSection::finalize() {
|
|
|
|
uint32_t MaxLocalIndex = 0;
|
2019-05-23 09:18:57 +00:00
|
|
|
for (std::unique_ptr<Symbol> &Sym : Symbols) {
|
2019-04-18 09:13:30 +00:00
|
|
|
Sym->NameIndex =
|
|
|
|
SymbolNames == nullptr ? 0 : SymbolNames->findIndex(Sym->Name);
|
2017-08-29 02:12:03 +00:00
|
|
|
if (Sym->Binding == STB_LOCAL)
|
|
|
|
MaxLocalIndex = std::max(MaxLocalIndex, Sym->Index);
|
|
|
|
}
|
|
|
|
// Now we need to set the Link and Info fields.
|
2019-04-18 09:13:30 +00:00
|
|
|
Link = SymbolNames == nullptr ? 0 : SymbolNames->Index;
|
2017-08-29 02:12:03 +00:00
|
|
|
Info = MaxLocalIndex + 1;
|
|
|
|
}
|
|
|
|
|
2018-07-16 19:48:52 +00:00
|
|
|
void SymbolTableSection::prepareForLayout() {
|
2019-04-12 11:59:30 +00:00
|
|
|
// Reserve proper amount of space in section index table, so we can
|
|
|
|
// layout sections correctly. We will fill the table with correct
|
|
|
|
// indexes later in fillShdnxTable.
|
2020-07-02 14:42:01 -07:00
|
|
|
if (SectionIndexTable)
|
2019-04-12 11:59:30 +00:00
|
|
|
SectionIndexTable->reserve(Symbols.size());
|
2019-04-18 09:13:30 +00:00
|
|
|
|
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.
|
2019-04-18 09:13:30 +00:00
|
|
|
// If the symbol names section has been removed, don't try to add strings to
|
|
|
|
// the table.
|
|
|
|
if (SymbolNames != nullptr)
|
2019-05-23 09:18:57 +00:00
|
|
|
for (std::unique_ptr<Symbol> &Sym : Symbols)
|
2019-04-18 09:13:30 +00:00
|
|
|
SymbolNames->addString(Sym->Name);
|
2017-08-29 02:12:03 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 11:59:30 +00:00
|
|
|
void SymbolTableSection::fillShndxTable() {
|
|
|
|
if (SectionIndexTable == nullptr)
|
|
|
|
return;
|
|
|
|
// Fill section index table with real section indexes. This function must
|
|
|
|
// be called after assignOffsets.
|
2019-05-23 09:18:57 +00:00
|
|
|
for (const std::unique_ptr<Symbol> &Sym : Symbols) {
|
2019-04-12 11:59:30 +00:00
|
|
|
if (Sym->DefinedIn != nullptr && Sym->DefinedIn->Index >= SHN_LORESERVE)
|
|
|
|
SectionIndexTable->addIndex(Sym->DefinedIn->Index);
|
|
|
|
else
|
|
|
|
SectionIndexTable->addIndex(SHN_UNDEF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<const Symbol *>
|
|
|
|
SymbolTableSection::getSymbolByIndex(uint32_t Index) const {
|
2017-08-29 02:12:03 +00:00
|
|
|
if (Symbols.size() <= Index)
|
2020-09-19 18:53:44 +03:00
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
"invalid symbol index: " + Twine(Index));
|
2017-08-29 02:12:03 +00:00
|
|
|
return Symbols[Index].get();
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<Symbol *> SymbolTableSection::getSymbolByIndex(uint32_t Index) {
|
|
|
|
Expected<const Symbol *> Sym =
|
|
|
|
static_cast<const SymbolTableSection *>(this)->getSymbolByIndex(Index);
|
|
|
|
if (!Sym)
|
|
|
|
return Sym.takeError();
|
|
|
|
|
|
|
|
return const_cast<Symbol *>(*Sym);
|
2018-05-25 11:01:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 02:12:03 +00:00
|
|
|
template <class ELFT>
|
2020-09-19 18:53:44 +03:00
|
|
|
Error ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) {
|
2019-05-23 09:18:57 +00:00
|
|
|
Elf_Sym *Sym = reinterpret_cast<Elf_Sym *>(Out.getBufferStart() + Sec.Offset);
|
2017-08-29 02:12:03 +00:00
|
|
|
// Loop though symbols setting each entry of the symbol table.
|
2019-05-23 09:18:57 +00:00
|
|
|
for (const std::unique_ptr<Symbol> &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;
|
|
|
|
}
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2017-08-29 02:12:03 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error SymbolTableSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
return Visitor.visit(*this);
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error SymbolTableSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
return 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
|
|
|
}
|
|
|
|
|
2019-02-27 11:18:27 +00:00
|
|
|
Error RelocationSection::removeSectionReferences(
|
2020-10-06 14:55:45 +03:00
|
|
|
bool AllowBrokenLinks, function_ref<bool(const SectionBase *)> ToRemove) {
|
2019-04-18 09:13:30 +00:00
|
|
|
if (ToRemove(Symbols)) {
|
|
|
|
if (!AllowBrokenLinks)
|
|
|
|
return createStringError(
|
|
|
|
llvm::errc::invalid_argument,
|
2019-05-22 13:23:26 +00:00
|
|
|
"symbol table '%s' cannot be removed because it is "
|
|
|
|
"referenced by the relocation section '%s'",
|
2019-04-18 09:13:30 +00:00
|
|
|
Symbols->Name.data(), this->Name.data());
|
|
|
|
Symbols = nullptr;
|
|
|
|
}
|
2019-02-27 11:18:27 +00:00
|
|
|
|
|
|
|
for (const Relocation &R : Relocations) {
|
2019-10-28 12:52:28 +03:00
|
|
|
if (!R.RelocSymbol || !R.RelocSymbol->DefinedIn ||
|
|
|
|
!ToRemove(R.RelocSymbol->DefinedIn))
|
2019-02-27 11:18:27 +00:00
|
|
|
continue;
|
2019-02-28 08:21:50 +00:00
|
|
|
return createStringError(llvm::errc::invalid_argument,
|
2019-05-22 13:23:26 +00:00
|
|
|
"section '%s' cannot be removed: (%s+0x%" PRIx64
|
2019-02-28 08:21:50 +00:00
|
|
|
") 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>
|
2020-09-19 18:53:44 +03:00
|
|
|
Error RelocSectionWithSymtabBase<SymTabType>::initialize(
|
2017-10-10 18:47:09 +00:00
|
|
|
SectionTableRef SecTable) {
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Link != SHN_UNDEF) {
|
|
|
|
Expected<SymTabType *> Sec = SecTable.getSectionOfType<SymTabType>(
|
2018-09-04 22:28:49 +00:00
|
|
|
Link,
|
|
|
|
"Link field value " + Twine(Link) + " in section " + Name +
|
|
|
|
" is invalid",
|
|
|
|
"Link field value " + Twine(Link) + " in section " + Name +
|
2020-09-19 18:53:44 +03:00
|
|
|
" is not a symbol table");
|
|
|
|
if (!Sec)
|
|
|
|
return Sec.takeError();
|
2017-09-25 20:37:28 +00:00
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
setSymTab(*Sec);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Info != SHN_UNDEF) {
|
|
|
|
Expected<SectionBase *> Sec =
|
|
|
|
SecTable.getSection(Info, "Info field value " + Twine(Info) +
|
|
|
|
" in section " + Name + " is invalid");
|
|
|
|
if (!Sec)
|
|
|
|
return Sec.takeError();
|
|
|
|
|
|
|
|
setSection(*Sec);
|
|
|
|
} else
|
2017-09-26 22:44:01 +00:00
|
|
|
setSection(nullptr);
|
2020-09-19 18:53:44 +03:00
|
|
|
|
|
|
|
return Error::success();
|
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>
|
2020-10-06 14:55:45 +03:00
|
|
|
static void setAddend(Elf_Rel_Impl<ELFT, false> &, uint64_t) {}
|
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);
|
2019-10-28 12:52:28 +03:00
|
|
|
Buf->setSymbolAndType(Reloc.RelocSymbol ? Reloc.RelocSymbol->Index : 0,
|
|
|
|
Reloc.Type, false);
|
2017-09-06 23:41:02 +00:00
|
|
|
++Buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2020-09-19 18:53:44 +03:00
|
|
|
Error ELFSectionWriter<ELFT>::visit(const RelocationSection &Sec) {
|
2020-10-24 17:35:55 +03:00
|
|
|
uint8_t *Buf = reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
|
2018-01-25 22:46:17 +00:00
|
|
|
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));
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error RelocationSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
return Visitor.visit(*this);
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error RelocationSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
return 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
|
|
|
}
|
|
|
|
|
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)
|
2019-10-28 12:52:28 +03:00
|
|
|
if (Reloc.RelocSymbol && ToRemove(*Reloc.RelocSymbol))
|
2019-02-01 15:20:36 +00:00
|
|
|
return createStringError(
|
|
|
|
llvm::errc::invalid_argument,
|
2019-05-22 13:23:26 +00:00
|
|
|
"not stripping symbol '%s' because it is named in a relocation",
|
2019-02-01 15:20:36 +00:00
|
|
|
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)
|
2019-10-28 12:52:28 +03:00
|
|
|
if (Reloc.RelocSymbol)
|
|
|
|
Reloc.RelocSymbol->Referenced = true;
|
2018-05-25 11:01:25 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error SectionWriter::visit(const DynamicRelocationSection &Sec) {
|
2019-05-23 09:18:57 +00:00
|
|
|
llvm::copy(Sec.Contents, Out.getBufferStart() + Sec.Offset);
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2017-09-06 23:41:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error DynamicRelocationSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
return Visitor.visit(*this);
|
2017-09-26 18:02:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error DynamicRelocationSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
return Visitor.visit(*this);
|
2019-06-13 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Error DynamicRelocationSection::removeSectionReferences(
|
|
|
|
bool AllowBrokenLinks, function_ref<bool(const SectionBase *)> ToRemove) {
|
|
|
|
if (ToRemove(Symbols)) {
|
|
|
|
if (!AllowBrokenLinks)
|
|
|
|
return createStringError(
|
|
|
|
llvm::errc::invalid_argument,
|
2019-05-22 13:23:26 +00:00
|
|
|
"symbol table '%s' cannot be removed because it is "
|
|
|
|
"referenced by the relocation section '%s'",
|
2019-06-13 09:56:14 +00:00
|
|
|
Symbols->Name.data(), this->Name.data());
|
|
|
|
Symbols = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SecToApplyRel contains a section referenced by sh_info field. It keeps
|
|
|
|
// a section to which the relocation section applies. When we remove any
|
|
|
|
// sections we also remove their relocation sections. Since we do that much
|
|
|
|
// earlier, this assert should never be triggered.
|
|
|
|
assert(!SecToApplyRel || !ToRemove(SecToApplyRel));
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
Error Section::removeSectionReferences(
|
|
|
|
bool AllowBrokenDependency,
|
|
|
|
function_ref<bool(const SectionBase *)> ToRemove) {
|
|
|
|
if (ToRemove(LinkSection)) {
|
2019-04-18 09:13:30 +00:00
|
|
|
if (!AllowBrokenDependency)
|
|
|
|
return createStringError(llvm::errc::invalid_argument,
|
2019-05-22 13:23:26 +00:00
|
|
|
"section '%s' cannot be removed because it is "
|
|
|
|
"referenced by the section '%s'",
|
2019-04-18 09:13:30 +00:00
|
|
|
LinkSection->Name.data(), this->Name.data());
|
|
|
|
LinkSection = nullptr;
|
|
|
|
}
|
2019-02-01 15:20:36 +00:00
|
|
|
return Error::success();
|
2017-10-10 18:47:09 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 19:53:44 +00:00
|
|
|
void GroupSection::finalize() {
|
2020-06-29 10:25:26 +01:00
|
|
|
this->Info = Sym ? Sym->Index : 0;
|
|
|
|
this->Link = SymTab ? SymTab->Index : 0;
|
2018-03-21 19:53:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 19:28:37 +07:00
|
|
|
Error GroupSection::removeSectionReferences(
|
|
|
|
bool AllowBrokenLinks, function_ref<bool(const SectionBase *)> ToRemove) {
|
2020-06-29 10:25:26 +01:00
|
|
|
if (ToRemove(SymTab)) {
|
|
|
|
if (!AllowBrokenLinks)
|
|
|
|
return createStringError(
|
|
|
|
llvm::errc::invalid_argument,
|
|
|
|
"section '.symtab' cannot be removed because it is "
|
|
|
|
"referenced by the group section '%s'",
|
|
|
|
this->Name.data());
|
|
|
|
SymTab = nullptr;
|
|
|
|
Sym = nullptr;
|
|
|
|
}
|
2020-05-29 19:28:37 +07:00
|
|
|
llvm::erase_if(GroupMembers, ToRemove);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
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,
|
2019-05-22 13:23:26 +00:00
|
|
|
"symbol '%s' cannot be removed because it is "
|
|
|
|
"referenced by the section '%s[%d]'",
|
2019-02-01 15:20:36 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-24 14:41:45 +00:00
|
|
|
void GroupSection::replaceSectionReferences(
|
|
|
|
const DenseMap<SectionBase *, SectionBase *> &FromTo) {
|
|
|
|
for (SectionBase *&Sec : GroupMembers)
|
|
|
|
if (SectionBase *To = FromTo.lookup(Sec))
|
|
|
|
Sec = To;
|
|
|
|
}
|
|
|
|
|
2020-05-29 19:27:28 +07:00
|
|
|
void GroupSection::onRemove() {
|
|
|
|
// As the header section of the group is removed, drop the Group flag in its
|
|
|
|
// former members.
|
|
|
|
for (SectionBase *Sec : GroupMembers)
|
|
|
|
Sec->Flags &= ~SHF_GROUP;
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error Section::initialize(SectionTableRef SecTable) {
|
2019-05-23 09:18:57 +00:00
|
|
|
if (Link == ELF::SHN_UNDEF)
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
|
|
|
|
|
|
|
Expected<SectionBase *> Sec =
|
2019-05-23 09:18:57 +00:00
|
|
|
SecTable.getSection(Link, "Link field value " + Twine(Link) +
|
|
|
|
" in section " + Name + " is invalid");
|
2020-09-19 18:53:44 +03:00
|
|
|
if (!Sec)
|
|
|
|
return Sec.takeError();
|
|
|
|
|
|
|
|
LinkSection = *Sec;
|
|
|
|
|
2019-05-23 09:18:57 +00:00
|
|
|
if (LinkSection->Type == ELF::SHT_SYMTAB)
|
|
|
|
LinkSection = nullptr;
|
2020-09-19 18:53:44 +03:00
|
|
|
|
|
|
|
return Error::success();
|
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
|
|
|
|
2019-05-14 10:59:04 +00:00
|
|
|
void GnuDebugLinkSection::init(StringRef File) {
|
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;
|
2019-11-01 13:49:42 -07:00
|
|
|
Type = OriginalType = ELF::SHT_PROGBITS;
|
2018-01-25 22:15:14 +00:00
|
|
|
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();
|
2019-05-14 10:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GnuDebugLinkSection::GnuDebugLinkSection(StringRef File,
|
|
|
|
uint32_t PrecomputedCRC)
|
|
|
|
: FileName(File), CRC32(PrecomputedCRC) {
|
|
|
|
init(File);
|
2018-01-25 22:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2020-09-19 18:53:44 +03:00
|
|
|
Error ELFSectionWriter<ELFT>::visit(const GnuDebugLinkSection &Sec) {
|
2020-10-24 17:35:55 +03:00
|
|
|
unsigned char *Buf =
|
|
|
|
reinterpret_cast<uint8_t *>(Out.getBufferStart()) + Sec.Offset;
|
2018-01-25 22:46:17 +00:00
|
|
|
Elf_Word *CRC =
|
|
|
|
reinterpret_cast<Elf_Word *>(Buf + Sec.Size - sizeof(Elf_Word));
|
|
|
|
*CRC = Sec.CRC32;
|
2019-03-29 08:08:20 +00:00
|
|
|
llvm::copy(Sec.FileName, Buf);
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error GnuDebugLinkSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
return Visitor.visit(*this);
|
2018-01-25 22:15:14 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error GnuDebugLinkSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
return 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
|
|
|
}
|
|
|
|
|
2018-03-21 19:53:44 +00:00
|
|
|
template <class ELFT>
|
2020-09-19 18:53:44 +03:00
|
|
|
Error ELFSectionWriter<ELFT>::visit(const GroupSection &Sec) {
|
2018-03-21 19:53:44 +00:00
|
|
|
ELF::Elf32_Word *Buf =
|
|
|
|
reinterpret_cast<ELF::Elf32_Word *>(Out.getBufferStart() + Sec.Offset);
|
|
|
|
*Buf++ = Sec.FlagWord;
|
2019-05-23 09:18:57 +00:00
|
|
|
for (SectionBase *S : Sec.GroupMembers)
|
2018-03-21 19:53:44 +00:00
|
|
|
support::endian::write32<ELFT::TargetEndianness>(Buf++, S->Index);
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2018-03-21 19:53:44 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error GroupSection::accept(SectionVisitor &Visitor) const {
|
|
|
|
return Visitor.visit(*this);
|
2018-03-21 19:53:44 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error GroupSection::accept(MutableSectionVisitor &Visitor) {
|
|
|
|
return 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
|
|
|
}
|
|
|
|
|
2017-08-01 00:33:58 +00:00
|
|
|
// Returns true IFF a section is wholly inside the range of a segment
|
2019-09-04 08:43:27 +00:00
|
|
|
static bool sectionWithinSegment(const SectionBase &Sec, const Segment &Seg) {
|
2017-08-01 00:33:58 +00:00
|
|
|
// 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.
|
2019-09-04 08:43:27 +00:00
|
|
|
uint64_t SecSize = Sec.Size ? Sec.Size : 1;
|
2019-05-24 00:21:46 +00:00
|
|
|
|
2021-05-12 12:39:30 +04:00
|
|
|
// Ignore just added sections.
|
|
|
|
if (Sec.OriginalOffset == std::numeric_limits<uint64_t>::max())
|
|
|
|
return false;
|
|
|
|
|
2019-09-04 08:43:27 +00:00
|
|
|
if (Sec.Type == SHT_NOBITS) {
|
|
|
|
if (!(Sec.Flags & SHF_ALLOC))
|
2019-05-24 00:21:46 +00:00
|
|
|
return false;
|
|
|
|
|
2019-09-04 08:43:27 +00:00
|
|
|
bool SectionIsTLS = Sec.Flags & SHF_TLS;
|
|
|
|
bool SegmentIsTLS = Seg.Type == PT_TLS;
|
2019-05-24 00:21:46 +00:00
|
|
|
if (SectionIsTLS != SegmentIsTLS)
|
|
|
|
return false;
|
|
|
|
|
2019-09-04 08:43:27 +00:00
|
|
|
return Seg.VAddr <= Sec.Addr &&
|
|
|
|
Seg.VAddr + Seg.MemSize >= Sec.Addr + SecSize;
|
2019-05-24 00:21:46 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 08:43:27 +00:00
|
|
|
return Seg.Offset <= Sec.OriginalOffset &&
|
|
|
|
Seg.Offset + Seg.FileSize >= Sec.OriginalOffset + SecSize;
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:56:14 +00:00
|
|
|
void BasicELFBuilder::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;
|
2019-09-14 01:36:31 +00:00
|
|
|
Obj->Machine = EM_NONE;
|
[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->Version = 1;
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:56:14 +00:00
|
|
|
void BasicELFBuilder::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
|
|
|
|
2019-06-13 09:56:14 +00:00
|
|
|
StringTableSection *BasicELFBuilder::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;
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:56:14 +00:00
|
|
|
SymbolTableSection *BasicELFBuilder::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;
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error BasicELFBuilder::initSections() {
|
2019-09-04 08:43:27 +00:00
|
|
|
for (SectionBase &Sec : Obj->sections())
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = Sec.initialize(Obj->sections()))
|
|
|
|
return Err;
|
|
|
|
|
|
|
|
return Error::success();
|
2019-06-13 09:56: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 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();
|
2021-02-23 15:32:45 +00:00
|
|
|
std::replace_if(
|
|
|
|
std::begin(SanitizedFilename), std::end(SanitizedFilename),
|
|
|
|
[](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,
|
2019-08-30 10:17:16 +00:00
|
|
|
/*Value=*/0, NewSymbolVisibility, 0, 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
|
|
|
SymTab->addSymbol(Prefix + "_end", STB_GLOBAL, STT_NOTYPE, &DataSection,
|
2019-08-30 10:17:16 +00:00
|
|
|
/*Value=*/DataSection.Size, NewSymbolVisibility, 0, 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
|
|
|
SymTab->addSymbol(Prefix + "_size", STB_GLOBAL, STT_NOTYPE, nullptr,
|
2019-08-30 10:17:16 +00:00
|
|
|
/*Value=*/DataSection.Size, NewSymbolVisibility, SHN_ABS,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<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();
|
2019-05-23 09:18:57 +00:00
|
|
|
|
|
|
|
SymbolTableSection *SymTab = addSymTab(addStrTab());
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = initSections())
|
|
|
|
return std::move(Err);
|
[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
|
|
|
addData(SymTab);
|
|
|
|
|
|
|
|
return std::move(Obj);
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:56:14 +00:00
|
|
|
// Adds sections from IHEX data file. Data should have been
|
|
|
|
// fully validated by this time.
|
|
|
|
void IHexELFBuilder::addDataSections() {
|
|
|
|
OwnedDataSection *Section = nullptr;
|
|
|
|
uint64_t SegmentAddr = 0, BaseAddr = 0;
|
|
|
|
uint32_t SecNo = 1;
|
|
|
|
|
|
|
|
for (const IHexRecord &R : Records) {
|
|
|
|
uint64_t RecAddr;
|
|
|
|
switch (R.Type) {
|
|
|
|
case IHexRecord::Data:
|
|
|
|
// Ignore empty data records
|
|
|
|
if (R.HexData.empty())
|
|
|
|
continue;
|
|
|
|
RecAddr = R.Addr + SegmentAddr + BaseAddr;
|
|
|
|
if (!Section || Section->Addr + Section->Size != RecAddr)
|
|
|
|
// OriginalOffset field is only used to sort section properly, so
|
|
|
|
// instead of keeping track of real offset in IHEX file, we use
|
|
|
|
// section number.
|
|
|
|
Section = &Obj->addSection<OwnedDataSection>(
|
|
|
|
".sec" + std::to_string(SecNo++), RecAddr,
|
|
|
|
ELF::SHF_ALLOC | ELF::SHF_WRITE, SecNo);
|
|
|
|
Section->appendHexData(R.HexData);
|
|
|
|
break;
|
|
|
|
case IHexRecord::EndOfFile:
|
|
|
|
break;
|
|
|
|
case IHexRecord::SegmentAddr:
|
|
|
|
// 20-bit segment address.
|
|
|
|
SegmentAddr = checkedGetHex<uint16_t>(R.HexData) << 4;
|
|
|
|
break;
|
|
|
|
case IHexRecord::StartAddr80x86:
|
|
|
|
case IHexRecord::StartAddr:
|
|
|
|
Obj->Entry = checkedGetHex<uint32_t>(R.HexData);
|
|
|
|
assert(Obj->Entry <= 0xFFFFFU);
|
|
|
|
break;
|
|
|
|
case IHexRecord::ExtendedAddr:
|
|
|
|
// 16-31 bits of linear base address
|
|
|
|
BaseAddr = checkedGetHex<uint16_t>(R.HexData) << 16;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown record type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<std::unique_ptr<Object>> IHexELFBuilder::build() {
|
2019-06-13 09:56:14 +00:00
|
|
|
initFileHeader();
|
|
|
|
initHeaderSegment();
|
|
|
|
StringTableSection *StrTab = addStrTab();
|
|
|
|
addSymTab(StrTab);
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = initSections())
|
|
|
|
return std::move(Err);
|
2019-06-13 09:56:14 +00:00
|
|
|
addDataSections();
|
|
|
|
|
|
|
|
return std::move(Obj);
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:53:44 +00:00
|
|
|
template <class ELFT> void ELFBuilder<ELFT>::setParentSegment(Segment &Child) {
|
2019-05-23 09:18:57 +00:00
|
|
|
for (Segment &Parent : Obj.segments()) {
|
2018-02-14 23:31:33 +00:00
|
|
|
// Every segment will overlap with itself but we don't want a segment to
|
2020-07-02 14:42:01 -07:00
|
|
|
// be its own parent so we avoid that situation.
|
2018-02-14 23:31:33 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
template <class ELFT> Error ELFBuilder<ELFT>::findEhdrOffset() {
|
2019-06-07 17:57:48 +00:00
|
|
|
if (!ExtractPartition)
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2019-06-07 17:57:48 +00:00
|
|
|
|
2019-09-04 08:43:27 +00:00
|
|
|
for (const SectionBase &Sec : Obj.sections()) {
|
|
|
|
if (Sec.Type == SHT_LLVM_PART_EHDR && Sec.Name == *ExtractPartition) {
|
|
|
|
EhdrOffset = Sec.Offset;
|
2020-09-19 18:53:44 +03:00
|
|
|
return Error::success();
|
2019-06-07 17:57:48 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-19 18:53:44 +03:00
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
"could not find partition named '" +
|
|
|
|
*ExtractPartition + "'");
|
2019-06-07 17:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2020-09-19 18:53:44 +03:00
|
|
|
Error ELFBuilder<ELFT>::readProgramHeaders(const ELFFile<ELFT> &HeadersFile) {
|
2017-08-01 00:33:58 +00:00
|
|
|
uint32_t Index = 0;
|
2020-09-19 18:53:44 +03:00
|
|
|
|
|
|
|
Expected<typename ELFFile<ELFT>::Elf_Phdr_Range> Headers =
|
|
|
|
HeadersFile.program_headers();
|
|
|
|
if (!Headers)
|
|
|
|
return Headers.takeError();
|
|
|
|
|
|
|
|
for (const typename ELFFile<ELFT>::Elf_Phdr &Phdr : *Headers) {
|
2019-06-07 17:57:48 +00:00
|
|
|
if (Phdr.p_offset + Phdr.p_filesz > HeadersFile.getBufSize())
|
2020-09-19 18:53:44 +03:00
|
|
|
return createStringError(
|
|
|
|
errc::invalid_argument,
|
|
|
|
"program header with offset 0x" + Twine::utohexstr(Phdr.p_offset) +
|
|
|
|
" and file size 0x" + Twine::utohexstr(Phdr.p_filesz) +
|
|
|
|
" goes past the end of the file");
|
2019-06-07 08:34:18 +00:00
|
|
|
|
2019-06-07 17:57:48 +00:00
|
|
|
ArrayRef<uint8_t> Data{HeadersFile.base() + Phdr.p_offset,
|
2019-03-25 16:36:26 +00:00
|
|
|
(size_t)Phdr.p_filesz};
|
|
|
|
Segment &Seg = Obj.addSegment(Data);
|
2017-08-01 00:33:58 +00:00
|
|
|
Seg.Type = Phdr.p_type;
|
|
|
|
Seg.Flags = Phdr.p_flags;
|
2019-06-07 17:57:48 +00:00
|
|
|
Seg.OriginalOffset = Phdr.p_offset + EhdrOffset;
|
|
|
|
Seg.Offset = Phdr.p_offset + EhdrOffset;
|
2017-08-01 00:33:58 +00:00
|
|
|
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++;
|
2019-09-04 08:43:27 +00:00
|
|
|
for (SectionBase &Sec : Obj.sections())
|
|
|
|
if (sectionWithinSegment(Sec, Seg)) {
|
|
|
|
Seg.addSection(&Sec);
|
|
|
|
if (!Sec.ParentSegment || Sec.ParentSegment->Offset > Seg.Offset)
|
|
|
|
Sec.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++;
|
2019-06-07 17:57:48 +00:00
|
|
|
ElfHdr.OriginalOffset = ElfHdr.Offset = EhdrOffset;
|
2018-02-14 23:31:33 +00:00
|
|
|
|
2020-09-09 17:03:53 +03:00
|
|
|
const typename ELFT::Ehdr &Ehdr = HeadersFile.getHeader();
|
2018-02-14 23:31:33 +00:00
|
|
|
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.
|
2019-06-07 17:57:48 +00:00
|
|
|
PrHdr.OriginalOffset = PrHdr.Offset = PrHdr.VAddr = EhdrOffset + Ehdr.e_phoff;
|
2018-02-14 23:31:33 +00:00
|
|
|
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.
|
2019-05-23 09:18:57 +00:00
|
|
|
for (Segment &Child : Obj.segments())
|
2018-02-14 23:31:33 +00:00
|
|
|
setParentSegment(Child);
|
|
|
|
setParentSegment(ElfHdr);
|
|
|
|
setParentSegment(PrHdr);
|
2020-09-19 18:53:44 +03:00
|
|
|
|
|
|
|
return Error::success();
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 19:53:44 +00:00
|
|
|
template <class ELFT>
|
2020-09-19 18:53:44 +03:00
|
|
|
Error ELFBuilder<ELFT>::initGroupSection(GroupSection *GroupSec) {
|
2019-03-24 13:31:08 +00:00
|
|
|
if (GroupSec->Align % sizeof(ELF::Elf32_Word) != 0)
|
2020-09-19 18:53:44 +03:00
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
"invalid alignment " + Twine(GroupSec->Align) +
|
|
|
|
" of group section '" + GroupSec->Name + "'");
|
2019-05-23 09:18:57 +00:00
|
|
|
SectionTableRef SecTable = Obj.sections();
|
2020-06-29 10:25:26 +01:00
|
|
|
if (GroupSec->Link != SHN_UNDEF) {
|
|
|
|
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");
|
2020-09-19 18:53:44 +03:00
|
|
|
if (!SymTab)
|
|
|
|
return SymTab.takeError();
|
|
|
|
|
|
|
|
Expected<Symbol *> Sym = (*SymTab)->getSymbolByIndex(GroupSec->Info);
|
2020-06-29 10:25:26 +01:00
|
|
|
if (!Sym)
|
2020-09-19 18:53:44 +03:00
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
"info field value '" + Twine(GroupSec->Info) +
|
|
|
|
"' in section '" + GroupSec->Name +
|
|
|
|
"' is not a valid symbol index");
|
|
|
|
GroupSec->setSymTab(*SymTab);
|
|
|
|
GroupSec->setSymbol(*Sym);
|
2020-06-29 10:25:26 +01:00
|
|
|
}
|
2018-03-21 19:53:44 +00:00
|
|
|
if (GroupSec->Contents.size() % sizeof(ELF::Elf32_Word) ||
|
|
|
|
GroupSec->Contents.empty())
|
2020-09-19 18:53:44 +03:00
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
"the content of the section " + GroupSec->Name +
|
|
|
|
" is malformed");
|
2018-03-21 19:53:44 +00:00
|
|
|
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);
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<SectionBase *> Sec = SecTable.getSection(
|
2019-05-22 13:23:26 +00:00
|
|
|
Index, "group member index " + Twine(Index) + " in section '" +
|
2020-09-19 18:53:44 +03:00
|
|
|
GroupSec->Name + "' is invalid");
|
|
|
|
if (!Sec)
|
|
|
|
return Sec.takeError();
|
|
|
|
|
|
|
|
GroupSec->addMember(*Sec);
|
2018-03-21 19:53:44 +00:00
|
|
|
}
|
2020-09-19 18:53:44 +03:00
|
|
|
|
|
|
|
return Error::success();
|
2018-03-21 19:53:44 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 02:12:03 +00:00
|
|
|
template <class ELFT>
|
2020-09-19 18:53:44 +03:00
|
|
|
Error ELFBuilder<ELFT>::initSymbolTable(SymbolTableSection *SymTab) {
|
|
|
|
Expected<const Elf_Shdr *> Shdr = ElfFile.getSection(SymTab->Index);
|
|
|
|
if (!Shdr)
|
|
|
|
return Shdr.takeError();
|
|
|
|
|
|
|
|
Expected<StringRef> StrTabData = ElfFile.getStringTableForSymtab(**Shdr);
|
|
|
|
if (!StrTabData)
|
|
|
|
return StrTabData.takeError();
|
|
|
|
|
2018-07-16 19:48:52 +00:00
|
|
|
ArrayRef<Elf_Word> ShndxData;
|
2017-08-29 02:12:03 +00:00
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<typename ELFFile<ELFT>::Elf_Sym_Range> Symbols =
|
|
|
|
ElfFile.symbols(*Shdr);
|
|
|
|
if (!Symbols)
|
|
|
|
return Symbols.takeError();
|
|
|
|
|
|
|
|
for (const typename ELFFile<ELFT>::Elf_Sym &Sym : *Symbols) {
|
2017-08-29 02:12:03 +00:00
|
|
|
SectionBase *DefSection = nullptr;
|
2020-09-19 18:53:44 +03:00
|
|
|
|
|
|
|
Expected<StringRef> Name = Sym.getName(*StrTabData);
|
|
|
|
if (!Name)
|
|
|
|
return Name.takeError();
|
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)
|
2020-09-19 18:53:44 +03:00
|
|
|
return createStringError(errc::invalid_argument,
|
|
|
|
"symbol '" + *Name +
|
|
|
|
"' has index SHN_XINDEX but no "
|
|
|
|
"SHT_SYMTAB_SHNDX section exists");
|
2018-07-16 19:48:52 +00:00
|
|
|
if (ShndxData.data() == nullptr) {
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<const Elf_Shdr *> ShndxSec =
|
|
|
|
ElfFile.getSection(SymTab->getShndxTable()->Index);
|
|
|
|
if (!ShndxSec)
|
|
|
|
return ShndxSec.takeError();
|
|
|
|
|
|
|
|
Expected<ArrayRef<Elf_Word>> Data =
|
|
|
|
ElfFile.template getSectionContentsAsArray<Elf_Word>(**ShndxSec);
|
|
|
|
if (!Data)
|
|
|
|
return Data.takeError();
|
|
|
|
|
|
|
|
ShndxData = *Data;
|
|
|
|
if (ShndxData.size() != Symbols->size())
|
|
|
|
return createStringError(
|
|
|
|
errc::invalid_argument,
|
|
|
|
"symbol section index table does not have the same number of "
|
|
|
|
"entries as the symbol table");
|
2018-07-16 19:48:52 +00:00
|
|
|
}
|
2020-09-19 18:53:44 +03:00
|
|
|
Elf_Word Index = ShndxData[&Sym - Symbols->begin()];
|
|
|
|
Expected<SectionBase *> Sec = Obj.sections().getSection(
|
2018-07-16 19:48:52 +00:00
|
|
|
Index,
|
2020-09-19 18:53:44 +03:00
|
|
|
"symbol '" + *Name + "' has invalid section index " + Twine(Index));
|
|
|
|
if (!Sec)
|
|
|
|
return Sec.takeError();
|
|
|
|
|
|
|
|
DefSection = *Sec;
|
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)) {
|
2020-09-19 18:53:44 +03:00
|
|
|
return createStringError(
|
|
|
|
errc::invalid_argument,
|
|
|
|
"symbol '" + *Name +
|
|
|
|
"' has unsupported value greater than or equal "
|
|
|
|
"to SHN_LORESERVE: " +
|
|
|
|
Twine(Sym.st_shndx));
|
2017-09-07 23:02:50 +00:00
|
|
|
}
|
|
|
|
} else if (Sym.st_shndx != SHN_UNDEF) {
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<SectionBase *> Sec = Obj.sections().getSection(
|
|
|
|
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));
|
2020-09-19 18:53:44 +03:00
|
|
|
if (!Sec)
|
|
|
|
return Sec.takeError();
|
|
|
|
|
|
|
|
DefSection = *Sec;
|
2017-08-29 02:12:03 +00:00
|
|
|
}
|
2017-09-20 17:11:58 +00:00
|
|
|
|
2020-09-19 18:53:44 +03: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
|
|
|
}
|
2020-09-19 18:53:44 +03:00
|
|
|
|
|
|
|
return Error::success();
|
2017-08-29 02:12:03 +00:00
|
|
|
}
|
|
|
|
|
2017-09-06 23:41:02 +00:00
|
|
|
template <class ELFT>
|
2020-10-06 14:55:45 +03:00
|
|
|
static void getAddend(uint64_t &, const Elf_Rel_Impl<ELFT, false> &) {}
|
2017-09-06 23:41:02 +00:00
|
|
|
|
|
|
|
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>
|
2020-09-19 18:53:44 +03:00
|
|
|
static Error 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);
|
2019-10-28 12:52:28 +03:00
|
|
|
|
|
|
|
if (uint32_t Sym = Rel.getSymbol(false)) {
|
|
|
|
if (!SymbolTable)
|
2020-09-19 18:53:44 +03:00
|
|
|
return createStringError(
|
|
|
|
errc::invalid_argument,
|
|
|
|
"'" + Relocs->Name + "': relocation references symbol with index " +
|
|
|
|
Twine(Sym) + ", but there is no symbol table");
|
|
|
|
Expected<Symbol *> SymByIndex = SymbolTable->getSymbolByIndex(Sym);
|
|
|
|
if (!SymByIndex)
|
|
|
|
return SymByIndex.takeError();
|
|
|
|
|
|
|
|
ToAdd.RelocSymbol = *SymByIndex;
|
2019-10-28 12:52:28 +03:00
|
|
|
}
|
|
|
|
|
2017-09-06 23:41:02 +00:00
|
|
|
Relocs->addRelocation(ToAdd);
|
|
|
|
}
|
2020-09-19 18:53:44 +03:00
|
|
|
|
|
|
|
return Error::success();
|
2017-09-06 23:41:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<SectionBase *> SectionTableRef::getSection(uint32_t Index,
|
|
|
|
Twine ErrMsg) {
|
2017-09-25 20:37:28 +00:00
|
|
|
if (Index == SHN_UNDEF || Index > Sections.size())
|
2020-09-19 18:53:44 +03:00
|
|
|
return createStringError(errc::invalid_argument, ErrMsg);
|
2017-09-25 20:37:28 +00:00
|
|
|
return Sections[Index - 1].get();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<T *> SectionTableRef::getSectionOfType(uint32_t Index,
|
|
|
|
Twine IndexErrMsg,
|
|
|
|
Twine TypeErrMsg) {
|
|
|
|
Expected<SectionBase *> BaseSec = getSection(Index, IndexErrMsg);
|
|
|
|
if (!BaseSec)
|
|
|
|
return BaseSec.takeError();
|
|
|
|
|
|
|
|
if (T *Sec = dyn_cast<T>(*BaseSec))
|
2017-09-25 20:37:28 +00:00
|
|
|
return Sec;
|
2020-09-19 18:53:44 +03:00
|
|
|
|
|
|
|
return createStringError(errc::invalid_argument, TypeErrMsg);
|
2017-09-25 20:37:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 00:33:58 +00:00
|
|
|
template <class ELFT>
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<SectionBase &> ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
|
2017-08-01 00:33:58 +00:00
|
|
|
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) {
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Expected<ArrayRef<uint8_t>> Data = ElfFile.getSectionContents(Shdr))
|
|
|
|
return Obj.addSection<DynamicRelocationSection>(*Data);
|
|
|
|
else
|
|
|
|
return Data.takeError();
|
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) {
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Expected<ArrayRef<uint8_t>> Data = ElfFile.getSectionContents(Shdr))
|
|
|
|
return Obj.addSection<Section>(*Data);
|
|
|
|
else
|
|
|
|
return Data.takeError();
|
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.
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Expected<ArrayRef<uint8_t>> Data = ElfFile.getSectionContents(Shdr))
|
|
|
|
return Obj.addSection<Section>(*Data);
|
|
|
|
else
|
|
|
|
return Data.takeError();
|
2018-03-21 19:53:44 +00:00
|
|
|
case SHT_GROUP:
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Expected<ArrayRef<uint8_t>> Data = ElfFile.getSectionContents(Shdr))
|
|
|
|
return Obj.addSection<GroupSection>(*Data);
|
|
|
|
else
|
|
|
|
return Data.takeError();
|
2017-09-20 17:11:58 +00:00
|
|
|
case SHT_DYNSYM:
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Expected<ArrayRef<uint8_t>> Data = ElfFile.getSectionContents(Shdr))
|
|
|
|
return Obj.addSection<DynamicSymbolTableSection>(*Data);
|
|
|
|
else
|
|
|
|
return Data.takeError();
|
2017-09-20 17:11:58 +00:00
|
|
|
case SHT_DYNAMIC:
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Expected<ArrayRef<uint8_t>> Data = ElfFile.getSectionContents(Shdr))
|
|
|
|
return Obj.addSection<DynamicSection>(*Data);
|
|
|
|
else
|
|
|
|
return Data.takeError();
|
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:
|
2020-09-19 18:53:44 +03:00
|
|
|
return Obj.addSection<Section>(ArrayRef<uint8_t>());
|
2018-10-01 10:29:41 +00:00
|
|
|
default: {
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<ArrayRef<uint8_t>> Data = ElfFile.getSectionContents(Shdr);
|
|
|
|
if (!Data)
|
|
|
|
return Data.takeError();
|
|
|
|
|
|
|
|
Expected<StringRef> Name = ElfFile.getSectionName(Shdr);
|
|
|
|
if (!Name)
|
|
|
|
return Name.takeError();
|
2018-10-01 10:29:41 +00:00
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
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) =
|
2020-09-19 18:53:44 +03:00
|
|
|
getDecompressedSizeAndAlignment<ELFT>(*Data);
|
|
|
|
Expected<CompressedSection> NewSection =
|
|
|
|
CompressedSection::create(*Data, DecompressedSize, DecompressedAlign);
|
|
|
|
if (!NewSection)
|
|
|
|
return NewSection.takeError();
|
|
|
|
|
|
|
|
return Obj.addSection<CompressedSection>(std::move(*NewSection));
|
2018-10-01 10:29:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03: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
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
template <class ELFT> Error ELFBuilder<ELFT>::readSectionHeaders() {
|
2017-08-01 00:33:58 +00:00
|
|
|
uint32_t Index = 0;
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<typename ELFFile<ELFT>::Elf_Shdr_Range> Sections =
|
|
|
|
ElfFile.sections();
|
|
|
|
if (!Sections)
|
|
|
|
return Sections.takeError();
|
|
|
|
|
|
|
|
for (const typename ELFFile<ELFT>::Elf_Shdr &Shdr : *Sections) {
|
2017-08-01 00:33:58 +00:00
|
|
|
if (Index == 0) {
|
|
|
|
++Index;
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<SectionBase &> Sec = makeSection(Shdr);
|
|
|
|
if (!Sec)
|
|
|
|
return Sec.takeError();
|
|
|
|
|
|
|
|
Expected<StringRef> SecName = ElfFile.getSectionName(Shdr);
|
|
|
|
if (!SecName)
|
|
|
|
return SecName.takeError();
|
|
|
|
Sec->Name = SecName->str();
|
|
|
|
Sec->Type = Sec->OriginalType = Shdr.sh_type;
|
|
|
|
Sec->Flags = Sec->OriginalFlags = 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++;
|
2020-11-11 10:20:30 -08:00
|
|
|
Sec->OriginalIndex = Sec->Index;
|
2020-09-19 18:53:44 +03:00
|
|
|
Sec->OriginalData =
|
2018-08-09 17:05:21 +00:00
|
|
|
ArrayRef<uint8_t>(ElfFile.base() + Shdr.sh_offset,
|
2021-06-12 18:54:08 -07:00
|
|
|
(Shdr.sh_type == SHT_NOBITS) ? (size_t)0 : Shdr.sh_size);
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
2020-09-19 18:53:44 +03:00
|
|
|
|
|
|
|
return Error::success();
|
2019-06-07 17:57:48 +00:00
|
|
|
}
|
2017-08-29 02:12:03 +00:00
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
template <class ELFT> Error ELFBuilder<ELFT>::readSections(bool EnsureSymtab) {
|
2020-09-09 17:03:53 +03:00
|
|
|
uint32_t ShstrIndex = ElfFile.getHeader().e_shstrndx;
|
2020-09-19 18:53:44 +03:00
|
|
|
if (ShstrIndex == SHN_XINDEX) {
|
|
|
|
Expected<const Elf_Shdr *> Sec = ElfFile.getSection(0);
|
|
|
|
if (!Sec)
|
|
|
|
return Sec.takeError();
|
|
|
|
|
|
|
|
ShstrIndex = (*Sec)->sh_link;
|
|
|
|
}
|
2019-10-31 09:12:06 -07:00
|
|
|
|
|
|
|
if (ShstrIndex == SHN_UNDEF)
|
|
|
|
Obj.HadShdrs = false;
|
2020-09-19 18:53:44 +03:00
|
|
|
else {
|
|
|
|
Expected<StringTableSection *> Sec =
|
2019-10-31 09:12:06 -07:00
|
|
|
Obj.sections().template getSectionOfType<StringTableSection>(
|
|
|
|
ShstrIndex,
|
|
|
|
"e_shstrndx field value " + Twine(ShstrIndex) + " in elf header " +
|
|
|
|
" is invalid",
|
|
|
|
"e_shstrndx field value " + Twine(ShstrIndex) + " in elf header " +
|
|
|
|
" does not reference a string table");
|
2020-09-19 18:53:44 +03:00
|
|
|
if (!Sec)
|
|
|
|
return Sec.takeError();
|
|
|
|
|
|
|
|
Obj.SectionNames = *Sec;
|
|
|
|
}
|
2019-10-31 09:12:06 -07: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)
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = Obj.SectionIndexTable->initialize(Obj.sections()))
|
|
|
|
return Err;
|
2018-07-16 19:48:52 +00:00
|
|
|
|
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) {
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = Obj.SymbolTable->initialize(Obj.sections()))
|
|
|
|
return Err;
|
|
|
|
if (Error Err = initSymbolTable(Obj.SymbolTable))
|
|
|
|
return Err;
|
2019-10-17 11:21:54 +00:00
|
|
|
} else if (EnsureSymtab) {
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = Obj.addNewSymbolTable())
|
|
|
|
return Err;
|
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.
|
2020-09-19 18:53:44 +03:00
|
|
|
for (SectionBase &Sec : Obj.sections()) {
|
2019-09-04 08:43:27 +00:00
|
|
|
if (&Sec == Obj.SymbolTable)
|
2017-09-25 20:37:28 +00:00
|
|
|
continue;
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = Sec.initialize(Obj.sections()))
|
|
|
|
return Err;
|
2019-09-04 08:43:27 +00:00
|
|
|
if (auto RelSec = dyn_cast<RelocationSection>(&Sec)) {
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<typename ELFFile<ELFT>::Elf_Shdr_Range> Sections =
|
|
|
|
ElfFile.sections();
|
|
|
|
if (!Sections)
|
|
|
|
return Sections.takeError();
|
|
|
|
|
|
|
|
const typename ELFFile<ELFT>::Elf_Shdr *Shdr =
|
|
|
|
Sections->begin() + RelSec->Index;
|
|
|
|
if (RelSec->Type == SHT_REL) {
|
|
|
|
Expected<typename ELFFile<ELFT>::Elf_Rel_Range> Rels =
|
|
|
|
ElfFile.rels(*Shdr);
|
|
|
|
if (!Rels)
|
|
|
|
return Rels.takeError();
|
|
|
|
|
|
|
|
if (Error Err = initRelocations(RelSec, Obj.SymbolTable, *Rels))
|
|
|
|
return Err;
|
|
|
|
} else {
|
|
|
|
Expected<typename ELFFile<ELFT>::Elf_Rela_Range> Relas =
|
|
|
|
ElfFile.relas(*Shdr);
|
|
|
|
if (!Relas)
|
|
|
|
return Relas.takeError();
|
|
|
|
|
|
|
|
if (Error Err = initRelocations(RelSec, Obj.SymbolTable, *Relas))
|
|
|
|
return Err;
|
|
|
|
}
|
2019-09-04 08:43:27 +00:00
|
|
|
} else if (auto GroupSec = dyn_cast<GroupSection>(&Sec)) {
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = initGroupSection(GroupSec))
|
|
|
|
return Err;
|
2017-09-06 23:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-19 18:53:44 +03:00
|
|
|
|
|
|
|
return Error::success();
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
template <class ELFT> Error ELFBuilder<ELFT>::build(bool EnsureSymtab) {
|
|
|
|
if (Error E = readSectionHeaders())
|
|
|
|
return E;
|
|
|
|
if (Error E = findEhdrOffset())
|
|
|
|
return E;
|
2019-06-07 17:57:48 +00:00
|
|
|
|
|
|
|
// The ELFFile whose ELF headers and program headers are copied into the
|
|
|
|
// output file. Normally the same as ElfFile, but if we're extracting a
|
|
|
|
// loadable partition it will point to the partition's headers.
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<ELFFile<ELFT>> HeadersFile = ELFFile<ELFT>::create(toStringRef(
|
|
|
|
{ElfFile.base() + EhdrOffset, ElfFile.getBufSize() - EhdrOffset}));
|
|
|
|
if (!HeadersFile)
|
|
|
|
return HeadersFile.takeError();
|
2018-01-25 20:24:17 +00:00
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
const typename ELFFile<ELFT>::Elf_Ehdr &Ehdr = HeadersFile->getHeader();
|
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;
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error E = readSections(EnsureSymtab))
|
|
|
|
return E;
|
|
|
|
return readProgramHeaders(*HeadersFile);
|
2018-01-25 20:24:17 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
Writer::~Writer() {}
|
|
|
|
|
|
|
|
Reader::~Reader() {}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<std::unique_ptr<Object>>
|
|
|
|
BinaryReader::create(bool /*EnsureSymtab*/) const {
|
2019-09-14 01:36:31 +00:00
|
|
|
return BinaryELFBuilder(MemBuf, NewSymbolVisibility).build();
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 09:56:14 +00:00
|
|
|
Expected<std::vector<IHexRecord>> IHexReader::parse() const {
|
|
|
|
SmallVector<StringRef, 16> Lines;
|
|
|
|
std::vector<IHexRecord> Records;
|
|
|
|
bool HasSections = false;
|
|
|
|
|
|
|
|
MemBuf->getBuffer().split(Lines, '\n');
|
|
|
|
Records.reserve(Lines.size());
|
|
|
|
for (size_t LineNo = 1; LineNo <= Lines.size(); ++LineNo) {
|
|
|
|
StringRef Line = Lines[LineNo - 1].trim();
|
|
|
|
if (Line.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Expected<IHexRecord> R = IHexRecord::parse(Line);
|
|
|
|
if (!R)
|
|
|
|
return parseError(LineNo, R.takeError());
|
|
|
|
if (R->Type == IHexRecord::EndOfFile)
|
|
|
|
break;
|
|
|
|
HasSections |= (R->Type == IHexRecord::Data);
|
|
|
|
Records.push_back(*R);
|
|
|
|
}
|
|
|
|
if (!HasSections)
|
|
|
|
return parseError(-1U, "no sections");
|
|
|
|
|
2020-02-10 07:06:45 -08:00
|
|
|
return std::move(Records);
|
2019-06-13 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<std::unique_ptr<Object>>
|
|
|
|
IHexReader::create(bool /*EnsureSymtab*/) const {
|
|
|
|
Expected<std::vector<IHexRecord>> Records = parse();
|
|
|
|
if (!Records)
|
|
|
|
return Records.takeError();
|
|
|
|
|
|
|
|
return IHexELFBuilder(*Records).build();
|
2019-06-13 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Expected<std::unique_ptr<Object>> ELFReader::create(bool EnsureSymtab) const {
|
2019-08-15 15:54:37 +00:00
|
|
|
auto Obj = std::make_unique<Object>();
|
2018-11-01 16:02:12 +00:00
|
|
|
if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(Bin)) {
|
2019-06-07 17:57:48 +00:00
|
|
|
ELFBuilder<ELF32LE> Builder(*O, *Obj, ExtractPartition);
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = Builder.build(EnsureSymtab))
|
|
|
|
return std::move(Err);
|
|
|
|
return std::move(Obj);
|
2018-11-01 16:02:12 +00:00
|
|
|
} else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(Bin)) {
|
2019-06-07 17:57:48 +00:00
|
|
|
ELFBuilder<ELF64LE> Builder(*O, *Obj, ExtractPartition);
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = Builder.build(EnsureSymtab))
|
|
|
|
return std::move(Err);
|
|
|
|
return std::move(Obj);
|
2018-11-01 16:02:12 +00:00
|
|
|
} else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(Bin)) {
|
2019-06-07 17:57:48 +00:00
|
|
|
ELFBuilder<ELF32BE> Builder(*O, *Obj, ExtractPartition);
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = Builder.build(EnsureSymtab))
|
|
|
|
return std::move(Err);
|
|
|
|
return std::move(Obj);
|
2018-11-01 16:02:12 +00:00
|
|
|
} else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(Bin)) {
|
2019-06-07 17:57:48 +00:00
|
|
|
ELFBuilder<ELF64BE> Builder(*O, *Obj, ExtractPartition);
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = Builder.build(EnsureSymtab))
|
|
|
|
return std::move(Err);
|
|
|
|
return std::move(Obj);
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
2020-09-19 18:53:44 +03:00
|
|
|
return createStringError(errc::invalid_argument, "invalid file type");
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void ELFWriter<ELFT>::writeEhdr() {
|
2020-10-24 17:35:55 +03:00
|
|
|
Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(Buf->getBufferStart());
|
[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);
|
[llvm-objcopy] Replace the size() helper with SectionTableRef::size
Summary:
BTW, STLExtras.h provides llvm::size() which is similar to std::size()
for random access iterators. However, if we prefer qualified
llvm::size(), the member function .size() will be more convenient.
Reviewers: jhenderson, jakehehrlich, rupprecht, grimar, alexshap, espindola
Reviewed By: grimar
Subscribers: emaste, arichardson, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60028
llvm-svn: 357347
2019-03-30 14:08:59 +00:00
|
|
|
if (WriteSectionHeaders && Obj.sections().size() != 0) {
|
2018-09-12 17:56:31 +00:00
|
|
|
Ehdr.e_shentsize = sizeof(Elf_Shdr);
|
2019-09-07 01:38:56 +00:00
|
|
|
Ehdr.e_shoff = Obj.SHOff;
|
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.
|
|
|
|
// """
|
[llvm-objcopy] Replace the size() helper with SectionTableRef::size
Summary:
BTW, STLExtras.h provides llvm::size() which is similar to std::size()
for random access iterators. However, if we prefer qualified
llvm::size(), the member function .size() will be more convenient.
Reviewers: jhenderson, jakehehrlich, rupprecht, grimar, alexshap, espindola
Reviewed By: grimar
Subscribers: emaste, arichardson, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60028
llvm-svn: 357347
2019-03-30 14:08:59 +00:00
|
|
|
auto Shnum = Obj.sections().size() + 1;
|
2018-07-16 19:48:52 +00:00
|
|
|
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() {
|
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
|
2019-05-23 09:18:57 +00:00
|
|
|
Elf_Shdr &Shdr =
|
2020-10-24 17:35:55 +03:00
|
|
|
*reinterpret_cast<Elf_Shdr *>(Buf->getBufferStart() + Obj.SHOff);
|
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.
|
[llvm-objcopy] Replace the size() helper with SectionTableRef::size
Summary:
BTW, STLExtras.h provides llvm::size() which is similar to std::size()
for random access iterators. However, if we prefer qualified
llvm::size(), the member function .size() will be more convenient.
Reviewers: jhenderson, jakehehrlich, rupprecht, grimar, alexshap, espindola
Reviewed By: grimar
Subscribers: emaste, arichardson, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60028
llvm-svn: 357347
2019-03-30 14:08:59 +00:00
|
|
|
uint64_t Shnum = Obj.sections().size() + 1;
|
2018-07-16 19:48:52 +00:00
|
|
|
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;
|
|
|
|
|
2019-05-23 09:18:57 +00:00
|
|
|
for (SectionBase &Sec : Obj.sections())
|
2018-01-25 22:46:17 +00:00
|
|
|
writeShdr(Sec);
|
2017-08-04 21:09:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
template <class ELFT> Error ELFWriter<ELFT>::writeSectionData() {
|
2019-05-23 09:18:57 +00:00
|
|
|
for (SectionBase &Sec : Obj.sections())
|
2019-03-25 16:36:26 +00:00
|
|
|
// Segments are responsible for writing their contents, so only write the
|
|
|
|
// section data if the section is not in a segment. Note that this renders
|
|
|
|
// sections in segments effectively immutable.
|
|
|
|
if (Sec.ParentSegment == nullptr)
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = Sec.accept(*SecWriter))
|
|
|
|
return Err;
|
|
|
|
|
|
|
|
return Error::success();
|
2019-03-25 16:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void ELFWriter<ELFT>::writeSegmentData() {
|
|
|
|
for (Segment &Seg : Obj.segments()) {
|
2019-10-24 15:48:32 -07:00
|
|
|
size_t Size = std::min<size_t>(Seg.FileSize, Seg.getContents().size());
|
2020-10-24 17:35:55 +03:00
|
|
|
std::memcpy(Buf->getBufferStart() + Seg.Offset, Seg.getContents().data(),
|
2019-10-24 15:48:32 -07:00
|
|
|
Size);
|
2019-03-25 16:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over removed sections and overwrite their old data with zeroes.
|
|
|
|
for (auto &Sec : Obj.removedSections()) {
|
|
|
|
Segment *Parent = Sec.ParentSegment;
|
|
|
|
if (Parent == nullptr || Sec.Type == SHT_NOBITS || Sec.Size == 0)
|
|
|
|
continue;
|
|
|
|
uint64_t Offset =
|
|
|
|
Sec.OriginalOffset - Parent->OriginalOffset + Parent->Offset;
|
2020-10-24 17:35:55 +03:00
|
|
|
std::memset(Buf->getBufferStart() + Offset, 0, Sec.Size);
|
2019-03-25 16:36:26 +00:00
|
|
|
}
|
2017-08-04 21:09:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 14:11:13 +00:00
|
|
|
template <class ELFT>
|
2020-10-24 17:35:55 +03:00
|
|
|
ELFWriter<ELFT>::ELFWriter(Object &Obj, raw_ostream &Buf, bool WSH,
|
2019-10-24 15:48:32 -07:00
|
|
|
bool OnlyKeepDebug)
|
|
|
|
: Writer(Obj, Buf), WriteSectionHeaders(WSH && Obj.HadShdrs),
|
|
|
|
OnlyKeepDebug(OnlyKeepDebug) {}
|
2019-04-02 14:11:13 +00:00
|
|
|
|
2020-10-06 14:55:45 +03:00
|
|
|
Error Object::removeSections(
|
|
|
|
bool AllowBrokenLinks, 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());
|
2020-05-29 19:27:28 +07:00
|
|
|
RemoveSec->onRemove();
|
[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)) {
|
2020-10-06 14:55:45 +03:00
|
|
|
if (Error E = KeepSec->removeSectionReferences(
|
|
|
|
AllowBrokenLinks, [&RemoveSections](const SectionBase *Sec) {
|
[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
|
|
|
return RemoveSections.find(Sec) != RemoveSections.end();
|
|
|
|
}))
|
|
|
|
return E;
|
2019-02-27 11:18:27 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 16:36:26 +00:00
|
|
|
// Transfer removed sections into the Object RemovedSections container for use
|
|
|
|
// later.
|
|
|
|
std::move(Iter, Sections.end(), std::back_inserter(RemovedSections));
|
|
|
|
// Now finally get rid of them all together.
|
2017-10-10 18:47:09 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-19 18:53:44 +03:00
|
|
|
Error Object::addNewSymbolTable() {
|
2020-07-01 10:15:39 +03:00
|
|
|
assert(!SymbolTable && "Object must not has a SymbolTable.");
|
|
|
|
|
|
|
|
// Reuse an existing SHT_STRTAB section if it exists.
|
|
|
|
StringTableSection *StrTab = nullptr;
|
|
|
|
for (SectionBase &Sec : sections()) {
|
|
|
|
if (Sec.Type == ELF::SHT_STRTAB && !(Sec.Flags & SHF_ALLOC)) {
|
|
|
|
StrTab = static_cast<StringTableSection *>(&Sec);
|
|
|
|
|
|
|
|
// Prefer a string table that is not the section header string table, if
|
|
|
|
// such a table exists.
|
|
|
|
if (SectionNames != &Sec)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!StrTab)
|
|
|
|
StrTab = &addSection<StringTableSection>();
|
|
|
|
|
|
|
|
SymbolTableSection &SymTab = addSection<SymbolTableSection>();
|
|
|
|
SymTab.Name = ".symtab";
|
|
|
|
SymTab.Link = StrTab->Index;
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = SymTab.initialize(sections()))
|
|
|
|
return Err;
|
2020-07-01 10:15:39 +03:00
|
|
|
SymTab.addSymbol("", 0, 0, nullptr, 0, 0, 0, 0);
|
|
|
|
|
|
|
|
SymbolTable = &SymTab;
|
2020-09-19 18:53:44 +03:00
|
|
|
|
|
|
|
return Error::success();
|
2020-07-01 10:15:39 +03:00
|
|
|
}
|
|
|
|
|
2018-01-25 22:46:17 +00:00
|
|
|
void Object::sortSections() {
|
[llvm-objcopy] Fix SHT_GROUP ordering.
Summary:
When llvm-objcopy sorts sections during finalization, it only sorts based on the offset, which can cause the group section to come after the sections it contains. This causes link failures when using gold to link objects created by llvm-objcopy.
Fix this for now by copying GNU objcopy's behavior of placing SHT_GROUP sections first. In the future, we may want to remove this sorting entirely to more closely preserve the input file layout.
This fixes https://bugs.llvm.org/show_bug.cgi?id=42052.
Reviewers: jakehehrlich, jhenderson, MaskRay, espindola, alexshap
Reviewed By: MaskRay
Subscribers: phuongtrang148993, emaste, arichardson, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62620
llvm-svn: 362973
2019-06-10 18:35:01 +00:00
|
|
|
// Use stable_sort to maintain the original ordering as closely as possible.
|
2019-04-22 15:53:43 +00:00
|
|
|
llvm::stable_sort(Sections, [](const SecPtr &A, const SecPtr &B) {
|
[llvm-objcopy] Fix SHT_GROUP ordering.
Summary:
When llvm-objcopy sorts sections during finalization, it only sorts based on the offset, which can cause the group section to come after the sections it contains. This causes link failures when using gold to link objects created by llvm-objcopy.
Fix this for now by copying GNU objcopy's behavior of placing SHT_GROUP sections first. In the future, we may want to remove this sorting entirely to more closely preserve the input file layout.
This fixes https://bugs.llvm.org/show_bug.cgi?id=42052.
Reviewers: jakehehrlich, jhenderson, MaskRay, espindola, alexshap
Reviewed By: MaskRay
Subscribers: phuongtrang148993, emaste, arichardson, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D62620
llvm-svn: 362973
2019-06-10 18:35:01 +00:00
|
|
|
// Put SHT_GROUP sections first, since group section headers must come
|
|
|
|
// before the sections they contain. This also matches what GNU objcopy
|
|
|
|
// does.
|
|
|
|
if (A->Type != B->Type &&
|
|
|
|
(A->Type == ELF::SHT_GROUP || B->Type == ELF::SHT_GROUP))
|
|
|
|
return A->Type == ELF::SHT_GROUP;
|
|
|
|
// For all other sections, sort by offset order.
|
2017-08-01 00:33:58 +00:00
|
|
|
return A->OriginalOffset < B->OriginalOffset;
|
2019-04-22 15:53:43 +00:00
|
|
|
});
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2019-04-22 15:53:43 +00:00
|
|
|
llvm::stable_sort(Segments, compareSegmentsByOffset);
|
2017-11-15 19:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function finds a consistent layout for a list of segments starting from
|
2019-09-04 03:54:43 +00:00
|
|
|
// an Offset. It assumes that Segments have been sorted by orderSegments and
|
2017-11-15 19:13:31 +00:00
|
|
|
// returns an Offset one past the end of the last segment.
|
2019-03-29 15:27:58 +00:00
|
|
|
static uint64_t layoutSegments(std::vector<Segment *> &Segments,
|
2017-11-15 19:13:31 +00:00
|
|
|
uint64_t Offset) {
|
2020-04-13 14:46:41 +03:00
|
|
|
assert(llvm::is_sorted(Segments, 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.
|
2019-05-23 09:18:57 +00:00
|
|
|
for (Segment *Seg : 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.
|
2019-05-23 09:18:57 +00:00
|
|
|
if (Seg->ParentSegment != nullptr) {
|
|
|
|
Segment *Parent = Seg->ParentSegment;
|
|
|
|
Seg->Offset =
|
|
|
|
Parent->Offset + Seg->OriginalOffset - Parent->OriginalOffset;
|
2017-09-19 21:37:35 +00:00
|
|
|
} else {
|
2019-08-31 10:48:09 +00:00
|
|
|
Seg->Offset =
|
|
|
|
alignTo(Offset, std::max<uint64_t>(Seg->Align, 1), Seg->VAddr);
|
2017-09-19 21:37:35 +00:00
|
|
|
}
|
2019-05-23 09:18:57 +00:00
|
|
|
Offset = std::max(Offset, Seg->Offset + Seg->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;
|
2019-09-04 08:43:27 +00:00
|
|
|
for (auto &Sec : Sections) {
|
|
|
|
Sec.Index = Index++;
|
|
|
|
if (Sec.ParentSegment != nullptr) {
|
|
|
|
auto Segment = *Sec.ParentSegment;
|
|
|
|
Sec.Offset =
|
|
|
|
Segment.Offset + (Sec.OriginalOffset - Segment.OriginalOffset);
|
2017-08-26 01:32:20 +00:00
|
|
|
} else {
|
2019-09-04 08:43:27 +00:00
|
|
|
Offset = alignTo(Offset, Sec.Align == 0 ? 1 : Sec.Align);
|
|
|
|
Sec.Offset = Offset;
|
|
|
|
if (Sec.Type != SHT_NOBITS)
|
|
|
|
Offset += Sec.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
|
|
|
|
2019-10-24 15:48:32 -07:00
|
|
|
// Rewrite sh_offset after some sections are changed to SHT_NOBITS and thus
|
|
|
|
// occupy no space in the file.
|
|
|
|
static uint64_t layoutSectionsForOnlyKeepDebug(Object &Obj, uint64_t Off) {
|
|
|
|
uint32_t Index = 1;
|
|
|
|
for (auto &Sec : Obj.sections()) {
|
|
|
|
Sec.Index = Index++;
|
|
|
|
|
|
|
|
auto *FirstSec = Sec.ParentSegment && Sec.ParentSegment->Type == PT_LOAD
|
|
|
|
? Sec.ParentSegment->firstSection()
|
|
|
|
: nullptr;
|
|
|
|
|
|
|
|
// The first section in a PT_LOAD has to have congruent offset and address
|
|
|
|
// modulo the alignment, which usually equals the maximum page size.
|
|
|
|
if (FirstSec && FirstSec == &Sec)
|
|
|
|
Off = alignTo(Off, Sec.ParentSegment->Align, Sec.Addr);
|
|
|
|
|
|
|
|
// sh_offset is not significant for SHT_NOBITS sections, but the congruence
|
|
|
|
// rule must be followed if it is the first section in a PT_LOAD. Do not
|
|
|
|
// advance Off.
|
|
|
|
if (Sec.Type == SHT_NOBITS) {
|
|
|
|
Sec.Offset = Off;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!FirstSec) {
|
|
|
|
// FirstSec being nullptr generally means that Sec does not have the
|
|
|
|
// SHF_ALLOC flag.
|
|
|
|
Off = Sec.Align ? alignTo(Off, Sec.Align) : Off;
|
|
|
|
} else if (FirstSec != &Sec) {
|
|
|
|
// The offset is relative to the first section in the PT_LOAD segment. Use
|
|
|
|
// sh_offset for non-SHF_ALLOC sections.
|
|
|
|
Off = Sec.OriginalOffset - FirstSec->OriginalOffset + FirstSec->Offset;
|
|
|
|
}
|
|
|
|
Sec.Offset = Off;
|
|
|
|
Off += Sec.Size;
|
|
|
|
}
|
|
|
|
return Off;
|
|
|
|
}
|
|
|
|
|
2020-11-11 09:21:10 -08:00
|
|
|
// Rewrite p_offset and p_filesz of non-PT_PHDR segments after sh_offset values
|
|
|
|
// have been updated.
|
2019-10-24 15:48:32 -07:00
|
|
|
static uint64_t layoutSegmentsForOnlyKeepDebug(std::vector<Segment *> &Segments,
|
|
|
|
uint64_t HdrEnd) {
|
|
|
|
uint64_t MaxOffset = 0;
|
|
|
|
for (Segment *Seg : Segments) {
|
2021-05-05 10:26:57 -07:00
|
|
|
if (Seg->Type == PT_PHDR)
|
2019-10-24 15:48:32 -07:00
|
|
|
continue;
|
|
|
|
|
2021-05-05 10:26:57 -07:00
|
|
|
// The segment offset is generally the offset of the first section.
|
|
|
|
//
|
|
|
|
// For a segment containing no section (see sectionWithinSegment), if it has
|
|
|
|
// a parent segment, copy the parent segment's offset field. This works for
|
|
|
|
// empty PT_TLS. If no parent segment, use 0: the segment is not useful for
|
|
|
|
// debugging anyway.
|
|
|
|
const SectionBase *FirstSec = Seg->firstSection();
|
|
|
|
uint64_t Offset =
|
|
|
|
FirstSec ? FirstSec->Offset
|
|
|
|
: (Seg->ParentSegment ? Seg->ParentSegment->Offset : 0);
|
2019-10-24 15:48:32 -07:00
|
|
|
uint64_t FileSize = 0;
|
|
|
|
for (const SectionBase *Sec : Seg->Sections) {
|
|
|
|
uint64_t Size = Sec->Type == SHT_NOBITS ? 0 : Sec->Size;
|
|
|
|
if (Sec->Offset + Size > Offset)
|
|
|
|
FileSize = std::max(FileSize, Sec->Offset + Size - Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the segment includes EHDR and program headers, don't make it smaller
|
|
|
|
// than the headers.
|
|
|
|
if (Seg->Offset < HdrEnd && HdrEnd <= Seg->Offset + Seg->FileSize) {
|
|
|
|
FileSize += Offset - Seg->Offset;
|
|
|
|
Offset = Seg->Offset;
|
|
|
|
FileSize = std::max(FileSize, HdrEnd - Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
Seg->Offset = Offset;
|
|
|
|
Seg->FileSize = FileSize;
|
|
|
|
MaxOffset = std::max(MaxOffset, Offset + FileSize);
|
|
|
|
}
|
|
|
|
return MaxOffset;
|
|
|
|
}
|
|
|
|
|
[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() {
|
2019-05-23 09:18:57 +00:00
|
|
|
Segment &ElfHdr = Obj.ElfHdrSegment;
|
[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
|
|
|
ElfHdr.Type = PT_PHDR;
|
|
|
|
ElfHdr.Flags = 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;
|
2019-05-23 09:18:57 +00:00
|
|
|
for (Segment &Segment : Obj.segments())
|
2018-01-25 22:46:17 +00:00
|
|
|
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);
|
2019-10-24 15:48:32 -07:00
|
|
|
|
|
|
|
uint64_t Offset;
|
|
|
|
if (OnlyKeepDebug) {
|
|
|
|
// For --only-keep-debug, the sections that did not preserve contents were
|
|
|
|
// changed to SHT_NOBITS. We now rewrite sh_offset fields of sections, and
|
|
|
|
// then rewrite p_offset/p_filesz of program headers.
|
|
|
|
uint64_t HdrEnd =
|
|
|
|
sizeof(Elf_Ehdr) + llvm::size(Obj.segments()) * sizeof(Elf_Phdr);
|
|
|
|
Offset = layoutSectionsForOnlyKeepDebug(Obj, HdrEnd);
|
|
|
|
Offset = std::max(Offset,
|
|
|
|
layoutSegmentsForOnlyKeepDebug(OrderedSegments, HdrEnd));
|
|
|
|
} else {
|
|
|
|
// 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.
|
|
|
|
Offset = layoutSegments(OrderedSegments, 0);
|
|
|
|
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));
|
2019-09-07 01:38:56 +00:00
|
|
|
Obj.SHOff = 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.
|
2019-04-02 14:11:13 +00:00
|
|
|
if (!WriteSectionHeaders)
|
2019-09-07 01:38:56 +00:00
|
|
|
return Obj.SHOff;
|
2019-04-02 14:11:13 +00:00
|
|
|
size_t ShdrCount = Obj.sections().size() + 1; // Includes null shdr.
|
2019-09-07 01:38:56 +00:00
|
|
|
return Obj.SHOff + ShdrCount * sizeof(Elf_Shdr);
|
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() {
|
2019-03-25 16:36:26 +00:00
|
|
|
// Segment data must be written first, so that the ELF header and program
|
|
|
|
// header tables can overwrite it, if covered by a segment.
|
|
|
|
writeSegmentData();
|
2018-01-25 22:46:17 +00:00
|
|
|
writeEhdr();
|
|
|
|
writePhdrs();
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error E = writeSectionData())
|
|
|
|
return E;
|
2018-01-25 22:46:17 +00:00
|
|
|
if (WriteSectionHeaders)
|
|
|
|
writeShdrs();
|
2020-10-24 17:35:55 +03:00
|
|
|
|
|
|
|
// TODO: Implement direct writing to the output stream (without intermediate
|
|
|
|
// memory buffer Buf).
|
|
|
|
Out.write(Buf->getBufferStart(), Buf->getBufferSize());
|
|
|
|
return Error::success();
|
2018-01-25 22:46:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 08:03:30 +00:00
|
|
|
static Error removeUnneededSections(Object &Obj) {
|
|
|
|
// We can remove an empty symbol table from non-relocatable objects.
|
|
|
|
// Relocatable objects typically have relocation sections whose
|
|
|
|
// sh_link field points to .symtab, so we can't remove .symtab
|
|
|
|
// even if it is empty.
|
|
|
|
if (Obj.isRelocatable() || Obj.SymbolTable == nullptr ||
|
|
|
|
!Obj.SymbolTable->empty())
|
|
|
|
return Error::success();
|
|
|
|
|
|
|
|
// .strtab can be used for section names. In such a case we shouldn't
|
|
|
|
// remove it.
|
|
|
|
auto *StrTab = Obj.SymbolTable->getStrTab() == Obj.SectionNames
|
|
|
|
? nullptr
|
|
|
|
: Obj.SymbolTable->getStrTab();
|
|
|
|
return Obj.removeSections(false, [&](const SectionBase &Sec) {
|
|
|
|
return &Sec == Obj.SymbolTable || &Sec == StrTab;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
[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,
|
2019-05-22 13:23:26 +00:00
|
|
|
"cannot write section header table because "
|
|
|
|
"section header string table was removed");
|
2018-01-25 22:46:17 +00:00
|
|
|
|
2019-07-23 08:03:30 +00:00
|
|
|
if (Error E = removeUnneededSections(Obj))
|
|
|
|
return E;
|
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;
|
[llvm-objcopy] Replace the size() helper with SectionTableRef::size
Summary:
BTW, STLExtras.h provides llvm::size() which is similar to std::size()
for random access iterators. However, if we prefer qualified
llvm::size(), the member function .size() will be more convenient.
Reviewers: jhenderson, jakehehrlich, rupprecht, grimar, alexshap, espindola
Reviewed By: grimar
Subscribers: emaste, arichardson, jdoerfert, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60028
llvm-svn: 357347
2019-03-30 14:08:59 +00:00
|
|
|
if (Obj.sections().size() >= SHN_LORESERVE) {
|
2019-05-23 09:18:57 +00:00
|
|
|
SectionTableRef Sections = Obj.sections();
|
2021-02-26 13:44:48 +00:00
|
|
|
// Sections doesn't include the null section header, so account for this
|
|
|
|
// when skipping the first N sections.
|
2018-07-16 19:48:52 +00:00
|
|
|
NeedsLargeIndexes =
|
2021-02-26 13:44:48 +00:00
|
|
|
any_of(drop_begin(Sections, SHN_LORESERVE - 1),
|
2021-01-19 20:19:15 -08:00
|
|
|
[](const SectionBase &Sec) { return Sec.HasSymbol; });
|
2018-07-16 19:48:52 +00:00
|
|
|
// 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-04-18 09:13:30 +00:00
|
|
|
// We do not support sections referring to the section index table.
|
|
|
|
if (Error E = Obj.removeSections(false /*AllowBrokenLinks*/,
|
|
|
|
[this](const SectionBase &Sec) {
|
|
|
|
return &Sec == Obj.SectionIndexTable;
|
|
|
|
}))
|
2019-02-01 15:20:36 +00:00
|
|
|
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)
|
2019-09-04 08:43:27 +00:00
|
|
|
for (const SectionBase &Sec : Obj.sections())
|
|
|
|
Obj.SectionNames->addString(Sec.Name);
|
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;
|
2019-08-15 15:54:37 +00:00
|
|
|
auto SecSizer = std::make_unique<ELFSectionSizer<ELFT>>();
|
2019-09-04 08:43:27 +00:00
|
|
|
for (SectionBase &Sec : Obj.sections()) {
|
2018-07-16 19:48:52 +00:00
|
|
|
Sec.Index = Index++;
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = Sec.accept(*SecSizer))
|
|
|
|
return Err;
|
[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
|
|
|
|
|
|
|
// 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.
|
2019-05-23 09:18:57 +00:00
|
|
|
for (SectionBase &Sec : Obj.sections())
|
2019-03-18 14:27:41 +00:00
|
|
|
if (auto StrTab = dyn_cast<StringTableSection>(&Sec))
|
|
|
|
StrTab->prepareForLayout();
|
|
|
|
|
2017-08-01 00:33:58 +00:00
|
|
|
assignOffsets();
|
|
|
|
|
2019-04-12 11:59:30 +00:00
|
|
|
// layoutSections could have modified section indexes, so we need
|
|
|
|
// to fill the index table after assignOffsets.
|
|
|
|
if (Obj.SymbolTable != nullptr)
|
|
|
|
Obj.SymbolTable->fillShndxTable();
|
|
|
|
|
2017-08-01 00:33:58 +00:00
|
|
|
// Finally now that all offsets and indexes have been set we can finalize any
|
|
|
|
// remaining issues.
|
2019-09-07 01:38:56 +00:00
|
|
|
uint64_t Offset = Obj.SHOff + sizeof(Elf_Shdr);
|
2019-09-04 08:43:27 +00:00
|
|
|
for (SectionBase &Sec : Obj.sections()) {
|
|
|
|
Sec.HeaderOffset = Offset;
|
2017-08-01 00:33:58 +00:00
|
|
|
Offset += sizeof(Elf_Shdr);
|
2018-01-25 22:46:17 +00:00
|
|
|
if (WriteSectionHeaders)
|
2019-09-04 08:43:27 +00:00
|
|
|
Sec.NameIndex = Obj.SectionNames->findIndex(Sec.Name);
|
|
|
|
Sec.finalize();
|
2017-08-01 00:33:58 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 17:35:55 +03:00
|
|
|
size_t TotalSize = totalSize();
|
|
|
|
Buf = WritableMemoryBuffer::getNewMemBuffer(TotalSize);
|
|
|
|
if (!Buf)
|
|
|
|
return createStringError(errc::not_enough_memory,
|
|
|
|
"failed to allocate memory buffer of " +
|
|
|
|
Twine::utohexstr(TotalSize) + " bytes");
|
|
|
|
|
|
|
|
SecWriter = std::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() {
|
2019-09-04 09:35:32 +00:00
|
|
|
for (const SectionBase &Sec : Obj.allocSections())
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = Sec.accept(*SecWriter))
|
|
|
|
return Err;
|
|
|
|
|
2020-10-24 17:35:55 +03:00
|
|
|
// TODO: Implement direct writing to the output stream (without intermediate
|
|
|
|
// memory buffer Buf).
|
|
|
|
Out.write(Buf->getBufferStart(), Buf->getBufferSize());
|
|
|
|
return Error::success();
|
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() {
|
2019-12-04 13:48:19 -08:00
|
|
|
// Compute the section LMA based on its sh_offset and the containing segment's
|
2020-04-30 22:15:05 -07:00
|
|
|
// p_offset and p_paddr. Also compute the minimum LMA of all non-empty
|
|
|
|
// sections as MinAddr. In the output, the contents between address 0 and
|
|
|
|
// MinAddr will be skipped.
|
2019-12-04 13:48:19 -08:00
|
|
|
uint64_t MinAddr = UINT64_MAX;
|
|
|
|
for (SectionBase &Sec : Obj.allocSections()) {
|
|
|
|
if (Sec.ParentSegment != nullptr)
|
|
|
|
Sec.Addr =
|
|
|
|
Sec.Offset - Sec.ParentSegment->Offset + Sec.ParentSegment->PAddr;
|
2021-02-01 15:01:25 -08:00
|
|
|
if (Sec.Type != SHT_NOBITS && Sec.Size > 0)
|
2020-04-30 22:15:05 -07:00
|
|
|
MinAddr = std::min(MinAddr, Sec.Addr);
|
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
|
2019-09-04 03:54:43 +00:00
|
|
|
// layoutSections, because we want to truncate the last segment to the end of
|
2020-04-30 22:15:05 -07:00
|
|
|
// its last non-empty section, to match GNU objcopy's behaviour.
|
2017-11-15 19:13:31 +00:00
|
|
|
TotalSize = 0;
|
2020-05-03 21:54:28 -07:00
|
|
|
for (SectionBase &Sec : Obj.allocSections())
|
|
|
|
if (Sec.Type != SHT_NOBITS && Sec.Size > 0) {
|
|
|
|
Sec.Offset = Sec.Addr - MinAddr;
|
2019-09-04 09:35:32 +00:00
|
|
|
TotalSize = std::max(TotalSize, Sec.Offset + Sec.Size);
|
2020-05-03 21:54:28 -07:00
|
|
|
}
|
2018-01-25 22:46:17 +00:00
|
|
|
|
2020-10-24 17:35:55 +03:00
|
|
|
Buf = WritableMemoryBuffer::getNewMemBuffer(TotalSize);
|
|
|
|
if (!Buf)
|
|
|
|
return createStringError(errc::not_enough_memory,
|
|
|
|
"failed to allocate memory buffer of " +
|
|
|
|
Twine::utohexstr(TotalSize) + " bytes");
|
|
|
|
SecWriter = std::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
|
|
|
|
2019-05-29 11:37:16 +00:00
|
|
|
bool IHexWriter::SectionCompare::operator()(const SectionBase *Lhs,
|
|
|
|
const SectionBase *Rhs) const {
|
|
|
|
return (sectionPhysicalAddr(Lhs) & 0xFFFFFFFFU) <
|
|
|
|
(sectionPhysicalAddr(Rhs) & 0xFFFFFFFFU);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t IHexWriter::writeEntryPointRecord(uint8_t *Buf) {
|
|
|
|
IHexLineData HexData;
|
|
|
|
uint8_t Data[4] = {};
|
|
|
|
// We don't write entry point record if entry is zero.
|
|
|
|
if (Obj.Entry == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (Obj.Entry <= 0xFFFFFU) {
|
|
|
|
Data[0] = ((Obj.Entry & 0xF0000U) >> 12) & 0xFF;
|
|
|
|
support::endian::write(&Data[2], static_cast<uint16_t>(Obj.Entry),
|
|
|
|
support::big);
|
|
|
|
HexData = IHexRecord::getLine(IHexRecord::StartAddr80x86, 0, Data);
|
|
|
|
} else {
|
|
|
|
support::endian::write(Data, static_cast<uint32_t>(Obj.Entry),
|
|
|
|
support::big);
|
|
|
|
HexData = IHexRecord::getLine(IHexRecord::StartAddr, 0, Data);
|
|
|
|
}
|
|
|
|
memcpy(Buf, HexData.data(), HexData.size());
|
|
|
|
return HexData.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t IHexWriter::writeEndOfFileRecord(uint8_t *Buf) {
|
|
|
|
IHexLineData HexData = IHexRecord::getLine(IHexRecord::EndOfFile, 0, {});
|
|
|
|
memcpy(Buf, HexData.data(), HexData.size());
|
|
|
|
return HexData.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
Error IHexWriter::write() {
|
2020-10-24 17:35:55 +03:00
|
|
|
IHexSectionWriter Writer(*Buf);
|
2019-05-29 11:37:16 +00:00
|
|
|
// Write sections.
|
|
|
|
for (const SectionBase *Sec : Sections)
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = Sec->accept(Writer))
|
|
|
|
return Err;
|
2019-05-29 11:37:16 +00:00
|
|
|
|
|
|
|
uint64_t Offset = Writer.getBufferOffset();
|
|
|
|
// Write entry point address.
|
2020-10-24 17:35:55 +03:00
|
|
|
Offset += writeEntryPointRecord(
|
|
|
|
reinterpret_cast<uint8_t *>(Buf->getBufferStart()) + Offset);
|
2019-05-29 11:37:16 +00:00
|
|
|
// Write EOF.
|
2020-10-24 17:35:55 +03:00
|
|
|
Offset += writeEndOfFileRecord(
|
|
|
|
reinterpret_cast<uint8_t *>(Buf->getBufferStart()) + Offset);
|
2019-05-29 11:37:16 +00:00
|
|
|
assert(Offset == TotalSize);
|
2020-10-24 17:35:55 +03:00
|
|
|
|
|
|
|
// TODO: Implement direct writing to the output stream (without intermediate
|
|
|
|
// memory buffer Buf).
|
|
|
|
Out.write(Buf->getBufferStart(), Buf->getBufferSize());
|
|
|
|
return Error::success();
|
2019-05-29 11:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Error IHexWriter::checkSection(const SectionBase &Sec) {
|
|
|
|
uint64_t Addr = sectionPhysicalAddr(&Sec);
|
|
|
|
if (addressOverflows32bit(Addr) || addressOverflows32bit(Addr + Sec.Size - 1))
|
|
|
|
return createStringError(
|
|
|
|
errc::invalid_argument,
|
2020-10-06 14:55:45 +03:00
|
|
|
"Section '%s' address range [0x%llx, 0x%llx] is not 32 bit",
|
|
|
|
Sec.Name.c_str(), Addr, Addr + Sec.Size - 1);
|
2019-05-29 11:37:16 +00:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
Error IHexWriter::finalize() {
|
|
|
|
// We can't write 64-bit addresses.
|
|
|
|
if (addressOverflows32bit(Obj.Entry))
|
|
|
|
return createStringError(errc::invalid_argument,
|
2020-10-24 17:35:55 +03:00
|
|
|
"Entry point address 0x%llx overflows 32 bits",
|
2019-05-29 11:37:16 +00:00
|
|
|
Obj.Entry);
|
|
|
|
|
2019-09-04 08:43:27 +00:00
|
|
|
for (const SectionBase &Sec : Obj.sections())
|
2021-06-16 10:08:20 -07:00
|
|
|
if ((Sec.Flags & ELF::SHF_ALLOC) && Sec.Type != ELF::SHT_NOBITS &&
|
|
|
|
Sec.Size > 0) {
|
2019-09-04 08:43:27 +00:00
|
|
|
if (Error E = checkSection(Sec))
|
2019-05-29 11:37:16 +00:00
|
|
|
return E;
|
2019-09-04 08:43:27 +00:00
|
|
|
Sections.insert(&Sec);
|
2019-05-29 11:37:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 17:35:55 +03:00
|
|
|
std::unique_ptr<WritableMemoryBuffer> EmptyBuffer =
|
|
|
|
WritableMemoryBuffer::getNewMemBuffer(0);
|
|
|
|
if (!EmptyBuffer)
|
|
|
|
return createStringError(errc::not_enough_memory,
|
|
|
|
"failed to allocate memory buffer of 0 bytes");
|
|
|
|
|
|
|
|
IHexSectionWriterBase LengthCalc(*EmptyBuffer);
|
2019-05-29 11:37:16 +00:00
|
|
|
for (const SectionBase *Sec : Sections)
|
2020-09-19 18:53:44 +03:00
|
|
|
if (Error Err = Sec->accept(LengthCalc))
|
|
|
|
return Err;
|
2019-05-29 11:37:16 +00:00
|
|
|
|
|
|
|
// We need space to write section records + StartAddress record
|
|
|
|
// (if start adress is not zero) + EndOfFile record.
|
|
|
|
TotalSize = LengthCalc.getBufferOffset() +
|
|
|
|
(Obj.Entry ? IHexRecord::getLineLength(4) : 0) +
|
|
|
|
IHexRecord::getLineLength(0);
|
2020-10-24 17:35:55 +03:00
|
|
|
|
|
|
|
Buf = WritableMemoryBuffer::getNewMemBuffer(TotalSize);
|
|
|
|
if (!Buf)
|
|
|
|
return createStringError(errc::not_enough_memory,
|
|
|
|
"failed to allocate memory buffer of " +
|
|
|
|
Twine::utohexstr(TotalSize) + " bytes");
|
|
|
|
|
2019-05-29 11:37:16 +00:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
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
|