本文整理匯總了Golang中cmd/internal/obj.Binitw函數的典型用法代碼示例。如果您正苦於以下問題:Golang Binitw函數的具體用法?Golang Binitw怎麽用?Golang Binitw使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Binitw函數的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: main
func main() {
log.SetFlags(0)
log.SetPrefix("asm: ")
GOARCH := obj.Getgoarch()
architecture := arch.Set(GOARCH)
if architecture == nil {
log.Fatalf("asm: unrecognized architecture %s", GOARCH)
}
flags.Parse()
// Create object file, write header.
fd, err := os.Create(*flags.OutputFile)
if err != nil {
log.Fatal(err)
}
ctxt := obj.Linknew(architecture.LinkArch)
if *flags.PrintOut {
ctxt.Debugasm = 1
}
ctxt.LineHist.TrimPathPrefix = *flags.TrimPath
ctxt.Flag_dynlink = *flags.Dynlink
if *flags.Shared || *flags.Dynlink {
ctxt.Flag_shared = 1
}
ctxt.Bso = obj.Binitw(os.Stdout)
defer ctxt.Bso.Flush()
output := obj.Binitw(fd)
fmt.Fprintf(output, "go object %s %s %s\n", obj.Getgoos(), obj.Getgoarch(), obj.Getgoversion())
fmt.Fprintf(output, "!\n")
lexer := lex.NewLexer(flag.Arg(0), ctxt)
parser := asm.NewParser(ctxt, architecture, lexer)
diag := false
ctxt.Diag = func(format string, args ...interface{}) {
diag = true
log.Printf(format, args...)
}
pList := obj.Linknewplist(ctxt)
var ok bool
pList.Firstpc, ok = parser.Parse()
if ok {
// reports errors to parser.Errorf
obj.Writeobjdirect(ctxt, output)
}
if !ok || diag {
log.Printf("asm: assembly of %s failed", flag.Arg(0))
os.Remove(*flags.OutputFile)
os.Exit(1)
}
output.Flush()
}
示例2: testEndToEnd
func testEndToEnd(t *testing.T, goarch string) {
lex.InitHist()
input := filepath.Join("testdata", goarch+".s")
output := filepath.Join("testdata", goarch+".out")
architecture, ctxt := setArch(goarch)
lexer := lex.NewLexer(input, ctxt)
parser := NewParser(ctxt, architecture, lexer)
pList := obj.Linknewplist(ctxt)
var ok bool
testOut = new(bytes.Buffer) // The assembler writes -S output to this buffer.
ctxt.Bso = obj.Binitw(os.Stdout)
defer obj.Bflush(ctxt.Bso)
ctxt.Diag = log.Fatalf
obj.Binitw(ioutil.Discard)
pList.Firstpc, ok = parser.Parse()
if !ok {
t.Fatalf("asm: %s assembly failed", goarch)
}
result := string(testOut.Bytes())
expect, err := ioutil.ReadFile(output)
// For Windows.
result = strings.Replace(result, `testdata\`, `testdata/`, -1)
if err != nil {
t.Fatal(err)
}
if result != string(expect) {
if false { // Enable to capture output.
fmt.Printf("%s", result)
os.Exit(1)
}
t.Errorf("%s failed: output differs", goarch)
r := strings.Split(result, "\n")
e := strings.Split(string(expect), "\n")
if len(r) != len(e) {
t.Errorf("%s: expected %d lines, got %d", goarch, len(e), len(r))
}
n := len(e)
if n > len(r) {
n = len(r)
}
for i := 0; i < n; i++ {
if r[i] != e[i] {
t.Errorf("%s:%d:\nexpected\n\t%s\ngot\n\t%s", output, i, e[i], r[i])
}
}
}
}
示例3: Fuzz
func Fuzz(data []byte) int {
f, err := ioutil.TempFile("", "fuzz.asm")
if err != nil {
return 0
}
defer os.Remove(f.Name())
defer f.Close()
_, err = f.Write(data)
if err != nil {
return 0
}
f.Close()
defer func() {
if x := recover(); x != nil {
if str, ok := x.(string); ok && str == "os.Exit" {
return
}
panic(x)
}
}()
const GOARCH = "amd64"
architecture := arch.Set(GOARCH)
fd := new(bytes.Buffer)
ctxt := obj.Linknew(architecture.LinkArch)
// Try to vary these and other flags:
// ctxt.Flag_dynlink
// ctxt.Flag_shared
ctxt.Bso = obj.Binitw(new(bytes.Buffer))
defer ctxt.Bso.Flush()
ctxt.Diag = func(format string, v ...interface{}) { panic("os.Exit") }
output := obj.Binitw(fd)
lexer := lex.NewLexer(f.Name(), ctxt)
parser := asm.NewParser(ctxt, architecture, lexer)
pList := obj.Linknewplist(ctxt)
var ok bool
pList.Firstpc, ok = parser.Parse()
if !ok {
return 0
}
obj.Writeobjdirect(ctxt, output)
output.Flush()
return 1
}
示例4: main
func main() {
log.SetFlags(0)
log.SetPrefix("asm: ")
GOARCH := obj.Getgoarch()
architecture := arch.Set(GOARCH)
if architecture == nil {
log.Fatalf("asm: unrecognized architecture %s", GOARCH)
}
flags.Parse(architecture.Thechar)
// Create object file, write header.
fd, err := os.Create(*flags.OutputFile)
if err != nil {
log.Fatal(err)
}
ctxt := obj.Linknew(architecture.LinkArch)
if *flags.PrintOut {
ctxt.Debugasm = 1
}
ctxt.Trimpath = *flags.TrimPath
if *flags.Shared {
ctxt.Flag_shared = 1
}
ctxt.Bso = obj.Binitw(os.Stdout)
defer obj.Bflush(ctxt.Bso)
ctxt.Diag = log.Fatalf
output := obj.Binitw(fd)
fmt.Fprintf(output, "go object %s %s %s\n", obj.Getgoos(), obj.Getgoarch(), obj.Getgoversion())
fmt.Fprintf(output, "!\n")
lexer := lex.NewLexer(flag.Arg(0), ctxt)
parser := asm.NewParser(ctxt, architecture, lexer)
pList := obj.Linknewplist(ctxt)
var ok bool
pList.Firstpc, ok = parser.Parse()
if !ok {
log.Fatalf("asm: assembly of %s failed", flag.Arg(0))
os.Exit(1)
}
obj.Writeobjdirect(ctxt, output)
obj.Bflush(output)
}
示例5: testEndToEnd
func testEndToEnd(t *testing.T, goarch, file string) {
lex.InitHist()
input := filepath.Join("testdata", file+".s")
architecture, ctxt := setArch(goarch)
lexer := lex.NewLexer(input, ctxt)
parser := NewParser(ctxt, architecture, lexer)
pList := obj.Linknewplist(ctxt)
var ok bool
testOut = new(bytes.Buffer) // The assembler writes test output to this buffer.
ctxt.Bso = obj.Binitw(os.Stdout)
defer ctxt.Bso.Flush()
failed := false
ctxt.Diag = func(format string, args ...interface{}) {
failed = true
t.Errorf(format, args...)
}
obj.Binitw(ioutil.Discard)
pList.Firstpc, ok = parser.Parse()
if !ok || failed {
t.Errorf("asm: %s assembly failed", goarch)
return
}
output := strings.Split(testOut.String(), "\n")
// Reconstruct expected output by independently "parsing" the input.
data, err := ioutil.ReadFile(input)
if err != nil {
t.Error(err)
return
}
lineno := 0
seq := 0
hexByLine := map[string]string{}
lines := strings.SplitAfter(string(data), "\n")
Diff:
for _, line := range lines {
lineno++
// The general form of a test input line is:
// // comment
// INST args [// printed form] [// hex encoding]
parts := strings.Split(line, "//")
printed := strings.TrimSpace(parts[0])
if printed == "" || strings.HasSuffix(printed, ":") { // empty or label
continue
}
seq++
var hexes string
switch len(parts) {
default:
t.Errorf("%s:%d: unable to understand comments: %s", input, lineno, line)
case 1:
// no comment
case 2:
// might be printed form or hex
note := strings.TrimSpace(parts[1])
if isHexes(note) {
hexes = note
} else {
printed = note
}
case 3:
// printed form, then hex
printed = strings.TrimSpace(parts[1])
hexes = strings.TrimSpace(parts[2])
if !isHexes(hexes) {
t.Errorf("%s:%d: malformed hex instruction encoding: %s", input, lineno, line)
}
}
if hexes != "" {
hexByLine[fmt.Sprintf("%s:%d", input, lineno)] = hexes
}
// Canonicalize spacing in printed form.
// First field is opcode, then tab, then arguments separated by spaces.
// Canonicalize spaces after commas first.
// Comma to separate argument gets a space; comma within does not.
var buf []byte
nest := 0
for i := 0; i < len(printed); i++ {
c := printed[i]
switch c {
case '{', '[':
nest++
case '}', ']':
nest--
case ',':
buf = append(buf, ',')
if nest == 0 {
buf = append(buf, ' ')
}
for i+1 < len(printed) && (printed[i+1] == ' ' || printed[i+1] == '\t') {
i++
}
continue
}
buf = append(buf, c)
}
//.........這裏部分代碼省略.........
示例6: dumpexport
func dumpexport() {
if buildid != "" {
exportf("build id %q\n", buildid)
}
size := 0 // size of export section without enclosing markers
if forceNewExport || newexport != 0 {
// binary export
// The linker also looks for the $$ marker - use char after $$ to distinguish format.
exportf("\n$$B\n") // indicate binary format
const verifyExport = true // enable to check format changes
if verifyExport {
// save a copy of the export data
var copy bytes.Buffer
bcopy := obj.Binitw(©)
size = Export(bcopy, Debug_export != 0)
bcopy.Flush() // flushing to bytes.Buffer cannot fail
if n, err := bout.Write(copy.Bytes()); n != size || err != nil {
Fatalf("error writing export data: got %d bytes, want %d bytes, err = %v", n, size, err)
}
// export data must contain no '$' so that we can find the end by searching for "$$"
if bytes.IndexByte(copy.Bytes(), '$') >= 0 {
Fatalf("export data contains $")
}
// verify that we can read the copied export data back in
// (use empty package map to avoid collisions)
savedPkgMap := pkgMap
savedPkgs := pkgs
pkgMap = make(map[string]*Pkg)
pkgs = nil
importpkg = mkpkg("")
Import(obj.Binitr(©)) // must not die
importpkg = nil
pkgs = savedPkgs
pkgMap = savedPkgMap
} else {
size = Export(bout, Debug_export != 0)
}
exportf("\n$$\n")
} else {
// textual export
lno := lineno
exportf("\n$$\n") // indicate textual format
exportsize = 0
exportf("package %s", localpkg.Name)
if safemode != 0 {
exportf(" safe")
}
exportf("\n")
for _, p := range pkgs {
if p.Direct {
dumppkg(p)
}
}
// exportlist grows during iteration - cannot use range
for len(exportlist) > 0 {
n := exportlist[0]
exportlist = exportlist[1:]
lineno = n.Lineno
dumpsym(n.Sym)
}
size = exportsize
exportf("\n$$\n")
lineno = lno
}
if Debug_export != 0 {
fmt.Printf("export data size = %d bytes\n", size)
}
}
示例7: testErrors
func testErrors(t *testing.T, goarch, file string) {
lex.InitHist()
input := filepath.Join("testdata", file+".s")
architecture, ctxt := setArch(goarch)
lexer := lex.NewLexer(input, ctxt)
parser := NewParser(ctxt, architecture, lexer)
pList := obj.Linknewplist(ctxt)
var ok bool
testOut = new(bytes.Buffer) // The assembler writes test output to this buffer.
ctxt.Bso = obj.Binitw(os.Stdout)
defer ctxt.Bso.Flush()
failed := false
var errBuf bytes.Buffer
ctxt.DiagFunc = func(format string, args ...interface{}) {
failed = true
s := fmt.Sprintf(format, args...)
if !strings.HasSuffix(s, "\n") {
s += "\n"
}
errBuf.WriteString(s)
}
pList.Firstpc, ok = parser.Parse()
obj.Flushplist(ctxt)
if ok && !failed {
t.Errorf("asm: %s had no errors", goarch)
}
errors := map[string]string{}
for _, line := range strings.Split(errBuf.String(), "\n") {
if line == "" || strings.HasPrefix(line, "\t") {
continue
}
m := fileLineRE.FindStringSubmatch(line)
if m == nil {
t.Errorf("unexpected error: %v", line)
continue
}
fileline := m[1]
if errors[fileline] != "" {
t.Errorf("multiple errors on %s:\n\t%s\n\t%s", fileline, errors[fileline], line)
continue
}
errors[fileline] = line
}
// Reconstruct expected errors by independently "parsing" the input.
data, err := ioutil.ReadFile(input)
if err != nil {
t.Error(err)
return
}
lineno := 0
lines := strings.Split(string(data), "\n")
for _, line := range lines {
lineno++
fileline := fmt.Sprintf("%s:%d", input, lineno)
if m := errRE.FindStringSubmatch(line); m != nil {
all := m[1]
mm := errQuotesRE.FindAllStringSubmatch(all, -1)
if len(mm) != 1 {
t.Errorf("%s: invalid errorcheck line:\n%s", fileline, line)
} else if err := errors[fileline]; err == "" {
t.Errorf("%s: missing error, want %s", fileline, all)
} else if !strings.Contains(err, mm[0][1]) {
t.Errorf("%s: wrong error for %s:\n%s", fileline, all, err)
}
} else {
if errors[fileline] != "" {
t.Errorf("unexpected error on %s: %v", fileline, errors[fileline])
}
}
delete(errors, fileline)
}
var extra []string
for key := range errors {
extra = append(extra, key)
}
sort.Strings(extra)
for _, fileline := range extra {
t.Errorf("unexpected error on %s: %v", fileline, errors[fileline])
}
}