mirror of
https://github.com/rui314/mold.git
synced 2024-11-11 05:46:58 +03:00
temporary
This commit is contained in:
parent
f8d2fa6e27
commit
b45fc629d2
@ -4,7 +4,7 @@ using namespace llvm;
|
||||
using namespace llvm::ELF;
|
||||
|
||||
InputChunk::InputChunk(ObjectFile *file, const ELF64LE::Shdr &shdr,
|
||||
StringRef name)
|
||||
std::string_view name)
|
||||
: file(file), shdr(shdr), name(name),
|
||||
output_section(OutputSection::get_instance(name, shdr.sh_flags, shdr.sh_type)) {}
|
||||
|
||||
@ -224,15 +224,15 @@ MergeableSection::MergeableSection(InputSection *isec, ArrayRef<u8> contents)
|
||||
: InputChunk(isec->file, isec->shdr, isec->name),
|
||||
parent(*MergedSection::get_instance(isec->name, isec->shdr.sh_flags,
|
||||
isec->shdr.sh_type)) {
|
||||
StringRef data((const char *)&contents[0], contents.size());
|
||||
std::string_view data((const char *)&contents[0], contents.size());
|
||||
u32 offset = 0;
|
||||
|
||||
while (!data.empty()) {
|
||||
size_t end = data.find('\0');
|
||||
if (end == StringRef::npos)
|
||||
if (end == std::string_view::npos)
|
||||
error(toString(this) + ": string is not null terminated");
|
||||
|
||||
StringRef substr = data.substr(0, end + 1);
|
||||
std::string_view substr = data.substr(0, end + 1);
|
||||
data = data.substr(end + 1);
|
||||
|
||||
StringPiece *piece = parent.map.insert(substr, StringPiece(substr));
|
||||
@ -245,5 +245,5 @@ MergeableSection::MergeableSection(InputSection *isec, ArrayRef<u8> contents)
|
||||
}
|
||||
|
||||
std::string toString(InputChunk *chunk) {
|
||||
return (toString(chunk->file) + ":(" + chunk->name + ")").str();
|
||||
return toString(chunk->file) + ":(" + std::string(chunk->name) + ")";
|
||||
}
|
||||
|
@ -6,20 +6,20 @@ using namespace llvm;
|
||||
using namespace llvm::ELF;
|
||||
using namespace llvm::sys;
|
||||
|
||||
static thread_local StringRef script_path;
|
||||
static thread_local StringRef script_dir;
|
||||
static thread_local std::string script_path;
|
||||
static thread_local std::string script_dir;
|
||||
|
||||
static std::vector<StringRef> tokenize(StringRef input) {
|
||||
std::vector<StringRef> vec;
|
||||
static std::vector<std::string_view> tokenize(std::string_view input) {
|
||||
std::vector<std::string_view> vec;
|
||||
while (!input.empty()) {
|
||||
if (input[0] == ' ' || input[0] == '\t' || input[0] == '\n') {
|
||||
input = input.substr(1);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (input.startswith("/*")) {
|
||||
if (input.starts_with("/*")) {
|
||||
int pos = input.find("*/", 2);
|
||||
if (pos == StringRef::npos)
|
||||
if (pos == std::string_view::npos)
|
||||
error("unclosed comment");
|
||||
input = input.substr(pos + 2);
|
||||
continue;
|
||||
@ -27,7 +27,7 @@ static std::vector<StringRef> tokenize(StringRef input) {
|
||||
|
||||
if (input[0] == '#') {
|
||||
int pos = input.find("\n", 1);
|
||||
if (pos == StringRef::npos)
|
||||
if (pos == std::string_view::npos)
|
||||
break;
|
||||
input = input.substr(pos + 1);
|
||||
continue;
|
||||
@ -35,7 +35,7 @@ static std::vector<StringRef> tokenize(StringRef input) {
|
||||
|
||||
if (input[0] == '"') {
|
||||
int pos = input.find('"', 1);
|
||||
if (pos == StringRef::npos)
|
||||
if (pos == std::string_view::npos)
|
||||
error("unclosed string literal");
|
||||
vec.push_back(input.substr(0, pos));
|
||||
input = input.substr(pos);
|
||||
@ -54,13 +54,13 @@ static std::vector<StringRef> tokenize(StringRef input) {
|
||||
return vec;
|
||||
}
|
||||
|
||||
static std::span<StringRef> skip(std::span<StringRef> tok, StringRef str) {
|
||||
static std::span<std::string_view> skip(std::span<std::string_view> tok, std::string_view str) {
|
||||
if (tok.empty() || tok[0] != str)
|
||||
error("expected '" + str + "'");
|
||||
return tok.subspan(1);
|
||||
}
|
||||
|
||||
static std::span<StringRef> read_output_format(std::span<StringRef> tok) {
|
||||
static std::span<std::string_view> read_output_format(std::span<std::string_view> tok) {
|
||||
tok = skip(tok, "(");
|
||||
while (!tok.empty() && tok[0] != ")")
|
||||
tok = tok.subspan(1);
|
||||
@ -69,24 +69,24 @@ static std::span<StringRef> read_output_format(std::span<StringRef> tok) {
|
||||
return tok.subspan(1);
|
||||
}
|
||||
|
||||
static MemoryBufferRef resolve_path(StringRef str) {
|
||||
if (str.startswith("/"))
|
||||
static MemoryBufferRef resolve_path(std::string str) {
|
||||
if (str.starts_with("/"))
|
||||
return must_open_input_file(config.sysroot + str);
|
||||
if (str.startswith("-l"))
|
||||
if (str.starts_with("-l"))
|
||||
return find_library(str.substr(2));
|
||||
if (std::string path = (script_dir + "/" + str).str(); fs::exists(path))
|
||||
if (std::string path = script_dir + "/" + str; fs::exists(path))
|
||||
return must_open_input_file(path);
|
||||
if (MemoryBufferRef *mb = open_input_file(str))
|
||||
return *mb;
|
||||
for (StringRef dir : config.library_paths) {
|
||||
std::string root = dir.startswith("/") ? config.sysroot : "";
|
||||
for (std::string &dir : config.library_paths) {
|
||||
std::string root = dir.starts_with("/") ? config.sysroot : "";
|
||||
if (MemoryBufferRef *mb = open_input_file(root + dir + "/" + str))
|
||||
return *mb;
|
||||
}
|
||||
error("library not found: " + str);
|
||||
}
|
||||
|
||||
static std::span<StringRef> read_group(std::span<StringRef> tok) {
|
||||
static std::span<std::string_view> read_group(std::span<std::string_view> tok) {
|
||||
tok = skip(tok, "(");
|
||||
|
||||
while (!tok.empty() && tok[0] != ")") {
|
||||
@ -97,7 +97,7 @@ static std::span<StringRef> read_group(std::span<StringRef> tok) {
|
||||
continue;
|
||||
}
|
||||
|
||||
read_file(resolve_path(tok[0]));
|
||||
read_file(resolve_path(std::string(tok[0])));
|
||||
tok = tok.subspan(1);
|
||||
}
|
||||
|
||||
@ -106,12 +106,12 @@ static std::span<StringRef> read_group(std::span<StringRef> tok) {
|
||||
return tok.subspan(1);
|
||||
}
|
||||
|
||||
void parse_linker_script(StringRef path, StringRef input) {
|
||||
void parse_linker_script(std::string path, std::string_view input) {
|
||||
script_path = path;
|
||||
script_dir = path.substr(0, path.find_last_of('/'));
|
||||
|
||||
std::vector<StringRef> vec = tokenize(input);
|
||||
std::span<StringRef> tok = vec;
|
||||
std::vector<std::string_view> vec = tokenize(input);
|
||||
std::span<std::string_view> tok = vec;
|
||||
|
||||
while (!tok.empty()) {
|
||||
if (tok[0] == "OUTPUT_FORMAT")
|
||||
@ -119,22 +119,22 @@ void parse_linker_script(StringRef path, StringRef input) {
|
||||
else if (tok[0] == "INPUT" || tok[0] == "GROUP")
|
||||
tok = read_group(tok.subspan(1));
|
||||
else
|
||||
error(path + ": unknown token: " + tok[0]);
|
||||
error(path + ": unknown token: " + std::string(tok[0]));
|
||||
}
|
||||
}
|
||||
|
||||
void parse_version_script(StringRef path) {
|
||||
void parse_version_script(std::string path) {
|
||||
script_path = path;
|
||||
script_dir = path.substr(0, path.find_last_of('/'));
|
||||
|
||||
MemoryBufferRef mb = must_open_input_file(path);
|
||||
std::vector<StringRef> vec = tokenize(mb.getBuffer());
|
||||
std::span<StringRef> tok = vec;
|
||||
std::vector<std::string_view> vec = tokenize(mb.getBuffer());
|
||||
std::span<std::string_view> tok = vec;
|
||||
tok = skip(tok, "{");
|
||||
|
||||
std::vector<StringRef> locals;
|
||||
std::vector<StringRef> globals;
|
||||
std::vector<StringRef> *cur = &locals;
|
||||
std::vector<std::string> locals;
|
||||
std::vector<std::string> globals;
|
||||
std::vector<std::string> *cur = &locals;
|
||||
|
||||
while (!tok.empty() && tok[0] != "}") {
|
||||
if (tok[0] == "local:") {
|
||||
@ -161,7 +161,7 @@ void parse_version_script(StringRef path) {
|
||||
continue;
|
||||
}
|
||||
|
||||
cur->push_back(tok[0]);
|
||||
cur->push_back(std::string(tok[0]));
|
||||
tok = skip(tok.subspan(1), ";");
|
||||
}
|
||||
|
||||
@ -169,7 +169,7 @@ void parse_version_script(StringRef path) {
|
||||
tok = skip(tok, ";");
|
||||
|
||||
if (!tok.empty())
|
||||
error(path + ": trailing garbage token: " + tok[0]);
|
||||
error(path + ": trailing garbage token: " + std::string(tok[0]));
|
||||
|
||||
if (locals.size() != 1 || locals[0] != "*")
|
||||
error(path + ": unsupported version script");
|
||||
|
40
main.cc
40
main.cc
@ -24,12 +24,12 @@ using llvm::opt::InputArgList;
|
||||
|
||||
class MyTimer {
|
||||
public:
|
||||
MyTimer(StringRef name) {
|
||||
MyTimer(std::string_view name) {
|
||||
timer = new Timer(name, name);
|
||||
timer->startTimer();
|
||||
}
|
||||
|
||||
MyTimer(StringRef name, llvm::TimerGroup &tg) {
|
||||
MyTimer(std::string_view name, llvm::TimerGroup &tg) {
|
||||
timer = new Timer(name, name, tg);
|
||||
timer->startTimer();
|
||||
}
|
||||
@ -117,8 +117,8 @@ static std::vector<MemoryBufferRef> get_archive_members(MemoryBufferRef mb) {
|
||||
return vec;
|
||||
}
|
||||
|
||||
MemoryBufferRef *open_input_file(const Twine &path) {
|
||||
int fd = open(path.str().c_str(), O_RDONLY);
|
||||
MemoryBufferRef *open_input_file(std::string path) {
|
||||
int fd = open(path.c_str(), O_RDONLY);
|
||||
if (fd == -1)
|
||||
return nullptr;
|
||||
|
||||
@ -131,12 +131,12 @@ MemoryBufferRef *open_input_file(const Twine &path) {
|
||||
error(path + ": mmap failed: " + strerror(errno));
|
||||
close(fd);
|
||||
|
||||
StringRef buf((char *)addr, st.st_size);
|
||||
std::string *filename = new std::string(path.str());
|
||||
std::string_view buf((char *)addr, st.st_size);
|
||||
std::string *filename = new std::string(path);
|
||||
return new MemoryBufferRef(buf, *filename);
|
||||
}
|
||||
|
||||
MemoryBufferRef must_open_input_file(const Twine &path) {
|
||||
MemoryBufferRef must_open_input_file(std::string path) {
|
||||
MemoryBufferRef *mb = open_input_file(path);
|
||||
if (!mb)
|
||||
error("cannot open " + path);
|
||||
@ -156,7 +156,7 @@ void read_file(MemoryBufferRef mb) {
|
||||
out::dsos.push_back(new SharedFile(mb, config.as_needed));
|
||||
break;
|
||||
case file_magic::unknown:
|
||||
parse_linker_script(mb.getBufferIdentifier(), mb.getBuffer());
|
||||
parse_linker_script(std::string(mb.getBufferIdentifier()), mb.getBuffer());
|
||||
break;
|
||||
default:
|
||||
error(mb.getBufferIdentifier() + ": unknown file type");
|
||||
@ -462,7 +462,7 @@ static void export_dynamic() {
|
||||
sym->ver_idx = VER_NDX_GLOBAL;
|
||||
});
|
||||
|
||||
for (StringRef name : config.globals)
|
||||
for (std::string_view name : config.globals)
|
||||
Symbol::intern(name)->ver_idx = VER_NDX_GLOBAL;
|
||||
|
||||
std::vector<std::vector<Symbol *>> vec(out::objs.size());
|
||||
@ -519,7 +519,7 @@ static void fill_symbol_versions() {
|
||||
|
||||
auto add_aux = [&](Symbol *sym) {
|
||||
SharedFile *file = (SharedFile *)sym->file;
|
||||
StringRef verstr = file->version_strings[sym->ver_idx];
|
||||
std::string_view verstr = file->version_strings[sym->ver_idx];
|
||||
|
||||
verneed->vn_cnt += 1;
|
||||
if (aux)
|
||||
@ -734,7 +734,7 @@ static u32 get_umask() {
|
||||
static u8 *open_output_file(u64 filesize) {
|
||||
MyTimer t("open_file", before_copy_timer);
|
||||
|
||||
int fd = open(config.output.str().c_str(), O_RDWR | O_CREAT, 0777);
|
||||
int fd = open(std::string(config.output).c_str(), O_RDWR | O_CREAT, 0777);
|
||||
if (fd == -1)
|
||||
error("cannot open " + config.output + ": " + strerror(errno));
|
||||
|
||||
@ -765,8 +765,8 @@ static int get_thread_count(InputArgList &args) {
|
||||
return tbb::global_control::active_value(tbb::global_control::max_allowed_parallelism);
|
||||
}
|
||||
|
||||
std::vector<StringRef> get_args(opt::InputArgList &args, int id) {
|
||||
std::vector<StringRef> vec;
|
||||
std::vector<std::string> get_args(opt::InputArgList &args, int id) {
|
||||
std::vector<std::string> vec;
|
||||
for (auto *arg : args.filtered(id))
|
||||
vec.push_back(arg->getValue());
|
||||
return vec;
|
||||
@ -777,8 +777,8 @@ static int parse_filler(opt::InputArgList &args) {
|
||||
if (!arg)
|
||||
return -1;
|
||||
|
||||
StringRef val = arg->getValue();
|
||||
if (!val.startswith("0x"))
|
||||
std::string_view val = arg->getValue();
|
||||
if (!val.starts_with("0x"))
|
||||
error("invalid argument: " + arg->getAsString(args));
|
||||
int ret;
|
||||
if (!to_integer(val.substr(2), ret, 16))
|
||||
@ -786,10 +786,10 @@ static int parse_filler(opt::InputArgList &args) {
|
||||
return (u8)ret;
|
||||
}
|
||||
|
||||
MemoryBufferRef find_library(const Twine &name) {
|
||||
for (StringRef dir : config.library_paths) {
|
||||
std::string root = dir.startswith("/") ? config.sysroot : "";
|
||||
std::string stem = (root + dir + "/lib" + name).str();
|
||||
MemoryBufferRef find_library(std::string name) {
|
||||
for (std::string_view dir : config.library_paths) {
|
||||
std::string root = dir.starts_with("/") ? config.sysroot : "";
|
||||
std::string stem = root + std::string(dir) + "/lib" + name;
|
||||
if (!config.is_static)
|
||||
if (MemoryBufferRef *mb = open_input_file(stem + ".so"))
|
||||
return *mb;
|
||||
@ -1009,7 +1009,7 @@ int main(int argc, char **argv) {
|
||||
out::dynstr->add_string(file->soname);
|
||||
|
||||
// Copy DT_RUNPATH strings to .dynstr.
|
||||
for (StringRef path : config.rpaths)
|
||||
for (std::string_view path : config.rpaths)
|
||||
out::dynstr->add_string(path);
|
||||
|
||||
// Add headers and sections that have to be at the beginning
|
||||
|
96
mold.h
96
mold.h
@ -27,6 +27,7 @@
|
||||
#include <mutex>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#define SECTOR_SIZE 512
|
||||
#define PAGE_SIZE 4096
|
||||
@ -52,7 +53,6 @@ using llvm::Error;
|
||||
using llvm::Expected;
|
||||
using llvm::MemoryBufferRef;
|
||||
using llvm::SmallVector;
|
||||
using llvm::StringRef;
|
||||
using llvm::Twine;
|
||||
using llvm::object::ELF64LE;
|
||||
using llvm::object::ELFFile;
|
||||
@ -69,17 +69,17 @@ class SharedFile;
|
||||
class Symbol;
|
||||
|
||||
struct Config {
|
||||
StringRef dynamic_linker = "/lib64/ld-linux-x86-64.so.2";
|
||||
StringRef output;
|
||||
std::string_view dynamic_linker = "/lib64/ld-linux-x86-64.so.2";
|
||||
std::string_view output;
|
||||
bool as_needed = false;
|
||||
bool export_dynamic = false;
|
||||
bool is_static = false;
|
||||
bool print_map = false;
|
||||
int filler = -1;
|
||||
std::string sysroot;
|
||||
std::vector<StringRef> library_paths;
|
||||
std::vector<StringRef> rpaths;
|
||||
std::vector<StringRef> globals;
|
||||
std::vector<std::string> library_paths;
|
||||
std::vector<std::string> rpaths;
|
||||
std::vector<std::string> globals;
|
||||
u64 image_base = 0x200000;
|
||||
};
|
||||
|
||||
@ -131,12 +131,12 @@ std::string toString(InputFile *);
|
||||
|
||||
namespace tbb {
|
||||
template<>
|
||||
struct tbb_hash_compare<StringRef> {
|
||||
static size_t hash(const StringRef& k) {
|
||||
struct tbb_hash_compare<std::string_view> {
|
||||
static size_t hash(const std::string_view& k) {
|
||||
return llvm::hash_value(k);
|
||||
}
|
||||
|
||||
static bool equal(const StringRef& k1, const StringRef& k2) {
|
||||
static bool equal(const std::string_view& k1, const std::string_view& k2) {
|
||||
return k1 == k2;
|
||||
}
|
||||
};
|
||||
@ -145,9 +145,9 @@ struct tbb_hash_compare<StringRef> {
|
||||
template<typename ValueT>
|
||||
class ConcurrentMap {
|
||||
public:
|
||||
typedef tbb::concurrent_hash_map<StringRef, ValueT> MapT;
|
||||
typedef tbb::concurrent_hash_map<std::string_view, ValueT> MapT;
|
||||
|
||||
ValueT *insert(StringRef key, const ValueT &val) {
|
||||
ValueT *insert(std::string_view key, const ValueT &val) {
|
||||
typename MapT::const_accessor acc;
|
||||
map.insert(acc, std::make_pair(key, val));
|
||||
return const_cast<ValueT *>(&acc->second);
|
||||
@ -164,7 +164,7 @@ private:
|
||||
//
|
||||
|
||||
struct StringPiece {
|
||||
StringPiece(StringRef data) : data(data) {}
|
||||
StringPiece(std::string_view data) : data(data) {}
|
||||
|
||||
StringPiece(const StringPiece &other)
|
||||
: data(other.data), isec(other.isec.load()),
|
||||
@ -172,7 +172,7 @@ struct StringPiece {
|
||||
|
||||
inline u64 get_addr() const;
|
||||
|
||||
StringRef data;
|
||||
std::string_view data;
|
||||
std::atomic<MergeableSection *> isec = ATOMIC_VAR_INIT(nullptr);
|
||||
u32 output_offset = -1;
|
||||
};
|
||||
@ -185,13 +185,13 @@ struct StringPieceRef {
|
||||
|
||||
class Symbol {
|
||||
public:
|
||||
Symbol(StringRef name)
|
||||
Symbol(std::string_view name)
|
||||
: name(name), is_placeholder(false), is_imported(false),
|
||||
is_weak(false), is_undef_weak(false), traced(false) {}
|
||||
|
||||
Symbol(const Symbol &other) : Symbol(other.name) {}
|
||||
|
||||
static Symbol *intern(StringRef name) {
|
||||
static Symbol *intern(std::string_view name) {
|
||||
static ConcurrentMap<Symbol> map;
|
||||
return map.insert(name, Symbol(name));
|
||||
}
|
||||
@ -204,7 +204,7 @@ public:
|
||||
inline u64 get_tlsld_addr() const;
|
||||
inline u64 get_plt_addr() const;
|
||||
|
||||
StringRef name;
|
||||
std::string_view name;
|
||||
InputFile *file = nullptr;
|
||||
const ELF64LE::Sym *esym = nullptr;
|
||||
InputSection *input_section = nullptr;
|
||||
@ -258,16 +258,16 @@ public:
|
||||
const ELF64LE::Shdr &shdr;
|
||||
OutputSection *output_section = nullptr;
|
||||
|
||||
StringRef name;
|
||||
std::string_view name;
|
||||
u32 offset;
|
||||
|
||||
protected:
|
||||
InputChunk(ObjectFile *file, const ELF64LE::Shdr &shdr, StringRef name);
|
||||
InputChunk(ObjectFile *file, const ELF64LE::Shdr &shdr, std::string_view name);
|
||||
};
|
||||
|
||||
class InputSection : public InputChunk {
|
||||
public:
|
||||
InputSection(ObjectFile *file, const ELF64LE::Shdr &shdr, StringRef name)
|
||||
InputSection(ObjectFile *file, const ELF64LE::Shdr &shdr, std::string_view name)
|
||||
: InputChunk(file, shdr, name) {}
|
||||
|
||||
void copy_buf() override;
|
||||
@ -305,7 +305,7 @@ public:
|
||||
virtual void copy_buf() {}
|
||||
virtual void update_shdr() {}
|
||||
|
||||
StringRef name;
|
||||
std::string_view name;
|
||||
int shndx = 0;
|
||||
Kind kind;
|
||||
bool starts_new_ptload = false;
|
||||
@ -360,9 +360,9 @@ public:
|
||||
// Sections
|
||||
class OutputSection : public OutputChunk {
|
||||
public:
|
||||
static OutputSection *get_instance(StringRef name, u64 flags, u32 type);
|
||||
static OutputSection *get_instance(std::string_view name, u64 flags, u32 type);
|
||||
|
||||
OutputSection(StringRef name, u32 type, u64 flags)
|
||||
OutputSection(std::string_view name, u32 type, u64 flags)
|
||||
: OutputChunk(REGULAR) {
|
||||
this->name = name;
|
||||
shdr.sh_type = type;
|
||||
@ -381,7 +381,7 @@ public:
|
||||
|
||||
class SpecialSection : public OutputChunk {
|
||||
public:
|
||||
SpecialSection(StringRef name, u32 type, u64 flags, u32 align = 1, u32 entsize = 0)
|
||||
SpecialSection(std::string_view name, u32 type, u64 flags, u32 align = 1, u32 entsize = 0)
|
||||
: OutputChunk(SYNTHETIC) {
|
||||
this->name = name;
|
||||
shdr.sh_type = type;
|
||||
@ -503,12 +503,12 @@ DynstrSection() : OutputChunk(SYNTHETIC) {
|
||||
shdr.sh_addralign = 1;
|
||||
}
|
||||
|
||||
u32 add_string(StringRef str);
|
||||
u32 find_string(StringRef str);
|
||||
u32 add_string(std::string_view str);
|
||||
u32 find_string(std::string_view str);
|
||||
void copy_buf() override;
|
||||
|
||||
private:
|
||||
std::vector<StringRef> contents;
|
||||
std::vector<std::string_view> contents;
|
||||
};
|
||||
|
||||
class DynamicSection : public OutputChunk {
|
||||
@ -572,19 +572,19 @@ public:
|
||||
void copy_buf() override;
|
||||
|
||||
private:
|
||||
static u32 hash(StringRef name);
|
||||
static u32 hash(std::string_view name);
|
||||
};
|
||||
|
||||
class MergedSection : public OutputChunk {
|
||||
public:
|
||||
static MergedSection *get_instance(StringRef name, u64 flags, u32 type);
|
||||
static MergedSection *get_instance(std::string_view name, u64 flags, u32 type);
|
||||
|
||||
static inline std::vector<MergedSection *> instances;
|
||||
|
||||
ConcurrentMap<StringPiece> map;
|
||||
|
||||
private:
|
||||
MergedSection(StringRef name, u64 flags, u32 type)
|
||||
MergedSection(std::string_view name, u64 flags, u32 type)
|
||||
: OutputChunk(SYNTHETIC) {
|
||||
this->name = name;
|
||||
shdr.sh_flags = flags;
|
||||
@ -638,7 +638,7 @@ public:
|
||||
std::vector<u8> contents;
|
||||
};
|
||||
|
||||
bool is_c_identifier(StringRef name);
|
||||
bool is_c_identifier(std::string_view name);
|
||||
std::vector<ELF64LE::Phdr> create_phdr();
|
||||
|
||||
//
|
||||
@ -672,7 +672,7 @@ public:
|
||||
|
||||
class ObjectFile : public InputFile {
|
||||
public:
|
||||
ObjectFile(MemoryBufferRef mb, StringRef archive_name);
|
||||
ObjectFile(MemoryBufferRef mb, std::string_view archive_name);
|
||||
|
||||
void parse();
|
||||
void initialize_mergeable_sections();
|
||||
@ -688,7 +688,7 @@ public:
|
||||
|
||||
static ObjectFile *create_internal_file();
|
||||
|
||||
StringRef archive_name;
|
||||
std::string_view archive_name;
|
||||
std::vector<InputSection *> sections;
|
||||
ArrayRef<ELF64LE::Sym> elf_syms;
|
||||
int first_global = 0;
|
||||
@ -716,7 +716,7 @@ private:
|
||||
bool has_common_symbol;
|
||||
|
||||
ArrayRef<ELF64LE::Shdr> elf_sections;
|
||||
StringRef symbol_strtab;
|
||||
std::string_view symbol_strtab;
|
||||
const ELF64LE::Shdr *symtab_sec;
|
||||
};
|
||||
|
||||
@ -730,19 +730,19 @@ public:
|
||||
void resolve_symbols();
|
||||
ArrayRef<Symbol *> find_aliases(Symbol *sym);
|
||||
|
||||
StringRef soname;
|
||||
std::string_view soname;
|
||||
|
||||
std::vector<StringRef> version_strings;
|
||||
std::vector<std::string_view> version_strings;
|
||||
|
||||
private:
|
||||
StringRef get_soname(ArrayRef<ELF64LE::Shdr> elf_sections);
|
||||
std::string_view get_soname(ArrayRef<ELF64LE::Shdr> elf_sections);
|
||||
void maybe_override_symbol(Symbol &sym, const ELF64LE::Sym &esym);
|
||||
std::vector<StringRef> read_verdef();
|
||||
std::vector<std::string_view> read_verdef();
|
||||
|
||||
std::vector<const ELF64LE::Sym *> elf_syms;
|
||||
std::vector<u16> versyms;
|
||||
|
||||
StringRef symbol_strtab;
|
||||
std::string_view symbol_strtab;
|
||||
const ELF64LE::Shdr *symtab_sec;
|
||||
};
|
||||
|
||||
@ -750,8 +750,8 @@ private:
|
||||
// linker_script.cc
|
||||
//
|
||||
|
||||
void parse_linker_script(StringRef path, StringRef input);
|
||||
void parse_version_script(StringRef path);
|
||||
void parse_linker_script(std::string path, std::string_view input);
|
||||
void parse_version_script(std::string path);
|
||||
|
||||
//
|
||||
// perf.cc
|
||||
@ -759,7 +759,7 @@ void parse_version_script(StringRef path);
|
||||
|
||||
class Counter {
|
||||
public:
|
||||
Counter(StringRef name, u32 value = 0) : name(name), value(value) {
|
||||
Counter(std::string_view name, u32 value = 0) : name(name), value(value) {
|
||||
static std::mutex mu;
|
||||
std::lock_guard lock(mu);
|
||||
instances.push_back(this);
|
||||
@ -779,7 +779,7 @@ public:
|
||||
static bool enabled;
|
||||
|
||||
private:
|
||||
StringRef name;
|
||||
std::string_view name;
|
||||
std::atomic_uint32_t value;
|
||||
|
||||
static std::vector<Counter *> instances;
|
||||
@ -795,9 +795,9 @@ void print_map();
|
||||
// main.cc
|
||||
//
|
||||
|
||||
MemoryBufferRef find_library(const Twine &path);
|
||||
MemoryBufferRef *open_input_file(const Twine &path);
|
||||
MemoryBufferRef must_open_input_file(const Twine &path);
|
||||
MemoryBufferRef find_library(std::string path);
|
||||
MemoryBufferRef *open_input_file(std::string path);
|
||||
MemoryBufferRef must_open_input_file(std::string path);
|
||||
void read_file(MemoryBufferRef mb);
|
||||
|
||||
//
|
||||
@ -860,7 +860,7 @@ inline void message(const Twine &msg) {
|
||||
inline std::string toString(const Twine &s) { return s.str(); }
|
||||
|
||||
inline std::string toString(Symbol sym) {
|
||||
return (StringRef(sym.name) + "(" + toString(sym.file) + ")").str();
|
||||
return std::string(sym.name) + "(" + toString(sym.file) + ")";
|
||||
}
|
||||
|
||||
inline u64 align_to(u64 val, u64 align) {
|
||||
@ -933,7 +933,7 @@ inline u64 InputChunk::get_addr() const {
|
||||
return output_section->shdr.sh_addr + offset;
|
||||
}
|
||||
|
||||
inline u32 elf_hash(StringRef name) {
|
||||
inline u32 elf_hash(std::string_view name) {
|
||||
u32 h = 0;
|
||||
for (u8 c : name) {
|
||||
h = (h << 4) + c;
|
||||
@ -945,7 +945,7 @@ inline u32 elf_hash(StringRef name) {
|
||||
return h;
|
||||
}
|
||||
|
||||
inline void write_string(u8 *buf, StringRef str) {
|
||||
inline void write_string(u8 *buf, std::string_view str) {
|
||||
memcpy(buf, str.data(), str.size());
|
||||
buf[str.size()] = '\0';
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
using namespace llvm;
|
||||
using namespace llvm::ELF;
|
||||
|
||||
ObjectFile::ObjectFile(MemoryBufferRef mb, StringRef archive_name)
|
||||
ObjectFile::ObjectFile(MemoryBufferRef mb, std::string_view archive_name)
|
||||
: InputFile(mb, false), archive_name(archive_name),
|
||||
is_in_archive(archive_name != "") {
|
||||
is_alive = (archive_name == "");
|
||||
@ -23,7 +23,7 @@ static const ELF64LE::Shdr
|
||||
}
|
||||
|
||||
void ObjectFile::initialize_sections() {
|
||||
StringRef section_strtab = CHECK(obj.getSectionStringTable(elf_sections), this);
|
||||
std::string_view section_strtab = CHECK(obj.getSectionStringTable(elf_sections), this);
|
||||
|
||||
// Read sections
|
||||
for (int i = 0; i < elf_sections.size(); i++) {
|
||||
@ -38,7 +38,7 @@ void ObjectFile::initialize_sections() {
|
||||
if (shdr.sh_info >= elf_syms.size())
|
||||
error(toString(this) + ": invalid symbol index");
|
||||
const ELF64LE::Sym &sym = elf_syms[shdr.sh_info];
|
||||
StringRef signature = CHECK(sym.getName(symbol_strtab), this);
|
||||
std::string_view signature = CHECK(sym.getName(symbol_strtab), this);
|
||||
|
||||
// Get comdat group members.
|
||||
ArrayRef<ELF64LE::Word> entries =
|
||||
@ -71,7 +71,8 @@ void ObjectFile::initialize_sections() {
|
||||
static Counter counter("regular_sections");
|
||||
counter.inc();
|
||||
|
||||
StringRef name = CHECK(obj.getSectionName(shdr, section_strtab), this);
|
||||
std::string_view name =
|
||||
CHECK(obj.getSectionName(shdr, StringRef(section_strtab)), this);
|
||||
this->sections[i] = new InputSection(this, shdr, name);
|
||||
break;
|
||||
}
|
||||
@ -126,7 +127,7 @@ void ObjectFile::initialize_symbols() {
|
||||
// Initialize local symbols
|
||||
for (int i = 1; i < first_global; i++) {
|
||||
const ELF64LE::Sym &esym = elf_syms[i];
|
||||
StringRef name = CHECK(esym.getName(symbol_strtab), this);
|
||||
std::string_view name = CHECK(esym.getName(symbol_strtab), this);
|
||||
|
||||
local_symbols.emplace_back(name);
|
||||
Symbol &sym = local_symbols.back();
|
||||
@ -153,9 +154,9 @@ void ObjectFile::initialize_symbols() {
|
||||
// Initialize global symbols
|
||||
for (int i = first_global; i < elf_syms.size(); i++) {
|
||||
const ELF64LE::Sym &esym = elf_syms[i];
|
||||
StringRef name = CHECK(esym.getName(symbol_strtab), this);
|
||||
std::string_view name = CHECK(esym.getName(symbol_strtab), this);
|
||||
int pos = name.find('@');
|
||||
if (pos != StringRef::npos)
|
||||
if (pos != std::string_view::npos)
|
||||
name = name.substr(0, pos);
|
||||
|
||||
symbols.push_back(Symbol::intern(name));
|
||||
@ -359,7 +360,7 @@ void ObjectFile::maybe_override_symbol(Symbol &sym, int symidx) {
|
||||
if (UNLIKELY(sym.traced))
|
||||
message("trace: " + toString(sym.file) +
|
||||
(sym.is_weak ? ": weak definition of " : ": definition of ") +
|
||||
sym.name);
|
||||
std::string(sym.name));
|
||||
}
|
||||
}
|
||||
|
||||
@ -382,7 +383,8 @@ void ObjectFile::resolve_symbols() {
|
||||
sym.is_placeholder = true;
|
||||
|
||||
if (UNLIKELY(sym.traced))
|
||||
message("trace: " + toString(sym.file) + ": lazy definition of " + sym.name);
|
||||
message("trace: " + toString(sym.file) + ": lazy definition of " +
|
||||
std::string(sym.name));
|
||||
}
|
||||
} else {
|
||||
maybe_override_symbol(sym, i);
|
||||
@ -405,7 +407,7 @@ ObjectFile::mark_live_objects(tbb::parallel_do_feeder<ObjectFile *> &feeder) {
|
||||
}
|
||||
|
||||
if (UNLIKELY(sym.traced))
|
||||
message("trace: " + toString(this) + ": reference to " + sym.name);
|
||||
message("trace: " + toString(this) + ": reference to " + std::string(sym.name));
|
||||
|
||||
if (esym.getBinding() != STB_WEAK && sym.file &&
|
||||
!sym.file->is_alive.exchange(true)) {
|
||||
@ -414,7 +416,7 @@ ObjectFile::mark_live_objects(tbb::parallel_do_feeder<ObjectFile *> &feeder) {
|
||||
|
||||
if (UNLIKELY(sym.traced))
|
||||
message("trace: " + toString(this) + " keeps " + toString(sym.file) +
|
||||
" for " + sym.name);
|
||||
" for " + std::string(sym.name));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -444,7 +446,8 @@ void ObjectFile::handle_undefined_weak_symbols() {
|
||||
sym.is_imported = false;
|
||||
|
||||
if (UNLIKELY(sym.traced))
|
||||
message("trace: " + toString(this) + ": unresolved weak symbol " + sym.name);
|
||||
message("trace: " + toString(this) + ": unresolved weak symbol " +
|
||||
std::string(sym.name));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -565,7 +568,7 @@ void ObjectFile::write_symtab() {
|
||||
write_sym(i);
|
||||
}
|
||||
|
||||
bool is_c_identifier(StringRef name) {
|
||||
bool is_c_identifier(std::string_view name) {
|
||||
static std::regex re("[a-zA-Z_][a-zA-Z0-9_]*");
|
||||
return std::regex_match(name.begin(), name.end(), re);
|
||||
}
|
||||
@ -575,7 +578,7 @@ ObjectFile *ObjectFile::create_internal_file() {
|
||||
constexpr int bufsz = 256;
|
||||
char *buf = new char[bufsz];
|
||||
std::unique_ptr<MemoryBuffer> mb =
|
||||
MemoryBuffer::getMemBuffer(StringRef(buf, bufsz));
|
||||
MemoryBuffer::getMemBuffer(std::string_view(buf, bufsz));
|
||||
|
||||
auto *obj = new ObjectFile(mb->getMemBufferRef(), "");
|
||||
obj->name = "<internal>";
|
||||
@ -587,7 +590,7 @@ ObjectFile *ObjectFile::create_internal_file() {
|
||||
obj->first_global = 1;
|
||||
obj->is_alive = true;
|
||||
|
||||
auto add = [&](StringRef name, u8 visibility = STV_DEFAULT) {
|
||||
auto add = [&](std::string_view name, u8 visibility = STV_DEFAULT) {
|
||||
ELF64LE::Sym esym = {};
|
||||
esym.setType(STT_NOTYPE);
|
||||
esym.st_shndx = SHN_ABS;
|
||||
@ -641,7 +644,7 @@ std::string toString(InputFile *file) {
|
||||
return (obj->archive_name + ":" + obj->name).str();
|
||||
}
|
||||
|
||||
StringRef SharedFile::get_soname(ArrayRef<ELF64LE::Shdr> elf_sections) {
|
||||
std::string_view SharedFile::get_soname(ArrayRef<ELF64LE::Shdr> elf_sections) {
|
||||
const ELF64LE::Shdr *sec = find_section(elf_sections, SHT_DYNAMIC);
|
||||
if (!sec)
|
||||
return name;
|
||||
@ -651,7 +654,7 @@ StringRef SharedFile::get_soname(ArrayRef<ELF64LE::Shdr> elf_sections) {
|
||||
|
||||
for (const ELF64LE::Dyn &dyn : tags)
|
||||
if (dyn.d_tag == DT_SONAME)
|
||||
return StringRef(symbol_strtab.data() + dyn.d_un.d_val);
|
||||
return std::string_view(symbol_strtab.data() + dyn.d_un.d_val);
|
||||
return name;
|
||||
}
|
||||
|
||||
@ -703,7 +706,7 @@ void SharedFile::parse() {
|
||||
elf_syms.push_back(x.first);
|
||||
versyms.push_back(x.second);
|
||||
|
||||
StringRef name = CHECK(x.first->getName(symbol_strtab), this);
|
||||
std::string_view name = CHECK(x.first->getName(symbol_strtab), this);
|
||||
symbols.push_back(Symbol::intern(name));
|
||||
}
|
||||
|
||||
@ -711,7 +714,7 @@ void SharedFile::parse() {
|
||||
counter.inc(elf_syms.size());
|
||||
}
|
||||
|
||||
std::vector<StringRef> SharedFile::read_verdef() {
|
||||
std::vector<std::string_view> SharedFile::read_verdef() {
|
||||
ArrayRef<ELF64LE::Shdr> elf_sections = CHECK(obj.sections(), this);
|
||||
const ELF64LE::Shdr *verdef_sec = find_section(elf_sections, SHT_GNU_verdef);
|
||||
if (!verdef_sec)
|
||||
@ -722,9 +725,9 @@ std::vector<StringRef> SharedFile::read_verdef() {
|
||||
error(toString(this) + ": .gnu.version_d is corrupted");
|
||||
|
||||
ArrayRef<u8> verdef = CHECK(obj.getSectionContents(*verdef_sec), this);
|
||||
StringRef strtab = CHECK(obj.getStringTable(*vername_sec), this);
|
||||
std::string_view strtab = CHECK(obj.getStringTable(*vername_sec), this);
|
||||
|
||||
std::vector<StringRef> ret(2);
|
||||
std::vector<std::string_view> ret(2);
|
||||
auto *ver = (ELF64LE::Verdef *)verdef.data();
|
||||
|
||||
for (;;) {
|
||||
@ -764,7 +767,7 @@ void SharedFile::resolve_symbols() {
|
||||
if (UNLIKELY(sym.traced))
|
||||
message("trace: " + toString(sym.file) +
|
||||
(sym.is_weak ? ": weak definition of " : ": definition of ") +
|
||||
sym.name);
|
||||
std::string(sym.name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -216,16 +216,16 @@ void ShstrtabSection::copy_buf() {
|
||||
}
|
||||
}
|
||||
|
||||
u32 DynstrSection::add_string(StringRef str) {
|
||||
u32 DynstrSection::add_string(std::string_view str) {
|
||||
u32 ret = shdr.sh_size;
|
||||
shdr.sh_size += str.size() + 1;
|
||||
contents.push_back(str);
|
||||
return ret;
|
||||
}
|
||||
|
||||
u32 DynstrSection::find_string(StringRef str) {
|
||||
u32 DynstrSection::find_string(std::string_view str) {
|
||||
u32 i = 1;
|
||||
for (StringRef s : contents) {
|
||||
for (std::string_view s : contents) {
|
||||
if (s == str)
|
||||
return i;
|
||||
i += s.size() + 1;
|
||||
@ -238,7 +238,7 @@ void DynstrSection::copy_buf() {
|
||||
base[0] = '\0';
|
||||
|
||||
int i = 1;
|
||||
for (StringRef s : contents) {
|
||||
for (std::string_view s : contents) {
|
||||
write_string(base + i, s);
|
||||
i += s.size() + 1;
|
||||
}
|
||||
@ -279,7 +279,7 @@ static std::vector<u64> create_dynamic_section() {
|
||||
for (SharedFile *file : out::dsos)
|
||||
define(DT_NEEDED, out::dynstr->find_string(file->soname));
|
||||
|
||||
for (StringRef path : config.rpaths)
|
||||
for (std::string_view path : config.rpaths)
|
||||
define(DT_RUNPATH, out::dynstr->find_string(path));
|
||||
|
||||
define(DT_RELA, out::reldyn->shdr.sh_addr);
|
||||
@ -303,7 +303,7 @@ static std::vector<u64> create_dynamic_section() {
|
||||
define(DT_VERNEEDNUM, out::verneed->shdr.sh_info);
|
||||
define(DT_DEBUG, 0);
|
||||
|
||||
auto find = [](StringRef name) -> OutputChunk * {
|
||||
auto find = [](std::string_view name) -> OutputChunk * {
|
||||
for (OutputChunk *chunk : out::chunks)
|
||||
if (chunk->name == name)
|
||||
return chunk;
|
||||
@ -328,20 +328,20 @@ void DynamicSection::copy_buf() {
|
||||
write_vector(out::buf + shdr.sh_offset, create_dynamic_section());
|
||||
}
|
||||
|
||||
static StringRef get_output_name(StringRef name) {
|
||||
static StringRef common_names[] = {
|
||||
static std::string_view get_output_name(std::string_view name) {
|
||||
static std::string_view common_names[] = {
|
||||
".text.", ".data.rel.ro.", ".data.", ".rodata.", ".bss.rel.ro.",
|
||||
".bss.", ".init_array.", ".fini_array.", ".tbss.", ".tdata.",
|
||||
};
|
||||
|
||||
for (StringRef s : common_names)
|
||||
if (name.startswith(s) || name == s.drop_back())
|
||||
return s.drop_back();
|
||||
for (std::string_view s : common_names)
|
||||
if (name.starts_with(s) || name == s.substr(0, s.size() - 1))
|
||||
return s.substr(0, s.size() - 1);
|
||||
return name;
|
||||
}
|
||||
|
||||
OutputSection *
|
||||
OutputSection::get_instance(StringRef name, u64 flags, u32 type) {
|
||||
OutputSection::get_instance(std::string_view name, u64 flags, u32 type) {
|
||||
name = get_output_name(name);
|
||||
flags = flags & ~(u64)SHF_GROUP;
|
||||
|
||||
@ -600,7 +600,7 @@ void HashSection::copy_buf() {
|
||||
}
|
||||
|
||||
MergedSection *
|
||||
MergedSection::get_instance(StringRef name, u64 flags, u32 type) {
|
||||
MergedSection::get_instance(std::string_view name, u64 flags, u32 type) {
|
||||
name = get_output_name(name);
|
||||
flags = flags & ~(u64)SHF_MERGE & ~(u64)SHF_STRINGS;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user