From 471c4d4151389db0872a5eb8e51273f3173054b0 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Thu, 8 Dec 2022 18:37:36 -0500 Subject: [PATCH] fixes so omf version 1 support works --- link.cpp | 18 +++++++++++++----- omf.cpp | 9 ++++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/link.cpp b/link.cpp index e274ff9..74603b3 100644 --- a/link.cpp +++ b/link.cpp @@ -798,7 +798,7 @@ void finish(void) { if (lkv == 0) save_bin(path, segments.back()); else - save_omf(path, segments, compress, express); + save_omf(path, segments, compress, express, ver); set_file_type(path, ftype, atype); } catch (std::exception &ex) { @@ -903,7 +903,11 @@ void finish3(void) { push(buffer, omf::opcode::GEQU); push(buffer, sym.name); - push(buffer, static_cast(0x00)); /* length attr */ + if (ver == 1) { + push(buffer, static_cast(0x00)); /* length attr */ + } else { + push(buffer, static_cast(0x00)); /* length attr */ + } push(buffer, static_cast('G')); /* type attr */ push(buffer, static_cast(sym.value)); } else { @@ -969,7 +973,11 @@ void finish3(void) { /* add global record */ push(buffer, omf::opcode::GLOBAL); push(buffer, iter1->second); /* name */ - push(buffer, static_cast(0x00)); /* length attr */ + if (ver == 1) { + push(buffer, static_cast(0x00)); /* length attr */ + } else { + push(buffer, static_cast(0x00)); /* length attr */ + } push(buffer, static_cast('N')); /* type attr */ push(buffer, static_cast(0x00)); /* public */ ++iter1; @@ -1006,7 +1014,7 @@ void finish3(void) { if (iter3 != resolved.end()) throw std::runtime_error("relocation offset error"); - void save_object(const std::string &path, omf::segment &s, uint32_t length); + void save_object(const std::string &path, omf::segment &s, uint32_t length, unsigned version); std::string path = save_file; @@ -1014,7 +1022,7 @@ void finish3(void) { if (verbose) printf("Saving %s\n", path.c_str()); try { - save_object(path, seg, pc); + save_object(path, seg, pc, ver); set_file_type(path, 0xb1, 0x0000); } catch (std::exception &ex) { errx(EX_OSERR, "%s: %s", path.c_str(), ex.what()); diff --git a/omf.cpp b/omf.cpp index 828190e..924df28 100644 --- a/omf.cpp +++ b/omf.cpp @@ -490,7 +490,7 @@ void save_bin(const std::string &path, omf::segment &segment) { close(fd); } -void save_object(const std::string &path, omf::segment &s, uint32_t length) { +void save_object(const std::string &path, omf::segment &s, uint32_t length, unsigned version) { /* data is already in OMF format. */ @@ -521,6 +521,7 @@ void save_object(const std::string &path, omf::segment &s, uint32_t length) { h.bytecount = sizeof(omf_header) + data.size() + s.data.size(); unsigned offset = 0; + if (version == 1) to_v1(h); to_little(h); offset += write(fd, &h, sizeof(h)); offset += write(fd, data.data(), data.size()); @@ -669,6 +670,12 @@ void save_omf(const std::string &path, std::vector &segments, bool to_little(h); offset += write(fd, &h, sizeof(h)); offset += write(fd, data.data(), data.size()); + + // version 1 needs 512-byte padding for all but final segment. + if (version == 1 && &s != &segments.back()) { + static uint8_t zero[512]; + offset += write(fd, zero, 512 - (offset & 511) ); + } } if (expressload) {