2020-11-19 03:24:10 +03:00
|
|
|
#include "mold.h"
|
|
|
|
|
2020-11-19 13:31:13 +03:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::sys;
|
|
|
|
|
|
|
|
static thread_local StringRef script_path;
|
|
|
|
static thread_local StringRef script_dir;
|
|
|
|
|
2020-11-19 03:24:10 +03:00
|
|
|
static std::vector<StringRef> tokenize(StringRef input) {
|
|
|
|
std::vector<StringRef> vec;
|
|
|
|
while (!input.empty()) {
|
|
|
|
if (input[0] == ' ' || input[0] == '\t' || input[0] == '\n') {
|
|
|
|
input = input.substr(1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input.startswith("/*")) {
|
|
|
|
int pos = input.find("*/", 2);
|
|
|
|
if (pos == StringRef::npos)
|
|
|
|
error("unclosed comment");
|
|
|
|
input = input.substr(pos + 2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input[0] == '"') {
|
|
|
|
int pos = input.find('"', 1);
|
|
|
|
if (pos == StringRef::npos)
|
|
|
|
error("unclosed string literal");
|
|
|
|
vec.push_back(input.substr(0, pos));
|
|
|
|
input = input.substr(pos);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pos = input.find_first_not_of(
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
"0123456789_.$/\\~=+[]*?-!^:");
|
|
|
|
|
|
|
|
if (pos == 0)
|
|
|
|
pos = 1;
|
|
|
|
vec.push_back(input.substr(0, pos));
|
|
|
|
input = input.substr(pos);
|
|
|
|
}
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ArrayRef<StringRef> skip(ArrayRef<StringRef> tok, StringRef str) {
|
|
|
|
if (tok.empty() || tok[0] != str)
|
|
|
|
error("expected '" + str + "'");
|
|
|
|
return tok.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ArrayRef<StringRef> read_output_format(ArrayRef<StringRef> tok) {
|
|
|
|
tok = skip(tok, "(");
|
|
|
|
while (!tok.empty() && tok[0] != ")")
|
|
|
|
tok = tok.slice(1);
|
|
|
|
if (tok.empty())
|
|
|
|
error("expected ')'");
|
|
|
|
return tok.slice(1);
|
|
|
|
}
|
|
|
|
|
2020-11-20 14:07:33 +03:00
|
|
|
static MemoryBufferRef resolve_path(StringRef str) {
|
2020-11-19 13:31:13 +03:00
|
|
|
if (str.startswith("/"))
|
2020-11-20 14:07:33 +03:00
|
|
|
return must_open_input_file(config.sysroot + str);
|
2020-11-19 13:31:13 +03:00
|
|
|
if (str.startswith("-l"))
|
|
|
|
return find_library(str.substr(2));
|
|
|
|
if (std::string path = (script_dir + "/" + str).str(); fs::exists(path))
|
2020-11-20 14:07:33 +03:00
|
|
|
return must_open_input_file(path);
|
|
|
|
if (MemoryBufferRef *mb = open_input_file(str))
|
|
|
|
return *mb;
|
2020-11-20 13:33:40 +03:00
|
|
|
for (StringRef dir : config.library_paths) {
|
|
|
|
std::string root = dir.startswith("/") ? config.sysroot : "";
|
2020-11-20 14:07:33 +03:00
|
|
|
if (MemoryBufferRef *mb = open_input_file(root + dir + "/" + str))
|
|
|
|
return *mb;
|
2020-11-20 13:33:40 +03:00
|
|
|
}
|
2020-11-19 13:31:13 +03:00
|
|
|
error("library not found: " + str);
|
|
|
|
}
|
|
|
|
|
2020-11-19 03:24:10 +03:00
|
|
|
static ArrayRef<StringRef> read_group(ArrayRef<StringRef> tok) {
|
|
|
|
tok = skip(tok, "(");
|
|
|
|
|
|
|
|
while (!tok.empty() && tok[0] != ")") {
|
|
|
|
if (tok[0] == "AS_NEEDED") {
|
|
|
|
tok = read_group(tok.slice(1));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-19 13:31:13 +03:00
|
|
|
read_file(resolve_path(tok[0]));
|
2020-11-19 03:24:10 +03:00
|
|
|
tok = tok.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok.empty())
|
|
|
|
error("expected ')'");
|
|
|
|
return tok.slice(1);
|
|
|
|
}
|
|
|
|
|
2020-11-19 13:31:13 +03:00
|
|
|
void parse_linker_script(StringRef path, StringRef input) {
|
|
|
|
script_path = path;
|
|
|
|
script_dir = path.substr(0, path.find_last_of('/'));
|
|
|
|
|
2020-11-19 03:24:10 +03:00
|
|
|
std::vector<StringRef> vec = tokenize(input);
|
|
|
|
ArrayRef<StringRef> tok = vec;
|
|
|
|
|
|
|
|
while (!tok.empty()) {
|
|
|
|
if (tok[0] == "OUTPUT_FORMAT")
|
|
|
|
tok = read_output_format(tok.slice(1));
|
|
|
|
else if (tok[0] == "GROUP")
|
|
|
|
tok = read_group(tok.slice(1));
|
|
|
|
else
|
|
|
|
error("unknown token: " + tok[0]);
|
|
|
|
}
|
|
|
|
}
|