1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-22 10:27:48 +03:00

temporary

This commit is contained in:
Rui Ueyama 2021-01-21 07:47:48 +09:00
parent bf3f853d23
commit 841cccd0bf
4 changed files with 62 additions and 5 deletions

17
elf.h
View File

@ -202,6 +202,23 @@ static constexpr u32 R_X86_64_IRELATIVE = 37;
static constexpr u32 R_X86_64_GOTPCRELX = 41;
static constexpr u32 R_X86_64_REX_GOTPCRELX = 42;
static constexpr u32 DW_EH_PE_absptr = 0;
static constexpr u32 DW_EH_PE_omit = 0xff;
static constexpr u32 DW_EH_PE_uleb128 = 0x01;
static constexpr u32 DW_EH_PE_udata2 = 0x02;
static constexpr u32 DW_EH_PE_udata4 = 0x03;
static constexpr u32 DW_EH_PE_udata8 = 0x04;
static constexpr u32 DW_EH_PE_signed = 0x08;
static constexpr u32 DW_EH_PE_sleb128 = 0x09;
static constexpr u32 DW_EH_PE_sdata2 = 0x0a;
static constexpr u32 DW_EH_PE_sdata4 = 0x0b;
static constexpr u32 DW_EH_PE_sdata8 = 0x0c;
static constexpr u32 DW_EH_PE_pcrel = 0x10;
static constexpr u32 DW_EH_PE_textrel = 0x20;
static constexpr u32 DW_EH_PE_datarel = 0x30;
static constexpr u32 DW_EH_PE_funcrel = 0x40;
static constexpr u32 DW_EH_PE_aligned = 0x50;
struct ElfSym {
bool is_defined() const { return !is_undef(); }
bool is_undef() const { return st_shndx == SHN_UNDEF; }

13
main.cc
View File

@ -885,6 +885,10 @@ static Config parse_nonpositional_args(std::span<std::string_view> args,
conf.sysroot = arg;
} else if (read_flag(args, "trace")) {
conf.trace = true;
} else if (read_flag(args, "eh-frame-hdr")) {
conf.eh_frame_hdr = true;
} else if (read_flag(args, "no-eh-frame-hdr")) {
conf.eh_frame_hdr = false;
} else if (read_flag(args, "pie")) {
conf.pie = true;
} else if (read_flag(args, "no-pie")) {
@ -1065,10 +1069,13 @@ int main(int argc, char **argv) {
out::symtab = new SymtabSection;
out::dynsym = new DynsymSection;
out::dynstr = new DynstrSection;
out::ehframe = new EhFrameSection;
out::eh_frame = new EhFrameSection;
out::copyrel = new CopyrelSection;
if (config.build_id)
out::buildid = new BuildIdSection;
if (config.eh_frame_hdr)
out::eh_frame_hdr = new EhFrameHdrSection;
if (!config.is_static) {
out::interp = new InterpSection;
@ -1091,7 +1098,7 @@ int main(int argc, char **argv) {
out::chunks.push_back(out::symtab);
out::chunks.push_back(out::strtab);
out::chunks.push_back(out::hash);
out::chunks.push_back(out::ehframe);
out::chunks.push_back(out::eh_frame);
out::chunks.push_back(out::copyrel);
out::chunks.push_back(out::versym);
out::chunks.push_back(out::verneed);
@ -1228,7 +1235,7 @@ int main(int argc, char **argv) {
erase(out::chunks, [](OutputChunk *chunk) {
return chunk->kind == OutputChunk::REGULAR && chunk->name == ".eh_frame";
});
out::ehframe->construct();
out::eh_frame->construct();
}
// Now that we have computed sizes for all sections and assigned

21
mold.h
View File

@ -58,6 +58,7 @@ struct Config {
bool build_id = false;
bool discard_all = false;
bool discard_locals = false;
bool eh_frame_hdr = false;
bool export_dynamic = false;
bool fork = true;
bool is_static = false;
@ -711,6 +712,21 @@ private:
u32 num_fdes;
};
class EhFrameHdrSection : public OutputChunk {
public:
EhFrameHdrSection() : OutputChunk(SYNTHETIC) {
name = ".eh_frame_hdr";
shdr.sh_type = SHT_PROGBITS;
shdr.sh_flags = SHF_ALLOC;
shdr.sh_addralign = 4;
shdr.sh_size = HEADER_SIZE;
}
void write(std::span<CieRecord *> cies);
enum { HEADER_SIZE = 12 };
};
class CopyrelSection : public OutputChunk {
public:
CopyrelSection() : OutputChunk(SYNTHETIC) {
@ -1062,7 +1078,8 @@ inline ShstrtabSection *shstrtab;
inline PltSection *plt;
inline SymtabSection *symtab;
inline DynsymSection *dynsym;
inline EhFrameSection *ehframe;
inline EhFrameSection *eh_frame;
inline EhFrameHdrSection *eh_frame_hdr;
inline CopyrelSection *copyrel;
inline VersymSection *versym;
inline VerneedSection *verneed;
@ -1106,7 +1123,7 @@ inline u64 Symbol::get_addr() const {
if (input_section) {
if (input_section->is_ehframe)
return out::ehframe->get_addr(*this);
return out::eh_frame->get_addr(*this);
if (!input_section->is_alive) {
// The control can reach here if there's a relocation that refers

View File

@ -806,6 +806,10 @@ void EhFrameSection::copy_buf() {
}
}
});
// Write to .eh_frame_hdr.
if (out::eh_frame_hdr)
out::eh_frame_hdr->write(cies);
}
u64 EhFrameSection::get_addr(const Symbol &sym) {
@ -847,6 +851,18 @@ u64 EhFrameSection::get_addr(const Symbol &sym) {
Fatal() << file << ": .eh_frame has bad symbol: " << sym.name;
}
void EhFrameHdrSection::write(std::span<CieRecord *> cies) {
u8 *base = out::buf + shdr.sh_offset;
base[0] = 1;
base[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
base[2] = DW_EH_PE_udata4;
base[3] = DW_EH_PE_datarel | DW_EH_PE_sdata4;
*(u32 *)(base + 4) = out::eh_frame->shdr.sh_addr - shdr.sh_addr - 4;
}
void CopyrelSection::add_symbol(Symbol *sym) {
assert(sym->is_imported);
if (sym->has_copyrel)