GNU Source Building | glibc | Part 2

Download – glibc-2.26.tar.xz

Similar to my last post GNU Source Building | Wget | Part 1, I will be covering the popular GNU C library package.

Glibc provides the user with an interface to the services provided by the kernel in which the user application can use a system call just like an ordinary function. Glibc implements both standard C functions like strcpy() and POSIX functions (which may be system calls).

Building glibc

Note the installation instructions below will install a local copy of glibc. Be sure not to install using sudo.

This may not be the case for you.

  1. mkdir glibc-build //our local folder to install Wget separate from our system
  2. tar -xv glibc-2.26.tar.xz //Decompress the package
  3. cd glibc-build //change directory
  4. ../glibc-2.26/configure --prefix=/YOUR_USER_NAME/wget-build//configure
  5. make //run the makefile (this process may take 15-20 mins)
    glibc Configure Capture
  6. Be sure to check that glibc successfully built and didn’t return any errors.


In order to test our new installation of glibc lets create a sample program named timer_test.c. This program simply returns the current local machine time.

#include #include #include int main() { time_t current_time; char* c_time_string; current_time = time(NULL); /* Convert to local time format. */ c_time_string = ctime(&current_time); printf("Current time is %s", c_time_string); }

gcc timer_test.c -o timer

Next lets put our new build of glibc to use. From the glibc-build directory we’re going to compile our test program

./ ../timer ../time

Testing glibc

We were instructed to experiment with the glibc definitions an implement a bug somewhere in the library.

Working with our same programtimer_test.c lets try and modify the functionality of the time() function.

cat ../glibc-2.26/time/time.c

Inside you’ll find the standard definition for the time() function

<pre>/* Copyright (C) 1991,96,97,2002 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <>.  */


/* Return the time now, and store it in *TIMER if not NULL.  */
time (timer)
     time_t *timer;
  __set_errno (ENOSYS);

  if (timer != NULL)
    *timer = (time_t) -1;
  return (time_t) -1;
libc_hidden_def (time)

stub_warning (time)

Let’s see what would happen if we alter the return values if the time condition is reversed.

/* Return the time now, and store it in *TIMER if not NULL. */
time (time_t *timer)
__set_errno (ENOSYS);

if (timer == NULL)
*timer = (time_t) +5;
return (time_t) +1;
libc_hidden_def (time)

stub_warning (time)

Once we’ve saved our changes he new implemenation won”t take effect until we ./configure && make the library again.

Then we re-compile our program

./ ../timer ../tester


Current time is Tue Jan 6 21:23:54 2018

At the time of writing the post, the correct time is Tue Jan 6 16:22:54 2018

Our newly introduce bug adds 5 hours to the current time and subtracts 1 minute.

Related Posts