/*
|
/*
|
* Copyright (c) 2016 by Alex I. Kuznetsov.
|
* Copyright (c) 2016 by Alex I. Kuznetsov.
|
*
|
*
|
* Part of the LXP32 CPU IP core.
|
* Part of the LXP32 CPU IP core.
|
*
|
*
|
* This module defines the LinkableObject class which represents
|
* This module defines the LinkableObject class which represents
|
* compiled LXP32 binary code.
|
* compiled LXP32 binary code.
|
*/
|
*/
|
|
|
#ifndef LINKABLEOBJECT_H_INCLUDED
|
#ifndef LINKABLEOBJECT_H_INCLUDED
|
#define LINKABLEOBJECT_H_INCLUDED
|
#define LINKABLEOBJECT_H_INCLUDED
|
|
|
#include <iostream>
|
#include <iostream>
|
#include <vector>
|
#include <vector>
|
#include <map>
|
#include <map>
|
#include <string>
|
#include <string>
|
#include <cstdint>
|
#include <cstdint>
|
|
|
class LinkableObject {
|
class LinkableObject {
|
public:
|
public:
|
typedef unsigned char Byte;
|
typedef unsigned char Byte;
|
typedef std::uint32_t Word;
|
typedef std::uint32_t Word;
|
typedef std::int_least64_t Integer;
|
typedef std::int_least64_t Integer;
|
|
|
enum SymbolType {Unknown,Local,Exported,Imported};
|
enum SymbolType {Unknown,Local,Exported,Imported};
|
enum RefType {Regular,Short};
|
enum RefType {Regular,Short};
|
|
|
struct Reference {
|
struct Reference {
|
std::string source;
|
std::string source;
|
int line;
|
int line;
|
Word rva;
|
Word rva;
|
Integer offset;
|
Integer offset;
|
RefType type;
|
RefType type;
|
};
|
};
|
struct SymbolData {
|
struct SymbolData {
|
SymbolType type=Unknown;
|
SymbolType type=Unknown;
|
Word rva;
|
Word rva;
|
std::vector<Reference> refs;
|
std::vector<Reference> refs;
|
};
|
};
|
|
|
typedef std::map<std::string,SymbolData> SymbolTable;
|
typedef std::map<std::string,SymbolData> SymbolTable;
|
|
|
private:
|
private:
|
std::string _name;
|
std::string _name;
|
std::vector<Byte> _code;
|
std::vector<Byte> _code;
|
SymbolTable _symbols;
|
SymbolTable _symbols;
|
Word _virtualAddress=0;
|
Word _virtualAddress=0;
|
|
|
public:
|
public:
|
std::string name() const;
|
std::string name() const;
|
void setName(const std::string &str);
|
void setName(const std::string &str);
|
|
|
Word virtualAddress() const;
|
Word virtualAddress() const;
|
void setVirtualAddress(Word addr);
|
void setVirtualAddress(Word addr);
|
|
|
Byte *code();
|
Byte *code();
|
const Byte *code() const;
|
const Byte *code() const;
|
std::size_t codeSize() const;
|
std::size_t codeSize() const;
|
|
|
Word addWord(Word w);
|
Word addWord(Word w);
|
Word addByte(Byte b);
|
Word addByte(Byte b);
|
Word addBytes(const Byte *p,std::size_t n);
|
Word addBytes(const Byte *p,std::size_t n);
|
Word addZeros(std::size_t n);
|
Word addZeros(std::size_t n);
|
|
|
Word addPadding(std::size_t size=sizeof(LinkableObject::Word));
|
Word addPadding(std::size_t size=sizeof(LinkableObject::Word));
|
|
|
Word getWord(Word rva) const;
|
Word getWord(Word rva) const;
|
void replaceWord(Word rva,Word value);
|
void replaceWord(Word rva,Word value);
|
|
|
void addSymbol(const std::string &name,Word rva);
|
void addSymbol(const std::string &name,Word rva);
|
void addImportedSymbol(const std::string &name);
|
void addImportedSymbol(const std::string &name);
|
void exportSymbol(const std::string &name);
|
void exportSymbol(const std::string &name);
|
void addReference(const std::string &symbolName,const Reference &ref);
|
void addReference(const std::string &symbolName,const Reference &ref);
|
|
|
SymbolData &symbol(const std::string &name);
|
SymbolData &symbol(const std::string &name);
|
const SymbolData &symbol(const std::string &name) const;
|
const SymbolData &symbol(const std::string &name) const;
|
const SymbolTable &symbols() const;
|
const SymbolTable &symbols() const;
|
|
|
void serialize(const std::string &filename) const;
|
void serialize(const std::string &filename) const;
|
void deserialize(const std::string &filename);
|
void deserialize(const std::string &filename);
|
|
|
private:
|
private:
|
void deserializeCode(std::istream &in);
|
void deserializeCode(std::istream &in);
|
void deserializeSymbol(std::istream &in);
|
void deserializeSymbol(std::istream &in);
|
static std::vector<std::string> tokenize(const std::string &str);
|
static std::vector<std::string> tokenize(const std::string &str);
|
};
|
};
|
|
|
#endif
|
#endif
|
|
|