2009-08-14 03:41:23 +00:00
|
|
|
//===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2010-05-05 19:00:56 +00:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2010-01-05 01:28:10 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-08-14 03:41:23 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2009-08-22 07:22:36 +00:00
|
|
|
// Sentinel value for the absolute pseudo section.
|
|
|
|
const MCSection *MCSymbol::AbsolutePseudoSection =
|
|
|
|
reinterpret_cast<const MCSection *>(1);
|
|
|
|
|
2009-09-13 18:04:46 +00:00
|
|
|
static bool isAcceptableChar(char C) {
|
2013-10-18 02:14:40 +00:00
|
|
|
if ((C < 'a' || C > 'z') &&
|
|
|
|
(C < 'A' || C > 'Z') &&
|
|
|
|
(C < '0' || C > '9') &&
|
|
|
|
C != '_' && C != '$' && C != '.' && C != '@')
|
|
|
|
return false;
|
|
|
|
return true;
|
2009-09-13 18:04:46 +00:00
|
|
|
}
|
|
|
|
|
2012-09-14 14:57:36 +00:00
|
|
|
/// NameNeedsQuoting - Return true if the identifier \p Str needs quotes to be
|
2010-01-17 20:11:03 +00:00
|
|
|
/// syntactically correct.
|
|
|
|
static bool NameNeedsQuoting(StringRef Str) {
|
2009-09-13 18:04:46 +00:00
|
|
|
assert(!Str.empty() && "Cannot create an empty MCSymbol");
|
2012-05-11 01:41:30 +00:00
|
|
|
|
2009-09-03 05:57:47 +00:00
|
|
|
// If any of the characters in the string is an unacceptable character, force
|
|
|
|
// quotes.
|
2009-09-13 18:04:46 +00:00
|
|
|
for (unsigned i = 0, e = Str.size(); i != e; ++i)
|
|
|
|
if (!isAcceptableChar(Str[i]))
|
2009-08-14 03:41:23 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:00:56 +00:00
|
|
|
void MCSymbol::setVariableValue(const MCExpr *Value) {
|
2010-11-15 14:40:36 +00:00
|
|
|
assert(!IsUsed && "Cannot set a variable that has already been used.");
|
2010-05-05 19:00:56 +00:00
|
|
|
assert(Value && "Invalid variable value!");
|
|
|
|
this->Value = Value;
|
MC: For variable symbols, maintain MCSymbol::Section as a cache.
Fixes PR19582.
Previously, when an asm assignment (.set or =) was created, we would look up
the section immediately in MCSymbol::setVariableValue. This caused symbols
to receive the wrong section if the RHS of the assignment had not been seen
yet. This had a knock-on effect in the object file emitters, causing them
to emit extra symbols, or to give symbols the wrong visibility or the wrong
section. For example, in the following asm:
.data
.Llocal:
.text
leaq .Llocal1(%rip), %rdi
.Llocal1 = .Llocal2
.Llocal2 = .Llocal
the first assignment would give .Llocal1 a null section, which would never get
fixed up by the second assignment. This would cause the ELF object file emitter
to consider .Llocal1 to be an undefined symbol and give it external linkage,
even though .Llocal1 should not have been emitted at all in the object file.
Or in the following asm:
alias_to_local = Ltmp0
Ltmp0:
the Mach-O object file emitter would give the alias_to_local symbol a n_type
of N_SECT and a n_sect of 0. This is invalid under the Mach-O specification,
which requires N_SECT symbols to receive a non-zero section number if the
symbol is defined in a section in the object file.
https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/MachORuntime/#//apple_ref/c/tag/nlist
After this change we do not look up the section when the assignment is created,
but instead look it up on demand and store it in Section, which is treated
as a cache if the symbol is a variable symbol.
This change also fixes a bug in MCExpr::FindAssociatedSection. Previously,
if we saw a subtraction, we would return the first referenced section, even in
cases where we should have been returning the absolute pseudo-section. Now we
always return the absolute pseudo-section for expressions that subtract two
section-derived expressions. This isn't always correct (e.g. if one of the
sections ends up being laid out at an absolute address), but it's probably
the best we can do without more context.
This allows us to remove code in two places where we appear to have been
working around this bug, in MachObjectWriter::markAbsoluteVariableSymbols
and in X86AsmPrinter::EmitStartOfAsmFile.
Re-applies r233595 (aka D8586), which was reverted in r233898.
Differential Revision: http://reviews.llvm.org/D8798
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@233995 91177308-0d34-0410-b5e6-96231b3b80d8
2015-04-03 01:46:11 +00:00
|
|
|
this->Section = nullptr;
|
2010-05-05 19:00:56 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 21:43:43 +00:00
|
|
|
void MCSymbol::print(raw_ostream &OS) const {
|
2010-01-17 19:23:46 +00:00
|
|
|
// The name for this MCSymbol is required to be a valid target name. However,
|
|
|
|
// some targets support quoting names with funny characters. If the name
|
|
|
|
// contains a funny character, then print it quoted.
|
2013-11-14 06:05:49 +00:00
|
|
|
StringRef Name = getName();
|
2015-04-23 23:34:51 +00:00
|
|
|
if (!NameNeedsQuoting(Name)) {
|
2013-11-14 06:05:49 +00:00
|
|
|
OS << Name;
|
2009-09-13 18:04:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-05-11 01:41:30 +00:00
|
|
|
|
2013-11-14 06:05:49 +00:00
|
|
|
OS << '"';
|
|
|
|
for (unsigned I = 0, E = Name.size(); I != E; ++I) {
|
|
|
|
char C = Name[I];
|
|
|
|
if (C == '\n')
|
|
|
|
OS << "\\n";
|
|
|
|
else if (C == '"')
|
|
|
|
OS << "\\\"";
|
|
|
|
else
|
|
|
|
OS << C;
|
|
|
|
}
|
|
|
|
OS << '"';
|
2009-08-14 03:41:23 +00:00
|
|
|
}
|
|
|
|
|
2012-09-12 05:06:18 +00:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2009-08-14 03:41:23 +00:00
|
|
|
void MCSymbol::dump() const {
|
2010-01-17 21:43:43 +00:00
|
|
|
print(dbgs());
|
2009-08-14 03:41:23 +00:00
|
|
|
}
|
2012-09-06 19:55:56 +00:00
|
|
|
#endif
|