Skip to content

Commit

Permalink
prepare to hide kernel symbols: expose libc symbols in certain files
Browse files Browse the repository at this point in the history
This 2nd patch in the series annotates number of standard glibc symbols
across number of source files, mostly under bsd, core, fs and libc
folders. This does not have any effect until we introduce the changes to
the makefile to support compiling kernel with most symbols hidden.

We choose to annotate the symbols that need to be exposed because the relevant files
will be compiled with the flag '-fvisibility=hidden' which would hide
all symbols unless annotated otherwise which takes precedence. This
approach helps to minimize the number of symbols that need to be annotated.
 certain files

Comparing to the version 1 of this patch, this one differs in the following
ways:
- removes changes made to bsd/sys/netinet/in.n, bsd/sys/rpc/xdr.h,
  bsd/sys/sys/md5.h and bsd/sys/sys/libkern.h to avoid exposing symbols
  originating from FreeBSD kernel that may not be completely compatible
  with Linux
- moves some of the OSV_*API annotation from header files to the source
  files

Signed-off-by: Waldemar Kozaczuk <jwkozaczuk@gmail.com>
  • Loading branch information
wkozaczuk committed Nov 28, 2021
1 parent 2df8652 commit 317d259
Show file tree
Hide file tree
Showing 32 changed files with 305 additions and 90 deletions.
7 changes: 4 additions & 3 deletions arch/aarch64/feexcept.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <sys/types.h>
#include <fenv.h>
#include <__fenv.h>
#include <osv/export.h>
// Note that musl's fenv.h does not define feenableexcept and friends, so
// we need to 'extern "C"' them here, as no header file does this.

Expand All @@ -55,7 +56,7 @@ typedef __uint64_t _fenv_t;
#define __mrs_fpsr(__r) __asm __volatile("mrs %0, fpsr" : "=r" (__r))
#define __msr_fpsr(__r) __asm __volatile("msr fpsr, %0" : : "r" (__r))

extern "C"
extern "C" OSV_LIBM_API
int feenableexcept(int mask)
{
_fenv_t __old_r, __new_r;
Expand All @@ -66,7 +67,7 @@ int feenableexcept(int mask)
return ((__old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT);
}

extern "C"
extern "C" OSV_LIBM_API
int fedisableexcept(int mask)
{
_fenv_t __old_r, __new_r;
Expand All @@ -77,7 +78,7 @@ int fedisableexcept(int mask)
return ((__old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT);
}

extern "C"
extern "C" OSV_LIBM_API
int fegetexcept()
{
_fenv_t __r;
Expand Down
4 changes: 4 additions & 0 deletions arch/x64/feexcept.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#include <osv/types.h>
#include <fenv.h>
#include <__fenv.h>
#include <osv/export.h>

OSV_LIBM_API
int feenableexcept(int mask)
{
// The feenableexcept() manual page suggests that -1 should be returned
Expand Down Expand Up @@ -38,6 +40,7 @@ int feenableexcept(int mask)
return ret;
}

OSV_LIBM_API
int fedisableexcept(int mask)
{
mask &= FE_ALL_EXCEPT;
Expand All @@ -56,6 +59,7 @@ int fedisableexcept(int mask)
return ret;
}

OSV_LIBM_API
int fegetexcept()
{
u16 cw;
Expand Down
37 changes: 19 additions & 18 deletions bsd/sys/kern/uipc_syscalls_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

#include <bsd/uipc_syscalls.h>
#include <osv/debug.h>
#include <osv/export.h>
#include "libc/af_local.h"

#include "libc/internal/libc.h"

#define sock_d(...) tprintf_d("socket-api", __VA_ARGS__);

extern "C"
extern "C" OSV_LIBC_API
int socketpair(int domain, int type, int protocol, int sv[2])
{
int error;
Expand All @@ -32,7 +33,7 @@ int socketpair(int domain, int type, int protocol, int sv[2])
return 0;
}

extern "C"
extern "C" OSV_LIBC_API
int getsockname(int sockfd, struct bsd_sockaddr *addr, socklen_t *addrlen)
{
int error;
Expand All @@ -49,7 +50,7 @@ int getsockname(int sockfd, struct bsd_sockaddr *addr, socklen_t *addrlen)
return 0;
}

extern "C"
extern "C" OSV_LIBC_API
int getpeername(int sockfd, struct bsd_sockaddr *addr, socklen_t *addrlen)
{
int error;
Expand All @@ -66,7 +67,7 @@ int getpeername(int sockfd, struct bsd_sockaddr *addr, socklen_t *addrlen)
return 0;
}

extern "C"
extern "C" OSV_LIBC_API
int accept4(int fd, struct bsd_sockaddr *__restrict addr, socklen_t *__restrict len, int flg)
{
int fd2, error;
Expand All @@ -83,7 +84,7 @@ int accept4(int fd, struct bsd_sockaddr *__restrict addr, socklen_t *__restrict
return fd2;
}

extern "C"
extern "C" OSV_LIBC_API
int accept(int fd, struct bsd_sockaddr *__restrict addr, socklen_t *__restrict len)
{
int fd2, error;
Expand All @@ -100,7 +101,7 @@ int accept(int fd, struct bsd_sockaddr *__restrict addr, socklen_t *__restrict l
return fd2;
}

extern "C"
extern "C" OSV_LIBC_API
int bind(int fd, const struct bsd_sockaddr *addr, socklen_t len)
{
int error;
Expand All @@ -117,7 +118,7 @@ int bind(int fd, const struct bsd_sockaddr *addr, socklen_t len)
return 0;
}

extern "C"
extern "C" OSV_LIBC_API
int connect(int fd, const struct bsd_sockaddr *addr, socklen_t len)
{
int error;
Expand All @@ -134,7 +135,7 @@ int connect(int fd, const struct bsd_sockaddr *addr, socklen_t len)
return 0;
}

extern "C"
extern "C" OSV_LIBC_API
int listen(int fd, int backlog)
{
int error;
Expand All @@ -151,7 +152,7 @@ int listen(int fd, int backlog)
return 0;
}

extern "C"
extern "C" OSV_LIBC_API
ssize_t recvfrom(int fd, void *__restrict buf, size_t len, int flags,
struct bsd_sockaddr *__restrict addr, socklen_t *__restrict alen)
{
Expand All @@ -171,7 +172,7 @@ ssize_t recvfrom(int fd, void *__restrict buf, size_t len, int flags,
return bytes;
}

extern "C"
extern "C" OSV_LIBC_API
ssize_t recv(int fd, void *buf, size_t len, int flags)
{
int error;
Expand All @@ -189,7 +190,7 @@ ssize_t recv(int fd, void *buf, size_t len, int flags)
return bytes;
}

extern "C"
extern "C" OSV_LIBC_API
ssize_t recvmsg(int fd, struct msghdr *msg, int flags)
{
ssize_t bytes;
Expand All @@ -207,7 +208,7 @@ ssize_t recvmsg(int fd, struct msghdr *msg, int flags)
return bytes;
}

extern "C"
extern "C" OSV_LIBC_API
ssize_t sendto(int fd, const void *buf, size_t len, int flags,
const struct bsd_sockaddr *addr, socklen_t alen)
{
Expand All @@ -227,7 +228,7 @@ ssize_t sendto(int fd, const void *buf, size_t len, int flags,
return bytes;
}

extern "C"
extern "C" OSV_LIBC_API
ssize_t send(int fd, const void *buf, size_t len, int flags)
{
int error;
Expand All @@ -245,7 +246,7 @@ ssize_t send(int fd, const void *buf, size_t len, int flags)
return bytes;
}

extern "C"
extern "C" OSV_LIBC_API
ssize_t sendmsg(int fd, const struct msghdr *msg, int flags)
{
ssize_t bytes;
Expand All @@ -263,7 +264,7 @@ ssize_t sendmsg(int fd, const struct msghdr *msg, int flags)
return bytes;
}

extern "C"
extern "C" OSV_LIBC_API
int getsockopt(int fd, int level, int optname, void *__restrict optval,
socklen_t *__restrict optlen)
{
Expand All @@ -281,7 +282,7 @@ int getsockopt(int fd, int level, int optname, void *__restrict optval,
return 0;
}

extern "C"
extern "C" OSV_LIBC_API
int setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
{
int error;
Expand All @@ -299,7 +300,7 @@ int setsockopt(int fd, int level, int optname, const void *optval, socklen_t opt
return 0;
}

extern "C"
extern "C" OSV_LIBC_API
int shutdown(int fd, int how)
{
int error;
Expand All @@ -322,7 +323,7 @@ int shutdown(int fd, int how)
return 0;
}

extern "C"
extern "C" OSV_LIBC_API
int socket(int domain, int type, int protocol)
{
int s, error;
Expand Down
10 changes: 9 additions & 1 deletion bsd/sys/xen/xenstore/xenstore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ __FBSDID("$FreeBSD$");

#include <xen/xenstore/xenstorevar.h>
#include <xen/xenstore/xenstore_internal.h>
#include <osv/export.h>

#include <vm/vm.h>
#include <vm/pmap.h>
Expand Down Expand Up @@ -1316,6 +1317,7 @@ class xs_sbuf {

/*-------------------------------- Public API --------------------------------*/
/*------- API comments for these methods can be found in xenstorevar.h -------*/
OSV_LIBXENSTORE_API
const char **
xs_directory(void *op, struct xs_transaction t, const char *path, u_int *num)
{
Expand Down Expand Up @@ -1360,6 +1362,7 @@ xs_exists(struct xs_transaction t, const char *dir, const char *node)
return (1);
}

OSV_LIBXENSTORE_API
char *
xs_read(void *op, struct xs_transaction t, const char *path, u_int *len)
{
Expand Down Expand Up @@ -1387,6 +1390,7 @@ xs_read(struct xs_transaction t, const char *dir, const char *node,
return 0;
}

OSV_LIBXENSTORE_API
int
xs_write(void *op, struct xs_transaction t, const char *path, const char *string)
{
Expand All @@ -1403,11 +1407,13 @@ xs_write(void *op, struct xs_transaction t, const char *path, const char *string
return (error);
}

OSV_LIBXENSTORE_API
void *xs_daemon_open()
{
return (void *)-1;
}

OSV_LIBXENSTORE_API
void xs_close(void *h)
{
}
Expand All @@ -1433,7 +1439,7 @@ xs_mkdir(struct xs_transaction t, const char *dir, const char *node)
return (ret);
}


OSV_LIBXENSTORE_API
int
xs_rm(void *op, struct xs_transaction t, const char *path)
{
Expand Down Expand Up @@ -1562,6 +1568,7 @@ xs_transaction_start(struct xs_transaction *t)
return (error);
}

OSV_LIBXENSTORE_API
int
xs_transaction_start(void *h)
{
Expand All @@ -1583,6 +1590,7 @@ xs_transaction_end(struct xs_transaction t, int abort)
return (xs_single(t, XS_TRANSACTION_END, abortstr, NULL, NULL));
}

OSV_LIBXENSTORE_API
int
xs_transaction_end(void *h, struct xs_transaction t, int abort)
{
Expand Down
5 changes: 4 additions & 1 deletion core/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <osv/run.hh>
#include <osv/power.hh>
#include <osv/trace.hh>
#include <osv/export.h>
#include <functional>
#include <thread>
#include <libgen.h>
Expand All @@ -26,9 +27,11 @@ extern int optind;

// Java uses this global variable (supplied by Glibc) to figure out
// aproximatively where the initial thread's stack end.
OSV_LD_LINUX_x86_64_API
void *__libc_stack_end;

extern "C" void __libc_start_main(int (*main)(int, char**), int, char**,
extern "C" OSV_LIBC_API
void __libc_start_main(int (*main)(int, char**), int, char**,
void(*)(), void(*)(), void(*)(), void*)
{
auto app = osv::application::get_current();
Expand Down
6 changes: 4 additions & 2 deletions core/elf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <osv/stubbing.hh>
#include <sys/utsname.h>
#include <osv/demangle.hh>
#include <osv/export.h>
#include <boost/version.hpp>
#include <deque>

Expand Down Expand Up @@ -1325,6 +1326,7 @@ program::program(void* addr)
"libaio.so.1",
"libxenstore.so.3.0",
"libcrypt.so.1",
"libutil.so",
};
auto ml = new modules_list();
ml->objects.push_back(_core.get());
Expand Down Expand Up @@ -1876,7 +1878,7 @@ char *object::setup_tls()
_module_index, _tls_segment, _tls_init_size, _tls_uninit_size);
}

extern "C"
extern "C" OSV_LD_LINUX_x86_64_API
void* __tls_get_addr(module_and_offset* mao)
{
#ifdef AARCH64_PORT_STUB
Expand All @@ -1901,7 +1903,7 @@ void* __tls_get_addr(module_and_offset* mao)
// also uses a static area for uname, we can just return that.
extern utsname utsname;

extern "C"
extern "C" OSV_LIBC_API
unsigned long getauxval(unsigned long type)
{
switch (type) {
Expand Down
Loading

0 comments on commit 317d259

Please sign in to comment.