1
1
mirror of https://github.com/rui314/mold.git synced 2024-12-28 02:44:48 +03:00

temporary

This commit is contained in:
Rui Ueyama 2020-10-25 13:17:10 +09:00
parent aa3519b300
commit 66d479aa7e
5 changed files with 41 additions and 41 deletions

View File

@ -105,7 +105,7 @@ void ObjectFile::initialize_sections() {
InputSection *target = sections[shdr.sh_info];
if (target) {
target->rels = CHECK(obj.relas(shdr), this);
if (target->hdr.sh_flags & SHF_ALLOC)
if (target->shdr.sh_flags & SHF_ALLOC)
num_relocs_alloc += target->rels.size();
}
}

View File

@ -4,12 +4,12 @@ using namespace llvm::ELF;
std::atomic_int num_relocs;
InputSection::InputSection(ObjectFile *file, const ELF64LE::Shdr &hdr, StringRef name)
: file(file), hdr(hdr) {
InputSection::InputSection(ObjectFile *file, const ELF64LE::Shdr &shdr, StringRef name)
: file(file), shdr(shdr) {
this->name = name;
this->output_section = OutputSection::get_instance(this);
uint64_t align = (hdr.sh_addralign == 0) ? 1 : hdr.sh_addralign;
uint64_t align = (shdr.sh_addralign == 0) ? 1 : shdr.sh_addralign;
if (align > UINT32_MAX)
error(toString(file) + ": section sh_addralign is too large");
if (__builtin_popcount(align) != 1)
@ -18,13 +18,13 @@ InputSection::InputSection(ObjectFile *file, const ELF64LE::Shdr &hdr, StringRef
}
uint64_t InputSection::get_size() const {
return hdr.sh_size;
return shdr.sh_size;
}
void InputSection::copy_to(uint8_t *buf) {
if (hdr.sh_type == SHT_NOBITS || hdr.sh_size == 0)
if (shdr.sh_type == SHT_NOBITS || shdr.sh_size == 0)
return;
ArrayRef<uint8_t> data = check(file->obj.getSectionContents(hdr));
ArrayRef<uint8_t> data = check(file->obj.getSectionContents(shdr));
memcpy(buf + offset, &data[0], data.size());
}

26
main.cc
View File

@ -112,11 +112,11 @@ thread_local int bar = 5;
// alloc writable bss
// nonalloc
static int get_rank(OutputSection *x) {
bool alloc = x->hdr.sh_flags & SHF_ALLOC;
bool writable = x->hdr.sh_flags & SHF_WRITE;
bool exec = x->hdr.sh_flags & SHF_EXECINSTR;
bool tls = x->hdr.sh_flags & SHF_TLS;
bool nobits = x->hdr.sh_type & SHT_NOBITS;
bool alloc = x->shdr.sh_flags & SHF_ALLOC;
bool writable = x->shdr.sh_flags & SHF_WRITE;
bool exec = x->shdr.sh_flags & SHF_EXECINSTR;
bool tls = x->shdr.sh_flags & SHF_TLS;
bool nobits = x->shdr.sh_type & SHT_NOBITS;
return (alloc << 5) | (!writable << 4) | (!exec << 3) | (tls << 2) | !nobits;
}
@ -133,10 +133,10 @@ static std::vector<OutputSection *> get_output_sections() {
return x > y;
// Tie-break to make output deterministic.
if (a->hdr.sh_flags != b->hdr.sh_flags)
return a->hdr.sh_flags < b->hdr.sh_flags;
if (a->hdr.sh_type != b->hdr.sh_type)
return a->hdr.sh_type < b->hdr.sh_type;
if (a->shdr.sh_flags != b->shdr.sh_flags)
return a->shdr.sh_flags < b->shdr.sh_flags;
if (a->shdr.sh_type != b->shdr.sh_type)
return a->shdr.sh_type < b->shdr.sh_type;
return a->name < b->name;
});
@ -152,7 +152,7 @@ create_shdrs(ArrayRef<OutputChunk *> output_chunks) {
for (OutputChunk *chunk : output_chunks)
if (!chunk->name.empty())
vec.push_back(&chunk->hdr);
vec.push_back(&chunk->shdr);
return vec;
}
@ -162,8 +162,8 @@ static void fill_shdrs(ArrayRef<OutputChunk *> output_chunks) {
for (OutputChunk *chunk : output_chunks) {
if (chunk->name.empty())
continue;
chunk->hdr.sh_offset = chunk->get_offset();
chunk->hdr.sh_size = chunk->get_size();
chunk->shdr.sh_offset = chunk->get_offset();
chunk->shdr.sh_size = chunk->get_size();
chunk->index = i++;
}
}
@ -294,7 +294,7 @@ int main(int argc, char **argv) {
output_chunks.push_back(out::shstrtab);
for (OutputChunk *chunk : output_chunks)
if (!chunk->name.empty())
chunk->hdr.sh_name = out::shstrtab->add_string(chunk->name);
chunk->shdr.sh_name = out::shstrtab->add_string(chunk->name);
// Add a section header.
out::shdr->entries = create_shdrs(output_chunks);

24
mold.h
View File

@ -181,7 +181,7 @@ inline std::string toString(Symbol sym) {
class InputSection {
public:
InputSection(ObjectFile *file, const ELF64LE::Shdr &hdr, StringRef name);
InputSection(ObjectFile *file, const ELF64LE::Shdr &shdr, StringRef name);
void copy_to(uint8_t *buf);
void relocate(uint8_t *buf);
@ -191,7 +191,7 @@ public:
ObjectFile *file;
OutputSection *output_section;
ArrayRef<ELF64LE::Rela> rels;
const ELF64LE::Shdr &hdr;
const ELF64LE::Shdr &shdr;
StringRef name;
uint64_t addr;
@ -222,7 +222,7 @@ public:
StringRef name;
uint32_t index = -1;
ELF64LE::Shdr hdr = {};
ELF64LE::Shdr shdr = {};
protected:
int64_t offset = -1;
@ -232,7 +232,7 @@ protected:
// ELF header
class OutputEhdr : public OutputChunk {
public:
OutputEhdr() { hdr.sh_flags = llvm::ELF::SHF_ALLOC; }
OutputEhdr() { shdr.sh_flags = llvm::ELF::SHF_ALLOC; }
void copy_to(uint8_t *buf) override {}
void relocate(uint8_t *buf) override;
@ -245,7 +245,7 @@ public:
// Section header
class OutputShdr : public OutputChunk {
public:
OutputShdr() { hdr.sh_flags = llvm::ELF::SHF_ALLOC; }
OutputShdr() { shdr.sh_flags = llvm::ELF::SHF_ALLOC; }
void copy_to(uint8_t *buf) override {
auto *p = (ELF64LE::Shdr *)(buf + offset);
@ -263,7 +263,7 @@ public:
// Program header
class OutputPhdr : public OutputChunk {
public:
OutputPhdr() { hdr.sh_flags = llvm::ELF::SHF_ALLOC; }
OutputPhdr() { shdr.sh_flags = llvm::ELF::SHF_ALLOC; }
void copy_to(uint8_t *buf) override;
@ -289,8 +289,8 @@ public:
OutputSection(StringRef name, uint64_t flags, uint32_t type) {
this->name = name;
hdr.sh_flags = flags;
hdr.sh_type = type;
shdr.sh_flags = flags;
shdr.sh_type = type;
all_instances.push_back(this);
}
@ -317,8 +317,8 @@ class InterpSection : public OutputChunk {
public:
InterpSection() {
name = ".interp";
hdr.sh_flags = llvm::ELF::SHF_ALLOC;
hdr.sh_type = llvm::ELF::SHT_PROGBITS;
shdr.sh_flags = llvm::ELF::SHF_ALLOC;
shdr.sh_type = llvm::ELF::SHT_PROGBITS;
}
void copy_to(uint8_t *buf) override {
@ -337,8 +337,8 @@ public:
StringTableSection(StringRef name) {
this->name = name;
contents = '\0';
hdr.sh_flags = 0;
hdr.sh_type = llvm::ELF::SHT_STRTAB;
shdr.sh_flags = 0;
shdr.sh_type = llvm::ELF::SHT_STRTAB;
}
uint64_t add_string(StringRef s) {

View File

@ -61,10 +61,10 @@ void OutputPhdr::construct(std::vector<OutputChunk *> &chunks) {
// Create PT_LOAD segments.
add(PT_LOAD, PF_R, {});
for (OutputChunk *chunk : chunks) {
if (!(chunk->hdr.sh_flags & SHF_ALLOC))
if (!(chunk->shdr.sh_flags & SHF_ALLOC))
break;
uint32_t flags = to_phdr_flags(chunk->hdr.sh_flags);
uint32_t flags = to_phdr_flags(chunk->shdr.sh_flags);
if (entries.back().phdr.p_flags == flags)
entries.back().members.push_back(chunk);
else
@ -73,11 +73,11 @@ void OutputPhdr::construct(std::vector<OutputChunk *> &chunks) {
// Create a PT_TLS.
for (int i = 0; i < chunks.size(); i++) {
if (chunks[i]->hdr.sh_flags & SHF_TLS) {
if (chunks[i]->shdr.sh_flags & SHF_TLS) {
std::vector<OutputChunk *> vec = {chunks[i++]};
while (i < chunks.size() && (chunks[i]->hdr.sh_flags & SHF_TLS))
while (i < chunks.size() && (chunks[i]->shdr.sh_flags & SHF_TLS))
vec.push_back(chunks[i++]);
add(PT_TLS, to_phdr_flags(chunks[i]->hdr.sh_flags), vec);
add(PT_TLS, to_phdr_flags(chunks[i]->shdr.sh_flags), vec);
}
}
}
@ -119,12 +119,12 @@ static StringRef get_output_name(StringRef name) {
OutputSection *OutputSection::get_instance(InputSection *isec) {
StringRef iname = get_output_name(isec->name);
uint64_t iflags = isec->hdr.sh_flags & ~SHF_GROUP;
uint64_t iflags = isec->shdr.sh_flags & ~SHF_GROUP;
auto find = [&]() -> OutputSection * {
for (OutputSection *osec : OutputSection::all_instances)
if (iname == osec->name && iflags == (osec->hdr.sh_flags & ~SHF_GROUP) &&
isec->hdr.sh_type == osec->hdr.sh_type)
if (iname == osec->name && iflags == (osec->shdr.sh_flags & ~SHF_GROUP) &&
isec->shdr.sh_type == osec->shdr.sh_type)
return osec;
return nullptr;
};
@ -140,5 +140,5 @@ OutputSection *OutputSection::get_instance(InputSection *isec) {
std::unique_lock unique_lock(mu);
if (OutputSection *osec = find())
return osec;
return new OutputSection(iname, iflags, isec->hdr.sh_type);
return new OutputSection(iname, iflags, isec->shdr.sh_type);
}