Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy to read Golang assembly output?

Tags:

assembly

go

I'm interested in examining the x86 assembly output of the standard Go compiler to see if my code is really being converted into reasonably efficient assembly code; hopefully, by profiling and examining the assembly output, I could get a clue as to where/how I should rewrite my Go code for maximum performance. But when I examine the code using the -S flag, Go spits out a mess! I'd like two things:

  1. Is there a way to make the Go compiler dump the assembly output into a file, not just print it out on Terminal?

  2. Also, is there a way to make the Go compiler separate out the assembly code into separate functions, with labels? I know some functions may be inlined and hence not appear in the assembly code. What I'm seeing know is just a homogenous blob of assembly which is almost impossible to understand.

like image 366
Gautam Avatar asked May 21 '14 17:05

Gautam


People also ask

How to read a file in Golang?

In this article we are discussing different ways to read files in Golang. We read the text and binary files. To read files in Go, we use the os, ioutil, io, and bufio packages. There are many ways to read a file. Here we will explore some of the common and effective ways to read a file in Go.

Why is Golang so important for assembly language development?

This is important, as now the compilation phase not only handles both the high-level code (golang) and the generation of assembly, it generates real instructions in the form of an intermediate representation known as obj. obj generates real instructions for the linker and is also agnostic to the type of assembler. Where does Golang fit in all this?

How does the Go compiler work?

The Go compiler outputs an abstract, portable form of assembly that doesn't actually map to any real hardware. The Go assembler then uses this pseudo-assembly output in order to generate concrete, machine-specific instructions for the targeted hardware.

What is Assembly used for in Go programming?

Some assembly optimizations also can be done for the gcc compiler. In other cases Go’s take on assembly is simply used to standardize the calling between code on all different CPU platforms. Some low-level synchronization primitives are handled by Assembler. This is shown both the runtime/atomic and sync/atomic packages.


2 Answers

  1. You can redirect the output to a file like this:

     go tool compile -S file.go > file.s 
  2. You can disable the optimization with -N:

     go tool compile -S -N file.go 

Alternatively, you can use gccgo:

gccgo -S -O0 -masm=intel test.go 

which will generate test.s. You can play with the -O0/1/2/3 to see the different optimizations.

like image 141
creack Avatar answered Oct 21 '22 00:10

creack


I don't recommend using the output of -S as the Go linker can change what gets written to the object code quite a lot. It does give you some idea as to what is going on.

The go assembler output is rather non-standard too.

When I want to do this I always use objdump which will give you a nice standard assembler output.

Eg for x86 / amd64

objdump -d executable > disassembly 

And for ARM (to get the register names to be the same as Go uses)

objdump -M reg-names-raw -d executable > disassembly 
like image 39
Nick Craig-Wood Avatar answered Oct 21 '22 01:10

Nick Craig-Wood