Skip to content

Instantly share code, notes, and snippets.

@fcwu
Created April 29, 2020 08:08
Show Gist options
  • Save fcwu/b450c7d21d841d456504a3e9ff590164 to your computer and use it in GitHub Desktop.
Save fcwu/b450c7d21d841d456504a3e9ff590164 to your computer and use it in GitHub Desktop.

a.go

package main

/*
#include <stdio.h>
char __attribute__((section(".signature"))) signature[40] ;
int helloFromC() {
        printf("hello from C\n");
}
*/
import "C"

import (
        "encoding/hex"
        "fmt"
        "unsafe"
)

func main() {
        fmt.Println("vim-go")
        C.helloFromC()
        b := C.GoBytes(unsafe.Pointer(&C.signature[0]), 40)
        fmt.Printf("%s", hex.Dump(b))
}

build

go build a.go

dump signature section → all 0

$ objdump -s -j .signature a

a:     file format elf64-x86-64

Contents of section .signature:
 7663a0 00000000 00000000 00000000 00000000  ................
 7663b0 00000000 00000000 00000000 00000000  ................
 7663c0 00000000 00000000                    ........

inject sha1

$ printf `sha1sum -b a | cut -d' ' -f1` > sha1
$ cat sha1
b1c7c29286fcbcdd059b5c13de386acad3c6b95b%                                                                                                                                                                   ╭─[email protected] ~/workspace/edge-core-update  ‹feat/license*›
$ objcopy --update-section .signature=sha1 a

dump and run

$ objdump -s -j .signature a                                                                                                                                                                       130a:     file format elf64-x86-64

Contents of section .signature:
 7663a0 62316337 63323932 38366663 62636464  b1c7c29286fcbcdd
 7663b0 30353962 35633133 64653338 36616361  059b5c13de386aca
 7663c0 64336336 62393562                    d3c6b95b
$ ./a
vim-go
hello from C
00000000  62 31 63 37 63 32 39 32  38 36 66 63 62 63 64 64  |b1c7c29286fcbcdd|
00000010  30 35 39 62 35 63 31 33  64 65 33 38 36 61 63 61  |059b5c13de386aca|
00000020  64 33 63 36 62 39 35 62                           |d3c6b95b|
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment