-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj.go
140 lines (120 loc) · 3.62 KB
/
obj.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package asm
import (
"encoding/binary"
"fmt"
"github.com/dnsge/orange/arch"
"github.com/dnsge/orange/asm/parser"
"github.com/dnsge/orange/linker/objfile"
"io"
)
type ObjectFile struct {
Sections []*Section
SymbolTable []*objfile.SymbolTableEntry
RelocationTable []*objfile.RelocationTableEntry
symbolTableMap map[string]struct{}
}
func printObjectFile(of *ObjectFile) {
for _, s := range of.SymbolTable {
fmt.Printf("symbol %s\n", s)
}
for _, r := range of.RelocationTable {
fmt.Printf("relocation %s\n", r)
}
}
func CreateObjectFile(layout *Layout) (*ObjectFile, error) {
of := &ObjectFile{
Sections: nil,
SymbolTable: nil,
RelocationTable: nil,
symbolTableMap: make(map[string]struct{}),
}
// copy sections
of.Sections = make([]*Section, len(layout.Sections))
for i, sec := range layout.Sections {
of.Sections[i] = sec
}
for labelName, statement := range layout.Labels {
offset, section, ok := layout.LocateStatementWithinSection(statement)
if !ok {
panic("failed to locate statement in section") // todo: Proper error instead of panicking
}
of.optionallyAddSymbol(labelName, &objfile.SymbolTableEntry{
LabelName: labelName,
SectionName: section.Name,
SectionOffset: offset,
Resolved: true,
})
}
return of, nil
}
// optionallyAddSymbol adds the given objfile.SymbolTableEntry to the ObjectFile's
// array of symbols if the label name has not already been added.
func (o *ObjectFile) optionallyAddSymbol(labelName string, entry *objfile.SymbolTableEntry) {
if _, ok := o.symbolTableMap[labelName]; ok {
// already added this symbol to the symbol table
return
}
o.SymbolTable = append(o.SymbolTable, entry)
o.symbolTableMap[labelName] = struct{}{}
}
func (o *ObjectFile) AssembleStatement(layout *Layout) func(s *parser.Statement, state TraversalState) ([]arch.Instruction, error) {
return func(s *parser.Statement, state TraversalState) ([]arch.Instruction, error) {
// Construct resolverTraversalState with encapsulated layout
resolver := &resolverTraversalState{
layout: layout,
state: state,
statement: s,
objectFile: o,
}
// transparently use typical assembly function
return AssembleStatement(s, resolver)
}
}
// 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]
func (o *ObjectFile) WriteToFile(layout *Layout, outputFile io.Writer) (err error) {
// Write # of sections, size of symbol table, size of relocation table
_, err = fmt.Fprintf(outputFile, "%d %d %d\n", len(o.Sections), len(o.SymbolTable), len(o.RelocationTable))
if err != nil {
return
}
// Write section sizes
for _, sec := range o.Sections {
_, err = fmt.Fprintf(outputFile, "%s %d\n", sec.Name, sec.Size)
if err != nil {
return
}
}
// Write symbol table entries
for _, entry := range o.SymbolTable {
err = entry.MarshalTo(outputFile)
if err != nil {
return
}
}
// Write relocation table entries
for _, entry := range o.RelocationTable {
err = entry.MarshalTo(outputFile)
if err != nil {
return
}
}
// write assembled statements
err = layout.Traverse(func(section *Section) error {
for _, a := range section.AssembledStatements {
err = binary.Write(outputFile, arch.ByteOrder, a)
if err != nil {
return err
}
}
return nil
})
return
}