Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a C program using a custom version of glibc and static linking?

Tags:

I have built glibc 2.14 and installed it in directory ~/GLIBC/glibc_install. Now I want to build and run programs using this C library instead of my system's default C library.

  • To be sure that I was using my custom glibc, I added a call to puts into glibc/stdio-common/printf.c:__printf to print a message.

  • Then I rebuilt and reinstalled glibc.

  • Then I wrote a "Hello, World" program and tried to compile and link it as follows:

    gcc -nodefaultlibs -static -lgcc -L~/GLIBC/glibc_install/lib -o myprog myprog.c 

But I get the following linker error report:

/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start': (.text+0x19): undefined reference to `__libc_csu_init' /usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start': (.text+0x25): undefined reference to `__libc_start_main' /tmp/ccACTQEp.o: In function `main': c1.c:(.text+0xa): undefined reference to `puts' collect2: ld returned 1 exit status 

What am I doing wrong?

like image 275
Amittai Aviram Avatar asked May 26 '12 03:05

Amittai Aviram


People also ask

How do I use specific version of glibc?

One option is to statically link your binary. This is probably the easiest option. You could also build your binary in a chroot build environment, or using a glibc-new => glibc-old cross-compiler.

Is glibc written in C?

The GNU C Library, commonly known as glibc, is the GNU Project's implementation of the C standard library. Despite its name, it now also directly supports C++ (and, indirectly, other programming languages). It was started in the 1980s by the Free Software Foundation (FSF) for the GNU operating system.

Can you have multiple versions of glibc?

It is very possible to have multiple versions of glibc on the same system (we do that every day). However, you need to know that glibc consists of many pieces (200+ shared libraries) which all must match.

What is glibc static?

The glibc-static package contains the C library static libraries for -static linking. You don't need these, unless you link statically, which is highly discouraged.


2 Answers

Following a couple of suggestions from the glibc help mailing list ([email protected]), I have a solution. It turns out that this task is a bit tricky because you have to tell the linker to omit everything it would normally include automatically (and silently), and then include back everything that it needs, including a bunch of start and end files. Some of the start and end files come from libc and some come from gcc, so the make rule is a bit complicated. Below is a general sample makefile to illustrate the approach. I will assume that you are building a program called prog from a source file called prog.c and that you have installed your custom glibc in directory /home/my_acct/glibc_install.

TARGET = prog OBJ = $(TARGET).o SRC = $(TARGET).c CC = gcc CFLAGS = -g LDFLAGS = -nostdlib -nostartfiles -static GLIBCDIR = /home/my_acct/glibc_install/lib STARTFILES = $(GLIBCDIR)/crt1.o $(GLIBCDIR)/crti.o `gcc --print-file-name=crtbegin.o` ENDFILES = `gcc --print-file-name=crtend.o` $(GLIBCDIR)/crtn.o LIBGROUP = -Wl,--start-group $(GLIBCDIR)/libc.a -lgcc -lgcc_eh -Wl,--end-group  $(TARGET): $(OBJ)         $(CC) $(LDFLAGS) -o $@ $(STARTFILES) $^ $(LIBGROUP) $(ENDFILES)   $(OBJ): $(SRC)         $(CC) $(CFLAGS) -c $^  clean:         rm -f *.o *.~ $(TARGET) 
like image 69
Amittai Aviram Avatar answered Sep 28 '22 11:09

Amittai Aviram


You command line is just bogus. Try:

gcc -nodefaultlibs -static -L~/GLIBC/glibc_install/lib -o myprog myprog.c -lgcc -lc -lgcc -lc 

or similar. You omitted -lc, and also erroneously had your libraries before your input files.

And you were searching for a library called libibgcc rather than libgcc...

like image 37
R.. GitHub STOP HELPING ICE Avatar answered Sep 28 '22 10:09

R.. GitHub STOP HELPING ICE