summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-10-14 15:57:38 -0400
committerIan Moffett <ian@osmora.org>2025-10-14 15:57:38 -0400
commit275be4b47037984205034920b4dc61e55fd2d6b5 (patch)
treedddd5ea1ece05a2e85f77042f4519caea1a1ef7c /src/lib
parentf8d199805cf54030641f804852202eb9e6c9b7c7 (diff)
libc: Add __libc_init as well as cmdline arguments
This commit introduces a libc initialization function @ __libc_init() a long with variables __argv __argc used for command line arguments Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libc/amd64/crt/crt0.S2
-rw-r--r--src/lib/libc/include/stdarg.h6
-rw-r--r--src/lib/libc/src/libc_init.c77
3 files changed, 84 insertions, 1 deletions
diff --git a/src/lib/libc/amd64/crt/crt0.S b/src/lib/libc/amd64/crt/crt0.S
index dc1a273..82bc9dd 100644
--- a/src/lib/libc/amd64/crt/crt0.S
+++ b/src/lib/libc/amd64/crt/crt0.S
@@ -30,7 +30,7 @@
.text
.globl _start
_start:
- call main
+ call __libc_init
mov $0x01, %rax
xor %rdi, %rdi
int $0x80
diff --git a/src/lib/libc/include/stdarg.h b/src/lib/libc/include/stdarg.h
index e88a0fe..7a37ae1 100644
--- a/src/lib/libc/include/stdarg.h
+++ b/src/lib/libc/include/stdarg.h
@@ -30,6 +30,8 @@
#ifndef _STDARG_H
#define _STDARG_H
+#include <sys/limits.h>
+
#ifndef __GNUC_VA_LIST
#define __GNUC_VA_LIST
typedef __builtin_va_list __gnuc_va_list;
@@ -41,4 +43,8 @@ typedef __gnuc_va_list va_list;
#define va_end(ap) __builtin_va_end((ap))
#define va_arg(ap, type) __builtin_va_arg((ap), type)
+/* L5 specific */
+extern char __argv[ARG_LEN][NARG_MAX];
+extern int __argc;
+
#endif /* !_STDARG_H */
diff --git a/src/lib/libc/src/libc_init.c b/src/lib/libc/src/libc_init.c
new file mode 100644
index 0000000..f29bd7f
--- /dev/null
+++ b/src/lib/libc/src/libc_init.c
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2025 Ian Marco Moffett and L5 engineers
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/syscall.h>
+#include <sys/limits.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <stdio.h>
+
+extern int main(void);
+
+/* Command line argument variables */
+char __argv[ARG_LEN][NARG_MAX];
+int __argc = 0;
+
+static int
+getargv(unsigned int argno, char *buf, size_t len)
+{
+ if (buf == NULL || len == 0) {
+ return -1;
+ }
+
+ return syscall(
+ SYS_getargv,
+ argno,
+ (uintptr_t)buf,
+ len
+ );
+}
+
+int
+__libc_init(void)
+{
+ int error;
+
+ for (int i = 0; i < NARG_MAX; ++i) {
+ error = getargv(
+ i,
+ __argv[i],
+ sizeof(__argv[i])
+ );
+
+ if (error < 0) {
+ break;
+ }
+
+ ++__argc;
+ }
+
+ return main();
+}