How do cross-compilie on mips without error? - c

I want to cross compile for MIPS. I am compiling this simple hello world:
#include <stdio.h>
int main(){
printf("HELLO WORLD\n");
return 0;
}
I compile it's so: mips-gcc e.c -static -o hello .
It is hello: ELF 32-bit MSB executable, MIPS, MIPS-I version 1 (SYSV), statically linked, not stripped
uname -a on MIPS system is:
Linux RT-AC1200 2.6.36 #1 Thu Mar 15 11:18:53 CST 2018 mips GNU/Linux
However, when I'm trying to start hello, I'm getting:
./hello: line 1: syntax error: unexpected "("
I tried to compile with different MIPS cross compilers(including gcc-mips-linux-gnu), however always I get this error. What do I do wrong? How to compile right ?

Related

Why it takes me a long time to compile this simple C code?

Why does it take so long (around 10 ~ 20 sec.) to compile my simple printf code?
#include <stdio.h>
int main (void){
printf("why am i so slow\n");
}
This is the output I got in VSCode terminal:
[Running] cd "c:\Users\Gnij\cs50_classes\" && gcc pset_hello.c -o pset_hello && "c:\Users\Gnij\cs50_classes\"pset_hello
why am I so slow
[Done] exited with code=0 in 19.531 seconds
Windows 11
Processor: Intel(R) Core(TM) i7-9750H CPU # 2.60GHz 2.59 GHz
Compiler: gcc (MinGW.org GCC-6.3.0-1) 6.3.0
I expected this to be done in less than five seconds.
However, when I was executing the .exe file of this program, it ran instantly. Why is this the case?

"bash: ./main: cannot execute binary file: Exec format error" using wsl2:Ubuntu on windows 11 Visual Studio Code

#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
I build by:
gcc -Wall -ansi -pedantic -c main.c -o main
uname -m gives "x86_64".
After compiling file main gives:
main: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
strace ./main gives:
execve("./main", ["./main"], 0x7ffc779cf7c0 /* 33 vars */) = -1 EACCES (Permission denied)
strace: exec: Permission denied
+++ exited with 1 +++"
You did only compile, but not link the program. Unlinked object files cannot be executed.
Change your build command to:
gcc -Wall -ansi -pedantic main.c -o main
Note the missing -c.

Libsodium on macOS, undefined symbols for x86_64

I'm using an Intel Mac running Catalina 10.15.1
I'm trying to use libsodium using gcc Apple clang version 11.0.0 (clang-1100.0.33.12)
I have both tried to install libsodium via home-brew and manually compile (which was successful), however, when trying to use libsodium I get this error:
Undefined symbols for architecture x86_64:
"_crypto_generichash", referenced from:
_main in sodium-ae2fd0.o
"_sodium_init", referenced from:
_main in sodium-ae2fd0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This is the basic code, using libsodium: stable 1.0.18 (bottled), HEAD
1 #include <sodium.h>
2
3 int main(void)
4 {
5
6 sodium_init();
7
8 #define MESSAGE ((const unsigned char *) "Arbitrary data to hash")
9 #define MESSAGE_LEN 22
10
11 unsigned char hash[crypto_generichash_BYTES];
12
13 crypto_generichash(hash, sizeof hash,
14 MESSAGE, MESSAGE_LEN,
15 NULL, 0);
16
17 return 0;
18 }
Any ideas?
The issue is probably not in the libsodium installation, but in the way your example application is compiled.
In order to link a library (besides the C library which is implicitly linked) when compiling a C program, you need to add the -l<library name> flag to the compilation command line:
cc -Wall -W -o example example.c -lsodium

C program for MIPS74kc never returns

I have a simple C "Hello world" program, compiled using GCC toolchain for mips74-kc running a Linux S/O (kernel 3.10.36)
#include <stdio.h>
int main() {
printf("Hello world\n");
return 0;
}
The program is compiled on a x64-86 Ubuntu machine, as I don't have GCC available on the MIPS machine. I compile the program with the static linking flag.
mips-linux-gnu-gcc --verbose -static -march=74kc main.c -o main
When I launch the program on the MIPS processor, the program holds and never returns, consuming 100 % of the CPU.
Does anyone have a clue on why this could happen?

Make text segment writable, ELF [duplicate]

This question already has answers here:
How can I make GCC compile the .text section as writable in an ELF binary?
(4 answers)
Closed 8 years ago.
I need to make .text segment of an executable ELF writable.
The program i need to modify is written in C and i can compile it. Any ideas?
Thanks A lot.
For the answer below, I'm going to use this test program:
#include <stdio.h>
#include <stdlib.h>
int
main (int argc, char **argv)
{
printf ("Hello world\n");
void *m = main;
*((char *) m) = 0;
exit (0);
}
Compile with:
$ gcc -g -o test test.c
As expected:
$ gdb test
...
(gdb) run
Starting program: /home/amb/so/test
Hello world
Program received signal SIGSEGV, Segmentation fault.
0x00000000004005a2 in main (argc=1, argv=0x7fffffffe628) at test.c:9
9 *((char *)m) = 0;
(gdb)
The obvious route here is to use the -Wl flag to gcc to pass -N or (aka --omagic) to the linker, i.e. gcc ... -Wl,--omagic ..., though this may have other undesirable results (e.g. disabling shared libraries). From the man page:
-N
--omagic
Set the text and data sections to be readable and writable. Also, do not page-align the
data segment, and disable linking against shared libraries. If the output format
supports Unix style magic numbers, mark the output as "OMAGIC". Note: Although a
writable text section is allowed for PE-COFF targets, it does not conform to the format
specification published by Microsoft.
Let's give that a go:
$ gcc --static -g -Wl,--omagic -o test test.c
$ ./test
Hello world
$
That works fine, but you've lost dynamic library support.
To keep dynamic library support, and retain a writable text segment, you should be able to use:
objcopy --writable-text ...
From the man page:
--writable-text
Mark the output text as writable. This option isn't meaningful for all object file
formats.
This ought to work, but doesn't, as objdump will verify. So here's a solution that gets a bit further than --writable-text which as OP has stated in the comments does not appear to do what it says on the tin^Wmanpage.
Let's see how the sections are marked:
$ gcc -g -o test test.
$ objdump -h test | fgrep -A1 .text
12 .text 00000192 0000000000400490 0000000000400490 00000490 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
Now let's get rid of that READONLY flag:
$ objcopy --set-section-flags .text=contents,alloc,load,code test test1
$ objdump -h test1 | fgrep -A1 .text
12 .text 00000192 0000000000400490 0000000000400490 00000490 2**4
CONTENTS, ALLOC, LOAD, CODE
and now READONLY has gone, as requested.
But:
$ gdb test1
...
(gdb) run
Starting program: /home/amb/so/test1
Hello world
Program received signal SIGSEGV, Segmentation fault.
0x00000000004005a2 in main (argc=1, argv=0x7fffffffe628) at test.c:9
9 *((char *)m) = 0;
(gdb)
I suspect the issue here is that something else other than the ELF section name is making the section read-only when actually loaded. Which is probably why people are suggesting you use mprotect. Sorry not to have been more help.

Resources