Check if there is a specific symbol in the executable file and its dependent libraries (simplified version)

Purpose

Sometimes you want to determine if a particular symbol is used in your current execution environment when debugging. (When you want to hook a specific function with LD_PRELOAD, etc. => Reference: Replace printf later with LD_PRELOAD --Qiita)

The investigation method in that case is summarized.

Execution source

Basically, I just use ldd and readelf, but I made it a tool because it was a big deal.

elf_symbol_searcher.sh


#!/bin/bash

APP=$1
SYMBOL=$2

LIST=$(echo $APP; ldd $APP \
    | grep -v -E 'ld-linux*.so' \
    | grep -v 'linux-vdso.so' \
    | grep -v 'linux-gate.so' \
    | awk '{print $3}')

for f in $LIST
do
    echo $f
    readelf -s -W $f | grep $SYMBOL
done

I'm getting the dependent libraries with ldd, but ld-linux * .so and Linux Virtual Dynamic Shared Objects (Reference library / l-lpic1-102-3 /)) is excluded. You can get the symbol with nm, but you can't see the symbol if it is strip, so we are using readelf this time.

The execution example is as follows. This time, the target is / usr / bin / weston, and the search target is ʻeglSwapBuffers`.

Execution result


$ ./elf_symbol_searcher.sh /usr/bin/weston eglSwapBuffers
/usr/bin/weston
   166: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND eglSwapBuffers
/usr/lib/x86_64-linux-gnu/libwayland-server.so.0
/usr/lib/x86_64-linux-gnu/libpixman-1.so.0
/usr/lib/x86_64-linux-gnu/mesa-egl/libEGL.so.1
   180: 0000000000006fb0   380 FUNC    GLOBAL DEFAULT   11 eglSwapBuffersRegionNOK
   187: 0000000000009850   339 FUNC    GLOBAL DEFAULT   11 eglSwapBuffers
   235: 00000000000079e0   427 FUNC    GLOBAL DEFAULT   11 eglSwapBuffersWithDamageEXT
/usr/lib/x86_64-linux-gnu/mesa-egl/libGLESv2.so.2
/usr/lib/x86_64-linux-gnu/libxkbcommon.so.0
/lib/x86_64-linux-gnu/libdl.so.2
/lib/x86_64-linux-gnu/libm.so.6
/lib/x86_64-linux-gnu/libc.so.6
/usr/lib/x86_64-linux-gnu/libffi.so.6
/lib/x86_64-linux-gnu/librt.so.1
/lib/x86_64-linux-gnu/libpthread.so.0
/usr/lib/x86_64-linux-gnu/libX11-xcb.so.1
/usr/lib/x86_64-linux-gnu/libX11.so.6
/usr/lib/x86_64-linux-gnu/libxcb-dri2.so.0
/usr/lib/x86_64-linux-gnu/libxcb-xfixes.so.0
/usr/lib/x86_64-linux-gnu/libxcb.so.1
/usr/lib/x86_64-linux-gnu/libwayland-client.so.0
/usr/lib/x86_64-linux-gnu/libgbm.so.1
/usr/lib/x86_64-linux-gnu/libdrm.so.2
/usr/lib/x86_64-linux-gnu/libglapi.so.0
/usr/lib/x86_64-linux-gnu/libXau.so.6
/usr/lib/x86_64-linux-gnu/libXdmcp.so.6

It's very simple, but I was able to find out that there are symbols in libEGL, which implements weston and ʻeglSwapBuffers` in the body.

The script is also published below. (I plan to write it in python to make it a little easier to use) https://github.com/koara-local/elf_symbol_searcher

TODO

The above can also be used for debugging purposes, but it would be nice if you could search in a little more detail.

Such

reference

Linux 101 Exam Preparation: Managing Shared Libraries

Recommended Posts

Check if there is a specific symbol in the executable file and its dependent libraries (simplified version)
[Golang] Check if a specific character string is included in the character string
If branch depending on whether there is a specific element in the list
Check if the string is a number in python
If you get a no attribute error in boto3, check the version
I want to receive the configuration file and check if the JSON file generated by jinja2 is a valid JSON
I created a script to check if English is entered in the specified position of the JSON file in Python.
How to check in Python if one of the elements of a list is in another list
Check if it is Unix in the scripting language
Check if it is Unix in the scripting language
Checks if there is a specific character string for all files under the directory that is Python and outputs the target line
When reading an image with SimpleITK, there is a problem if there is Japanese in the path
[Sublime Text 2] Always execute a specific file in the project
Save the pystan model and results in a pickle file
Is there a bias in the numbers that appear in the Fibonacci numbers?
What to do if you cat or tail a binary file and the terminal is garbled
I made a function to check if the webhook is received in Lambda for the time being
Python will fail if there is a space after the backslash
[Blender] Script to check if the selected one is a mesh
Anyway, the fastest serial communication log is left in a file
Delete a particular character in Python if it is the last
Count specific strings in a file
Is there a special in scipy? ??
What to do if there is a decimal in python json .dumps
Replace the directory name and the file name in the directory together with a Linux command.
I made a program to check the size of a file in Python