bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Implement our own functions to convert between host and network


From: Flavio Cruz
Subject: [PATCH] Implement our own functions to convert between host and network values
Date: Fri, 16 Dec 2022 14:56:36 -0500

We add htonl, ntohl and ntohs which were the last symbols we relied on
from libc.

The Makefile.am file was updated to reflect the fact that we only require 
libgcc.
---
 Makefile.am      | 20 ++++++++------------
 Makefrag.am      |  4 +++-
 util/byteorder.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 util/byteorder.h | 31 +++++++++++++++++++++++++++++++
 4 files changed, 87 insertions(+), 13 deletions(-)
 create mode 100644 util/byteorder.c
 create mode 100644 util/byteorder.h

diff --git a/Makefile.am b/Makefile.am
index 33f770f6..0b7ebc56 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -167,31 +167,27 @@ gnumach_o_LINK = $(LD) $(LDFLAGS) -u _start -r -o $@
 noinst_PROGRAMS += \
        gnumach.o
 
-# This is the list of routines we decide is OK to steal from the C library or 
libgcc.
-clib_routines := htonl htons ntohl ntohs                       \
-                udivdi3 __udivdi3 __udivmoddi4 __umoddi3       \
-                __divdi3 __moddi3                              \
-                __rel_iplt_start __rel_iplt_end                \
-                __rela_iplt_start __rela_iplt_end              \
-                __ffsdi2                                       \
-                _START _start etext _edata end _end # actually ld magic, not 
libc.
+# This is the list of routines we use from libgcc.
+libgcc_routines := __udivmoddi4 __umoddi3 __ffsdi2
+# References generated by ld.
+ld_magic_routines := __rel_iplt_start __rel_iplt_end etext _edata _end
 gnumach-undef: gnumach.$(OBJEXT)
        $(NM_V) $(NM) -u $< | sed 's/  *U  *//' | sort -u > $@
 MOSTLYCLEANFILES += gnumach-undef
 gnumach-undef-bad: gnumach-undef Makefile
-       $(AM_V_GEN) sed '$(foreach r,$(clib_routines),/^$r$$/d;)' $< > $@
+       $(AM_V_GEN) sed '$(foreach r,$(libgcc_routines) 
$(ld_magic_routines),/^$r$$/d;)' $< > $@
 MOSTLYCLEANFILES += gnumach-undef-bad
-clib-routines.o: gnumach-undef gnumach-undef-bad
+libgcc-routines.o: gnumach-undef gnumach-undef-bad
        $(AM_V_at) if test -s gnumach-undef-bad; \
        then cat gnumach-undef-bad; exit 2; else true; fi
        $(AM_V_CCLD) $(CCLD) $(LDFLAGS) -nostdlib -nostartfiles -r -static \
-         -o $@ `sed 's/^/-Wl,-u,/' < $<` -x c /dev/null -lc -lgcc
+         -o $@ `sed 's/^/-Wl,-u,/' < $<` -x c /dev/null -lgcc
        @if nm $@ | grep __init_cpu_features; \
        then echo "Please install a 32bit libc without multiarch support (on 
Debian systems, the libc6-dev:i386 package containing 
/usr/lib/i386-linux-gnu/libc.a)". ; \
        false ; fi
 
 gnumach_LINK = $(LD) $(LDFLAGS) $(LINKFLAGS) $(gnumach_LINKFLAGS) -o $@
-gnumach_LDADD = gnumach.o clib-routines.o
+gnumach_LDADD = gnumach.o libgcc-routines.o
 
 #
 # Installation.
diff --git a/Makefrag.am b/Makefrag.am
index 51b15260..e2593c42 100644
--- a/Makefrag.am
+++ b/Makefrag.am
@@ -244,7 +244,9 @@ libkernel_a_SOURCES += \
        util/putchar.h \
        util/puts.c \
        util/atoi.c \
-       util/atoi.h
+       util/atoi.h \
+       util/byteorder.h \
+       util/byteorder.c
 
 #
 # Virtual memory implementation.
diff --git a/util/byteorder.c b/util/byteorder.c
new file mode 100644
index 00000000..18311972
--- /dev/null
+++ b/util/byteorder.c
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Mach.
+ *
+ * GNU Mach is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any later
+ * version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "util/byteorder.h"
+
+uint16_t ntohs(uint16_t netshort) {
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+       return __builtin_bswap16(netshort);
+#else
+       return netshort;
+#endif
+}
+
+uint32_t ntohl(uint32_t netlong) {
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+       return __builtin_bswap32(netlong);
+#else
+       return netlong;
+#endif
+}
+
+uint32_t htonl(uint32_t hostlong) {
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+       return __builtin_bswap32(hostlong);
+#else
+       return hostlong;
+#endif
+}
diff --git a/util/byteorder.h b/util/byteorder.h
new file mode 100644
index 00000000..2b780ffe
--- /dev/null
+++ b/util/byteorder.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2022 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Mach.
+ *
+ * GNU Mach is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any later
+ * version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef _UTIL_BYTEORDER_H_
+#define _UTIL_BYTEORDER_H_
+
+#include <stdint.h>
+
+uint16_t ntohs(uint16_t netshort);
+uint32_t ntohl(uint32_t netlong);
+
+uint32_t htonl(uint32_t hostlong);
+
+#endif /* _UTIL_BYTEORDER_H_ */
-- 
2.37.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]