[Support] Add a DataExtractor constructor that takes ArrayRef<uint8_t>

The new constructor can simplify some llvm-readobj call sites.

Reviewed By: grimar, dblaikie

Differential Revision: https://reviews.llvm.org/D67797

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@372473 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Fangrui Song 2019-09-21 15:05:03 +00:00
parent b014fe6032
commit b3a6782005
3 changed files with 16 additions and 21 deletions

View File

@ -82,6 +82,11 @@ public:
/// valid. /// valid.
DataExtractor(StringRef Data, bool IsLittleEndian, uint8_t AddressSize) DataExtractor(StringRef Data, bool IsLittleEndian, uint8_t AddressSize)
: Data(Data), IsLittleEndian(IsLittleEndian), AddressSize(AddressSize) {} : Data(Data), IsLittleEndian(IsLittleEndian), AddressSize(AddressSize) {}
DataExtractor(ArrayRef<uint8_t> Data, bool IsLittleEndian,
uint8_t AddressSize)
: Data(StringRef(reinterpret_cast<const char *>(Data.data()),
Data.size())),
IsLittleEndian(IsLittleEndian), AddressSize(AddressSize) {}
/// Get the data pointed to by this extractor. /// Get the data pointed to by this extractor.
StringRef getData() const { return Data; } StringRef getData() const { return Data; }

View File

@ -114,9 +114,7 @@ void PrinterContext<ELFT>::printEHFrameHdr(uint64_t EHFrameHdrOffset,
W.printString("Corresponding Section", *SectionName); W.printString("Corresponding Section", *SectionName);
} }
DataExtractor DE( DataExtractor DE(makeArrayRef(Obj->base() + EHFrameHdrOffset, EHFrameHdrSize),
StringRef(reinterpret_cast<const char *>(Obj->base()) + EHFrameHdrOffset,
EHFrameHdrSize),
ELFT::TargetEndianness == support::endianness::little, ELFT::TargetEndianness == support::endianness::little,
ELFT::Is64Bits ? 8 : 4); ELFT::Is64Bits ? 8 : 4);

View File

@ -4639,10 +4639,9 @@ void GNUStyle<ELFT>::printNotes(const ELFFile<ELFT> *Obj) {
OS << " " << N.Type << ":\n " << N.Value << '\n'; OS << " " << N.Type << ":\n " << N.Value << '\n';
} else if (Name == "CORE") { } else if (Name == "CORE") {
if (Type == ELF::NT_FILE) { if (Type == ELF::NT_FILE) {
DataExtractor DescExtractor( DataExtractor DescExtractor(Descriptor,
StringRef(reinterpret_cast<const char *>(Descriptor.data()), ELFT::TargetEndianness == support::little,
Descriptor.size()), sizeof(Elf_Addr));
ELFT::TargetEndianness == support::little, sizeof(Elf_Addr));
Expected<CoreNote> Note = readCoreNote(DescExtractor); Expected<CoreNote> Note = readCoreNote(DescExtractor);
if (Note) if (Note)
printCoreNote<ELFT>(OS, *Note); printCoreNote<ELFT>(OS, *Note);
@ -4836,10 +4835,7 @@ void DumpStyle<ELFT>::printNonRelocatableStackSizes(
const Elf_Shdr *ElfSec = Obj->getSection(Sec.getRawDataRefImpl()); const Elf_Shdr *ElfSec = Obj->getSection(Sec.getRawDataRefImpl());
ArrayRef<uint8_t> Contents = ArrayRef<uint8_t> Contents =
unwrapOrError(this->FileName, EF->getSectionContents(ElfSec)); unwrapOrError(this->FileName, EF->getSectionContents(ElfSec));
DataExtractor Data( DataExtractor Data(Contents, Obj->isLittleEndian(), sizeof(Elf_Addr));
StringRef(reinterpret_cast<const char *>(Contents.data()),
Contents.size()),
Obj->isLittleEndian(), sizeof(Elf_Addr));
// A .stack_sizes section header's sh_link field is supposed to point // A .stack_sizes section header's sh_link field is supposed to point
// to the section that contains the functions whose stack sizes are // to the section that contains the functions whose stack sizes are
// described in it. // described in it.
@ -4937,10 +4933,7 @@ void DumpStyle<ELFT>::printRelocatableStackSizes(
RelocationResolver Resolver; RelocationResolver Resolver;
std::tie(IsSupportedFn, Resolver) = getRelocationResolver(*Obj); std::tie(IsSupportedFn, Resolver) = getRelocationResolver(*Obj);
auto Contents = unwrapOrError(this->FileName, StackSizesSec.getContents()); auto Contents = unwrapOrError(this->FileName, StackSizesSec.getContents());
DataExtractor Data( DataExtractor Data(Contents, Obj->isLittleEndian(), sizeof(Elf_Addr));
StringRef(reinterpret_cast<const char *>(Contents.data()),
Contents.size()),
Obj->isLittleEndian(), sizeof(Elf_Addr));
for (const RelocationRef &Reloc : RelocSec.relocations()) { for (const RelocationRef &Reloc : RelocSec.relocations()) {
if (!IsSupportedFn(Reloc.getType())) if (!IsSupportedFn(Reloc.getType()))
reportError(createStringError( reportError(createStringError(
@ -5831,10 +5824,9 @@ void LLVMStyle<ELFT>::printNotes(const ELFFile<ELFT> *Obj) {
W.printString(N.Type, N.Value); W.printString(N.Type, N.Value);
} else if (Name == "CORE") { } else if (Name == "CORE") {
if (Type == ELF::NT_FILE) { if (Type == ELF::NT_FILE) {
DataExtractor DescExtractor( DataExtractor DescExtractor(Descriptor,
StringRef(reinterpret_cast<const char *>(Descriptor.data()), ELFT::TargetEndianness == support::little,
Descriptor.size()), sizeof(Elf_Addr));
ELFT::TargetEndianness == support::little, sizeof(Elf_Addr));
Expected<CoreNote> Note = readCoreNote(DescExtractor); Expected<CoreNote> Note = readCoreNote(DescExtractor);
if (Note) if (Note)
printCoreNoteLLVMStyle(*Note, W); printCoreNoteLLVMStyle(*Note, W);