Skip to content

Commit

Permalink
Merge pull request #44 from akawashiro/stop-using-glog
Browse files Browse the repository at this point in the history
Stop using glog
  • Loading branch information
akawashiro authored Oct 13, 2023
2 parents b375453 + cf8a6cc commit 82ec62a
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 38 deletions.
30 changes: 12 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,17 @@ set(CUSTOM_LIBC_PATH CACHE PATH "")
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

include(third-party)
get_third_party(glog)
set(WITH_GFLAGS
OFF
CACHE INTERNAL "" FORCE)
set(WITH_UNWIND OFF)
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/glog
${CMAKE_CURRENT_BINARY_DIR}/glog EXCLUDE_FROM_ALL)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/glog
${CMAKE_CURRENT_BINARY_DIR}/glog/src)
add_definitions(-DC10_USE_GLOG=1)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/raw_write)

add_library(tls_secure tls_secure.cc)

add_executable(sloader sloader.cc dyn_loader.cc libc_mapping.cc sloader_dl.cc
utils.cc sloader_static.map)
target_link_libraries(sloader glog tls_secure)
target_compile_options(sloader PUBLIC -Wall -Werror)
target_link_options(sloader PUBLIC -static -Wl,-verbose -T
${CMAKE_CURRENT_SOURCE_DIR}/sloader_static.map)

if(CUSTOM_LIBC_PATH)
execute_process(
COMMAND ${CMAKE_CXX_COMPILER} --print-file-name=crtbegin.o
Expand All @@ -49,11 +37,10 @@ if(CUSTOM_LIBC_PATH)
OUTPUT_VARIABLE LIBSTDCXX
OUTPUT_STRIP_TRAILING_WHITESPACE)

add_executable(sloader_custom_libc sloader.cc dyn_loader.cc libc_mapping.cc
sloader_dl.cc utils.cc sloader_static.map)
add_executable(sloader sloader.cc dyn_loader.cc libc_mapping.cc sloader_dl.cc
utils.cc sloader_static.map)
target_link_libraries(
sloader_custom_libc
glog
sloader
${CUSTOM_LIBC_PATH}/lib/crt1.o
${CUSTOM_LIBC_PATH}/lib/crti.o
${CRT_BEGIN}
Expand All @@ -67,9 +54,9 @@ if(CUSTOM_LIBC_PATH)
${CRT_END}
${CUSTOM_LIBC_PATH}/lib/crtn.o
tls_secure)
target_compile_options(sloader_custom_libc PUBLIC -Wall -Werror)
target_compile_options(sloader PUBLIC -Wall -Werror)
target_link_options(
sloader_custom_libc
sloader
BEFORE
PUBLIC
-nostdlib
Expand All @@ -78,6 +65,13 @@ if(CUSTOM_LIBC_PATH)
-Wl,-verbose
-T
${CMAKE_CURRENT_SOURCE_DIR}/sloader_static.map)
else()
add_executable(sloader sloader.cc dyn_loader.cc libc_mapping.cc sloader_dl.cc
utils.cc sloader_static.map)
target_link_libraries(sloader tls_secure)
target_compile_options(sloader PUBLIC -Wall -Werror)
target_link_options(sloader PUBLIC -static -Wl,-verbose -T
${CMAKE_CURRENT_SOURCE_DIR}/sloader_static.map)
endif()

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/CTestCustom.cmake
Expand Down
11 changes: 6 additions & 5 deletions debian-Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
FROM debian:12.2
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y ninja-build cmake gcc g++ git python3 python3-distutils python3-dev python3-pip nasm clang-format libcap-dev tmux zsh neovim
RUN apt-get update
RUN apt-get install -y ninja-build cmake gcc g++ git python3 python3-distutils python3-dev python3-pip nasm clang-format libcap-dev tmux zsh neovim ccache gawk bison
# RUN pip3 install torch==1.13.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
COPY . /sloader
WORKDIR /sloader
# TODO
# RUN ./run-format.sh
RUN ./scripts/clone_glibc.sh glibc-2.36
RUN ./scripts/build_glibc.sh
RUN rm -rf build
RUN mkdir build
RUN cmake -GNinja -S . -B build
RUN cmake --build build
RUN cmake -GNinja -S . -B build -DCUSTOM_LIBC_PATH=/sloader/glibc-install
RUN cmake --build build -j $(nproc)
RUN cd build && ctest --output-on-failure -j $(nproc)
RUN ./make-sloader-itself.sh
5 changes: 4 additions & 1 deletion dyn_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ std::vector<std::filesystem::path> read_ldsoconf() {

ELFBinary::ELFBinary(const std::filesystem::path path) : path_(path) {
int fd = open(path_.c_str(), O_RDONLY);
CHECK(fd >= 0) << path_;
LOG(INFO) << LOG_KEY(path_) << LOG_KEY(fd);
CHECK(fd >= 0);

size_t size = lseek(fd, 0, SEEK_END);
CHECK_GT(size, 8UL + 16UL);
Expand Down Expand Up @@ -598,6 +599,7 @@ Elf64_Addr DynLoader::TLSSymOffset(const std::string& name) {
return (reinterpret_cast<const char*>(sloader_dummy_to_secure_tls_space) + 4096 - addr);
}
LOG(FATAL) << "Cannot find " << name;
std::abort();
}

void DynLoader::Relocate() {
Expand Down Expand Up @@ -703,6 +705,7 @@ void DynLoader::Relocate() {
size = sym.st_size;
} else {
LOG(FATAL) << "Cannot find " << name;
std::abort();
break;
}
void* dest = reinterpret_cast<void*>(bin.base_addr() + r.r_offset);
Expand Down
5 changes: 0 additions & 5 deletions misc/build_glibc.sh

This file was deleted.

10 changes: 10 additions & 0 deletions scripts/build_glibc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#! /bin/bash -eux

cd $(git rev-parse --show-toplevel)/glibc-build
if which bear; then
LD_LIBRARY_PATH="" bear -- make all -j 20
LD_LIBRARY_PATH="" bear -- make install -j 20
else
LD_LIBRARY_PATH="" make all -j $(nproc)
LD_LIBRARY_PATH="" make install -j $(nproc)
fi
11 changes: 9 additions & 2 deletions misc/clone_glibc.sh → scripts/clone_glibc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

cd $(git rev-parse --show-toplevel)

if [[ $# -ne 1 ]]
then
echo "Usage: $0 <glibc-version>"
exit 1
fi
GLIBC_VERSION=$1

if [[ ! -d glibc ]]
then
git clone git@github.com:akawashiro/glibc.git
git clone --depth 1 --no-single-branch https://github.com/akawashiro/glibc.git

pushd glibc
git remote add upstream https://sourceware.org/git/glibc.git
git fetch --all
git checkout glibc-2.35
git checkout ${GLIBC_VERSION}
popd
fi

Expand Down
8 changes: 4 additions & 4 deletions sloader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ Elf64_Half GetEType(const std::filesystem::path& filepath) {
size_t mapped_size = (size + 0xfff) & ~0xfff;

char* p = (char*)mmap(NULL, mapped_size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
CHECK(p != MAP_FAILED) << LOG_BITS(mapped_size) << LOG_BITS(size) << LOG_KEY(filepath);
LOG(FATAL) << LOG_BITS(mapped_size) << LOG_BITS(size) << LOG_KEY(filepath);
CHECK(p != MAP_FAILED);

Elf64_Ehdr* ehdr = reinterpret_cast<Elf64_Ehdr*>(p);
return ehdr->e_type;
}

int main(int argc, char* const argv[], char** envp) {
google::InitGoogleLogging(argv[0]);

std::string argv0 = argv[1];
std::filesystem::path fullpath;

Expand All @@ -56,7 +55,7 @@ int main(int argc, char* const argv[], char** envp) {
}

LOG(INFO) << LOG_KEY(fullpath);
CHECK(std::filesystem::exists(fullpath)) << fullpath;
CHECK(std::filesystem::exists(fullpath));

std::vector<std::string> args;
for (int i = 1; i < argc; i++) {
Expand All @@ -74,5 +73,6 @@ int main(int argc, char* const argv[], char** envp) {
GetDynLoader()->Run();
} else {
LOG(FATAL) << "Unsupported etype = " << LOG_KEY(etype);
std::abort();
}
}
2 changes: 0 additions & 2 deletions utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ std::string ShowSTB(unsigned char bind) {
return "STB_HIPROC";
default: {
return std::to_string(bind);
// LOG(FATAL) << LOG_KEY(bind);
// std::abort();
}
}
}
Expand Down
23 changes: 22 additions & 1 deletion utils.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
#pragma once

#include <elf.h>
#include <glog/logging.h>

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <vector>

class nullstream : public std::ostream {
public:
nullstream() : std::ostream(nullptr) {}
};
inline nullstream null;

#define CHECK(x) \
if (!(x)) { \
std::cerr << "CHECK failed: " << #x << std::endl; \
exit(1); \
}
#define CHECK_GT(x, y) CHECK((x) > (y))
#define CHECK_LT(x, y) CHECK((x) < (y))
#define CHECK_EQ(x, y) CHECK((x) == (y))
#define CHECK_NE(x, y) CHECK((x) != (y))
#define CHECK_GE(x, y) CHECK((x) >= (y))
#define CHECK_LE(x, y) CHECK((x) <= (y))
/* Replace null with std::cout to enable logging */
#define LOG(loglevel) \
null << #loglevel << " " << __FILE__ << ":" << __LINE__ << " " \
<< " "

#define LOG_KEY_VALUE(key, value) " " << key << "=" << value
#define LOG_KEY(key) LOG_KEY_VALUE(#key, key)
#define LOG_64BITS(key) LOG_KEY_VALUE(#key, HexString(key, 16))
Expand Down

0 comments on commit 82ec62a

Please sign in to comment.