Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is ld.so a shared object?

I'm currently writing a dynamic linker and I there is something bugging me. Why is ld.so (my system's dynamic linker) a shared object ? Why can't it just be a static executable (ET_EXEC) ?

I tried looking for answer in the linux kernel's binfmt_elf.c but to my understanding it clearly shows that your ELF interpreter could be a static executable.

EDIT:I think my thinking sums up to: Can a dynamic linker be a simple ELF executable (ET_EXEC) ?

binfmt_elf.c.559:

/* First of all, some simple consistency checks */
    if (interp_elf_ex->e_type != ET_EXEC &&
        interp_elf_ex->e_type != ET_DYN)
        goto out;

PS: I hope this is the right place, I don't know if I'm supposed to put it here or on Unix stack exchange. I'm also sorry if my question is stupid but not having an answer to this is driving me crazy.

like image 887
nitram Avatar asked Aug 28 '19 07:08

nitram


People also ask

What is shared object?

A shared object is an indivisible unit that is generated from one or more relocatable objects. Shared objects can be bound with dynamic executables to form a runable process. As their name implies, shared objects can be shared by more than one application.

What does Ld So do?

DESCRIPTION. ld.so is a self-contained, position independent program image providing run-time support for loading and link-editing shared objects into a process's address space.

What does ld stand for in LD_LIBRARY_PATH?

LD_LIBRARY_PATH tells the dynamic link loader (ld. so – this little program that starts all your applications) where to search for the dynamic shared libraries an application was linked against.


1 Answers

Can a dynamic linker be a simple ELF executable (ET_EXEC) ?

Yes, it can.

However, an ET_EXEC must be loaded at the address it was linked at, and that address may conflict with the address at which a.out itself is linked. If such conflict happens, the kernel will either kill the process before it even starts, or it will mmap a.out "on top of" ld.so, and the resulting binary will crash.

You can move ld.so out of the way of usual a.out link address, but someone can always link a.out at un-usual address.

If instead you link ld.so as ET_DYN, with load address of zero, then none of the above problems could happen.

like image 147
Employed Russian Avatar answered Sep 28 '22 07:09

Employed Russian