void computeSectionAddresses(const MCAssembler &Asm,
const MCAsmLayout &Layout);
+ void markAbsoluteVariableSymbols(MCAssembler &Asm,
+ const MCAsmLayout &Layout);
void ExecutePostLayoutBinding(MCAssembler &Asm,
const MCAsmLayout &Layout) override;
#define LLVM_MC_MCSYMBOL_H
#include "llvm/ADT/StringRef.h"
-#include "llvm/MC/MCExpr.h"
#include "llvm/Support/Compiler.h"
namespace llvm {
/// Section - The section the symbol is defined in. This is null for
/// undefined symbols, and the special AbsolutePseudoSection value for
- /// absolute symbols. If this is a variable symbol, this caches the
- /// variable value's section.
- mutable const MCSection *Section;
+ /// absolute symbols.
+ const MCSection *Section;
/// Value - If non-null, the value for a variable symbol.
const MCExpr *Value;
MCSymbol(const MCSymbol&) = delete;
void operator=(const MCSymbol&) = delete;
- const MCSection *getSectionPtr() const {
- if (Section || !Value)
- return Section;
- return Section = Value->FindAssociatedSection();
- }
-
public:
/// getName - Get the symbol name.
StringRef getName() const { return Name; }
///
/// Defined symbols are either absolute or in some section.
bool isDefined() const {
- return getSectionPtr() != nullptr;
+ return Section != nullptr;
}
/// isInSection - Check if this symbol is defined in some section (i.e., it
/// isAbsolute - Check if this is an absolute symbol.
bool isAbsolute() const {
- return getSectionPtr() == AbsolutePseudoSection;
+ return Section == AbsolutePseudoSection;
}
/// getSection - Get the section associated with a defined, non-absolute
/// symbol.
const MCSection &getSection() const {
assert(isInSection() && "Invalid accessor!");
- return *getSectionPtr();
+ return *Section;
}
/// setSection - Mark the symbol as defined in the section \p S.
- void setSection(const MCSection &S) {
- assert(!isVariable() && "Cannot set section of variable");
- Section = &S;
- }
+ void setSection(const MCSection &S) { Section = &S; }
/// setUndefined - Mark the symbol as undefined.
void setUndefined() {
Section = nullptr;
}
+ /// setAbsolute - Mark the symbol as absolute.
+ void setAbsolute() { Section = AbsolutePseudoSection; }
+
/// @}
/// @name Variable Symbols
/// @{
if (RHS_S == MCSymbol::AbsolutePseudoSection)
return LHS_S;
- if (BE->getOpcode() == MCBinaryExpr::Sub)
- return MCSymbol::AbsolutePseudoSection;
-
// Otherwise, return the first non-null section.
return LHS_S ? LHS_S : RHS_S;
}
assert(!IsUsed && "Cannot set a variable that has already been used.");
assert(Value && "Invalid variable value!");
this->Value = Value;
- this->Section = nullptr;
+
+ // Variables should always be marked as in the same "section" as the value.
+ const MCSection *Section = Value->FindAssociatedSection();
+ if (Section)
+ setSection(*Section);
+ else
+ setUndefined();
}
void MCSymbol::print(raw_ostream &OS) const {
}
}
+void MachObjectWriter::markAbsoluteVariableSymbols(MCAssembler &Asm,
+ const MCAsmLayout &Layout) {
+ for (MCSymbolData &SD : Asm.symbols()) {
+ if (!SD.getSymbol().isVariable())
+ continue;
+
+ // Is the variable is a symbol difference (SA - SB + C) expression,
+ // and neither symbol is external, mark the variable as absolute.
+ const MCExpr *Expr = SD.getSymbol().getVariableValue();
+ MCValue Value;
+ if (Expr->EvaluateAsRelocatable(Value, &Layout, nullptr)) {
+ if (Value.getSymA() && Value.getSymB())
+ const_cast<MCSymbol*>(&SD.getSymbol())->setAbsolute();
+ }
+ }
+}
+
void MachObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
const MCAsmLayout &Layout) {
computeSectionAddresses(Asm, Layout);
// Create symbol data for any indirect symbols.
BindIndirectSymbols(Asm);
+
+ // Mark symbol difference expressions in variables (from .set or = directives)
+ // as absolute.
+ markAbsoluteVariableSymbols(Asm, Layout);
}
bool MachObjectWriter::
// must be registered in .sxdata. Use of any unregistered handlers will
// cause the process to terminate immediately. LLVM does not know how to
// register any SEH handlers, so its object files should be safe.
+ S->setAbsolute();
OutStreamer.EmitSymbolAttribute(S, MCSA_Global);
OutStreamer.EmitAssignment(
S, MCConstantExpr::Create(int64_t(1), MMI->getContext()));
+++ /dev/null
-// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | llvm-readobj -r | FileCheck %s
-
-a:
- .section foo
- c = b
-b:
- // CHECK: 0x0 R_X86_64_PC32 .text 0x0
- .long a - c
bar6 = bar5
bar6:
-// Test that indirect local aliases do not appear as symbols.
-.data
-.Llocal:
-
-.text
-leaq .Llocal1(%rip), %rdi
-.Llocal1 = .Llocal2
-.Llocal2 = .Llocal
-
// CHECK: Symbols [
// CHECK-NEXT: Symbol {
// CHECK-NEXT: Name: (0)
// CHECK-NEXT: Value: 0x[[DEFINED_EARLY]]
// CHECK-NEXT: }
- // alias_to_late was an alias to defined_late. But we can resolve it.
+ // defined_late was defined. Just after defined_early.
// CHECK: Symbol {
-// CHECK-NEXT: Name: alias_to_late
+// CHECK-NEXT: Name: defined_late
// CHECK-NEXT: Type: Section (0xE)
// CHECK-NEXT: Section: __data (0x2)
// CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
// CHECK-NEXT: Value: 0x[[DEFINED_LATE:[0-9A-F]+]]
// CHECK-NEXT: }
- // defined_late was defined. Just after defined_early.
+ // alias_to_late was an alias to defined_late. But we can resolve it.
// CHECK: Symbol {
-// CHECK-NEXT: Name: defined_late
+// CHECK-NEXT: Name: alias_to_late
// CHECK-NEXT: Type: Section (0xE)
// CHECK-NEXT: Section: __data (0x2)
// CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
// CHECK: Symbol {
// CHECK-NEXT: Name: alias_to_local (42)
// CHECK-NEXT: Type: Section (0xE)
-// CHECK-NEXT: Section: __data (0x2)
+// CHECK-NEXT: Section: (0x0)
// CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
// CHECK-NEXT: Flags [ (0x0)
// CHECK-NEXT: ]