Skip to content

Commit

Permalink
optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
leleliu008 committed Jan 16, 2025
1 parent 5215023 commit 060dd7a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 200 deletions.
201 changes: 2 additions & 199 deletions ndk-pkg
Original file line number Diff line number Diff line change
Expand Up @@ -8961,209 +8961,12 @@ EOF
##################################################################################

if [ "$LINUX" = 1 ] ; then
wfetch 'https://ziglang.org/builds/zig-linux-x86_64-0.14.0-dev.2627+6a21d18ad.tar.xz' --no-buffer
wfetch 'https://ziglang.org/builds/zig-linux-x86_64-0.14.0-dev.2647+5322459a0.tar.xz' --no-buffer
run $TAR xf zig-linux-x86_64-*.tar.xz --strip-components=1 --no-same-owner

wfetch 'https://github.com/leleliu008/test/releases/download/2023.10.08/binutils-2.43.1-linux-any-x86_64.release.tar.xz' --no-buffer
run $TAR xf binutils-*.tar.xz --strip-components=1 --no-same-owner

# https://github.com/ziglang/zig/issues/19790
for item in cc c++
do
cat > "zig-$item" <<EOF
#!/bin/sh
set -e
unset CC CXX AS LD AR RANLIB
exec "$NDKPKG_CORE_DIR/zig" $item "\$@" --target=x86_64-linux-gnu
EOF
chmod +x "zig-$item"
done

cat > check-if-has-dynamic-section.c <<EOF
#include <stdio.h>
#include <stdlib.h>

#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

#include <elf.h>


int handle_elf32(const int fd, const char * const fp) {
Elf32_Ehdr ehdr;

int ret = read(fd, &ehdr, sizeof(Elf32_Ehdr));

if (ret == -1) {
perror(fp);
return 11;
}

if (ret != sizeof(Elf32_Ehdr)) {
perror(fp);
return 12;
}

///////////////////////////////////////////////////////////

Elf32_Phdr phdr;

for (unsigned int i = 1; i < ehdr.e_phnum; i++) {
ret = pread(fd, &phdr, sizeof(Elf32_Phdr), ehdr.e_phoff + i * ehdr.e_phentsize);

if (ret == -1) {
perror(fp);
return 17;
}

if ((size_t)ret != sizeof(Elf32_Phdr)) {
perror(fp);
fprintf(stderr, "not fully read.\n");
return 18;
}

if (phdr.p_type == PT_DYNAMIC) {
return 0;
}
}

fprintf(stderr, "no .dynamic section in file: %s\n", fp);
return 200;
}

int handle_elf64(const int fd, const char * const fp) {
Elf64_Ehdr ehdr;

int ret = read(fd, &ehdr, sizeof(Elf64_Ehdr));

if (ret == -1) {
perror(fp);
return 11;
}

if (ret != sizeof(Elf64_Ehdr)) {
perror(fp);
return 12;
}

///////////////////////////////////////////////////////////

Elf64_Phdr phdr;

for (unsigned int i = 1; i < ehdr.e_phnum; i++) {
ret = pread(fd, &phdr, sizeof(Elf64_Phdr), ehdr.e_phoff + i * ehdr.e_phentsize);

if (ret == -1) {
perror(fp);
return 17;
}

if ((size_t)ret != sizeof(Elf64_Phdr)) {
perror(fp);
fprintf(stderr, "not fully read.\n");
return 18;
}

if (phdr.p_type == PT_DYNAMIC) {
return 0;
}
}

fprintf(stderr, "no .dynamic section in file: %s\n", fp);
return 200;
}

int main(int argc, const char *argv[]) {
if (argc != 2) {
printf("Usage: %s <ELF-FILEPATH>\n", argv[0]);
return 1;
}

if (argv[1][0] == '\0') {
printf("Usage: %s <ELF-FILEPATH>, <ELF-FILEPATH> is unspecified.\n", argv[0]);
return 2;
}

int fd = open(argv[1], O_RDONLY);

if (fd == -1) {
perror(argv[1]);
return 3;
}

struct stat st;

if (fstat(fd, &st) == -1) {
perror(argv[1]);
close(fd);
return 4;
}

if (st.st_size < 5) {
fprintf(stderr, "NOT an ELF file: %s\n", argv[1]);
close(fd);
return 100;
}

///////////////////////////////////////////////////////////

unsigned char a[5];

ssize_t readBytes = read(fd, a, 5);

if (readBytes == -1) {
perror(argv[0]);
close(fd);
return 5;
}

if (readBytes != 5) {
perror(argv[0]);
close(fd);
fprintf(stderr, "not fully read.\n");
return 6;
}

///////////////////////////////////////////////////////////

// https://www.sco.com/developers/gabi/latest/ch4.eheader.html
if ((a[0] != 0x7F) || (a[1] != 0x45) || (a[2] != 0x4C) || (a[3] != 0x46)) {
fprintf(stderr, "NOT an ELF file: %s\n", argv[1]);
close(fd);
return 100;
}

///////////////////////////////////////////////////////////

off_t offset = lseek(fd, 0, SEEK_SET);

if (offset == -1) {
perror(argv[0]);
close(fd);
return 7;
}

///////////////////////////////////////////////////////////

int ret;

switch (a[4]) {
case 1: ret = handle_elf32(fd, argv[1]); break;
case 2: ret = handle_elf64(fd, argv[1]); break;
default:
fprintf(stderr, "Invalid ELF file: %s\n", argv[1]);
ret = 101;
}

close(fd);

return ret;
}
EOF
unset CC CXX AS LD
run ./zig cc --target=x86_64-linux-musl -flto -Os -s -o check-if-has-dynamic-section check-if-has-dynamic-section.c

run ln -s ../wrapper-native-cc bin/cc
run ln -s ../wrapper-native-c++ bin/c++
fi
Expand Down Expand Up @@ -9693,7 +9496,7 @@ fi
fi
}

NDKPKG_VERSION=0.19.5
NDKPKG_VERSION=0.19.6

NDKPKG_ARG0="$0"
NDKPKG_ARG1="$1"
Expand Down
2 changes: 1 addition & 1 deletion ndk-pkg-core-latest-release-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024.12.13
2025.01.16

0 comments on commit 060dd7a

Please sign in to comment.