Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ELF Dynamic loader symbol lookup ordering

What is the search order for symbol lookup when resolving dynamic relocations?

When resolving symbols for a shared library does the loader first search in the 'main executable' (to let the main executable override definitions...) or what?

like image 540
JohnTortugo Avatar asked Oct 01 '12 00:10

JohnTortugo


People also ask

How to find all ELF objects loaded by the dynamic linker?

The ldd tool can be used to find all the ELF objects loaded by the dynamic linker: The dynamic linker uses symbols to link ELF objects together: a component can export a symbol corresponding to a a functions or a variable which is instantiated in this ELF object;

How many processing Elf relocations are there in the dynamic loader?

ELF Dynamic loader symbol lookup ordering 30 Processing ELF relocations - understanding the relocs, symbols, section data, and how they work together 12 Linking error with `libopencv_highgui.so` under Ubuntu 14.04, strange result with `libtiff.so.5` 1 GCC: why global variable missing in dynamic symbol table? 2

How to change the loader for a specific Elf?

Whereas to change the loader for a specific ELF, we can use patchelf and specify the interpreter and the elf to patch: But, when shipping such a specially crafted package, one also has to make sure that loader loads the libraries he shipped with it, and not the ones pre-installed on the target system.

What is the best reference for elf types and flags values?

Python pwnypack package documentation: Great reference for ELF types and flags values. For example, supported flags in the FLAGS and FLAGS_1 sections


1 Answers

Per my understanding, each executable object has its own "lookup scope":

  • The main executable is usually the first object in the "global" lookup scope. This means that symbols defined in the main executable would override those in dependent shared libraries. Shared objects that are added using the LD_PRELOAD facility are added to the global lookup scope, right after the main executable.
  • However, if the shared object being loaded uses the DF_SYMBOLIC flag, then symbol references that originate within that object will look for definitions within the object before searching in the global lookup scope.
  • Shared objects opened using dlopen() may have their own dependencies. If the RTLD_GLOBAL flag was not set during the call to dlopen(), these dependencies are added to the lookup scope for that object, but do not affect the global lookup scope. If the RTLD_GLOBAL flag was passed to dlopen(), then the shared object (and its dependencies) will be added to the "global" lookup scope, changing the behavior of subsequent symbol lookups.

Ulrich Drepper's guide "How to Write Shared Libraries" is recommended reading on this topic.

like image 78
jkoshy Avatar answered Nov 13 '22 13:11

jkoshy