Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux default libraries [closed]

Tags:

c++

I know that on Windows, you get some libraries linked in by default to your process- like kernel32.dll, etc. Are there any equivalent libraries on Linux?

I am creating some Linux binaries and looking for support routines, especially malloc, etc. On Windows I simply implemented malloc() on top of HeapAlloc (which is also the approach taken by the VS CRT) but I'm not sure what to do here. For reasons, I'm not going to link to libc when creating the binary if at all possible.

like image 219
Puppy Avatar asked Sep 02 '25 09:09

Puppy


2 Answers

By default, G++ in Linux will link against the C standard library and C++ standard library. Occasionally it will also bring in the math library automatically, although historically you need to ask for that with -lm.

On my Ubuntu box, I compiled and linked the following simple "Hello World" app:

#include <iostream>

int main()
{
    std::cout << "Hello world!" << std::endl;
}

I compiled it as follows: g++ hello.cpp

The ldd utility lists the libraries g++ linked this against:

$ ldd a.out
linux-vdso.so.1 =>  (0x00007fff1d344000)
libstdc++.so.6 => /usr/local/lib64/libstdc++.so.6 (0x00007fd7fb031000)
libm.so.6 => /lib/libm.so.6 (0x00007fd7fadae000)
libgcc_s.so.1 => /usr/local/lib64/libgcc_s.so.1 (0x00007fd7fab97000)
libc.so.6 => /lib/libc.so.6 (0x00007fd7fa813000)
/lib64/ld-linux-x86-64.so.2 (0x00007fd7fb365000)

The first line, linux-vdso.so.1 isn't actually a library. Google it if you want to learn about some magic hackery. The rest are pretty pedestrian:

  • libstdc++ is the C++ standard library
  • libm is the aforementioned math library. I don't know if C++ includes it by default, but historically the C compiler did not include it unless you specified -lm at link time.
  • libgcc_s is a GCC-specific support library, containing various support routines (ie. for oddball things like oddball divides, structure-copies, etc.)
  • libc is the C standard library. It also contains a lot of POSIX functionality.
  • ld-linux-x86-64 is the dynamic library loader. This is actually an executable.

So, that's the default bit of kit.

Pieces such as malloc, new, printf, etc. are all in there, along with the full C++ standard library (what some call the "STL").

If you're asking what support comes by default, this is it. If you're trying to implement your own versions of these things... the -nodefaultlibs flag will let you. You might also need -ffreestanding and maybe even -fno-builtins.

If you want to see how these pieces are built (including how glibc calls mmap and/or sbrk to get memory to populate a malloc heap), you can download the source for glibc and take a look. There isn't a level below glibc you can target directly other than making system calls directly.

Assuming you're building your code with GCC / G++, you may need to include some of these libraries, such as libgcc_s and libstdc++. You might be able to limit / eliminate your dependence on libstdc++ if you refrain from using standard library functions, and build with -ffreestanding. But, I'll be honest: I only know of the flag, I've never used it.

like image 79
Joe Z Avatar answered Sep 03 '25 23:09

Joe Z


I think you want some lower level API for memory management. Then this question might help you.

It suggests mmap function. It hope it might help you.

like image 36
doptimusprime Avatar answered Sep 03 '25 22:09

doptimusprime