Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Aug 15, 2024
1 parent 3f593c6 commit c295648
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 34 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ target_sources(mold PRIVATE
lib/compress.cc
lib/crc32.cc
lib/demangle.cc
lib/elf.cc
lib/filepath.cc
lib/glob.cc
lib/hyperloglog.cc
Expand All @@ -384,6 +383,7 @@ target_sources(mold PRIVATE
src/arch-sparc64.cc
src/arch-x86-64.cc
src/config.cc
src/elf.cc
third-party/rust-demangle/rust-demangle.c
)

Expand Down
12 changes: 4 additions & 8 deletions lib/archive-file.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#pragma once

#include "common.h"
#include "filetype.h"

namespace mold {

Expand Down Expand Up @@ -164,14 +163,11 @@ std::vector<MappedFile *> read_fat_archive_members(Context &ctx, MappedFile *mf)

template <typename Context, typename MappedFile>
std::vector<MappedFile *> read_archive_members(Context &ctx, MappedFile *mf) {
switch (get_file_type(ctx, mf)) {
case FileType::AR:
std::string_view str = mf->get_contents();
if (str.starts_with("!<arch>\n"))
return read_fat_archive_members(ctx, mf);
case FileType::THIN_AR:
return read_thin_archive_members(ctx, mf);
default:
unreachable();
}
assert(str.starts_with("!<thin>\n"));
return read_thin_archive_members(ctx, mf);
}

} // namespace mold
12 changes: 6 additions & 6 deletions lib/integers.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
// big-endian SPARC machine to create a little-endian RV64 binary.
//
// 2. Even though data members in all ELF data strucutres are naturally
// aligned, they are not guaranteed to be aligned on memory. Because
// archive file (.a file) aligns each member only to a 2 byte boundary,
// anything larger than 2 bytes may be unaligned in an mmap'ed memory.
// Unaligned access is an undefined behavior in C/C++, so we shouldn't
// cast an arbitrary pointer to a uint32_t, for example, to read a
// 32 bit value.
// aligned, they are not guaranteed to be aligned on memory because of
// archive files. Archive files (.a files) align each member only to a
// 2 byte boundary, so anything larger than 2 bytes may be misaligned
// in an mmap'ed memory. Misaligned access is an undefined behavior in
// C/C++, so we shouldn't cast an arbitrary pointer to a uint32_t, for
// example, to read a 32 bit value.
//
// The data types defined in this file don't depend on host byte order and
// don't do unaligned access.
Expand Down
2 changes: 2 additions & 0 deletions lib/tar.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// This file contains functions to create a tar file.

#include "common.h"

#ifdef _WIN32
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/elf.h → src/elf.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "integers.h"
#include "../lib/integers.h"

#include <concepts>
#include <ostream>
Expand Down
34 changes: 17 additions & 17 deletions lib/filetype.h → src/filetype.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "common.h"
#include "../lib/common.h"
#include "elf.h"

namespace mold {
Expand Down Expand Up @@ -140,23 +140,23 @@ FileType get_file_type(Context &ctx, MappedFile *mf) {
return FileType::UNKNOWN;
}

inline std::string filetype_to_string(FileType type) {
switch (type) {
case FileType::UNKNOWN: return "UNKNOWN";
case FileType::EMPTY: return "EMPTY";
case FileType::ELF_OBJ: return "ELF_OBJ";
case FileType::ELF_DSO: return "ELF_DSO";
case FileType::AR: return "AR";
case FileType::THIN_AR: return "THIN_AR";
case FileType::TEXT: return "TEXT";
case FileType::GCC_LTO_OBJ: return "GCC_LTO_OBJ";
case FileType::LLVM_BITCODE: return "LLVM_BITCODE";
}
return "UNKNOWN";
}

inline std::ostream &operator<<(std::ostream &out, FileType type) {
out << filetype_to_string(type);
auto to_string = [&] {
switch (type) {
case FileType::UNKNOWN: return "UNKNOWN";
case FileType::EMPTY: return "EMPTY";
case FileType::ELF_OBJ: return "ELF_OBJ";
case FileType::ELF_DSO: return "ELF_DSO";
case FileType::AR: return "AR";
case FileType::THIN_AR: return "THIN_AR";
case FileType::TEXT: return "TEXT";
case FileType::GCC_LTO_OBJ: return "GCC_LTO_OBJ";
case FileType::LLVM_BITCODE: return "LLVM_BITCODE";
default: return "UNKNOWN";
}
};

out << to_string();
return out;
}

Expand Down
1 change: 1 addition & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "mold.h"
#include "filetype.h"
#include "../lib/archive-file.h"
#include "../lib/output-file.h"

Expand Down
2 changes: 1 addition & 1 deletion src/mold.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "../lib/common.h"
#include "../lib/elf.h"
#include "elf.h"

#include <atomic>
#include <bitset>
Expand Down

0 comments on commit c295648

Please sign in to comment.