Last active
December 2, 2022 23:33
-
-
Save willcl-ark/bcc4fffe2abf6760c95913f0c42127d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
A script to check that generated out files which use special compilation units | |
do not contain any disallowed sections. See #18553 for additional context. | |
Special instructions include: | |
SSE42 | |
SSE41 | |
AVX | |
AVX2 | |
SHANI | |
Disallowed sections include: | |
.text.startup | |
Example usage: | |
python3 contrib/devtools/si-check.py | |
""" | |
import glob | |
import os | |
import re | |
import sys | |
import lief | |
cwd = os.getcwd() | |
DISALLOWED_SECTIONS = [ | |
".text.startup", | |
] | |
# Compile the regex of special instructions to search for | |
pattern = re.compile(r".*(SSE42|SSE41|AVX|AVX2|SHANI).*", re.IGNORECASE) | |
# Perform the search over file names | |
files = [file for file in glob.glob(f"{cwd}/**/*.o", recursive=True) if pattern.search(file)] | |
# Warn if no files found but don't fail. This could be a correct success, or it | |
# could be that glob did not find the files (run from wrong root directory) | |
if not files: | |
print(f"{__file__}: no special instruction *.o files found in {cwd}", file=sys.stderr) | |
sys.exit(0) | |
else: | |
print(f"{__file__}: checking {', '.join(files)}", file=sys.stdout) | |
# Parse files for disallowed sections | |
error = False | |
for file in files: | |
out_file = lief.parse(file) | |
for section in DISALLOWED_SECTIONS: | |
if out_file.has_section(section): | |
print(f"{__file__}: ERROR {file} contains disallowed section {section}", file=sys.stderr) | |
error = True | |
sys.exit(1) if error else sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment