Updated GcToolchainTricks (markdown)

Mingye Wang 2021-08-02 17:38:02 +08:00
parent 6ff1677d41
commit fc657ac757

@ -11,7 +11,7 @@ The most important step is compiling that file to file.syso (` gcc -c -O3 -o fil
and put the resulting syso in the package source directory. and put the resulting syso in the package source directory.
And then, suppose your assembly function is named Func, you need one stub And then, suppose your assembly function is named Func, you need one stub
[cmd/asm](https://golang.org/cmd/asm) assembly file to call it: [cmd/asm](https://golang.org/cmd/asm) assembly file to call it:
``` ```as
TEXT ·Func(SB),$0-8 // please set the correct parameter size (8) here TEXT ·Func(SB),$0-8 // please set the correct parameter size (8) here
JMP Func(SB) JMP Func(SB)
``` ```
@ -34,7 +34,7 @@ There are a lot of ways to bundle data in Go binary, for example:
The key trick for the 3rd alternative is that the linker for the ` gc ` toolchain has the ability to link COFF object files of a different architecture into the binary without problem, so you don't need to provide syso files for all supported architectures. As long as the syso file doesn't contain instructions, you can just use one to embed the data. The key trick for the 3rd alternative is that the linker for the ` gc ` toolchain has the ability to link COFF object files of a different architecture into the binary without problem, so you don't need to provide syso files for all supported architectures. As long as the syso file doesn't contain instructions, you can just use one to embed the data.
The assembly template to generate the COFF .syso file: The assembly template to generate the COFF .syso file:
``` ```as
/* data.S, as -o data.syso */ /* data.S, as -o data.syso */
.section .rdata,"dr" /* put in COFF section .rdata */ .section .rdata,"dr" /* put in COFF section .rdata */
.globl _bindataA /* no longer need to prepend package name here */ .globl _bindataA /* no longer need to prepend package name here */
@ -51,7 +51,7 @@ _ebindataB:
``` ```
And two other files, first a Plan 9 C source file that assembles the slice for Go: And two other files, first a Plan 9 C source file that assembles the slice for Go:
``` ```c
/* slice.c */ /* slice.c */
#include "runtime.h" #include "runtime.h"
extern byte _bindataA[], _bindataB[], _ebindataA, _ebindataB; extern byte _bindataA[], _bindataB[], _ebindataA, _ebindataB;
@ -67,7 +67,7 @@ void ·getDataSlices(Slice a, Slice b) {
``` ```
And finally, the Go file that uses the embedded slide: And finally, the Go file that uses the embedded slide:
``` ```go
/* data.go */ /* data.go */
package bindata package bindata
@ -98,7 +98,7 @@ The gc toolchain linker, [cmd/link](https://golang.org/cmd/link), provides a `-X
Let's suppose this file is part of the package `company/buildinfo`: Let's suppose this file is part of the package `company/buildinfo`:
``` ```go
package buildinfo package buildinfo
var BuildTime string var BuildTime string