Skip to content

Commit

Permalink
[libc] add headers sys/auxv sys/prctl and sys/time
Browse files Browse the repository at this point in the history
These headers are uncommonly used, and from extensions, but some basic
support is needed. Macros have been added where available.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D135731
  • Loading branch information
michaelrj-google committed Oct 12, 2022
1 parent bb70696 commit 35a4fe4
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 0 deletions.
4 changes: 4 additions & 0 deletions libc/config/linux/x86_64/headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ set(TARGET_PUBLIC_HEADERS
libc.include.pthread
libc.include.sched
libc.include.signal
libc.include.spawn
libc.include.stdio
libc.include.stdlib
libc.include.string
libc.include.threads
libc.include.time
libc.include.unistd

libc.include.sys_auxv
libc.include.sys_ioctl
libc.include.sys_mman
libc.include.sys_prctl
libc.include.sys_random
libc.include.sys_resource
libc.include.sys_stat
libc.include.sys_syscall
libc.include.sys_time
libc.include.sys_utsname
libc.include.sys_wait
)
27 changes: 27 additions & 0 deletions libc/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ add_gen_header(
# them.
file(MAKE_DIRECTORY "sys")

add_gen_header(
sys_auxv
DEF_FILE sys/auxv.h.def
GEN_HDR sys/auxv.h
DEPENDS
.llvm_libc_common_h
.llvm-libc-macros.sys_auxv_macros
)

add_gen_header(
sys_ioctl
DEF_FILE sys/ioctl.h.def
Expand All @@ -248,6 +257,14 @@ add_gen_header(
.llvm-libc-macros.sys_mman_macros
)

add_gen_header(
sys_prctl
DEF_FILE sys/prctl.h.def
GEN_HDR sys/prctl.h
DEPENDS
.llvm_libc_common_h
)

add_gen_header(
sys_random
DEF_FILE sys/random.h.def
Expand Down Expand Up @@ -301,6 +318,16 @@ add_gen_header(
../config/${LIBC_TARGET_OS}/syscall_numbers.h.inc
)

add_gen_header(
sys_time
DEF_FILE sys/time.h.def
GEN_HDR sys/time.h
DEPENDS
.llvm_libc_common_h
.llvm-libc-types.struct_timeval
.llvm-libc-macros.sys_time_macros
)

add_gen_header(
sys_utsname
DEF_FILE sys/utsname.h.def
Expand Down
14 changes: 14 additions & 0 deletions libc/include/llvm-libc-macros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ add_header(
stdlib-macros.h
)

add_header(
sys_auxv_macros
HDR
sys-auxv-macros.h
)

add_header(
sys_ioctl_macros
HDR
Expand Down Expand Up @@ -77,6 +83,14 @@ add_header(
.linux.sys_resource_macros
)

add_header(
sys_time_macros
HDR
sys-time-macros.h
DEPENDS
.linux.sys_time_macros
)

add_header(
sys_wait_macros
HDR
Expand Down
6 changes: 6 additions & 0 deletions libc/include/llvm-libc-macros/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ add_header(
sys-stat-macros.h
)

add_header(
sys_time_macros
HDR
sys-time-macros.h
)

add_header(
unistd_macros
HDR
Expand Down
53 changes: 53 additions & 0 deletions libc/include/llvm-libc-macros/linux/sys-time-macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//===-- Definition of macros from sys/time.h ------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef __LLVM_LIBC_MACROS_LINUX_SYS_TIME_MACROS_H
#define __LLVM_LIBC_MACROS_LINUX_SYS_TIME_MACROS_H

// Add two timevals and put the result in timeval_ptr_result. If the resulting
// usec value is greater than 999,999 then the microseconds are turned into full
// seconds (1,000,000 is subtracted from usec and 1 is added to sec).
#define timeradd(timeval_ptr_a, timeval_ptr_b, timeval_ptr_result) \
(timeval_ptr_result)->tv_sec = \
(timeval_ptr_a)->tv_sec + (timeval_ptr_b)->tv_sec + \
(((timeval_ptr_a)->tv_usec + (timeval_ptr_b)->tv_usec) >= 1000000 ? 1 \
: 0); \
(timeval_ptr_result)->tv_usec = \
(timeval_ptr_a)->tv_usec + (timeval_ptr_b)->tv_usec - \
(((timeval_ptr_a)->tv_usec + (timeval_ptr_b)->tv_usec) >= 1000000 \
? 1000000 \
: 0);

// Subtract two timevals and put the result in timeval_ptr_result. If the
// resulting usec value is less than 0 then 1,000,000 is added to usec and 1 is
// subtracted from sec.
#define timersub(timeval_ptr_a, timeval_ptr_b, timeval_ptr_result) \
(timeval_ptr_result)->tv_sec = \
(timeval_ptr_a)->tv_sec - (timeval_ptr_b)->tv_sec - \
(((timeval_ptr_a)->tv_usec - (timeval_ptr_b)->tv_usec) < 0 ? 1 : 0); \
(timeval_ptr_result)->tv_usec = \
(timeval_ptr_a)->tv_usec - (timeval_ptr_b)->tv_usec + \
(((timeval_ptr_a)->tv_usec - (timeval_ptr_b)->tv_usec) < 0 ? 1000000 \
: 0);

// Reset a timeval to the epoch.
#define timerclear(timeval_ptr) \
(timeval_ptr)->tv_sec = 0; \
(timeval_ptr)->tv_usec = 0;

// Determine if a timeval is set to the epoch.
#define timerisset(timeval_ptr) \
(timeval_ptr)->tv_sec != 0 || (timeval_ptr)->tv_usec != 0;

// Compare two timevals using CMP.
#define timercmp(timeval_ptr_a, timeval_ptr_b, CMP) \
(((timeval_ptr_a)->tv_sec == (timeval_ptr_b)->tv_sec) \
? ((timeval_ptr_a)->tv_usec CMP(timeval_ptr_b)->tv_usec) \
: ((timeval_ptr_a)->tv_sec CMP(timeval_ptr_b)->tv_sec))

#endif // __LLVM_LIBC_MACROS_LINUX_SYS_TIME_MACROS_H
43 changes: 43 additions & 0 deletions libc/include/llvm-libc-macros/sys-auxv-macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===-- Macros defined in sys/auxv.h header file --------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef __LLVM_LIBC_MACROS_AUXV_MACROS_H
#define __LLVM_LIBC_MACROS_AUXV_MACROS_H

// Macros defining the aux vector indexes.
#define AT_NULL 0
#define AT_IGNORE 1
#define AT_EXECFD 2
#define AT_PHDR 3
#define AT_PHENT 4
#define AT_PHNUM 5
#define AT_PAGESZ 6
#define AT_BASE 7
#define AT_FLAGS 8
#define AT_ENTRY 9
#define AT_NOTELF 10
#define AT_UID 11
#define AT_EUID 12
#define AT_GID 13
#define AT_EGID 14
#define AT_PLATFORM 15
#define AT_HWCAP 16
#define AT_CLKTCK 17

#define AT_SECURE 23
#define AT_BASE_PLATFORM 24
#define AT_RANDOM 25
#define AT_HWCAP2 26

#define AT_EXECFN 31

#ifndef AT_MINSIGSTKSZ
#define AT_MINSIGSTKSZ 51
#endif

#endif // __LLVM_LIBC_MACROS_AUXV_MACROS_H
16 changes: 16 additions & 0 deletions libc/include/llvm-libc-macros/sys-time-macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//===-- Macros defined in sys/time.h header file --------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef __LLVM_LIBC_MACROS_SYS_TIME_MACROS_H
#define __LLVM_LIBC_MACROS_SYS_TIME_MACROS_H

#ifdef __unix__
#include "linux/sys-time-macros.h"
#endif

#endif // __LLVM_LIBC_MACROS_SYS_TIME_MACROS_H
18 changes: 18 additions & 0 deletions libc/include/sys/auxv.h.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- GNU header auxv.h -------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SYS_AUXV_H
#define LLVM_LIBC_SYS_AUXV_H

#include <__llvm-libc-common.h>

#include <llvm-libc-macros/sys-auxv-macros.h>

%%public_api()

#endif // LLVM_LIBC_SYS_AUXV_H
22 changes: 22 additions & 0 deletions libc/include/sys/prctl.h.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Linux header prctl.h ----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SYS_PRCTL_H
#define LLVM_LIBC_SYS_PRCTL_H

#include <__llvm-libc-common.h>

// Process control is highly platform specific, so the platform usually defines
// the macros itself.
#include <linux/prctl.h>

// TODO: Define the prctl macros.

%%public_api()

#endif // LLVM_LIBC_SYS_PRCTL_H
20 changes: 20 additions & 0 deletions libc/include/sys/time.h.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Linux header time.h -----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SYS_TIME_H
#define LLVM_LIBC_SYS_TIME_H

#include <__llvm-libc-common.h>

#include <llvm-libc-types/struct_timeval.h>

#include <llvm-libc-macros/sys-time-macros.h>

%%public_api()

#endif // LLVM_LIBC_SYS_TIME_H
9 changes: 9 additions & 0 deletions libc/spec/gnu_ext.td
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> {
]
>;

HeaderSpec SysAuxv = HeaderSpec<
"sys/auxv.h",
[], // Macros
[], // Types
[], // Enumerations
[] // Functions
>;

HeaderSpec SendFile = HeaderSpec<
"sys/sendfile.h",
[], // Macros
Expand Down Expand Up @@ -194,6 +202,7 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> {
PThread,
Sched,
SendFile,
SysAuxv,
StdIO,
String,
UniStd,
Expand Down
27 changes: 27 additions & 0 deletions libc/spec/linux.td
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
def StructTimevalType : NamedType<"struct timeval">;

def Linux : StandardSpec<"Linux"> {
HeaderSpec Errno = HeaderSpec<
"errno.h",
Expand Down Expand Up @@ -63,6 +65,15 @@ def Linux : StandardSpec<"Linux"> {
[Macro<"MAP_ANONYMOUS">]
>;


HeaderSpec SysPrctl = HeaderSpec<
"sys/prctl.h",
[], // Macros
[], // Types
[], // Enumerations
[] // Functions
>;

HeaderSpec SysRandom = HeaderSpec<
"sys/random.h",
[
Expand All @@ -85,6 +96,20 @@ def Linux : StandardSpec<"Linux"> {
]
>;

HeaderSpec SysTime = HeaderSpec<
"sys/time.h",
[
Macro<"timeradd">,
Macro<"timersub">,
Macro<"timerclear">,
Macro<"timerisset">,
Macro<"timercmp">,
],
[StructTimevalType], // Types
[], // Enumerations
[] // Functions
>;

HeaderSpec Signal = HeaderSpec<
"signal.h",
[
Expand Down Expand Up @@ -130,7 +155,9 @@ def Linux : StandardSpec<"Linux"> {
let Headers = [
Errno,
SysMMan,
SysPrctl,
SysRandom,
SysTime,
Signal,
];
}

0 comments on commit 35a4fe4

Please sign in to comment.