Skip to content

Commit 14b1197

Browse files
committed
WASM_X64: Create and return only header binary
1 parent f12e42c commit 14b1197

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

src/libasr/codegen/x86_assembler.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
namespace LCompilers {
1212

1313
void X86Assembler::save_binary64(const std::string &filename) {
14-
Vec<uint8_t> bin = create_elf64_x86_binary(m_al, *this);
14+
Vec<uint8_t> header = create_elf64_x86_header(m_al, *this);
1515
{
1616
std::ofstream out;
1717
out.open(filename);
18-
out.write((const char*) bin.p, bin.size());
18+
out.write((const char*) header.p, header.size());
19+
out.write((const char*) m_code.p, m_code.size());
1920
}
2021
#ifdef LFORTRAN_LINUX
2122
std::string mode = "0755";
@@ -424,7 +425,7 @@ void align_by_byte(Allocator &al, Vec<uint8_t> &code, uint64_t alignment) {
424425
}
425426
}
426427

427-
Vec<uint8_t> create_elf64_x86_binary(Allocator &al, X86Assembler &a) {
428+
Vec<uint8_t> create_elf64_x86_header(Allocator &al, X86Assembler &a) {
428429

429430
/*
430431
The header segment is a segment which holds the elf and program headers.
@@ -455,23 +456,19 @@ Vec<uint8_t> create_elf64_x86_binary(Allocator &al, X86Assembler &a) {
455456
const uint64_t data_seg_size = a.get_defined_symbol("data_segment_end").value - a.get_defined_symbol("data_segment_start").value;
456457
Elf64_Phdr p_data_seg = get_seg_header(6, origin_addr, data_seg_size, p_text_seg.offset, p_text_seg.filesz);
457458

458-
Vec<uint8_t> bin;
459-
bin.reserve(al, HEADER_SEGMENT_SIZE);
459+
Vec<uint8_t> header;
460+
header.reserve(al, HEADER_SEGMENT_SIZE);
460461

461462
{
462-
append_header_bytes(al, e, bin);
463-
append_header_bytes(al, p_program, bin);
464-
append_header_bytes(al, p_text_seg, bin);
465-
append_header_bytes(al, p_data_seg, bin);
466-
467-
LCompilers::align_by_byte(al, bin, 0x1000);
468-
}
463+
append_header_bytes(al, e, header);
464+
append_header_bytes(al, p_program, header);
465+
append_header_bytes(al, p_text_seg, header);
466+
append_header_bytes(al, p_data_seg, header);
469467

470-
for (auto b:a.get_machine_code()) {
471-
bin.push_back(al, b);
468+
LCompilers::align_by_byte(al, header, 0x1000);
472469
}
473470

474-
return bin;
471+
return header;
475472
}
476473

477474
void emit_print_64(X86Assembler &a, const std::string &msg_label, uint64_t size)

src/libasr/codegen/x86_assembler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,7 @@ void emit_print_float(X86Assembler &a, const std::string &name);
16271627
template <typename T>
16281628
void append_header_bytes(Allocator &al, T src, Vec<uint8_t> &des);
16291629
void align_by_byte(Allocator &al, Vec<uint8_t> &code, uint64_t alignment);
1630-
Vec<uint8_t> create_elf64_x86_binary(Allocator &al, X86Assembler &a);
1630+
Vec<uint8_t> create_elf64_x86_header(Allocator &al, X86Assembler &a);
16311631

16321632
void emit_print_64(X86Assembler &a, const std::string &msg_label, uint64_t size);
16331633
void emit_print_int_64(X86Assembler &a, const std::string &name);

0 commit comments

Comments
 (0)