From 43021ba28dfb03d2cebd89d1e71cae227203bd38 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Thu, 13 Apr 2023 19:55:45 +0530 Subject: [PATCH] tmp --- src/libasr/codegen/x86_assembler.cpp | 18 +++++++++--------- src/libasr/codegen/x86_assembler.h | 5 ++--- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/libasr/codegen/x86_assembler.cpp b/src/libasr/codegen/x86_assembler.cpp index 44687daf38..eba43756cb 100644 --- a/src/libasr/codegen/x86_assembler.cpp +++ b/src/libasr/codegen/x86_assembler.cpp @@ -410,11 +410,9 @@ void set_data_segment(X86Assembler &a, Elf64_Phdr* p_text_seg, Elf64_Phdr* p) { p->align = 0x1000; } -template -void append_bytes(Allocator &al, T src, Vec &des) { - char *byteArray = (char *)&src; - for (size_t i = 0; i < sizeof(src); i++) { - des.push_back(al, byteArray[i]); +void append_bytes(Allocator &al, Vec& src, Vec &des) { + for (size_t i = 0; i < src.size(); i++) { + des.push_back(al, src[i]); } } @@ -450,16 +448,18 @@ Vec create_elf64_x86_binary(Allocator &al, X86Assembler &a) { const int PROGRAM_HEADER_SIZE = binary.size(); + e->entry = a.get_defined_symbol("_start").value + PROGRAM_HEADER_SIZE; + p_program->filesz = PROGRAM_HEADER_SIZE; p_program->memsz = p_program->filesz; p_text_seg->offset = p_program->offset + p_program->filesz; - p_text_seg->vaddr = a.origin() + p_text_seg->offset; - p_text_seg->paddr = a.origin() + p_text_seg->offset; + p_text_seg->vaddr = a.origin() + p_text_seg->offset + PROGRAM_HEADER_SIZE; + p_text_seg->paddr = p_text_seg->vaddr; p_data_seg->offset = p_text_seg->offset + p_text_seg->filesz; - p_data_seg->vaddr = a.origin() + p_data_seg->offset; - p_data_seg->paddr = a.origin() + p_data_seg->offset; + p_data_seg->vaddr = a.origin() + p_data_seg->offset + PROGRAM_HEADER_SIZE; + p_data_seg->paddr = p_data_seg->vaddr; append_bytes(al, a.get_machine_code(), binary); return binary; diff --git a/src/libasr/codegen/x86_assembler.h b/src/libasr/codegen/x86_assembler.h index c8b7daef91..bddbd8de76 100644 --- a/src/libasr/codegen/x86_assembler.h +++ b/src/libasr/codegen/x86_assembler.h @@ -433,7 +433,7 @@ class X86Assembler { public: X86Assembler(Allocator &al, bool bits64) : m_al{al} { m_code.reserve(m_al, 1024*128); - m_origin = 0x08049000; + m_origin = 0x08048000; #ifdef LFORTRAN_ASM_PRINT if (bits64) { m_asm_code = "BITS 64\n"; @@ -1542,8 +1542,7 @@ void emit_print(X86Assembler &a, const std::string &msg_label, void emit_print_int(X86Assembler &a, const std::string &name); void emit_print_float(X86Assembler &a, const std::string &name); -template -void append_bytes(Allocator &al, T src, Vec &des); +void append_bytes(Allocator &al, Vec& src, Vec &des); void align_by_byte(Allocator &al, Vec &code, uint64_t alignment); Vec create_elf64_x86_binary(Allocator &al, X86Assembler &a);