Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ include_directories(${LLVM_INCLUDE_DIRS})
# Explicitly select components to link against
llvm_map_components_to_libnames(LLVM_LINK_LIBRARIES
support
object)
object
irreader)

# LLVM compiler flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fno-rtti -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS")
Expand Down
1 change: 1 addition & 0 deletions lib/include/ebc/BitcodeContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class BitcodeContainer {
protected:
void SetData(const char* data, std::size_t size) noexcept;
std::pair<const char*, std::size_t> GetData() const;
int verifyBC(std::string filename) const;

private:
std::vector<std::size_t> GetEmbeddedFileOffsets() const;
Expand Down
1 change: 1 addition & 0 deletions lib/include/ebc/util/Bitcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ BitcodeType GetBitcodeType(std::string file);
/// @param size Bitcode binary data size.
/// @param fileName The desired filename for the bitcode file.
void WriteToFile(const char *data, std::size_t size, std::string file);
void AppendToFile(const char *data, std::size_t size, std::string filename);
}
}
}
56 changes: 54 additions & 2 deletions lib/src/BitcodeContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
#include "ebc/util/Bitcode.h"
#include "ebc/util/UUID.h"

#include "llvm/IR/Module.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/DiagnosticPrinter.h"

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <streambuf>

using namespace llvm;
namespace ebc {

BitcodeContainer::BitcodeContainer(const char *data, std::size_t size)
Expand Down Expand Up @@ -87,7 +94,21 @@ std::vector<std::unique_ptr<EmbeddedFile>> BitcodeContainer::GetEmbeddedFiles()
std::size_t size = end - begin;

auto fileName = _prefix + util::uuid::UuidToString(util::uuid::GenerateUUID());
util::bitcode::WriteToFile(_data + begin, size, fileName);

fileName = fileName + ".bc";
util::bitcode::WriteToFile(_data + begin, size, fileName); // 此处需要处理末尾的00

if (verifyBC(fileName) != 0) {
util::bitcode::WriteToFile(_data + begin, size - 4, fileName);

if (verifyBC(fileName) != 0) {
const char padding[4] = {0, 0, 0, 0};
util::bitcode::AppendToFile(padding, 4, fileName);
if (verifyBC(fileName) != 0) {
std::cerr << "eroor" << std::endl;
}
}
}

auto embeddedFile = EmbeddedFileFactory::CreateEmbeddedFile(fileName);
embeddedFile->SetCommands(_commands, EmbeddedFile::CommandSource::Clang);
Expand Down Expand Up @@ -116,4 +137,35 @@ void BitcodeContainer::SetPrefix(std::string prefix) {
_prefix = std::move(prefix);
}

} // namespace ebc
static void DiagnosticHandler(const DiagnosticInfo &DI, void *Context) {
bool *HasError = static_cast<bool *>(Context);
if (DI.getSeverity() == DS_Error)
*HasError = true;

DiagnosticPrinterRawOStream DP(errs());
errs() << LLVMContext::getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
DI.print(DP);
errs() << "\n";
}

int BitcodeContainer::verifyBC(std::string filename) const {
LLVMContext Context;
Context.setDiscardValueNames(false);
// Set a diagnostic handler that doesn't exit on the first error
bool HasError = false;
Context.setDiagnosticHandler(DiagnosticHandler, &HasError);

//input module
SMDiagnostic Err;
std::unique_ptr<Module> theOrginal = parseIRFile(filename, Err, Context);
if (!theOrginal) {
std ::cerr << filename <<std::endl;
std::cerr << "can not parse bitcode!" << Err.getMessage().str() <<"\n";

return 1;
}

return 0;
}

} // namespace ebc
12 changes: 12 additions & 0 deletions lib/src/util/Bitcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ void WriteToFile(const char *data, std::size_t size, std::string file) {
output.close();
}

void AppendToFile(const char *data, std::size_t size, std::string filename) {
std::ofstream file;
file.open(filename, std::ios::app);
if (file.bad())
{
std::cerr << "can not open file" << std::endl;
}

file.write(data, static_cast<std::streamsize>(size));
file.close();
}

} // namespace bitcode
} // namespace util
} // namespace ebc