MCSectionELF.cpp revision 766f25306af343fb2784350cb4d8cd9ca180f0d3
1//===- lib/MC/MCSectionELF.cpp - ELF Code Section Representation ----------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/MC/MCSectionELF.h"
11#include "llvm/MC/MCAsmInfo.h"
12#include "llvm/MC/MCContext.h"
13#include "llvm/MC/MCExpr.h"
14#include "llvm/MC/MCSymbol.h"
15#include "llvm/Support/ELF.h"
16#include "llvm/Support/raw_ostream.h"
17
18using namespace llvm;
19
20MCSectionELF::~MCSectionELF() {} // anchor.
21
22// ShouldOmitSectionDirective - Decides whether a '.section' directive
23// should be printed before the section name
24bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
25                                              const MCAsmInfo &MAI) const {
26
27  // FIXME: Does .section .bss/.data/.text work everywhere??
28  if (Name == ".text" || Name == ".data" ||
29      (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
30    return true;
31
32  return false;
33}
34
35void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
36                                        raw_ostream &OS,
37                                        const MCExpr *Subsection) const {
38
39  if (ShouldOmitSectionDirective(SectionName, MAI)) {
40    OS << '\t' << getSectionName();
41    if (Subsection)
42      OS << '\t' << *Subsection;
43    OS << '\n';
44    return;
45  }
46
47  StringRef name = getSectionName();
48  if (name.find_first_not_of("0123456789_."
49                             "abcdefghijklmnopqrstuvwxyz"
50                             "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == name.npos) {
51    OS << "\t.section\t" << name;
52  } else {
53    OS << "\t.section\t\"";
54    for (const char *b = name.begin(), *e = name.end(); b < e; ++b) {
55      if (*b == '"') // Unquoted "
56        OS << "\\\"";
57      else if (*b != '\\') // Neither " or backslash
58        OS << *b;
59      else if (b + 1 == e) // Trailing backslash
60        OS << "\\\\";
61      else {
62        OS << b[0] << b[1]; // Quoted character
63        ++b;
64      }
65    }
66    OS << '"';
67  }
68
69  // Handle the weird solaris syntax if desired.
70  if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
71      !(Flags & ELF::SHF_MERGE)) {
72    if (Flags & ELF::SHF_ALLOC)
73      OS << ",#alloc";
74    if (Flags & ELF::SHF_EXECINSTR)
75      OS << ",#execinstr";
76    if (Flags & ELF::SHF_WRITE)
77      OS << ",#write";
78    if (Flags & ELF::SHF_EXCLUDE)
79      OS << ",#exclude";
80    if (Flags & ELF::SHF_TLS)
81      OS << ",#tls";
82    OS << '\n';
83    return;
84  }
85
86  OS << ",\"";
87  if (Flags & ELF::SHF_ALLOC)
88    OS << 'a';
89  if (Flags & ELF::SHF_EXCLUDE)
90    OS << 'e';
91  if (Flags & ELF::SHF_EXECINSTR)
92    OS << 'x';
93  if (Flags & ELF::SHF_GROUP)
94    OS << 'G';
95  if (Flags & ELF::SHF_WRITE)
96    OS << 'w';
97  if (Flags & ELF::SHF_MERGE)
98    OS << 'M';
99  if (Flags & ELF::SHF_STRINGS)
100    OS << 'S';
101  if (Flags & ELF::SHF_TLS)
102    OS << 'T';
103
104  // If there are target-specific flags, print them.
105  if (Flags & ELF::XCORE_SHF_CP_SECTION)
106    OS << 'c';
107  if (Flags & ELF::XCORE_SHF_DP_SECTION)
108    OS << 'd';
109
110  OS << '"';
111
112  OS << ',';
113
114  // If comment string is '@', e.g. as on ARM - use '%' instead
115  if (MAI.getCommentString()[0] == '@')
116    OS << '%';
117  else
118    OS << '@';
119
120  if (Type == ELF::SHT_INIT_ARRAY)
121    OS << "init_array";
122  else if (Type == ELF::SHT_FINI_ARRAY)
123    OS << "fini_array";
124  else if (Type == ELF::SHT_PREINIT_ARRAY)
125    OS << "preinit_array";
126  else if (Type == ELF::SHT_NOBITS)
127    OS << "nobits";
128  else if (Type == ELF::SHT_NOTE)
129    OS << "note";
130  else if (Type == ELF::SHT_PROGBITS)
131    OS << "progbits";
132
133  if (EntrySize) {
134    assert(Flags & ELF::SHF_MERGE);
135    OS << "," << EntrySize;
136  }
137
138  if (Flags & ELF::SHF_GROUP)
139    OS << "," << Group->getName() << ",comdat";
140  OS << '\n';
141
142  if (Subsection)
143    OS << "\t.subsection\t" << *Subsection << '\n';
144}
145
146bool MCSectionELF::UseCodeAlign() const {
147  return getFlags() & ELF::SHF_EXECINSTR;
148}
149
150bool MCSectionELF::isVirtualSection() const {
151  return getType() == ELF::SHT_NOBITS;
152}
153
154unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) {
155  if (Kind.isMergeable1ByteCString()) return 1;
156  if (Kind.isMergeable2ByteCString()) return 2;
157  if (Kind.isMergeable4ByteCString()) return 4;
158  if (Kind.isMergeableConst4())       return 4;
159  if (Kind.isMergeableConst8())       return 8;
160  if (Kind.isMergeableConst16())      return 16;
161  return 0;
162}
163