Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross Compiling for Raspi - Executing the programm ends in "Segmentation fault"

I have a self-written programm which I want to build also for Raspberry Pi from my x86 machine. I'm using eclipse generated makefiles and cannot change this thing.

I've read this tutorial for CC for raspi : Hackaday-Link. Because raspi also have installed gcc version 4.9, I also try it with this version of the cross compiler. The problem also exisits with this hello world programm:

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    cout << "hello world!" << endl;
}

When compiling and running it directly on the raspi, the output is hello world!. OK, thats fine. But when cross-compiling it with version 4.9 of arm-linux-gnueabihf-g++-4.9, then scp it to the raspi, make it executable and run it, the output of ./hello_world is Segmentation fault. When executing sudo ./hello_world there is no output.

I tried to get some information about the files and see that the locally on the raspi compiled program outputed:

pi@raspberrypi:~ $ file hello_world
hello_world: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.32, BuildID[sha1]=41161ae762d940b12c3313ca065a3badd284f6d3, not stripped

and the cross-compiled version outputs

pi@raspberrypi:~ $ file hello_world
hello_world: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, BuildID[sha1]=4f1f4fb86710ef8130f148dc5adae1c0c18092fd, not stripped

Can anyone tell me what the problem is and how to solve it?

like image 311
alabamajack Avatar asked Oct 29 '22 21:10

alabamajack


1 Answers

Toolchain compiler arm-linux-gnueabihf-gcc can have different default parameters which one can get running:

 arm-linux-gnueabihf-gcc -Q --help=target

Compiler installed on Raspberry Stretch (I will leave essential information only):

-march= armv6 -marm [enabled] -mfloat-abi= hard -mfp16-format= none -mfpu= vfp

Stretch default cross-compiler:

-march= armv7-a -marm [disabled] -mfloat-abi= hard -mfp16-format= none -mfpu= vfpv3-d16

Now you see the difference in architecture. So to sompile with cross-compiler one will need to set march to match your desired CPU. Also note that Debian cross compiler by default emits Thumb code and Raspberry Stretch emits ARM code

I suggest you're cross-compiling for newer CPU family while your device doesn't support it.

like image 95
norekhov Avatar answered Nov 02 '22 09:11

norekhov