/*
 * stat_shim.c
 * Cross-compilation fix: glibc's libc_nonshared.a has 'stat' as a hidden
 * symbol which causes "final link failed: bad value" when a DSO (libvmf.so)
 * references it. We provide stat as a visible symbol using a direct syscall
 * so it works with both glibc (build host) and uClibc (device runtime).
 */
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/stat.h>
#include <errno.h>

/* ARM32: __NR_stat (106) returns old struct kernel_stat with 16-bit fields.
 * glibc's struct stat is the 64-bit layout (same as struct stat64).
 * Use __NR_stat64 (195) so the kernel fills the 64-bit layout that matches. */
#ifndef __NR_stat64
#define __NR_stat64 195
#endif

__attribute__((visibility("default")))
int stat(const char *path, struct stat *buf)
{
    return (int)syscall(__NR_stat64, path, buf);
}

/* stat64: explicit 64-bit stat. Use this (not stat) when struct stat64 is declared
 * so that the kernel's 96-byte write matches the caller's buffer size. */
__attribute__((visibility("default")))
int stat64(const char *path, struct stat64 *buf)
{
    return (int)syscall(__NR_stat64, path, buf);
}

/* ARM32: fstat via fstat64 syscall (197).
 * glibc emits __fxstat(3, fd, buf) for fstat() calls; uClibc has no __fxstat. */
#ifndef __NR_fstat64
#define __NR_fstat64 197
#endif

__attribute__((visibility("default")))
int __fxstat(int ver, int fd, struct stat *buf)
{
    (void)ver;
    return (int)syscall(__NR_fstat64, fd, buf);
}
