GNU C Library(glibc)の実装を覗く

ソースファイルを入手

Index of /gnu/glibcにあるglibc-*というのがソースファイル。

$ curl https://ftp.gnu.org/gnu/glibc/glibc-2.27.tar.gz | tar zx
$ cd glibc-2.27

目的の関数を探す

# ファイル名から関数を探す
$ find . -type f -name "*printf*" | less

# ファイル内から関数を探す
$ find stdio-common -type f -print | xargs grep 'printf' | less

stdlib.h に含まれている関数 abs を覗いてみよう

/usr/include/stdlib.h

/* Return the absolute value of X.  */
extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;

glibc-2.27/stdlib/abs.c

/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
...
 */

#include <stdlib.h>

#undef abs

/* Return the absolute value of I.  */
int
abs (int i)
{
  return i < 0 ? -i : i;
}