Skip to content

Commit

Permalink
Add elf_strings index for offset
Browse files Browse the repository at this point in the history
* added recursive query for RPATH to README
* added index for elf_strings for offset column
  • Loading branch information
fzakaria committed Sep 27, 2023
1 parent dcab32d commit c3c4029
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,6 @@ polymorphic nature of the dynamic entries needed_.

The below uses a file built with [NixOS](https://nixos.org) as they all have RUNPATH set.

A recursive query can further be used to split the row into multiple rows.

```console
sqlelf /nix/store/gjr9ylm023rl9di484g1wxcd1jp84xxv-nix-2.8.1/bin/nix \
--sql "SELECT elf_strings.path, elf_strings.value
Expand All @@ -328,6 +326,33 @@ WHERE elf_dynamic_entries.tag = 'RUNPATH';"
└─────────────────────────────────────────────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```

A recursive query can further be used to split the row into multiple rows.

```console
sqlelf /nix/store/gjr9ylm023rl9di484g1wxcd1jp84xxv-nix-2.8.1/bin/nix \
--sql "WITH split(rpath, str) AS (
SELECT '', elf_strings.value||':' as rpath
FROM elf_dynamic_entries
INNER JOIN elf_strings ON elf_dynamic_entries.value = elf_strings.offset
WHERE elf_dynamic_entries.tag = 'RUNPATH'
UNION ALL SELECT
substr(str, 0, instr(str, ':')),
substr(str, instr(str, ':')+1)
FROM split WHERE str!=''
) SELECT rpath FROM split WHERE rpath!='';"
WARNING:root:SQLITE_LOG: automatic index on elf_strings(offset) (284) SQLITE_WARNING SQLITE_WARNING_AUTOINDEX
┌────────────────────────────────────────────────────────────────────┐
│ rpath │
│ /nix/store/gjr9ylm023rl9di484g1wxcd1jp84xxv-nix-2.8.1/lib │
│ /nix/store/pkxyfwarcq081rybpbnprjmnkiy1cz6g-libsodium-1.0.18/lib │
│ /nix/store/r6mrf9pz4dpax6rcszcmbyrpsk8j6saz-editline-1.17.1/lib │
│ /nix/store/ppm63lvkyfa58sgcnr2ddzh14dy1k9fn-boehm-gc-8.0.6/lib │
│ /nix/store/sgw2i15l01rwxzj62745h30bsjmh7wc1-lowdown-0.11.1-lib/lib │
│ /nix/store/bvy2z17rzlvkx2sj7fy99ajm853yv898-glibc-2.34-210/lib │
│ /nix/store/gka59hya7l7qp26s0rydcgq8hj0d7v7k-gcc-11.3.0-lib/lib │
└────────────────────────────────────────────────────────────────────┘
```

</details>


Expand Down
5 changes: 4 additions & 1 deletion sqlelf/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from functools import reduce
from typing import TextIO

from sqlelf import elf
from sqlelf import sql as api_sql


Expand Down Expand Up @@ -65,7 +66,9 @@ def start(args: list[str] = sys.argv[1:], stdin: TextIO = sys.stdin) -> None:
if len(filenames) == 0:
sys.exit("No valid ELF files were provided")

sql_engine = api_sql.make_sql_engine(filenames, recursive=program_args.recursive)
sql_engine = api_sql.make_sql_engine(
filenames, recursive=program_args.recursive, cache_flags=elf.CacheFlag.ALL()
)
shell = sql_engine.shell(stdin=stdin)

if program_args.sql and len(program_args.filenames) > 0:
Expand Down
5 changes: 5 additions & 0 deletions sqlelf/elf.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ def strings_generator() -> Iterator[dict[str, Any]]:
cache_flags,
)

if CacheFlag.STRINGS in cache_flags:
connection.execute(
"""CREATE INDEX elf_strings_offset_idx ON elf_strings (offset);"""
)


def split_with_index(str: str, delimiter: str) -> list[tuple[int, str]]:
"""Split a string with the delimiter and return the index
Expand Down

0 comments on commit c3c4029

Please sign in to comment.