It broke the Clang build, see llvm-commits thread.
> Expected<ArrayRef<uint8_t>> may be better but use Expected<StringRef> for now.
>
> Follow-up of D61781.
llvm-svn: 360878
uint64_t getAddress() const;
uint64_t getIndex() const;
uint64_t getSize() const;
- Expected<StringRef> getContents() const;
+ std::error_code getContents(StringRef &Result) const;
/// Get the alignment of this section as the actual value (not log 2).
uint64_t getAlignment() const;
return OwningObject->getSectionSize(SectionPimpl);
}
-inline Expected<StringRef> SectionRef::getContents() const {
+inline std::error_code SectionRef::getContents(StringRef &Result) const {
Expected<ArrayRef<uint8_t>> Res =
OwningObject->getSectionContents(SectionPimpl);
if (!Res)
- return Res.takeError();
- return StringRef(reinterpret_cast<const char *>(Res->data()), Res->size());
+ return errorToErrorCode(Res.takeError());
+ Result = StringRef(reinterpret_cast<const char *>(Res->data()), Res->size());
+ return std::error_code();
}
inline uint64_t SectionRef::getAlignment() const {
// Try to obtain an already relocated version of this section.
// Else use the unrelocated section from the object file. We'll have to
// apply relocations ourselves later.
- if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data)) {
- Expected<StringRef> E = Section.getContents();
- if (E)
- Data = *E;
- else
- // maybeDecompress below will error.
- consumeError(E.takeError());
- }
+ if (!L || !L->getLoadedSectionContents(*RelocatedSection, Data))
+ Section.getContents(Data);
if (auto Err = maybeDecompress(Section, Name, Data)) {
ErrorPolicy EP = HandleError(createError(
if (Obj->getArch() == Triple::ppc64) {
for (section_iterator Section : Obj->sections()) {
StringRef Name;
+ StringRef Data;
if (auto EC = Section->getName(Name))
return EC;
if (Name == ".opd") {
- Expected<StringRef> E = Section->getContents();
- if (!E)
- return errorToErrorCode(E.takeError());
- OpdExtractor.reset(new DataExtractor(*E, Obj->isLittleEndian(),
+ if (auto EC = Section->getContents(Data))
+ return EC;
+ OpdExtractor.reset(new DataExtractor(Data, Obj->isLittleEndian(),
Obj->getBytesInAddress()));
OpdAddress = Section->getAddress();
break;
Section.getName(Name);
Name = Name.substr(Name.find_first_not_of("._"));
if (Name == "gnu_debuglink") {
- Expected<StringRef> ContentsOrErr = Section.getContents();
- if (!ContentsOrErr) {
- consumeError(ContentsOrErr.takeError());
- return false;
- }
- DataExtractor DE(*ContentsOrErr, Obj->isLittleEndian(), 0);
+ StringRef Data;
+ Section.getContents(Data);
+ DataExtractor DE(Data, Obj->isLittleEndian(), 0);
uint32_t Offset = 0;
if (const char *DebugNameStr = DE.getCStr(&Offset)) {
// 4-byte align the offset.
if (!SecRef.isVirtual()) {
// If this section has content then record it.
- Expected<StringRef> Content = SecRef.getContents();
- if (!Content)
- return Content.takeError();
- if (Content->size() != SecRef.getSize())
+ StringRef Content;
+ if (auto EC = SecRef.getContents(Content))
+ return errorCodeToError(EC);
+ if (Content.size() != SecRef.getSize())
return make_error<JITLinkError>("Section content size does not match "
"declared size for " +
Name);
- MachOSec.setContent(*Content);
+ MachOSec.setContent(Content);
} else {
// If this is a zero-fill section then just record the size.
MachOSec.setZeroFill(SecRef.getSize());
if (!IsVirtual && !IsZeroInit) {
// In either case, set the location of the unrelocated section in memory,
// since we still process relocations for it even if we're not applying them.
- if (Expected<StringRef> E = Section.getContents())
- data = *E;
- else
- return E.takeError();
+ if (auto EC = Section.getContents(data))
+ return errorCodeToError(EC);
pData = data.data();
}
}
if (!Plt || !RelaPlt || !GotPlt)
return {};
- Expected<StringRef> PltContents = Plt->getContents();
- if (!PltContents) {
- consumeError(PltContents.takeError());
+ StringRef PltContents;
+ if (Plt->getContents(PltContents))
return {};
- }
- auto PltEntries = MIA->findPltEntries(Plt->getAddress(),
- arrayRefFromStringRef(*PltContents),
+ ArrayRef<uint8_t> PltBytes((const uint8_t *)PltContents.data(),
+ Plt->getSize());
+ auto PltEntries = MIA->findPltEntries(Plt->getAddress(), PltBytes,
GotPlt->getAddress(), Triple);
// Build a map from GOT entry virtual address to PLT entry virtual address.
DenseMap<uint64_t, uint64_t> GotToPlt;
IRObjectFile::findBitcodeInObject(const ObjectFile &Obj) {
for (const SectionRef &Sec : Obj.sections()) {
if (Sec.isBitcode()) {
- Expected<StringRef> Contents = Sec.getContents();
- if (!Contents)
- return Contents.takeError();
- if (Contents->size() <= 1)
+ StringRef SecContents;
+ if (std::error_code EC = Sec.getContents(SecContents))
+ return errorCodeToError(EC);
+ if (SecContents.size() <= 1)
return errorCodeToError(object_error::bitcode_section_not_found);
- return MemoryBufferRef(*Contents, Obj.getFileName());
+ return MemoryBufferRef(SecContents, Obj.getFileName());
}
}
}
const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
- if (Expected<StringRef> E = (*unwrap(SI))->getContents())
- return E->data();
- else
- report_fatal_error(E.takeError());
+ StringRef ret;
+ if (std::error_code ec = (*unwrap(SI))->getContents(ret))
+ report_fatal_error(ec.message());
+ return ret.data();
}
uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
}
Error InstrProfSymtab::create(SectionRef &Section) {
- Expected<StringRef> DataOrErr = Section.getContents();
- if (!DataOrErr)
- return DataOrErr.takeError();
- Data = *DataOrErr;
+ if (auto EC = Section.getContents(Data))
+ return errorCodeToError(EC);
Address = Section.getAddress();
// If this is a linked PE/COFF file, then we have to skip over the null byte
return E;
// Get the contents of the given sections.
- if (Expected<StringRef> E = CoverageSection->getContents())
- CoverageMapping = *E;
- else
- return E.takeError();
-
+ if (auto EC = CoverageSection->getContents(CoverageMapping))
+ return errorCodeToError(EC);
if (Error E = ProfileNames.create(*NamesSection))
return E;
"Failed to find XRay instrumentation map.",
std::make_error_code(std::errc::executable_format_error));
- if (Expected<StringRef> E = I->getContents())
- Contents = *E;
- else
- return E.takeError();
+ if (I->getContents(Contents))
+ return errorCodeToError(
+ std::make_error_code(std::errc::executable_format_error));
RelocMap Relocs;
if (ObjFile.getBinary()->isELF()) {
void DwarfLinker::RelocationManager::findValidRelocsMachO(
const object::SectionRef &Section, const object::MachOObjectFile &Obj,
const DebugMapObject &DMO) {
- Expected<StringRef> ContentsOrErr = Section.getContents();
- if (!ContentsOrErr) {
- consumeError(ContentsOrErr.takeError());
- Linker.reportWarning("error reading section", DMO);
- return;
- }
- DataExtractor Data(*ContentsOrErr, Obj.isLittleEndian(), 0);
+ StringRef Contents;
+ Section.getContents(Contents);
+ DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
bool SkipNext = false;
for (const object::RelocationRef &Reloc : Section.relocations()) {
static void emitSectionContents(const object::ObjectFile &Obj,
StringRef SecName, MCStreamer *MS) {
- if (auto Sec = getSectionByName(Obj, SecName)) {
- if (Expected<StringRef> E = Sec->getContents())
- MS->EmitBytes(*E);
- else
- consumeError(E.takeError());
- }
+ StringRef Contents;
+ if (auto Sec = getSectionByName(Obj, SecName))
+ if (!Sec->getContents(Contents))
+ MS->EmitBytes(Contents);
}
void DwarfStreamer::copyInvariantDebugSection(const object::ObjectFile &Obj) {
if (!Section.getName(SectionName) && SectionName == ".plt")
continue;
- Expected<StringRef> Contents = Section.getContents();
- if (!Contents)
- return Contents.takeError();
- ArrayRef<uint8_t> SectionBytes = arrayRefFromStringRef(*Contents);
+ StringRef SectionContents;
+ if (Section.getContents(SectionContents))
+ return make_error<StringError>("Failed to retrieve section contents",
+ inconvertibleErrorCode());
+ ArrayRef<uint8_t> SectionBytes((const uint8_t *)SectionContents.data(),
+ Section.getSize());
parseSectionContents(SectionBytes,
{Section.getAddress(), Section.getIndex()});
}
uint64_t ProfileNamesAddress = ProfileNames.getAddress();
StringRef CoverageMappingData;
StringRef ProfileNamesData;
- if (Expected<StringRef> E = CoverageMapping.getContents())
- CoverageMappingData = *E;
- else {
- consumeError(E.takeError());
+ if (CoverageMapping.getContents(CoverageMappingData) ||
+ ProfileNames.getContents(ProfileNamesData))
return 1;
- }
- if (Expected<StringRef> E = ProfileNames.getContents())
- ProfileNamesData = *E;
- else {
- consumeError(E.takeError());
- return 1;
- }
int FD;
if (auto Err = sys::fs::openFileForWrite(OutputFilename, FD)) {
exit(1);
}
-LLVM_ATTRIBUTE_NORETURN static void error(Error Err) {
+static void error(Error Err) {
+ if (!Err)
+ return;
logAllUnhandledErrors(std::move(Err), WithColor::error(outs()),
"reading file: ");
outs().flush();
exit(1);
}
-template <typename T>
-T unwrapOrError(Expected<T> EO) {
- if (!EO)
- error(EO.takeError());
- return std::move(*EO);
-}
-
} // namespace llvm
static void reportError(StringRef Input, StringRef Message) {
// Skip virtual or BSS sections.
if (Sec.isBSS() || Sec.isVirtual())
continue;
- StringRef SecContents = unwrapOrError(Sec.getContents());
+ StringRef SecContents;
+ error(Sec.getContents(SecContents));
Expected<uint64_t> SymAddressOrErr = Sym.getAddress();
error(errorToErrorCode(SymAddressOrErr.takeError()));
uint64_t SymAddress = *SymAddressOrErr;
else
reportError(Arc->getFileName(), cxxdump_error::unrecognized_file_format);
}
- if (Err)
- error(std::move(Err));
+ error(std::move(Err));
}
static void dumpInput(StringRef File) {
if (std::error_code Err = Section.getName(Name))
return errorCodeToError(Err);
- Expected<StringRef> ContentsOrErr = Section.getContents();
- if (!ContentsOrErr)
- return ContentsOrErr.takeError();
- StringRef Contents = *ContentsOrErr;
+ StringRef Contents;
+ if (auto Err = Section.getContents(Contents))
+ return errorCodeToError(Err);
if (auto Err = handleCompressedSection(UncompressedSections, Name, Contents))
return Err;
section_type = Sec.flags & MachO::SECTION_TYPE;
}
- StringRef BytesStr = unwrapOrError(Sect->getContents(), O->getFileName());
-
+ StringRef BytesStr;
+ Sect->getContents(BytesStr);
const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
switch (section_type) {
}
uint32_t section_type = section_flags & MachO::SECTION_TYPE;
- StringRef BytesStr =
- unwrapOrError(Section.getContents(), O->getFileName());
+ StringRef BytesStr;
+ Section.getContents(BytesStr);
const char *sect = reinterpret_cast<const char *>(BytesStr.data());
uint32_t sect_size = BytesStr.size();
uint64_t sect_addr = Section.getAddress();
if (SegName == "__TEXT" && SectName == "__info_plist") {
if (!NoLeadingHeaders)
outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
- StringRef BytesStr =
- unwrapOrError(Section.getContents(), O->getFileName());
+ StringRef BytesStr;
+ Section.getContents(BytesStr);
const char *sect = reinterpret_cast<const char *>(BytesStr.data());
outs() << format("%.*s", BytesStr.size(), sect) << "\n";
return;
S = (*(info->Sections))[SectIdx];
offset = Address - SectAddress;
left = SectSize - offset;
- StringRef SectContents = unwrapOrError(
- ((*(info->Sections))[SectIdx]).getContents(), info->O->getFileName());
+ StringRef SectContents;
+ ((*(info->Sections))[SectIdx]).getContents(SectContents);
return SectContents.data() + offset;
}
}
StringRef SegName = O->getSectionFinalSegmentName(Ref);
outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
- StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName());
+ StringRef BytesStr;
+ S.getContents(BytesStr);
const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint64_t)) {
StringRef SegName = O->getSectionFinalSegmentName(Ref);
outs() << "Contents of (" << SegName << "," << SectName << ") section\n";
- StringRef BytesStr = unwrapOrError(S.getContents(), O->getFileName());
+ StringRef BytesStr;
+ S.getContents(BytesStr);
const char *Contents = reinterpret_cast<const char *>(BytesStr.data());
for (uint32_t i = 0; i < S.getSize(); i += sizeof(uint32_t)) {
if (SegmentName != DisSegName)
continue;
- StringRef BytesStr =
- unwrapOrError(Sections[SectIdx].getContents(), Filename);
+ StringRef BytesStr;
+ Sections[SectIdx].getContents(BytesStr);
ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(BytesStr);
uint64_t SectAddress = Sections[SectIdx].getAddress();
uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
- StringRef Contents =
- unwrapOrError(CompactUnwind.getContents(), Obj->getFileName());
+ StringRef Contents;
+ CompactUnwind.getContents(Contents);
+
SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
// First populate the initial raw offsets, encodings and so on from the entry.
outs() << "Contents of __unwind_info section:\n";
- StringRef Contents =
- unwrapOrError(UnwindInfo.getContents(), Obj->getFileName());
+ StringRef Contents;
+ UnwindInfo.getContents(Contents);
ptrdiff_t Pos = 0;
//===----------------------------------
SmallString<40> Comments;
raw_svector_ostream CommentStream(Comments);
- ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(
- unwrapOrError(Section.getContents(), Obj->getFileName()));
+ StringRef BytesStr;
+ error(Section.getContents(BytesStr));
+ ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(BytesStr);
uint64_t VMAAdjustment = 0;
if (shouldAdjustVA(Section))
void printSectionContents(const ObjectFile *Obj) {
for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
StringRef Name;
+ StringRef Contents;
error(Section.getName(Name));
uint64_t BaseAddr = Section.getAddress();
uint64_t Size = Section.getSize();
continue;
}
- StringRef Contents = unwrapOrError(Section.getContents(), Obj->getFileName());
+ error(Section.getContents(Contents));
// Dump out the content as hex and printable ascii characters.
for (std::size_t Addr = 0, End = Contents.size(); Addr < End; Addr += 16) {
if (!ClangASTSection)
return;
- StringRef ClangASTContents = unwrapOrError(
- ClangASTSection.getValue().getContents(), Obj->getFileName());
+ StringRef ClangASTContents;
+ error(ClangASTSection.getValue().getContents(ClangASTContents));
outs().write(ClangASTContents.data(), ClangASTContents.size());
}
return;
}
- StringRef FaultMapContents =
- unwrapOrError(FaultMapSection.getValue().getContents(), Obj->getFileName());
+ StringRef FaultMapContents;
+ error(FaultMapSection.getValue().getContents(FaultMapContents));
+
FaultMapParser FMP(FaultMapContents.bytes_begin(),
FaultMapContents.bytes_end());
else
continue;
- Expected<StringRef> ContentsOrErr = S.getContents();
- if (!ContentsOrErr)
- return ContentsOrErr.takeError();
+ StringRef Contents;
+ if (auto EC = S.getContents(Contents))
+ return errorCodeToError(EC);
uint32_t Magic;
- BinaryStreamReader Reader(*ContentsOrErr, llvm::support::little);
+ BinaryStreamReader Reader(Contents, llvm::support::little);
if (auto EC = Reader.readInteger(Magic))
return EC;
if (Magic != COFF::DEBUG_SECTION_MAGIC)
static inline bool isCodeViewDebugSubsection(object::SectionRef Section,
StringRef Name,
BinaryStreamReader &Reader) {
- StringRef SectionName;
+ StringRef SectionName, Contents;
if (Section.getName(SectionName))
return false;
if (SectionName != Name)
return false;
- Expected<StringRef> ContentsOrErr = Section.getContents();
- if (!ContentsOrErr) {
- consumeError(ContentsOrErr.takeError());
+ if (Section.getContents(Contents))
return false;
- }
- Reader = BinaryStreamReader(*ContentsOrErr, support::little);
+ Reader = BinaryStreamReader(Contents, support::little);
uint32_t Magic;
if (Reader.bytesRemaining() < sizeof(uint32_t))
return false;
void COFFDumper::printCodeViewSymbolSection(StringRef SectionName,
const SectionRef &Section) {
- StringRef SectionContents = unwrapOrError(Section.getContents());
+ StringRef SectionContents;
+ error(Section.getContents(SectionContents));
StringRef Data = SectionContents;
SmallVector<StringRef, 10> FunctionNames;
StringRef SectionName;
error(S.getName(SectionName));
if (SectionName == ".debug$T") {
- StringRef Data = unwrapOrError(S.getContents());
+ StringRef Data;
+ error(S.getContents(Data));
uint32_t Magic;
error(consume(Data, Magic));
if (Magic != 4)
ListScope D(W, "CodeViewTypes");
W.printNumber("Section", SectionName, Obj->getSectionID(Section));
- StringRef Data = unwrapOrError(Section.getContents());
+ StringRef Data;
+ error(Section.getContents(Data));
if (opts::CodeViewSubsectionBytes)
W.printBinaryBlock("Data", Data);
if (opts::SectionData &&
!(Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)) {
- StringRef Data = unwrapOrError(Sec.getContents());
+ StringRef Data;
+ error(Sec.getContents(Data));
+
W.printBinaryBlock("SectionData", Data);
}
}
void COFFDumper::printCOFFDirectives() {
for (const SectionRef &Section : Obj->sections()) {
+ StringRef Contents;
StringRef Name;
error(Section.getName(Name));
if (Name != ".drectve")
continue;
- StringRef Contents = unwrapOrError(Section.getContents());
+ error(Section.getContents(Contents));
+
W.printString("Directive(s)", Contents);
}
}
if (!Name.startswith(".rsrc"))
continue;
- StringRef Ref = unwrapOrError(S.getContents());
+ StringRef Ref;
+ error(S.getContents(Ref));
if ((Name == ".rsrc") || (Name == ".rsrc$01")) {
ResourceSectionRef RSF(Ref);
if (StackMapSection == object::SectionRef())
return;
- StringRef StackMapContents = unwrapOrError(StackMapSection.getContents());
+ StringRef StackMapContents;
+ StackMapSection.getContents(StackMapContents);
ArrayRef<uint8_t> StackMapContentsArray =
arrayRefFromStringRef(StackMapContents);
if (AddrsigSection == object::SectionRef())
return;
- StringRef AddrsigContents = unwrapOrError(AddrsigSection.getContents());
+ StringRef AddrsigContents;
+ AddrsigSection.getContents(AddrsigContents);
ArrayRef<uint8_t> AddrsigContentsArray(AddrsigContents.bytes_begin(),
AddrsigContents.size());
}
}
- if (opts::SectionData && !Section.isBSS())
- W.printBinaryBlock("SectionData", unwrapOrError(Section.getContents()));
+ if (opts::SectionData) {
+ bool IsBSS = Section.isBSS();
+ if (!IsBSS) {
+ StringRef Data;
+ error(Section.getContents(Data));
+
+ W.printBinaryBlock("SectionData", Data);
+ }
+ }
}
}
if (StackMapSection == object::SectionRef())
return;
- StringRef StackMapContents = unwrapOrError(StackMapSection.getContents());
+ StringRef StackMapContents;
+ StackMapSection.getContents(StackMapContents);
ArrayRef<uint8_t> StackMapContentsArray =
arrayRefFromStringRef(StackMapContents);
error(E);
W.startLine() << "String dump of section '" << SectionName << "':\n";
- StringRef SectionContent = unwrapOrError(Section.getContents());
+ StringRef SectionContent;
+ Section.getContents(SectionContent);
const uint8_t *SecContent = SectionContent.bytes_begin();
const uint8_t *CurrentWord = SecContent;
error(E);
W.startLine() << "Hex dump of section '" << SectionName << "':\n";
- StringRef SectionContent = unwrapOrError(Section.getContents());
+ StringRef SectionContent;
+ Section.getContents(SectionContent);
const uint8_t *SecContent = SectionContent.bytes_begin();
const uint8_t *SecEnd = SecContent + SectionContent.size();
if (!SectSize)
continue;
- Expected<StringRef> BytesStr = Section.getContents();
- failIfError(BytesStr);
- ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(*BytesStr);
+ StringRef BytesStr;
+ failIfError(Section.getContents(BytesStr));
+ ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(BytesStr);
for (uint64_t Index = 0, Size = 0; Index < Section.getSize();
Index += Size) {