Skip to content

Commit

Permalink
symbol: Add Alias(), for dyn+static copies of symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonKagstrom committed Jul 10, 2024
1 parent 8e3ba07 commit a2e8a89
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
6 changes: 6 additions & 0 deletions include/emilpro/i_symbol.hh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public:

virtual std::span<const std::reference_wrapper<IInstruction>> Instructions() const = 0;

/**
* @brief A pointer to an alias for this symbol (dynamic + static for example)
*
* @return the other symbol, or nullptr for none
*/
virtual const ISymbol* Alias() const = 0;

virtual void WaitForCommit() = 0;
};
Expand Down
1 change: 1 addition & 0 deletions include/emilpro/mock/mock_symbol.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public:
MAKE_CONST_MOCK0(Offset, uint64_t(), final);
MAKE_CONST_MOCK0(Size, size_t(), final);
MAKE_CONST_MOCK0(Section, (const ISection&)(), final);
MAKE_CONST_MOCK0(Alias, (const ISymbol*)(), final);
MAKE_CONST_MOCK0(Data, std::span<const std::byte>(), final);
MAKE_CONST_MOCK0(Flags, const std::string&(), final);
MAKE_CONST_MOCK0(Instructions,
Expand Down
4 changes: 2 additions & 2 deletions qt/emilpro/mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ MainWindow::UpdateRefersToView(const emilpro::ISymbol& symbol)
{
m_refers_to_view_model->removeRows(0, m_refers_to_view_model->rowCount());

auto symbol_refs = symbol.RefersTo();
auto symbol_refs = symbol.Alias()->RefersTo();
for (const auto& ref : symbol_refs)
{
auto section = ref.section;
Expand Down Expand Up @@ -700,7 +700,7 @@ MainWindow::UpdateReferredByView(const emilpro::ISymbol& symbol)
{
m_referred_by_view_model->removeRows(0, m_referred_by_view_model->rowCount());

auto symbol_refs = symbol.ReferredBy();
auto symbol_refs = symbol.Alias()->ReferredBy();
for (const auto& ref : symbol_refs)
{
auto section = ref.section;
Expand Down
11 changes: 10 additions & 1 deletion src/section/section.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,16 @@ Section::FixupSymbolSizes()
{
const auto& symbols = it->second;

if (symbols.empty())
{
continue;
}

auto adjust = last_offset;
for (auto* symbol : symbols)

// Use the last symbol as alias for all symbols (including itself)
auto alias = symbols.back();
for (auto symbol : symbols)
{
if (int64_t size = adjust - symbol->Offset(); size >= 0)
{
Expand All @@ -93,6 +101,7 @@ Section::FixupSymbolSizes()

last_offset = symbol->Offset();
m_symbol_refs.push_back(*symbol);
symbol->SetAlias(alias);
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/symbol/include/symbol.hh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public:
std::span<const std::reference_wrapper<IInstruction>> Instructions() const final;
std::span<const IInstruction::Referer> ReferredBy() const final;
std::span<const IInstruction::Referer> RefersTo() const final;
const ISymbol* Alias() const final;

void WaitForCommit() final;

Expand All @@ -40,6 +41,10 @@ public:
void AddReferredBy(std::span<const IInstruction::Referer> referers);
void AddRefersTo(const IInstruction::Referer& referer);

void SetAlias(Symbol* alias);

Symbol *DoGetAlias();

// Called when all referrers/referred by are done
void Commit();

Expand All @@ -61,6 +66,7 @@ private:

std::vector<IInstruction::Referer> m_referred_by_store;
std::vector<IInstruction::Referer> m_refers_to_store;
Symbol *m_alias;

mutable std::mutex m_mutex;

Expand Down
19 changes: 19 additions & 0 deletions src/symbol/symbol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Symbol::Symbol(const ISection& section,
, m_flags(flags)
, m_name(name)
, m_data(section.Data().subspan(offset))
, m_alias(this)
{
// Use what c++filt uses...
int demangle_flags = DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE;
Expand Down Expand Up @@ -122,6 +123,12 @@ Symbol::RefersTo() const
return m_refers_to;
}

const ISymbol*
Symbol::Alias() const
{
return m_alias;
}

std::vector<std::reference_wrapper<IInstruction>>&
Symbol::InstructionsStore()
{
Expand All @@ -143,6 +150,18 @@ Symbol::AddRefersTo(const IInstruction::Referer& referer)
m_refers_to_store.emplace_back(referer);
}

void
Symbol::SetAlias(Symbol* alias)
{
m_alias = alias;
}

Symbol*
Symbol::DoGetAlias()
{
return m_alias;
}

void
Symbol::SetInstructions(std::span<const std::reference_wrapper<IInstruction>> instructions)
{
Expand Down

0 comments on commit a2e8a89

Please sign in to comment.