Skip to content

Commit

Permalink
Remove dependency to OpenSSL's RAND_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Sep 5, 2021
1 parent b495395 commit 76018da
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 11 additions & 1 deletion build-static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ RUN apt-get update && \
rm -rf /var/lib/apt/lists/*
EOF

LDFLAGS='-fuse-ld=lld -static'

# libstdc++'s `std::__glibcxx_rwlock_rdlock` refers these symbols
# as weak symbols, although they need to be defined. Otherwise,
# the program crashes after juping to address 0.
# So, we force loading symbols as a workaround.
LDFLAGS="$LDFLAGS -Wl,-u,pthread_rwlock_rdlock"
LDFLAGS="$LDFLAGS -Wl,-u,pthread_rwlock_unlock"
LDFLAGS="$LDFLAGS -Wl,-u,pthread_rwlock_wrlock"

docker run -it --rm -v `pwd`:/mold -u $(id -u):$(id -g) \
mold-build-ubuntu20 \
make -C /mold -j$(nproc) EXTRA_LDFLAGS='-fuse-ld=lld -static'
make -C /mold -j$(nproc) EXTRA_LDFLAGS="$LDFLAGS"
10 changes: 7 additions & 3 deletions elf/output-chunks.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "mold.h"

#include <openssl/rand.h>
#include <shared_mutex>
#include <sys/mman.h>
#include <tbb/parallel_for_each.h>
Expand Down Expand Up @@ -1665,8 +1664,13 @@ static void compute_sha256(Context<E> &ctx, i64 offset) {
template <typename E>
static std::vector<u8> get_uuid_v4(Context<E> &ctx) {
std::vector<u8> buf(16);
if (!RAND_bytes(buf.data(), buf.size()))
Fatal(ctx) << "RAND_bytes failed";

FILE *fp = fopen("/dev/urandom", "r");
if (!fp)
Fatal(ctx) << "cannot open /dev/urandom: " << strerror(errno);
if (fread(buf.data(), buf.size(), 1, fp) != 1)
Fatal(ctx) << "fread on /dev/urandom: short read";
fclose(fp);

// Indicate that this is UUIDv4.
buf[6] &= 0b00001111;
Expand Down

0 comments on commit 76018da

Please sign in to comment.