-2

I have built and installed another glibc from source, and I want to have existing executables written in c++ to run with the custom glibc for experimental purpose. In order to do this, i tried to change the loader of the executable. Firstly, a link named ld_linux-x86-64.so.2 was created under /lib64 , with its path pointing to the new loader

sudo ln /home/ubuntu/glibc-2.27-amd64/lib/ld-2.27.so /lib64/ld_linux-x86-64.so.2

Secondly, the loader path in the executable was modified via text editor, changing '/lib64/ld-linux-x86-64.so.2' into '/lib64/ld_linux-x86-64.so.2'. I launched the executable and got the following error:

./demo_cpp: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory

The patched cpp program failed to run and it seems that the c++ std lib is missing. However, this method did work for program written by pure C. Using the method mentioned here, i tried the following command and get the exactly same error:

patchelf --set-interpreter /home/ubuntu/glibc-2.27-amd64/lib/ld-linux-x86-64.so.2 --set-rpath /home/ubuntu/glibc-2.27-amd64/lib demo_cpp

So I wonder if it is possible to change glibc for a cpp program? Do i need to build cpp std lib from source too?

kayochin
  • 431
  • 6
  • 11
  • 1
    Loading an alternative shared library should not require installation of the entire system loader. That's something that's completely irrelevant. – Sam Varshavchik Mar 27 '21 at 14:48
  • @SamVarshavchik Does that mean i can load modified glibc without changing the loader of the executable? How to do that? – kayochin Mar 27 '21 at 14:51
  • 1
    This is explained in the `ld.so` manual page. it is unlikely that the executable will explicitly include the default library directories (`/lib` and `/usr/lib`) in it's `DT_RPATH`, if it has one. As such, the search path for shared libraries is overridable as explained in the manual page. The only exception to that is a suid program, which you did not mention. – Sam Varshavchik Mar 27 '21 at 14:54
  • Read documentation of [GCC](http://gcc.gnu.org/), and compile with `g++ -Wall -Wextra -g -H demo.cpp -o demo_cpp` then run `ldd ./demo_cpp` – Basile Starynkevitch Mar 27 '21 at 14:56
  • @BasileStarynkevitch I'm sorry the cpp code is causing misunderstanding. It is removed from question. Actually, my attempt is to patch glibc for existing cpp program without source – kayochin Mar 27 '21 at 15:07
  • Sorry, I don't understand your question. Either improve it (by adding several paragraphs, with some [mre]) or send me an email (in English or in Russian) about it. Explain in several paragraphs how and why did you built and installed another glibc. See also https://linuxfromscratch.org/ – Basile Starynkevitch Mar 27 '21 at 15:08
  • Are you allowed to use [strace(1)](https://man7.org/linux/man-pages/man1/strace.1.html) ? Are you allowed to use [schroot](https://linux.die.net/man/1/schroot) ? Please improve your question, and explain, in written English, what `schroot` and `strace` gave you. I am not sure you technically can patch glibc for existing cpp program without source, and in many countries that could be illegal – Basile Starynkevitch Mar 27 '21 at 15:14
  • 2
    I guess you want to hijack some library functions. That's what LD_PRELOAD is good for. It's quite common topic on SO, especially hijacking malloc/calloc/realloc/free – Zsigmond Lőrinczy Mar 28 '21 at 05:37
  • @ZsigmondLőrinczy thx. I will check the LD_PRELOAD trick, and see if and how it works for a cpp program – kayochin Mar 28 '21 at 08:43

1 Answers1

0

You need to read books about operating systems, compilers and linkers.

I recommend reading

Once you have read all the above books, consider reading documentation of several Linux man pages, e.g:

Study also the documentation and the source code of GNU libc and of the Linux kernel

Since they are free software !

So I wonder if it is possible to change glibc for a cpp program?

Yes it is possible, no it is not easy!

See also schroot and LinuxFromScratch

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547