llvm-project/llvm/lib/MC/MCSection.cpp
Fangrui Song b22e22ebfa MC: Allocate initial fragment and define section symbol in changeSection
Reland #150574 with a MCStreamer::changeSection change:
In Mach-O, DWARF sections use Begin as a temporary label, requiring a label
definition, unlike section symbols in other file formats.
(Tested by dec978036ef1037753e7de5b78c978e71c49217b)

---

13a79bbfe583e1d8cc85d241b580907260065eb8 (2017) introduced fragment
creation in MCContext for createELFSectionImpl, which was inappropriate.
Fragments should only be created when using MCSteramer, not during
`MCContext::get*Section` calls.
`initMachOMCObjectFileInfo` defines multiple sections, some of which may
not be used by the code generator. This caused symbol names matching
these sections to be incorrectly marked as undefined (see
https://reviews.llvm.org/D55173).

The fragment code was later replicated in other file formats, such as
WebAssembly (see https://reviews.llvm.org/D46561), XCOFF, and GOFF.

This patch fixes the problem by moving initial fragment allocation from
MCContext::createSection to MCStreamer::changeSection.
While MCContext still creates a section symbol, the symbol is not
attached to the initial fragment. In addition,

* Move `emitLabel`/`setFragment` from `switchSection*` and
  overridden changeSection to `MCObjectStreamer::changeSection` for
  consistency.
* De-virtualize `switchSectionNoPrint`.
* test/CodeGen/XCore/section-name.ll now passes. XCore doesn't support
  MCObjectStreamer. I don't think the MCAsmStreamer output behavior
  change matters.

Pull Request: https://github.com/llvm/llvm-project/pull/150574
2025-07-26 00:05:10 -07:00

101 lines
3.3 KiB
C++

//===- lib/MC/MCSection.cpp - Machine Code Section Representation ---------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/MC/MCSection.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include <utility>
using namespace llvm;
MCSection::MCSection(SectionVariant V, StringRef Name, bool IsText, bool IsBss,
MCSymbol *Begin)
: Begin(Begin), HasInstructions(false), IsRegistered(false), IsText(IsText),
IsBss(IsBss), LinkerRelaxable(false), Name(Name), Variant(V) {
DummyFragment.setParent(this);
}
MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) {
if (!End)
End = Ctx.createTempSymbol("sec_end");
return End;
}
bool MCSection::hasEnded() const { return End && End->isInSection(); }
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void MCSection::dump(
DenseMap<const MCFragment *, SmallVector<const MCSymbol *, 0>> *FragToSyms)
const {
raw_ostream &OS = errs();
OS << "MCSection Name:" << getName();
for (auto &F : *this) {
OS << '\n';
F.dump();
if (!FragToSyms)
continue;
auto It = FragToSyms->find(&F);
if (It == FragToSyms->end())
continue;
for (auto *Sym : It->second) {
OS << "\n Symbol @" << Sym->getOffset() << ' ' << Sym->getName();
if (Sym->isTemporary())
OS << " Temporary";
}
}
}
#endif
void MCFragment::setVarContents(ArrayRef<char> Contents) {
auto &S = getParent()->ContentStorage;
if (VarContentStart + Contents.size() > VarContentEnd) {
VarContentStart = S.size();
S.resize_for_overwrite(S.size() + Contents.size());
}
VarContentEnd = VarContentStart + Contents.size();
llvm::copy(Contents, S.begin() + VarContentStart);
}
void MCFragment::addFixup(MCFixup Fixup) { appendFixups({Fixup}); }
void MCFragment::appendFixups(ArrayRef<MCFixup> Fixups) {
auto &S = getParent()->FixupStorage;
if (LLVM_UNLIKELY(FixupEnd != S.size())) {
// Move the elements to the end. Reserve space to avoid invalidating
// S.begin()+I for `append`.
auto Size = FixupEnd - FixupStart;
auto I = std::exchange(FixupStart, S.size());
S.reserve(S.size() + Size);
S.append(S.begin() + I, S.begin() + I + Size);
}
S.append(Fixups.begin(), Fixups.end());
FixupEnd = S.size();
}
void MCFragment::setVarFixups(ArrayRef<MCFixup> Fixups) {
auto &S = getParent()->FixupStorage;
if (VarFixupStart + Fixups.size() > VarFixupEnd) {
VarFixupStart = S.size();
S.resize_for_overwrite(S.size() + Fixups.size());
}
VarFixupEnd = VarFixupStart + Fixups.size();
// Source fixup offsets are relative to the variable part's start. Add the
// fixed part size to make them relative to the fixed part's start.
std::transform(Fixups.begin(), Fixups.end(), S.begin() + VarFixupStart,
[Fixed = getFixedSize()](MCFixup F) {
F.setOffset(Fixed + F.getOffset());
return F;
});
}