Skip to content

Commit

Permalink
WASM_X64: Pass only required params to create_elf64_x86_header()
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaikh-Ubaid committed Apr 14, 2023
1 parent 590702a commit 89ea081
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/libasr/codegen/x86_assembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
namespace LCompilers {

void X86Assembler::save_binary64(const std::string &filename) {
Vec<uint8_t> header = create_elf64_x86_header(m_al, *this);
Vec<uint8_t> 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);
Expand Down Expand Up @@ -425,7 +428,8 @@ void align_by_byte(Allocator &al, Vec<uint8_t> &code, uint64_t alignment) {
}
}

Vec<uint8_t> create_elf64_x86_header(Allocator &al, X86Assembler &a) {
Vec<uint8_t> 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.
Expand All @@ -445,15 +449,11 @@ Vec<uint8_t> 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<uint8_t> header;
Expand Down
7 changes: 6 additions & 1 deletion src/libasr/codegen/x86_assembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1627,7 +1631,8 @@ void emit_print_float(X86Assembler &a, const std::string &name);
template <typename T>
void append_header_bytes(Allocator &al, T src, Vec<uint8_t> &des);
void align_by_byte(Allocator &al, Vec<uint8_t> &code, uint64_t alignment);
Vec<uint8_t> create_elf64_x86_header(Allocator &al, X86Assembler &a);
Vec<uint8_t> 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);
Expand Down

0 comments on commit 89ea081

Please sign in to comment.