This is the mail archive of the mailing list for the glibc project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: atexit in a glibc function

On Saturday 16 June 2012 00:35:27 Amittai Aviram wrote:
> I have been writing a custom variant of glibc Version 2.14 that differs
> primarily in malloc/malloc.c .  My malloc.c has a function called
> "init_globals" that initializes some global data structures, which opens
> some files and pipes in the process.  In order to make sure that these
> pipes and files get closed when the process terminates, I have a
> "clean_up" function to close them, and, at the end of "init_globals," I
> have
> atexit(&clean_up);
> But, when I try to build this variant glibc, the linker reports "undefined
> reference to 'atexit'":
> gcc   -shared -static-libgcc -Wl,-O1  -Wl,-z,defs
> -Wl,-dynamic-linker=/home/afa9/GLIBC/glibc_install/lib/
> 2  -B/home/afa9/GLIBC/glibc_obj/csu/ 
> -Wl,--version-script=/home/afa9/GLIBC/glibc_obj/
> -Wl, -Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both
> -nostdlib -nostartfiles -e __libc_main -L/home/afa9/GLIBC/glibc_obj
> -L/home/afa9/GLIBC/glibc_obj/math -L/home/afa9/GLIBC/glibc_obj/elf
> -L/home/afa9/GLIBC/glibc_obj/dlfcn -L/home/afa9/GLIBC/glibc_obj/nss
> -L/home/afa9/GLIBC/glibc_obj/nis -L/home/afa9/GLIBC/glibc_obj/rt
> -L/home/afa9/GLIBC/glibc_obj/resolv -L/home/afa9/GLIBC/glibc_obj/crypt
> -L/home/afa9/GLIBC/glibc_obj/nptl
> -Wl,-rpath-link=/home/afa9/GLIBC/glibc_obj:/home/afa9/GLIBC/glibc_obj/math
> :/home/afa9/GLIBC/glibc_obj/elf:/home/afa9/GLIBC/glibc_obj/dlfcn:/home/afa9
> /GLIBC/glibc_obj/nss:/home/afa9/GLIBC/glibc_obj/nis:/home/afa9/GLIBC/glibc_
> obj/rt:/home/afa9/GLIBC/glibc_obj/resolv:/home/afa9/GLIBC/glibc_obj/crypt:/
> home/afa9/GLIBC/glibc_obj/nptl -o /home/afa9/GLIBC/glibc_obj/ -T
> /home/afa9/GLIBC/glibc_obj/
> /home/afa9/GLIBC/glibc_obj/csu/abi-note.o
> /home/afa9/GLIBC/glibc_obj/elf/soinit.os
> /home/afa9/GLIBC/glibc_obj/libc_pic.os
> /home/afa9/GLIBC/glibc_obj/elf/sofini.os
> /home/afa9/GLIBC/glibc_obj/elf/interp.os
> /home/afa9/GLIBC/glibc_obj/elf/ -lgcc
> /home/afa9/GLIBC/glibc_obj/libc_pic.os: In function `init_globals':
> /home/afa9/GLIBC/glibc/malloc/malloc.c:5186: undefined reference to
> `atexit' collect2: ld returned 1 exit status
> My malloc.c definitely #includes <stdlib.h>.  Why doesn't the linker
> recognize "atexit"?  Thanks!

because in the x86-64 port, atexit isn't provided by the C library.  it's in 
libc_nonshared.a which means the symbol ends up in the final application, not 
in the  so you may not use atexit() inside of the C library.

malloc (and friends) are designed to be overridden at runtime.  create a 
shared library that exports those symbols and simply LD_PRELOAD it and those 
funcs will be called whenever memory is needed.  why do you need to rebuild 
glibc to do what you want ?

Attachment: signature.asc
Description: This is a digitally signed message part.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]