Skip to content

Commit cb307a2

Browse files
committed
Make the big switch: Change MCSectionMachO to represent a section *semantically*
instead of syntactically as a string. This means that it keeps track of the segment, section, flags, etc directly and asmprints them in the right format. This also includes parsing and validation support for llvm-mc and "attribute(section)", so we should now start getting errors about invalid section attributes from the compiler instead of the assembler on darwin. Still todo: 1) Uniquing of darwin mcsections 2) Move all the Darwin stuff out to MCSectionMachO.[cpp|h] 3) there are a few FIXMEs, for example what is the syntax to get the S_GB_ZEROFILL segment type? llvm-svn: 78547
1 parent 40268af commit cb307a2

File tree

18 files changed

+727
-285
lines changed

18 files changed

+727
-285
lines changed

llvm/include/llvm/MC/MCSection.h

Lines changed: 141 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,154 @@ namespace llvm {
6767
raw_ostream &OS) const;
6868
};
6969

70+
71+
/// MCSectionMachO - This represents a section on a Mach-O system (used by
72+
/// Mac OS X). On a Mac system, these are also described in
73+
/// /usr/include/mach-o/loader.h.
7074
class MCSectionMachO : public MCSection {
71-
std::string Name;
75+
char SegmentName[16]; // Not necessarily null terminated!
76+
char SectionName[16]; // Not necessarily null terminated!
7277

73-
/// IsDirective - This is true if the section name is a directive, not
74-
/// something that should be printed with ".section".
75-
///
76-
/// FIXME: This is a hack. Switch to a semantic view of the section instead
77-
/// of a syntactic one.
78-
bool IsDirective;
78+
/// TypeAndAttributes - This is the SECTION_TYPE and SECTION_ATTRIBUTES
79+
/// field of a section, drawn from the enums below.
80+
unsigned TypeAndAttributes;
81+
82+
/// Reserved2 - The 'reserved2' field of a section, used to represent the
83+
/// size of stubs, for example.
84+
unsigned Reserved2;
7985

80-
MCSectionMachO(const StringRef &Name, bool IsDirective, SectionKind K,
81-
MCContext &Ctx);
86+
MCSectionMachO(const StringRef &Segment, const StringRef &Section,
87+
unsigned TAA, unsigned reserved2, SectionKind K)
88+
: MCSection(K), TypeAndAttributes(TAA), Reserved2(reserved2) {
89+
assert(Segment.size() <= 16 && Section.size() <= 16 &&
90+
"Segment or section string too long");
91+
for (unsigned i = 0; i != 16; ++i) {
92+
if (i < Segment.size())
93+
SegmentName[i] = Segment[i];
94+
else
95+
SegmentName[i] = 0;
96+
97+
if (i < Section.size())
98+
SectionName[i] = Section[i];
99+
else
100+
SectionName[i] = 0;
101+
}
102+
}
82103
public:
83104

84-
static MCSectionMachO *Create(const StringRef &Name, bool IsDirective,
105+
static MCSectionMachO *Create(const StringRef &Segment,
106+
const StringRef &Section,
107+
unsigned TypeAndAttributes,
108+
unsigned Reserved2,
85109
SectionKind K, MCContext &Ctx);
110+
111+
/// These are the section type and attributes fields. A MachO section can
112+
/// have only one Type, but can have any of the attributes specified.
113+
enum {
114+
// TypeAndAttributes bitmasks.
115+
SECTION_TYPE = 0x000000FFU,
116+
SECTION_ATTRIBUTES = 0xFFFFFF00U,
117+
118+
// Valid section types.
119+
120+
/// S_REGULAR - Regular section.
121+
S_REGULAR = 0x00U,
122+
/// S_ZEROFILL - Zero fill on demand section.
123+
S_ZEROFILL = 0x01U,
124+
/// S_CSTRING_LITERALS - Section with literal C strings.
125+
S_CSTRING_LITERALS = 0x02U,
126+
/// S_4BYTE_LITERALS - Section with 4 byte literals.
127+
S_4BYTE_LITERALS = 0x03U,
128+
/// S_8BYTE_LITERALS - Section with 8 byte literals.
129+
S_8BYTE_LITERALS = 0x04U,
130+
/// S_LITERAL_POINTERS - Section with pointers to literals.
131+
S_LITERAL_POINTERS = 0x05U,
132+
/// S_NON_LAZY_SYMBOL_POINTERS - Section with non-lazy symbol pointers.
133+
S_NON_LAZY_SYMBOL_POINTERS = 0x06U,
134+
/// S_LAZY_SYMBOL_POINTERS - Section with lazy symbol pointers.
135+
S_LAZY_SYMBOL_POINTERS = 0x07U,
136+
/// S_SYMBOL_STUBS - Section with symbol stubs, byte size of stub in
137+
/// the Reserved2 field.
138+
S_SYMBOL_STUBS = 0x08U,
139+
/// S_SYMBOL_STUBS - Section with only function pointers for
140+
/// initialization.
141+
S_MOD_INIT_FUNC_POINTERS = 0x09U,
142+
/// S_MOD_INIT_FUNC_POINTERS - Section with only function pointers for
143+
/// termination.
144+
S_MOD_TERM_FUNC_POINTERS = 0x0AU,
145+
/// S_COALESCED - Section contains symbols that are to be coalesced.
146+
S_COALESCED = 0x0BU,
147+
/// S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4
148+
/// gigabytes).
149+
S_GB_ZEROFILL = 0x0CU,
150+
/// S_INTERPOSING - Section with only pairs of function pointers for
151+
/// interposing.
152+
S_INTERPOSING = 0x0DU,
153+
/// S_16BYTE_LITERALS - Section with only 16 byte literals.
154+
S_16BYTE_LITERALS = 0x0EU,
155+
/// S_DTRACE_DOF - Section contains DTrace Object Format.
156+
S_DTRACE_DOF = 0x0FU,
157+
/// S_LAZY_DYLIB_SYMBOL_POINTERS - Section with lazy symbol pointers to
158+
/// lazy loaded dylibs.
159+
S_LAZY_DYLIB_SYMBOL_POINTERS = 0x10U,
86160

87-
const std::string &getName() const { return Name; }
88-
bool isDirective() const { return IsDirective; }
161+
LAST_KNOWN_SECTION_TYPE = S_LAZY_DYLIB_SYMBOL_POINTERS,
162+
163+
164+
// Valid section attributes.
165+
166+
/// S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine
167+
/// instructions.
168+
S_ATTR_PURE_INSTRUCTIONS = 1U << 31,
169+
/// S_ATTR_NO_TOC - Section contains coalesced symbols that are not to be
170+
/// in a ranlib table of contents.
171+
S_ATTR_NO_TOC = 1U << 30,
172+
/// S_ATTR_STRIP_STATIC_SYMS - Ok to strip static symbols in this section
173+
/// in files with the MY_DYLDLINK flag.
174+
S_ATTR_STRIP_STATIC_SYMS = 1U << 29,
175+
/// S_ATTR_NO_DEAD_STRIP - No dead stripping.
176+
S_ATTR_NO_DEAD_STRIP = 1U << 28,
177+
/// S_ATTR_LIVE_SUPPORT - Blocks are live if they reference live blocks.
178+
S_ATTR_LIVE_SUPPORT = 1U << 27,
179+
/// S_ATTR_SELF_MODIFYING_CODE - Used with i386 code stubs written on by
180+
/// dyld.
181+
S_ATTR_SELF_MODIFYING_CODE = 1U << 26,
182+
/// S_ATTR_DEBUG - A debug section.
183+
S_ATTR_DEBUG = 1U << 25,
184+
/// S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
185+
S_ATTR_SOME_INSTRUCTIONS = 1U << 10,
186+
/// S_ATTR_EXT_RELOC - Section has external relocation entries.
187+
S_ATTR_EXT_RELOC = 1U << 9,
188+
/// S_ATTR_LOC_RELOC - Section has local relocation entries.
189+
S_ATTR_LOC_RELOC = 1U << 8
190+
};
191+
192+
StringRef getSegmentName() const {
193+
// SegmentName is not necessarily null terminated!
194+
if (SegmentName[15])
195+
return StringRef(SegmentName, 16);
196+
return StringRef(SegmentName);
197+
}
198+
StringRef getSectionName() const {
199+
// SectionName is not necessarily null terminated!
200+
if (SectionName[15])
201+
return StringRef(SectionName, 16);
202+
return StringRef(SectionName);
203+
}
204+
205+
unsigned getTypeAndAttributes() const { return TypeAndAttributes; }
206+
207+
208+
/// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
209+
/// This is a string that can appear after a .section directive in a mach-o
210+
/// flavored .s file. If successful, this fills in the specified Out
211+
/// parameters and returns an empty string. When an invalid section
212+
/// specifier is present, this returns a string indicating the problem.
213+
static std::string ParseSectionSpecifier(StringRef Spec, // In.
214+
StringRef &Segment, // Out.
215+
StringRef &Section, // Out.
216+
unsigned &TAA, // Out.
217+
unsigned &StubSize); // Out.
89218

90219
virtual void PrintSwitchToSection(const TargetAsmInfo &TAI,
91220
raw_ostream &OS) const;

llvm/include/llvm/Target/TargetLoweringObjectFile.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,30 @@ class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
257257
Mangler *) const;
258258

259259
/// getMachOSection - Return the MCSection for the specified mach-o section.
260-
/// FIXME: Switch this to a semantic view eventually.
261-
const MCSection *getMachOSection(const char *Name, bool isDirective,
262-
SectionKind K) const;
260+
/// This requires the operands to be valid.
261+
const MCSection *getMachOSection(StringRef Segment, StringRef Section,
262+
unsigned TypeAndAttributes,
263+
SectionKind K) const {
264+
return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
265+
}
266+
const MCSection *getMachOSection(StringRef Segment, StringRef Section,
267+
unsigned TypeAndAttributes,
268+
unsigned Reserved2, SectionKind K) const;
269+
270+
/// getTextCoalSection - Return the "__TEXT,__textcoal_nt" section we put weak
271+
/// symbols into.
272+
const MCSection *getTextCoalSection() const {
273+
return TextCoalSection;
274+
}
275+
276+
/// getLazySymbolPointerSection - Return the section corresponding to
277+
/// the .lazy_symbol_pointer directive.
278+
const MCSection *getLazySymbolPointerSection() const;
279+
280+
/// getNonLazySymbolPointerSection - Return the section corresponding to
281+
/// the .non_lazy_symbol_pointer directive.
282+
const MCSection *getNonLazySymbolPointerSection() const;
283+
263284
};
264285

265286

llvm/lib/MC/MCAsmStreamer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
222222
OS << ".zerofill ";
223223

224224
// This is a mach-o specific directive.
225-
OS << '"' << ((MCSectionMachO*)Section)->getName() << '"';
225+
const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
226+
OS << '"' << MOSection->getSegmentName() << ","
227+
<< MOSection->getSectionName() << '"';
226228

227229

228230
if (Symbol != NULL) {

0 commit comments

Comments
 (0)