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
7 changes: 5 additions & 2 deletions llvm/include/llvm/Object/Binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Binary {

ID_GOFF,
ID_Wasm,
ID_Binary,

ID_EndObjects
};
Expand Down Expand Up @@ -191,7 +192,8 @@ DEFINE_ISA_CONVERSION_FUNCTIONS(Binary, LLVMBinaryRef)
/// @param Source The data to create the Binary from.
Expected<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
LLVMContext *Context = nullptr,
bool InitContent = true);
bool InitContent = true,
bool RawBinary = false);

template <typename T> class OwningBinary {
std::unique_ptr<T> Bin;
Expand Down Expand Up @@ -243,7 +245,8 @@ template <typename T> const T* OwningBinary<T>::getBinary() const {

Expected<OwningBinary<Binary>> createBinary(StringRef Path,
LLVMContext *Context = nullptr,
bool InitContent = true);
bool InitContent = true,
bool RawBinary = false);

} // end namespace object

Expand Down
130 changes: 130 additions & 0 deletions llvm/include/llvm/Object/BinaryObjectFile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
//===- BinaryObjectFile.h - Binary object file implementation ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file declares the BinaryObjectFile class.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be really helpful to have a summary of what a BinaryObjectFile actually represents/how it represents it.

//
//===----------------------------------------------------------------------===//

#ifndef LLVM_OBJECT_RAWOBJECTFILE_H
#define LLVM_OBJECT_RAWOBJECTFILE_H

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Object/Binary.h"
#include "llvm/Object/Error.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Object/SymbolicFile.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBufferRef.h"
#include "llvm/Support/ScopedPrinter.h"
#include "llvm/TargetParser/SubtargetFeature.h"
#include "llvm/TargetParser/Triple.h"
#include <cassert>
#include <cstdint>

namespace llvm {

template <typename T> class SmallVectorImpl;

namespace object {

struct BinarySymbol {
uint32_t Flags = 0;
uint64_t Value = 0;
StringRef Name;
};

struct BinaryRelocation {
uint64_t Offset = 0;
uint64_t Symbol = 0;
uint64_t Type = 0;
};

struct BinarySection {
uint64_t Offset = 0;
uint64_t Index = 0;
uint64_t Address = 0;
uint64_t Size = 0;
StringRef Name;
std::vector<BinaryRelocation> Relocations;
};

class BinaryObjectFile : public ObjectFile {
private:
std::vector<BinarySymbol> Symbols;
std::vector<BinarySection> Sections;

public:
BinaryObjectFile(MemoryBufferRef Source);

bool is64Bit() const override;

basic_symbol_iterator symbol_begin() const override;
basic_symbol_iterator symbol_end() const override;
section_iterator section_begin() const override;
section_iterator section_end() const override;

const BinarySymbol &getBinarySymbol(const DataRefImpl &Symb) const;
const BinarySymbol &getBinarySymbol(const SymbolRef &Symb) const;

void moveSymbolNext(DataRefImpl &Symb) const override;
Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override;
Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;

const BinarySection &getBinarySection(const DataRefImpl Ref) const;
const BinarySection &getBinarySection(const SectionRef &Section) const;

void moveSectionNext(DataRefImpl &Sec) const override;
Expected<StringRef> getSectionName(DataRefImpl Sec) const override;
uint64_t getSectionAddress(DataRefImpl Sec) const override;
uint64_t getSectionIndex(DataRefImpl Sec) const override;
uint64_t getSectionSize(DataRefImpl Sec) const override;
Expected<ArrayRef<uint8_t>>
getSectionContents(DataRefImpl Sec) const override;
uint64_t getSectionAlignment(DataRefImpl Sec) const override;
bool isSectionCompressed(DataRefImpl Sec) const override;
bool isSectionText(DataRefImpl Sec) const override;
bool isSectionData(DataRefImpl Sec) const override;
bool isSectionBSS(DataRefImpl Sec) const override;
bool isSectionVirtual(DataRefImpl Sec) const override;

relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
relocation_iterator section_rel_end(DataRefImpl Sec) const override;

const BinaryRelocation &getBinaryRelocation(const RelocationRef &Ref) const;
const BinaryRelocation &getBinaryRelocation(DataRefImpl Ref) const;

// Overrides from RelocationRef.
void moveRelocationNext(DataRefImpl &Rel) const override;
uint64_t getRelocationOffset(DataRefImpl Rel) const override;
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
uint64_t getRelocationType(DataRefImpl Rel) const override;
void getRelocationTypeName(DataRefImpl Rel,
SmallVectorImpl<char> &Result) const override;

uint8_t getBytesInAddress() const override;
StringRef getFileFormatName() const override;
Triple::ArchType getArch() const override;
Expected<SubtargetFeatures> getFeatures() const override;
std::optional<StringRef> tryGetCPUName() const override;
bool isRelocatableObject() const override;
};

} // end namespace object
} // end namespace llvm

#endif // LLVM_OBJECT_RAWOBJECTFILE_H
4 changes: 4 additions & 0 deletions llvm/include/llvm/Object/ObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class SectionRef;
class SymbolRef;
class symbol_iterator;
class WasmObjectFile;
class BinaryObjectFile;

using section_iterator = content_iterator<SectionRef>;

Expand Down Expand Up @@ -400,6 +401,9 @@ class ObjectFile : public SymbolicFile {

static Expected<std::unique_ptr<WasmObjectFile>>
createWasmObjectFile(MemoryBufferRef Object);

static Expected<std::unique_ptr<BinaryObjectFile>>
createBinaryObjectFile(MemoryBufferRef Object);
};

/// A filtered iterator for SectionRefs that skips sections based on some given
Expand Down
16 changes: 12 additions & 4 deletions llvm/lib/Object/Binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/BinaryFormat/Magic.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/BinaryObjectFile.h"
#include "llvm/Object/Error.h"
#include "llvm/Object/MachOUniversal.h"
#include "llvm/Object/Minidump.h"
Expand Down Expand Up @@ -44,9 +45,14 @@ MemoryBufferRef Binary::getMemoryBufferRef() const { return Data; }

Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
LLVMContext *Context,
bool InitContent) {
bool InitContent,
bool RawBinary) {
file_magic Type = identify_magic(Buffer.getBuffer());

if (RawBinary) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: no need for braces for single line if.

return ObjectFile::createBinaryObjectFile(Buffer);
}

switch (Type) {
case file_magic::archive:
return Archive::create(Buffer);
Expand Down Expand Up @@ -103,8 +109,10 @@ Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
llvm_unreachable("Unexpected Binary File Type");
}

Expected<OwningBinary<Binary>>
object::createBinary(StringRef Path, LLVMContext *Context, bool InitContent) {
Expected<OwningBinary<Binary>> object::createBinary(StringRef Path,
LLVMContext *Context,
bool InitContent,
bool RawBinary) {
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
MemoryBuffer::getFileOrSTDIN(Path, /*IsText=*/false,
/*RequiresNullTerminator=*/false);
Expand All @@ -113,7 +121,7 @@ object::createBinary(StringRef Path, LLVMContext *Context, bool InitContent) {
std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();

Expected<std::unique_ptr<Binary>> BinOrErr =
createBinary(Buffer->getMemBufferRef(), Context, InitContent);
createBinary(Buffer->getMemBufferRef(), Context, InitContent, RawBinary);
if (!BinOrErr)
return BinOrErr.takeError();
std::unique_ptr<Binary> &Bin = BinOrErr.get();
Expand Down
Loading
Loading