3

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.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • As far as I know, libc is automatically linked by gcc (and I think the linking is dynamic, actually, so it shouldn't take up much more binary space). So, you should have access to malloc(), etc just by including the appropriate headers. Which compiler are you going to use? – Los Frijoles Dec 11 '13 at 05:15
  • 1
    Are you actually asking what the default libraries are, or do you just need someone to point you at "-nostdlib"? – kfsone Dec 11 '13 at 05:17
  • AFAIK Linux just responds to syscalls, and libc almost directly uses those syscalls, and the only stuff linked in is stuff you're using or stuff it requires. – cHao Dec 11 '13 at 05:18
  • 1
    Maybe is this what you are looking for http://stackoverflow.com/questions/2782010/how-to-dynamically-allocate-memory-using-assembly-and-system-calls-under-linux ? – felknight Dec 11 '13 at 05:27
  • @DeadMG I think you need lower level call to allocate memory. See edit in my answer. may be `mmap` may help you. – doptimusprime Dec 11 '13 at 05:29

4 Answers4

2

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.

Joe Z
  • 17,413
  • 3
  • 28
  • 39
  • I'm sorry but is this what you asking for? – felknight Dec 11 '13 at 05:26
  • why don't you compile and link this : `int main(){}` and see what is really necessary? In your code, you use iostream, and that may introduce some dependencies – BЈовић Dec 11 '13 at 07:26
  • Just for reference, although the standard says it should, `-ffreestanding` didn't play well with exceptions for me; stuff would break unless i also disabled exceptions. I take that back...it *could* have worked, but i'd have had to include functions that quack like the runtime library's internal exception stuff. – cHao Dec 13 '13 at 09:35
1

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.

Community
  • 1
  • 1
doptimusprime
  • 9,115
  • 6
  • 52
  • 90
  • 3
    Nearly 84k rep, over 60k of it in the C and C++ tags. I'm fairly certain he knows how to link (or not link). – cHao Dec 11 '13 at 05:20
  • You are right, but where do you know, that he isn't programming under windows earlier and now want to change to linux ? – demonking Dec 11 '13 at 05:38
0

According to ldd The following libraries are linked by default with g++ 4.8 on my ubuntu linux machine for basic c++ programs.

linux-vdso.so.1 =>  (0x00007fffe11fe000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f1d1e49b000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f1d1e285000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f1d1debc000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f1d1dbb8000)
/lib64/ld-linux-x86-64.so.2 (0x00007f1d1e7c7000)

You can disable linking them using -nodefaultlibs switch but you also need to specify that they need to be linked later using the appropriate switches.

en4bz
  • 1,092
  • 14
  • 18
0

On Linux, glibc is equivalent of a lot of stuff in the Windows libraries. The POSIX C interface functions, including the C standard library and also the kernel system call wrappers are all in libc. It is the primary wrapper layer providing ABI compatibility between various kernel versions. Not using it is very stupid.

The Windows CRT indeed has a different status, because POSIX has a different status on Windows. Where the Win32 API provides the OS interface on Windows, it is POSIX (including the C stdlib!) on Linux.

When on Linux, link glibc.

You will also need to link some compiler support library, be it LLVM's compiler-rt or GCC's libgcc.

rubenvb
  • 74,642
  • 33
  • 187
  • 332