Open geraldstanje opened 1 week ago
That looks like an issue with the Rust toolchain itself. The problem here is that the compiler is running in the aarch64
platform, but you are explicitly telling the compiler to build the binary for x86_64-unknown-linux-gnu
. So, the compiler is trying to cross-compile the program for x86_64
by using the -m64
option, which is not supported by your compiler.
hi @plusvic i have a macbook pro m1 with osx, run the dockerfile above to compile for linux amd64 (AMD64 = x86-64). did i install the wrong compiler?
hi,
i got it to work:
Dockerfile:
FROM golang:latest AS builder
RUN apt-get update && apt-get install -y ca-certificates
RUN apt-get install -y xz-utils libssl-dev
WORKDIR /home/contrib
COPY . .
ENV PATH="/root/.cargo/bin:$PATH"
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain=1.79.0 -y --verbose
RUN cargo install cargo-c
RUN git clone https://github.com/VirusTotal/yara-x
RUN rustup target add x86_64-unknown-linux-gnu
RUN cd yara-x && cargo cinstall -p yara-x-capi --target x86_64-unknown-linux-gnu
RUN GOOS=linux CGO_ENABLED=1 GOARCH=amd64 CGO_CFLAGS="$(pkg-config --cflags yara_x_capi)" CGO_LDFLAGS="-Wl,-rpath,/usr/local/lib $(pkg-config --libs yara_x_capi)" go build -o contrib -v .
FROM ubuntu:latest
COPY --from=builder /usr/local/lib/libyara_x_capi.so.0.4 /usr/local/lib/libyara_x_capi.so.0.4
COPY --from=builder /home/contrib/contrib /home/contrib/contrib
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
ENTRYPOINT ["/home/contrib/contrib"]
main.go:
package main
import (
"fmt"
"time"
yarax "github.com/VirusTotal/yara-x/go"
)
func main() {
// Compile some YARA rules.
rules, _ := yarax.Compile(`
rule foo {
strings:
$foo = "foo"
condition:
$foo
}
rule bar {
strings:
$bar = "bar"
condition:
$bar
}`)
// Use the compiled rules for scanning some data.
matchingRules, _ := rules.Scan([]byte("foobar"))
// Iterate over the matching rules.
for _, r := range matchingRules {
fmt.Printf("rule %s matched\n", r.Identifier())
}
time.Sleep(1000 * time.Second)
}
build.sh:
app_name="yara_test:v1"
docker build -t ${app_name} . --platform linux/amd64
output when executing the golang binary:
rule foo matched
rule bar matched
i run into a segfault with rustup - so i had to disable rosetta in the docker desktop (docker desktop 4.28.0 on my macbook pro m1): infos are here: https://github.com/rust-lang/rustup/issues/3902 and https://github.com/rust-lang/rust/issues/125430
could be helpful to add this into the docs...
how can i static link the lib libyara_x_capi to the golang app? also is there a way to use CGO_ENABLED=0 for that?
cc @plusvic @steffenfritz
For static link, did you tried go build -tags static_link
?
https://github.com/VirusTotal/yara-x/blob/c45c8c7ad3c78174f981b6c74a5a3620c65b03d9/go/main.go#L4-L6
When you use the static_link
build tag, CGO passes the --static
argument to pkg-config, which means that your program will be linked with a static version of the library.
Regarding to CGO_ENABLED
, it must be CGO_ENABLED=1
, you can't disable CGO when you are building a Go module like this, that uses C code for interfacing with a C library.
hi,
i tried to build the lib for amd64 - but got the following error. any idea how to fix it?
docker file:
error: