From 0630d23b5dd2e6c1cfd84fa20617d6929a13405c Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Fri, 14 Apr 2023 11:56:51 +0530 Subject: [PATCH] WASM_X64: Pass only required params to create_elf64_x86_header() --- src/libasr/codegen/x86_assembler.cpp | 16 ++++++++-------- src/libasr/codegen/x86_assembler.h | 7 ++++++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/libasr/codegen/x86_assembler.cpp b/src/libasr/codegen/x86_assembler.cpp index e09633edae..feea8843a3 100644 --- a/src/libasr/codegen/x86_assembler.cpp +++ b/src/libasr/codegen/x86_assembler.cpp @@ -11,7 +11,10 @@ namespace LCompilers { void X86Assembler::save_binary64(const std::string &filename) { - Vec header = create_elf64_x86_header(m_al, *this); + Vec header = create_elf64_x86_header( + m_al, origin(), get_defined_symbol("_start").value, + compute_seg_size("text_segment_start", "text_segment_end"), + compute_seg_size("data_segment_start", "data_segment_end")); { std::ofstream out; out.open(filename); @@ -425,7 +428,8 @@ void align_by_byte(Allocator &al, Vec &code, uint64_t alignment) { } } -Vec create_elf64_x86_header(Allocator &al, X86Assembler &a) { +Vec create_elf64_x86_header(Allocator &al, uint64_t origin, uint64_t entry, + uint64_t text_seg_size, uint64_t data_seg_size) { /* The header segment is a segment which holds the elf and program headers. @@ -445,15 +449,11 @@ Vec create_elf64_x86_header(Allocator &al, X86Assembler &a) { const int HEADER_SEGMENT_SIZE = 0x1000; // adjust/offset the origin address as per the extra bytes of HEADER_SEGMENT_SIZE - uint64_t origin_addr = a.origin() - HEADER_SEGMENT_SIZE; + uint64_t origin_addr = origin - HEADER_SEGMENT_SIZE; - Elf64_Ehdr e = get_elf_header(a.get_defined_symbol("_start").value); + Elf64_Ehdr e = get_elf_header(entry); Elf64_Phdr p_program = get_seg_header(4, origin_addr, HEADER_SEGMENT_SIZE, 0, 0); - - const uint64_t text_seg_size = a.get_defined_symbol("text_segment_end").value - a.get_defined_symbol("text_segment_start").value; Elf64_Phdr p_text_seg = get_seg_header(5, origin_addr, text_seg_size, p_program.offset, p_program.filesz); - - const uint64_t data_seg_size = a.get_defined_symbol("data_segment_end").value - a.get_defined_symbol("data_segment_start").value; Elf64_Phdr p_data_seg = get_seg_header(6, origin_addr, data_seg_size, p_text_seg.offset, p_text_seg.filesz); Vec header; diff --git a/src/libasr/codegen/x86_assembler.h b/src/libasr/codegen/x86_assembler.h index f89ef01169..bf722c668a 100644 --- a/src/libasr/codegen/x86_assembler.h +++ b/src/libasr/codegen/x86_assembler.h @@ -554,6 +554,10 @@ R"(BITS 64 EMIT("\n\talign " + std::to_string(alignment) + ", db 0"); } + uint64_t compute_seg_size(std::string start_flag, std::string end_flag) { + return get_defined_symbol(end_flag).value - get_defined_symbol(start_flag).value; + } + void define_symbol(const std::string &name, uint32_t value) { if (m_symbols.find(name) == m_symbols.end()) { Symbol s; @@ -1627,7 +1631,8 @@ void emit_print_float(X86Assembler &a, const std::string &name); template void append_header_bytes(Allocator &al, T src, Vec &des); void align_by_byte(Allocator &al, Vec &code, uint64_t alignment); -Vec create_elf64_x86_header(Allocator &al, X86Assembler &a); +Vec create_elf64_x86_header(Allocator &al, uint64_t origin, uint64_t entry, + uint64_t text_seg_size, uint64_t data_seg_size); void emit_print_64(X86Assembler &a, const std::string &msg_label, uint64_t size); void emit_print_int_64(X86Assembler &a, const std::string &name);