24 Eylül 2019 Salı

Linux Loader

Giriş
Bazı notlarım şöyle

1. Loader Nedir?
Açıklaması şöyle.
The shell invokes the loader function, which copies the code and data in the executable file a.out into memory, and then transfers control to the beginning of the program. The loader is a program called execve, which loads the code and data of the executable object file into memory and then runs the program by jumping to the first instruction.
2. Loader Tarafından Kullanılan Path
Loader'ın yükleyeceği path'ler şurada dizinde tanımlı ve sıraları önemli.
/etc/ld.so.conf.d/
Bu dosyalarda değişiklik yaparsak şu komutu çalıştırmak gerekli.
ldconfig
3. Loader Nerede?
Loader şudur.
/lib64/ld-linux-x86-64.so.2
Örnek
Şöyle yaparız
$ ldd /lib64/ld-linux-x86-64.so.2
        statically linked
Örnek
ld-linux.so.2 aslında bir loader. Centos 6'da uygulamadan çıkmak yeterli değil. Bir sebepten ld-linux.so.2 loader'ını da kill etmek gerekir.

Örnek
Loader ile bir uygulama çalıştırmak için şöyle yaparız.
# /lib64/ld-linux-x86-64.so.2 ./chmod
chmod: missing operand
4. loader ve malloc
loader libc.so dosyasını yüklemeden kendi içindeki malloc metodunu kullanır. Açıklaması şöyle.
Technically the dynamic linker doesn’t need object resolution and relocation for itself, since it’s fully resolved as-is, but it does define symbols and it has to take care of those when resolving the binary it’s “interpreting”, and those symbols are updated to point to their implementations in the loaded libraries. In particular, this affects malloc — the linker has a minimal version built-in, with the corresponding symbol, but that’s replaced by the C library’s version once it’s loaded and relocated (or even by an interposed version if there is one), with some care taken to ensure this doesn’t happen at a point where it might break the linker.
Örnek
Bir projede Centos 7 ile derlenmiş binary dosyayı Centos 6 üzerinde çalıştırmak gerekti.
Nasıl yapacağımı buradan okudum.

Şöyle yaptık.

1. ldd komutu ile binary dosyanın bağımlı olduğu tüm .so dosyalarını buldum. Bağımlılıklar arasında ld-linux.so.2 dosyası da vardı.
2. Bu dosyaları ve binary dosyayı Centos 6'da açtığım bir dizine kopyaladım.
3. env PRE_LOAD= ... ld-linux.so myprogram şeklinde çalıştırdım


Hiç yorum yok:

Yorum Gönder