Files
faumaray bba5c8a39a initial commit
Signed-off-by: Aleksey Selihov <faumaray@gmail.com>
2026-05-22 02:40:32 +03:00

144 lines
5.9 KiB
C++

#include "merge.hpp"
#include "format/vlc_format.hpp"
#include <sstream>
#include <unordered_map>
namespace voidlibc::voidlink {
namespace {
constexpr std::uint64_t align_up(std::uint64_t v, std::uint64_t a) {
return (v + (a - 1)) & ~(a - 1);
}
} // namespace
std::variant<LoweredText, std::string>
merge_lowered(std::vector<LoweredText> objects) {
if (objects.empty())
return std::string("merge_lowered: no objects to merge");
if (objects.size() == 1)
return std::move(objects[0]);
using format::kSymUndefined;
const std::size_t N = objects.size();
LoweredText merged{};
merged.section.target_os = objects[0].section.target_os;
merged.section.entry_offset = 0;
// --- Step 1: Compute blob starts and build merged code/data blobs ---
std::vector<std::uint64_t> code_starts(N), data_starts(N);
for (std::size_t oi = 0; oi < N; ++oi) {
const auto& sec = objects[oi].section;
code_starts[oi] =
align_up(static_cast<std::uint64_t>(merged.section.code.size()), 16);
merged.section.code.resize(static_cast<std::size_t>(code_starts[oi]), 0);
merged.section.code.insert(merged.section.code.end(),
sec.code.begin(), sec.code.end());
data_starts[oi] =
align_up(static_cast<std::uint64_t>(merged.section.data.size()), 8);
merged.section.data.resize(static_cast<std::size_t>(data_starts[oi]), 0);
merged.section.data.insert(merged.section.data.end(),
sec.data.begin(), sec.data.end());
}
// --- Step 2a: Intern all defined symbols from all objects ---
// Named defined symbols are deduplicated (first strong definition wins).
// Anonymous defined symbols (empty name, e.g. section symbols) are always unique.
std::unordered_map<std::string, std::uint32_t> name_to_global;
// Per-object local sym_id → global sym_id map.
std::vector<std::unordered_map<std::uint32_t, std::uint32_t>> l2g(N);
for (std::size_t oi = 0; oi < N; ++oi) {
const auto& syms = objects[oi].symbols;
for (std::uint32_t li = 0; li < static_cast<std::uint32_t>(syms.size()); ++li) {
const auto& sym = syms[li];
if (sym.section_index == kSymUndefined) continue; // imports handled below
std::uint32_t gid;
// Local symbols (not exported, not weak) are never deduplicated
// across objects — GCC emits many per-TU local names like .LC0,
// .LC1 that are distinct even when identically spelled.
const bool is_local = !sym.exported && !sym.weak;
if (sym.name.empty() || is_local) {
// Anonymous or local: always a new unique entry.
gid = static_cast<std::uint32_t>(merged.symbols.size());
} else {
auto it = name_to_global.find(sym.name);
if (it != name_to_global.end()) {
// Duplicate name: strong+strong = error; weak is overridden by strong.
const auto& existing = merged.symbols[it->second];
if (!sym.weak && !existing.weak) {
return "merge_lowered: duplicate strong definition of '" + sym.name + "'";
}
if (!sym.weak && existing.weak) {
// New strong definition overrides weak — update in place.
auto& ms = merged.symbols[it->second];
ms = sym;
ms.value += sym.data_section ? data_starts[oi] : code_starts[oi];
}
// If existing is strong and new is weak, keep existing.
l2g[oi][li] = it->second;
continue;
}
gid = static_cast<std::uint32_t>(merged.symbols.size());
name_to_global[sym.name] = gid;
}
SymbolSpec adjusted = sym;
adjusted.value += sym.data_section ? data_starts[oi] : code_starts[oi];
merged.symbols.push_back(std::move(adjusted));
l2g[oi][li] = gid;
}
}
// --- Step 2b: Intern all imports, resolving cross-object references ---
for (std::size_t oi = 0; oi < N; ++oi) {
const auto& syms = objects[oi].symbols;
for (std::uint32_t li = 0; li < static_cast<std::uint32_t>(syms.size()); ++li) {
const auto& sym = syms[li];
if (sym.section_index != kSymUndefined) continue; // defined, already handled
if (l2g[oi].count(li)) continue; // already mapped (shouldn't happen)
auto it = name_to_global.find(sym.name);
if (it != name_to_global.end()) {
// Resolved internally — maps to the defining global entry.
l2g[oi][li] = it->second;
} else {
// Genuinely external import — add once.
std::uint32_t gid = static_cast<std::uint32_t>(merged.symbols.size());
name_to_global[sym.name] = gid;
merged.symbols.push_back(sym); // section_index remains kSymUndefined
l2g[oi][li] = gid;
}
}
}
// --- Step 3: Merge relocs with adjusted offsets and global sym_ids ---
for (std::size_t oi = 0; oi < N; ++oi) {
for (auto r : objects[oi].section.relocs) {
r.dst_offset += static_cast<std::uint32_t>(code_starts[oi]);
auto it = l2g[oi].find(r.sym_id);
if (it == l2g[oi].end()) {
std::ostringstream os;
os << "merge_lowered: object " << oi
<< " reloc at 0x" << std::hex << (r.dst_offset - code_starts[oi])
<< " references unmapped sym_id " << std::dec << r.sym_id;
return os.str();
}
r.sym_id = it->second;
merged.section.relocs.push_back(r);
}
}
return std::move(merged);
}
} // namespace voidlibc::voidlink