Skip to content

Commit

Permalink
Update scripts/gen_tests_elf_py.py to not use libdrgn/include/elf.h
Browse files Browse the repository at this point in the history
libdrgn/include/elf.h no longer has the definitions that the script
needs. Update it to use the system elf.h and update tests/elf.py with one
new definition. Also make scripts/gen_elf_compat.py read elf.h in the
same way.

Signed-off-by: Omar Sandoval <[email protected]>
  • Loading branch information
osandov committed Aug 24, 2023
1 parent 3d07cb1 commit 4a2734b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
33 changes: 14 additions & 19 deletions scripts/gen_elf_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,23 @@

def main() -> None:
argparse.ArgumentParser(
description="Generate definitions missing from older versions of glibc for libdrgn/include/elf.h from /usr/include/elf.h"
description="Generate definitions missing from older versions of glibc for libdrgn/include/elf.h from elf.h"
).parse_args()

macros = {}

regex = re.compile(r"\s*#\s*define\s+(" + "|".join(MACROS) + r")\s+(.*)")
# Dump out just the macro definitions with gcc.
with subprocess.Popen(
[
"gcc",
"-dM",
"-E",
"/usr/include/elf.h",
],
stdout=subprocess.PIPE,
contents = subprocess.check_output(
["gcc", "-dD", "-E", "-"],
input="#include <elf.h>\n",
universal_newlines=True,
) as proc:
for line in proc.stdout:
match = regex.match(line)
if not match:
continue
macros[match.group(1)] = match.group(2)
)

macros = {
match.group(1): match.group(2)
for match in re.finditer(
r"^\s*#\s*define\s+(" + "|".join(MACROS) + r")\s+(.*)",
contents,
re.MULTILINE,
)
}

print("// Generated by scripts/gen_elf_compat.py.")
for macro in MACROS:
Expand Down
12 changes: 7 additions & 5 deletions scripts/gen_tests_elf_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
# SPDX-License-Identifier: LGPL-2.1-or-later

import argparse
from pathlib import Path
import subprocess
import re
import sys


def main() -> None:
argparse.ArgumentParser(
description="Generate tests/elf.py from libdrgn/include/elf.h"
description="Generate tests/elf.py from elf.h"
).parse_args()

contents = Path("libdrgn/include/elf.h").read_text()
contents = re.sub(r"/\*.*?\*/", "", contents, flags=re.DOTALL)
contents = re.sub(r"\\\n", "", contents)
contents = subprocess.check_output(
["gcc", "-dD", "-E", "-"],
input="#include <elf.h>\n",
universal_newlines=True,
)

enums = {
name: []
Expand Down
3 changes: 2 additions & 1 deletion tests/elf.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ class SHT(enum.IntEnum):
PREINIT_ARRAY = 0x10
GROUP = 0x11
SYMTAB_SHNDX = 0x12
NUM = 0x13
RELR = 0x13
NUM = 0x14
LOOS = 0x60000000
GNU_ATTRIBUTES = 0x6FFFFFF5
GNU_HASH = 0x6FFFFFF6
Expand Down

0 comments on commit 4a2734b

Please sign in to comment.