Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed May 5, 2024
1 parent d683e8a commit 7b2cb47
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 31 deletions.
6 changes: 3 additions & 3 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ class Out {
SyncStream out{std::cout};
};

static std::string_view fatal_color = "mold: \033[0;1;31mfatal:\033[0m ";
static std::string_view fatal_mono = "mold: fatal: ";
static std::string_view error_color = "mold: \033[0;1;31merror:\033[0m ";
static std::string_view fatal_color = "mold: \033[0;1;31mfatal:\033[0m ";
static std::string_view error_mono = "mold: error: ";
static std::string_view warning_color = "mold: \033[0;1;35mwarning:\033[0m ";
static std::string_view error_color = "mold: \033[0;1;31merror:\033[0m ";
static std::string_view warning_mono = "mold: warning: ";
static std::string_view warning_color = "mold: \033[0;1;35mwarning:\033[0m ";

template <typename Context>
class Fatal {
Expand Down
8 changes: 4 additions & 4 deletions common/compress.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void ZlibCompressor::write_to(u8 *buf) {
offsets[i] = offsets[i - 1] + shards[i - 1].size();

tbb::parallel_for((i64)0, (i64)shards.size(), [&](i64 i) {
memcpy(&buf[offsets[i]], shards[i].data(), shards[i].size());
memcpy(buf + offsets[i], shards[i].data(), shards[i].size());
});

// Write a trailer
Expand All @@ -146,10 +146,10 @@ void ZlibCompressor::write_to(u8 *buf) {

static std::vector<u8> zstd_compress(std::string_view input) {
std::vector<u8> buf(ZSTD_COMPRESSBOUND(input.size()));
constexpr int level = 3; // compression level; must be between 1 to 22
constexpr int LEVEL = 3; // compression level; must be between 1 to 22

size_t sz = ZSTD_compress(buf.data(), buf.size(), input.data(), input.size(),
level);
LEVEL);
assert(!ZSTD_isError(sz));
buf.resize(sz);
buf.shrink_to_fit();
Expand Down Expand Up @@ -178,7 +178,7 @@ void ZstdCompressor::write_to(u8 *buf) {
offsets[i] = offsets[i - 1] + shards[i - 1].size();

tbb::parallel_for((i64)0, (i64)shards.size(), [&](i64 i) {
memcpy(&buf[offsets[i]], shards[i].data(), shards[i].size());
memcpy(buf + offsets[i], shards[i].data(), shards[i].size());
});
}

Expand Down
12 changes: 6 additions & 6 deletions elf/linker-script.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ class SyntaxError {
if (contents[i] == '\n')
lineno++;

std::string label = ctx.script_file->name + ":" +
std::to_string(lineno) + ": ";
i64 indent = strlen("mold: fatal: ") + label.size();
i64 column = errpos.data() - line.data();

std::stringstream ss;
ss << ctx.script_file->name << ":" << lineno << ": ";
i64 indent = (i64)ss.tellp() + strlen("mold: ");
ss << line << "\n" << std::setw(indent + column) << " " << "^ ";
out << ss.str();
out << label << line << "\n"
<< std::string(indent + column, ' ') << "^ ";
}

template <class T> SyntaxError &operator<<(T &&val) {
template <typename T> SyntaxError &operator<<(T &&val) {
out << std::forward<T>(val);
return *this;
}
Expand Down
24 changes: 6 additions & 18 deletions elf/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ template <typename E> u64 get_dtp_addr(Context<E> &);
// output-chunks.cc
//

template <typename E>
OutputSection<E> *find_section(Context<E> &ctx, u32 sh_type);

template <typename E>
OutputSection<E> *find_section(Context<E> &ctx, std::string_view name);

template <typename E>
u64 get_eflags(Context<E> &ctx) {
return 0;
Expand Down Expand Up @@ -2454,24 +2460,6 @@ inline InputSection<E> *ObjectFile<E>::get_section(const ElfSym<E> &esym) {
return sections[get_shndx(esym)].get();
}

template <typename E>
OutputSection<E> *find_section(Context<E> &ctx, u32 sh_type) {
for (Chunk<E> *chunk : ctx.chunks)
if (OutputSection<E> *osec = chunk->to_osec())
if (osec->shdr.sh_type == sh_type)
return osec;
return nullptr;
}

template <typename E>
OutputSection<E> *find_section(Context<E> &ctx, std::string_view name) {
for (Chunk<E> *chunk : ctx.chunks)
if (OutputSection<E> *osec = chunk->to_osec())
if (osec->name == name)
return osec;
return nullptr;
}

template <typename E>
u64 Symbol<E>::get_addr(Context<E> &ctx, i64 flags) const {
if (SectionFragment<E> *frag = get_frag()) {
Expand Down
21 changes: 21 additions & 0 deletions elf/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ static u32 djb_hash(std::string_view name) {
return h;
}

template <typename E>
OutputSection<E> *find_section(Context<E> &ctx, u32 sh_type) {
for (Chunk<E> *chunk : ctx.chunks)
if (OutputSection<E> *osec = chunk->to_osec())
if (osec->shdr.sh_type == sh_type)
return osec;
return nullptr;
}

template <typename E>
OutputSection<E> *find_section(Context<E> &ctx, std::string_view name) {
for (Chunk<E> *chunk : ctx.chunks)
if (OutputSection<E> *osec = chunk->to_osec())
if (osec->name == name)
return osec;
return nullptr;
}

template <typename E>
static u64 get_entry_addr(Context<E> &ctx) {
if (ctx.arg.relocatable)
Expand Down Expand Up @@ -2903,6 +2921,9 @@ template class GdbIndexSection<E>;
template class CompressedSection<E>;
template class RelocSection<E>;
template class ComdatGroupSection<E>;

template OutputSection<E> *find_section(Context<E> &, u32);
template OutputSection<E> *find_section(Context<E> &, std::string_view);
template i64 to_phdr_flags(Context<E> &ctx, Chunk<E> *chunk);
template ElfSym<E> to_output_esym(Context<E> &, Symbol<E> &, u32, U32<E> *);

Expand Down
11 changes: 11 additions & 0 deletions test/elf/linker-script-error.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
. $(dirname $0)/common.inc

cat <<EOF | $CC -o $t/a.o -c -xc -
int main() {}
EOF

echo 'VERSION { ver_x /*' > $t/b.script

! $CC -B. -o $t/exe $t/a.o $t/b.script 2> $t/log
grep -q 'unclosed comment' $t/log

0 comments on commit 7b2cb47

Please sign in to comment.