Module Name:    src
Committed By:   riastradh
Date:           Sat Mar 29 20:57:58 UTC 2025

Modified Files:
        src/lib/libc/gen: ctype_.c ctype_guard.h tolower_.c toupper_.c

Log Message:
libc: Restore ELF symbol sizes for _C_ctype_tab_ &c.

This is needed for dynamic position-dependent executables that refer
directly to _C_type_tab_ to get correct copy relocations to see the
table content.

Unfortunately, such executables won't get a guard page.

Fortunately, referring to _C_ctype_tab_ directly (and not the
indirection _ctype_tab_ as the ctype(3) macros do) is very weird and
unlikely to happen in the real world (none of the public interfaces
use it; it is exported for libc++.so/libstdc++.so to use, but those
aren't pies).  So missing the guard page in this case is probably not
so bad.

The symbol sizes are also needed for, e.g., gdb to nicely identify
addresses that lie in the table.

PR lib/58208: ctype(3) provides poor runtime feedback of abuse


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.22 src/lib/libc/gen/ctype_.c
cvs rdiff -u -r1.1 -r1.2 src/lib/libc/gen/ctype_guard.h
cvs rdiff -u -r1.15 -r1.16 src/lib/libc/gen/tolower_.c \
    src/lib/libc/gen/toupper_.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/gen/ctype_.c
diff -u src/lib/libc/gen/ctype_.c:1.21 src/lib/libc/gen/ctype_.c:1.22
--- src/lib/libc/gen/ctype_.c:1.21	Sat Mar 29 19:40:42 2025
+++ src/lib/libc/gen/ctype_.c	Sat Mar 29 20:57:58 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: ctype_.c,v 1.21 2025/03/29 19:40:42 riastradh Exp $	*/
+/*	$NetBSD: ctype_.c,v 1.22 2025/03/29 20:57:58 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1989 The Regents of the University of California.
@@ -39,7 +39,7 @@
 #if 0
 /*static char *sccsid = "from: @(#)ctype_.c	5.6 (Berkeley) 6/1/90";*/
 #else
-__RCSID("$NetBSD: ctype_.c,v 1.21 2025/03/29 19:40:42 riastradh Exp $");
+__RCSID("$NetBSD: ctype_.c,v 1.22 2025/03/29 20:57:58 riastradh Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -88,6 +88,8 @@ const unsigned char _C_compat_bsdctype_g
 	_L,	_L,	_L,	_L,	_L,	_L,	_L,	_L,
 	_L,	_L,	_L,	_P,	_P,	_P,	_P,	_C
 };
+__ctype_table_size(_C_compat_bsdctype, _C_compat_bsdctype_guarded,
+    1 + _CTYPE_NUM_CHARS, /*sizeof(char)*/1);
 
 #undef _C
 #undef _S
@@ -155,6 +157,8 @@ static const unsigned short _C_ctype_tab
 	_A|_G|_L|_R,	_A|_G|_L|_R,	_A|_G|_L|_R,	_G|_R|_P,
 	_G|_R|_P,	_G|_R|_P,	_G|_R|_P,	_C,
 };
+__ctype_table_size(_C_ctype_tab_, _C_ctype_tab_guarded_,
+    1 + _CTYPE_NUM_CHARS, __SIZEOF_SHORT__);
 
 #undef _A
 #undef _BL

Index: src/lib/libc/gen/ctype_guard.h
diff -u src/lib/libc/gen/ctype_guard.h:1.1 src/lib/libc/gen/ctype_guard.h:1.2
--- src/lib/libc/gen/ctype_guard.h:1.1	Sat Mar 29 19:40:42 2025
+++ src/lib/libc/gen/ctype_guard.h	Sat Mar 29 20:57:58 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: ctype_guard.h,v 1.1 2025/03/29 19:40:42 riastradh Exp $	*/
+/*	$NetBSD: ctype_guard.h,v 1.2 2025/03/29 20:57:58 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2025 The NetBSD Foundation, Inc.
@@ -108,6 +108,11 @@
 	__asm(".type " #name ",@object")
 #endif
 
+#define	__ctype_table_size(name, guard, nelem, elemsize)		      \
+	__CTASSERT(sizeof((guard)[0]) == (elemsize));			      \
+	__CTASSERT(sizeof(guard) == _CTYPE_GUARD_SIZE + (nelem)*(elemsize));  \
+	__asm(".size " #name "," ___STRING((nelem) * (elemsize)))
+
 #if _CTYPE_GUARD_PAGE
 
 #  include <machine/vmparam.h>

Index: src/lib/libc/gen/tolower_.c
diff -u src/lib/libc/gen/tolower_.c:1.15 src/lib/libc/gen/tolower_.c:1.16
--- src/lib/libc/gen/tolower_.c:1.15	Sat Mar 29 19:40:42 2025
+++ src/lib/libc/gen/tolower_.c	Sat Mar 29 20:57:58 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: tolower_.c,v 1.15 2025/03/29 19:40:42 riastradh Exp $	*/
+/*	$NetBSD: tolower_.c,v 1.16 2025/03/29 20:57:58 riastradh Exp $	*/
 
 /*
  * Written by J.T. Conklin <j...@netbsd.org>.
@@ -7,7 +7,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_RCS) && !defined(lint)
-__RCSID("$NetBSD: tolower_.c,v 1.15 2025/03/29 19:40:42 riastradh Exp $");
+__RCSID("$NetBSD: tolower_.c,v 1.16 2025/03/29 20:57:58 riastradh Exp $");
 #endif /* LIBC_RCS and not lint */
 
 #include <sys/ctype_bits.h>
@@ -60,6 +60,8 @@ static const short _C_tolower_tab_guarde
 	0xf0,	0xf1,	0xf2,	0xf3,	0xf4,	0xf5,	0xf6,	0xf7,
 	0xf8,	0xf9,	0xfa,	0xfb,	0xfc,	0xfd,	0xfe,	0xff
 };
+__ctype_table_size(_C_tolower_tab_, _C_tolower_tab_guarded_,
+    1 + _CTYPE_NUM_CHARS, __SIZEOF_SHORT__);
 
 #ifdef __BUILD_LEGACY
 #ifdef __weak_alias
Index: src/lib/libc/gen/toupper_.c
diff -u src/lib/libc/gen/toupper_.c:1.15 src/lib/libc/gen/toupper_.c:1.16
--- src/lib/libc/gen/toupper_.c:1.15	Sat Mar 29 19:40:42 2025
+++ src/lib/libc/gen/toupper_.c	Sat Mar 29 20:57:58 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: toupper_.c,v 1.15 2025/03/29 19:40:42 riastradh Exp $	*/
+/*	$NetBSD: toupper_.c,v 1.16 2025/03/29 20:57:58 riastradh Exp $	*/
 
 /*
  * Written by J.T. Conklin <j...@netbsd.org>.
@@ -7,7 +7,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_RCS) && !defined(lint)
-__RCSID("$NetBSD: toupper_.c,v 1.15 2025/03/29 19:40:42 riastradh Exp $");
+__RCSID("$NetBSD: toupper_.c,v 1.16 2025/03/29 20:57:58 riastradh Exp $");
 #endif /* LIBC_RCS and not lint */
 
 #include <sys/ctype_bits.h>
@@ -60,6 +60,8 @@ static const short _C_toupper_tab_guarde
 	0xf0,	0xf1,	0xf2,	0xf3,	0xf4,	0xf5,	0xf6,	0xf7,
 	0xf8,	0xf9,	0xfa,	0xfb,	0xfc,	0xfd,	0xfe,	0xff
 };
+__ctype_table_size(_C_toupper_tab_, _C_toupper_tab_guarded_,
+    1 + _CTYPE_NUM_CHARS, __SIZEOF_SHORT__);
 
 #ifdef __BUILD_LEGACY
 #ifdef __weak_alias

Reply via email to