Documentation ¶
Index ¶
- Variables
- func AssembleExecutable(inputFile io.Reader, outputFile io.Writer) error
- func AssembleObjectFile(inputFile io.Reader, outputFile io.Writer) error
- func AssembleStatement(s *parser.Statement, state TraversalState) ([]arch.Instruction, error)
- func CalculateStatementSize(statement *parser.Statement) int
- func IsDataDirective(kind lexer.TokenKind) bool
- type Layout
- func (l *Layout) Assemble(...) error
- func (l *Layout) InitWithStatements(statements []*parser.Statement) error
- func (l *Layout) LocateLabel(label string) (uint32, bool)
- func (l *Layout) LocateLabelSection(label string) (*Section, bool)
- func (l *Layout) LocateStatement(statement *parser.Statement) (int, bool)
- func (l *Layout) LocateStatementWithinSection(statement *parser.Statement) (int, *Section, bool)
- func (l *Layout) SectionByName(name string) *Section
- func (l *Layout) Traverse(traversalFunc func(section *Section) error) error
- type ObjectFile
- type Section
- type TraversalState
Constants ¶
This section is empty.
Variables ¶
var (
ErrInvalidOpcode = fmt.Errorf("invalid opcode")
)
Functions ¶
func AssembleStatement ¶
func AssembleStatement(s *parser.Statement, state TraversalState) ([]arch.Instruction, error)
AssembleStatement turns a parser.Statement into a 32-bit word that will exist in the final binary for a program.
Instructions are assembled according to the ISA and data directives, like .fill or .string are assembled to include the raw data in the form of 32-bit words.
Strings are thus padded with null bytes at the end to make the string occupy a multiple of 32 bits.
func CalculateStatementSize ¶
CalculateStatementSize returns the number of bytes that a statement occupies within the final binary. For example, most directives take up zero bytes while standard instructions take up 4 bytes.
func IsDataDirective ¶
IsDataDirective returns whether the TokenKind represents a directive that will appear as data in the final assembled binary
Types ¶
type Layout ¶
func (*Layout) Assemble ¶
func (l *Layout) Assemble(assembleFunc func(*parser.Statement, TraversalState) ([]arch.Instruction, error)) error
Assemble iterates over each statement throughout the binary, calling the assembler function along the way with each statement and a bound TraversalState.
func (*Layout) InitWithStatements ¶
InitWithStatements initializes the layout with a list of statements
We assume we are starting with the .text section, regardless of whether this function was called earlier and terminated with a different section.
func (*Layout) LocateLabel ¶
LocateLabel returns the absolute address of the label in the final binary
func (*Layout) LocateLabelSection ¶
LocateLabelSection returns the section of the label
func (*Layout) LocateStatement ¶
LocateStatement returns the absolute address of the statement in the final binary
func (*Layout) LocateStatementWithinSection ¶
LocateStatementWithinSection returns the relative address of the statement to the start of the section it resides in.
func (*Layout) SectionByName ¶
SectionByName returns the existing section with the given name or returns a newly created section with the name at the end of the current binary.
type ObjectFile ¶
type ObjectFile struct { Sections []*Section SymbolTable []*objfile.SymbolTableEntry RelocationTable []*objfile.RelocationTableEntry // contains filtered or unexported fields }
func CreateObjectFile ¶
func CreateObjectFile(layout *Layout) (*ObjectFile, error)
func (*ObjectFile) AssembleStatement ¶
func (o *ObjectFile) AssembleStatement(layout *Layout) func(s *parser.Statement, state TraversalState) ([]arch.Instruction, error)
func (*ObjectFile) WriteToFile ¶
func (o *ObjectFile) WriteToFile(layout *Layout, outputFile io.Writer) (err error)
WriteToFile writes the ObjectFile to the given io.Writer completely.
The file format is as follows:
[# of sections] [# of symbol table entries] [# of relocation table entries] - for each section, [section name] [section size] - for each entry, [label name] [section name] [offset] [resolved] - for each entry, [label name] [section name] [offset] [instruction/directive id] [raw assembled instructions]