clone system call OS X not linking - undefined symbols [duplicate] - c

This question already has answers here:
Where is clone() method in sched.h on Mac OS X
(2 answers)
Closed 9 years ago.
I would like to use the clone system call on OS X. It's a Unix system call so it shouldn't be a problem, right? I have successfully tried using fork, vfork and other similar functions. Here is the program I'm trying:
#include <sched.h> //Clone resides here
#include <stdlib.h> //standard library
#include <stdio.h>
#include <time.h>
#include <limits.h>
#include <sys/shm.h>
#include <errno.h>
#include <sys/sem.h>
#include <signal.h>
#include <sys/types.h>
int helloWorld();
int main(int argc, char *argv[])
{
int (*functionPointer)() = &helloWorld; //The first argument of clone accepts a pointer to a function which must return int
void **childStack = (void**)malloc(1024); //We will give our child 1kB of stack space
clone(functionPointer, childStack, 0, NULL); //First arugment is the function to be called, second one is our stack, CLONE_VM means to share memory, last NULL PID description
return 0;
}
int helloWorld()
{
printf("Hello (clone) world!\r\n");
return 0;
}
Compiling with gcc -o test my_file.c gives:
Undefined symbols for architecture x86_64:
"_clone", referenced from:
_main in ccG3qOjx.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Feel free to ignore the comments, since I'm just learning. And one more thing.. if I try to pass CLONE_VM in the arguments it doesn't even compile giving me the error:
my_file.c: In function ‘main’:
my_file.c:12: error: ‘CLONE_VM’ undeclared (first use in this function)
my_file.c:12: error: (Each undeclared identifier is reported only once
my_file.c:12: error: for each function it appears in.)
Am I missing an #include? If so, which one?
What am I doing wrong and how to fix it?

clone is specific to Linux, so for OS X you're stuck with fork, or use threads if you can manage with that.

Related

Undefined symbol "_clone" on OS X

Code:
#include <stdio.h>
#include <sched.h>
#include <stdlib.h>
#include <sys/wait.h>
#define _GNU_SOURCE
void *stack_memory()
{
const int stackSize = 65536;
void* stack = (void*)malloc(stackSize);
if (stack == NULL) {
printf("%s\n", "Cannot allocate memory \n");
exit(EXIT_FAILURE);
}
return stack;
}
int jail(void *args)
{
printf("Hello !! - child \n");
return EXIT_SUCCESS;
}
int main()
{
printf("%s\n", "Hello, world! - parent");
clone(jail, stack_memory(), SIGCHLD, 0);
return EXIT_SUCCESS;
}
Error:
Undefined symbols for architecture x86_64: "_clone", referenced
from:
_main in docker-4f3ae8.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code
1 (use -v to see invocation)
Linux doesn't prefix symbols with a leading _ so you're not using Linux.
But the clone(2) system call is Linux-specific, according to its man page.
clone() is Linux-specific and should not be used in programs intended
to be portable.
Probably you're using OS X or something. And you're compiling as C, so calling an un-declared function isn't a compile-time error (just a big warning). This is why it's a linker error instead of a compile-time error (and you ignored compiler warnings.)
And BTW, #define _GNU_SOURCE after including header files is pointless. You have to define feature-request macros before including headers to get them to define prototypes for GNU-only functions in cases where that's not already the default.

How do calls to the execvp system call work?

I was doing a research into the contents of another StackOverflow question and I thought it was a good time to brush up my knowledge of unix system calls.
While experimenting with execvp (WITHOUT fork on purpose) I ran into something that confuses me
I wrote 4 test programs
Program 1
#include <stdio.h>
int main() {
//printf("Doge\n");
execvp("ls");
printf("Foo\n");
return 0;
}
The program works as expected, the contents of the directory are printed and the Foo print statement is not
Program 2
However when I uncomment the first print statement and have the program be this
#include <stdio.h>
int main() {
printf("Doge\n");
execvp("ls");
printf("Foo\n");
return 0;
}
execvp returns a -1 and both print statements are issued. why?
Program 3
I vaguely remember having to use unistd.h when experimenting with unix system calls from college.
So I included it, but not execvp has a different signature and it needed some more args than just the name of the program. So I did this
#include <stdio.h>
#include <unistd.h>
int main() {
printf("Doge\n");
char *const parmList[] = {"ls", NULL};
execvp("ls", parmList);
printf("Foo\n");
return 0;
}
And this works. This has confused me. Why did exec work in the first program?
I also used This as a reference to the system calls.
Finally I wrote
Program 4
#include <stdio.h>
//#include <unistd.h>
int main() {
printf("Doge\n");
char *const parmList[] = {"ls", NULL};
execvp("ls", parmList);
printf("Foo\n");
return 0;
}
Which also works as expected.
Can someone explain what's going on?
With this snippet
#include <stdio.h>
int main() {
execvp("ls");
printf("Foo\n");
return 0;
}
you're invoking undefined behaviour. You're not providing the prototype for execvp which requires an argument list (null terminated) as a second parameter.
Using gcc without any warning option silently uses execvp as implicitly declared, and doesn't check parameters. It just calls the function. The function then looks for a second parameter and encounters... whatever is left of the call stack (or registers, depending on call conventions), that's why a previous printf call can change the behaviour.
Using gcc -Wall gives the following warning:
test.c:5:9: warning: implicit declaration of function 'execvp' [-Wimplicit-function-declaration]
execvp("ls");
Including the proper include (#include <unistd.h>) leads to:
test.c:6:9: error: too few arguments to function 'execvp'
execvp("ls");
^~~~~~
That's why you've got strange behaviour. Don't look further. Use execvp with 2 arguments, period. In your case "Program 3" is the way to go, and always set warning level to the maximum, if possible (gcc and clang: -Wall -Wextra -pedantic -Werror)

Undefined symbol when calling from Lua

I have a lib, called "test.so" that uses functions from two libraries that reference each other. If I call the functions of test.so within a C program, it works just fine, so I assume there's no error in C functions.
However, when I call it from Lua, it throws a Seg Fault error due to an "undefined symbol". The problem is, the symbol is defined, I can see it when I run nm test.so.
Reading the thread below
Lua: C++ modules can't reference eachother, undefined symbol
I tried to creating a new module that would load test.so using dlopen, as described by Rena.
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdio.h>
#include <dlfcn.h>
int luaopen_drmaa(lua_State *L){
printf("Chamou luaopen_test\n");
if( dlopen("/home/erica/Downloads/test.so", RTLD_NOW | RTLD_GLOBAL) == NULL)
printf("%s\n", dlerror());
else
printf("Chamou dlopen e teve retorno nao null\n");
return 0;
}
I compiled it:
gcc -g drmaa_lib.c -shared -fpic -I /usr/include/lua5.1 -I /usr/local/include/ -L /usr/local/lib/m -llua5.1 -o drmaa.so
But when I run, I get:
$ lua5.1
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> require "drmaa"
Chamou luaopen_test
Chamou dlopen e teve retorno nao null
> submitJob()
stdin:1: attempt to call global 'submitJob' (a nil value)
stack traceback:
stdin:1: in main chunk
[C]: ?
Then I tried inserting in main function
luaopen_test(L);
and above the main function
extern int luaopen_test(lua_State *L);
to actually open the lib, but I get this error:
$ lua5.1
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> require "drmaa"
error loading module 'drmaa' from file './drmaa.so':
./drmaa.so: undefined symbol: luaopen_test
stack traceback:
[C]: ?
[C]: in function 'require'
stdin:1: in main chunk
[C]: ?
The author of the referenced question doesn't show details of the development of the solution.
Does anyone has a clue of what may make it work?
Thanks in advance.
I was obviously doing it wrong, to get the function that actually opens the library I should have used ldsym function. The new code for drmaa lib (the one that loads test.so) is
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdio.h>
#include <dlfcn.h>
typedef void Register(lua_State*);
int luaopen_drmaa(lua_State *L){
void* lib = dlopen("/home/erica/Downloads/test.so", RTLD_NOW | RTLD_GLOBAL);
if(!lib)
{
printf("%s\n", dlerror());
return 1;
}
Register* loadFunc = (Register*)dlsym(lib, "luaopen_test");
if(!loadFunc)
{
printf("%s\n", dlerror());
return 1;
}
loadFunc(L);
return 0;
}
This answer was base in some code of this thread:
Lua shared object loading with C++ segfaults

C - Unable to get shmget/shmat to compile

I'm pulling out my hair trying to figure out why this isn't working on my Minix system.
When I try to compile my C program, I get the following error:
#make
link pm/pm
program.o: In function `do_function':
program.c:(.text+0x1e): undefined reference to `shmget'
program.c:(.text+0x36): undefined reference to `shmat'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
*** Error code 1
Stop.
make: stopped in /usr/src/servers/pm
#
This is my code:
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/stat.h>
/* Some other includes here */
typedef struct {
//struct elemLi *next;
//elem sem;
int ref_count;
} elemLi;
int do_function(void){
int segment_id; //Shared Memory ID
struct elemLi* sli; //Shared Memory Pointer
segment_id = shmget(IPC_PRIVATE, sizeof(elemLi),0660 | IPC_CREAT);
sli = (struct elemLi *)shmat(segment_id,NULL,0);
return -1;
}
As you can see, I've included the proper header files for these calls and it's still saying the references are undefined. I was able to successfully use this in another program, so I've completely run out of ideas as to why this isn't working in this instance.
Edit: This is within a system call. I assume that doesn't make a difference.
Thanks for the help everyone, it looks like it was a missing library in the linker. I added libc, using -lc, to the Makefile, and now it seems to be compiling fine.

LD_PRELOAD not working with my program

For testing LD_PRELOAD, I wrote my own getpid, which prints something before calling the original getpid using dlsym. The code is given below.
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <dlfcn.h>
typedef pid_t (*getpidType)(void);
pid_t getpid(void)
{
printf("Hello, getpid!\n");
getpidType f = (getpidType)dlsym(RTLD_NEXT, "getpid");
return f();
}
However when I use such getpid in my program and run it using LD_PRELOAD, by typing LD_PRELOAD=./prelib.so ./prog, I get the following error.
./prog: symbol lookup error: ./prelib.so: undefined symbol: dlsym
But If I do LD_PRELOAD=./prelib.so bash -c 'echo $$', there is no such error. Any idea what is going on here.
Linking it with libdl.so.2 by using -ldl in the makefile solved the problem.

Resources