google / gopacket

Provides packet processing capabilities for Go
BSD 3-Clause "New" or "Revised" License
6.33k stars 1.13k forks source link

Cross compile for arm #100

Open amlwwalker opened 9 years ago

amlwwalker commented 9 years ago

Any where I can find instructions on how to successfully cross compile this library for ARM? I've followed Dave Cheney's instructions but that just compiles the standard library for ARM, not depenencies. Its a cgo issue I think

cnsgcu commented 8 years ago

I am having the same issue. Anyone has a workaround or solution?

cnsgcu commented 8 years ago

Nvm, Figured out the solution.

gconnell commented 8 years ago

Any chance you could post solution?

On Tue, Dec 1, 2015 at 8:14 PM, cnsgcu notifications@github.com wrote:

Nvm, Figured out the solution.

— Reply to this email directly or view it on GitHub https://github.com/google/gopacket/issues/100#issuecomment-161166672.

cnsgcu commented 8 years ago

Download and extract android ndk. I have it under ~/Android/Ndk

Compile libpcap - NOTE: May need to install flex, and yacc, bison

  1. Download pcap from https://github.com/android/platform_external_libpcap and unzip it(I rename it to libpcap)
  2. cd into libpcap folder and create the following symbolic links - NOTE: Need to change ndk directory accordingly ln -s /home/cuong/Android/Ndk/platforms/android-21/arch-arm/usr/lib/crtbegin_so.o crtbegin_so.o ln -s /home/cuong/Android/Ndk/platforms/android-21/arch-arm/usr/lib/crtend_so.o crtend_so.o
  3. Download or copy the scanner.c.top file from https://github.com/cnsgcu/Droidfluid/blob/master/app/src/main/prebuilt/scanner.c.top into this folder(libpcap)
  4. Use the following script to build libpcap - NOTE: Need to change prebuilt/linux-x86 to prebuilt/linux-x86_64, LIBPCAP_DIR, and NDK_ROOT accordingly

    !/bin/sh

    LIBPCAP_DIR=libpcap # libpcap NDK_ROOT=~/Android/Ndk # Path to the Android NDK root

    openlib() { if [ ! -d "$2" ]; then echo "$1 not found.\nPlace it at pwd/$2"; exit fi cd $2 make clean; make distclean }

    checkerror() { if [ "$?" -ne 0 ]; then echo "Error building $1. See log above."; exit fi }

    export PATH=$PATH:$NDK_ROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86/bin export PATH=$PATH:$NDK_ROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86/include

    openlib "libpcap" $LIBPCAP_DIR ./configure \ --host=arm-linux-androideabi \ --with-pcap=linux \ CC=arm-linux-androideabi-gcc \ LD=arm-linux-androideabi-ld \ CFLAGS="--sysroot=$NDK_ROOT/platforms/android-21/arch-arm/" \ LDFLAGS="-L$NDK_ROOT/platforms/android-21/arch-arm/usr/lib/" checkerror "libpcap" make checkerror "libpcap" cd -

Build your app and gopacket - NOTE: Need to change ndk directory accordingly

  1. Create a standalone toolchain /home/cuong/Android/Ndk/build/tools/make-standalone-toolchain.sh --platform=android-21 --install-dir=standalone --toolchain=arm-linux-androideabi-4.8
  2. Setup PATH env export PATH=/home/cuong/standalone/bin:$PATH
  3. Correct type errors in the following go files /usr/local/go/src/net/cgo_resnew.go gerrno, err := C.getnameinfo(sa, salen, (*C.char)(unsafe.Pointer(&b[0])), (C.size_t)(C.socklen_t(len(b))), nil, 0, C.NI_NAMEREQD) github.com/gopacket/pcap/pcap.go hdr.ts.tv_sec = (C.kernel_time_t)(C.gopacket_time_secs_t(ci.Timestamp.Unix())) hdr.ts.tv_usec = (C.kernel_suseconds_t)(C.gopacket_time_usecs_t(ci.Timestamp.Nanosecond() / 1000))
  4. Run this command to build your app and gopacket CGO_ENABLED=1 CGO_CFLAGS="-I/home/cuong/standalone/sysroot/usr/include -I/home/cuong/Downloads/libpcap" CGO_LDFLAGS="-L/home/cuong/standalone/lib/ -L/home/cuong/Downloads/libpcap" CC=arm-linux-androideabi-gcc LD=arm-linux-androideabi-ld GOOS=android GOARCH=arm GOARM=7 go build -ldflags="-extldflags=-pie" -v .

I am currently busy with coming exams and can not write a general solution. Just write down what I did. Give it a try and let me know if it works.

initzero commented 8 years ago

I was able to cross compile a gopacket Go program for the Pi Zero using the official toolchain that they provide @

git clone git://github.com/raspberrypi/tools.git

Then I cross compiled libpcap-1.7.4 using the toolchain:

./configure --host=arm-linux-gnueabihf --prefix=$PATH_TO_SYSROOT --with-pcap=linux && make && make install

Then I used the flags from step #4 of @cnsgcu's post above, substituting the appropriate values into CGO_CFLAGS, CGO_LDFLAGS, CC, LD, GOOS, and GOARM to match my toolchain and target.

Copied the libpcap.so* files to the Pi Zero's /usr/lib folder and copied my cross compiled Go program and it works as expected.

Reference for setting up raspi toolchain

zboya commented 7 years ago

I have same problem,

GOOS=linux GOARCH=arm go build -o bin/pcapdump-arm src/vendor/github.com/google/gopacket/examples/pcapdump/main.go 
# command-line-arguments
src/vendor/github.com/google/gopacket/examples/pcapdump/main.go:31: undefined: pcap.Handle
src/vendor/github.com/google/gopacket/examples/pcapdump/main.go:34: undefined: pcap.OpenOffline
src/vendor/github.com/google/gopacket/examples/pcapdump/main.go:41: undefined: pcap.NewInactiveHandle
src/vendor/github.com/google/gopacket/examples/pcapdump/main.go:54: undefined: pcap.TimestampSourceFromString

How can I fix it?

evilsocket commented 6 years ago

It's actually quite easy to cross compile this library for ARM (requires gcc-arm-linux-gnueabi, byacc and flex packages).

Step 1: download and cross compile libpcap-1.8.1 (adjust PCAPV to use a different libpcap version):

cd /tmp
export PCAPV=1.8.1
wget http://www.tcpdump.org/release/libpcap-$PCAPV.tar.gz
tar xvf libpcap-$PCAPV.tar.gz
cd libpcap-$PCAPV
export CC=arm-linux-gnueabi-gcc
./configure --host=arm-linux --with-pcap=linux
make

Step 2: now cross compile your tool:

cd $GOPATH/src/github.com/your-user/your-tool
env CC=arm-linux-gnueabi-gcc CGO_ENABLED=1 GOOS=linux GOARCH=arm CGO_LDFLAGS="-L/tmp/libpcap-$PCAPV" go build .

You are welcome.

xiam commented 6 years ago

If you're cross compiling for Android using the NDK this could come in handy: https://github.com/google/gopacket/pull/421