Libc strcpy usage on kernels

Table of Contents

1 Introduction

A simple search for a common libc function, strpcy, on various kernels. What it proves? Nothing… it's just a reflect of the organization of the code.

1.1 Linux

In the moment, 3 somewhat redundant functions.

1.2 Darwin/XNU (Mac OS)

At the moment, only 1 declaration.

1.3 DragonflyBSD

3 functions, but 2 is for GDB support (according to the files…)

1.4 OpenBSD

No version of strcpy is found… But we have strncpy, bonus security points :)… But, 3 strncpy declarations… :(

1.5 NetBSD

It uses GCC __builtin_strcpy, unless on VAX or if defined _STANDALONE, in which case it uses a callback table, and I had not found the function declaration yet.

  • http://fxr.watson.org/fxr/source/lib/libkern/libkern.h?v=NETBSD#L273
    /* Prototypes for which GCC built-ins exist. */
    void    *memcpy(void *, const void *, size_t);
    int      memcmp(const void *, const void *, size_t);
    void    *memset(void *, int, size_t);
    #if __GNUC_PREREQ__(2, 95) && (__GNUC_PREREQ__(4, 0) || !defined(__vax__)) && \
      !defined(_STANDALONE)
    #define memcpy(d, s, l)         __builtin_memcpy(d, s, l)
    #define memcmp(a, b, l)         __builtin_memcmp(a, b, l)
    #endif
    #if __GNUC_PREREQ__(2, 95) && !defined(__vax__) && !defined(_STANDALONE)
    #define memset(d, v, l)         __builtin_memset(d, v, l)
    #endif
    
    char    *strcpy(char *, const char *);
    int      strcmp(const char *, const char *);
    size_t   strlen(const char *);
    char    *strsep(char **, const char *);
    #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
    #define strcpy(d, s)            __builtin_strcpy(d, s)
    #define strcmp(a, b)            __builtin_strcmp(a, b)
    #define strlen(a)               __builtin_strlen(a)
    #endif
    
  • VAX version: http://fxr.watson.org/fxr/source/arch/vax/include/macros.h?v=NETBSD#L197
    static __inline char * __attribute__((__unused__))
      strcpy(char *cp, const char *c2)
    {
      __asm volatile("locc $0,$65535,(%1);"
                     "subl3 %%r0,$65535,%%r2;"
                     "movc3 %%r2,(%1),(%0);"
                     "movb $0,(%%r3)"
                     :
                     : "r" (cp), "r" (c2)
                     : "r0","r1","r2","r3","r4","r5","memory","cc");
      return  cp;
    }     
    

1.6 Minix

No declaration, uses compilling host libc

1.7 OpenSolaris

Defined as bcopy, which is defined as memmove, wich I found no definition… I supose it's using <string.h> memmove

1.8 March

1 definition

1.9 Plan9

1 external definition, assuming GCC builtin…

1.10 FreeBSD

3 definitions, with one somewhat invalid, and some use of builtin functions

Date: 2012-08-03 13:42:04 BRT

Author: Joaquim Pedro França Simão (osmano807) <osmano807@gmail.com>

Org version 7.8.11 with Emacs version 24

Validate XHTML 1.0