実行バイナリにライブラリの検索パスを埋め込みたい その2

コンパイル時にLDFLAGSに-Wl,-rpathをセットすることで、実行バイナリにシェアードライブラリの検索パスを埋め込む方法を過去記事で紹介しました。

今回はPKG_CONFIG_PATHを使用した例を紹介します。

例としてPHPをOpenSSLにリンクさせるために、PKG_CONFIG_PATHを使用します。

それでは早速、作業に入ります。
```
PKG_CONFIG_PATH=/usr/local/lib/openssl/lib64/pkgconfig:/lib64/pkgconfig:/usr/lib64/pkgconfig \
./configure \
--prefix=/usr/local/ap/php \
--enable-fpm \
--with-config-file-path=/usr/local/ap/php/etc \
--enable-libgcc \
--with-openssl \
--with-zlib \
--with-zlib-dir=/usr \
--enable-gd \
--with-freetype \
--with-jpeg \
--with-xpm \
--with-webp \
--enable-bcmath \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-dba=shared \
--enable-exif \
--with-curl \
--enable-gd-jis-conv \
--with-gettext \
--enable-mbstring \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-pcntl \
--with-readline \
--enable-soap \
--enable-sockets \
--with-xsl \
--with-imap \
--with-imap-ssl \
--with-libdir=lib64 \
--with-kerberos \
--with-zip \
--enable-intl \
--enable-opcache \
--enable-shared
```
上記のようにpkgconfigへのパスをセットすると、-Wl,-rpathの替わりになってくれます。それではコンパイルしてインストールしましょう。
```
make all
make install
```
で、実際に生成されたバイナリの中身を確認します。
```
readelf -d ./php

Dynamic section at offset 0xfffb88 contains 64 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libcrypt.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc-client.so.2007]
 0x0000000000000001 (NEEDED)             Shared library: [libresolv.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libreadline.so.7]
 0x0000000000000001 (NEEDED)             Shared library: [libncurses.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libtinfo.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [librt.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libbz2.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libutil.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libxml2.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libgssapi_krb5.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libkrb5.so.3]
 0x0000000000000001 (NEEDED)             Shared library: [libk5crypto.so.3]
 0x0000000000000001 (NEEDED)             Shared library: [libcom_err.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libssl.so.3]
 0x0000000000000001 (NEEDED)             Shared library: [libcrypto.so.3]
 0x0000000000000001 (NEEDED)             Shared library: [libsqlite3.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libz.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libcurl.so.4]
 0x0000000000000001 (NEEDED)             Shared library: [libpng16.so.16]
 0x0000000000000001 (NEEDED)             Shared library: [libwebp.so.7]
 0x0000000000000001 (NEEDED)             Shared library: [libjpeg.so.62]
 0x0000000000000001 (NEEDED)             Shared library: [libXpm.so.4]
 0x0000000000000001 (NEEDED)             Shared library: [libX11.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libfreetype.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libicuio.so.60]
 0x0000000000000001 (NEEDED)             Shared library: [libicui18n.so.60]
 0x0000000000000001 (NEEDED)             Shared library: [libicuuc.so.60]
 0x0000000000000001 (NEEDED)             Shared library: [libicudata.so.60]
 0x0000000000000001 (NEEDED)             Shared library: [libonig.so.5]
 0x0000000000000001 (NEEDED)             Shared library: [libxslt.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libexslt.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libgcrypt.so.20]
 0x0000000000000001 (NEEDED)             Shared library: [libgpg-error.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libzip.so.5]
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000f (RPATH)              Library rpath: [/usr/lib/gcc/x86_64-redhat-linux/8:/usr/local/lib/openssl/lib64]
 0x000000000000000c (INIT)               0x4447d8
 0x000000000000000d (FINI)               0x8ef0b8
 0x0000000000000019 (INIT_ARRAY)         0x15ff9e8
 0x000000000000001b (INIT_ARRAYSZ)       16 (bytes)
 0x000000000000001a (FINI_ARRAY)         0x15ff9f8
 0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
 0x000000006ffffef5 (GNU_HASH)           0x400298
 0x0000000000000005 (STRTAB)             0x41f988
 0x0000000000000006 (SYMTAB)             0x405660
 0x000000000000000a (STRSZ)              96793 (bytes)
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000015 (DEBUG)              0x0
 0x0000000000000003 (PLTGOT)             0x1600000
 0x0000000000000002 (PLTRELSZ)           42240 (bytes)
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000017 (JMPREL)             0x43a2d8
 0x0000000000000007 (RELA)               0x439a20
 0x0000000000000008 (RELASZ)             2232 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x000000006ffffffe (VERNEED)            0x439690
 0x000000006fffffff (VERNEEDNUM)         13
 0x000000006ffffff0 (VERSYM)             0x4373a2
 0x0000000000000000 (NULL)               0x0
```
Library rpathを見るとpkgconfigで指定したパスがバイナリに埋め込まれていることがわかります。

ちなみにlddでリンクの状態を見ると
```
ldd ./php
        （途中省略）
        libssl.so.3 => /usr/local/lib/openssl/lib64/libssl.so.3 (0x00007f9ebc51e000)
        libcrypto.so.3 => /usr/local/lib/openssl/lib64/libcrypto.so.3 (0x00007f9ebbdce000)
        （途中省略）
```
pkgconfigで指定したシェアードライブラリにリンクされていることがわかります。

以上です。お疲れ様でした！