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; StringRef SectionName;
Section.getName(SectionName); Section.getName(SectionName);
if (SectionName == HashSectionName) { if (SectionName == HashSectionName) {
StringRef SectionData; llvm::Expected<llvm::StringRef> SectionData = Section.getContents();
Section.getContents(SectionData); if (!SectionData) {
return true;
}
ArrayRef<uint8_t> PrevHashData( ArrayRef<uint8_t> PrevHashData(
reinterpret_cast<const uint8_t *>(SectionData.data()), reinterpret_cast<const uint8_t *>(SectionData->data()),
SectionData.size()); SectionData->size());
LLVM_DEBUG(if (PrevHashData.size() == sizeof(MD5::MD5Result)) { LLVM_DEBUG(if (PrevHashData.size() == sizeof(MD5::MD5Result)) {
if (DiagMutex) DiagMutex->lock(); if (DiagMutex) DiagMutex->lock();
SmallString<32> HashStr; SmallString<32> HashStr;

View File

@@ -107,26 +107,38 @@ public:
/// Look inside the object file 'ObjectFile' and append any linker flags found in /// Look inside the object file 'ObjectFile' and append any linker flags found in
/// its ".swift1_autolink_entries" section to 'LinkerFlags'. /// 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, 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 // Search for the section we hold autolink entries in
for (auto &Section : ObjectFile->sections()) { for (auto &Section : ObjectFile->sections()) {
llvm::StringRef SectionName; llvm::StringRef SectionName;
Section.getName(SectionName); Section.getName(SectionName);
if (SectionName == ".swift1_autolink_entries") { if (SectionName == ".swift1_autolink_entries") {
llvm::StringRef SectionData; llvm::Expected<llvm::StringRef> SectionData = Section.getContents();
Section.getContents(SectionData); 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 // entries are null-terminated, so extract them and push them into
// the set. // the set.
llvm::SmallVector<llvm::StringRef, 4> SplitFlags; llvm::SmallVector<llvm::StringRef, 4> SplitFlags;
SectionData.split(SplitFlags, llvm::StringRef("\0", 1), -1, SectionData->split(SplitFlags, llvm::StringRef("\0", 1), -1,
/*KeepEmpty=*/false); /*KeepEmpty=*/false);
for (const auto &Flag : SplitFlags) for (const auto &Flag : SplitFlags)
LinkerFlags.push_back(Flag); LinkerFlags.push_back(Flag);
} }
} }
return false;
} }
/// Look inside the binary 'Bin' and append any linker flags found in its /// 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, StringRef BinaryFileName,
std::vector<std::string> &LinkerFlags) { std::vector<std::string> &LinkerFlags) {
if (auto *ObjectFile = llvm::dyn_cast<llvm::object::ELFObjectFileBase>(Bin)) { if (auto *ObjectFile = llvm::dyn_cast<llvm::object::ELFObjectFileBase>(Bin)) {
extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags); return extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags, Instance);
return false;
} else if (auto *ObjectFile = } else if (auto *ObjectFile =
llvm::dyn_cast<llvm::object::COFFObjectFile>(Bin)) { llvm::dyn_cast<llvm::object::COFFObjectFile>(Bin)) {
extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags); return extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags, Instance);
return false;
} else if (auto *Archive = llvm::dyn_cast<llvm::object::Archive>(Bin)) { } else if (auto *Archive = llvm::dyn_cast<llvm::object::Archive>(Bin)) {
llvm::Error Error = llvm::Error::success(); llvm::Error Error = llvm::Error::success();
for (const auto &Child : Archive->children(Error)) { for (const auto &Child : Archive->children(Error)) {

View File

@@ -169,10 +169,15 @@ collectASTModules(llvm::cl::list<std::string> &InputNames,
(ELF && Name == swift::ELFASTSectionName) || (ELF && Name == swift::ELFASTSectionName) ||
(COFF && Name == swift::COFFASTSectionName)) { (COFF && Name == swift::COFFASTSectionName)) {
uint64_t Size = Section.getSize(); 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); 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}); Modules.push_back({Module, Size});
} }
} }

View File

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

View File

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