I am running busybox v1.27.2 on an embedded linux system. To test my userspace build environment, I have cross compiled a simple hello-world application titled "hello". The system does not have library files available, so I have statically linked with uClibc. I have confirmed the binary was built correct using file:
hello: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, not stripped
when I try and execute from target rootfs, I get the following:
/ # ./hello
hello: applet not found
I have tried executing from /usr/bin and other directories, result is the same. I understand this message can occur when symlinks are not correctly pointing to busybox binary. However I am confused as this application should not depend on busybox. Any help would be appreciated.
Here is the code for reference:
// C library headers
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
printf("hello world");
return 0;
}
Fixed this buy re-compiling uClibc & "hello" binary with arm-buildroot-uclinux-uclibcgnueabi-gcc toolchain from buildroot
Related
i'm currently in programming studies and do CTFs in my spare time to get a few extra skills.
Right now i'm facing a binary (i dont have the source code but could ghidra it) which:
putchar a single char
clock_nanosleep for X seconds
putchar a single char
clock_nanosleep for Y seconds
...
(I've strace the binary and saw that the binary uses "clock_nanosleep" with "CLOCK_REALTIME")
My question is: Is there a way to execute the binary while skipping the "clock_nanosleep" ?
My guesses:
Maybe block him the time.h lib so he cant execute "clock_nanosleep" ?
Maybe modify the "CLOCK_REALTIME" so that the program thinks the time have passed and finishes the "clock_nanosleep" ?
Extra informations:
File : ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=5eca63735d90098f20f45ca172e242372994976e, for GNU/Linux 3.2.0, stripped
Strings : /lib64/ld-linux-x86-64.so.2 libc.so.6
You can LD_PRELOAD to neuter the clock_nanosleep call:
#include <time.h>
int clock_nanosleep(clockid_t clockid, int flags,
const struct timespec *request,
struct timespec *remain) {
return 0;
}
compile with
> gcc inject.c -shared -fPIC -o inject.so
and run with
> LD_PRELOAD=$PWD/inject.so ./program
this will effectively disable calls to clock_nanosleep by making them return immediately.
If you don't want to run your program under LD_PRELOAD, you can get creative with solutions - for instance, patch every opcode that calls clock_nanosleep with nops, which is automatable with Ghidra scripting.
I am trying to get a minimal example of using Go code in C to work, such as this. I am struggling with the following compilation error message:
ld: warning: ignoring file
/Users/username/gocode/src/example/example.dylib, file was built for
archive which is not the architecture being linked (x86_64):
/Users/username/gocode/src/example/example.dylib
My attempt is the following. I have a simple Go package in a file main.go:
package example
import "C"
//export GoEcho
func GoEcho(s *C.char) string {
return C.GoString(s)
}
func main() {}
which I then compile using either
go build -buildmode=c-archive -o example.dylib main.go
or
go build -buildmode=c-shared -o example.dylib main.go
or
GOARCH=amd64 go build -buildmode=c-shared -o LibraryLinkExamples.dylib main.go
The operating system is OS X 10.11 and the the Go version is, as go version puts it, go1.9 darwin/amd64.
The C code I'm using is the following:
#include "example.h"
#include <stdio.h>
int main(int argc, char const *argv[])
{
GoString res = GoEcho("test");
printf("%.*s\n", (int)res.n, res.p);
return 0;
}
I don't understand why I am getting the "which is not the architecture being linked (x86_64)" error message when I am building the library and using it on the same operating system, even the computer? What can explain this?
Update
In the official documentation here it says:
amd64 (also known as x86-64)
meaning it should be x86-64 compatible with this setting. That it isn't is possibly a bug in go build?
I have a Dreambox 500 which on Wikipedia says has a PCP processor which is PowerPC:
$ cat /proc/cpuinfo
processor: 0
cpu: STBx25xx
clock: 252MHz
Review: 9.80 (pvr 5151 0950)
bogomips: 250.36
Machine: Dream Multimedia Dreambox TV
plb bus clock: 63MHz
I would normally install GCC but it has low storage on it and I need to compile a program for it.
I've heard GCC can compile powerpc but I had no luck doing so.
Example this code
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
And I use this to compile
gcc example.c -mtune=powerpc
But it give this error
example.c:1:0 error: bad value (powerpc) for -mtune- switch
#include <stdio.h>
^
Thank you!
You should use cross-compiler, because your target architecture differs from host one. Host is the architecture of your system (usually amd64 (x86_64) or i386 (x86_32)). And target arch is the arch on which your compiled program will run (powerpc in your case).
Many GNU/Linux distors provide crosscompilers as a separate packages. For example, for Ubuntu these packages are available:
sudo apt-get install gcc-4.8-powerpc-linux-gnu g++-4.8-powerpc-linux-gnu binutils-4.8-powerpc-linux-gnu
Packages above are for trusty. In later releases different GCC versions are available.
Then you can compile your program using powerpc-linux-gnu-gcc-4.8. Or you can set your environment variables CC and CXX to powerpc-linux-gnu-gcc-4.8 and powerpc-linux-gnu-g++-4.8 accordingly.
upd:
I found crosscompiler toolchain for Dreambox 500 here, but it contains relatively old GCC (3.4).
In order to use it extract downloaded file to /opt/cross/dm500, add /opt/cross/dm500/cdk/bin to path via export PATH=$PATH:/opt/cross/dm500/cdk/bin and use gcc from here with appropriate prefix.
After being on a programming forum for a while, found a guy with the same problem, and after a while he found a way to fix it and I tried it and it works.
The thing I have to do is
powerpc-gcc someprog.c -static
I have no idea what the -static does but it increases the executable file size and at the end it works!
I want to do some modifications to the glibc library. The first step is to be able to use a specific version when I compile a program. I'm under ubuntu 12.10 and my directories are :
/mydirectory/glibc-2.17 (where I have extracted the last version from the website)
/mydirectory/glibc-2.17-build (where I have executed the configure and make command)
/mydirectory/test/helloworld.c (where I have my helloworld program)
The helloworld.c is the following:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
char glibc[256] = "xxxx"; /* How to detect the glibc version here ? */
printf("hello, world\n");
printf("glibc version = %s\n", glibc);
return 0;
}
First how can I print the version of glibc ? (I think that there is a macro/constant in glibc for that).
Second, what command line should I use to compile my helloworld.c file to use the glibc that is in /mydirectory/glibc-2.17-build ?
Use -L pathname to explicitly specify a pathname to ld as Barmar has said in the comment.
It's suggested to use static linking -static or there might be problems during execution I think.
Actually my own solution to this problem would be: compile and link the source code as normal, and invoke with LD_PRELOAD set to your specified version of shared objects.
See http://linux.die.net/man/8/ld.so
Looking into learning C. As I understand it when I say #include <stdio.h> it grabs stdio.h from the default location...usually a directory inside your working directory called include. How do I actually get the file stdio.h? Do I need to download a bunch of .h files and move them from project to project inside the include directory? I did the following in a test.c file. I then ran make test and it outputted a binary. When I ran ./test I did not see hello print onto my screen. I thought I wasn't seeing output maybe because it doesn't find the stdio.h library. But then again if I remove the greater than or less than signs in stdio the compiler gives me an error. Any ideas?
I'm on a Mac running this from the command line. I am using: GNU Make 3.81. This program built for i386-apple-darwin10.0
#include <stdio.h>
main()
{
printf("hello");
}
Edit: I have updated my code to include a datatype for the main function and to return 0. I still get the same result...compiles without error and when I run the file ./test it doesn't print anything on screen.
#include <stdio.h>
int main()
{
printf("hello");
return 0;
}
Update:
If I add a \n inside of the printf it works! so this will work:
#include <stdio.h>
int main()
{
printf("hello\n");
return 0;
}
Your code should have preferably
printf("hello\n");
or
puts("hello");
If you want to know where does the standard header file <stdio.h> comes from, you could run your compiler with appropriate flags. If it is gcc, try compiling with
gcc -H -v -Wall hello.c -o hello
Pedantically, a standard header file is even not required to exist as a file; the standard permits an implementation which would process the #include <stdio.h> without accessing the file system (but e.g. by retrieving internal resources inside the compiler, or from a database...). Few compilers behave that way, most really access something in the file system.
If you didn't have the file, you'd get a compilation error.
My guess is the text was printed, but the console closed before you got the chance to see it.
Also, main returns an int, and you should return 0; to signal successful completion.
#include <header.h>, with angle brackets, searches in standard system locations, known to the compiler-- not in your project's subdirectories. In Unix systems (including your Mac, I believe), stdio.h is typically in /usr/include. If you use #include "header.h", you're searching subdirectories first and then the same places as with <header.h>.
But you don't need to find or copy the header to run your program. It is read at compilation time, so your ./test doesn't need it at all. Your program looks like it should have worked. Is it possible that you just typed "test", not "./test", and got the system command "test"? (Suggestion: Don't name your programs "test".)
Just going to leave this here : STILL! in 2018, December... Linux Mint 18.3
has no support for C development.
innocent / # cc ThoseSorts.c
ThoseSorts.c:1:19: fatal error: stdio.h: No such file or directory
compilation terminated.
innocent / # gcc ThoseSorts.c
ThoseSorts.c:1:19: fatal error: stdio.h: No such file or directory
compilation terminated.
innocent / # apt show libc6
(Abbreviated)::
Package: libc6
Version: 2.23-0ubuntu10
Priority: required
Section: libs
Source: glibc
Origin: Ubuntu
Installed-Size: 11.2 MB
Depends: libgcc1
Homepage: http://www.gnu.org/software/libc/libc.html
Description: GNU C Library: Shared libraries
Contains the standard libraries that are used by nearly all programs on
the system. This package includes shared versions of the standard C library
and the standard math library, as well as many others.
innocent / # apt-get install libc6-dev libc-dev
So, magic... and a minute later they are all installed on the
computer and then things work as they should.
Not all distros bundle up all the C support libs in each ISO.
Hunh.
hardlyinnocent / # gcc ThoseSorts.c
hardlyinnocent / # ./a.out
20
18
17
16
... ... ...