asm

package
v0.0.0-...-cf91f57 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2022 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidOpcode = fmt.Errorf("invalid opcode")
)

Functions

func AssembleExecutable

func AssembleExecutable(inputFile io.Reader, outputFile io.Writer) error

func AssembleObjectFile

func AssembleObjectFile(inputFile io.Reader, outputFile io.Writer) error

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

func CalculateStatementSize(statement *parser.Statement) int

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

func IsDataDirective(kind lexer.TokenKind) bool

IsDataDirective returns whether the TokenKind represents a directive that will appear as data in the final assembled binary

Types

type Layout

type Layout struct {
	Sections []*Section
	Labels   map[string]*parser.Statement
}

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

func (l *Layout) InitWithStatements(statements []*parser.Statement) error

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

func (l *Layout) LocateLabel(label string) (uint32, bool)

LocateLabel returns the absolute address of the label in the final binary

func (*Layout) LocateLabelSection

func (l *Layout) LocateLabelSection(label string) (*Section, bool)

LocateLabelSection returns the section of the label

func (*Layout) LocateStatement

func (l *Layout) LocateStatement(statement *parser.Statement) (int, bool)

LocateStatement returns the absolute address of the statement in the final binary

func (*Layout) LocateStatementWithinSection

func (l *Layout) LocateStatementWithinSection(statement *parser.Statement) (int, *Section, bool)

LocateStatementWithinSection returns the relative address of the statement to the start of the section it resides in.

func (*Layout) SectionByName

func (l *Layout) SectionByName(name string) *Section

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.

func (*Layout) Traverse

func (l *Layout) Traverse(traversalFunc func(section *Section) error) error

Traverse iterates over each section throughout the binary, calling the traversal function along the way.

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]

type Section

type Section struct {
	// The user-defined section name
	Name string
	// The size of the section in bytes
	Size int

	Statements          []*parser.Statement
	StatementSizes      []int
	AssembledStatements []arch.Instruction
}

type TraversalState

type TraversalState interface {
	parser.Relocator
	Section() *Section
	Address() int
	AdvanceAddress(amount int)
}

Directories

Path Synopsis
gen

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL