这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ set(PACKAGE_NAME "termux-elf-cleaner" CACHE STRING "Name of the package")

add_executable("${PACKAGE_NAME}"
elf-cleaner.cpp
arghandling.c
)

target_compile_definitions("${PACKAGE_NAME}"
Expand Down
73 changes: 0 additions & 73 deletions arghandling.c

This file was deleted.

5 changes: 0 additions & 5 deletions arghandling.h

This file was deleted.

120 changes: 81 additions & 39 deletions elf-cleaner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ along with termux-elf-cleaner. If not, see
#endif

#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -38,17 +39,18 @@ along with termux-elf-cleaner. If not, see
#include <thread>
#include <vector>

#include "arghandling.h"

// Include a local elf.h copy as not all platforms have it.
#include "elf.h"

/* Taken from emacs */
#define ARRAYELTS(arr) (sizeof (arr) / sizeof (arr)[0])

/* Default to api level 21 unless arg --api-level given */
uint8_t supported_dt_flags_1 = (DF_1_NOW | DF_1_GLOBAL);
int api_level = 21;

bool dry_run = false;
bool quiet = false;
int dry_run = 0;
int quiet = 0;

static char const *const usage_message[] =
{ "\
Expand Down Expand Up @@ -160,10 +162,11 @@ bool process_elf(uint8_t* bytes, size_t elf_file_size, char const* file_name)
printf("%s: Removing the %s dynamic section entry from '%s'\n",
PACKAGE_NAME, removed_name, file_name);
// Tag the entry with DT_NULL and put it last:
if (!dry_run)
if (!dry_run) {
dynamic_section_entry->d_tag = DT_NULL;
// Decrease j to process new entry index:
std::swap(dynamic_section[j--], dynamic_section[last_nonnull_entry_idx--]);
// Decrease j to process new entry index:
std::swap(dynamic_section[j--], dynamic_section[last_nonnull_entry_idx--]);
}
} else if (dynamic_section_entry->d_tag == DT_FLAGS_1) {
// Remove unsupported DF_1_* flags to avoid linker warnings.
decltype(dynamic_section_entry->d_un.d_val) orig_d_val =
Expand All @@ -182,14 +185,25 @@ bool process_elf(uint8_t* bytes, size_t elf_file_size, char const* file_name)
}
}
}
}
else if (api_level < 23 &&
} else if (api_level < 23 &&
(section_header_entry->sh_type == SHT_GNU_verdef ||
section_header_entry->sh_type == SHT_GNU_verneed ||
section_header_entry->sh_type == SHT_GNU_versym)) {
if (!quiet)
printf("%s: Removing version section from '%s'\n",
PACKAGE_NAME, file_name);
switch (section_header_entry->sh_type) {
case SHT_GNU_verdef:
printf("%s: Removing VERDEF section from '%s'\n",
PACKAGE_NAME, file_name);
break;
case SHT_GNU_verneed:
printf("%s: Removing VERNEED section from '%s'\n",
PACKAGE_NAME, file_name);
break;
case SHT_GNU_versym:
printf("%s: Removing VERSYM section from '%s'\n",
PACKAGE_NAME, file_name);
break;
}
if (!dry_run)
section_header_entry->sh_type = SHT_NULL;
}
Expand Down Expand Up @@ -297,49 +311,77 @@ int parse_file(const char *file_name)

int main(int argc, char **argv)
{
int skip_args = 0;
if (argc == 1 || argmatch(argv, argc, "-help", "--help", 3, NULL, &skip_args)) {
int c;
int options_index = 0;
int threads_count = std::thread::hardware_concurrency();

static struct option options[] = {
{"api-level", required_argument, NULL, 'a'},
{"dry-run", no_argument, &dry_run, 1},
{"jobs", required_argument, NULL, 'j'},
{"quiet", no_argument, &quiet, 1},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{0, 0, 0, 0}
};

while (true)
{
c = getopt_long(argc, argv, "hva:dqj:",
options, &options_index);

if (c == -1)
break;

switch (c) {
case 'a':
api_level = atoi(optarg);
if (api_level <= 0)
api_level = 21;
break;
case 'j':
threads_count = atoi(optarg);
if (threads_count < 1)
threads_count = 1;
break;
case 'v':
printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
printf(("%s\n"
"%s comes with ABSOLUTELY NO WARRANTY.\n"
"You may redistribute copies of %s\n"
"under the terms of the GNU General Public License.\n"
"For more information about these matters, "
"see the file named COPYING.\n"),
COPYRIGHT, PACKAGE_NAME, PACKAGE_NAME);
return 0;
case 'h':
printf("Usage: %s [OPTION-OR-FILENAME]...\n", argv[0]);
for (unsigned int i = 0; i < ARRAYELTS(usage_message); i++)
fputs(usage_message[i], stdout);
return 0;
}
}

if (optind >= argc) {
printf("Usage: %s [OPTION-OR-FILENAME]...\n", argv[0]);
for (unsigned int i = 0; i < ARRAYELTS(usage_message); i++)
fputs(usage_message[i], stdout);
return 0;
}

if (argmatch(argv, argc, "-version", "--version", 3, NULL, &skip_args)) {
printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
printf(("%s\n"
"%s comes with ABSOLUTELY NO WARRANTY.\n"
"You may redistribute copies of %s\n"
"under the terms of the GNU General Public License.\n"
"For more information about these matters, "
"see the file named COPYING.\n"),
COPYRIGHT, PACKAGE_NAME, PACKAGE_NAME);
return 0;
}

argmatch(argv, argc, "-api-level", "--api-level", 3, &api_level, &skip_args);
int files_count = argc - (optind);
if (argc - (optind) <= threads_count)
threads_count = files_count;

if (api_level >= 23) {
// The supported DT_FLAGS_1 values as of Android 6.0.
supported_dt_flags_1 = (DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE);
}

if (argmatch(argv, argc, "-dry-run", "--dry-run", 3, NULL, &skip_args))
dry_run = true;

if (argmatch(argv, argc, "-quiet", "--quiet", 3, NULL, &skip_args))
quiet = true;

int threads_count = std::thread::hardware_concurrency();
int files_count = argc - (skip_args + 1);
argmatch(argv, argc, "-jobs", "--jobs", 1, &threads_count, &skip_args);
if (argc - (skip_args + 1) <= threads_count) threads_count = files_count;
if (threads_count < 1) threads_count = 1;

std::vector<std::future<void>> futures;
std::counting_semaphore sem(threads_count);

for (int i = skip_args + 1; i < argc; i++) {
for (int i = optind; i < argc; i++) {
sem.acquire();
const char* file = argv[i];
futures.push_back(std::async([file, &sem]() {
Expand Down
Empty file modified tests/curl-7.83.1-aarch64-api21-cleaned
100755 → 100644
Empty file.
Empty file modified tests/curl-7.83.1-aarch64-api24-cleaned
100755 → 100644
Empty file.
Empty file modified tests/curl-7.83.1-aarch64-original
100755 → 100644
Empty file.
Empty file modified tests/curl-7.83.1-arm-api21-cleaned
100755 → 100644
Empty file.
Empty file modified tests/curl-7.83.1-arm-api24-cleaned
100755 → 100644
Empty file.
Empty file modified tests/curl-7.83.1-arm-original
100755 → 100644
Empty file.
Empty file modified tests/curl-7.83.1-i686-api21-cleaned
100755 → 100644
Empty file.
Empty file modified tests/curl-7.83.1-i686-api24-cleaned
100755 → 100644
Empty file.
Empty file modified tests/curl-7.83.1-i686-original
100755 → 100644
Empty file.
Empty file modified tests/curl-7.83.1-x86_64-api21-cleaned
100755 → 100644
Empty file.
Empty file modified tests/curl-7.83.1-x86_64-api24-cleaned
100755 → 100644
Empty file.
Empty file modified tests/curl-7.83.1-x86_64-original
100755 → 100644
Empty file.
30 changes: 16 additions & 14 deletions tests/test-dynamic-section.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -e

if [ $# != 5 ]; then
echo "Usage path/to/test-dynamic-section.sh <termux-elf-cleaner> <source-dir> <binary-name> <arch> <api>"
echo "Usage path/to/test-dynamic-section.sh <elf-cleaner> <source-dir> <binary-name> <arch> <api>"
exit 1
fi

Expand All @@ -11,35 +11,37 @@ source_dir="$2"
binary_name="$3"
arch="$4"
api="$5"
test_dir="$(dirname $1)/tests"

progname="$(basename "$elf_cleaner")"
basefile="$source_dir/tests/$binary_name-$arch"
origfile="$basefile-original"
testfile="$basefile-api$api.test"
expectedfile="$basefile-api$api-cleaned"
testfile="$(basename $origfile)"

if [ "$api" = "21" ]; then
expected_logs="$progname: Removing version section from '$testfile'
$progname: Removing version section from '$testfile'
$progname: Removing the DT_RUNPATH dynamic section entry from '$testfile'
$progname: Removing the DT_VERNEEDNUM dynamic section entry from '$testfile'
$progname: Removing the DT_VERNEED dynamic section entry from '$testfile'
$progname: Removing the DT_VERSYM dynamic section entry from '$testfile'
$progname: Replacing unsupported DF_1_* flags 134217737 with 1 in '$testfile'
$progname: Removing the DT_GNU_HASH dynamic section entry from '$testfile'"
expected_logs="$progname: Removing VERSYM section from '$test_dir/$testfile'
$progname: Removing VERNEED section from '$test_dir/$testfile'
$progname: Removing the DT_RUNPATH dynamic section entry from '$test_dir/$testfile'
$progname: Removing the DT_VERNEEDNUM dynamic section entry from '$test_dir/$testfile'
$progname: Removing the DT_VERNEED dynamic section entry from '$test_dir/$testfile'
$progname: Removing the DT_VERSYM dynamic section entry from '$test_dir/$testfile'
$progname: Replacing unsupported DF_1_* flags 134217737 with 1 in '$test_dir/$testfile'
$progname: Removing the DT_GNU_HASH dynamic section entry from '$test_dir/$testfile'"
elif [ "$api" = "24" ]; then
expected_logs="$progname: Replacing unsupported DF_1_* flags 134217737 with 9 in '$testfile'"
expected_logs="$progname: Replacing unsupported DF_1_* flags 134217737 with 9 in '$test_dir/$testfile'"
else
echo "Unknown API level $api"
exit 1
fi

cp "$origfile" "$testfile"
if [ "$("$elf_cleaner" --api-level "$api" "$testfile")" != "$expected_logs" ]; then
mkdir -p "$test_dir"
cp "$origfile" "$test_dir/"
if [ "$("$elf_cleaner" --api-level "$api" "$test_dir/$testfile")" != "$expected_logs" ]; then
echo "Logs do not match for $testfile"
exit 1
fi
if not cmp -s "$testfile" "$expectedfile"; then
if not cmp -s "$test_dir/$testfile" "$expectedfile"; then
echo "Expected and actual files differ for $testfile"
exit 1
fi
12 changes: 7 additions & 5 deletions tests/test-threads.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@
set -e

if [ $# != 2 ]; then
echo "Usage path/to/test-threads.sh <termux-elf-cleaner> <source-dir>"
echo "Usage path/to/test-threads.sh <elf-cleaner> <source-dir>"
exit 1
fi

elf_cleaner="$1"
source_dir="$2"
test_dir="$(dirname $1)/tests"

mkdir -p "$test_dir"
for i in {1..100}; do
for arch in aarch64 arm i686 x86_64; do
for api in 21 24; do
cp "$source_dir/tests/curl-7.83.1-$arch-original" "$source_dir/tests/curl-8.83.1-$arch-api$api-threads-$i.test"
cp "$source_dir/tests/curl-7.83.1-$arch-original" "$test_dir/curl-7.83.1-$arch-api$api-threads-$i.test"
done
done
done

for api in 21 24; do
"$elf_cleaner" --api-level "$api" --quiet --jobs 4 "$source_dir/tests/curl-8.83.1-"*"-api$api-threads"*".test"
"$elf_cleaner" --api-level "$api" --quiet --jobs 4 "$test_dir/curl-7.83.1-"*"-api$api-threads"*".test"
done

for i in {1..100}; do
for arch in aarch64 arm i686 x86_64; do
for api in 21 24; do
if not cmp -s "$source_dir/tests/curl-7.83.1-$arch-api$api-cleaned" "$source_dir/tests/curl-8.83.1-$arch-api$api-threads-$i.test"; then
echo "Expected and actual files differ for $source_dir/tests/curl-8.83.1-$arch-threads-$i.test"
if not cmp -s "$source_dir/tests/curl-7.83.1-$arch-api$api-cleaned" "$test_dir/curl-7.83.1-$arch-api$api-threads-$i.test"; then
echo "Expected and actual files differ for curl-7.83.1-$arch"
exit 1
fi
done
Expand Down
Loading
Loading