Skip to content

Commit

Permalink
Account for non-zero starting address when recording source mappings
Browse files Browse the repository at this point in the history
Fixes #355
  • Loading branch information
mortbopet committed Apr 2, 2024
1 parent b71f0dd commit 027e678
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/assembler/assembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,12 @@ class Assembler : public AssemblerBase {
for (const auto &line : tokenizedLines) {
// Get offset of currently emitting position in memory relative to section
// position
VInt addr_offset = currentSection->data.size();
VInt rel_addr_offset = currentSection->data.size();
for (const auto &s : line.symbols) {
// Record symbol position as its absolute address in memory
auto res = m_symbolMap.addSymbol(
line, s,
addr_offset + program.sections.at(m_currentSection).address);
rel_addr_offset + program.sections.at(m_currentSection).address);
if (res) {
errors.push_back(res.value());
continue;
Expand All @@ -388,17 +388,21 @@ class Assembler : public AssemblerBase {
// Currently emitting segment may have changed during the assembler
// directive; refresh state
currentSection = &program.sections.at(m_currentSection);
addr_offset = currentSection->data.size();
rel_addr_offset = currentSection->data.size();
if (!wasDirective) {
/// Maintain a pointer to the instruction that was assembled.
std::shared_ptr<InstructionBase> assembledWith;
runOperation(machineCode, assembleInstruction, line, assembledWith);
assert(assembledWith && "Expected the assembler instruction to be set");
program.sourceMapping[addr_offset].insert(line.sourceLine());

// Update source mapping - this uses the absolute address of the
// instruction.
VInt abs_addr_offset = rel_addr_offset + currentSection->address;
program.sourceMapping[abs_addr_offset].insert(line.sourceLine());

if (!machineCode.linksWithSymbol.symbol.isEmpty()) {
LinkRequest req(line.sourceLine());
req.offset = addr_offset;
req.offset = rel_addr_offset;
req.fieldRequest = machineCode.linksWithSymbol;
req.section = m_currentSection;
req.instrAlignment = m_isa->instrByteAlignment();
Expand All @@ -409,7 +413,7 @@ class Assembler : public AssemblerBase {
/// Instructions should always be emitted on an aligned boundary wrt.
/// their size.
const unsigned alignmentDiff =
addr_offset % (m_isa->instrByteAlignment());
rel_addr_offset % (m_isa->instrByteAlignment());
if (alignmentDiff != 0) {
errors.push_back(
{line.sourceLine(),
Expand Down

0 comments on commit 027e678

Please sign in to comment.