SectionRef::getContents() now returns Expected

Updated uses of object::SectionRef::getContents() since it now returns
an Expected<StringRef> instead of modifying the one it's passed.

See also: git-svn-id:
https://llvm.org/svn/llvm-project/llvm/trunk@360892
91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Gwen Mittertreiner
2019-05-17 17:56:17 -07:00
committed by Gwen Mittertreiner
parent dff6b6e258
commit 67cfef2d60
5 changed files with 43 additions and 23 deletions

View File

@@ -385,11 +385,13 @@ static bool needsRecompile(StringRef OutputFilename, ArrayRef<uint8_t> HashData,
StringRef SectionName;
Section.getName(SectionName);
if (SectionName == HashSectionName) {
StringRef SectionData;
Section.getContents(SectionData);
llvm::Expected<llvm::StringRef> SectionData = Section.getContents();
if (!SectionData) {
return true;
}
ArrayRef<uint8_t> PrevHashData(
reinterpret_cast<const uint8_t *>(SectionData.data()),
SectionData.size());
reinterpret_cast<const uint8_t *>(SectionData->data()),
SectionData->size());
LLVM_DEBUG(if (PrevHashData.size() == sizeof(MD5::MD5Result)) {
if (DiagMutex) DiagMutex->lock();
SmallString<32> HashStr;

View File

@@ -107,26 +107,38 @@ public:
/// Look inside the object file 'ObjectFile' and append any linker flags found in
/// its ".swift1_autolink_entries" section to 'LinkerFlags'.
static void
/// Return 'true' if there was an error, and 'false' otherwise.
static bool
extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile,
std::vector<std::string> &LinkerFlags) {
std::vector<std::string> &LinkerFlags,
CompilerInstance &Instance) {
// Search for the section we hold autolink entries in
for (auto &Section : ObjectFile->sections()) {
llvm::StringRef SectionName;
Section.getName(SectionName);
if (SectionName == ".swift1_autolink_entries") {
llvm::StringRef SectionData;
Section.getContents(SectionData);
llvm::Expected<llvm::StringRef> SectionData = Section.getContents();
if (!SectionData) {
std::string message;
{
llvm::raw_string_ostream os(message);
logAllUnhandledErrors(SectionData.takeError(), os, "");
}
Instance.getDiags().diagnose(SourceLoc(), diag::error_open_input_file,
ObjectFile->getFileName() , message);
return true;
}
// entries are null-terminated, so extract them and push them into
// the set.
llvm::SmallVector<llvm::StringRef, 4> SplitFlags;
SectionData.split(SplitFlags, llvm::StringRef("\0", 1), -1,
/*KeepEmpty=*/false);
SectionData->split(SplitFlags, llvm::StringRef("\0", 1), -1,
/*KeepEmpty=*/false);
for (const auto &Flag : SplitFlags)
LinkerFlags.push_back(Flag);
}
}
return false;
}
/// Look inside the binary 'Bin' and append any linker flags found in its
@@ -138,12 +150,10 @@ static bool extractLinkerFlags(const llvm::object::Binary *Bin,
StringRef BinaryFileName,
std::vector<std::string> &LinkerFlags) {
if (auto *ObjectFile = llvm::dyn_cast<llvm::object::ELFObjectFileBase>(Bin)) {
extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags);
return false;
return extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags, Instance);
} else if (auto *ObjectFile =
llvm::dyn_cast<llvm::object::COFFObjectFile>(Bin)) {
extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags);
return false;
return extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags, Instance);
} else if (auto *Archive = llvm::dyn_cast<llvm::object::Archive>(Bin)) {
llvm::Error Error = llvm::Error::success();
for (const auto &Child : Archive->children(Error)) {

View File

@@ -169,10 +169,15 @@ collectASTModules(llvm::cl::list<std::string> &InputNames,
(ELF && Name == swift::ELFASTSectionName) ||
(COFF && Name == swift::COFFASTSectionName)) {
uint64_t Size = Section.getSize();
StringRef ContentsReference;
Section.getContents(ContentsReference);
llvm::Expected<llvm::StringRef> ContentsReference = Section.getContents();
if (!ContentsReference) {
llvm::outs() << "error: " << name << " "
<< errorToErrorCode(OF.takeError()).message() << "\n";
return false;
}
char *Module = Alloc.Allocate<char>(Size);
std::memcpy(Module, (void *)ContentsReference.begin(), Size);
std::memcpy(Module, (void *)ContentsReference->begin(), Size);
Modules.push_back({Module, Size});
}
}

View File

@@ -26,6 +26,7 @@
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/Object/MachOUniversal.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Error.h"
#if defined(_WIN32)
#include <io.h>
@@ -137,11 +138,11 @@ public:
for (SectionRef S : O->sections()) {
if (!needToRelocate(S))
continue;
StringRef Content;
if (auto EC = S.getContents(Content))
reportError(EC);
std::memcpy(&Memory[getSectionAddress(S)], Content.data(),
Content.size());
llvm::Expected<llvm::StringRef> Content = S.getContents();
if (!Content)
reportError(errorToErrorCode(Content.takeError()));
std::memcpy(&Memory[getSectionAddress(S)], Content->data(),
Content->size());
}
}

View File

@@ -825,7 +825,9 @@ TEST_F(LexerTest, DiagnoseEmbeddedNulOffset) {
// This test requires mmap because llvm::sys::Memory doesn't support protecting
// pages to have no permissions.
TEST_F(LexerTest, EncodedStringSegmentPastTheEnd) {
size_t PageSize = llvm::sys::Process::getPageSize();
Expected<size_t> ExptPageSize = llvm::sys::Process::getPageSize();
ASSERT_TRUE(bool(ExptPageSize));
size_t PageSize = *ExptPageSize;
void *FirstPage = mmap(/*addr*/nullptr, PageSize * 2, PROT_NONE,
MAP_PRIVATE | MAP_ANON, /*fd*/-1, /*offset*/0);